Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
To remediate a “Repo Deployment Keys Rotated” finding in GitHub, you essentially need to create a new SSH deploy key pair and replace the old deploy key in the repository via the GitHub web console.Below are the minimal, step‑by‑step instructions.
Keep the private key secure; you’ll configure it wherever the deployment/CI runs.
1. Generate a New SSH Key Pair (Deploy Key)
Run this on a secure machine that will hold the private key (e.g., your CI server or a secure admin machine):- Press Enter for no passphrase (deploy keys usually must be non‑interactive).
- This creates:
- Private key:
~/.ssh/<repo-name>-deploy-key - Public key:
~/.ssh/<repo-name>-deploy-key.pub
- Private key:
2. Add the New Deploy Key in GitHub Console
- Go to the repository in GitHub.
- Click Settings (top of repo).
- In the left menu, click Deploy keys.
- Click Add deploy key.
- Fill in:
- Title: Something clear (e.g.,
CI Deploy Key (rotated 2026-07-25)). - Key: Paste the public key you copied.
- Allow write access:
- Enable only if this key must push to the repo; otherwise leave unchecked.
- Title: Something clear (e.g.,
- Click Add key.
3. Update the System That Uses the Deploy Key
On the system that actually performs the deployment (CI/CD tool, build server, etc.):-
Replace the old private key with the new private key (
<repo-name>-deploy-key). -
Ensure SSH config (if used) points to this new key, for example in
~/.ssh/config: -
Test connectivity:
You should see a message like “Hi
<user>! You’ve successfully authenticated…”
4. Remove the Old Deploy Key (Complete the Rotation)
Once you confirm your deployments/CI work with the new key:- In the same repo, go to Settings → Deploy keys.
- Find the old deploy key entry.
- Click Delete (or the trash icon) next to the old key.
- Confirm deletion.
5. (Optional) Enforce Better Practices
- Use read-only keys unless write is strictly needed.
- Rotate keys on a regular schedule.
- Track which system each deploy key belongs to via clear titles.
Using CLI
Using CLI
Here’s how to remediate a “Repo Deployment Keys Rotated” issue by rotating GitHub deploy keys using the GitHub CLI (
Note the
Files created:
Verify it was added:
gh).Assumptions:- You have
ghinstalled and authenticated (gh auth login). - You have
ssh-keygenavailable.
1. Identify the repo and existing deploy keys
id and title of the key(s) you want to rotate.2. Generate a new SSH key pair
- Private:
~/.ssh/deploy_key_$OWNER_$REPO - Public:
~/.ssh/deploy_key_$OWNER_$REPO.pub
3. Add the new deploy key via GitHub CLI
4. Update automation/CI to use the new private key
Wherever the old deploy key was used (CI/CD tool, server, etc.):-
Replace the old private key with:
~/.ssh/deploy_key_$OWNER_$REPO -
Ensure permissions are strict:
-
Test connectivity:
5. Remove the old deploy key via GitHub CLI
Once the new key is confirmed working:-
Get the ID of the old key:
-
Delete the old key (replace
OLD_KEY_ID): -
Confirm removal:
6. (Optional) Enforce a rotation practice
- Document:
- Key name pattern
- Rotation interval (e.g., 90 days)
- Procedure above as standard runbook.
- If “GitHub IAM” is a scanner/policy engine, re-run it to confirm the misconfiguration is cleared.
Using Python
Using Python
Below is a concrete, step‑by‑step way to “remediate” a Repo Deployment Keys Rotated finding for GitHub using Python – i.e., automatically rotate (replace) deploy keys on a repo via the GitHub API.
Set your GitHub token as an environment variable (safer than hard‑coding):
Important:
Save
Use this to identify the old key either by
If you share the name pattern of the old keys and how your CI/CD currently stores the private key (GitHub Actions secret, Jenkins credential, etc.), I can tailor the Python script to fully automate the update on that side as well.
1. Prerequisites
- Personal Access Token (PAT) (or GitHub App token) with:
reposcope (for private repos)- Or at least
public_repo(for public repos)
- Python 3.8+ installed.
requestsandcryptography(for generating SSH keys):
2. High‑Level Rotation Flow
For each affected repository:- Generate a new SSH keypair.
- Add the new public key as a deploy key via GitHub API.
- Update any systems/CI/CD pipelines that use the private key.
- Remove the old deploy key from the repo via GitHub API.
3. Generate a New SSH Keypair in Python
4. Add the New Deploy Key via GitHub API
GitHub API endpoint:POST /repos/{owner}/{repo}/keysSave
private_key_pem securely (secret manager, CI/CD secret, etc.) and update any service that uses the old deploy key to now use this new private key.5. List Existing Deploy Keys (Find Old Key to Remove)
GitHub API endpoint:GET /repos/{owner}/{repo}/keystitle, a known substring in the key, or manually.6. Remove the Old Deploy Key
GitHub API endpoint:DELETE /repos/{owner}/{repo}/keys/{key_id}7. Putting It All Together (Rotation Script)
- Generate new keypair.
- Add new public key as deploy key.
- Update your CI/CD/deployment system with the new private key.
- Confirm it can clone/push.
- Delete the old deploy key.
- Accepts
owner,repo, and theold_key_title(orold_key_id) as input. - Outputs the new private key so you can inject it into your secrets store.
8. Optional: Automate Regular Rotation
To make this a recurring remediation:- Put the rotation script into a secure automation environment (e.g., GitHub Actions, Jenkins, or a separate runner).
- Store the PAT or GitHub App token in a secret manager.
- Schedule the job (e.g., run monthly/quarterly).
- After each run, automatically update the target systems’ SSH private key secrets.
If you share the name pattern of the old keys and how your CI/CD currently stores the private key (GitHub Actions secret, Jenkins credential, etc.), I can tailor the Python script to fully automate the update on that side as well.
Using Terraform
Using Terraform
github_repository_deploy_key resource from Terraform so terraform plan shows the old key being destroyed and the new key created.
