Skip to content

Commit

Permalink
feat: Disable universe domain check if credentials include an explici…
Browse files Browse the repository at this point in the history
…t request to do so
  • Loading branch information
dazuma committed Oct 9, 2024
1 parent 2209fd8 commit eeae4f6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 3 additions & 9 deletions gapic-common/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ AllCops:
- "test/**/*"

Metrics/ClassLength:
Exclude:
- "lib/gapic/generic_lro/operation.rb"
- "lib/gapic/rest/grpc_transcoder.rb"
Max: 200

Metrics/CyclomaticComplexity:
Exclude:
- "lib/gapic/headers.rb"
- "lib/gapic/rest/client_stub.rb"
Max: 15

Metrics/PerceivedComplexity:
Exclude:
- "lib/gapic/headers.rb"
- "lib/gapic/rest/client_stub.rb"
Max: 15

Naming/FileName:
Exclude:
Expand Down
5 changes: 5 additions & 0 deletions gapic-common/lib/gapic/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
module Gapic
# A collection of common header values.
module Headers
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

##
# @param ruby_version [String] The ruby version. Defaults to `RUBY_VERSION`.
# @param lib_name [String] The client library name.
Expand All @@ -32,6 +34,7 @@ module Headers
# `:grpc` to send the GRPC library version (if defined)
# `:rest` to send the REST library version (if defined)
# Defaults to `[:grpc]`
#
def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, gax_version: nil,
gapic_version: nil, grpc_version: nil, rest_version: nil, protobuf_version: nil,
transports_version_send: [:grpc]
Expand All @@ -52,4 +55,6 @@ def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, g
x_goog_api_client_header.join " ".freeze
end
end

# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
end
4 changes: 2 additions & 2 deletions gapic-common/lib/gapic/universe_domain_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def setup_universe_domain universe_domain: nil,
raise ArgumentError, "endpoint or endpoint_template is required" if endpoint.nil? && endpoint_template.nil?
raise ArgumentError, "credentials is required" if credentials.nil?

# TODO: The env logic should live in google-cloud-env
universe_domain ||= ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] || "googleapis.com"
endpoint ||= endpoint_template.sub ENDPOINT_SUBSTITUTION, universe_domain

if credentials.respond_to?(:universe_domain) && credentials.universe_domain != universe_domain
if credentials.respond_to?(:universe_domain) && credentials.universe_domain != universe_domain &&
!(credentials.respond_to?(:disable_universe_domain_check) && credentials.disable_universe_domain_check)
raise UniverseDomainMismatch,
"Universe domain is #{universe_domain} but credentials are in #{credentials.universe_domain}"
end
Expand Down
14 changes: 14 additions & 0 deletions gapic-common/test/gapic/grpc/service_stub_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def universe_domain
end
end

FakeCredentialsWithDisabledCheck = Class.new FakeCredentials do
def disable_universe_domain_check
true
end
end

FakeRpcCall = Class.new do
def initialize method_stub
@method_stub = method_stub
Expand Down Expand Up @@ -270,6 +276,14 @@ def test_universe_domain_credentials_mismatch
end
end

def test_universe_domain_credentials_with_mismatch_disabled
creds = FakeCredentialsWithDisabledCheck.new universe_domain: "myuniverse.com"
service_stub = Gapic::ServiceStub.new FakeGrpcStub,
endpoint_template: "myservice.$UNIVERSE_DOMAIN$",
credentials: creds
assert_equal "googleapis.com", service_stub.universe_domain
end

def test_endpoint_override
creds = FakeCredentials.new universe_domain: "myuniverse.com"
service_stub = Gapic::ServiceStub.new FakeGrpcStub,
Expand Down

0 comments on commit eeae4f6

Please sign in to comment.