git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #66274 from talyz/gitlab

nixos/gitlab: Add support for secure secrets and more

commit2f3b9cd52cf501c81bc92d2ea5c65547198defa9 authorFlorian Klink <flokli@flokli.de> dateSat 7 Sep 2019, 12:52 (7 years ago) parent493e51d240649a
4 files changed+379−164
nixos/lib/utils.nix+112 nixos/modules/services/misc/gitlab.nix+245−88 nixos/modules/services/misc/gitlab.xml+16−40 nixos/tests/gitlab.nix+6−36
nixos/lib/utils.nix+112
@@ -24,4 +24,116 @@ pkgs: with pkgs.lib;
2424 throw "${shell} is not a shell package"
2525 else
2626 shell;
27+
28+ /* Recurse into a list or an attrset, searching for attrs named like
29+ the value of the "attr" parameter, and return an attrset where the
30+ names are the corresponding jq path where the attrs were found and
31+ the values are the values of the attrs.
32+
33+ Example:
34+ recursiveGetAttrWithJqPrefix {
35+ example = [
36+ {
37+ irrelevant = "not interesting";
38+ }
39+ {
40+ ignored = "ignored attr";
41+ relevant = {
42+ secret = {
43+ _secret = "/path/to/secret";
44+ };
45+ };
46+ }
47+ ];
48+ } "_secret" -> { ".example[1].relevant.secret" = "/path/to/secret"; }
49+ */
50+ recursiveGetAttrWithJqPrefix = item: attr:
51+ let
52+ recurse = prefix: item:
53+ if item ? ${attr} then
54+ nameValuePair prefix item.${attr}
55+ else if isAttrs item then
56+ map (name: recurse (prefix + "." + name) item.${name}) (attrNames item)
57+ else if isList item then
58+ imap0 (index: item: recurse (prefix + "[${toString index}]") item) item
59+ else
60+ [];
61+ in listToAttrs (flatten (recurse "" item));
62+
63+ /* Takes an attrset and a file path and generates a bash snippet that
64+ outputs a JSON file at the file path with all instances of
65+
66+ { _secret = "/path/to/secret" }
67+
68+ in the attrset replaced with the contents of the file
69+ "/path/to/secret" in the output JSON.
70+
71+ When a configuration option accepts an attrset that is finally
72+ converted to JSON, this makes it possible to let the user define
73+ arbitrary secret values.
74+
75+ Example:
76+ If the file "/path/to/secret" contains the string
77+ "topsecretpassword1234",
78+
79+ genJqSecretsReplacementSnippet {
80+ example = [
81+ {
82+ irrelevant = "not interesting";
83+ }
84+ {
85+ ignored = "ignored attr";
86+ relevant = {
87+ secret = {
88+ _secret = "/path/to/secret";
89+ };
90+ };
91+ }
92+ ];
93+ } "/path/to/output.json"
94+
95+ would generate a snippet that, when run, outputs the following
96+ JSON file at "/path/to/output.json":
97+
98+ {
99+ "example": [
100+ {
101+ "irrelevant": "not interesting"
102+ },
103+ {
104+ "ignored": "ignored attr",
105+ "relevant": {
106+ "secret": "topsecretpassword1234"
107+ }
108+ }
109+ ]
110+ }
111+ */
112+ genJqSecretsReplacementSnippet = genJqSecretsReplacementSnippet' "_secret";
113+
114+ # Like genJqSecretsReplacementSnippet, but allows the name of the
115+ # attr which identifies the secret to be changed.
116+ genJqSecretsReplacementSnippet' = attr: set: output:
117+ let
118+ secrets = recursiveGetAttrWithJqPrefix set attr;
119+ in ''
120+ if [[ -h '${output}' ]]; then
121+ rm '${output}'
122+ fi
123+ ''
124+ + concatStringsSep
125+ "\n"
126+ (imap1 (index: name: "export secret${toString index}=$(<'${secrets.${name}}')")
127+ (attrNames secrets))
128+ + "\n"
129+ + "${pkgs.jq}/bin/jq >'${output}' '"
130+ + concatStringsSep
131+ " | "
132+ (imap1 (index: name: ''${name} = $ENV.secret${toString index}'')
133+ (attrNames secrets))
134+ + ''
135+ ' <<'EOF'
136+ ${builtins.toJSON set}
137+ EOF
138+ '';
27139}
nixos/modules/services/misc/gitlab.nix+245−88
@@ -1,4 +1,4 @@
1−{ config, lib, pkgs, ... }:
1+{ config, lib, pkgs, utils, ... }:
22
33# TODO: support non-postgresql
44
@@ -12,14 +12,12 @@ let
1212 gitlabSocket = "${cfg.statePath}/tmp/sockets/gitlab.socket";
1313 gitalySocket = "${cfg.statePath}/tmp/sockets/gitaly.socket";
1414 pathUrlQuote = url: replaceStrings ["/"] ["%2F"] url;
15− pgSuperUser = config.services.postgresql.superUser;
1615
1716 databaseConfig = {
1817 production = {
1918 adapter = "postgresql";
2019 database = cfg.databaseName;
2120 host = cfg.databaseHost;
22− password = cfg.databasePassword;
2321 username = cfg.databaseUsername;
2422 encoding = "utf8";
2523 pool = cfg.databasePool;
@@ -66,13 +64,6 @@ let
6664
6765 redisConfig.production.url = "redis://localhost:6379/";
6866
69− secretsConfig.production = {
70− secret_key_base = cfg.secrets.secret;
71− otp_key_base = cfg.secrets.otp;
72− db_key_base = cfg.secrets.db;
73− openid_connect_signing_key = cfg.secrets.jws;
74− };
75−
7667 gitlabConfig = {
7768 # These are the default settings from config/gitlab.example.yml
7869 production = flip recursiveUpdate cfg.extraConfig {
@@ -180,10 +171,11 @@ let
180171 address: "${cfg.smtp.address}",
181172 port: ${toString cfg.smtp.port},
182173 ${optionalString (cfg.smtp.username != null) ''user_name: "${cfg.smtp.username}",''}
183− ${optionalString (cfg.smtp.password != null) ''password: "${cfg.smtp.password}",''}
174+ ${optionalString (cfg.smtp.passwordFile != null) ''password: "@smtpPassword@",''}
184175 domain: "${cfg.smtp.domain}",
185176 ${optionalString (cfg.smtp.authentication != null) "authentication: :${cfg.smtp.authentication},"}
186177 enable_starttls_auto: ${toString cfg.smtp.enableStartTLSAuto},
178+ ca_file: "/etc/ssl/certs/ca-certificates.crt",
187179 openssl_verify_mode: '${cfg.smtp.opensslVerifyMode}'
188180 }
189181 end
@@ -244,13 +236,33 @@ in {
244236
245237 databaseHost = mkOption {
246238 type = types.str;
247− default = "127.0.0.1";
248− description = "Gitlab database hostname.";
239+ default = "";
240+ description = ''
241+ Gitlab database hostname. An empty string means <quote>use
242+ local unix socket connection</quote>.
243+ '';
249244 };
250245
251− databasePassword = mkOption {
252− type = types.str;
253− description = "Gitlab database user password.";
246+ databasePasswordFile = mkOption {
247+ type = with types; nullOr path;
248+ default = null;
249+ description = ''
250+ File containing the Gitlab database user password.
251+
252+ This should be a string, not a nix path, since nix paths are
253+ copied into the world-readable nix store.
254+ '';
255+ };
256+
257+ databaseCreateLocally = mkOption {
258+ type = types.bool;
259+ default = true;
260+ description = ''
261+ Whether a database should be automatically created on the
262+ local host. Set this to <literal>false</literal> if you plan
263+ on provisioning a local database yourself or use an external
264+ one.
265+ '';
254266 };
255267
256268 databaseName = mkOption {
@@ -338,10 +350,15 @@ in {
338350 '';
339351 };
340352
341− initialRootPassword = mkOption {
342− type = types.str;
353+ initialRootPasswordFile = mkOption {
354+ type = with types; nullOr path;
355+ default = null;
343356 description = ''
344− Initial password of the root account if this is a new install.
357+ File containing the initial password of the root account if
358+ this is a new install.
359+
360+ This should be a string, not a nix path, since nix paths are
361+ copied into the world-readable nix store.
345362 '';
346363 };
347364
@@ -365,15 +382,20 @@ in {
365382 };
366383
367384 username = mkOption {
368− type = types.nullOr types.str;
385+ type = with types; nullOr str;
369386 default = null;
370387 description = "Username of the SMTP server for Gitlab.";
371388 };
372389
373− password = mkOption {
374− type = types.nullOr types.str;
390+ passwordFile = mkOption {
391+ type = types.nullOr types.path;
375392 default = null;
376− description = "Password of the SMTP server for Gitlab.";
393+ description = ''
394+ File containing the password of the SMTP server for Gitlab.
395+
396+ This should be a string, not a nix path, since nix paths
397+ are copied into the world-readable nix store.
398+ '';
377399 };
378400
379401 domain = mkOption {
@@ -383,7 +405,7 @@ in {
383405 };
384406
385407 authentication = mkOption {
386− type = types.nullOr types.str;
408+ type = with types; nullOr str;
387409 default = null;
388410 description = "Authentitcation type to use, see http://api.rubyonrails.org/classes/ActionMailer/Base.html";
389411 };
@@ -401,68 +423,125 @@ in {
401423 };
402424 };
403425
404− secrets.secret = mkOption {
405− type = types.str;
426+ secrets.secretFile = mkOption {
427+ type = with types; nullOr path;
428+ default = null;
406429 description = ''
407− The secret is used to encrypt variables in the DB. If
408− you change or lose this key you will be unable to access variables
409− stored in database.
430+ A file containing the secret used to encrypt variables in
431+ the DB. If you change or lose this key you will be unable to
432+ access variables stored in database.
410433
411434 Make sure the secret is at least 30 characters and all random,
412435 no regular words or you'll be exposed to dictionary attacks.
436+
437+ This should be a string, not a nix path, since nix paths are
438+ copied into the world-readable nix store.
413439 '';
414440 };
415441
416− secrets.db = mkOption {
417− type = types.str;
442+ secrets.dbFile = mkOption {
443+ type = with types; nullOr path;
444+ default = null;
418445 description = ''
419− The secret is used to encrypt variables in the DB. If
420− you change or lose this key you will be unable to access variables
421− stored in database.
446+ A file containing the secret used to encrypt variables in
447+ the DB. If you change or lose this key you will be unable to
448+ access variables stored in database.
422449
423450 Make sure the secret is at least 30 characters and all random,
424451 no regular words or you'll be exposed to dictionary attacks.
452+
453+ This should be a string, not a nix path, since nix paths are
454+ copied into the world-readable nix store.
425455 '';
426456 };
427457
428− secrets.otp = mkOption {
429− type = types.str;
458+ secrets.otpFile = mkOption {
459+ type = with types; nullOr path;
460+ default = null;
430461 description = ''
431− The secret is used to encrypt secrets for OTP tokens. If
432− you change or lose this key, users which have 2FA enabled for login
433− won't be able to login anymore.
462+ A file containing the secret used to encrypt secrets for OTP
463+ tokens. If you change or lose this key, users which have 2FA
464+ enabled for login won't be able to login anymore.
434465
435466 Make sure the secret is at least 30 characters and all random,
436467 no regular words or you'll be exposed to dictionary attacks.
468+
469+ This should be a string, not a nix path, since nix paths are
470+ copied into the world-readable nix store.
437471 '';
438472 };
439473
440− secrets.jws = mkOption {
441− type = types.str;
474+ secrets.jwsFile = mkOption {
475+ type = with types; nullOr path;
476+ default = null;
442477 description = ''
443− The secret is used to encrypt session keys. If you change or lose
444− this key, users will be disconnected.
478+ A file containing the secret used to encrypt session
479+ keys. If you change or lose this key, users will be
480+ disconnected.
445481
446482 Make sure the secret is an RSA private key in PEM format. You can
447483 generate one with
448484
449485 openssl genrsa 2048
486+
487+ This should be a string, not a nix path, since nix paths are
488+ copied into the world-readable nix store.
450489 '';
451490 };
452491
453492 extraConfig = mkOption {
454493 type = types.attrs;
455494 default = {};
456− example = {
457− gitlab = {
458− default_projects_features = {
459− builds = false;
495+ example = literalExample ''
496+ {
497+ gitlab = {
498+ default_projects_features = {
499+ builds = false;
500+ };
501+ };
502+ omniauth = {
503+ enabled = true;
504+ auto_sign_in_with_provider = "openid_connect";
505+ allow_single_sign_on = ["openid_connect"];
506+ block_auto_created_users = false;
507+ providers = [
508+ {
509+ name = "openid_connect";
510+ label = "OpenID Connect";
511+ args = {
512+ name = "openid_connect";
513+ scope = ["openid" "profile"];
514+ response_type = "code";
515+ issuer = "https://keycloak.example.com/auth/realms/My%20Realm";
516+ discovery = true;
517+ client_auth_method = "query";
518+ uid_field = "preferred_username";
519+ client_options = {
520+ identifier = "gitlab";
521+ secret = { _secret = "/var/keys/gitlab_oidc_secret"; };
522+ redirect_uri = "https://git.example.com/users/auth/openid_connect/callback";
523+ };
524+ };
525+ }
526+ ];
460527 };
461528 };
462− };
529+ '';
463530 description = ''
464− Extra options to be merged into config/gitlab.yml as nix
465− attribute set.
531+ Extra options to be added under
532+ <literal>production</literal> in
533+ <filename>config/gitlab.yml</filename>, as a nix attribute
534+ set.
535+
536+ Options containing secret data should be set to an attribute
537+ set containing the attribute <literal>_secret</literal> - a
538+ string pointing to a file containing the value the option
539+ should be set to. See the example to get a better picture of
540+ this: in the resulting
541+ <filename>config/gitlab.yml</filename> file, the
542+ <literal>production.omniauth.providers[0].args.client_options.secret</literal>
543+ key will be set to the contents of the
544+ <filename>/var/keys/gitlab_oidc_secret</filename> file.
466545 '';
467546 };
468547 };
@@ -470,12 +549,66 @@ in {
470549
471550 config = mkIf cfg.enable {
472551
552+ assertions = [
553+ {
554+ assertion = cfg.databaseCreateLocally -> (cfg.user == cfg.databaseUsername);
555+ message = "For local automatic database provisioning services.gitlab.user and services.gitlab.databaseUsername should be identical.";
556+ }
557+ {
558+ assertion = (cfg.databaseHost != "") -> (cfg.databasePasswordFile != null);
559+ message = "When services.gitlab.databaseHost is customized, services.gitlab.databasePasswordFile must be set!";
560+ }
561+ {
562+ assertion = cfg.initialRootPasswordFile != null;
563+ message = "services.gitlab.initialRootPasswordFile must be set!";
564+ }
565+ {
566+ assertion = cfg.secrets.secretFile != null;
567+ message = "services.gitlab.secrets.secretFile must be set!";
568+ }
569+ {
570+ assertion = cfg.secrets.dbFile != null;
571+ message = "services.gitlab.secrets.dbFile must be set!";
572+ }
573+ {
574+ assertion = cfg.secrets.otpFile != null;
575+ message = "services.gitlab.secrets.otpFile must be set!";
576+ }
577+ {
578+ assertion = cfg.secrets.jwsFile != null;
579+ message = "services.gitlab.secrets.jwsFile must be set!";
580+ }
581+ ];
582+
473583 environment.systemPackages = [ pkgs.git gitlab-rake gitlab-rails cfg.packages.gitlab-shell ];
474584
475585 # Redis is required for the sidekiq queue runner.
476586 services.redis.enable = mkDefault true;
587+
477588 # We use postgres as the main data store.
478− services.postgresql.enable = mkDefault true;
589+ services.postgresql = optionalAttrs cfg.databaseCreateLocally {
590+ enable = true;
591+ ensureUsers = singleton { name = cfg.databaseUsername; };
592+ };
593+ # The postgresql module doesn't currently support concepts like
594+ # objects owners and extensions; for now we tack on what's needed
595+ # here.
596+ systemd.services.postgresql.postStart = mkAfter (optionalString cfg.databaseCreateLocally ''
597+ $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${cfg.databaseName}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${cfg.databaseName}" OWNER "${cfg.databaseUsername}"'
598+ current_owner=$($PSQL -tAc "SELECT pg_catalog.pg_get_userbyid(datdba) FROM pg_catalog.pg_database WHERE datname = '${cfg.databaseName}'")
599+ if [[ "$current_owner" != "${cfg.databaseUsername}" ]]; then
600+ $PSQL -tAc 'ALTER DATABASE "${cfg.databaseName}" OWNER TO "${cfg.databaseUsername}"'
601+ if [[ -e "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}" ]]; then
602+ echo "Reassigning ownership of database ${cfg.databaseName} to user ${cfg.databaseUsername} failed on last boot. Failing..."
603+ exit 1
604+ fi
605+ touch "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
606+ $PSQL "${cfg.databaseName}" -tAc "REASSIGN OWNED BY \"$current_owner\" TO \"${cfg.databaseUsername}\""
607+ rm "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
608+ fi
609+ $PSQL '${cfg.databaseName}' -tAc "CREATE EXTENSION IF NOT EXISTS pg_trgm"
610+ '');
611+
479612 # Use postfix to send out mails.
480613 services.postfix.enable = mkDefault true;
481614
@@ -527,14 +660,9 @@ in {
527660
528661 "L+ /run/gitlab/shell-config.yml - - - - ${pkgs.writeText "config.yml" (builtins.toJSON gitlabShellConfig)}"
529662
530− "L+ ${cfg.statePath}/config/gitlab.yml - - - - ${pkgs.writeText "gitlab.yml" (builtins.toJSON gitlabConfig)}"
531− "L+ ${cfg.statePath}/config/database.yml - - - - ${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)}"
532− "L+ ${cfg.statePath}/config/secrets.yml - - - - ${pkgs.writeText "secrets.yml" (builtins.toJSON secretsConfig)}"
533663 "L+ ${cfg.statePath}/config/unicorn.rb - - - - ${./defaultUnicornConfig.rb}"
534−
535664 "L+ ${cfg.statePath}/config/initializers/extra-gitlab.rb - - - - ${extraGitlabRb}"
536− ] ++ optional cfg.smtp.enable
537− "L+ ${cfg.statePath}/config/initializers/smtp_settings.rb - - - - ${smtpSettings}" ;
665+ ];
538666
539667 systemd.services.gitlab-sidekiq = {
540668 after = [ "network.target" "redis.service" "gitlab.service" ];
@@ -626,46 +754,75 @@ in {
626754 gnupg
627755 ];
628756 preStart = ''
629− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
630− ${pkgs.sudo}/bin/sudo -u ${cfg.user} rm -rf ${cfg.statePath}/db/*
631− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
632− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
633−
634− ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
635−
636− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${cfg.packages.gitlab-shell}/bin/install
637−
638− if ! test -e "${cfg.statePath}/db-created"; then
639− if [ "${cfg.databaseHost}" = "127.0.0.1" ]; then
640− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql postgres -c "CREATE ROLE ${cfg.databaseUsername} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${cfg.databasePassword}'"
641− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} ${config.services.postgresql.package}/bin/createdb --owner ${cfg.databaseUsername} ${cfg.databaseName}
642−
643− # enable required pg_trgm extension for gitlab
644− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql ${cfg.databaseName} -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"
757+ cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
758+ rm -rf ${cfg.statePath}/db/*
759+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
760+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
761+
762+ ${cfg.packages.gitlab-shell}/bin/install
763+
764+ ${optionalString cfg.smtp.enable ''
765+ install -m u=rw ${smtpSettings} ${cfg.statePath}/config/initializers/smtp_settings.rb
766+ ${optionalString (cfg.smtp.passwordFile != null) ''
767+ smtp_password=$(<'${cfg.smtp.passwordFile}')
768+ ${pkgs.replace}/bin/replace-literal -e '@smtpPassword@' "$smtp_password" '${cfg.statePath}/config/initializers/smtp_settings.rb'
769+ ''}
770+ ''}
771+
772+ (
773+ umask u=rwx,g=,o=
774+
775+ ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
776+
777+ ${if cfg.databasePasswordFile != null then ''
778+ export db_password="$(<'${cfg.databasePasswordFile}')"
779+
780+ if [[ -z "$db_password" ]]; then
781+ >&2 echo "Database password was an empty string!"
782+ exit 1
783+ fi
784+
785+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
786+ '.production.password = $ENV.db_password' \
787+ >'${cfg.statePath}/config/database.yml'
788+ ''
789+ else ''
790+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
791+ >'${cfg.statePath}/config/database.yml'
792+ ''
793+ }
794+
795+ ${utils.genJqSecretsReplacementSnippet
796+ gitlabConfig
797+ "${cfg.statePath}/config/gitlab.yml"
798+ }
799+
800+ if [[ -h '${cfg.statePath}/config/secrets.yml' ]]; then
801+ rm '${cfg.statePath}/config/secrets.yml'
645802 fi
646803
647− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:schema:load
648−
649− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-created"
650− fi
651−
652− # Always do the db migrations just to be sure the database is up-to-date
653− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:migrate
654−
655− if ! test -e "${cfg.statePath}/db-seeded"; then
656− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${gitlab-rake}/bin/gitlab-rake db:seed_fu \
657− GITLAB_ROOT_PASSWORD='${cfg.initialRootPassword}' GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
658− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-seeded"
659− fi
804+ export secret="$(<'${cfg.secrets.secretFile}')"
805+ export db="$(<'${cfg.secrets.dbFile}')"
806+ export otp="$(<'${cfg.secrets.otpFile}')"
807+ export jws="$(<'${cfg.secrets.jwsFile}')"
808+ ${pkgs.jq}/bin/jq -n '{production: {secret_key_base: $ENV.secret,
809+ otp_key_base: $ENV.db,
810+ db_key_base: $ENV.otp,
811+ openid_connect_signing_key: $ENV.jws}}' \
812+ > '${cfg.statePath}/config/secrets.yml'
813+ )
814+
815+ initial_root_password="$(<'${cfg.initialRootPasswordFile}')"
816+ ${gitlab-rake}/bin/gitlab-rake gitlab:db:configure GITLAB_ROOT_PASSWORD="$initial_root_password" \
817+ GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
660818
661819 # We remove potentially broken links to old gitlab-shell versions
662820 rm -Rf ${cfg.statePath}/repositories/**/*.git/hooks
663821
664− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${pkgs.git}/bin/git config --global core.autocrlf "input"
822+ ${pkgs.git}/bin/git config --global core.autocrlf "input"
665823 '';
666824
667825 serviceConfig = {
668− PermissionsStartOnly = true; # preStart must be run as root
669826 Type = "simple";
670827 User = cfg.user;
671828 Group = cfg.group;
nixos/modules/services/misc/gitlab.xml+16−40
@@ -54,8 +54,8 @@
5454<programlisting>
5555services.gitlab = {
5656 <link linkend="opt-services.gitlab.enable">enable</link> = true;
57− <link linkend="opt-services.gitlab.databasePassword">databasePassword</link> = "eXaMpl3";
58− <link linkend="opt-services.gitlab.initialRootPassword">initialRootPassword</link> = "UseNixOS!";
57+ <link linkend="opt-services.gitlab.databasePasswordFile">databasePasswordFile</link> = "/var/keys/gitlab/db_password";
58+ <link linkend="opt-services.gitlab.initialRootPasswordFile">initialRootPasswordFile</link> = "/var/keys/gitlab/root_password";
5959 <link linkend="opt-services.gitlab.https">https</link> = true;
6060 <link linkend="opt-services.gitlab.host">host</link> = "git.example.com";
6161 <link linkend="opt-services.gitlab.port">port</link> = 443;
@@ -67,38 +67,10 @@ services.gitlab = {
6767 <link linkend="opt-services.gitlab.smtp.port">port</link> = 25;
6868 };
6969 secrets = {
70− <link linkend="opt-services.gitlab.secrets.db">db</link> = "uPgq1gtwwHiatiuE0YHqbGa5lEIXH7fMsvuTNgdzJi8P0Dg12gibTzBQbq5LT7PNzcc3BP9P1snHVnduqtGF43PgrQtU7XL93ts6gqe9CBNhjtaqUwutQUDkygP5NrV6";
71− <link linkend="opt-services.gitlab.secrets.secret">secret</link> = "devzJ0Tz0POiDBlrpWmcsjjrLaltyiAdS8TtgT9YNBOoUcDsfppiY3IXZjMVtKgXrFImIennFGOpPN8IkP8ATXpRgDD5rxVnKuTTwYQaci2NtaV1XxOQGjdIE50VGsR3";
72− <link linkend="opt-services.gitlab.secrets.otp">otp</link> = "e1GATJVuS2sUh7jxiPzZPre4qtzGGaS22FR50Xs1TerRVdgI3CBVUi5XYtQ38W4xFeS4mDqi5cQjExE838iViSzCdcG19XSL6qNsfokQP9JugwiftmhmCadtsnHErBMI";
73− <link linkend="opt-services.gitlab.secrets.jws">jws</link> = ''
74− -----BEGIN RSA PRIVATE KEY-----
75− MIIEpAIBAAKCAQEArrtx4oHKwXoqUbMNqnHgAklnnuDon3XG5LJB35yPsXKv/8GK
76− ke92wkI+s1Xkvsp8tg9BIY/7c6YK4SR07EWL+dB5qwctsWR2Q8z+/BKmTx9D99pm
77− hnsjuNIXTF7BXrx3RX6BxZpH5Vzzh9nCwWKT/JCFqtwH7afNGGL7aMf+hdaiUg/Q
78− SD05yRObioiO4iXDolsJOhrnbZvlzVHl1ZYxFJv0H6/Snc0BBA9Fl/3uj6ANpbjP
79− eXF1SnJCqT87bj46r5NdVauzaRxAsIfqHroHK4UZ98X5LjGQFGvSqTvyjPBS4I1i
80− s7VJU28ObuutHxIxSlH0ibn4HZqWmKWlTS652wIDAQABAoIBAGtPcUTTw2sJlR3x
81− 4k2wfAvLexkHNbZhBdKEa5JiO5mWPuLKwUiZEY2CU7Gd6csG3oqNWcm7/IjtC7dz
82− xV8p4yp8T4yq7vQIJ93B80NqTLtBD2QTvG2RCMJEPMzJUObWxkVmyVpLQyZo7KOd
83− KE/OM+aj94OUeEYLjRkSCScz1Gvq/qFG/nAy7KPCmN9JDHuhX26WHo2Rr1OnPNT/
84− 7diph0bB9F3b8gjjNTqXDrpdAqVOgR/PsjEBz6DMY+bdyMIn87q2yfmMexxRofN6
85− LulpzSaa6Yup8N8H6PzVO6KAkQuf1aQRj0sMwGk1IZEnj6I0KbuHIZkw21Nc6sf2
86− ESFySDECgYEA1PnCNn5tmLnwe62Ttmrzl20zIS3Me1gUVJ1NTfr6+ai0I9iMYU21
87− 5czuAjJPm9JKQF2vY8UAaCj2ZoObtHa/anb3xsCd8NXoM3iJq5JDoXI1ldz3Y+ad
88− U/bZUg1DLRvAniTuXmw9iOTwTwPxlDIGq5k+wG2Xmi1lk7zH8ezr9BMCgYEA0gfk
89− EhgcmPH8Z5cU3YYwOdt6HSJOM0OyN4k/5gnkv+HYVoJTj02gkrJmLr+mi1ugKj46
90− 7huYO9TVnrKP21tmbaSv1dp5hS3letVRIxSloEtVGXmmdvJvBRzDWos+G+KcvADi
91− fFCz6w8v9NmO40CB7y/3SxTmSiSxDQeoi9LhDBkCgYEAsPgMWm25sfOnkY2NNUIv
92− wT8bAlHlHQT2d8zx5H9NttBpR3P0ShJhuF8N0sNthSQ7ULrIN5YGHYcUH+DyLAWU
93− TuomP3/kfa+xL7vUYb269tdJEYs4AkoppxBySoz8qenqpz422D0G8M6TpIS5Y5Qi
94− GMrQ6uLl21YnlpiCaFOfSQMCgYEAmZxj1kgEQmhZrnn1LL/D7czz1vMMNrpAUhXz
95− wg9iWmSXkU3oR1sDIceQrIhHCo2M6thwyU0tXjUft93pEQocM/zLDaGoVxtmRxxV
96− J08mg8IVD3jFoyFUyWxsBIDqgAKRl38eJsXvkO+ep3mm49Z+Ma3nM+apN3j2dQ0w
97− 3HLzXaECgYBFLMEAboVFwi5+MZjGvqtpg2PVTisfuJy2eYnPwHs+AXUgi/xRNFjI
98− YHEa7UBPb5TEPSzWImQpETi2P5ywcUYL1EbN/nqPWmjFnat8wVmJtV4sUpJhubF4
99− Vqm9LxIWc1uQ1q1HDCejRIxIN3aSH+wgRS3Kcj8kCTIoXd1aERb04g==
100− -----END RSA PRIVATE KEY-----
101− '';
70+ <link linkend="opt-services.gitlab.secrets.dbFile">dbFile</link> = "/var/keys/gitlab/db";
71+ <link linkend="opt-services.gitlab.secrets.secretFile">secretFile</link> = "/var/keys/gitlab/secret";
72+ <link linkend="opt-services.gitlab.secrets.otpFile">otpFile</link> = "/var/keys/gitlab/otp";
73+ <link linkend="opt-services.gitlab.secrets.jwsFile">jwsFile</link> = "/var/keys/gitlab/jws";
10274 };
10375 <link linkend="opt-services.gitlab.extraConfig">extraConfig</link> = {
10476 gitlab = {
@@ -113,12 +85,16 @@ services.gitlab = {
11385 </para>
11486
11587 <para>
116− If you're setting up a new Gitlab instance, generate new secrets. You for
117− instance use <literal>tr -dc A-Za-z0-9 &lt; /dev/urandom | head -c
118− 128</literal> to generate a new secret. Gitlab encrypts sensitive data
119− stored in the database. If you're restoring an existing Gitlab instance, you
120− must specify the secrets secret from <literal>config/secrets.yml</literal>
121− located in your Gitlab state folder.
88+ If you're setting up a new Gitlab instance, generate new
89+ secrets. You for instance use <literal>tr -dc A-Za-z0-9 &lt;
90+ /dev/urandom | head -c 128 &gt; /var/keys/gitlab/db</literal> to
91+ generate a new db secret. Make sure the files can be read by, and
92+ only by, the user specified by <link
93+ linkend="opt-services.gitlab.user">services.gitlab.user</link>. Gitlab
94+ encrypts sensitive data stored in the database. If you're restoring
95+ an existing Gitlab instance, you must specify the secrets secret
96+ from <literal>config/secrets.yml</literal> located in your Gitlab
97+ state folder.
12298 </para>
12399
124100 <para>
nixos/tests/gitlab.nix+6−36
@@ -29,44 +29,14 @@ import ./make-test.nix ({ pkgs, lib, ...} : with lib; {
2929
3030 services.gitlab = {
3131 enable = true;
32− databasePassword = "dbPassword";
33− inherit initialRootPassword;
32+ databasePasswordFile = pkgs.writeText "dbPassword" "xo0daiF4";
33+ initialRootPasswordFile = pkgs.writeText "rootPassword" initialRootPassword;
3434 smtp.enable = true;
3535 secrets = {
36− secret = "secret";
37− otp = "otpsecret";
38− db = "dbsecret";
39−
40− # nix-shell -p openssl --run "openssl genrsa 2048"
41− jws = ''
42− -----BEGIN RSA PRIVATE KEY-----
43− MIIEpAIBAAKCAQEA13/qEio76OWUtWO0WIz9lWnsTWOU8Esv4sQHDq9PCEFsLt21
44− PAXrlWhLjjWcxGfsrDwnh7YErGHYL62BMSxMdFJolaknlQK/O/V8UETDe45VoHM+
45− Znk270RfUcfYFgiihnXUZXVmL0om9TsQSk646wCcjCY9LxtxUyKNhvT7KjgYw2aX
46− z34aw7M+Js3T2p1TjZPSC82GtmtKkJEKFMi5EjprLTDE7EdcUzr9Xuw+kQ+gRm9k
47− 7FE+JQqSoprwE3Q0v2OAn3UhLMgg0gNFRnsc5l6IAshDzV+H22RPqKKlJjVjjfPY
48− 0TQSvYLVApigHbDPH0BoCXfjFfQazbbP3OUHrwIDAQABAoIBAQCMU+tkcMQaYIV5
49− qLdjgkwO467QpivyXcOM8wF1eosIYTHFQvIlZ+WEoSmyLQ8shlADyBgls01Pw1c3
50− lNAv6RzQEmmwKzpvOh61OKH+0whIiOMRXHoh2IUBQZCgfHYlwvGyhUAN4WjtGmhM
51− AG4XNTQNM5S9Xpkw97nP3Qwz+YskbbkrfqtCEVy9ro+4nhbjqPsuO3adbnkva4zR
52− cyurRhrHgHU6LPjn5NHnHH4qw2faY2oAsL8pmpkTbO5IqWDvOcbjNfjVPgVoq26O
53− bbaa1qs4nmc80qQgMjRPJef535xyf3eLsSlDvpf6O8sPrJzVR1zaqEqixpQCZDac
54− +kRiSBrhAoGBAOwHiq0PuyJh6VzBu7ybqX6+gF/wA4Jkwzx6mbfaBgurvU1aospp
55− kisIonAkxSbxllZMnjbkShZEdATYKeT9o5NEhnU4YnHfc5bJZbiWOZAzYGLcY7g8
56− vDQ31pBItyY4pFgPbSpNlbUvUsoPVJ45RasRADDTNCzMzdjFQQXst2V9AoGBAOm7
57− sSpzYfFPLEAhieAkuhtbsX58Boo46djiKVfzGftfp6F9aHTOfzGORU5jrZ16mSbS
58− qkkC6BEFrATX2051dzzXC89fWoJYALrsffE5I3KlKXsCAWSnCP1MMxOfH+Ls61Mr
59− 7pK/LKfvJt53mUH4jIdbmmFUDwbg18oBEH+x9PmbAoGAS/+JqXu9N67rIxDGUE6W
60− 3tacI0f2+U9Uhe67/DTZaXyc8YFTlXU0uWKIWy+bw5RaYeM9tlL/f/f+m2i25KK+
61− vrZ7zNag7CWU5GJovGyykDnauTpZaYM03mN0VPT08/uc/zXIYqyknbhlIeaZynCK
62− fDB3LUF0NVCknz20WCIGU0kCgYEAkxY0ZXx61Dp4pFr2wwEZxQGs7uXpz64FKyEX
63− 12r6nMATY4Lh6y/Px0W6w5vis8lk+5Ny6cNUevHQ0LNuJS+yu6ywl+1vrbrnqroM
64− f3LvpcPeGLSoX8jl1VDQi7aFgG6LoKly1xJLbdsH4NPutB9PgBbbTghx9GgmI88L
65− rPA2M6UCgYBOmkYJocNgxg6B1/n4Tb9fN1Q/XuJrFDE6NxVUoke+IIyMPRH7FC3m
66− VMYzu+b7zTVJjaBb1cmJemxl/xajziWDofJYPefhdbOVU7HXtmJFY0IG3pVxU1zW
67− 3bmDj5QAtCUDpuuNa6GEIT0YR4+D/V7o3DmlZ0tVIwKJmVJoQ2f5dw==
68− -----END RSA PRIVATE KEY-----
69− '';
36+ secretFile = pkgs.writeText "secret" "Aig5zaic";
37+ otpFile = pkgs.writeText "otpsecret" "Riew9mue";
38+ dbFile = pkgs.writeText "dbsecret" "we2quaeZ";
39+ jwsFile = pkgs.runCommand "oidcKeyBase" {} "${pkgs.openssl}/bin/openssl genrsa 2048 > $out";
7040 };
7141 };
7242 };