awesome-everything RU
↑ Back to the climb

Security

Secrets management: code and config reading

Crux Read real config and code snippets — a committed .env, a Vault dynamic-secret fetch, a KMS envelope-encryption helper, and a leaky log line — predict the failure and pick the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Secrets failures are diagnosed in diffs, config, and logs. Read each snippet, predict what leaks or breaks, then choose the fix a senior engineer would make first — before reaching for a tuning flag.

Goal

Practise the loop you run in every secrets incident: read the artifact, find where the value escapes its trust boundary or where the lifetime is wrong, and reach for the highest-leverage fix.

Snippet 1 — the committed .env

# config/.env  — tracked in git, pushed to a public mirror last Tuesday
DATABASE_URL=postgres://app:Sup3rSecret@db.prod.internal:5432/main
STRIPE_SECRET_KEY=sk_live_51HxQ...redacted...9aZ
JWT_SIGNING_KEY=hs256-7f3e9c1b2a8d4e6f
Quiz

This .env was committed and pushed to a public mirror. You add it to .gitignore and delete it. Which statement is correct?

Snippet 2 — the Vault dynamic-secret fetch

// Fetch a short-lived DB credential from Vault's database engine.
secret, err := client.Logical().Read("database/creds/reporting-ro")
if err != nil {
    return err
}
user := secret.Data["username"].(string)
pass := secret.Data["password"].(string)
// leaseDuration is seconds until Vault auto-revokes this credential
lease := time.Duration(secret.LeaseDuration) * time.Second

db := openDB(user, pass)
defer db.Close()
// ... long-running worker keeps using db for hours ...
Quiz

The role mints a credential with a one-hour lease, but this worker runs for hours. What is the defect and the fix?

Snippet 3 — the KMS envelope-encryption helper

def encrypt_blob(kms, key_id: str, plaintext: bytes) -> dict:
    # ask KMS for a data key: plaintext to use now + wrapped to store
    resp = kms.generate_data_key(KeyId=key_id, KeySpec="AES_256")
    data_key = resp["Plaintext"]
    wrapped_key = resp["CiphertextBlob"]

    ciphertext = aes_gcm_encrypt(data_key, plaintext)   # encrypt LOCALLY

    return {
        "ciphertext": ciphertext,
        "data_key": data_key,        # stored next to the ciphertext
        "wrapped_key": wrapped_key,
    }
Quiz

This helper does local AES-GCM encryption with a KMS-issued data key — envelope encryption. What undoes the whole scheme?

Snippet 4 — the leaky log line

app.post("/charge", async (req, res) => {
  const cfg = { apiKey: process.env.STRIPE_SECRET_KEY, amount: req.body.amount };
  try {
    const charge = await stripe.charges.create(cfg);
    res.json({ id: charge.id });
  } catch (err) {
    // dump everything we have to help debugging
    console.error("charge failed", { cfg, err, body: req.body });
    res.status(500).json({ error: "charge failed" });
  }
});
Quiz

A scanner flags this handler. Where does the secret escape its trust boundary, and what is the fix?

Recap

Every secrets incident is read in artifacts: a committed .env means all three values are leaked and must be rotated, not gitignored; a dynamic credential whose lease is never renewed expires mid-run, so renew or re-fetch; envelope encryption is undone the instant you persist the plaintext data key beside the ciphertext — store only the wrapped key and unwrap via KMS; and a secret-bearing object in a log line ships your live key to every aggregator, so redact it and rotate. Read the artifact, find where the value escapes its boundary or its lifetime is wrong, then fix the boundary before you touch a flag.

Continue the climb ↑Secrets management: climb the maturity ladder
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.