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

RESOURCE-354 Accept credentials from URI #655

Merged
merged 7 commits into from
May 18, 2022
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
6 changes: 6 additions & 0 deletions libraries/azure_generic_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def to_s(class_name = nil)
end
end

def resource_id
return NullResponse.new if failed_resource?

id
end

def resource_group
return unless exists?
res_group, _provider, _res_type = Helpers.res_group_provider_type_from_uri(id)
Expand Down
40 changes: 24 additions & 16 deletions libraries/backend/azure_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class AzureConnection
# @return [String] the graph api endpoint api version, e.g. v1.0
attr_reader :graph_api_endpoint_api_version

# @return [Hash] tenant_id, client_id, client_secret, subscription_id
attr_reader :credentials

# Creates a HTTP client.
def initialize(client_args)
# Validate parameter's type.
Expand All @@ -62,13 +59,6 @@ def initialize(client_args)
@key_vault_dns_suffix = @client_args[:endpoint].key_vault_dns_suffix
@graph_api_endpoint_api_version = @client_args[:endpoint].graph_api_endpoint_api_version

@credentials = {
tenant_id: ENV['AZURE_TENANT_ID'],
client_id: ENV['AZURE_CLIENT_ID'],
client_secret: ENV['AZURE_CLIENT_SECRET'],
subscription_id: ENV['AZURE_SUBSCRIPTION_ID'],
}

@connection ||= Faraday.new do |conn|
# Implement user provided HTTP client params for handling TimeOut exceptions.
# https://www.rubydoc.info/gems/faraday/Faraday/Request/Retry
Expand All @@ -81,6 +71,18 @@ def initialize(client_args)
end
end

# azure://<client_id>:<secret>@<tenant_id>/<subscription_id>
# @return [Hash] tenant_id, client_id, client_secret, subscription_id
def credentials
# azure://<user>:<password>@<host>/<path>
@credentials ||= {
tenant_id: creds_from_uri[:host] || ENV['AZURE_TENANT_ID'],
client_id: creds_from_uri[:user] || ENV['AZURE_CLIENT_ID'],
client_secret: creds_from_uri[:password] || ENV['AZURE_CLIENT_SECRET'],
subscription_id: creds_from_uri[:path]&.gsub('/', '') || ENV['AZURE_SUBSCRIPTION_ID'],
}
end

def provider_details
@@provider_details
end
Expand Down Expand Up @@ -149,17 +151,17 @@ def rest_api_call(opts)
#
def authenticate(resource)
# Validate the presence of credentials.
unless @credentials.values.compact.delete_if(&:empty?).size == 4
unless credentials.values.compact.delete_if(&:empty?).size == 4
raise HTTPClientError::MissingCredentials, 'The following must be set in the Environment:'\
" #{@credentials.keys}.\n"\
"Missing: #{@credentials.keys.select { |key| @credentials[key].nil? }}"
" #{credentials.keys}.\n"\
"Missing: #{credentials.keys.select { |key| credentials[key].nil? }}"
end
# Build up the url that is required to authenticate with Azure REST API
auth_url = "#{@client_args[:endpoint].active_directory_endpoint_url}#{@credentials[:tenant_id]}/oauth2/token"
auth_url = "#{@client_args[:endpoint].active_directory_endpoint_url}#{credentials[:tenant_id]}/oauth2/token"
body = {
grant_type: 'client_credentials',
client_id: @credentials[:client_id],
client_secret: @credentials[:client_secret],
client_id: credentials[:client_id],
client_secret: credentials[:client_secret],
resource: resource,
}
headers = {
Expand Down Expand Up @@ -248,4 +250,10 @@ def send_request(opts)
raise StandardError, "This method is not supported: #{opts[:method]}"
end
end

private

def creds_from_uri
Inspec::Config.cached.unpack_train_credentials
end
end