Skip to content
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

fix: GCECredentials lazily fetches from the metadata server to ensure a universe domain is known #509

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/googleauth/compute_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ def initialize options = {}
super options
end

# @private
# Overrides universe_domain getter to fetch lazily if it hasn't been
# fetched yet. This is necessary specifically for Compute Engine because
# the universe comes from the metadata service, and isn't known
# immediately on credential construction. All other credential types read
# the universe from their json key or other immediate input.
def universe_domain
value = super
return value unless value.nil?
fetch_access_token!
super
end

# Overrides the super class method to change how access tokens are
# fetched.
def fetch_access_token _options = {}
Expand Down
15 changes: 15 additions & 0 deletions spec/googleauth/compute_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def make_auth_stubs opts
expect(@client.universe_domain).to eq("googleapis.com")
end

it "sets the universe without explicit fetch_access_token" do
make_auth_stubs access_token: "1/abcde"
expect(@client.universe_domain).to eq("googleapis.com")
end

it "returns a consistent expiry using cached data" do
make_auth_stubs access_token: "1/abcde"
@client.fetch_access_token!
Expand All @@ -121,6 +126,11 @@ def make_auth_stubs opts
expect(@client.universe_domain).to eq("googleapis.com")
end

it "sets the universe without explicit fetch_access_token" do
make_auth_stubs access_token: "1/abcde"
expect(@client.universe_domain).to eq("googleapis.com")
end

it "returns a consistent expiry using cached data" do
make_auth_stubs access_token: "1/abcde"
@client.fetch_access_token!
Expand All @@ -144,6 +154,11 @@ def make_auth_stubs opts
expect(@client.universe_domain).to eq("myuniverse.com")
end

it "sets the universe without explicit fetch_access_token" do
make_auth_stubs access_token: "1/abcde"
expect(@client.universe_domain).to eq("myuniverse.com")
end

it "supports updating the universe_domain" do
make_auth_stubs access_token: "1/abcde"
@client.fetch_access_token!
Expand Down
Loading