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 →
- 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) - Fail fast if missing:
: "${API_TOKEN:?API_TOKEN not set}". - Keep it out of argv:
curl -H @<(printf 'Authorization: Bearer %s\n' "$token") "$url"(curl 7.55+ reads headers from a file). - Disable tracing around it (
set +x) and never echo it; mask in logs. - 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.
More on Scenarios: writing and hardening scripts
You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?IntermediateA script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerYou need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?BeginnerA maintenance script must run as the `deploy` user, never as root, because files it creates must be owned by `deploy`. How do you enforce that?BeginnerTen scripts in a repo each copy-paste their own `log`, `die`, retry and config loading. How do you fix this?IntermediateA script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.Beginner