Sub topic 14
Storing, rotating and revoking credentials safely
This chapter assumes you can call the GitHub REST API with a bearer token, you know the difference between a fine-grained personal access token (PAT) and a GitHub App, and you are comfortable with environment variables and a terminal. A narrow token still does damage if it leaks, so this chapter covers how to keep credentials out of the wrong places and how to replace or kill them quickly.
What you are protecting
Each credential has a different risk profile.
| Credential | Lifetime | Impact if leaked | Main defense |
|---|---|---|---|
| Fine-grained PAT | Up to the expiration you set | Whatever the selected repositories and permissions allow | Short expiration, minimal scope |
| App private key | Until you delete it | Attacker can mint installation tokens for every installation | Secret store, key rotation |
| App JWT | 10 minutes | Can act as the app itself, briefly | Never store it |
| Installation access token | 1 hour | Limited to that installation's repositories and permissions | Never store it, request a new one |
The private key is the most valuable item. A leaked key lets an attacker keep creating fresh tokens until you remove the key.
Storing credentials
Follow these rules in order of importance.
- Never commit a token or private key to a repository, including private ones. Git history keeps it.
- Never put a token in a URL, a command-line argument, or a log line. Arguments show up in process listings and shell history.
- Read secrets from environment variables or a secret manager at runtime.
- In GitHub Actions, use encrypted secrets, or use the built-in
GITHUB_TOKENwhen it is enough, since it is created per job and expires when the job ends. - Store a private key in a secret manager such as AWS Secrets Manager, HashiCorp Vault or your platform's equivalent. Load it into memory and avoid writing it to disk where you can.
The examples in this tutorial read the key from a file path in GITHUB_APP_PRIVATE_KEY_PATH. That is fine for local work. In production, restrict the file to the service account.
chmod 600 "$GITHUB_APP_PRIVATE_KEY_PATH"
ls -l "$GITHUB_APP_PRIVATE_KEY_PATH"Do not cache JWTs or installation tokens for long. The Octokit App class from the earlier example creates and refreshes them for you, so your code never has to hold them.
Rotating credentials
Rotation means replacing a credential on a schedule or after a suspected exposure. The safe pattern is to create the new one before removing the old one.
For a fine-grained PAT, GitHub does not let you extend a token. Generate a replacement, update the places that use it, confirm they work, then delete the old token.
For an App private key, GitHub lets an app hold more than one key at once. That allows rotation with no downtime.
Installation tokens and JWTs need no rotation, because they expire on their own.
Revoking credentials
Revoke immediately when a credential leaks, a person leaves, or a service is retired.
- Fine-grained PAT: delete it in your token settings. Organization owners can also revoke tokens that have access to their organization's resources.
- App private key: delete the key in the app's settings. Existing JWTs signed with it stop working when they expire, within 10 minutes.
- Installation token: you can revoke one early with
DELETE /installation/token, authenticated with that token. - Installation: uninstalling the app removes its access to that account.
curl -L -X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $INSTALLATION_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/installation/tokenA successful call returns status 204 with no body.
Detecting problems
- Turn on secret scanning and push protection for your repositories, so pushes that contain known token formats are blocked.
- Check the audit log and each token's last-used information to find unused credentials, then delete them.
- Prefer a short PAT expiration. A forgotten token that expires after 30 days costs far less than one that never expires.
Exercise
Your GitHub App's private key was accidentally printed in a CI log. List the steps you take, in order, and explain why a JWT or installation token minted earlier is less urgent than the key.
Show answer
Generate a new private key and deploy it, verify the app works with it, then delete the exposed key and review the audit log for unexpected activity. Also clean up the log. The key is the urgent item because it lets an attacker mint new JWTs and installation tokens at will. A JWT lasts 10 minutes and an installation token lasts 1 hour, so earlier ones expire on their own, but only deleting the key stops new ones from being created.