Skip to main content
Detecting and Rotating Leaked Cloud Credentials

Detecting and Rotating Leaked Cloud Credentials

KU
· Founder & Chief Editor
• 8 min read•Updated •20 views

Static keys leak. The question isn't if but how fast you notice and how clean your response runbook is when the pager goes off.

Key takeaways

  • When an AWS access key leaks, deactivate it immediately with aws iam update-access-key --status Inactive, then pull its CloudTrail history to find what it touched.
  • Rotate the legitimate consumer to a new key, delete the old one, and remove the root cause.
  • Long term, replace static keys with role assumption and OIDC federation.

When an AWS access key leaks, deactivate it first (aws iam update-access-key --status Inactive), because that is instant and reversible. Then read the key's CloudTrail history to size the damage, rotate the legitimate workload onto a new key, delete the old one, and fix how it escaped. Detection is layered: pre-commit scanning, push protection, and behavioral alerts.

Verified against

gitleaks v8.30.1, trufflehog v3.99.7, AWS CLI v2, against the AWS IAM, CloudTrail LookupEvents and AWSCompromisedKeyQuarantineV3 reference pages and the GitHub push protection docs. Checked October 2026.

The scenario we plan for looks like this: a contractor pushes a .env file to a public fork at 2am, and before anyone notices, someone in another timezone is launching GPU instances to mine crypto. Static credentials leak. Not because your team is careless, but because a long-lived string that grants API access will eventually end up somewhere it shouldn't: a committed config, a debug log, a Slack paste, a stolen laptop, a CI variable printed by a well-meaning set -x.

If your security model assumes keys stay secret, your model is wrong. Assume the leak. Build for detection and fast response.

Detection in layers

No single control catches everything, so stack them.

Pre-commit and pre-push. The cheapest place to stop a leak is before it lands. Run gitleaks in CI and, better, as a local hook. Note that gitleaks deprecated the old detect and protect subcommands in v8.19.0; they still work but are hidden, and the current form is gitleaks git. The README also says the project is now feature complete and future releases are security patches only, which is fine for a scanner you pin.

Bash
# scan the whole history of a repo
$ gitleaks git --report-format json --report-path leaks.json .

# scan only what's staged, as a pre-commit hook
$ gitleaks git --pre-commit --staged --verbose

trufflehog goes a step further and checks whether a found key is live, which cuts the noise from expired or fake test keys:

Bash
$ trufflehog git file://. --results=verified

Turn on GitHub push protection at the organization level. It blocks pushes containing recognized secret patterns, and it is off by default (it needs GitHub Secret Protection). It has stopped more real leaks for us than any scanner we run ourselves, because it catches the human before the mistake is public. A user with write access can bypass a block by giving a reason, and that bypass raises an alert, so route those alerts somewhere a person reads.

Provider-side detection. AWS watches for exposed access keys. When it finds one, it attaches the AWSCompromisedKeyQuarantineV3 managed policy to the user and opens a support case. As of October 2026 the policy denies the high-blast-radius actions: ec2:RunInstances, iam:CreateUser, iam:CreateAccessKey, lambda:CreateFunction, bedrock:InvokeModel and dozens more. AWS's own policy description says do not remove it; follow the support case instead. Do not treat that email as spam. If you use a third party, GitGuardian watches public GitHub and will often ping you before your own scanners run.

Behavioral detection. Scanning finds keys that leaked through code. It does nothing for a key stolen off a laptop. For that you need to watch behavior. GuardDuty flags anomalies like calls from a new geography, GetCallerIdentity reconnaissance, or a burst of RunInstances. If you're rolling your own, CloudTrail plus a few Athena queries for "actions this principal has never called before" gets you most of the way. Pair it with tight least privilege so a live key can only do so much.

The response runbook

When a key is confirmed leaked, work in this order. Speed matters more than elegance.

1. Disable first, ask questions later. Deactivating a key is reversible and instant. Deleting it destroys evidence. Do this before anything else:

Bash
$ aws iam update-access-key \
    --user-name ci-deploy \
    --access-key-id AKIAEXAMPLE12345 \
    --status Inactive

Now the attacker is locked out and you still have the key on record for the investigation.

