git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #39002 from serokell/oauth2_proxy_mod

oauth2_proxy: refactor service

commite71c36369ffe365b6f82c8b0a901d21496b7d71c authorYegor Timoshenko <yegortimoshenko@riseup.net> dateFri 27 Apr 2018, 22:15 (9 years ago) parent547a41a048c991
1 file changed+106−60
nixos/modules/services/security/oauth2_proxy.nix+106−60
nixos/modules/services/security/oauth2_proxy.nix+106−60
@@ -6,70 +6,81 @@ with lib;
66let
77 cfg = config.services.oauth2_proxy;
88
9− # Use like:
10− # repeatedArgs (arg: "--arg=${arg}") args
11− repeatedArgs = concatMapStringsSep " ";
12−
139 # oauth2_proxy provides many options that are only relevant if you are using
1410 # a certain provider. This set maps from provider name to a function that
1511 # takes the configuration and returns a string that can be inserted into the
1612 # command-line to launch oauth2_proxy.
1713 providerSpecificOptions = {
18− azure = cfg: ''
19− --azure-tenant=${cfg.azure.tenant} \
20− --resource=${cfg.azure.resource} \
21− '';
22−
23− github = cfg: ''
24− ${optionalString (!isNull cfg.github.org) "--github-org=${cfg.github.org}"} \
25− ${optionalString (!isNull cfg.github.team) "--github-org=${cfg.github.team}"} \
26− '';
27−
28− google = cfg: ''
29− --google-admin-email=${cfg.google.adminEmail} \
30− --google-service-account=${cfg.google.serviceAccountJSON} \
31− ${repeatedArgs (group: "--google-group=${group}") cfg.google.groups} \
32− '';
14+ azure = cfg: {
15+ azure.tenant = cfg.azure.tenant;
16+ resource = cfg.azure.resource;
17+ };
18+
19+ github = cfg: { github = {
20+ inherit (cfg.github) org team;
21+ }; };
22+
23+ google = cfg: { google = with cfg.google; optionalAttrs (groups != []) {
24+ admin-email = adminEmail;
25+ service-account = serviceAccountJSON;
26+ group = groups;
27+ }; };
3328 };
3429
3530 authenticatedEmailsFile = pkgs.writeText "authenticated-emails" cfg.email.addresses;
3631
37− getProviderOptions = cfg: provider: providerSpecificOptions.${provider} or (_: "") cfg;
38−
39− mkCommandLine = cfg: ''
40− --provider='${cfg.provider}' \
41− ${optionalString (!isNull cfg.email.addresses) "--authenticated-emails-file='${authenticatedEmailsFile}'"} \
42− --approval-prompt='${cfg.approvalPrompt}' \
43− ${optionalString (cfg.passBasicAuth && !isNull cfg.basicAuthPassword) "--basic-auth-password='${cfg.basicAuthPassword}'"} \
44− --client-id='${cfg.clientID}' \
45− --client-secret='${cfg.clientSecret}' \
46− ${optionalString (!isNull cfg.cookie.domain) "--cookie-domain='${cfg.cookie.domain}'"} \
47− --cookie-expire='${cfg.cookie.expire}' \
48− --cookie-httponly=${boolToString cfg.cookie.httpOnly} \
49− --cookie-name='${cfg.cookie.name}' \
50− --cookie-secret='${cfg.cookie.secret}' \
51− --cookie-secure=${boolToString cfg.cookie.secure} \
52− ${optionalString (!isNull cfg.cookie.refresh) "--cookie-refresh='${cfg.cookie.refresh}'"} \
53− ${optionalString (!isNull cfg.customTemplatesDir) "--custom-templates-dir='${cfg.customTemplatesDir}'"} \
54− ${repeatedArgs (x: "--email-domain='${x}'") cfg.email.domains} \
55− --http-address='${cfg.httpAddress}' \
56− ${optionalString (!isNull cfg.htpasswd.file) "--htpasswd-file='${cfg.htpasswd.file}' --display-htpasswd-form=${boolToString cfg.htpasswd.displayForm}"} \
57− ${optionalString (!isNull cfg.loginURL) "--login-url='${cfg.loginURL}'"} \
58− --pass-access-token=${boolToString cfg.passAccessToken} \
59− --pass-basic-auth=${boolToString cfg.passBasicAuth} \
60− --pass-host-header=${boolToString cfg.passHostHeader} \
61− --proxy-prefix='${cfg.proxyPrefix}' \
62− ${optionalString (!isNull cfg.profileURL) "--profile-url='${cfg.profileURL}'"} \
63− ${optionalString (!isNull cfg.redeemURL) "--redeem-url='${cfg.redeemURL}'"} \
64− ${optionalString (!isNull cfg.redirectURL) "--redirect-url='${cfg.redirectURL}'"} \
65− --request-logging=${boolToString cfg.requestLogging} \
66− ${optionalString (!isNull cfg.scope) "--scope='${cfg.scope}'"} \
67− ${repeatedArgs (x: "--skip-auth-regex='${x}'") cfg.skipAuthRegexes} \
68− ${optionalString (!isNull cfg.signatureKey) "--signature-key='${cfg.signatureKey}'"} \
69− --upstream='${cfg.upstream}' \
70− ${optionalString (!isNull cfg.validateURL) "--validate-url='${cfg.validateURL}'"} \
71− ${optionalString cfg.tls.enable "--tls-cert='${cfg.tls.certificate}' --tls-key='${cfg.tls.key}' --https-address='${cfg.tls.httpsAddress}'"} \
72− '' + getProviderOptions cfg cfg.provider;
32+ getProviderOptions = cfg: provider: providerSpecificOptions.${provider} or (_: {}) cfg;
33+
34+ allConfig = with cfg; {
35+ inherit (cfg) provider scope upstream;
36+ approval-prompt = approvalPrompt;
37+ basic-auth-password = basicAuthPassword;
38+ client-id = clientID;
39+ client-secret = clientSecret;
40+ custom-templates-dir = customTemplatesDir;
41+ email-domain = email.domains;
42+ http-address = httpAddress;
43+ login-url = loginURL;
44+ pass-access-token = passAccessToken;
45+ pass-basic-auth = passBasicAuth;
46+ pass-host-header = passHostHeader;
47+ proxy-prefix = proxyPrefix;
48+ profile-url = profileURL;
49+ redeem-url = redeemURL;
50+ redirect-url = redirectURL;
51+ request-logging = requestLogging;
52+ skip-auth-regex = skipAuthRegexes;
53+ signature-key = signatureKey;
54+ validate-url = validateURL;
55+ htpasswd-file = htpasswd.file;
56+ cookie = {
57+ inherit (cookie) domain secure expire name secret refresh;
58+ httponly = cookie.httpOnly;
59+ };
60+ set-xauthrequest = setXauthrequest;
61+ } // lib.optionalAttrs (!isNull cfg.email.addresses) {
62+ authenticated-emails-file = authenticatedEmailsFile;
63+ } // lib.optionalAttrs (cfg.passBasicAuth) {
64+ basic-auth-password = cfg.basicAuthPassword;
65+ } // lib.optionalAttrs (!isNull cfg.htpasswd.file) {
66+ display-htpasswd-file = cfg.htpasswd.displayForm;
67+ } // lib.optionalAttrs tls.enable {
68+ tls-cert = tls.certificate;
69+ tls-key = tls.key;
70+ https-address = tls.httpsAddress;
71+ } // (getProviderOptions cfg cfg.provider) // cfg.extraConfig;
72+
73+ mapConfig = key: attr:
74+ if (!isNull attr && attr != []) then (
75+ if (builtins.typeOf attr) == "set" then concatStringsSep " "
76+ (mapAttrsToList (name: value: mapConfig (key + "-" + name) value) attr) else
77+ if (builtins.typeOf attr) == "list" then concatMapStringsSep " " (mapConfig key) attr else
78+ if (builtins.typeOf attr) == "bool" then "--${key}=${boolToString attr}" else
79+ if (builtins.typeOf attr) == "string" then "--${key}='${attr}'" else
80+ "--${key}=${toString attr}")
81+ else "";
82+
83+ configString = concatStringsSep " " (mapAttrsToList mapConfig allConfig);
7384in
7485{
7586 options.services.oauth2_proxy = {
@@ -110,7 +121,7 @@ in
110121 };
111122
112123 clientID = mkOption {
113− type = types.str;
124+ type = types.nullOr types.str;
114125 description = ''
115126 The OAuth Client ID.
116127 '';
@@ -118,7 +129,7 @@ in
118129 };
119130
120131 clientSecret = mkOption {
121− type = types.str;
132+ type = types.nullOr types.str;
122133 description = ''
123134 The OAuth Client Secret.
124135 '';
@@ -272,7 +283,8 @@ in
272283 ####################################################
273284 # UPSTREAM Configuration
274285 upstream = mkOption {
275− type = types.commas;
286+ type = with types; coercedTo string (x: [x]) (listOf string);
287+ default = [];
276288 description = ''
277289 The http url(s) of the upstream endpoint or <literal>file://</literal>
278290 paths for static files. Routing is based on the path.
@@ -365,7 +377,7 @@ in
365377 };
366378
367379 secret = mkOption {
368− type = types.str;
380+ type = types.nullOr types.str;
369381 description = ''
370382 The seed string for secure cookies.
371383 '';
@@ -494,10 +506,43 @@ in
494506 '';
495507 };
496508
509+ setXauthrequest = mkOption {
510+ type = types.nullOr types.bool;
511+ default = false;
512+ description = ''
513+ Set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode). Setting this to 'null' means using the upstream default (false).
514+ '';
515+ };
516+
517+ extraConfig = mkOption {
518+ default = {};
519+ description = ''
520+ Extra config to pass to oauth2_proxy.
521+ '';
522+ };
523+
524+ keyFile = mkOption {
525+ type = types.nullOr types.string;
526+ default = null;
527+ description = ''
528+ oauth2_proxy allows passing sensitive configuration via environment variables.
529+ Make a file that contains lines like
530+ OAUTH2_PROXY_CLIENT_SECRET=asdfasdfasdf.apps.googleuserscontent.com
531+ and specify the path here.
532+ '';
533+ example = "/run/keys/oauth2_proxy";
534+ };
535+
497536 };
498537
499538 config = mkIf cfg.enable {
500539
540+ services.oauth2_proxy = mkIf (!isNull cfg.keyFile) {
541+ clientID = mkDefault null;
542+ clientSecret = mkDefault null;
543+ cookie.secret = mkDefault null;
544+ };
545+
501546 users.extraUsers.oauth2_proxy = {
502547 description = "OAuth2 Proxy";
503548 };
@@ -511,7 +556,8 @@ in
511556 serviceConfig = {
512557 User = "oauth2_proxy";
513558 Restart = "always";
514− ExecStart = "${cfg.package.bin}/bin/oauth2_proxy ${mkCommandLine cfg}";
559+ ExecStart = "${cfg.package.bin}/bin/oauth2_proxy ${configString}";
560+ EnvironmentFile = mkIf (cfg.keyFile != null) cfg.keyFile;
515561 };
516562 };
517563