Repository navigation
fix(oauth2): ensure universe_domain field is correctly parsed and used with impersonated sa creds - #16727
fix(oauth2): ensure universe_domain field is correctly parsed and used with impersonated sa creds#16727scotthart wants to merge 2 commits into
Conversation
…d with impersonated sa creds
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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):
-
[Medium] Canonicalize
universe_domaincasing (google/cloud/internal/oauth2_impersonate_service_account_credentials.cc, lines 178–191 and 245–258)
ParseImpersonationUrllowercasesparsed_url->universe_domain, and bothtop_universe_domainandsource_universe_domainare validated withabsl::EqualsIgnoreCase. However, line 188 preserves the raw casing oftop_universe_domain, and lines 245–255 leave(*it)["universe_domain"]un-normalized whensource_universe_domainmatches only case-insensitively. Downstream checks likeServiceAccountUseOAuth()(oauth2_service_account_credentials.cc:386-389) compareuniverse_domain != GoogleDefaultUniverseDomain()case-sensitively, so"GOOGLEAPIS.COM"would be misclassified as a custom universe domain and non-canonical casing onsource_credentialswould leak throughImpersonateServiceAccountCredentials::universe_domain(). Always settinginfo.universe_domain = std::move(parsed_url->universe_domain);and normalizing(*it)["universe_domain"] = info.universe_domain;avoids this cleanly. -
[Medium]
iamcredentials.mtls.<universe_domain>/.rep.hosts (google/cloud/internal/oauth2_impersonate_service_account_credentials.cc, lines 102–108)
Stripping only"iamcredentials."causeshttps://iamcredentials.mtls.googleapis.com/...(or regional.rep.hosts) to passIsValidHostname()withuniverse_domain = "mtls.googleapis.com". If top-leveluniverse_domainis omitted, that bogus domain gets propagated tosource_credentialsandAddUniverseDomainOption(); if top-leveluniverse_domainis"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. -
[Nit] Hardcoded
expireTimein 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 callingGetToken(std::chrono::system_clock::now()). Using a timestamp relative tonow(or a fixedtime_point) keeps the tests hermetic if expiration checks/caching are ever added here.
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
No description provided.