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

Move credential checks into the AzureConnection#authenticate #314

Merged
merged 2 commits into from
Oct 1, 2020
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
27 changes: 7 additions & 20 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ end
namespace :syntax do
desc 'InSpec syntax check'
task :inspec do
puts '-> Checking The Environment Variables: Assigning placeholders if they are not defined.'
ENV['AZURE_SUBSCRIPTION_ID'] = 'placeHolder' unless ENV['AZURE_SUBSCRIPTION_ID']
ENV['AZURE_CLIENT_ID'] = 'placeHolder' unless ENV['AZURE_CLIENT_ID']
ENV['AZURE_TENANT_ID'] = 'placeHolder' unless ENV['AZURE_TENANT_ID']
ENV['AZURE_CLIENT_SECRET'] = 'placeHolder' unless ENV['AZURE_CLIENT_SECRET']
puts '-> Checking InSpec Control Syntax'
stdout, status = Open3.capture2("bundle exec inspec vendor #{INTEGRATION_DIR} --overwrite --chef-license accept-silent &&
bundle exec inspec check #{INTEGRATION_DIR}")
Expand Down Expand Up @@ -103,23 +98,15 @@ namespace :azure do
end
end

# Minitest
Rake::TestTask.new(:unit) do |t|
t.libs << 'test/unit'
t.libs << 'libraries'
t.verbose = true
t.warning = false
t.test_files = FileList['test/unit/**/*_test.rb']
end

namespace :test do

task :unit do
ENV['AZURE_SUBSCRIPTION_ID'] = 'placeHolder' unless ENV['AZURE_SUBSCRIPTION_ID']
ENV['AZURE_CLIENT_ID'] = 'placeHolder' unless ENV['AZURE_CLIENT_ID']
ENV['AZURE_TENANT_ID'] = 'placeHolder' unless ENV['AZURE_TENANT_ID']
ENV['AZURE_CLIENT_SECRET'] = 'placeHolder' unless ENV['AZURE_CLIENT_SECRET']
Rake::Task['unit'].execute
# Minitest
Rake::TestTask.new(:unit) do |t|
t.libs << 'test/unit'
t.libs << 'libraries'
t.verbose = true
t.warning = false
t.test_files = FileList['test/unit/**/*_test.rb']
end

task :integration, [:controls] => ['attributes:write', :setup_env] do |_t, args|
Expand Down
6 changes: 1 addition & 5 deletions libraries/azure_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ def initialize(opts = {})
# Fail resource if the http client is not properly set up.
begin
@azure = AzureConnection.new(@client_args)
rescue HTTPClientError::MissingCredentials => e
message = "HTTP Client Error.\n#{e.message}"
resource_fail(message)
raise HTTPClientError, message
rescue StandardError => e
message = "Resource is failed due to #{e}"
message = "HTTP client is failed due to #{e}"
resource_fail(message)
raise StandardError, message
end
Expand Down
12 changes: 6 additions & 6 deletions libraries/backend/azure_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ def initialize(client_args)
client_secret: ENV['AZURE_CLIENT_SECRET'],
subscription_id: ENV['AZURE_SUBSCRIPTION_ID'],
}
# Validate the presence of credentials.
unless @credentials.values.compact.delete_if(&:empty?).size == 4
raise HTTPClientError::MissingCredentials, 'The following must be set in the Environment:'\
" #{@credentials.keys}.\n"\
"Provided: #{@credentials}"
end

@connection ||= Faraday.new do |conn|
# Implement user provided HTTP client params for handling TimeOut exceptions.
Expand Down Expand Up @@ -123,6 +117,12 @@ def rest_get_call(url, params = {})
# https://docs.microsoft.com/en-us/rest/api/azure/
#
def authenticate(resource)
# Validate the presence of credentials.
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? }}"
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"
body = {
Expand Down
8 changes: 6 additions & 2 deletions test/integration/verify/controls/azurerm_resource_groups.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
resource_group = input('resource_group', value: nil)

# Added to test `inspec check` command
resource_group_names = azure_resource_groups.names

control 'azurerm_resource_groups' do
describe azurerm_resource_groups do
it { should exist }
its('names') { should include(resource_group) }
it { should exist }
its('names') { should include(resource_group) }
its('names.size') { should eq resource_group_names.size }
end

describe azurerm_resource_groups.where(name: resource_group) do
Expand Down