forked from inspec/inspec-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
220 lines (190 loc) · 6.55 KB
/
Rakefile
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true
require 'bundler'
require 'bundler/gem_helper'
require 'rake/testtask'
require 'rubocop/rake_task'
require 'fileutils'
require 'open3'
require_relative 'lib/attribute_file_writer'
require_relative 'lib/environment_file'
RuboCop::RakeTask.new
FIXTURE_DIR = "#{Dir.pwd}/test/fixtures"
TERRAFORM_DIR = 'terraform'
REQUIRED_ENVS = %w{AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_TENANT_ID AZURE_SUBSCRIPTION_ID}.freeze
INTEGRATION_DIR = 'test/integration/verify'
TF_PLAN_FILE_NAME = 'inspec-azure.plan'
TF_PLAN_FILE = File.join(TERRAFORM_DIR, TF_PLAN_FILE_NAME)
ATTRIBUTES_FILE_NAME = ''
task default: :test
desc 'Testing tasks'
task test: %w{lint test:unit}
desc 'Linting tasks'
task lint: [:rubocop, :'syntax:ruby', :'syntax:inspec']
task :setup_env do
puts '-> Loading Environment Variables'
ENV['TF_VAR_subscription_id'] = ENV['AZURE_SUBSCRIPTION_ID']
ENV['TF_VAR_tenant_id'] = ENV['AZURE_TENANT_ID']
ENV['TF_VAR_client_id'] = ENV['AZURE_CLIENT_ID']
ENV['TF_VAR_client_secret'] = ENV['AZURE_CLIENT_SECRET']
puts '-> Ensuring required Environment Variables are set'
missing = REQUIRED_ENVS.reject { |var| ENV.key?(var) }
abort("ENV missing: #{missing.join(', ')}") if missing.any?
# Notify user which optional components they are using
options = EnvironmentFile.options('.envrc')
if options.empty?
puts "-> You are not using any optional components. See the README for more information.\n\n"
else
puts "-> You are using the following optional components:\n\n"
options.each do |option|
puts "* #{option}\n"
end
puts "\nTo change these options run: rake options[component] or add desired components to your .envrc file. See the README for more information.\n\n"
end
end
namespace :syntax do
desc 'InSpec syntax check'
task :inspec do
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}")
puts stdout
%w{errors}.each do |type|
sh("rm -rf #{INTEGRATION_DIR}/vendor")
abort("InSpec check failed with syntax #{type}!") if !!(/[1-9]\d* #{type}/ =~ stdout)
end
status.exitstatus
end
desc 'Ruby syntax check'
task :ruby do
puts '-> Checking Ruby Syntax'
files = %w{Gemfile Rakefile} + Dir['./lib*/**/*.rb'] + Dir['./test/**/*.rb']
files.each do |file|
sh('ruby', '-c', file) do |ok, res|
next if ok
puts 'Syntax check FAILED'
exit res.exitstatus
end
end
end
end
namespace :azure do
desc 'Authenticate with the Azure CLI'
task login: [:setup_env] do
puts '-> Logging into Azure'
sh(
'az', 'login',
'--service-principal',
'-u', ENV['AZURE_CLIENT_ID'],
'-p', ENV['AZURE_CLIENT_SECRET'],
'--tenant', ENV['AZURE_TENANT_ID']
)
puts '-> Setting Subscription'
sh(
'az', 'account', 'set',
'-s', ENV['AZURE_SUBSCRIPTION_ID']
)
end
end
namespace :test do
# 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] => ['tf:write_tf_output_to_file', :setup_env] do |_t, args|
cmd = %W( bundle exec inspec exec #{INTEGRATION_DIR}
--input-file terraform/#{ENV['ATTRIBUTES_FILE']}
--reporter cli
--no-distinct-exit
-t azure://#{ENV['AZURE_SUBSCRIPTION_ID']}
--chef-license accept-silent )
if args[:controls]
sh(*cmd, '--controls', args[:controls], *args.extras)
else
sh(*cmd)
end
end
end
namespace :tf do # rubocop:disable Metrics/BlockLength
workspace = ENV['WORKSPACE']
task init: [:'azure:login'] do
abort('$WORKSPACE not set. Please source .envrc.') if workspace.nil?
abort('$WORKSPACE has no content. Check .envrc.') if workspace.empty?
Dir.chdir(TERRAFORM_DIR) do
sh('terraform', 'init')
end
end
task workspace: [:init] do
Dir.chdir(TERRAFORM_DIR) do
sh('terraform', 'workspace', 'select', workspace) do |ok, _|
next if ok
sh('terraform', 'workspace', 'new', workspace)
end
end
end
desc 'Creates a Terraform execution plan from the plan file'
task :plan, [:optionals] => [:workspace] do |_t, args|
if args[:optionals]
ignore_list = Array(args[:optionals]) + args.extras
ignore_list.each do |component|
ENV["TF_VAR_#{component}_count"] = '0'
end
end
Dir.chdir(TERRAFORM_DIR) do
sh('terraform', 'get')
sh('terraform', 'plan', '-out', 'inspec-azure.plan')
end
Rake::Task['tf:write_tf_output_to_file'].invoke
end
desc 'Executes the Terraform plan'
task :apply, [:optionals] do |_t, args|
if File.exist?(TF_PLAN_FILE)
puts "-> Applying an existing terraform plan: #{TF_PLAN_FILE}"
unless args[:optionals].nil?
puts "These arguments are ignored: #{Array(args[:optionals]) + args.extras}."
end
else
Rake::Task['tf:plan'].invoke(args[:optionals])
end
Dir.chdir(TERRAFORM_DIR) do
sh('terraform', 'apply', 'inspec-azure.plan')
end
end
desc 'Destroys the Terraform environment'
task destroy: [:workspace] do
Dir.chdir(TERRAFORM_DIR) do
sh('terraform', 'destroy', '-force')
end
end
task write_tf_output_to_file: [:workspace] do
Dir.chdir(TERRAFORM_DIR) do
stdout, stderr, status = Open3.capture3('terraform output -json')
abort(stderr) unless status.success?
abort('$ATTRIBUTES_FILE not set. Please source .envrc.') if ENV['ATTRIBUTES_FILE'].nil?
abort('$ATTRIBUTES_FILE has no content. Check .envrc.') if ENV['ATTRIBUTES_FILE'].empty?
AttributeFileWriter.write_yaml(ENV['ATTRIBUTES_FILE'], stdout)
end
end
end
namespace :docs do
desc 'Prints markdown links for resource doc files to update the README'
task :resource_links do
puts "\n"
Dir.entries('docs/resources').sort
.reject { |file| File.directory?(file) }
.collect { |file| "- [#{file.split('.')[0]}](docs/resources/#{file})" }
.map { |link| puts link }
puts "\n"
end
end
namespace :attributes do
desc 'Create attributes used for integration testing'
task :write do
abort('$ATTRIBUTES_FILE not set. Please source .envrc.') if ENV['ATTRIBUTES_FILE'].nil?
abort('$ATTRIBUTES_FILE has no content. Check .envrc.') if ENV['ATTRIBUTES_FILE'].empty?
Rake::Task['tf:write_tf_output_to_file'].invoke
end
end