Sub topic 2
Rotate the credential first
This chapter assumes you can clone, commit, and push with Git, and that you can read a short shell script. It also assumes you have admin access, or can reach someone with admin access, to the service that issued the leaked credential. The examples use an AWS access key. The steps are the same for API tokens, database passwords, and signing keys.
Why rotation comes before cleanup
Once a secret has been pushed to a remote, you should treat it as public. Rewriting history later removes it from your repository. It does not remove it from copies that already exist. Those copies include:
- Clones on teammates' laptops and on CI runners.
- Forks of the repository.
- Mirrors and backups.
- Search indexes and caches kept by the hosting platform.
- Automated scrapers that watch public commit feeds.
Scrapers matter most for public repositories. Bots that look for credentials can pick up a key within minutes of a push. You cannot undo that by rewriting history, so the only reliable fix is to make the leaked value useless.
Revoking the credential also changes how urgent the rest of the work is. After rotation, the history rewrite is cleanup you do carefully. Before rotation, it is a race you cannot win.
Decide what counts as exposed
Before you start, work out how far the secret travelled. The answer sets how aggressive you need to be.
| Situation | Assume | Response |
|---|---|---|
| Pushed to a public repository | Already harvested | Rotate immediately and audit use |
| Pushed to a private repository | Visible to everyone with read access, including past members and integrations | Rotate. Audit if access was broad |
| Committed locally, never pushed | Only your machine has it | Rotation is optional. Rewrite history before pushing |
| Pasted into an issue, pull request, or chat | Exposed outside Git as well | Rotate, and clean those places too |
The last row is why rotation matters beyond Git. A history rewrite does not touch issue comments, wiki pages, or build logs.
The rotation sequence
How you order the steps depends on how fast you must stop the leak and how much downtime you can accept. A safe default has four phases.
- Create the replacement credential.
- Update every consumer to use it.
- Revoke the old credential.
- Review what the old credential did.
If the leaked credential is high privilege and you suspect abuse, you can swap steps 2 and 3. Revoke first and accept a short outage. Stopping an attacker is worth more than avoiding a brief failure in your own service.
Example: an AWS access key
The commands below assume the leaked key AKIAEXAMPLEKEY12345 belongs to an IAM user named deploy-bot. An IAM user can hold at most two access keys, so you may need to delete an unused key before you can create a new one.
# 1. Create the replacement key. Save the output in your secrets manager.
aws iam create-access-key --user-name deploy-bot
# 2. After consumers use the new key, disable the old one.
aws iam update-access-key \
--user-name deploy-bot \
--access-key-id AKIAEXAMPLEKEY12345 \
--status Inactive
# 3. Look for activity from the old key in the last events.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLEKEY12345 \
--max-results 50
# 4. Delete the old key once you are sure nothing needs it.
aws iam delete-access-key \
--user-name deploy-bot \
--access-key-id AKIAEXAMPLEKEY12345Deactivating a key before deleting it gives you a way back. If a forgotten service breaks, you can set the status to Active again, fix that service, and then deactivate once more. Deleting the key cannot be undone.
Other credential types
The pattern is the same everywhere, but the details differ.
| Credential | How to rotate | Watch for |
|---|---|---|
| Cloud access key | Create a second key, switch over, disable the old one | Quotas on keys per user |
| Personal access token | Revoke it in the provider's settings and issue a new one | Tokens tied to a person who may have left |
| Database password | Change the password, then update the connection settings | Connection pools that keep using the old password until restart |
| Webhook or signing secret | Generate a new secret on both sides | Verification fails if the two sides disagree during the switch |
| SSH or TLS private key | Generate a new key pair and replace the public half or certificate | Old certificates stay valid until they expire unless revoked |
| Third-party API key | Regenerate in the vendor dashboard | Some vendors invalidate the old key instantly |
For a private key, the key pair is the secret, so you cannot "change" it. You replace it. Remove the old public key from every authorized_keys file or trust store that holds it, and revoke any certificate that was issued for it.
Review what the credential did
Revoking the credential stops new misuse. It does not tell you what already happened. Look at the audit log for the service and check three things:
- Source addresses. Are there requests from networks you do not recognize?
- Actions. Did the key list resources, create new users, or launch compute? Attackers often create persistence, such as new access keys or roles, so check for those even if the old key is now dead.
- Time window. Start from the commit that introduced the secret, not from the day you noticed it. The next chapter shows how to find that commit.
If you find misuse, the scope grows. Credentials created by the attacker also need revoking, and you may have reporting obligations to customers or regulators. Involve your security contact early.
The audit trail usually has limits. CloudTrail's event history, for example, keeps a rolling 90 days of management events. Export the relevant events before you start cleanup so the evidence is not lost.
Common mistakes
- Rewriting history first. The secret stays valid while you work, and the rewrite gives a false sense of safety. Anyone who already copied the secret still has a working key.
- Rotating only one environment. The same value is often reused in staging, production, and developer machines. Search for the value in your other configuration before you call the job done.
- Forgetting dependent systems. Cron jobs, serverless functions, and CI pipelines often read the key from places nobody remembers. This is why deactivating before deleting is useful.
- Treating "private repository" as safe. Private repositories still expose the value to everyone who ever had read access, and to every clone those people made.
- Skipping the audit. A rotated key with no log review leaves open the question of whether an attacker created other access.
What happens next
When the old credential is dead and the audit is done, the urgency is gone. You can now clean the history without time pressure. The rewrite still has value. It removes a dead secret that scanners keep flagging, and it removes any other sensitive content committed alongside it. The following chapters cover how a rewrite changes commits, how git filter-repo differs from older tools, and how to run it safely on a fresh clone.
Exercise
A teammate discovers that a database password was pushed to a private repository two days ago. They propose running git filter-repo right away, then changing the password later "when things calm down". What is wrong with that order, and what should the sequence be?
Show answer
The password stays valid while the rewrite happens, and the rewrite cannot remove copies that already exist in clones, forks, and caches. Anyone who copied the password can still use it. The sequence should be: set a new password and update the applications that connect (restarting any connection pools), then confirm the old password no longer works, then review database logs from the time of the original commit, and only then rewrite history.
Exercise
Why is it better to set an AWS access key to Inactive before deleting it, and when might you skip that step?
Show answer
Inactive is reversible. If a forgotten service breaks, you can set the key to Active again, fix the service, and then deactivate it once more. Deletion cannot be undone. You might skip the inactive step when you suspect active abuse and need the key gone immediately, accepting that an outage is better than continued misuse.