Skip to content

feat(auth): AWS sourced external accounts#4790

Merged
alvarowolfx merged 5 commits intogoogleapis:mainfrom
alvarowolfx:impl-ext-acc-aws
Feb 25, 2026
Merged

feat(auth): AWS sourced external accounts#4790
alvarowolfx merged 5 commits intogoogleapis:mainfrom
alvarowolfx:impl-ext-acc-aws

Conversation

@alvarowolfx
Copy link
Copy Markdown
Collaborator

Towards #3644

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.98%. Comparing base (d27b560) to head (7df7dc8).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
...redentials/external_account_sources/aws_sourced.rs 94.08% 10 Missing ⚠️
src/auth/src/credentials/external_account.rs 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4790      +/-   ##
==========================================
- Coverage   95.01%   94.98%   -0.03%     
==========================================
  Files         204      205       +1     
  Lines        7877     8054     +177     
==========================================
+ Hits         7484     7650     +166     
- Misses        393      404      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alvarowolfx
Copy link
Copy Markdown
Collaborator Author

Successfully tested using our internal go/3pi-sdk-testing process.

use google_cloud_auth::credentials::Builder;
use google_cloud_storage::client::StorageControl;

const BUCKET_ID: &str = "REDACTED";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    println!("Building Credentials...");
    // Will use ADC with the environment variable set
    let creds = Builder::default().build()?;
    println!("Credentials built: {:?}", creds);

    let client = StorageControl::builder()
        .with_credentials(creds)
        .build()
        .await?;
    let bucket = client
        .get_bucket()
        .set_name(format!("projects/_/buckets/{BUCKET_ID}"))
        .send()
        .await?;
    println!("successfully obtained bucket metadata {bucket:?}");

    Ok(())
}

output:

[REDACTED rust]$ export GOOGLE_APPLICATION_CREDENTIALS=/home/ec2-user/aws-credentials.json
[REDACTED rust]$ ./external_account 
Building Credentials...
Credentials built: Credentials { inner: AccessTokenCredentials { inner: ExternalAccountCredentials { token_provider: TokenCache { rx_token: Receiver { shared: Shared { value: RwLock(PhantomData<std::sync::poison::rwlock::RwLock<core::option::Option<core::result::Result<(google_cloud_auth::token::Token, google_cloud_auth::credentials::EntityTag), google_cloud_gax::error::credentials::CredentialsError>>>>, RwLock { data: None }), version: Version(0), is_closed: false, ref_count_rx: 1 }, version: Version(0) } }, quota_project_id: None } } }

successfully obtained bucket metadata Bucket { name: "projects/_/buckets/REDACTED", bucket_id: "REDACTED ", etag: "CAg=", project: "projects/REDACTED", metageneration: 8, location: "US", location_type: "multi-region", storage_class: "STANDARD", rpo: "DEFAULT", acl: [], default_object_acl: [], lifecycle: None, create_time: Some(Timestamp { seconds: 1600382470, nanos: 633000000 }), cors: [], update_time: Some(Timestamp { seconds: 1719936392, nanos: 166000000 }), default_event_based_hold: false, labels: {}, website: None, versioning: None, logging: None, owner: None, encryption: None, billing: None, retention_policy: None, iam_config: Some(IamConfig { uniform_bucket_level_access: Some(UniformBucketLevelAccess { enabled: false, lock_time: None }), public_access_prevention: "inherited" }), satisfies_pzs: false, custom_placement_config: None, autoclass: None, hierarchical_namespace: None, soft_delete_policy: Some(SoftDeletePolicy { retention_duration: Some(Duration { seconds: 604800, nanos: 0 }), effective_time: Some(Timestamp { seconds: 1709280000, nanos: 0 }) }), object_retention: None, ip_filter: None }

@alvarowolfx alvarowolfx marked this pull request as ready for review February 24, 2026 19:56
@alvarowolfx alvarowolfx requested review from a team as code owners February 24, 2026 19:56
Copy link
Copy Markdown
Collaborator

@coryan coryan left a comment

Choose a reason for hiding this comment

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

Pretty good, a few nits and suggestions. Try to refactor the code making HTTP requests so the error handling is all in one place.

Comment on lines +227 to +232
let sts_url = if sts_url.starts_with("http") {
sts_url
} else {
format!("https://{sts_url}")
};
let url = url::Url::parse(&sts_url)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you are already going to use url::Url::parse() shouldn't that be able to handle the missing scheme?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

url::Url::parse() return Err with relative URL without a base if scheme is missing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SG, though I think I assume you could do something like:

