Skip to content

fix(google-auth): add AWS Fargate metadata endpoint support#16129

Open
ggrossman wants to merge 2 commits intogoogleapis:mainfrom
Quickture:fix/aws-fargate-metadata-support
Open

fix(google-auth): add AWS Fargate metadata endpoint support#16129
ggrossman wants to merge 2 commits intogoogleapis:mainfrom
Quickture:fix/aws-fargate-metadata-support

Conversation

@ggrossman
Copy link

Summary

On AWS Fargate, the IMDS security credentials endpoint does not use role names in the URL structure (unlike EC2 instances). The current _DefaultAwsSecurityCredentialsSupplier unconditionally fetches the IAM role name and appends it to the credentials URL, which fails on Fargate with a RefreshError.

This PR:

  • Adds _is_fargate_environment() to detect Fargate via ECS-specific environment variables (ECS_CONTAINER_METADATA_URI_V4, ECS_CONTAINER_METADATA_URI, or AWS_EXECUTION_ENV containing AWS_ECS_FARGATE)
  • Skips the role name lookup on Fargate and calls the security credentials URL directly
  • Preserves the existing EC2 behavior (3 metadata requests: region, role, credentials) when not on Fargate
  • Fargate path makes only 2 metadata requests: region + credentials

Behavior comparison

Environment Metadata requests Credentials URL
EC2 (unchanged) region → role name → credentials {url}/{role_name}
Fargate (new) region → credentials {url} (no role suffix)

Files changed

  • packages/google-auth/google/auth/aws.py — Fargate detection + conditional role name handling
  • packages/google-auth/tests/test_aws.py — 5 new test cases covering all detection methods, EC2 regression, and unit tests for _is_fargate_environment()

Fixes https://github.com/googleapis/google-auth-library-python/issues/1099

On AWS Fargate, the IMDS security credentials endpoint does not use
role names in the URL structure (unlike EC2). The current implementation
unconditionally fetches the role name and appends it to the credentials
URL, which fails on Fargate.

This change detects Fargate environments via ECS-specific environment
variables (ECS_CONTAINER_METADATA_URI_V4, ECS_CONTAINER_METADATA_URI,
or AWS_EXECUTION_ENV containing AWS_ECS_FARGATE) and skips the role
name lookup, calling the security credentials URL directly.

Fixes googleapis/google-auth-library-python#1099
@ggrossman ggrossman requested review from a team as code owners March 19, 2026 05:46
@google-cla
Copy link

google-cla bot commented Mar 19, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where Google Auth's AWS security credentials supplier failed on AWS Fargate due to an incorrect assumption about the metadata endpoint URL structure. It introduces logic to detect Fargate environments and adjust the credential retrieval process to bypass the role name lookup, aligning with Fargate's metadata service behavior. This change ensures seamless authentication for applications running on Fargate while maintaining compatibility with traditional EC2 environments.

Highlights

  • Fargate Environment Detection: Implemented _is_fargate_environment() to identify AWS Fargate based on ECS environment variables.
  • Conditional Role Name Handling: Modified credential retrieval to skip IAM role name lookup for Fargate environments, as their metadata endpoint does not use role names in the URL.
  • Optimized Metadata Requests: Reduced the number of metadata requests for Fargate environments from three (region, role, credentials) to two (region, credentials).
  • EC2 Behavior Preservation: Ensured that the existing behavior for EC2 instances, including role name lookup, remains unchanged.
  • Comprehensive Testing: Added five new test cases to cover Fargate detection methods, Fargate credential retrieval, and to confirm EC2 regression.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for AWS Fargate metadata endpoints by detecting the Fargate environment and skipping the IAM role name lookup, which is not used in Fargate's credential URL structure. The changes are well-implemented and include comprehensive tests for the new logic and for ensuring existing EC2 behavior is preserved. The suggestion to improve code conciseness is valid and has been retained.

Comment on lines +630 to +636
if os.environ.get("ECS_CONTAINER_METADATA_URI_V4"):
return True
if os.environ.get("ECS_CONTAINER_METADATA_URI"):
return True
if "AWS_ECS_FARGATE" in os.environ.get("AWS_EXECUTION_ENV", ""):
return True
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability and conciseness, you can combine these checks into a single boolean expression.

        return bool(
            os.environ.get("ECS_CONTAINER_METADATA_URI_V4")
            or os.environ.get("ECS_CONTAINER_METADATA_URI")
            or "AWS_ECS_FARGATE" in os.environ.get("AWS_EXECUTION_ENV", "")
        )

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