Marcio Cunha

Declarative Secrets Management with HashiCorp Vault and Automatic Credential Rotation in Production

Learn how to structure declarative secret management using HashiCorp Vault and automate credential rotation in production environments to mitigate leaks and compliance failures.

Marcio Cunha•6 min
Also available in:EspañolPortuguês
Summary
  • The declarative approach eliminates manual state and ensures the secrets vault strictly reflects the code versioned in Git.
  • Automatic credential rotation drastically reduces the vulnerability window in case of a key leakage.
  • Short-lived tokens and strict role-based policies protect the ecosystem against unauthorized lateral movements.
  • Integrating Vault with Kubernetes controllers simplifies secure environment variable injection without plain-text exposure.
  • Continuous auditing of access logs is an essential requirement to track failures and comply with rigorous security regulations.

The Operational Challenge of Manual Secret Management in Production

Managing passwords, API keys, and digital certificates in corporate environments is one of the most critical and stressful tasks for engineering teams. Traditionally, this sensitive data was scattered across local configuration files, protected spreadsheets, or even sent through corporate messaging channels. In practice, this means opening catastrophic loopholes for accidental leaks and total loss of control over who accesses what. When an employee leaves the company or a server is compromised, the process of revoking these credentials is usually slow, manual, and painful.

To solve this chaos, the industry has adopted centralized digital vaults, with HashiCorp Vault being the most popular tool for this purpose. Vault stores data protected by heavy encryption and requires rigorous authentication before releasing any information. However, merely centralizing secrets does not solve the human factor problem. Engineers still had to access graphical interfaces or type manual commands to create, update, and destroy credentials, keeping the operation vulnerable to typos and operational oversights.

The natural evolution of this process was the introduction of infrastructure as code and declarative models. Instead of telling the system step-by-step what to do, the operator defines the desired state for secrets and permissions in configuration files. An automated tool reads this manifest and adjusts the environment to match exactly what was written. This mindset shift brings predictability, eliminates configuration drift, and allows any security audit to verify the history of changes directly within version control.

Architecture and Operation of HashiCorp Vault in Distributed Environments

HashiCorp Vault functions as a highly specialized database, designed from inception to store secrets with maximum security. When we send data to Vault, it is not saved plainly; the system applies advanced encryption algorithms before writing the information to persistent storage, which can be a relational database or a cloud storage service. In practice, even if someone breaks into the server and steals the hard drive files, the data remains unreadable without the master decryption key.

One of Vault's most important concepts is the secrets engine mechanism, which acts like specialized drawers for different types of data. There is an exclusive engine to generate dynamic database credentials, another to issue security certificates based on public key infrastructure, and even engines to manage access tokens for cloud providers like AWS, Google Cloud, and Azure. This modularity allows the same tool to centralize security across the entire corporate infrastructure, regardless of the technology used.

Another fundamental pillar is the authentication system and role-based access control. Before any application or user can retrieve a secret from the vault, Vault requires proof of identity through methods like tokens, TLS certificates, or integration with corporate directories. Once authenticated, the requester receives only the strictly necessary permissions to perform their function, following the principle of least privilege. If a web service only needs to read a read-only database password, it will never have access to the system's administrative keys.

Declarative Implementation with Configuration Files and GitOps

Declarative management eliminates the reliance on manual commands in the production terminal. With tools that automate the application of manifests in Vault, such as the Terraform operator or the Vault Secrets Operator in Kubernetes, all configuration of policies, secret paths, and authentication methods is described in plain text files and treated as programming code. In practice, this means any modification to security must go through a code review process, where other peers evaluate the change before it is applied to the real environment.

To illustrate what this definition looks like in practice, imagine a Terraform code block that configures a restricted access policy and an automated secret path. This file describes the exact expected final result on the Vault server, ensuring that repeated executions always produce the same secure state without unwanted side effects:

resource "vault_policy" "webapp_policy" {
name = "webapp-readonly"

policy = <<-EOT
path "secret/data/production/webapp" {
capabilities = ["read"]
}
EOT
}

resource "vault_mount" "kv_production" {
path = "secret"
type = "kv"
options = { version = "2" }
description = "Declarative secrets vault for production"
}

Adopting this workflow brings undeniable operational advantages to engineering teams. The history of who changed a security policy or created a new secret is permanently recorded in Git history. If an incorrect configuration causes a system outage, the team can simply revert the previous commit and reapply the stable state within seconds. Furthermore, it eliminates the problem of tribal knowledge, where only a single senior employee knows how to configure the company's security access.

Automatic Credential Rotation and Risk Mitigation

One of the biggest myths in information security is believing that creating a strong and complex password solves the problem forever. In reality, the longer a credential remains unchanged, the higher the probability of it being leaked, intercepted on insecure networks, or improperly copied by former employees. This is where automatic credential rotation comes in, a process where the system periodically alters database passwords, API keys, and access tokens without requiring human intervention.

HashiCorp Vault manages this lifecycle exemplarily through dynamic credentials and native rotation plugins. When an application requests access to a PostgreSQL database, for example, Vault does not hand over a static and permanent password. Instead, it creates a temporary account with a short expiration date, delivers the access data to the application, and when the deadline expires, Vault itself revokes access and deletes the user from the database. In practice, this means that even if an attacker steals this credential, it will be useless a few minutes later.

For static credentials that must exist — such as third-party API keys that do not support dynamic creation —, Vault uses scheduled routines based on rotation scripts. The system periodically connects to the external service's API, generates a new key, updates the internal vault, and safely restarts the services that depend on it. This level of automation turns security from a reactive headache into a proactive process, shielding infrastructure against prolonged leaks and ensuring compliance with strict market standards.

Final Considerations and Recommended Practices for Operation

Implementing declarative secrets management and automatic rotation requires a profound cultural shift within the technology team. It is not enough to just install HashiCorp Vault and configure rotation scripts; you must ensure all applications are prepared to handle short-term credentials and periodic restarts without losing stability. Continuous monitoring of the vault's audit logs becomes mandatory to identify unauthorized access attempts and anomalous behaviors in real time.

Ultimately, a company's security maturity is not measured by the complexity of its passwords, but by the ease and speed with which it can rotate and revoke compromised access. By unifying declarative infrastructure with Vault's secret engine, organizations build a resilient, auditable environment prepared to absorb technological growth without opening operational gaps. The initial transition effort pays off amply by eliminating the human factor from the most critical points of digital security.