-
Notifications
You must be signed in to change notification settings - Fork 121
impl(gax-internal): add universe_domain mod helper #5243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alvarowolfx
merged 3 commits into
googleapis:main
from
alvarowolfx:impl-gax-internal-universe-domain-mod
Apr 6, 2026
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use google_cloud_auth::credentials::Credentials; | ||
| use google_cloud_auth::errors::CredentialsError; | ||
|
|
||
| #[allow(dead_code)] | ||
| pub(crate) const DEFAULT_UNIVERSE_DOMAIN: &str = "googleapis.com"; | ||
| const UNIVERSE_DOMAIN_VAR: &str = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"; | ||
|
|
||
| #[allow(dead_code)] | ||
| pub(crate) async fn resolve( | ||
| universe_domain_client_override: Option<&str>, | ||
| cred: &Credentials, | ||
| ) -> Result<String, CredentialsError> { | ||
| let env_universe = std::env::var(UNIVERSE_DOMAIN_VAR).ok(); | ||
| let cred_universe = cred.universe_domain().await; | ||
|
|
||
| let universe_domain = env_universe | ||
| .as_deref() | ||
| .or(universe_domain_client_override) | ||
| .unwrap_or(DEFAULT_UNIVERSE_DOMAIN) | ||
| .to_string(); | ||
|
|
||
| let cred_universe = cred_universe.as_deref().unwrap_or(DEFAULT_UNIVERSE_DOMAIN); | ||
|
|
||
| if universe_domain != cred_universe { | ||
dbolduc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Err(CredentialsError::from_msg( | ||
| false, | ||
| format!( | ||
| "The configured universe domain ({}) does not match the universe domain found in the credentials ({}). If you haven't configured the universe domain explicitly, `googleapis.com` is the default.", | ||
| universe_domain, cred_universe | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| Ok(universe_domain) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use google_cloud_auth::credentials::{CacheableResource, CredentialsProvider}; | ||
| use http::{Extensions, HeaderMap}; | ||
| use scoped_env::ScopedEnv; | ||
| use serial_test::serial; | ||
| use test_case::test_case; | ||
|
|
||
| type TestResult = anyhow::Result<()>; | ||
| type AuthResult<T> = std::result::Result<T, CredentialsError>; | ||
|
|
||
| mockall::mock! { | ||
| #[derive(Debug)] | ||
| Credentials {} | ||
|
|
||
| impl CredentialsProvider for Credentials { | ||
| async fn headers(&self, extensions: Extensions) -> AuthResult<CacheableResource<HeaderMap>>; | ||
| async fn universe_domain(&self) -> Option<String>; | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[test_case(None, None, None, Ok(DEFAULT_UNIVERSE_DOMAIN); "default")] | ||
| #[test_case(Some("universe.com"), None, Some("universe.com"), Ok("universe.com"); "env var only")] | ||
| #[test_case(None, Some("universe.com"), Some("universe.com"), Ok("universe.com"); "client override only")] | ||
| #[test_case(Some("universe.com"), Some("universe.com"), Some("universe.com"), Ok("universe.com"); "all")] | ||
| #[test_case(None, None, Some("universe.com"), Err(CredentialsError::from_msg(false, "universe domain mismatch")); "credentials only")] | ||
| #[test_case(None, Some("test.com"), Some("universe.com"), Err(CredentialsError::from_msg(false, "universe domain mismatch")); "client override mismatch")] | ||
| #[test_case( Some("test.com"), None, Some("universe.com"), Err(CredentialsError::from_msg(false, "universe domain mismatch")); "env var override mismatch")] | ||
| #[serial] | ||
| async fn universe_domain_resolve( | ||
| env_domain: Option<&str>, | ||
| client_override: Option<&str>, | ||
| cred_domain: Option<&str>, | ||
| expected: Result<&str, CredentialsError>, | ||
| ) -> TestResult { | ||
| let _env = match env_domain { | ||
| Some(domain) => ScopedEnv::set("GOOGLE_CLOUD_UNIVERSE_DOMAIN", domain), | ||
| None => ScopedEnv::remove("GOOGLE_CLOUD_UNIVERSE_DOMAIN"), | ||
| }; | ||
| let mut provider = MockCredentials::new(); | ||
| let cred_domain = cred_domain.map(|s| s.to_string()); | ||
| provider | ||
| .expect_universe_domain() | ||
| .returning(move || cred_domain.clone()); | ||
| let cred = Credentials::from(provider); | ||
|
|
||
| let universe_domain = resolve(client_override, &cred).await; | ||
| let expected = expected.map(|s| s.to_string()); | ||
| match (universe_domain, expected) { | ||
dbolduc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (Ok(got), Ok(expected)) => { | ||
| assert_eq!(got, expected, "{got:?}"); | ||
| Ok(()) | ||
| } | ||
| (Err(_), Err(_)) => Ok(()), | ||
| (got, expected) => panic!("Expected {:?}, got {:?}", expected, got), | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if my creds have no universe, and my client has a non-GDU universe, that is a fail. Should it be?
(There is not a test case for this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be a fail. No universe means GDU. If the client provides a non GDU, it's mismatch. I've added a test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implies to me that we messed up the
Credentials::universe_domain()API. It should return aStringinstead of anOption<String>... oh well, too late to fix.