CSE Roadmap

Core tier · about 3 hours · 12 parts

Part 2 of 12 · 2 min read

Secrets and container hygiene: catching what a scanner can and can't

A pattern scanner is a safety net, not proof that nothing leaked.

Think of a metal detector at a door. It catches most knives but misses anything made of plastic or ceramic. It will occasionally beep at someone's belt buckle. It is worth having, but nobody should treat "it didn't beep" as proof the room is safe.

In plain words: a pattern-based secret scanner catches many real secrets but misses some and occasionally flags something harmless, so secrets still need to be kept out of images and repos structurally, not just caught after the fact.

A worked example. On 16 lines containing 10 real secrets (API keys, AWS keys, a private key block, plain assignments), a toy pattern scanner finds 7, raises 1 false alarm (a line that looked like a key but was a test value), and misses 3 — a password embedded in a URL, a bearer token, and a GitHub token, none matching its patterns. A separate Dockerfile check on a badly configured file finds 4 issues (an unpinned base image, running as root, no healthcheck, a secret baked into an ENV line); the same check on a properly configured file finds 0.

The key lines. From code/mini-deploy/deploy.py:

# from code/mini-deploy/deploy.py
SECRET_PATTERNS = [
    ("api_key", re.compile(r"\b(sk|pk)[-_][A-Za-z0-9_\-]{16,}\b")),
    ("aws_access_key", re.compile(r"\bAKIA[0-9A-Z]{16}\b")),
    ("private_key", re.compile(r"-----BEGIN (RSA |EC |DSA |OPENSSH |ENCRYPTED )?PRIVATE KEY-----")),
]

Read it as: each pattern recognises one specific shape of secret. So anything shaped differently passes straight through. Examples are a token with no recognisable prefix, or a password sitting inside an ordinary-looking string. That is exactly why secrets belong in a secrets manager injected at runtime, not in the image or repo at all. The scanner is a backstop for mistakes, not the actual control.

Secrets manager

A service that stores credentials securely and hands them to a running workload at call time, instead of baking them into code, images, or configuration files.

Example: a deploy step that reads an API key from the secrets manager into an environment variable at container start, never writing it into the image itself.