let url = match url::Url::parse(sts_url) {
    Ok(u) => Ok(u),
    Err(ParseError::RelativeUrlWithoutBase) => url::Url::parse(format!("https://{sts_url}")),
    Err(e) => Err(e),
};
let url = url.map_err(|e| ....)?;

Comment on lines +269 to +287
fn parse_region_from_zone(zone: &str) -> Option<String> {
let zone = zone.trim();
if zone.is_empty() {
return None;
}
if let Some(last_char) = zone.chars().last() {
if last_char.is_ascii_alphabetic() && zone.len() > 1 {
let potential_region = &zone[..zone.len() - 1];
if potential_region
.chars()
.last()
.is_some_and(|c| c.is_ascii_digit())
{
return Some(potential_region.to_string());
}
}
}
Some(zone.to_string())
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider:

Suggested change
fn parse_region_from_zone(zone: &str) -> Option<String> {
let zone = zone.trim();
if zone.is_empty() {
return None;
}
if let Some(last_char) = zone.chars().last() {
if last_char.is_ascii_alphabetic() && zone.len() > 1 {
let potential_region = &zone[..zone.len() - 1];
if potential_region
.chars()
.last()
.is_some_and(|c| c.is_ascii_digit())
{
return Some(potential_region.to_string());
}
}
}
Some(zone.to_string())
}
fn parse_region_from_zone(zone: &str) -> Option<&str> {
let zone = zone.trim();
let parts: Vec<&str> = id.split('-').collect();
match &parts[..] {
[geo, region, letter] if !letter.is_empty() && !geo.is_empty() => Some(region),
_ => None,
}
}

If you push me, we can save the memory allocation using:

fn parse_region_from_zone(zone: &str) -> Option<&str> {
    let zone = zone.trim();
    let parts = id.split('-');
    match (parts.next(), parts.next(), parts.next(), parts.next()) {
        (Some(geo), Some(region), Some(z), None] if !z.is_empty() && !geo.is_empty() => Some(region),
        _ => None,
    }
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, I see in the tests that AWS zones can have four parts: just add a branch to the match case.

Copy link
Copy Markdown
Collaborator Author

@alvarowolfx alvarowolfx Feb 25, 2026

Choose a reason for hiding this comment

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

This function is about converting from a zone like "us-east-1d" to a region "us-east-1", which is mostly just about removing the final d letter at the end. There is just this special case where some gov zones already don't have the letter at the end, so I check that if the last one is a letter or not. Returning just the region is not the would return east instead of us-east

Comment on lines +408 to +414
if !response.status().is_success() {
return Err(errors::from_http_response(
response,
"could not resolve AWS credentials",
)
.await);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Here is another...

Copy link
Copy Markdown
Collaborator Author

@alvarowolfx alvarowolfx left a comment

Choose a reason for hiding this comment

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

addressed some of the review comments

Comment on lines +227 to +232
let sts_url = if sts_url.starts_with("http") {
sts_url
} else {
format!("https://{sts_url}")
};
let url = url::Url::parse(&sts_url)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

url::Url::parse() return Err with relative URL without a base if scheme is missing

Comment on lines +269 to +287
fn parse_region_from_zone(zone: &str) -> Option<String> {
let zone = zone.trim();
if zone.is_empty() {
return None;
}
if let Some(last_char) = zone.chars().last() {
if last_char.is_ascii_alphabetic() && zone.len() > 1 {
let potential_region = &zone[..zone.len() - 1];
if potential_region
.chars()
.last()
.is_some_and(|c| c.is_ascii_digit())
{
return Some(potential_region.to_string());
}
}
}
Some(zone.to_string())
}
Copy link
Copy Markdown
Collaborator Author

@alvarowolfx alvarowolfx Feb 25, 2026

Choose a reason for hiding this comment

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

This function is about converting from a zone like "us-east-1d" to a region "us-east-1", which is mostly just about removing the final d letter at the end. There is just this special case where some gov zones already don't have the letter at the end, so I check that if the last one is a letter or not. Returning just the region is not the would return east instead of us-east

@alvarowolfx alvarowolfx requested a review from coryan February 25, 2026 17:16
Comment on lines +227 to +232
let sts_url = if sts_url.starts_with("http") {
sts_url
} else {
format!("https://{sts_url}")
};
let url = url::Url::parse(&sts_url)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SG, though I think I assume you could do something like:

let url = match url::Url::parse(sts_url) {
    Ok(u) => Ok(u),
    Err(ParseError::RelativeUrlWithoutBase) => url::Url::parse(format!("https://{sts_url}")),
    Err(e) => Err(e),
};
let url = url.map_err(|e| ....)?;

@alvarowolfx alvarowolfx merged commit 2c37dd6 into googleapis:main Feb 25, 2026
35 checks passed
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.

2 participants