Sub topic 6
Giving the job credentials without leaking them
This chapter assumes you can write shell scripts, understand environment variables, and know how a scheduler such as cron or systemd starts a job. An unattended agent has no person to type a password or approve a login. It needs credentials ahead of time, and every place you put them is a place they can leak.
What the job needs
A scheduled agent run usually needs two kinds of secrets:
- A model credential, such as an API key or token for the agent itself.
- Repository or service credentials, such as a token that can push a branch or open a pull request.
Give each one the smallest scope that works. A token that can only push branches to one repository is far less dangerous than your personal account token.
Where secrets should not go
- In the prompt file. Prompts get logged and sometimes committed.
- In the script or the crontab. Both are often readable by other users or checked into version control.
- On the command line. Arguments appear in
psoutput for every user on the machine. - In the repository the agent works in. The agent may read, copy, or commit what it finds.
Where secrets should go
Each scheduler has a standard place to keep them.
| Scheduler | Where to store secrets |
|---|---|
| cron | A file readable only by the job's user, loaded by the wrapper script |
| systemd | An `EnvironmentFile=` with restricted permissions |
| CI | The platform's encrypted secrets store |
For cron and systemd, create the file and lock down its permissions:
sudo install -m 600 -o deploy -g deploy /dev/null /etc/nightly-agent.env
sudoedit /etc/nightly-agent.envThe file holds one NAME=value line per secret, for example AGENT_API_KEY=.... Mode 600 means only the owner can read it. The systemd unit from the scheduling chapter already points at this file with EnvironmentFile=/etc/nightly-agent.env.
For a cron job, have the wrapper script load the file instead of putting values in the crontab:
#!/usr/bin/env bash
set -eu
# Export every variable defined in the file, then stop exporting.
set -a
. /etc/nightly-agent.env
set +aIn CI, store the value as an encrypted secret and map it into the step's environment:
- name: Run agent
env:
AGENT_API_KEY: ${{ secrets.AGENT_API_KEY }}
run: ./scripts/nightly-agent.shKeeping secrets out of logs
Your wrapper writes the agent's full output to a log file. If a secret is printed anywhere, it ends up there.
- Never run the script with
set -x. It prints every expanded command, including secret values. - Don't echo environment variables for debugging.
- Tell the prompt not to print credentials, but don't rely on that alone.
- Restrict the log directory with
chmod 700.
CI platforms mask registered secrets in logs, but masking only matches the exact value. A transformed copy, such as a base64 version, may not be hidden.
Rotation and revocation
Assume a secret will leak eventually. Use tokens with an expiry date where the service supports it. Keep a note of where each one is stored so you can replace it quickly. If a log or repository may have exposed a token, revoke it first and investigate afterward.
Common errors
- The job works in your shell but fails under cron, because your shell had the variable set and cron did not. Load the file inside the script.
- The environment file has
exportin front of each line. systemd does not accept that syntax. Use plainNAME=value. - The file is readable by everyone, so any local user can read the key. Check with
ls -l.
Exercise
A teammate puts AGENT_API_KEY=abc123 directly in the crontab line that launches the wrapper script. Name two problems with this and say where the key should go instead.
Show answer
The key is stored in plain text in a file that may be visible to other users or backed up, and it can show up in process listings, and it is hard to rotate. Put it in a file with mode 600 owned by the job's user (or the CI secrets store), and have the wrapper script or the systemd EnvironmentFile load it.