Interview Prep/Shell Scripting/Scenarios: writing and hardening scripts

A script needs an API token, and the script lives in a Git repo. How do you handle the token?

IntermediateScenarioScenarios: writing and hardening scripts
Answer

Situation read → The token must never be committed, logged or visible in the process list.

What I'd do →

  1. Read it at runtime from the platform's secret source: an env var injected by CI (masked), a file with mode 0600 (/etc/opskit/token), or a secrets manager:
    token=$(aws secretsmanager get-secret-value --secret-id prod/api --query SecretString --output text)
    
  2. Fail fast if missing: : "${API_TOKEN:?API_TOKEN not set}".
  3. Keep it out of argv: curl -H @<(printf 'Authorization: Bearer %s\n' "$token") "$url" (curl 7.55+ reads headers from a file).
  4. Disable tracing around it (set +x) and never echo it; mask in logs.
  5. Add a secret scanner (gitleaks) in pre-commit and CI.

Why → Secrets leak through Git history, ps, set -x and log aggregation far more often than through clever attacks.

Interview tip → Mention that rotating a leaked token is mandatory; deleting the commit is not enough.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: writing and hardening scripts