Sha256: 6dd0c0d4c0356efa3b66e4d19ca11ad49eb1d97c2b857b9775c0834a12ebc66c

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'googleauth'
module KubernetesDeploy
  module KubeclientBuilder
    class GoogleFriendlyConfig < Kubeclient::Config
      def fetch_user_auth_options(user)
        if user.dig('auth-provider', 'name') == 'gcp'
          { bearer_token: new_token }
        else
          super
        end
      end

      def self.read(filename)
        new(YAML.safe_load(File.read(filename), [Time]), File.dirname(filename))
      end

      def new_token
        scopes = ['https://www.googleapis.com/auth/cloud-platform']
        authorization = Google::Auth.get_application_default(scopes)

        authorization.apply({})

        authorization.access_token

      rescue Signet::AuthorizationError => e
        err_message = json_error_message(e.response.body) || e.message
        raise KubeException.new(e.response.status, err_message, e.response.body)
      end

      private

      def json_error_message(body)
        err = JSON.parse(body || '') || {}
        err['message']
      rescue JSON::ParserError
        nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kubernetes-deploy-0.24.0 lib/kubernetes-deploy/kubeclient_builder/google_friendly_config.rb
kubernetes-deploy-0.23.0 lib/kubernetes-deploy/kubeclient_builder/google_friendly_config.rb