1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{
config,
lib,
pkgs,
...
}:
let
inherit (lib) types;
settingsFormat = pkgs.formats.toml { };
in
{
options.common.services.oauth2-proxy = lib.mkOption {
description = "Configuration for oauth2-proxy instances.";
default = { };
type = types.attrsOf (
types.submodule {
options = {
enable = (lib.mkEnableOption "oauth2-proxy") // {
default = true;
};
package = lib.mkPackageOption pkgs "oauth2-proxy" { };
environmentFile = lib.mkOption {
type = with lib.types; nullOr path;
default = null;
};
nginx = {
enable = lib.mkEnableOption "";
virtualHost = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
protectedLocations = lib.mkOption {
type = with lib.types; listOf str;
default = [ "/" ];
};
};
settings = lib.mkOption {
default = { };
type = types.submodule {
freeformType = settingsFormat.type;
options = {
proxy_prefix = lib.mkOption {
type = lib.types.str;
default = "/oauth2";
};
ping_path = lib.mkOption {
type = lib.types.str;
default = "/oauth2/ping";
};
trusted_proxy_ips = lib.mkOption {
type = lib.types.str;
default = "::1";
};
email_domains = lib.mkOption {
type = lib.types.str;
default = "*";
};
code_challenge_method = lib.mkOption {
type = lib.types.str;
default = "S256";
};
whitelist_domains = lib.mkOption {
type = with lib.types; listOf str;
default = [ "oidc.fc9f.de" ];
};
};
};
};
};
}
);
};
config = {
assertions = lib.flatten (
lib.flip lib.mapAttrsToList config.common.services.oauth2-proxy (name: cfg: [ ])
);
services.nginx.virtualHosts =
lib.flip lib.mapAttrs'
(lib.filterAttrs (name: value: value.nginx.enable) config.common.services.oauth2-proxy)
(
name: cfg:
lib.nameValuePair cfg.nginx.virtualHost {
locations =
(builtins.listToAttrs (
lib.map (
location:
lib.nameValuePair location {
extraConfig = ''
auth_request ${config.common.services.oauth2-proxy."${name}".settings.proxy_prefix}/auth;
error_page 401 403 =302 $scheme://$host${
config.common.services.oauth2-proxy."${name}".settings.proxy_prefix
}/start?rd=$scheme://$host$request_uri;
'';
}
) config.common.services.oauth2-proxy."${name}".nginx.protectedLocations
))
// {
"${config.common.services.oauth2-proxy.${name}.settings.proxy_prefix}".extraConfig = ''
auth_request off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Auth-Request-Redirect $request_uri;
proxy_pass http://${config.common.services.oauth2-proxy.${name}.settings.http_address};
'';
"= ${config.common.services.oauth2-proxy."${name}".settings.proxy_prefix}/auth".extraConfig = ''
internal;
auth_request off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Uri $request_uri;
proxy_set_header Content-Length "";
proxy_pass_request_body off;
proxy_pass http://${config.common.services.oauth2-proxy."${name}".settings.http_address}${
config.common.services.oauth2-proxy."${name}".settings.proxy_prefix
}/auth;
'';
"= ${config.common.services.oauth2-proxy."${name}".settings.proxy_prefix}/sign_out".extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Auth-Request-Redirect ${
config.common.services.oauth2-proxy.${name}.settings.oidc_issuer_url
}/logout;
proxy_pass http://${config.common.services.oauth2-proxy.${name}.settings.http_address};
'';
"= ${config.common.services.oauth2-proxy."${name}".settings.proxy_prefix}/useerinfo".return = "403";
};
}
);
systemd.services = lib.flip lib.mapAttrs' config.common.services.oauth2-proxy (
name: cfg:
let
unitName = "oauth2-proxy${lib.optionalString (name != "") "-${name}"}";
in
(lib.nameValuePair unitName (
let
settingsSecretsReplaced = pkgs.zphaLib.replaceSecrets "${unitName}.service" (
lib.filterAttrsRecursive (name: value: value != null) cfg.settings
);
settingsFile = settingsFormat.generate "${unitName}.toml" (settingsSecretsReplaced.output);
in
{
enable = cfg.enable;
# confinement.enable = true;
restartTriggers = [ settingsFile ];
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} --config ${settingsFile}";
Restart = "always";
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
LoadCredential = settingsSecretsReplaced.credentials;
ProtectSystem = "full";
ProtectControlGroups = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
LockPersonality = true;
DevicePolicy = "strict";
DeviceAllow = [ "/dev/stdin r" ];
SystemCallArchitectures = "native";
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateTmp = true;
PrivateDevices = true;
UMask = "077";
RestrictAddressFamilies = "AF_INET AF_INET6";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
RemoveIPC = true;
PrivateIPC = true;
SystemCallFilter = [
"@system-service"
"~@resources"
"~@mount"
"~@setuid"
"~@cpu-emulation"
"~@debug"
"~@keyring"
"~@obsolete"
"~@pkey"
"setrlimit"
];
};
}
))
);
};
meta.maintainers = with lib.maintainers; [
zaphyra
];
}