diff --git a/Google.Api.Gax.Grpc.IntegrationTests/ClientBuilderBaseTest.cs b/Google.Api.Gax.Grpc.IntegrationTests/ClientBuilderBaseTest.cs index ae663175..284ee4ef 100644 --- a/Google.Api.Gax.Grpc.IntegrationTests/ClientBuilderBaseTest.cs +++ b/Google.Api.Gax.Grpc.IntegrationTests/ClientBuilderBaseTest.cs @@ -314,8 +314,6 @@ public class SampleClientBuilder : ClientBuilderBase public new string GetEffectiveEndpoint() => base.GetEffectiveEndpoint(); - - private readonly string _name; /// @@ -357,7 +355,7 @@ public override CallInvoker Build() } protected override ChannelPool GetChannelPool() => ChannelPool; - + public void ResetChannelCreation() { EndpointUsedToCreateChannel = null; diff --git a/Google.Api.Gax.Grpc/GoogleCredentialExtensions.cs b/Google.Api.Gax.Grpc/GoogleCredentialExtensions.cs index 57a01b88..aa5fab9c 100644 --- a/Google.Api.Gax.Grpc/GoogleCredentialExtensions.cs +++ b/Google.Api.Gax.Grpc/GoogleCredentialExtensions.cs @@ -12,58 +12,53 @@ using System.Threading; using System.Threading.Tasks; -namespace Google.Api.Gax.Grpc +namespace Google.Api.Gax.Grpc; + +/// +/// Extension methods for Google credential universe domain validation. +/// +internal static class GoogleCredentialExtensions { /// - /// Extension methods for Google credential universe domain validation. + /// Returns a channel credential based on , that will validate its own universe domain + /// against . /// - public static class GoogleCredentialExtensions + /// The Google credential to build the channel credentials from. Must not be null. + /// The universe domain to validate against. Must not be null. + /// should result in the same value as this. + internal static ChannelCredentials ToChannelCredentials(this GoogleCredential googleCredential, string universeDomain) => + new GoogleCredentialWithUniverseDomainValidation(googleCredential, universeDomain).ToChannelCredentials(); + + private class GoogleCredentialWithUniverseDomainValidation : ITokenAccessWithHeaders { - /// - /// Returns a channel credential based on , that will validate its own universe domain - /// against . - /// - /// The Google credential to build the channel credentials from. Must not be null. - /// The universe domain to validate against. Must not be null. - /// should result in the same value as this. - public static ChannelCredentials ToChannelCredentials(this GoogleCredential googleCredential, string universeDomain) => - new GoogleCredentialWithUniverseDomainValidation(googleCredential, universeDomain).ToChannelCredentials(); + private readonly ITokenAccessWithHeaders _underlying; + private readonly string _universeDomain; + private readonly Lazy _universeDomainsMatchCheckCache; - private class GoogleCredentialWithUniverseDomainValidation : ITokenAccessWithHeaders + public GoogleCredentialWithUniverseDomainValidation(GoogleCredential googleCredential, string universeDomain) { - private readonly ITokenAccessWithHeaders _underlying; - private readonly string _universeDomain; - private readonly Lazy> _universeDomainsMatchCache; - - public GoogleCredentialWithUniverseDomainValidation(GoogleCredential googleCredential, string universeDomain) - { - _underlying = GaxPreconditions.CheckNotNull(googleCredential, nameof(googleCredential)); - _universeDomain = GaxPreconditions.CheckNotNull(universeDomain, nameof(universeDomain)); - _universeDomainsMatchCache = new Lazy>(UniverseDomainsMatchUncached); - } + _underlying = GaxPreconditions.CheckNotNull(googleCredential, nameof(googleCredential)); + _universeDomain = GaxPreconditions.CheckNotNull(universeDomain, nameof(universeDomain)); + _universeDomainsMatchCheckCache = new Lazy(UniverseDomainsMatchCheckUncached); + } - public async Task GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default) - { - // We don't need to check the actual value, this either returns true or throws. - await _universeDomainsMatchCache.Value.WithCancellationToken(cancellationToken).ConfigureAwait(false); - return await _underlying.GetAccessTokenForRequestAsync(authUri, cancellationToken).ConfigureAwait(false); - } + public async Task GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default) + { + await _universeDomainsMatchCheckCache.Value.WithCancellationToken(cancellationToken).ConfigureAwait(false); + return await _underlying.GetAccessTokenForRequestAsync(authUri, cancellationToken).ConfigureAwait(false); + } - public async Task GetAccessTokenWithHeadersForRequestAsync(string authUri = null, CancellationToken cancellationToken = default) - { - // We don't need to check the actual value, this either returns true or throws. - await _universeDomainsMatchCache.Value.WithCancellationToken(cancellationToken).ConfigureAwait(false); - return await _underlying.GetAccessTokenWithHeadersForRequestAsync(authUri, cancellationToken).ConfigureAwait(false); - } + public async Task GetAccessTokenWithHeadersForRequestAsync(string authUri = null, CancellationToken cancellationToken = default) + { + await _universeDomainsMatchCheckCache.Value.WithCancellationToken(cancellationToken).ConfigureAwait(false); + return await _underlying.GetAccessTokenWithHeadersForRequestAsync(authUri, cancellationToken).ConfigureAwait(false); + } - private async Task UniverseDomainsMatchUncached() + private async Task UniverseDomainsMatchCheckUncached() + { + string credentialUniverseDomain = await (_underlying as GoogleCredential).GetUniverseDomainAsync(default).ConfigureAwait(false); + if (credentialUniverseDomain != _universeDomain) { - string credentialUniverseDomain = await (_underlying as GoogleCredential).GetUniverseDomainAsync(default).ConfigureAwait(false); - if (credentialUniverseDomain == _universeDomain) - { - return true; - } - throw new InvalidOperationException( $"The service client universe domain {_universeDomain} does not match the credential universe domain {credentialUniverseDomain}." + $"You can configure the universe domain for your service client by using the UniverseDomain property on the corresponding client builder.");