Skip to content

Commit

Permalink
Allow more flexibility to GOOGLE_APPLICATION_CREDENTIALS parsing
Browse files Browse the repository at this point in the history
The GoogleAuth library supports creating credentials for multiple credential types.

Signed-off-by: Robert Clark <[email protected]>
  • Loading branch information
rbclark committed Jul 1, 2022
1 parent 87c6d23 commit a063561
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions generate/libraries/gcp_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def camel_case(data)

class GcpApiConnection
def initialize
@service_account_file = ENV['GOOGLE_APPLICATION_CREDENTIALS']
@google_application_credentials = ENV['GOOGLE_APPLICATION_CREDENTIALS']
end

def fetch_auth
Expand All @@ -200,8 +200,8 @@ def fetch_auth
[
'https://www.googleapis.com/auth/cloud-platform',
],
).from_service_account_json!(
@service_account_file,
).from_google_credentials_json!(
@google_application_credentials,
)
end
Network::Authorization.new.from_application_default!
Expand Down Expand Up @@ -362,10 +362,10 @@ def for!(*scopes)
self
end

def from_service_account_json!(service_account_file)
def from_google_credentials_json!(credentials_file)
raise 'Missing argument for scopes' if @scopes.empty?
@authorization = ::Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(service_account_file),
@authorization = ::Google::Auth::DefaultCredentials.make_creds(
json_key_io: File.open(credentials_file),
scope: @scopes,
)
self
Expand Down
14 changes: 7 additions & 7 deletions libraries/gcp_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,17 @@ class GcpApiConnection
def initialize
config_name = Inspec::Config.cached.unpack_train_credentials[:host]
ENV['CLOUDSDK_ACTIVE_CONFIG_NAME'] = config_name
@service_account_file = config_name.blank? && ENV['GOOGLE_APPLICATION_CREDENTIALS']
@google_application_credentials = config_name.blank? && ENV['GOOGLE_APPLICATION_CREDENTIALS']
end

def fetch_auth
unless @service_account_file.nil?
unless @google_application_credentials.nil?
return Network::Authorization.new.for!(
[
'https://www.googleapis.com/auth/cloud-platform',
],
).from_service_account_json!(
@service_account_file,
).from_google_credentials_json!(
@google_application_credentials,
)
end
Network::Authorization.new.from_application_default!
Expand Down Expand Up @@ -387,10 +387,10 @@ def for!(*scopes)
self
end

def from_service_account_json!(service_account_file)
def from_google_credentials_json!(credentials_file)
raise 'Missing argument for scopes' if @scopes.empty?
@authorization = ::Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(service_account_file),
@authorization = ::Google::Auth::DefaultCredentials.make_creds(
json_key_io: File.open(credentials_file),
scope: @scopes,
)
self
Expand Down

0 comments on commit a063561

Please sign in to comment.