Skip to content
Proudly Canadian, serving Windsor to Toronto, Ontario · clients welcome from every countryContact usClient login
CodeLumaDevelopment Inc.

Home / Blog / Article

CodeLuma insights · June 4, 2026 · 6 min read

Managing Environment Variables and Secrets Safely in Production Teams

How to handle API keys, passwords and configuration securely across development, staging and production: environment variables, secrets managers, rotation, least privilege, leak prevention and incident response.

Every application depends on secrets: database passwords, payment and email API keys, signing keys, third-party tokens. Leaked secrets are one of the most common causes of serious breaches, because they let attackers walk in through the front door with valid credentials. Automated bots scan public code repositories for exposed keys within minutes of a commit, and stolen cloud credentials have led to enormous bills and stolen data. Handling secrets well is not complicated, but it requires discipline from the whole team. This guide sets out practical rules.

Not all configuration is secret; treat each kind appropriately, and prefer identity-based access over stored credentials.
Not all configuration is secret; treat each kind appropriately, and prefer identity-based access over stored credentials.

Configuration versus secrets

Start by distinguishing them. Configuration is anything that varies between environments: the URL of a service, a feature toggle, a timeout value, the name of a storage bucket. Most of it is not sensitive. Secrets are values that grant access or prove identity: passwords, API keys, private keys, tokens, certificates and connection strings containing credentials. The widely followed twelve-factor approach says to keep configuration out of code and in the environment, so the same build can run anywhere. Secrets follow the same principle, with stronger protection.

A processor chip on a glowing circuit board
Original CodeLuma 3D render: a processor chip on a glowing circuit board.

Rule one: never put secrets in source code

Do not hard-code secrets or commit them to version control, even in a private repository, even temporarily. Repositories are cloned to many laptops, accessed by many people and sometimes made public by accident. Once a secret is in the history, deleting the file does not remove it; treat it as compromised and rotate it. Keep local configuration files with secrets out of version control using ignore rules, and provide an example file with placeholder values for onboarding. Add automated secret scanning, both as a pre-commit check and in your repository platform and pipeline, to catch mistakes before or shortly after they happen.

Environment variables: useful, with limits

Environment variables are a simple, widely supported way to pass configuration and secrets to applications. They keep values out of code and make it easy to vary per environment. But they have drawbacks: they may appear in process listings, crash dumps, debug pages, logs and error reports; they are inherited by child processes; and managing them by hand across many servers is error-prone. For modest systems they are acceptable with care; for larger or more sensitive setups, combine them with a secrets manager that injects values at runtime.

Use a secrets manager

Dedicated secrets managers, offered by cloud providers and as standalone tools, store secrets encrypted, control access through policies, keep audit logs, support versioning and rotation and let applications fetch secrets at startup or on demand. Benefits include central management, fine-grained permissions per service, an access trail and simpler rotation. Applications authenticate to the manager using their runtime identity, such as a cloud role attached to the workload, so no long-lived credential needs to be stored. Configure the deployment pipeline to fetch or reference secrets rather than baking them into images or artefacts.

Separate environments completely

Use different secrets for development, testing, staging and production, so a leak from a less protected environment does not endanger production. Developers should not need production credentials for daily work; provide safe sandbox accounts and test data. Restrict who can read production secrets, and limit human access in favour of automated systems. Avoid copying production data with real personal information into lower environments. Consistent infrastructure definitions, described in our infrastructure as code guide, help keep environments separate and repeatable.

Practices that reduce the likelihood and impact of leaked credentials.
Practices that reduce the likelihood and impact of leaked credentials.

Least privilege and short lifetimes

Every credential should have the minimum permissions necessary. A key that only sends email should not be able to read customer data. Create separate keys per service and per purpose so that a leak has a limited blast radius and the source of misuse can be traced. Prefer short-lived credentials, such as temporary tokens issued to workloads, over permanent keys, which reduces the window of exposure. Where third-party services offer restricted or scoped keys, IP allow-listing and spending limits, use them. Payment providers, for example, offer restricted keys; see our payment integration guide.

Rotation and revocation

Rotate secrets on a schedule and whenever people with access leave, a device is lost or exposure is suspected. Design applications to support rotation without downtime, for example by accepting two valid keys during a transition, or by reloading secrets dynamically. Automate rotation for database credentials and other secrets that your secrets manager supports. Maintain an inventory of all secrets: what they are, where they are used, who owns them and when they were last rotated. You cannot rotate what you cannot find.

A wall of sticky notes used for planning
Original CodeLuma 3D render: a wall of sticky notes used for planning.

Protect secrets in transit, at rest and in logs

  • Use TLS for every connection that carries secrets.
  • Encrypt secrets stores and backups, and control access to the encryption keys.
  • Avoid printing secrets in logs, error messages, analytics events, URLs and support tickets. Configure logging libraries to redact known patterns.
  • Do not send secrets through chat or email. Use secure sharing features of a password manager or secrets tool.
  • Protect CI/CD systems: pipeline variables are secrets too, so restrict who can view and edit them and avoid exposing them to untrusted code, such as builds from external contributors. See our CI/CD article.
  • Do not embed secrets in front-end code, mobile apps or container images; anything shipped to a client can be extracted.

Prefer identity to stored credentials

Where possible, avoid having secrets at all. Cloud platforms let workloads assume roles with permissions granted by identity, so no key needs to be stored or rotated. Service-to-service authentication using signed short-lived tokens, mutual TLS or workload identity removes entire categories of risk. Database access can often use IAM-based authentication. Moving from static keys to identity-based access is one of the most valuable long-term security improvements.

Prepare for leaks

Assume it will eventually happen and prepare a playbook: identify the leaked secret and what it grants, revoke or rotate it immediately, review logs for misuse, check for related exposure such as other secrets stored in the same place, remove it from history where possible, notify affected parties if data may have been accessed, and record lessons learned. Cloud providers and payment platforms often alert you when they detect exposed keys; monitor those notifications. Include leak response in your wider incident plan; see our security audit guide.

Make the secure way the easy way

People bypass security when it is painful. Provide simple tooling: a documented process for adding a secret, local development helpers that pull sandbox secrets, templates for new projects with secret scanning enabled, and quick access requests. Train the team, especially new developers and contractors, and review access whenever roles change.

If your team is passing keys around in chat or has never audited where credentials live, a review is overdue. Our software team can move you to a proper secrets workflow, and our hosting setup can provide managed secrets, scanning and least-privilege access from day one.


Put this into practice with CodeLuma

CodeLuma keeps secrets out of code and repositories, moves them into managed vaults with rotation and least privilege, and sets up scanning so a leaked key is caught in minutes, not months.

Start a conversation. Tell us about your project and we will reply with practical next steps, or browse all CodeLuma services. CodeLuma Development Inc. is a Canadian company that works with teams in Windsor, Essex, London, Toronto, Mississauga and across Ontario, throughout Canada and remotely.

Keep reading

Share this article: Facebook · LinkedIn · X · Email

← All articles

Ready to put this into practice?

Talk to a Canadian full-stack team that builds complex, connected systems for clients across Canada and worldwide.

Start a project