Skip to content

Commit 478af43

Browse files
authored
feat(common): Disable universe domain check if credentials include an explicit request to do so (#1119)
1 parent 835ccd8 commit 478af43

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

gapic-common/.rubocop.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@ AllCops:
66
- "test/**/*"
77

88
Metrics/ClassLength:
9-
Exclude:
10-
- "lib/gapic/generic_lro/operation.rb"
11-
- "lib/gapic/rest/grpc_transcoder.rb"
9+
Max: 200
1210

1311
Metrics/CyclomaticComplexity:
14-
Exclude:
15-
- "lib/gapic/headers.rb"
16-
- "lib/gapic/rest/client_stub.rb"
12+
Max: 15
1713

1814
Metrics/PerceivedComplexity:
19-
Exclude:
20-
- "lib/gapic/headers.rb"
21-
- "lib/gapic/rest/client_stub.rb"
15+
Max: 15
2216

2317
Naming/FileName:
2418
Exclude:

gapic-common/lib/gapic/headers.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
module Gapic
2020
# A collection of common header values.
2121
module Headers
22+
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
23+
2224
##
2325
# @param ruby_version [String] The ruby version. Defaults to `RUBY_VERSION`.
2426
# @param lib_name [String] The client library name.
@@ -32,6 +34,7 @@ module Headers
3234
# `:grpc` to send the GRPC library version (if defined)
3335
# `:rest` to send the REST library version (if defined)
3436
# Defaults to `[:grpc]`
37+
#
3538
def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, gax_version: nil,
3639
gapic_version: nil, grpc_version: nil, rest_version: nil, protobuf_version: nil,
3740
transports_version_send: [:grpc]
@@ -52,4 +55,6 @@ def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, g
5255
x_goog_api_client_header.join " ".freeze
5356
end
5457
end
58+
59+
# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
5560
end

gapic-common/lib/gapic/universe_domain_concerns.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def setup_universe_domain universe_domain: nil,
6060
raise ArgumentError, "endpoint or endpoint_template is required" if endpoint.nil? && endpoint_template.nil?
6161
raise ArgumentError, "credentials is required" if credentials.nil?
6262

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

67-
if credentials.respond_to?(:universe_domain) && credentials.universe_domain != universe_domain
66+
if !(credentials.respond_to?(:disable_universe_domain_check) && credentials.disable_universe_domain_check) &&
67+
credentials.respond_to?(:universe_domain) && credentials.universe_domain != universe_domain
6868
raise UniverseDomainMismatch,
6969
"Universe domain is #{universe_domain} but credentials are in #{credentials.universe_domain}"
7070
end

gapic-common/test/gapic/grpc/service_stub_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def universe_domain
5252
end
5353
end
5454

55+
FakeCredentialsWithDisabledCheck = Class.new FakeCredentials do
56+
def disable_universe_domain_check
57+
true
58+
end
59+
end
60+
5561
FakeRpcCall = Class.new do
5662
def initialize method_stub
5763
@method_stub = method_stub
@@ -270,6 +276,14 @@ def test_universe_domain_credentials_mismatch
270276
end
271277
end
272278

279+
def test_universe_domain_credentials_with_mismatch_disabled
280+
creds = FakeCredentialsWithDisabledCheck.new universe_domain: "myuniverse.com"
281+
service_stub = Gapic::ServiceStub.new FakeGrpcStub,
282+
endpoint_template: "myservice.$UNIVERSE_DOMAIN$",
283+
credentials: creds
284+
assert_equal "googleapis.com", service_stub.universe_domain
285+
end
286+
273287
def test_endpoint_override
274288
creds = FakeCredentials.new universe_domain: "myuniverse.com"
275289
service_stub = Gapic::ServiceStub.new FakeGrpcStub,

0 commit comments

Comments
 (0)