Join our Newsletter — 33% off our NHI Course
Home› FAQ› Governance, Ownership & Risk› What is the difference between storing secrets in…
Governance, Ownership & Risk

What is the difference between storing secrets in code and storing them in a vault?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated September 28, 2026 Domain: Governance, Ownership & Risk

Storing secrets in code makes them part of the application lifecycle, where they are easy to copy and difficult to control. Storing them in a vault centralises access, supports auditing, and makes rotation and revocation more manageable. The core difference is governance. Code distributes secrets. A vault keeps them under explicit access control.

Why Code and Vaults Create Different Secret-Control Boundaries

secrets in code become part of the software artifact itself, which means they move wherever the code moves: source control, build pipelines, test environments, developer machines, and logs. A vault changes the control boundary. The secret stays outside the application and is retrieved at runtime under policy, so access can be mediated, logged, rotated, and revoked without editing the codebase.

The practical difference is not just storage location, it is whether the secret is governed as application content or as controlled credential material. When a secret is embedded in code, every copy of the repository becomes a potential copy of the secret. When a vault is used, the application generally holds an access path, not the secret itself, which reduces spread and makes governance more explicit.

That distinction matters most when teams need to change or retire the secret. A hardcoded secret usually requires code changes, redeployment, and careful hunting for every place it was copied. A vault supports a cleaner lifecycle: the value can be replaced centrally, access can be narrowed, and the application can be pointed to a new credential without reissuing the whole code artifact.

What Changes in Rotation, Revocation, and Auditability

Rotation is where the governance gap becomes obvious. If a secret is baked into code, rotation becomes a release-management problem because the old value may be replicated in multiple branches, images, configs, and backups. If the secret is kept in a vault, rotation can be treated as a credential-management task, often with shorter operational blast radius and less chance of missing a copy.

Revocation also behaves differently. In code, removing a secret from the current version does not guarantee that prior versions, deployed artifacts, or cached copies are gone. In a vault, revocation is a first-class action, so access can be cut off centrally and the change can be traced through logs and policy records. That is why vaulting usually improves accountability as well as control.

Auditability is stronger when access is brokered. A vault can record who or what requested a secret, when it was retrieved, and under what policy. Code cannot usually provide that level of event history by itself. For teams that need evidence of access review, rotation discipline, or separation of duties, that difference is often the deciding factor. NHIMG’s Secrets Management Guide is useful background on centralising secrets, rotation, and secretless patterns.

Why the Risk Profile Is Different Even When the Secret Is the Same

The secret value may be identical in both cases, but the exposure surface is not. Code-based storage increases copy risk, developer visibility risk, and accidental disclosure through repositories, build logs, issue trackers, and cloned environments. Vault-based storage reduces those exposures, but only if access controls, retrieval policies, and secret lifetime are configured correctly.

The main design trade-off is convenience versus containment. Code is easy for developers to use, but it is hard to govern at scale. A vault adds dependency on an external control plane, but it gives you a place to enforce least privilege, monitor retrieval, and separate secret access from application deployment. OWASP Non-Human Identity Top 10 and OWASP Cheat Sheet Series both reinforce the broader principle that secrets and credentials should be handled as controlled trust material, not embedded as ordinary source code.

In practice, a vault does not make a secret safe by default. It only shifts the failure mode from “everyone who can read the code can see the secret” to “only identities with authorised retrieval paths can obtain it.” That is a much better security boundary, but it still depends on strong policy, proper rotation, and careful control of the consuming application.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 addresses the attack surface, NIST SP 800-53 Rev 5 and OWASP ASVS set the technical controls, and ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
OWASP Non-Human Identity Top 10NHI-02 — Secret LeakageHardcoded secrets in code create direct secret leakage risk.
NHI-07 — Long-Lived SecretsVaulting helps reduce long-lived credential exposure and rotation friction.
NHI-05 — Overprivileged NHIVault retrieval permissions can become excessive if not tightly scoped.
Recommendation — Move secrets out of code and enforce centralised retrieval and rotation. Prefer short-lived secrets and rotate centrally through the vault. Scope vault access to the minimum identity and secret path needed.
NIST SP 800-53 Rev 5IA-5 — Authenticator ManagementCompares lifecycle handling of secrets, rotation, revocation and storage.
AC-6 — Least PrivilegeVaults improve access mediation when retrieval is restricted to least privilege.
Recommendation — Manage secret issuance, rotation, and revocation through controlled lifecycle processes. Limit secret retrieval to the smallest set of identities and operations.
ISO/IEC 27001:2022A.5.15 — Access controlThe topic is fundamentally about governing access to secrets versus exposing them in code.
A.5.17 — Authentication informationSecrets are authentication information whose storage and handling determine exposure.
Recommendation — Define and enforce access rules for secrets separately from application source control. Protect authentication information with controlled storage, rotation, and revocation.
OWASP ASVSV9 — Self-contained TokensSecret handling affects how credentials are stored and exposed in applications.
Recommendation — Keep sensitive tokens and keys out of application code and source repositories.

Practitioner Guidance

What to prioritise: Treat hardcoded secrets as a governance defect, not just a cleanliness issue. The first question is whether the secret can be removed from the code path without breaking operational continuity; if yes, centralise it and reduce the number of places that can leak it.

What to verify: Confirm that the vault is actually enforcing access policy at retrieval time, not simply acting as another storage bucket. The consuming application should authenticate with a distinct identity, retrieve only the secret it needs, and expose no long-lived secret in source control, images, or environment files.

Common mistake: Teams often move the secret into a vault but leave broad retrieval permissions, static credentials, or manual copy steps in place. That preserves much of the original risk while creating a false sense of control.

Practitioner takeaway: The meaningful difference is not “file versus vault,” it is whether secret access is governed, observable, and revocable without redeploying the application.

Deepen Your Knowledge

Sign up to our weekly newsletter — get 33% off our NHI Foundation Level Course

    NHIMG Editorial Note
    Reviewed and updated by the NHIMG editorial team on September 28, 2026.
    NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org