Secrets management: keeping keys and passwords out of your code

Line drawing of keys lifted out of an open code document and dropped into a small safe, the safe in blue

An API key in a Git repository is one of the cheapest ways into a company. A practical guide to finding the secrets you already leak, moving them somewhere safe and making new leaks hard.

In short

Secrets management is the practice of storing, distributing, rotating and auditing credentials such as API keys, database passwords, tokens and certificates outside source code. Find existing leaks with secret scanning, move secrets into a central vault or your cloud's secret store, prefer short-lived and workload-identity credentials over static keys, rotate what remains, and block new secrets at commit time.

Credentials in code spread quietly: into forks, build logs, container images, wikis and laptops. Once a secret is in Git history, deleting the file does not remove it. This guide is written by Devies, a Gothenburg-based product and engineering consultancy, for CTOs, platform leads and developers who want a clear path to secrets management that works in day-to-day development, not only on paper.

What is secrets management, and why does it matter?

Secrets management is the set of tools and routines that control the full life cycle of machine credentials: creation, storage, access, distribution, rotation, revocation and audit.

Definitions:

  • A secret is any value that grants access on its own: passwords, API keys, OAuth client secrets, access tokens, private keys, signing keys and connection strings.
  • A secrets vault is a central service that stores secrets encrypted, controls who and what may read them, and logs every access.
  • Workload identity is a way for a running service to prove who it is to a cloud provider or another service without a stored secret, for example through a managed identity or federated token.

Why it matters: leaked credentials let attackers walk in through the front door, with no exploit needed. They are hard to spot in logs because the access looks legitimate. The OWASP Secrets Management Cheat Sheet lists centralisation, automation of rotation, least privilege, auditing and detection as core principles.

Typical places secrets hide:

Location

Why it leaks

Better alternative

Source code and config files

Why it leaks

Committed by mistake, copied to forks

Better alternative

Vault reference or environment injection at runtime

CI/CD variables and logs

Why it leaks

Printed in debug output, shared across projects

Better alternative

Masked, scoped variables or OIDC federation to the cloud

Container images

Why it leaks

Baked in at build time, pushed to registries

Better alternative

Mounted at runtime from a secret store

Chat, tickets and wikis

Why it leaks

Pasted to "help a colleague"

Better alternative

Shared through the vault with access control

Developer laptops

Why it leaks

Long-lived personal keys with broad rights

Better alternative

Short-lived, single sign-on based credentials

How do you get secrets under control, step by step?

Get secrets under control in the order that removes the most risk fastest: find what is already exposed, contain it, then change how secrets are created and used.

Path from a magnifying glass over code to a vault feeding running services, with a gate at the commit step in blue
  1. Scan everything you have. Run a secret scanner across all repositories including full Git history, CI logs, container images and infrastructure code. Expect findings; the point is to know.
  2. Triage and revoke. For each finding: is it valid, what does it grant, who owns it? Revoke or rotate valid secrets first, starting with production and cloud administrator keys. Removing the secret from code without rotating it does not help.
  3. Choose one home for secrets. Pick a central vault or your cloud provider's secret store per environment. Avoid several overlapping tools.
  4. Replace static keys with identities where possible. Use managed or workload identities for services in the cloud and OIDC federation for CI pipelines, so there is no long-lived secret to steal.
  5. Inject secrets at runtime. Applications read secrets from the vault at start-up or on demand, not from files in the image or repository.
  6. Apply least privilege. Each secret grants only what one service needs, in one environment. Separate production from test completely.
  7. Automate rotation. Set rotation periods by risk, and rotate immediately when a person with access leaves or a leak is suspected. Prefer dynamic secrets that expire on their own.
  8. Block new leaks at the source. Enable pre-commit hooks and server-side push protection. GitHub push protection, for example, blocks pushes that contain recognised secret patterns before they reach the repository.
  9. Log and alert on access. Vault audit logs should feed your monitoring, with alerts for unusual reads or use from unexpected locations.

