Skip to content

fix(aws-auth): follow full credential chain and honor AWS_DEFAULT_REGION - #75

Open
ozpool wants to merge 1 commit into
Infisical:mainfrom
ozpool:fix/aws-iam-credential-chain-region
Open

fix(aws-auth): follow full credential chain and honor AWS_DEFAULT_REGION#75
ozpool wants to merge 1 commit into
Infisical:mainfrom
ozpool:fix/aws-iam-credential-chain-region

Conversation

@ozpool

@ozpool ozpool commented Jun 15, 2026

Copy link
Copy Markdown

Description

RetrieveAwsCredentials only returned the credentials resolved by the default AWS provider chain when a region had already been detected. When the region was unknown, it fell through to GetAwsRegion, which calls the EC2 instance metadata service (169.254.169.254) directly.

On ECS Fargate there is no instance metadata endpoint, so aws-iam auth failed even when valid credentials were available — task-role credentials via AWS_CONTAINER_CREDENTIALS_RELATIVE_URI, or plain AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY env vars. Setting AWS_EC2_METADATA_DISABLED=true didn't help because the metadata call is a raw HTTP request rather than going through the SDK.

This was reported for the CLI in Infisical/infisical#6523; the CLI delegates aws-iam login to this SDK, and the KMIP/gateway/relay flows call RetrieveAwsCredentials too, so they share the same gap.

Changes

  • RetrieveAwsCredentials: resolve credentials from the standard provider chain unconditionally (env vars → shared config → ECS/EKS container credentials → EC2 IMDS), then resolve the region separately. Region detection no longer short-circuits credential resolution, so an environment without IMDS still authenticates as long as credentials are present.
  • Region resolution order is now: SDK config → AWS_REGIONAWS_DEFAULT_REGION → EC2 metadata service.
  • GetAwsRegion: honor AWS_DEFAULT_REGION in addition to AWS_REGION before reaching for IMDS.

Testing

Added unit tests for the region precedence (AWS_REGION over AWS_DEFAULT_REGION, and the AWS_DEFAULT_REGION fallback). go build ./..., go vet ./packages/util/ and the new tests pass locally.

RetrieveAwsCredentials only returned the credentials resolved by the
default provider chain when a region had already been detected. When the
region was unknown it fell through to GetAwsRegion, which queries the EC2
instance metadata service directly. On ECS Fargate there is no instance
metadata endpoint, so aws-iam auth failed even though valid task-role
credentials were available via AWS_CONTAINER_CREDENTIALS_RELATIVE_URI (or
plain AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY env vars).

Resolve credentials from the standard provider chain unconditionally, and
only look up the region separately: SDK config first, then AWS_REGION /
AWS_DEFAULT_REGION, and the metadata service last. Region detection no
longer short-circuits credential resolution.

GetAwsRegion now also honors AWS_DEFAULT_REGION before reaching for IMDS.
@greptile-apps

greptile-apps Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes AWS IAM authentication in environments without EC2 IMDS (e.g. ECS Fargate) by decoupling credential resolution from region resolution in RetrieveAwsCredentials, and adds AWS_DEFAULT_REGION as a recognised fallback in GetAwsRegion.

  • RetrieveAwsCredentials now always calls config.LoadDefaultConfig (full SDK provider chain) before attempting region detection, so task-role credentials via AWS_CONTAINER_CREDENTIALS_RELATIVE_URI or plain env-var credentials are resolved even when no region is available from IMDS.
  • GetAwsRegion checks AWS_REGION then AWS_DEFAULT_REGION before falling back to the raw EC2 metadata service call; two unit tests cover this priority order.
  • The raw HTTP call to 169.254.169.254 in GetAwsEC2IdentityDocumentRegion is still the last resort for region resolution and does not honour AWS_EC2_METADATA_DISABLED=true, so environments that set that flag but omit both region env vars will see a 5-second timeout before failing on the region step.

Confidence Score: 4/5

Safe to merge for the primary ECS Fargate use-case; the raw IMDS fallback for region is a pre-existing limitation that remains after this change.

The credential-resolution logic is now correctly decoupled from region detection, which is the right fix for Fargate environments. The residual gap — the raw IMDS HTTP call for region still ignoring AWS_EC2_METADATA_DISABLED — is a pre-existing limitation and not a regression introduced here. It causes a 5-second delay and a returned error rather than incorrect authentication, so the failure mode is safe.

