SMSMC BlogPortal Login

A password baked into a Docker image stays readable after a later step deletes it. Pass secrets in at runtime.

Why passwords and keys baked into container images leak, and the safer ways to give containers their secrets at runtime.

By Jim Venuto

Securing Containerized Data: Why Secrets Don't Belong in Images

A developer adds a database password to a Dockerfile to get a build working, then pushes the image to a public registry. Anyone who pulls that image can read the password, even if a later build step deleted the file, because every layer of an image is kept.

Containers are isolated by design, so how a secret gets into one matters a great deal.

Storing Secrets in Container Images

The practice of embedding secret data directly within the container image, either as files or hard-coded secrets, is fraught with security risks:

  1. Exposure Risk: Secrets embedded in container images are readable by anyone who can see the source code or pull the image, and deleting a file in a later layer does not remove it from earlier ones.
  2. Immutability Issue: Once you embed a secret into an image, it remains unchanged unless you rebuild it. This rigidity does not align with dynamic environments where you must regularly rotate or update secrets.
  3. Decryption Dilemma: If you attempt to mitigate the exposure risk by encrypting the secret within the image, you then face the problem of securely providing a decryption key to the container, introducing another layer of complexity.
  4. Lifecycle Management: Hardcoding secrets in container images bypasses centralized, automated systems for managing secrets, such as CyberArk or HashiCorp Vault, designed to control the lifecycle of secrets securely.

Best Practices for Container Secret Management

Better alternatives include:

  • Environment Variables: While also potentially risky if not handled correctly, passing secrets as environment variables at runtime is a common practice. This method still requires careful management to avoid exposing secrets through logs or command-line history.

  • Secrets Management Tools: Utilizing dedicated secrets management tools (e.g., HashiCorp Vault, AWS Secrets Manager) allows for dynamic injection of secrets into containers at runtime. These tools provide robust mechanisms for secret rotation, access control, and audit logs.

  • Secrets as a Service: Cloud providers offer Secrets as a Service, integrating with container orchestration tools to securely inject secrets into containers without them ever being stored less securely or embedded in source code.

  • Secret Scanning: Scan code repositories and images before they are published, so a secret that slips in is caught and rotated.

Key Takeaway

Embedding secrets in container images or source code may seem convenient, but it is insecure. Inject secrets at runtime from a secrets manager, rotate them regularly, and scan images and repositories so a leak is caught before anyone else finds it.