How do you design for fewer secrets in the first place?

The best secret is the one that does not exist, so design systems so that services authenticate through identities and short-lived tokens rather than shared static keys.

Practical patterns:

  • Cloud-native identities: services running in Azure, AWS or Google Cloud can use built-in managed identities to reach databases, storage and key vaults without passwords.
  • Federated CI: modern CI platforms can exchange a signed identity token for short-lived cloud credentials per pipeline run.
  • Dynamic database credentials: a vault creates a user with a short lifetime on request and removes it afterwards.
  • Single sign-on for people: developers use their own identity with MFA to get temporary access, instead of shared admin keys. This connects secrets management with identity and access management.

Where static secrets remain, typically for third-party APIs that only issue keys, keep them in the vault, restrict them by IP or scope if the provider allows, and rotate them on a schedule.

How does secrets management fit into the pipeline?

Secrets management fits into the pipeline as both a protected resource and a gate: the pipeline must use secrets safely and must stop new ones from entering the code.

Checks to add:

  • Secret scanning on every pull request, alongside static analysis and dependency checks.
  • Pipeline credentials scoped per project and environment, never shared across all builds.
  • Logs configured to mask secrets, and debug output reviewed before it is enabled in production pipelines.
  • Release artefacts and container images scanned for embedded credentials before publishing.

A compromised pipeline is a supply chain risk, because whoever controls the build controls what customers receive; see software supply chain security. For the wider set of pipeline gates, read DevSecOps: security built into every release.

What should you do when a secret leaks?

When a secret leaks, rotate first and investigate second, because attackers scan public repositories continuously and can use a key within minutes.

  1. Revoke or rotate the secret immediately.
  2. Check access logs for use of the old secret since it was exposed.
  3. Remove it from code and history where practical, knowing that copies may already exist.
  4. Find out why it leaked and fix the cause: a missing hook, a debug log, an unclear process.
  5. Record the incident. If personal data or production systems may have been accessed, follow your incident and reporting procedures.

Devies

Want a second opinion?

Devies, a Gothenburg-based product and engineering consultancy, works on questions like this with Nordic product and IT teams. Tell us where you are and we will suggest a sensible first step.

Talk to Devies

Frequently asked questions

What counts as a secret in software development?

A secret is any value that grants access on its own, without further proof of identity. Typical examples are passwords, API keys, OAuth client secrets, personal access tokens, private keys for TLS or code signing, cloud access keys and database connection strings with embedded credentials. Configuration values such as feature flags or URLs are not secrets, but they are often stored in the same files, which is how real secrets end up in code.

Is it enough to delete a secret from the repository?

No. Git keeps the full history, so a deleted secret remains in earlier commits, in clones, forks and CI caches. Treat any secret that has been committed as compromised: rotate or revoke it first, check logs for misuse, and only then clean the history if needed. Push protection and pre-commit scanning are the way to stop the problem recurring.

Should we use a dedicated vault or the cloud provider's secret store?

If you run mainly in one cloud, the provider's secret store integrates well with managed identities, access policies and logging, and is usually the simplest choice. A dedicated vault product makes sense across several clouds and on-premises systems, or when you need features such as dynamic database credentials and advanced policies. What matters most is having one clearly owned home per environment.

How often should secrets be rotated?

Rotate by risk and by event. High-privilege and production secrets should rotate automatically and frequently, and immediately when someone with access leaves or a leak is suspected. Better still, replace long-lived secrets with short-lived tokens or workload identities that expire on their own. Avoid manual rotation schedules nobody follows: automate, or remove the secret entirely.

Sources

Here’s more

Join Our Newsletter

We handle your data according to our privacy policy.

Shopping Cart

We use cookies

We use cookies to improve your experience on this website. You may choose which types of cookies to allow and change your preferences at any time. Disabling cookies may impact your experience on this website. You can learn more by viewing our Cookie Policy.

Check out

Contact Form text

We will contact you text

We handle your data according to our privacy policy.