The region-fallback path in packages/util/auth.go (GetAwsEC2IdentityDocumentRegion) warrants a follow-up to respect the AWS_EC2_METADATA_DISABLED environment variable.

Security Review

  • IMDS link-local request (packages/util/auth.go, GetAwsEC2IdentityDocumentRegion): Two raw resty HTTP requests are issued to 169.254.169.254 as a last-resort region fallback. URLs are hardcoded constants (not user-supplied), so exploitation requires out-of-band routing manipulation. Risk is low but the call bypasses AWS_EC2_METADATA_DISABLED=true.
  • No user-controlled URLs are introduced by this PR; credential and region sources (env vars, SDK config, IMDS) are all trusted system sources.

Important Files Changed

Filename Overview
packages/util/auth.go Credentials are now always resolved via the AWS SDK provider chain before region is consulted; region falls back through SDK config → env vars → EC2 IMDS. The raw IMDS call in GetAwsEC2IdentityDocumentRegion still does not respect AWS_EC2_METADATA_DISABLED.
packages/util/auth_test.go New unit tests cover AWS_REGION precedence and AWS_DEFAULT_REGION fallback using t.Setenv for safe isolation; no issues found.

Comments Outside Diff (1)

  1. packages/util/auth.go, line 209-218 (link)

    P2 security SSRF note — raw IMDS HTTP call still reachable

    GetAwsEC2IdentityDocumentRegion makes two raw HTTP requests to 169.254.169.254 (the link-local EC2 Instance Metadata Service). The URLs are hardcoded constants so there is no user-controlled redirect, but the function is now the last-resort fallback for any caller — including RetrieveAwsCredentials — on every platform that lacks AWS_REGION / AWS_DEFAULT_REGION. In a non-EC2 network environment a misconfigured or attacker-controlled routing table could theoretically redirect 169.254.169.254 to an internal endpoint. The risk is low in practice (link-local is typically not routable), but worth acknowledging in the context of the custom instruction to flag SSRF vectors.

    Context Used: Flag SSRF risks (source)

Reviews (1): Last reviewed commit: "fix(aws-auth): follow full credential ch..." | Re-trigger Greptile

Comment thread packages/util/auth.go
Comment on lines +242 to +246
if awsRegion == "" {
awsRegion, err = GetAwsRegion()
if err != nil {
return aws.Credentials{}, "", err
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 AWS_EC2_METADATA_DISABLED still bypassed for region resolution

The PR correctly moves credential retrieval through the AWS SDK (which honours AWS_EC2_METADATA_DISABLED=true), but when awsCfg.Region is empty the fallback reaches GetAwsRegion()GetAwsEC2IdentityDocumentRegion, which issues a raw resty HTTP request to 169.254.169.254. That raw call does not check AWS_EC2_METADATA_DISABLED, so an operator who disables IMDS but forgets to set AWS_REGION / AWS_DEFAULT_REGION will still see a timeout attempt against the metadata service in the region-resolution path. Credentials succeed; region resolution hangs until the 5-second timeout fires and then returns an error.

For ECS Fargate the fix is sufficient as long as a region env var is set, but the IMDS-disabled bypass is a residual gap worth documenting (or mitigating by checking the env var before the raw HTTP call).

@ozpool

ozpool commented Jun 30, 2026

Copy link
Copy Markdown
Author

Hi @varonix0 — gentle first nudge on this one, no rush. It's been green since open (GitGuardian + Greptile pass) and fixes aws-iam auth on ECS Fargate, where there's no instance-metadata endpoint so the current region-detection path fails even with valid task-role / env credentials. The change just follows the full AWS provider chain and honors AWS_DEFAULT_REGION before falling back to IMDS. Happy to rebase or add a test if useful.

@infisical-cla-app

Copy link
Copy Markdown

📝 Contributor License Agreement required

Before this PR can merge, every contributor must sign the Infisical CLA.
Signing is quick: sign in with GitHub, review the CLA, and accept.

👉 Sign the CLA

Still needs to sign:

Once everyone has signed, the check updates automatically — no need to close and reopen the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant