diff --git a/README.md b/README.md index ebf7658..6c02385 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,13 @@ INSTALL_LOCK is always set to true, since we want to configure Gitea with this h _All default settings are made directly in the generated `app.ini`, not in the Values._ +#### Rebuilding app.ini + +By default, removed settings remain in an existing `app.ini`. +Set `deployment.initAppIni.config.rebuild` to `true` to rebuild it on startup from the current configuration sources. +The existing `security.SECRET_KEY`, `security.INTERNAL_TOKEN`, `oauth2.JWT_SECRET`, and `server.LFS_JWT_SECRET` are preserved. +All other settings that are present only in `app.ini`, including manually managed secrets, are removed. + #### Database defaults If a builtIn database is enabled the database configuration is set automatically. @@ -1052,6 +1059,7 @@ To comply with the Gitea helm chart definition of the digest parameter, a "custo | `deployment.initDirectories.resources` | Compute Resources required by the initDirectories container. Defaults to `initContainers.resources`. Cannot be updated. | `nil` | | `deployment.initDirectories.securityContext` | Security context of the initDirectories container. Defaults to `deployment.gitea.securityContext`. | `{}` | | `deployment.initDirectories.volumeMounts` | Additional volume mounts. | `[]` | +| `deployment.initAppIni.config.rebuild` | Rebuild app.ini on startup, preserving Gitea secret keys but removing unmanaged settings. | `false` | | `deployment.initAppIni.env` | Additional environment variables to pass to the init container. | `[]` | | `deployment.initAppIni.envFrom` | List of environment variables mounted from configMaps or secrets for the initAppIni container. | `[]` | | `deployment.initAppIni.image.registry` | image registry, e.g. gcr.io,docker.io. | `docker.gitea.com` | diff --git a/scripts/init-containers/config/config_environment.sh b/scripts/init-containers/config/config_environment.sh index ccd725a..8495a50 100755 --- a/scripts/init-containers/config/config_environment.sh +++ b/scripts/init-containers/config/config_environment.sh @@ -158,4 +158,11 @@ if [ -f "${GITEA_APP_INI}" ]; then unset GITEA__SERVER__LFS_JWT_SECRET fi -gitea config edit-ini --apply-env --config "$GITEA_APP_INI" --out "$GITEA_APP_INI" +if [[ "${GITEA_REBUILD_APP_INI:-false}" == "true" && -f "${GITEA_APP_INI}" ]]; then + keep_keys_file="$(mktemp)" + trap 'rm -f "${keep_keys_file}"' EXIT + printf '%s\n' '[security]' 'SECRET_KEY=' 'INTERNAL_TOKEN=' '[oauth2]' 'JWT_SECRET=' '[server]' 'LFS_JWT_SECRET=' > "${keep_keys_file}" + gitea config edit-ini --apply-env --config "$GITEA_APP_INI" --config-keep-keys "${keep_keys_file}" --out "$GITEA_APP_INI" +else + gitea config edit-ini --apply-env --config "$GITEA_APP_INI" --out "$GITEA_APP_INI" +fi diff --git a/templates/_initContainers.tpl b/templates/_initContainers.tpl index 0b02fe3..89b856c 100644 --- a/templates/_initContainers.tpl +++ b/templates/_initContainers.tpl @@ -84,6 +84,8 @@ value: /tmp/existing-envs - name: ENV_TO_INI_MOUNT_POINT value: /env-to-ini-mounts + - name: GITEA_REBUILD_APP_INI + value: {{ $config.config.rebuild | quote }} {{- if .Values.cloudnativePG.enabled }} - name: GITEA__database__PASSWD valueFrom: diff --git a/unittests/bash/tests/init-containers/config/config_environment.bats b/unittests/bash/tests/init-containers/config/config_environment.bats index fc1a84c..c2824b1 100644 --- a/unittests/bash/tests/init-containers/config/config_environment.bats +++ b/unittests/bash/tests/init-containers/config/config_environment.bats @@ -30,6 +30,11 @@ case "$*" in 'generate secret LFS_JWT_SECRET') echo 'mocked-lfs-jwt-secret' ;; + "config edit-ini --apply-env --config $GITEA_APP_INI --config-keep-keys "*" --out $GITEA_APP_INI") + test "$GITEA_EDIT_INI_EXPECTED" -eq 1 + cat "$7" + echo 'Stubbed gitea config edit-ini rebuild was called!' + ;; "config edit-ini --apply-env --config $GITEA_APP_INI --out $GITEA_APP_INI") if [ "$GITEA_EDIT_INI_EXPECTED" -eq 1 ]; then echo 'Stubbed gitea config edit-ini was called!' @@ -115,6 +120,34 @@ function write_mounted_file() { refute_line '+GITEA__SERVER__LFS_JWT_SECRET=mocked-lfs-jwt-secret' } +@test "rebuilds an existing app.ini while preserving secret keys" { + expect_gitea_config_edit_ini_call + export GITEA_REBUILD_APP_INI=true + touch "$GITEA_APP_INI" + + run "$PROJECT_ROOT/scripts/init-containers/config/config_environment.sh" + + assert_success + assert_line '[security]' + assert_line 'SECRET_KEY=' + assert_line 'INTERNAL_TOKEN=' + assert_line '[oauth2]' + assert_line 'JWT_SECRET=' + assert_line '[server]' + assert_line 'LFS_JWT_SECRET=' + assert_line 'Stubbed gitea config edit-ini rebuild was called!' +} + +@test "uses normal initialization when rebuild is enabled for a new app.ini" { + expect_gitea_config_edit_ini_call + export GITEA_REBUILD_APP_INI=true + + run "$PROJECT_ROOT/scripts/init-containers/config/config_environment.sh" + + assert_success + assert_line 'Stubbed gitea config edit-ini was called!' +} + @test "ensures that preset environment variables take precedence over auto-generated ones" { expect_gitea_config_edit_ini_call export GITEA__OAUTH2__JWT_SECRET="pre-defined-jwt-secret" diff --git a/unittests/helm/deployment/additionalConfig.yaml b/unittests/helm/deployment/additionalConfig.yaml index c417fbf..b964470 100644 --- a/unittests/helm/deployment/additionalConfig.yaml +++ b/unittests/helm/deployment/additionalConfig.yaml @@ -31,7 +31,7 @@ tests: path: spec.template.spec.initContainers[1].env - lengthEqual: path: spec.template.spec.initContainers[1].env - count: 6 + count: 7 - isSubset: path: spec.template.spec.initContainers[1] content: @@ -48,6 +48,8 @@ tests: value: /tmp/existing-envs - name: ENV_TO_INI_MOUNT_POINT value: /env-to-ini-mounts + - name: GITEA_REBUILD_APP_INI + value: "false" - it: "Render the Deployment with standard additionalConfigFromEnvs" template: templates/deployment.yaml set: @@ -59,7 +61,7 @@ tests: path: spec.template.spec.initContainers[1].env - lengthEqual: path: spec.template.spec.initContainers[1].env - count: 8 + count: 9 - isSubset: path: spec.template.spec.initContainers[1] content: @@ -76,6 +78,8 @@ tests: value: /tmp/existing-envs - name: ENV_TO_INI_MOUNT_POINT value: /env-to-ini-mounts + - name: GITEA_REBUILD_APP_INI + value: "false" - name: GITEA_database_HOST value: my-db:123 - name: GITEA_database_USER @@ -93,7 +97,7 @@ tests: path: spec.template.spec.initContainers[1].env - lengthEqual: path: spec.template.spec.initContainers[1].env - count: 8 + count: 9 - isSubset: path: spec.template.spec.initContainers[1] content: @@ -110,6 +114,8 @@ tests: value: /tmp/existing-envs - name: ENV_TO_INI_MOUNT_POINT value: /env-to-ini-mounts + - name: GITEA_REBUILD_APP_INI + value: "false" - name: GITEA_database_HOST value: my-db-host:321 - name: GITEA_database_USER @@ -131,7 +137,7 @@ tests: path: spec.template.spec.initContainers[1].env - lengthEqual: path: spec.template.spec.initContainers[1].env - count: 7 + count: 8 - isSubset: path: spec.template.spec.initContainers[1] content: @@ -148,6 +154,8 @@ tests: value: /tmp/existing-envs - name: ENV_TO_INI_MOUNT_POINT value: /env-to-ini-mounts + - name: GITEA_REBUILD_APP_INI + value: "false" - name: GITEA_database_HOST valueFrom: secretKeyRef: diff --git a/unittests/helm/deployment/rebuildAppIni.yaml b/unittests/helm/deployment/rebuildAppIni.yaml new file mode 100644 index 0000000..202a640 --- /dev/null +++ b/unittests/helm/deployment/rebuildAppIni.yaml @@ -0,0 +1,24 @@ +suite: Test deployment app.ini rebuild +release: + name: gitea-unittests + namespace: testing +templates: + - templates/deployment.yaml +tests: + - it: Disable app.ini rebuild by default + asserts: + - contains: + path: spec.template.spec.initContainers[1].env + content: + name: GITEA_REBUILD_APP_INI + value: "false" + + - it: Enable app.ini rebuild when configured + set: + deployment.initAppIni.config.rebuild: true + asserts: + - contains: + path: spec.template.spec.initContainers[1].env + content: + name: GITEA_REBUILD_APP_INI + value: "true" diff --git a/values.yaml b/values.yaml index 8fcf312..d03e336 100644 --- a/values.yaml +++ b/values.yaml @@ -220,6 +220,10 @@ deployment: # readOnly: true initAppIni: + ## @param deployment.initAppIni.config.rebuild Rebuild app.ini on startup, preserving Gitea secret keys but removing unmanaged settings. + config: + rebuild: false + ## @param deployment.initAppIni.env Additional environment variables to pass to the init container. env: [] # - name: VARIABLE