API credentials and TLS verification
API credentials represent authority to act on a service. TLS protects the connection and authenticates the server when the certificate chain and hostname are verified. Use HTTPS for remote APIs with the normal verification enabled; if a private service uses a private CA, configure a trusted CA bundle (for example --cacert) according to its owner. Do not disable verification to make an error disappear. Certificate errors require investigation of trust, hostname, time and deployment, not a bypass.
Minimize secret exposure
Use an approved secret manager or a tightly permissioned credential file with a short lifetime and least-privilege scope. Do not hard-code production keys, put them in URLs or print entire headers, bodies, environment dumps or curl verbose traces. Shell expansions in arguments may be visible to local process inspection. A file-based curl header (--header @file) avoids putting the header contents directly in its command line, but file permissions, backups, logging, process memory and the host remain trust boundaries. Validate service identity and redirects before forwarding credentials. Use an allowlisted host and protocol; never forward auth to arbitrary pagination URLs.
The only header sent in this lab is a fixed synthetic value DEMO_ONLY to 127.0.0.1, never a real token. The lab uses plaintext HTTP only because it is an isolated mock; it does not test TLS. In production, do not use HTTP for secrets. The other examples check private file permissions and log only approved metadata. A successful response does not prove adequate security; test revocation, rotation and certificate failures in an authorized dedicated environment. curl 8.14.1 and jq 1.7 are external tools, not Bash features. Create the synthetic header exclusively before registering its cleanup trap, so rejected setup does not remove a preexisting file. The trap preserves failure and reports cleanup failure after successful work. The response projection is a fixed teaching allowlist, not proof of safe logging for arbitrary data.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. HTTP examples require the included lab-fixtures/v25/mock_api.py server: run python3 on that file in a separate terminal, export BM_API_BASE with its printed loopback URL, and stop the server with Ctrl-C when done. Use a fresh server for each HTTP example. curl and jq are external dependencies.
Send a synthetic file-based header
[[ ${BM_API_BASE-} =~ ^http://127\.0\.0\.1:[0-9]{1,5}$ ]] || exit 2umask 077(set -C; printf '%s\n' 'Authorization: Bearer DEMO_ONLY' > headers.txt) || exit 1cleanup() { local rc=$? rm -f -- headers.txt || { (( rc != 0 )) || rc=1; } exit "$rc"}trap cleanup EXITcode=$(curl -q --noproxy "*" --proto "=http" -sS --connect-timeout 1 --max-time 2 --header @headers.txt -o body.json -w '%{http_code}' "$BM_API_BASE/auth") || exit 1printf 'http=%s\n' "$code"[[ $code == 200 ]] && jq -es 'length==1 and (.[0] | type=="object" and .ok==true)' body.jsonExpected stdout:
http=200trueThe EXIT trap removes the synthetic header when the shell exits normally or via a handled failure. This does not guarantee cleanup after forced termination. No actual credential is used.
Create a private synthetic credential file
umask 077printf '%s\n' 'DEMO_ONLY' > token.fixture || exit 1stat -c '%a' token.fixture || exit 1rm -- token.fixture || exit 1[[ ! -e token.fixture && ! -L token.fixture ]] || exit 1printf 'removed\n' Expected stdout:
600removedFile mode is checked in the local GNU filesystem. Removing a file does not guarantee secure erasure.
Log only approved metadata
printf '%s\n' '{"request_id":"req-42","status":200,"token":"DEMO_ONLY"}' > response.json || exit 1jq -c '{request_id,status}' response.jsonExpected stdout:
{"request_id":"req-42","status":200}A fixed allowlist excludes the synthetic token. In production validate even approved fields before logging them.
Common mistakes
Do not log tokens or disable TLS verification. A file-based header is not a secret manager and HTTP is only used for this synthetic local lab.
Practice
Compare a request with the provided synthetic header and one without it. Explain why neither tests TLS. Sketch a verified HTTPS configuration for a hypothetical service without using real credentials.
Next: CI/CD and build scripts