Skip to content

fix(oauth2): fail closed on universe_domain - #16757

Merged
scotthart merged 2 commits into
googleapis:mainfrom
scotthart:oauth2_ud_fail_closed
Dec 4, 2026
Merged

scotthart merged 2 commits into
googleapis:mainfrom
scotthart:oauth2_ud_fail_closed

Conversation

@scotthart

Copy link
Copy Markdown
Member

Previously, oauth2_internal::Credentials::universe_domain() provided a default implementation returning "googleapis.com". Any credential subclass that omitted an override silently reported the Google Default Universe (GDU), which could misroute AddUniverseDomainOption() or REST service account impersonation requests to googleapis.com endpoints in non-GDU / TPC environments.

Additionally, AuthorizedUserCredentials parsed universe_domain into AuthorizedUserCredentialsInfo without exposing it via universe_domain(), and ParseAuthorizedUserCredentials() defaulted an omitted token_uri to https://oauth2.googleapis.com/token even when a non-GDU universe_domain was specified.

@scotthart
scotthart requested a review from a team as a code owner December 3, 2026 20:22

@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 refactors the Credentials base class by making universe_domain() pure virtual, ensuring all derived credential types explicitly implement it. It adds custom universe domain support to AccessTokenCredentials and AuthorizedUserCredentials, validates token_uri requirements for non-default domains, and introduces extensive unit tests to cover these scenarios. As there are no review comments to evaluate, I have no feedback to provide.

@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.

Thanks for tightening up universe_domain() to fail closed and forwarding client_factory in ImpersonateServiceAccountCredentials! A few items to address/discuss:

  1. Build failure (-Werror=range-loop-construct) in google/cloud/internal/oauth2_authorized_user_credentials_test.cc:433 (ParseNonStringTokenUriFails):
    for (std::string const& bad_value : {"123", "null", "[]", "{}"}) binds std::string const& to a temporary constructed from char const*, failing the Fedora/GCC builds (cmake-gcs-rest-pr, noex-pr, openssl-4-pr). Switching the loop variable to char const* const bad_value (with std::string(...) concatenation) or std::string_view const bad_value (with absl::StrCat) will fix this.

  2. Breaking change for GDU REST MakeImpersonateServiceAccountCredentials(MakeAccessTokenCredentials(...), ...):
    Returning kNotFound from AccessTokenCredentials::universe_domain() when internal::UniverseDomainOption is absent breaks existing GDU callers that impersonate a service account using MakeAccessTokenCredentials() over REST (as reflected in the updated ImpersonateServiceAccount test in unified_rest_credentials_test.cc:478-480). Because UniverseDomainOption is still in google::cloud::internal (TODO(#13115)), public callers currently have no public API option to restore the previous behavior, and gRPC impersonation (MakeMinimalIamCredentialsOptions) still defaults to iamcredentials.googleapis.com. Could we confirm whether this breaking change is intended prior to exposing a public UniverseDomainOption (and/or aligning gRPC behavior)?

  3. Empty info_.universe_domain check in AuthorizedUserCredentials::universe_domain() (google/cloud/internal/oauth2_authorized_user_credentials.cc:147-149):
    While ParseAuthorizedUserCredentials() always populates universe_domain, AuthorizedUserCredentialsInfo is an aggregate struct that can be constructed with an empty universe_domain. Checking if (info_.universe_domain.empty()) and returning internal::InvalidArgumentError(...) (consistent with AccessTokenCredentials::universe_domain()) ensures AuthorizedUserCredentials also fails closed and cannot return an OK empty string.

  4. Nit (Docs): Consider updating the Doxygen comments on GetUniverseDomain() / AddUniverseDomainOption() in google/cloud/universe_domain.h to note that credential types without a universe domain (such as API key, GDCH, or access token credentials without UniverseDomainOption) return non-OK statuses (kUnimplemented / kNotFound).

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.23810% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.93%. Comparing base (261271c) to head (9062739).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
google/cloud/internal/oauth2_credentials_test.cc 0.00% 2 Missing ⚠️
...ternal/oauth2_minimal_iam_credentials_rest_test.cc 92.85% 1 Missing ⚠️
...le/cloud/internal/unified_rest_credentials_test.cc 99.10% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16757      +/-   ##
==========================================
- Coverage   92.93%   92.93%   -0.01%     
==========================================
  Files        2282     2282              
  Lines      232838   233330     +492     
==========================================
+ Hits       216399   216839     +440     
- Misses      16439    16491      +52     

☔ 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.

@scotthart
scotthart merged commit 42aafa2 into googleapis:main Dec 4, 2026
68 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