Environments, Configuration, and Secrets
Design the environments a change passes through, separate configuration from code, manage secrets safely, and keep environments consistent and test data safe.
Introduction
The pipeline builds one artifact. What makes it behave differently in staging and production is configuration: database addresses, feature switches, credentials, limits. Most deployment surprises come from here: a setting that differs, a secret that expired, a variable that was never set.
This lesson covers how many environments you need, how to configure them without rebuilding, and how to handle secrets without leaking them.
Learning Objectives
- Decide which environments are needed and what each is for.
- Separate configuration from code and from secrets.
- Validate configuration at startup and in the pipeline.
- Store, distribute, rotate, and audit secrets safely.
- Manage test data and keep environments consistent.
- Use AI to review configuration without exposing secrets.
1. Environments and Their Purpose
| Environment | Purpose | Data | Typical lifetime |
|---|---|---|---|
| Local / development | Developer work and fast feedback | Synthetic | Per developer |
| CI / ephemeral | Automated tests on every change | Synthetic, created per run | Minutes |
| Preview (optional) | Review a branch or pull request | Synthetic | Days |
| Staging | Production-like verification before release; rehearse migrations and deployments | Synthetic or anonymized | Long-lived |
| Production | Real users | Real | Permanent |
Do not create more environments than you can keep consistent and afford. For a small pilot, local, CI, staging, and production are usually enough. Each extra environment must have an owner, a purpose, and a cost.
Principles:
- Production-like staging. It should use the same artifact, the same deployment method, the same infrastructure modules, and similar (smaller) sizes. A staging environment that differs fundamentally does not predict production.
- Production data stays in production. Staging gets synthetic or properly anonymized data, in line with the quality strategy’s test data rules.
- Ephemeral where possible. Environments created from code (lesson 4) for a test and destroyed afterwards cost less and cannot drift.
- Access matches risk: wide in development, restricted in production, with all production changes through the pipeline.
2. Separate Configuration From Code
The same artifact must run everywhere, so anything that differs between environments is supplied from outside.
Three categories, handled differently:
| Category | Examples | Where it lives | Secret? |
|---|---|---|---|
| Constants and defaults | Maximum body length, validation rules | In the code | No |
| Environment configuration | Database host, log level, feature flags, queue name, limits | Environment variables or config files per environment, versioned | No |
| Secrets | Database password, API keys, signing keys | Secrets manager | Yes |
Guidelines:
- Business rules stay in code, versioned and tested with it. Do not move behavior into configuration to avoid redeploying; configuration changes can break production without the review and tests that code receives.
- Version non-secret configuration in the repository, per environment, reviewed like code. Apply it through the pipeline.
- Use sensible defaults that work locally, and require explicit values for anything dangerous in production.
- Name configuration clearly and document every setting: purpose, type, default, and effects (the documentation course generates the configuration reference from the schema).
- Avoid configuration explosion. Every setting multiplies the test matrix. Add one only for a real difference.
3. Validate Configuration
Configuration errors should fail early and loudly.
1
2
3
4
5
6
7
8
9
class Settings(BaseSettings):
environment: Literal["local", "staging", "production"]
database_url: SecretStr
queue_url: str
max_upload_mb: int = Field(10, ge=1, le=50)
summary_provider_timeout_s: float = Field(30, gt=0)
feature_evidence_links: bool = False
settings = Settings() # raises at startup if anything is missing or invalid
- Fail at startup with a clear message if required values are missing or invalid, instead of failing on the first request.
- Validate in the pipeline: check that each environment’s configuration satisfies the schema before deployment.
- Do not log secret values. Use types that mask them, and log which settings were loaded, not their contents.
- Test configuration: a test that loads each environment’s configuration against the schema catches missing keys before release.
4. Feature Flags
Feature flags separate deployment (code is in production) from release (users can use it).
- Use them to hide incomplete features, enable gradual rollout, and provide a quick off-switch for risky behavior, such as the AI summary generation.
- Give every flag an owner, a purpose, and an expiry. Flags left forever become hidden complexity and untested combinations.
- Test both states of a flag that guards important behavior.
- Remove flags once a feature is fully released, as a planned cleanup.
- Distinguish release flags (temporary), operational flags (kill switches, long-lived), and permission flags (entitlements), and handle them differently.
5. Secrets Management
A secret is anything that grants access: passwords, tokens, keys, certificates. They require stricter handling than configuration.
Rules:
- Never commit secrets. Not in code, configuration, images, test fixtures, documentation, or chat. Add a secret scanner to the pipeline and as a pre-commit check.
- Use a secrets manager (cloud or self-hosted) as the source. Applications read secrets at startup or on demand using their own identity, with permission to read only their own.
- Prefer short-lived, automatically issued credentials (for example, database credentials generated on demand, or workload identity) over long-lived static ones.
- Least privilege. The API can read the database credential; the worker can read that plus the queue credential; neither can read the deployment keys.
- Separate secrets per environment. A leak in staging must not expose production.
- Rotate regularly and after any suspected exposure, and make rotation routine, with a documented procedure that has been practiced. A secret that cannot be rotated without an outage is a risk.
- Audit access to secrets, and review who has it.
- Redact secrets from logs, error messages, crash reports, and support exports.
If a secret leaks, treat it as compromised: revoke and rotate immediately, then investigate the exposure. Removing it from the repository history does not make it safe again.
Document the process (who can create, change, and rotate each secret, and how) without including values.
6. Test Data in Non-Production Environments
Carry the quality strategy’s rules into delivery:
- Never copy production data into lower environments unless it is anonymized by a reviewed, tested process, and approved by whoever owns data protection.
- Generate synthetic data with a repeatable seed script, so that staging can be rebuilt on demand.
- Include realistic volume and shape in staging for performance rehearsals, such as the 50,000-row import (illustrative).
- Keep the model provider safe in tests: use a stub or a sandbox key with spend limits in lower environments; never use production credentials.
7. Keeping Environments Consistent
| Risk | Control |
|---|---|
| Environments built by hand differ | Create all from infrastructure code |
| Configuration keys missing in one environment | Schema validation per environment in the pipeline |
| Versions of dependencies differ | The same artifact and locked dependencies everywhere |
| Staging is smaller and hides performance problems | Rehearse with production-scale data periodically |
| Manual fixes in production | Restrict access; require pipeline changes; detect drift |
| Third-party sandboxes behave differently from production | Contract tests and a small live check outside the main pipeline |
Maintain an environment matrix that lists, per environment, the infrastructure size, configuration highlights, data source, who has access, and known differences from production. Known differences are acceptable; unknown ones are not.
8. Using AI With Configuration and Secrets
The main risk is leaking secrets into the tool. Set the rule first: no real secrets or production hostnames in prompts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Act as a configuration reviewer.
Inputs (all values replaced by placeholders such as <REDACTED>):
- Configuration schema and the per-environment configuration files:
[paste]
- Deployment definitions showing how configuration and secrets are
injected: [paste]
- Rules: secrets only from the secrets manager; no environment-
specific code paths.
Tasks:
1. List settings present in one environment but missing in another.
2. List settings that differ between staging and production, and
whether each difference looks intentional.
3. List any value that looks like a secret stored in plain
configuration.
4. List settings without validation, defaults that are unsafe in
production, and flags without an owner or expiry.
5. List secrets that more services can read than need to.
Do not guess values. Mark uncertainty.
Use AI to draft the configuration reference, the environment matrix, and a rotation runbook skeleton, then verify each against the real system. Run a secret scanner on anything AI produces before committing, since models sometimes insert realistic-looking example credentials.
9. Practical Exercise
For SignalDesk (or your project):
- Decide the environment set and purpose of each; write the environment matrix with known differences from production.
- Define the configuration schema with validation, and document each setting.
- Create per-environment configuration files in version control, with no secrets, and a test that validates all of them against the schema.
- Move all secrets to a secrets manager with per-service, per-environment access, and remove any from code, images, and history where possible (rotating any that were exposed).
- Add a secret scanner to the pre-commit hook and the pipeline.
- Add a feature flag for AI summaries, with owner, purpose, expiry, and a test of both states.
- Write a synthetic data seed for staging, and a rule that forbids unreviewed production copies.
- Practice rotating one secret in staging with no downtime, and write the procedure.
- Run the AI configuration review with redacted inputs and verify its findings.
Verification
You are ready to continue when:
- The same artifact starts in every environment with only configuration and secrets differing.
- Invalid or missing configuration stops startup with a clear message, and the pipeline validates each environment’s configuration.
- No secrets exist in the repository, images, logs, or documentation, and scanners enforce this.
- Each service can read only the secrets it needs, and a rotation procedure has been practiced.
- Feature flags have owners and expiry dates.
- Non-production environments contain only synthetic or approved anonymized data.
Key Takeaways
- One artifact, many environments: only configuration and secrets differ.
- Keep business rules in code, environment settings versioned, and secrets in a secrets manager.
- Validate configuration early, and test every environment’s configuration.
- Secrets need least privilege, separation, rotation, and a plan for leaks.
- Never put real secrets into AI tools; use AI to review structure, not values.
What’s Next?
We now have an artifact, a place to run it, and its configuration. Next, we decide how to change what is running: deployment strategies that limit the damage of a mistake and make rollback a routine action.