INF-23Recommended
Create the secret's place in code and its value in the store
Create each secret's entry, owner and access policy in code. Put its value in the store by rotation or by hand, never through a code variable.
How to do it
Declare the secret in the infrastructure code, with its name, its encryption key and the one role that may read it. Leave the value out. The store generates it, or the database service manages it, as RDS does with manage_master_user_password. Plain settings, such as a region or a feature limit, go in the variables file or a parameter store. The app reads values as Keep secrets in a secrets store says.
Examples
Terraform example
resource "aws_secretsmanager_secret" "payment_api_key" {
name = "production/shop/payment-api-key"
kms_key_id = aws_kms_key.secrets.arn
tags = { owner = "payments-team" }
}
resource "aws_db_instance" "orders" {
identifier = "shop-orders"
username = "shop_admin"
manage_master_user_password = true # the database service creates and rotates it
}
Shell example
# Fails when a secret argument holds a literal value
! grep -rnE '(secret_string|password|private_key|secret_key|access_key|api_key) *= *"[^"]+"' infra --include=*.tf
How to check
The grep below finds no secret_string, password or key argument set to a quoted literal. Every secret in the store has an owner tag and exactly one reading role, plus its rotation function where one exists.
Sources
Related rules
- ENG-26 Keep secrets in a secrets store in Engineering
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/configuration-and-secrets/secret-containers-in-code">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.