File: google_application_default_credentials.rb

package info (click to toggle)
ruby-kubeclient 4.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,112 kB
  • sloc: ruby: 4,225; makefile: 6
file content (31 lines) | stat: -rw-r--r-- 986 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module Kubeclient
  # Get a bearer token from the Google's application default credentials.
  class GoogleApplicationDefaultCredentials
    class GoogleDependencyError < LoadError # rubocop:disable Lint/InheritException
    end

    class << self
      def token
        begin
          require 'googleauth'
        rescue LoadError => e
          raise GoogleDependencyError,
                'Error requiring googleauth gem. Kubeclient itself does not include the ' \
                'googleauth gem. To support auth-provider gcp, you must include it in your ' \
                "calling application. Failed with: #{e.message}"
        end

        scopes = [
          'https://www.googleapis.com/auth/cloud-platform',
          'https://www.googleapis.com/auth/userinfo.email'
        ]

        authorization = Google::Auth.get_application_default(scopes)
        authorization.apply({})
        authorization.access_token
      end
    end
  end
end