Field Notes: Secrets & Policies in Real Teams
Short lessons from secrets setups and policy gates that went wrong, grouped by the mistake they prevent.
Secrets
- Inventory before tooling. The first Vault project in most companies stalls because nobody knows where secrets live. Grep the repos (gitleaks with a baseline), the CI variable settings, the Kubernetes Secrets and the wiki, and write down owner and consumers for each. Migrate the most dangerous first: cloud admin keys, database superusers, signing keys.
- A secret in Git history is a rotation task, not a cleanup task. Rewriting history is optional; rotating is not (Secret Scanning).
- The rotation you never practised doesn't work. Rotate every static secret on a schedule, automatically, so the day you rotate in an incident is a normal day. Track "last rotated" in KV custom metadata or the cloud store's tags and alert on stale ones.
- Short TTLs need renewal that works. Teams that set 1-hour database leases and then discover at 3 a.m. that the app never reconnects end up raising TTLs to a month. Test expiry in staging: set tiny TTLs (
default_ttl=2m) and watch the app survive. - Don't build a second secret zero. An AppRole
secret_idstored as a long-lived CI variable is a static Vault password with extra steps. Use JWT/OIDC for CI, Kubernetes auth for pods, cloud auth for VMs (JWT/OIDC Auth for CI). - Name paths for access control.
secret/<team>/<app>/<env>or mounts per environment, decided once. Paths are what policies match; a flat namespace forces wildcard grants. - Never print secrets to debug. Print
keys, lengths or a hash prefix:vault kv get -format=json … | jq '.data.data | keys',printf %s "$PW" | sha256sum | cut -c1-8. - Environment variables leak into crash dumps and child processes. Prefer files on tmpfs that are re-read on change (How Secrets Leak).
- Plan files and state are secret. Encrypted remote state, restricted access, short artifact retention, and write-only arguments where providers offer them.
- Break glass, on paper. Keep a documented emergency path (Shamir/recovery key holders, a break-glass age key for SOPS, a sealed envelope account for the cloud) and test it twice a year. An outage of Vault itself must not lock you out of fixing Vault.
- Vault is now tier-0. When every app reads secrets at start, a Vault outage is a platform outage. Run it HA, monitor it, cache in agents, and make apps keep working with the credentials they already have.
Policies
- Messages are the user interface. "deny" is useless; "aws_db_instance.main: storage_encrypted must be true (see wiki/db-standards)" gets fixed in minutes.
- Warn before you deny. Introduce rules as
warn(or Audit/dryrun in clusters), publish the violation count, give teams a date, then switch todeny. - Exceptions are data, with an expiry and a ticket. Disabled checks become permanent; exceptions with an end date come back for review automatically. Here is a Conftest gate that fails when an exception has expired:
bash · try it yourself
mkdir -p policy data
cat > policy/workloads.rego <<'REGO'
package main
deny_root contains msg if {
input.kind == "Deployment"
not input.spec.template.spec.securityContext.runAsNonRoot
msg := sprintf("%s must run as non-root", [input.metadata.name])
}
# exceptions are data: who, which rule, why, until when
active(e) if e.expires > data.today
exception contains rules if {
some e in data.exceptions
e.name == input.metadata.name
active(e)
rules := [e.rule]
}
warn contains msg if {
some e in data.exceptions
e.name == input.metadata.name
not active(e)
msg := sprintf("exception for %s (%s) expired on %s: fix it or renew via %s", [e.name, e.rule, e.expires, e.ticket])
}
REGO
cat > data/exceptions.yaml <<'YAML'
exceptions:
- {name: legacy-importer, rule: root, expires: "2026-12-31", ticket: LNK-142}
- {name: link-preview, rule: root, expires: "2026-09-30", ticket: LNK-117}
YAML
cat > deploy.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata: {name: legacy-importer}
spec: {template: {spec: {containers: [{name: app, image: "ghcr.io/acme/importer:2.0.1"}]}}}
---
apiVersion: apps/v1
kind: Deployment
metadata: {name: link-preview}
spec: {template: {spec: {containers: [{name: app, image: "ghcr.io/acme/preview:1.0.3"}]}}}
YAML
# the pipeline passes today's date as data, so the policy itself stays deterministic
echo '{"today": "2026-10-28"}' > data/today.json
conftest test -d data deploy.yaml; echo "exit $?"WARN - deploy.yaml - main - exception for link-preview (root) expired on 2026-09-30: fix it or renew via LNK-117
FAIL - deploy.yaml - main - link-preview must run as non-root
EXCP - deploy.yaml - main - data.main.exception[_][_] == "root"
4 tests, 1 passed, 1 warning, 1 failure, 1 exception
exit 1
- Test policies like code (Testing Policies): every rule has a firing and a non-firing test, coverage is measured, and the policy repo has its own CI.
- Version and pin policies. Pipelines consume a tagged policy bundle; a policy change is a release with notes, not a silent edit that breaks 40 repositories at once.
- Same policy, every enforcement point. A rule that exists only in admission surprises developers at deploy time; a rule only in CI can be bypassed. Run it in both, from the same source.
- Check the plan, not just the code, and check the rendered manifests, not the templates.
- Keep the gate fast. Conftest on a plan JSON takes milliseconds; the slow part is
plan. Cache providers, split states, and run policy checks in the same job as the plan. - Watch for the always-passing policy. A refactor of plan JSON (a new provider version renames an attribute) can make a rule undefined. Include a known-bad fixture in the policy repository's tests and in the pipeline itself, so a rule that stops firing breaks the build.
Test yourself
Why should policy exceptions carry an expiry date?
- Conftest requires it
- So they come back for review automatically instead of becoming permanent holes
- To speed up evaluation
- For billing
Why does the session pass today's date as data instead of reading the clock in Rego?
- Rego has no time functions
- It keeps policy evaluation deterministic and testable; the pipeline decides the date
- Conftest forbids clocks
- Dates must be JSON