2. Assess blast radius. What did the key touch? Pull its recent activity from CloudTrail:

Bash
$ aws cloudtrail lookup-events \
    --lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLE12345 \
    --start-time 2026-07-13T00:00:00Z \
    --query 'Events[].{Time:EventTime,Name:EventName,Source:EventSource}' \
    --output table

Two limits bite here. LookupEvents only covers management events from the last 90 days, in one Region per call, and it is rate limited to two requests per second per account per Region. Run it in every Region the account uses, and go to your trail in S3 or CloudTrail Lake for data events like GetObject.

Read the output looking for two things: reconnaissance (ListBuckets, GetCallerIdentity, ListUsers) and impact (RunInstances, CreateUser, PutObject, GetObject on data buckets). If you see CreateAccessKey or CreateUser, the attacker made themselves persistence and disabling one key isn't enough. Widen the hunt.

3. Rotate. Issue a new key, deploy it to the legitimate consumer, verify the workload is healthy, then delete the old one. IAM allows at most two access keys per user, which is exactly what makes this create-then-delete order possible. Order matters so you don't cause your own outage.

4. Root cause. How did it leak? A committed file means you also need to purge git history and assume it's public forever. A CI log means fix the logging, not just the key. Our notes on hardening GitHub Actions cover the masking and permissions side. Write it down in a postmortem while it's fresh.

5. Eliminate the credential type. This is the step most teams skip, and it's the only one that stops the incident from recurring. If a workload leaked a key, that workload probably shouldn't have had a static key at all.

The credential you don't have can't leak

Every step above is response work you're forced into because a long-lived secret existed. The durable fix is to stop having the secret. AWS's own IAM guide opens its access-key page by recommending temporary credentials such as roles instead.

Most things that hold static keys don't need them. CI runners, EC2 instances, EKS pods, Lambda, GitHub Actions, and workloads in other clouds can all get short-lived credentials through role assumption and OIDC federation instead. There's no key to scan for, nothing to rotate, and a stolen token expires on its own whether you notice or not. We wrote up the mechanics in keyless authentication.

Static keys don't disappear entirely. A legacy vendor integration, an on-prem box, a partner API: some corners keep them. For those, scope them to the minimum, put them in a secrets manager with rotation (we compared the options in Vault vs Secrets Manager vs Doppler), and keep the detection layers running. But treat every remaining static key as a liability you're carrying deliberately, not a default.

Questions people ask

What should I do first if my AWS access key is leaked?

Deactivate it with aws iam update-access-key --status Inactive. That locks the attacker out instantly while keeping the key on record for the investigation, and you can reactivate it if you were wrong. Do not delete it yet. Then query CloudTrail by access key ID to see what it did, rotate the legitimate workload to a new key, and only then delete the old one.

Should I remove the AWSCompromisedKeyQuarantine policy AWS attached?

No. AWS's managed policy description says not to remove it and to follow the instructions in the support case it opens. The policy only denies actions attackers abuse, like launching instances, creating users or keys, and invoking Bedrock models, so existing resources keep running. Finish the investigation and rotation first; the affected user should be replaced by a role or a fresh, scoped key anyway.

How do I see what a leaked AWS key accessed?

Run aws cloudtrail lookup-events with AttributeKey=AccessKeyId and the key ID. It returns management events from the last 90 days in one Region, so repeat it per Region. It does not show S3 object reads or other data events; for those, query your CloudTrail trail logs in S3 or an event data store in CloudTrail Lake.

The call we'd make

Detection and a rehearsed runbook are table stakes: run gitleaks in CI, enable push protection, wire up CloudTrail anomaly alerts, and drill the disable-assess-rotate sequence until it's muscle memory. But don't let a smooth runbook become an excuse to keep the keys around. Every static credential you federate away is one you never have to detect, rotate, or explain in a postmortem. Spend your effort there. The best leaked-credential incident is the one that can't happen because there was no credential to leak.

Sources

Explore topics:SecurityAWSCloud
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Detecting and Rotating Leaked Cloud Credentials. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/detecting-and-rotating-leaked-cloud-credentials

Markdown

[Detecting and Rotating Leaked Cloud Credentials](https://www.devopsness.com/blog/detecting-and-rotating-leaked-cloud-credentials) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.