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: Prevent access tokens from being fetched at service account construction in the self-signed-jwt case #467

Merged
merged 1 commit into from
Jan 25, 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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ jobs:
node-version: "16.x"
- name: Install dependencies
shell: bash
run: "gem install --no-document bundler toys && bundle install"
run: |
gem install --no-document toys
bundle install
- name: Test ${{ matrix.task }}
shell: bash
run: toys do ${{ matrix.task }} < /dev/null
22 changes: 16 additions & 6 deletions lib/googleauth/service_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ class ServiceAccountCredentials < Signet::OAuth2::Client
attr_reader :quota_project_id

def enable_self_signed_jwt?
@enable_self_signed_jwt
# Use a self-singed JWT if there's no information that can be used to
# obtain an OAuth token, OR if there are scopes but also an assertion
# that they are default scopes that shouldn't be used to fetch a token,
# OR we are not in the default universe and thus OAuth isn't supported.
target_audience.nil? && (scope.nil? || @enable_self_signed_jwt || universe_domain != "googleapis.com")
end

# Creates a ServiceAccountCredentials.
Expand Down Expand Up @@ -95,17 +99,18 @@ def initialize options = {}
# Extends the base class to use a transient
# ServiceAccountJwtHeaderCredentials for certain cases.
def apply! a_hash, opts = {}
# Use a self-singed JWT if there's no information that can be used to
# obtain an OAuth token, OR if there are scopes but also an assertion
# that they are default scopes that shouldn't be used to fetch a token,
# OR we are not in the default universe and thus OAuth isn't supported.
if target_audience.nil? && (scope.nil? || enable_self_signed_jwt? || universe_domain != "googleapis.com")
if enable_self_signed_jwt?
apply_self_signed_jwt! a_hash
else
super
end
end

# Modifies this logic so it also requires self-signed-jwt to be disabled
def needs_access_token?
super && !enable_self_signed_jwt?
end

private

def apply_self_signed_jwt! a_hash
Expand Down Expand Up @@ -216,6 +221,11 @@ def new_jwt_token jwt_aud_uri = nil, options = {}

JWT.encode assertion, @signing_key, SIGNING_ALGORITHM
end

# Duck-types the corresponding method from BaseClient
def needs_access_token?
false
end
end
end
end
6 changes: 6 additions & 0 deletions spec/googleauth/service_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def expect_is_encoded_jwt hdr
expect_is_encoded_jwt auth_header
end
end

describe "#needs_access_token?" do
it "should always return false" do
expect(@client.needs_access_token?).to eq(false)
end
end
end
end

Expand Down