Class: Compliance::ComplianceCLI
Class Method Summary
collapse
Instance Method Summary
collapse
default_options, detect, exec_options, exit_on_failure?, parse_reporters, profile_options, target_options, validate_reporters
Class Method Details
.banner(command, _namespace = nil, _subcommand = false) ⇒ Object
13
14
15
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 13
def self.banner(command, _namespace = nil, _subcommand = false)
"#{basename} #{subcommand_prefix} #{command.usage}"
end
|
.subcommand_prefix ⇒ Object
17
18
19
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 17
def self.subcommand_prefix
namespace
end
|
Instance Method Details
#download(profile_name) ⇒ Object
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
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 100
def download(profile_name)
o = options.dup
configure_logger(o)
config = Compliance::Configuration.new
return if !loggedin(config)
profile_name = Compliance::API.sanitize_profile_name(profile_name)
if Compliance::API.exist?(config, profile_name)
puts "Downloading `#{profile_name}`"
fetcher = Compliance::Fetcher.resolve(
{
compliance: profile_name,
},
)
_owner, id = profile_name.split('/')
file_name = fetcher.fetch(o.name || id)
puts "Profile stored to #{file_name}"
else
puts "Profile #{profile_name} is not available in Chef Compliance."
exit 1
end
end
|
#exec(*tests) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 78
def exec(*tests)
config = Compliance::Configuration.new
return if !loggedin(config)
o = opts(:exec).dup
diagnose(o)
configure_logger(o)
tests = tests.map { |t| 'compliance://' + Compliance::API.sanitize_profile_name(t) }
runner = Inspec::Runner.new(o)
tests.each { |target| runner.add_target(target) }
exit runner.run
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message
exit 1
end
|
#login(server) ⇒ Object
44
45
46
47
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 44
def login(server)
options['server'] = server
Compliance::API.login(options)
end
|
#logout ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 227
def logout
config = Compliance::Configuration.new
unless config.supported?(:oidc) || config['token'].nil? || config['server_type'] == 'automate'
config = Compliance::Configuration.new
url = "#{config['server']}/logout"
Compliance::API.post(url, config['token'], config['insecure'], !config.supported?(:oidc))
end
success = config.destroy
if success
puts 'Successfully logged out'
else
puts 'Could not log out'
end
end
|
#profiles ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 52
def profiles
config = Compliance::Configuration.new
return if !loggedin(config)
config['owner'] = options['owner'] || config['user']
msg, profiles = Compliance::API.profiles(config)
profiles.sort_by! { |hsh| hsh['title'] }
if !profiles.empty?
headline('Available profiles:')
profiles.each { |profile|
li("#{profile['title']} v#{profile['version']} (#{mark_text(profile['owner_id'] + '/' + profile['name'])})")
}
else
puts msg, 'Could not find any profiles'
exit 1
end
rescue Compliance::ServerConfigurationMissing
STDERR.puts "\nServer configuration information is missing. Please login using `inspec compliance login`"
exit 1
end
|
#upload(path) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity
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
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 132
def upload(path) config = Compliance::Configuration.new
return if !loggedin(config)
config['owner'] = options['owner'] || config['user']
unless File.exist?(path)
puts "Directory #{path} does not exist."
exit 1
end
vendor_deps(path, options) if File.directory?(path)
o = options.dup
configure_logger(o)
profile = Inspec::Profile.for_target(path, o)
error_count = 0
error = lambda { |msg|
error_count += 1
puts msg
}
result = profile.check
unless result[:summary][:valid]
error.call('Profile check failed. Please fix the profile before upload.')
else
puts('Profile is valid')
end
if (config['token'].nil? && config['refresh_token'].nil?) || config['user'].nil?
error.call('Please login via `inspec compliance login`')
end
profile_name = profile.params[:name]
if Compliance::API.exist?(config, "#{config['owner']}/#{profile_name}") && !options['overwrite']
error.call('Profile exists on the server, use --overwrite')
end
if error_count > 0
puts "Found #{error_count} error(s)"
exit 1
end
if File.directory?(path)
archive_path = Dir::Tmpname.create([profile_name, '.tar.gz']) {}
puts "Generate temporary profile archive at #{archive_path}"
profile.archive({ output: archive_path, ignore_errors: false, overwrite: true })
else
archive_path = path
end
puts "Start upload to #{config['owner']}/#{profile_name}"
pname = ERB::Util.url_encode(profile_name)
Compliance::API.is_automate_server?(config) ? upload_msg = 'Uploading to Chef Automate' : upload_msg = 'Uploading to Chef Compliance'
puts upload_msg
success, msg = Compliance::API.upload(config, config['owner'], pname, archive_path)
if success
puts 'Successfully uploaded profile'
else
puts 'Error during profile upload:'
puts msg
exit 1
end
end
|
#version ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/bundles/inspec-compliance/cli.rb', line 211
def version
config = Compliance::Configuration.new
info = Compliance::API.version(config)
if !info.nil? && info['version']
puts "Name: #{info['api']}"
puts "Version: #{info['version']}"
else
puts 'Could not determine server version.'
exit 1
end
rescue Compliance::ServerConfigurationMissing
puts "\nServer configuration information is missing. Please login using `inspec compliance login`"
exit 1
end
|