Secrets management: keeping keys, tokens and passwords out of code

Secrets management is the practice of keeping API keys, access tokens, database passwords and private keys out of source code and other places they can be copied from. Secrets leak through repositories, CI logs, container images and chat tools. The fix is a dedicated secrets manager, short-lived credentials, routine scanning, and least privilege, so that when one secret does leak you can rotate it quickly and the damage stays small.

What counts as a secret

A secret is any value that proves identity or grants access on its own. Typical examples are cloud provider access keys, API tokens for SaaS platforms, database connection strings with embedded passwords, SSH and TLS private keys, signing keys, webhook secrets and service account credentials.

What makes secrets dangerous is that they are bearer credentials: whoever holds the value can usually use it, from anywhere, without passing a second factor. A leaked key does not care who copied it. That is why secrets need different handling from ordinary configuration values such as hostnames or feature flags.

Where secrets leak

Most leaks are not sophisticated. They happen because a secret was placed somewhere convenient that turned out to be widely readable. The common paths are:

  • Source code repositories. Keys hard-coded in application code or committed in configuration files such as .env. Deleting the line in a later commit does not remove it: the value stays in the version history and in every clone and fork made before the change.
  • CI/CD logs. Build scripts that echo environment variables, debug output that prints a full request with an authorization header, or test failures that dump configuration. Build logs are often visible to far more people than the secret store is.
  • Container images. Secrets copied into an image during the build, or passed as build arguments, can persist in image layers even when a later step deletes the file. Anyone who can pull the image can inspect those layers.
  • Chat and ticketing tools. Credentials pasted into a chat channel or a support ticket to “just get it working” end up stored, searchable and synced to many devices.

Attackers know these places too. MITRE ATT&CK describes the search for credentials left in files, repositories and other storage as Unsecured Credentials (T1552), a technique used after initial access to expand control. A single exposed token can turn a minor foothold into access to cloud accounts, build pipelines or customer data.

Why deleting a leaked secret is not enough

The most common mistake after a leak is treating it as a cleanup task. Removing the secret from the file, scrubbing the log or rewriting Git history all reduce future exposure, but none of them undo the past. You cannot know who already copied the value from a repository, a cached build log, a mirrored image or a chat export.

The only reliable response is to rotate: issue a new credential, move every legitimate consumer to it, and revoke the old one so it stops working everywhere. Then review the provider’s access logs for use of the old credential during the exposure window. This is why the goal of a secrets programme is not only to prevent leaks but to make rotation routine and cheap. If rotating a key means a risky, manual change across ten services, teams will put it off, and the leaked key stays valid.

Building a rotation-friendly practice

Centralise secrets in a dedicated secrets manager

A secrets manager stores secrets encrypted, controls who and what can read each one, logs every access and supports rotation. Applications fetch secrets at runtime instead of carrying them in code, images or static config files. The OWASP Secrets Management Cheat Sheet covers the design choices in detail, including centralisation, access control, auditing and the secret lifecycle.

Centralising also gives you an inventory. You cannot rotate what you do not know exists, and a single store answers the questions “where is this key used?” and “who can read it?”.

Prefer short-lived credentials

Where possible, replace long-lived static keys with credentials that are issued on demand and expire on their own. Workload identity, federated identity for CI pipelines and dynamically generated database credentials are common patterns. A credential that expires shortly after issue gives an attacker who finds it in a log very little to work with, and it removes most of the rotation burden because expiry does the rotation for you.

Scan repositories, history and pipelines

Prevention fails sometimes, so detection has to cover the gap. Secret scanning tools look for patterns that match known credential formats in code and commit history. GitHub secret scanning, for example, scans repositories for known secret types, and push protection can stop a commit that contains one before it lands. Scan the full history, not just the current branch, because old commits still hold old secrets. Extend scanning to CI logs, container images and infrastructure-as-code where your tooling allows it.

Apply least privilege to every secret

Assume that some secret will eventually leak, and limit what it can do. Scope each credential to one service and the smallest set of actions and resources it needs, and avoid sharing one powerful key across several applications. A read-only token for a single bucket that leaks is an incident; an administrator key for the whole cloud account that leaks is a crisis. Separate credentials per environment so a development key never opens production.

Why this matters

Analysis: secret leaks sit outside the usual patch cycle. There is no vendor fix and no CVE to track, so they rarely appear in vulnerability dashboards built around CVSS scores or the CISA KEV catalog. Yet an exposed credential often gives an attacker direct, authenticated access without exploiting any flaw at all. For a typical organisation with many repositories, pipelines and cloud accounts, the realistic risk is not one dramatic breach but a steady trickle of exposed keys, each one a potential entry point. A practice that makes rotation fast turns each of those into a short, contained task.

What defenders can do

  1. Treat every exposed secret as compromised. Rotate and revoke it first, then clean up the code, logs or images, then check the provider’s access logs for misuse.
  2. Enable secret scanning and push protection on all repositories, and run a one-time scan of full history to find secrets that are already exposed.
  3. Move secrets into a dedicated secrets manager and remove them from source code, .env files committed to version control, container images and build arguments.
  4. Replace long-lived keys with short-lived credentials for CI pipelines and workloads wherever the platform supports it.
  5. Scope every credential with least privilege, one purpose per secret and separate credentials per environment.
  6. Harden CI/CD output. Use the pipeline’s secret masking, avoid printing environment variables, and restrict who can read build logs.
  7. Set a policy for chat and tickets: credentials are never shared there, and anything pasted by mistake is rotated.
  8. Rehearse rotation. Rotate a few important secrets on a schedule so the process is documented and proven before an emergency forces it.

Secrets work fits alongside vulnerability management rather than replacing it; for how to rank software flaws, see our guide to patch prioritization with CVSS, EPSS and KEV.

Further reading

Related free tool: test how long a password would take to crack with our Password Strength Checker. It runs entirely in your browser and nothing you type is sent or stored.

Explainers on Cyber Strike News

Sources

Spotted an error or outdated detail? Email contact@cyberstrikenews.com with the article link. We correct and note every change. Read our Editorial Policy.