Skip to content

Commit 635f91e

Browse files
authored
feat(core): Verify credential universe domain against configured universe domain (#17569)
1 parent 3fc901f commit 635f91e

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

google-apis-core/lib/google/apis/core/base_service.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ def fetch_all(max: nil, items: :items, cache: true, response_page_token: :next_p
336336
return PagedResults.new(self, max: max, items: items, cache: cache, response_page_token: response_page_token, &block)
337337
end
338338

339+
# Verify that the universe domain setting matches the universe domain
340+
# in the credentials, if present.
341+
#
342+
# @raise [Google::Apis::UniverseDomainError] if there is a mismatch
343+
def verify_universe_domain!
344+
auth = authorization
345+
auth_universe_domain = auth.universe_domain if auth.respond_to? :universe_domain
346+
if auth_universe_domain && auth_universe_domain != universe_domain
347+
raise UniverseDomainError,
348+
"Universe domain is #{universe_domain} but credentials are in #{auth_universe_domain}"
349+
end
350+
true
351+
end
352+
339353
protected
340354

341355
# Create a new upload command.
@@ -348,6 +362,7 @@ def fetch_all(max: nil, items: :items, cache: true, response_page_token: :next_p
348362
# Request-specific options
349363
# @return [Google::Apis::Core::UploadCommand]
350364
def make_upload_command(method, path, options)
365+
verify_universe_domain!
351366
template = Addressable::Template.new(root_url + upload_path + path)
352367
if batch?
353368
command = MultipartUploadCommand.new(method, template, client_version: client_version)
@@ -372,6 +387,7 @@ def make_upload_command(method, path, options)
372387
# Request-specific options
373388
# @return [Google::Apis::Core::StorageUploadCommand]
374389
def make_storage_upload_command(method, path, options)
390+
verify_universe_domain!
375391
template = Addressable::Template.new(root_url + upload_path + path)
376392
command = StorageUploadCommand.new(method, template, client_version: client_version)
377393
command.options = request_options.merge(options)
@@ -389,6 +405,7 @@ def make_storage_upload_command(method, path, options)
389405
# Request-specific options
390406
# @return [Google::Apis::Core::DownloadCommand]
391407
def make_download_command(method, path, options)
408+
verify_universe_domain!
392409
template = Addressable::Template.new(root_url + base_path + path)
393410
command = DownloadCommand.new(method, template, client_version: client_version)
394411
command.options = request_options.merge(options)
@@ -408,6 +425,7 @@ def make_download_command(method, path, options)
408425
# Request-specific options
409426
# @return [Google::Apis::Core::StorageDownloadCommand]
410427
def make_storage_download_command(method, path, options)
428+
verify_universe_domain!
411429
template = Addressable::Template.new(root_url + base_path + path)
412430
command = StorageDownloadCommand.new(method, template, client_version: client_version)
413431
command.options = request_options.merge(options)
@@ -426,6 +444,7 @@ def make_storage_download_command(method, path, options)
426444
# Request-specific options
427445
# @return [Google::Apis::Core::DownloadCommand]
428446
def make_simple_command(method, path, options)
447+
verify_universe_domain!
429448
full_path =
430449
if path.start_with? "/"
431450
path[1..-1]

google-apis-core/lib/google/apis/errors.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,9 @@ class ServerError < Error
8989
# Error class for problems in batch requests.
9090
class BatchError < Error
9191
end
92+
93+
# Error class for universe domain issues
94+
class UniverseDomainError < Error
95+
end
9296
end
9397
end

google-apis-core/spec/google/apis/core/service_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@
133133
expect(a_request(:get, 'https://www.googleapis.com/zoo/animals').with(headers: expected_headers)).to have_been_made
134134
end
135135

136+
it "should verify universe domain" do
137+
service.authorization = OpenStruct.new universe_domain: "mydomain.com"
138+
expect do
139+
command
140+
end.to raise_error(Google::Apis::UniverseDomainError)
141+
end
142+
136143
include_examples 'with options'
137144
end
138145

@@ -531,4 +538,23 @@
531538
service.root_url = "https://endpoint2.$UNIVERSE_DOMAIN$/"
532539
expect(service.root_url).to eql "https://endpoint2.mydomain6.com/"
533540
end
541+
542+
describe "#verify_universe_domain!" do
543+
it "should skip universe domain verification if credentials do not have them" do
544+
service_ud.authorization = "I have no universe domain"
545+
service_ud.verify_universe_domain!
546+
end
547+
548+
it "should verify default universe domain" do
549+
service_ud.authorization = OpenStruct.new universe_domain: "googleapis.com"
550+
service_ud.verify_universe_domain!
551+
end
552+
553+
it "should raise on universe domain mismatch" do
554+
service_ud.authorization = OpenStruct.new universe_domain: "mydomain.com"
555+
expect do
556+
service_ud.verify_universe_domain!
557+
end.to raise_error(Google::Apis::UniverseDomainError)
558+
end
559+
end
534560
end

0 commit comments

Comments
 (0)