Agent Wranglers

INF-27Recommended

Rotate secrets automatically

Let the secrets store rotate each secret on a schedule. The app picks up the new value without a deploy.

How to do it

Set the schedule to the key age limit in keys with the least power. Turn on the store's rotation, such as rotation functions in AWS Secrets Manager or dynamic secrets in Vault. Keep the old and new value valid together while the switch happens. The app caches a secret for 1 hour at most. It fetches it again when a call fails with an authentication error. Rotate at once after a leak, with the same job. Where the store cannot rotate a secret, rotate it by hand within the same limit.

Examples

Shell example

# Prints every secret that does not rotate or rotated over 90 days ago. Expect nothing.
aws secretsmanager list-secrets --output json | jq -r --arg limit "$(date -u -d '-90 days' +%FT%T)" \
  '.SecretList[] | select(.RotationEnabled != true or (.LastRotatedDate // "") < $limit) | .Name'

TypeScript example

import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';

const CACHE_MS = 60 * 60 * 1000;
const store = new SecretsManagerClient({});
const cache = new Map();

export async function getSecret(name, { refresh = false } = {}) {
  const cached = cache.get(name);
  if (!refresh && cached && Date.now() - cached.fetchedAt < CACHE_MS) return cached.value;
  const { SecretString } = await store.send(new GetSecretValueCommand({ SecretId: name }));
  cache.set(name, { value: SecretString, fetchedAt: Date.now() });
  return SecretString;
}
// On an authentication error: getSecret(name, { refresh: true }) and retry once.

Python example

import time
import boto3

CACHE_SECONDS = 3600
store = boto3.client("secretsmanager")
cache = {}

def get_secret(name, refresh=False):
    cached = cache.get(name)
    if not refresh and cached and time.monotonic() - cached[1] < CACHE_SECONDS:
        return cached[0]
    value = store.get_secret_value(SecretId=name)["SecretString"]
    cache[name] = (value, time.monotonic())
    return value

How to check

The query below lists no secret without rotation or with a last rotation older than that limit. Documented secrets rotated by hand within the limit are the only exception. Trigger a rotation on staging, and the app keeps serving without a restart.

Sources

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/automatic-secret-rotation">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.