Skip to content

fix(oauth2): ensure universe_domain field is correctly parsed and used with impersonated sa creds - #16727

Open
scotthart wants to merge 2 commits into
googleapis:mainfrom
scotthart:impersonate_sa_parse_ud
Open

scotthart wants to merge 2 commits into
googleapis:mainfrom
scotthart:impersonate_sa_parse_ud

Conversation

@scotthart

Copy link
Copy Markdown
Member

No description provided.

@scotthart
scotthart requested a review from a team as a code owner November 30, 2026 21:58

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

Copy link
Copy Markdown

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 introduces universe domain validation for impersonated service account credentials. It parses the universe domain from the impersonation URL and ensures consistency between the top-level universe domain, the impersonation URL host, and the source credentials' universe domain. The review feedback correctly identifies two critical safety issues where parsing JSON fields without type validation could throw exceptions, violating the SDK's policy against using C++ exceptions in library code. These should be addressed by validating that the fields are of the expected types (string and object, respectively) before parsing.

@scotthart scotthart left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Overall this looks really solid—great catch on the missing zero-arg universe_domain() overrides on ExternalAccountCredentials and ImpersonateServiceAccountCredentials, and the end-to-end tests in oauth2_google_credentials_test.cc and universe_domain_test.cc are thorough.

A couple of minor edge cases to consider before merging (note: the two gemini-code-assist comments on lines 171 and 241 appear to be false positives since !it->is_string() and !it->is_object() are already checked at lines 163 and 234):

  1. [Medium] Canonicalize universe_domain casing (google/cloud/internal/oauth2_impersonate_service_account_credentials.cc, lines 178–191 and 245–258)
    ParseImpersonationUrl lowercases parsed_url->universe_domain, and both top_universe_domain and source_universe_domain are validated with absl::EqualsIgnoreCase. However, line 188 preserves the raw casing of top_universe_domain, and lines 245–255 leave (*it)["universe_domain"] un-normalized when source_universe_domain matches only case-insensitively. Downstream checks like ServiceAccountUseOAuth() (oauth2_service_account_credentials.cc:386-389) compare universe_domain != GoogleDefaultUniverseDomain() case-sensitively, so "GOOGLEAPIS.COM" would be misclassified as a custom universe domain and non-canonical casing on source_credentials would leak through ImpersonateServiceAccountCredentials::universe_domain(). Always setting info.universe_domain = std::move(parsed_url->universe_domain); and normalizing (*it)["universe_domain"] = info.universe_domain; avoids this cleanly.

  2. [Medium] iamcredentials.mtls.<universe_domain> / .rep. hosts (google/cloud/internal/oauth2_impersonate_service_account_credentials.cc, lines 102–108)
    Stripping only "iamcredentials." causes https://iamcredentials.mtls.googleapis.com/... (or regional .rep. hosts) to pass IsValidHostname() with universe_domain = "mtls.googleapis.com". If top-level universe_domain is omitted, that bogus domain gets propagated to source_credentials and AddUniverseDomainOption(); if top-level universe_domain is "googleapis.com", it fails as a mismatch. Worth either handling optional "mtls." (if expected) or explicitly rejecting "mtls."/.rep. hosts with a clear error rather than deriving them as the universe domain.

  3. [Nit] Hardcoded expireTime in tests (google/cloud/internal/oauth2_google_credentials_test.cc, lines 586, 653, 703)
    "expireTime":"2026-12-01T00:00:00Z" is right around the current date while calling GetToken(std::chrono::system_clock::now()). Using a timestamp relative to now (or a fixed time_point) keeps the tests hermetic if expiration checks/caching are ever added here.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.30233% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.81%. Comparing base (434f132) to head (41aeee7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
google/cloud/internal/oauth2_google_credentials.cc 92.30% 1 Missing ⚠️
.../oauth2_impersonate_service_account_credentials.cc 98.85% 1 Missing ⚠️
...h2_impersonate_service_account_credentials_test.cc 99.25% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16727      +/-   ##
==========================================
- Coverage   92.82%   92.81%   -0.01%     
==========================================
  Files        2279     2279              
  Lines      229093   229505     +412     
==========================================
+ Hits       212647   213013     +366     
- Misses      16446    16492      +46     

☔ View full report in Codecov by Harness.
📢 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.

This branch has not been deployed

No deployments
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