Repository navigation
feat(chart): add extraEnv and rollout on secrets - #672
Conversation
Operators can't inject settings from their own Secrets or ConfigMaps, such as MONGO_URI, and pods don't restart when a Secret they read changes. Add extraEnv, extraEnvFrom and rolloutOnSecrets to the app's values, for the app and the worker. Rendering fails if extraEnv sets a variable that the chart sets. foca-common 0.5.0. Closes #626 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fail if extraEnv has a secret as a literal value, as the chart does for the key secret in the overlay, or sets a name twice. Check the types of the new values for app charts without the schema. Check the annotation in CI for the app and the worker. Docs: the MONGO_URI example needs config.db.host; the annotation is an unsalted hash. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The chart's Service and probes expect port 8080, and foca.gunicorn_config reads PORT. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR releases foca-common 0.5.0 with validated Sequence diagram for Secret checksum rollout during Helm upgradesequenceDiagram
actor User
participant Helm
participant Kubernetes
participant AppDeployment
participant WorkerDeployment
User->>Helm: helm upgrade
Helm->>Kubernetes: lookup Secret data
Kubernetes-->>Helm: Secret data or not found
Helm->>Helm: sha256sum rolloutOnSecrets data
Helm->>Kubernetes: apply Deployments with checksum/secrets
Kubernetes->>AppDeployment: roll out pods when annotation changes
Kubernetes->>WorkerDeployment: roll out pods when annotation changes
Flow diagram for validated extra environment configurationflowchart TD
Start["Read extraEnv and extraEnvFrom"] --> Types{"Valid list types?"}
Types -- No --> Fail["Rendering fails"]
Types -- Yes --> EnvChecks["Check duplicate, reserved, and literal secret values"]
EnvChecks --> Safe{"Validation passes?"}
Safe -- No --> Fail
Safe -- Yes --> Render["Render env and envFrom"]
Render --> Targets["App and worker containers"]
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #672 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7231 7231
=========================================
Hits 7231 7231
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="charts/foca-common/templates/_helpers.tpl" line_range="353" />
<code_context>
+{{- end -}}
+{{- end -}}
+{{- if $data -}}
+{{- toJson $data | sha256sum -}}
+{{- end -}}
+{{- end -}}
</code_context>
<issue_to_address>
**Weak secrets become guessable**
When a listed Secret contains a weak value and a cluster reader can read Deployments, `rolloutChecksum` hashes all listed Secret data without a key, then exposes that digest in `checksum/secrets`; the reader can test guesses offline and recover the weak value.
Use a keyed MAC with a separately protected key, or avoid the annotation and use a Secret-watching controller.
</issue_to_address>
### Comment 2
<location path="charts/foca-common/templates/_deployment.tpl" line_range="79" />
<code_context>
+ {{- . | nindent 8 }}
+ {{- end }}
+ {{- with include "foca-common.extraEnvFrom" . }}
+ envFrom:
+ {{- . | nindent 8 }}
+ {{- end }}
</code_context>
<issue_to_address>
**EnvFrom can override the app port**
When an `extraEnvFrom` Secret or ConfigMap contains `PORT` with a value other than `8080` and the app uses `foca.gunicorn_config`, the chart does not set `PORT` in `env`, so `extraEnvFrom` can supply it despite `extraEnv` rejecting it. `foca.gunicorn_config` then binds to that port (or fails on an invalid value), while the Service and probes remain fixed at 8080, leaving the app unavailable.
Set `PORT` explicitly to `8080` in the app container’s `env` so it takes precedence over `envFrom`.
</issue_to_address>
### Comment 3
<location path="charts/foca-common/templates/_helpers.tpl" line_range="347" />
<code_context>
+{{- if not (and (kindIs "string" .) .) -}}
+{{- fail (printf "%s.rolloutOnSecrets must be a list of names of Secrets" $key) -}}
+{{- end -}}
+{{- $secret := lookup "v1" "Secret" $root.Release.Namespace . -}}
+{{- if $secret -}}
+{{- $_ := set $data . $secret.data -}}
</code_context>
<issue_to_address>
**Pods keep stale Secret values**
When a listed Secret is created or updated by the release, or changes after `lookup` reads it but before the upgrade completes, `lookup` reads the current cluster Secret, and `rolloutChecksum` hashes that snapshot rather than the Secret data ultimately applied; on first install the data can also be absent. The checksum therefore misses the change and existing pods keep using stale Secret-derived values.
Use a rollout mechanism that reacts to the Secret data actually applied or changed, rather than relying solely on the pre-apply `lookup` snapshot.
</issue_to_address>
### Comment 4
<location path="charts/foca-common/templates/_helpers.tpl" line_range="246" />
<code_context>
+{{- define "foca-common.extraEnv" -}}
+{{- $key := .settings.valuesKey -}}
+{{- $values := index .root.Values $key -}}
+{{- $extraEnv := $values.extraEnv | default list -}}
+{{- if not (kindIs "slice" $extraEnv) -}}
+{{- fail (printf "%s.extraEnv must be a list of EnvVar" $key) -}}
</code_context>
<issue_to_address>
**Invalid settings are ignored**
When a schema-less app chart sets `extraEnv`, `extraEnvFrom`, or `rolloutOnSecrets` to `false`, `0`, an empty string, or an empty map, `default list` converts these values to an empty list before the type checks in the helpers run, so the checks pass and the invalid setting is silently ignored.
Validate each setting's type before applying `default list`.
Also at `charts/foca-common/templates/_helpers.tpl:293`, `charts/foca-common/templates/_helpers.tpl:338`.
</issue_to_address>Linked issues
#626 feat(chart): add extraEnv and rollout on secrets · 5 of 6 met · 1 to check by hand
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Add extraEnv, extraEnvFrom, and rolloutOnSecrets for the app and worker containers, not the bundled broker or MongoDB. | ✅ Met | Both Deployment templates use the new helpers for their app/worker containers. |
| 2 | Fail rendering when extraEnv sets variables the chart owns, when the relevant chart settings are in use. | ✅ Met | The reserved-name helper checks the chart-owned variables, including the conditional MongoDB, broker, plugin-store, and Gunicorn variables. |
| 3 | Use lookup for rolloutOnSecrets and document that helm template and Argo CD do not read Secrets, with Stakater Reloader as an alternative and no dependency on it. | ✅ Met | The checksum helper uses lookup; the documentation describes its behavior and the Reloader alternative. |
| 4 | Add the schema, petstore example, documentation, and migration notes. | ✅ Met | The schema and example values are updated, and the docs explain that older app charts need the updated app-values schema to use the new settings. |
| 5 | Test app and worker environment lists, reserved-name failures, and rolloutOnSecrets rendering with helm template. | ✅ Met | Template tests cover both Deployments, reserved variables, and rendering without a Secret lookup result. |
| 6 | Pass helm lint, chart unit tests, and the kind install. | 👀 Check by hand (runtime) | Confirm the PR's CI run passes the chart install check; the description says the local kind install could not run. |
Sourcery assessment
Needs a human reviewer. 4 findings to address first, and when a listed Secret changes, Helm puts an unsalted hash of its data in the Deployment annotation; anyone who can read Deployments can test guesses against that hash, potentially exposing weak secret values. Reverting removes the annotation going forward, but cannot undo guesses or disclosure that already occurred.
Blocking findings: charts/foca-common/templates/_helpers.tpl:353, charts/foca-common/templates/_deployment.tpl:79, charts/foca-common/templates/_helpers.tpl:347, charts/foca-common/templates/_helpers.tpl:246
extraEnvFrom could set PORT, which the Service and probes fix at 8080,
so set it in the app container. Wrong types such as false or {} were
ignored without the schema, as `default list` replaced them before
the type checks.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eat/626-chart-extra-env
Summary
foca-common 0.5.0:
<valuesKey>.extraEnv,extraEnvFromandrolloutOnSecrets, for the app and worker containers, not for the bundled broker or MongoDB.extraEnvsets a variable that the chart sets (FOCA_CONFIG_OVERLAY,PORT, andGUNICORN_CMD_ARGS,MONGO_USERNAME,MONGO_PASSWORD,CELERY_BROKER_URL,CELERY_RESULT_BACKEND,FOCA_PLUGINS_STORE_KEY_SECRETif the chart's settings for them are in use).extraEnv(MONGO_PASSWORD,FOCA_PLUGINS_STORE_KEY_SECRET, andMONGO_URIorCELERY_*with credentials), and on wrong types for app charts without the schema.rolloutOnSecrets: a checksum of the data of the named Secrets becomes the annotationchecksum/secrets, vialookup; nothing inhelm templateand Argo CD, without an error. Documented, with Stakater Reloader as the alternative, with no dependency on it.docs/helm-chart.md, including what older app charts need).Closes #626
Verification
pytest charts/foca-common/testswith Helm 3.22.1 (not necessarily the CI version; CI runs the snapshots with its own),helm lint --strict: pass.ruff check,ruff format --check,mypy,mkdocs build --strict: pass.Chart installis the only real test oflookup; see the checks of this PR.Declined review findings
{"name": "A", "value": 1}: the API server rejects non-string values at install.🤖 Generated with Claude Code
Summary by Sourcery
Extend the common Helm chart with safe custom environment configuration and optional Secret-triggered rollouts for application and worker pods.
New Features:
Bug Fixes:
Enhancements:
Build:
CI:
Documentation:
Tests: