More Info:
GitHub GPG keys are used to cryptographically sign code commits and should be rotated every 180 days.Risk Level
HighAddress
SecurityCompliance Standards
- Cloudanix Best Practice
Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
Below are step‑by‑step instructions to rotate (replace) a GPG key used for commit signing in GitHub, using the GitHub web console. The process is:
Recommended answers:Identify the new key’s ID (e.g., Copy the entire block including
(Replace Test signing a commit in any repo:On GitHub, open the commit and confirm it shows as Verified and linked to your account.
If you share more about your current state (number of keys, whether they’re already in GitHub, etc.), I can narrow this to just the exact actions you need.
- Generate a new GPG key locally
- Add the new key to your GitHub account (console)
- Update your local Git configuration to use the new key
- Remove/disable the old key in GitHub (and locally)
1. Generate a new GPG key (locally)
On your workstation (not in GitHub):- Key type:
RSA and RSA(or modern ECC if you know you need it) - Key size:
4096(for RSA) - Expiration: set an appropriate expiry date (e.g., 1 year)
- Name: your real name
- Email: the same email you use on GitHub
- Comment: something like
GitHub signing key - Passphrase: strong passphrase
ABCD1234EFGH5678).Export the public key in ASCII:-----BEGIN PGP PUBLIC KEY BLOCK----- and -----END PGP PUBLIC KEY BLOCK-----.2. Add the new GPG key in the GitHub web console
- In your browser, sign in to GitHub.
- Click your profile picture (top‑right) → Settings.
- In the left sidebar, select SSH and GPG keys.
- Under GPG keys, click New GPG key.
- Title: something meaningful, e.g.,
Work laptop GPG key (2026-07). - Key: paste the ASCII‑armored public key you copied.
- Click Add GPG key.
- Confirm with your password or 2FA if prompted.
3. Configure Git locally to use the new key
Tell Git to sign commits by default and use the new key:ABCD1234EFGH5678 with your real key ID.)If using GPG v2, you may also need:4. Remove or disable the old GPG key in GitHub
Once you confirm new signed commits are verified:- In GitHub, go to Settings → SSH and GPG keys.
- Under GPG keys, locate the old key.
- Click Delete next to the old key.
- Confirm deletion.
5. (Optional) Revoke / remove the old key locally
To fully complete rotation:-
Revoke old key (if you have or generate a revocation certificate):
-
Or delete old secret key from your keyring (only if you are sure you don’t need it):
If you share more about your current state (number of keys, whether they’re already in GitHub, etc.), I can narrow this to just the exact actions you need.
Using CLI
Using CLI
Below is a minimal, step‑by‑step rotation process for GitHub GPG keys using the GitHub CLI (
Note the ID or email associated with the key you want to rotate.
Recommended options:Copy the new key ID, e.g.
4. Upload the New GPG Key to GitHub via
Confirm it appears:
Test signing:Push a signed commit and verify it shows as “Verified” in GitHub.
Then optionally delete it:
This completes GPG key rotation for GitHub using the GitHub CLI.
gh).Assumptions:- You already have
ghinstalled and authenticated (gh auth login). - You’re rotating a personal GPG key used to sign commits/tags that GitHub verifies.
1. List Existing GPG Keys in GitHub
2. Generate a New GPG Key Locally
- Key type:
RSA and RSA(or Ed25519 if supported via newer GPG) - Key size: at least
4096for RSA - Expiration: set a future expiration date (e.g. 1y)
- Use the same email as your GitHub account
ABCD1234EF567890.3. Export the New Public Key
4. Upload the New GPG Key to GitHub via gh
5. Update Local Git to Use the New Key
Set the new key as the signing key:6. Remove the Old GPG Key from GitHub
After verifying the new key works, remove the old one by ID:-
Get the GitHub key ID (not GPG key ID) from:
-
Delete the old key:
7. (Optional) Revoke Old Key Locally
If the old key should no longer be trusted anywhere:This completes GPG key rotation for GitHub using the GitHub CLI.
Using Python
Using Python
Below is a minimal, practical rotation approach: detect old GPG keys in GitHub, generate a new key, upload it, and remove the old key using Python and the GitHub API.
Adjust the criteria as you like.
Note:
You can integrate step 4 and 5 into a single workflow: generate, export, upload.
If you specify:
1. Prerequisites
-
GitHub Personal Access Token (PAT)
- Create one (Classic) with scopes:
read:gpg_keywrite:gpg_key
- Store it as an environment variable, e.g.
GITHUB_TOKEN.
- Create one (Classic) with scopes:
-
Python packages
-
GnuPG installed on the machine (needed by python-gnupg):
- Linux:
sudo apt-get install gnupg(or equivalent) - macOS:
brew install gnupg - Windows: install Gpg4win
- Linux:
2. Define Rotation Policy
Example policy (adapt as needed):- Rotate any GPG key:
- older than 90 days, or
- expiring within 7 days.
3. Script: List Existing GPG Keys and Identify “Stale” Ones
4. Generate a New GPG Key Using Python
This example generates a new key for a given user/email, with an explicit expiration.- Use the same name/email as your GitHub user email.
- Keep private key + passphrase secure (recommended: hardware token or OS keystore).
5. Upload the New Public GPG Key to GitHub
6. Remove Old (Rotated) GPG Keys from GitHub
Once the new key is uploaded and you’ve validated it works (by signing and pushing a test commit), remove the old keys:7. Hook This Into Your IAM / Security Process
- Run the rotation script:
- Periodically via CI (GitHub Actions, Jenkins, etc.).
- Or as part of an internal identity lifecycle tool.
- Store:
- Mapping of GitHub username → GPG fingerprint.
- Key creation and (optional) expiration dates for compliance/audit.
8. Optional: Sign Commits with the New Key in Git
On developer machines (not via Python but for completeness):If you specify:
- how you store/manage private keys (local, HSM, KMS, Vault),
- and whether you want org-wide or per-user automation,
Using Terraform
Using Terraform
Terraform cannot manage or rotate GitHub GPG signing keys.The official
integrations/github (hashicorp/github) provider does not expose any resource or argument for user GPG keys; they are per‑user credentials managed via the GitHub UI or API, not repository or org configuration. Because of that, there is no github_* Terraform resource that can invalidate, delete, or create GPG keys, so this finding cannot be remediated on github-applicationintegration-scm-repository via Terraform.To remediate:-
Each affected GitHub user must:
- Generate a new GPG key locally.
- Upload it under: GitHub → Settings → SSH and GPG keys → New GPG key.
- Configure
git commit -Swith the new key. - Delete the old GPG key from the same page.
-
Optionally, automate rotation outside Terraform via:
gh apior GitHub REST API for GPG keys.- A scheduled job (e.g., GitHub Actions, external CI) that:
- Lists keys older than 180 days.
- Creates/upload new keys and deletes old ones.
terraform plan change to verify, since no Terraform-managed resource can represent or rotate these GPG keys.
