# ip-locked-creds: AWS credentials that only work from your IP (STS GetScopedCredentials credential_process helper)

> Source: <https://gist.github.com/batzel-upenn/f39dffac046941877cf32e979f549c31>
> Published: 2026-11-23 00:44:48+00:00

**Recommendation:** Issue agent and CLI credentials through a credential_process helper that scopes them to your current IP with a 15-minute lifetime, and add sts:GetScopedCredentials to the role first.
More in the playbook "Securing agents and AI tools": <https://wpnews.pro/playbooks/agent-security>

I spend most of my day in AWS accounts across our organization, signed in through Identity Center, usually with an AI coding assistant open next to my terminal. So I think about where my credentials end up more than I used to. When AWS announced a new STS API in November 2026, my first thought was: can I make my credentials useless to anyone who isn't at my desk?

It turns out I can, and it took under 100 lines of Python.

`GetScopedCredentials` ([announcement](https://aws.amazon.com/about-aws/whats-new/2026/11/sts-getscopedcredentials/), [AWS Security Blog](https://aws.amazon.com/blogs/security/scope-down-permissions-of-existing-sessions-using-aws-sts-getscopedcredentials/)) takes temporary credentials you already have, plus an inline policy, a shorter duration, or both. It gives you back credentials for the same identity. The new credentials can do only what both your session and the policy allow, so they can never do more than what you started with. CloudTrail logs every call, including the policy you sent.

AWS pitches it mainly for giving AI agents narrower permissions. I wanted something simpler first.

Temporary keys leak in boring ways. They get pasted into a chat or a ticket, printed in a debug log, saved in a test file, or read by an assistant and kept in its transcript. While setting this up, I found 25 old temporary keys pasted into my own `~/.aws/credentials` file from months ago. They'd long since expired, but they were a good reminder.

A leaked temporary key normally works from anywhere until it expires, which can be up to 12 hours away. A scope-down policy can carry conditions, so I lock mine to the public IP I'm on:

```
{
  "Version": "2012-10-17",
  "Statement": [
    {"Effect": "Allow", "Action": "*", "Resource": "*"},
    {"Effect": "Deny", "Action": "*", "Resource": "*",
     "Condition": {
       "NotIpAddress": {"aws:SourceIp": "${IP}"},
       "BoolIfExists": {"aws:ViaAWSService": "false"}
     }}
  ]
}
```

- **The `Allow *` is needed.** A scope-down policy can only narrow, so a policy with nothing but a deny in it leaves you with no permissions at all.`Allow *` keeps whatever your role already has.
- **The deny does the locking.** It blocks any request that doesn't come from`${IP}` , which my helper fills in with my current address each time it issues credentials.
- **`aws:ViaAWSService` is required.** Some services make follow-on calls with your credentials. Athena reads your S3 data, and CloudFormation creates resources for you when the stack has no service role. Those calls come from AWS addresses, so without this exception the lock would break them.`BoolIfExists` keeps the deny aimed at requests you make directly.
- **The lifetime is 15 minutes.** A key that leaks fails everywhere except my network, and soon stops working there too.

**1. Get current tools.** You need Python 3.10 or newer and boto3 1.44.9 or later. The AWS CLI only needs to be v2, because the helper calls STS through boto3. Check that your boto3 has the API:

``` python
python3 -c "import boto3; print(boto3.__version__, 'GetScopedCredentials' in boto3.client('sts', region_name='us-east-1').meta.service_model.operation_names)"
```

One gotcha: the Python that ships with macOS is 3.9. pip there quietly installs an older boto3 that doesn't have the call, and the error you get is just "no attribute `get_scoped_credentials`".

**2. Check that your role is allowed to call it.** `GetScopedCredentials` needs an explicit `sts:GetScopedCredentials` allow in the role's own permissions. Roles with `*`, such as AdministratorAccess and PowerUserAccess, have it automatically. Plenty of roles don't. When I first tried it on a read-only security role, I got `AccessDenied ... no identity-based policy allows the sts:GetScopedCredentials action`. The AWS managed policies I checked, including ReadOnlyAccess and ViewOnlyAccess, don't grant it.

The fix is one statement added to the role, or to the permission set that creates it:

```
{"Sid": "AllowScopeDown", "Effect": "Allow", "Action": "sts:GetScopedCredentials", "Resource": "*"}
```

It adds no access, because the call can only return credentials that are narrower than the ones you already have. I tested it on a role with only ReadOnlyAccess: denied before, allowed about 10 seconds after the change, and the scoped credentials still couldn't write anything.

**3. Add the helper.** `ip-locked-creds` looks up my public IP and calls `GetScopedCredentials` with the policy above. It prints the result in the format the CLI's `credential_process` expects. It caches the credentials until my IP changes or they're close to expiring, because the CLI runs it on every command. The heart of it:

```
ip = urlopen("https://checkip.amazonaws.com").read().decode().strip()
sts = boto3.Session(profile_name=source).client("sts")
r = sts.get_scoped_credentials(
    Policy=json.dumps(POLICY).replace("${IP}", ip),
    DurationSeconds=900,
    ScopedSessionDescription=f"ip-locked:{source}")
```

**4. Split each profile in two.** I renamed each SSO profile with a `-sso` suffix and added a profile with the original name that calls the helper:

```
[profile dev-sso]
sso_session = mycompany
sso_account_id = 111122223333
sso_role_name = Developer
region = us-east-1

[profile dev]
credential_process = /path/to/ip-locked-creds dev-sso
region = us-east-1
```

Profiles that need to chain roles add `--allow-assume` (see the limits below).
I still run `aws sso login` in the morning as before. Everything that uses `--profile dev` or `AWS_PROFILE=dev` now gets IP-locked credentials without knowing it. That includes my scripts, my tools and the assistant. I had about 17 profiles, so I wrote a small script to do the split for me.

The helper, the config splitter and a short README are in [this gist](https://gist.github.com/batzel-upenn/f39dffac046941877cf32e979f549c31).

**Don't test this with `aws sts get-caller-identity`.** It's everyone's go-to check, but IAM can't deny it. It succeeds from any IP and under any policy, so it tells you your credentials are valid, not what they're allowed to do. Use something deniable, such as `aws s3 ls`.

What I saw:

- `aws s3 ls --profile dev` worked. The first call took about 2 seconds, and later ones, served from the cache, under 1 second.
- Credentials locked to a different address got `AccessDenied` from S3 and`UnauthorizedOperation` from EC2.`get-caller-identity` happily succeeded.
- When I dropped off the VPN and reconnected, my address changed. The next command issued fresh credentials locked to the new address, and I didn't have to do anything.
- CloudTrail shows each `GetScopedCredentials` call with the address it came from and the IP written into its policy. They matched every time.

To simulate a leak yourself, export one set of credentials and change networks:

```
eval "$(aws configure export-credentials --profile dev --format env)"
aws s3 ls        # works
# switch networks
aws s3 ls        # AccessDenied
```

- **Shared addresses.** On a campus network or a VPN, many machines share one public IP, so the lock admits all of them. At home it's much tighter.
- **Role chaining.** If you call`sts:AssumeRole` with IP-locked credentials, the new session has no lock, because the scope-down policy doesn't carry over to the new session. I tested it: a scoped session that couldn't list buckets assumed a role, and the new session listed them fine. Off my network the`AssumeRole` call itself is denied, but a role I assume from my desk gives me ordinary credentials, and those can leak. So the helper denies`sts:AssumeRole*` by default. Only two of my profiles, the ones I use to jump into other accounts, run it with`--allow-assume` . The rest stay locked down.
- **The SSO token.** It still sits in`~/.aws/sso/cache` , and anything that can read it can mint full credentials. That's the problem for[Part 2](https://gist.github.com/batzel-upenn/9bc6ca479b3153d81da0df457716ea92) : keeping the token, and the full credentials, away from the processes I run every day, including that AI assistant.
