git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/gitea: make more secrets persistent (#108676)

Added JWT_SECRET and INTERNAL_TOKEN to be persistent, like SECRET_KEY and LFS_JWT_SECRET do. Also renamed some vars belonging to SECRET_KEY and LFS_JWT_SECRET to get a consistent naming scheme over all secrets.

commit10eed48d10d1328621cda42bc2cf25a2fb53b209 authorclerie <clerie@users.noreply.github.com> dateFri 15 Jan 2021, 12:54 (6 years ago) parentf3042e3
1 file changed+41−9
nixos/modules/services/misc/gitea.nix+41−9
nixos/modules/services/misc/gitea.nix+41−9
@@ -349,7 +349,7 @@ in
349349 {
350350 DOMAIN = cfg.domain;
351351 STATIC_ROOT_PATH = cfg.staticRootPath;
352− LFS_JWT_SECRET = "#jwtsecret#";
352+ LFS_JWT_SECRET = "#lfsjwtsecret#";
353353 ROOT_URL = cfg.rootUrl;
354354 }
355355 (mkIf cfg.enableUnixSocket {
@@ -381,6 +381,7 @@ in
381381
382382 security = {
383383 SECRET_KEY = "#secretkey#";
384+ INTERNAL_TOKEN = "#internaltoken#";
384385 INSTALL_LOCK = true;
385386 };
386387
@@ -396,6 +397,10 @@ in
396397 mailer = mkIf (cfg.mailerPasswordFile != null) {
397398 PASSWD = "#mailerpass#";
398399 };
400+
401+ oauth2 = {
402+ JWT_SECRET = "#oauth2jwtsecret#";
403+ };
399404 };
400405
401406 services.postgresql = optionalAttrs (usePostgresql && cfg.database.createDatabase) {
@@ -455,10 +460,20 @@ in
455460 wantedBy = [ "multi-user.target" ];
456461 path = [ gitea pkgs.git ];
457462
463+ # In older versions the secret naming for JWT was kind of confusing.
464+ # The file jwt_secret hold the value for LFS_JWT_SECRET and JWT_SECRET
465+ # wasn't persistant at all.
466+ # To fix that, there is now the file oauth2_jwt_secret containing the
467+ # values for JWT_SECRET and the file jwt_secret gets renamed to
468+ # lfs_jwt_secret.
469+ # We have to consider this to stay compatible with older installations.
458470 preStart = let
459471 runConfig = "${cfg.stateDir}/custom/conf/app.ini";
460472 secretKey = "${cfg.stateDir}/custom/conf/secret_key";
461− jwtSecret = "${cfg.stateDir}/custom/conf/jwt_secret";
473+ oauth2JwtSecret = "${cfg.stateDir}/custom/conf/oauth2_jwt_secret";
474+ oldLfsJwtSecret = "${cfg.stateDir}/custom/conf/jwt_secret"; # old file for LFS_JWT_SECRET
475+ lfsJwtSecret = "${cfg.stateDir}/custom/conf/lfs_jwt_secret"; # new file for LFS_JWT_SECRET
476+ internalToken = "${cfg.stateDir}/custom/conf/internal_token";
462477 in ''
463478 # copy custom configuration and generate a random secret key if needed
464479 ${optionalString (cfg.useWizard == false) ''
@@ -468,24 +483,41 @@ in
468483 ${gitea}/bin/gitea generate secret SECRET_KEY > ${secretKey}
469484 fi
470485
471− if [ ! -e ${jwtSecret} ]; then
472− ${gitea}/bin/gitea generate secret LFS_JWT_SECRET > ${jwtSecret}
486+ # Migrate LFS_JWT_SECRET filename
487+ if [[ -e ${oldLfsJwtSecret} && ! -e ${lfsJwtSecret} ]]; then
488+ mv ${oldLfsJwtSecret} ${lfsJwtSecret}
489+ fi
490+
491+ if [ ! -e ${oauth2JwtSecret} ]; then
492+ ${gitea}/bin/gitea generate secret JWT_SECRET > ${oauth2JwtSecret}
493+ fi
494+
495+ if [ ! -e ${lfsJwtSecret} ]; then
496+ ${gitea}/bin/gitea generate secret LFS_JWT_SECRET > ${lfsJwtSecret}
497+ fi
498+
499+ if [ ! -e ${internalToken} ]; then
500+ ${gitea}/bin/gitea generate secret INTERNAL_TOKEN > ${internalToken}
473501 fi
474502
475− KEY="$(head -n1 ${secretKey})"
503+ SECRETKEY="$(head -n1 ${secretKey})"
476504 DBPASS="$(head -n1 ${cfg.database.passwordFile})"
477− JWTSECRET="$(head -n1 ${jwtSecret})"
505+ OAUTH2JWTSECRET="$(head -n1 ${oauth2JwtSecret})"
506+ LFSJWTSECRET="$(head -n1 ${lfsJwtSecret})"
507+ INTERNALTOKEN="$(head -n1 ${internalToken})"
478508 ${if (cfg.mailerPasswordFile == null) then ''
479509 MAILERPASSWORD="#mailerpass#"
480510 '' else ''
481511 MAILERPASSWORD="$(head -n1 ${cfg.mailerPasswordFile} || :)"
482512 ''}
483− sed -e "s,#secretkey#,$KEY,g" \
513+ sed -e "s,#secretkey#,$SECRETKEY,g" \
484514 -e "s,#dbpass#,$DBPASS,g" \
485− -e "s,#jwtsecret#,$JWTSECRET,g" \
515+ -e "s,#oauth2jwtsecret#,$OAUTH2JWTSECRET,g" \
516+ -e "s,#lfsjwtsecret#,$LFSJWTSECRET,g" \
517+ -e "s,#internaltoken#,$INTERNALTOKEN,g" \
486518 -e "s,#mailerpass#,$MAILERPASSWORD,g" \
487519 -i ${runConfig}
488− chmod 640 ${runConfig} ${secretKey} ${jwtSecret}
520+ chmod 640 ${runConfig} ${secretKey} ${oauth2JwtSecret} ${lfsJwtSecret} ${internalToken}
489521 ''}
490522
491523 # update all hooks' binary paths