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

Getting id-token from compute engine default service account not working #299

Closed
feliara8 opened this issue Feb 24, 2021 · 5 comments · Fixed by #425
Closed

Getting id-token from compute engine default service account not working #299

feliara8 opened this issue Feb 24, 2021 · 5 comments · Fixed by #425
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@feliara8
Copy link

feliara8 commented Feb 24, 2021

Environment details

  • OS: Compute engine with machine for docker containers
  • Ruby version: 2.7.1
  • Gem name and version: googleauth last version

Description

I want to make a call to an api in a iap secured app engine from a compute engine with the proper service account.
I am trying to follow this link
which included the ruby code to do the job, but its not working

NOTE: Node code works perfectly

Steps to reproduce

  1. Create a compute engine with a service account
  2. Go to ruby console
  3. Run ruby code to get id_token, but access token is retrieved despite passing target_audience

Code example

# url = "The Identity-Aware Proxy-protected URL to fetch"
# client_id = "The client ID used by Identity-Aware Proxy"
require "googleauth"
require "faraday"

# The client ID as the target audience for IAP
id_token_creds = Google::Auth::Credentials.default target_audience: client_id

headers = {}
id_token_creds.client.apply! headers

resp = Faraday.get url, nil, headers

if resp.status == 200
  puts "X-Goog-Iap-Jwt-Assertion:"
  puts resp.body
else
  puts "Error requesting IAP"
  puts resp.status
  puts resp.headers
end

Thanks!

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Feb 25, 2021
@dazuma dazuma added priority: p2 Moderately-important priority. Fix may not be included in next release. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 5, 2021
@yoshi-automation yoshi-automation removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Mar 5, 2021
@bouk
Copy link
Contributor

bouk commented Mar 23, 2021

It seems the target_audience option is not being saved. If you do id_token_creds.target_audience you'll get nil.

As a workaround you can do id_token_creds.client.target_audience = '...'

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels Jun 21, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Nov 3, 2021
@ernsheong
Copy link

Thanks for workaround, I also expected Google::Auth::Credentials.default target_audience: ... to work because of sample code in #258

But it is also not yet documented:
https://www.rubydoc.info/github/google/google-auth-library-ruby/Google/Auth/Credentials#default-class_method

@henriquesobral
Copy link

I had the same problem using Cloud Run.

Looking at the code, the problem is at line 63 of application_default.rb file.
When GCECredentials is instantiated, it didn't take the options arguments and therefore target_audience became nil.
This is why it is necessary to revalue target_audience again.

@StupidCodeFactory
Copy link
Contributor

StupidCodeFactory commented Apr 3, 2023

I've had the same problem, using Cloud Run and Could Job. I believe I've fixed the issue in this PR:
#425 🤞 this will get approved and merged soon!
In my case to circumvent the issue I've done an absolutely horrendous monkey patch 🙈 before invoking the authentication code in my code:

require "googleauth"

module Google::Auth
  module_function

  def get_application_default(scope = nil, options = {})
    creds = DefaultCredentials.from_env(scope, options) ||
            DefaultCredentials.from_well_known_path(scope, options) ||
            DefaultCredentials.from_system_default_path(scope, options)
    return creds unless creds.nil?

    unless GCECredentials.on_gce? options
      GCECredentials.unmemoize_all
      raise NOT_FOUND_ERROR
    end
    # Merge option with the scope and pass this down to the Oauth2 client
    GCECredentials.new(options.merge(scope: scope)) 
  end
end

This works and I now get an id_token rather than an auth_token

@bkoski
Copy link

bkoski commented Apr 29, 2023

So many thanks @StupidCodeFactory, I spent an hour banging my head against this before I realized the fix was in the latest gem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants