-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathjob_spec.rb
376 lines (324 loc) · 13.6 KB
/
job_spec.rb
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
require 'MiqContainerGroup/MiqContainerGroup'
class MockKubeClient
include ArrayRecursiveOpenStruct
def create_pod(*_args)
nil
end
def proxy_url(*_args)
'https://test.com'
end
def headers(*_args)
[]
end
def get_pod(*_args)
array_recursive_ostruct(
:metadata => {
:annotations => {
'manageiq.org/jobid' => '5'
}
},
:status => {
:containerStatuses => [
{ :ready => true },
]
}
)
end
def get_service_account(*_args)
array_recursive_ostruct(
:metadata => {
:name => 'inspector-admin'
},
:imagePullSecrets => [
{ :name => 'inspector-admin-dockercfg-blabla' }
]
)
end
def ssl_options(*_args)
{}
end
def auth_options(*_args)
{}
end
end
class MockImageInspectorClient
def initialize(for_id, repo_digest = nil)
@for_id = for_id
@repo_digest = repo_digest
end
def fetch_metadata(*_args)
if @repo_digest
OpenStruct.new('Id' => @for_id, 'RepoDigests' => ["123456677899987765543322", @repo_digest])
else
OpenStruct.new('Id' => @for_id)
end
end
def fetch_oscap_arf
File.read(
File.expand_path(File.join(File.dirname(__FILE__), "ssg-fedora-ds-arf.xml"))
).encode("UTF-8")
end
end
describe ManageIQ::Providers::Kubernetes::ContainerManager::Scanning::Job do
context "SmartState Analysis Methods" do
before(:each) do
EvmSpecHelper.create_guid_miq_server_zone
@ems = FactoryGirl.create(:ems_kubernetes, :hostname => 'hostname')
end
context "#initialize" do
it "Creates a new scan job" do
image = FactoryGirl.create(:container_image, :ext_management_system => @ems)
job = @ems.raw_scan_job_create(image.class, image.id, "bob")
expect(job).to have_attributes(
:dispatch_status => "pending",
:state => "waiting_to_start",
:status => "ok",
:message => "process initiated",
:target_class => "ContainerImage",
:userid => "bob"
)
end
it "Is backward compatible with #13722" do
# https://github.com/ManageIQ/manageiq/pull/13722
image = FactoryGirl.create(:container_image, :ext_management_system => @ems)
User.current_user = FactoryGirl.create(:user, :userid => "bob")
job = @ems.raw_scan_job_create(image)
expect(job).to have_attributes(
:dispatch_status => "pending",
:state => "waiting_to_start",
:status => "ok",
:message => "process initiated",
:target_class => "ContainerImage",
:userid => "bob"
)
end
it "Is backward compatible with #54" do
# https://github.com/ManageIQ/manageiq-providers-kubernetes/pull/54/files
image = FactoryGirl.create(:container_image, :ext_management_system => @ems)
User.current_user = FactoryGirl.create(:user, :userid => "bob")
job = @ems.raw_scan_job_create(image.class, image.id)
expect(job).to have_attributes(
:dispatch_status => "pending",
:state => "waiting_to_start",
:status => "ok",
:message => "process initiated",
:target_class => "ContainerImage",
:userid => "bob"
)
end
end
end
context "A single Container Scan Job," do
IMAGE_ID = '3629a651e6c11d7435937bdf41da11cf87863c03f2587fa788cf5cbfe8a11b9a'.freeze
IMAGE_NAME = 'test'.freeze
before(:each) do
@server = EvmSpecHelper.local_miq_server
allow_any_instance_of(described_class).to receive_messages(:kubernetes_client => MockKubeClient.new)
allow_any_instance_of(described_class).to receive_messages(
:image_inspector_client => MockImageInspectorClient.new(IMAGE_ID))
@ems = FactoryGirl.create(
:ems_kubernetes, :hostname => "test.com", :zone => @server.zone, :port => 8443,
:authentications => [AuthToken.new(:name => "test", :type => 'AuthToken', :auth_key => "a secret")]
)
@image = FactoryGirl.create(
:container_image, :ext_management_system => @ems, :name => IMAGE_NAME,
:image_ref => "docker://#{IMAGE_ID}"
)
allow_any_instance_of(@image.class).to receive(:scan_metadata) do |_instance, _args|
@job.signal(:data, '<summary><scanmetadata></scanmetadata></summary>')
end
allow_any_instance_of(@image.class).to receive(:sync_metadata) do |_instance, _args|
@job.signal(:data, '<summary><syncmetadata></syncmetadata></summary>')
end
User.current_user = FactoryGirl.create(:user)
@job = @ems.raw_scan_job_create(@image.class, @image.id)
allow(MiqQueue).to receive(:put_unless_exists) do |args|
@job.signal(*args[:args])
end
end
context "completes successfully" do
before(:each) do
allow_any_instance_of(described_class).to receive_messages(:collect_compliance_data) unless OpenscapResult.openscap_available?
expect(@job.state).to eq 'waiting_to_start'
@job.signal(:start)
end
it 'should report success' do
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'ok'
end
it 'should persist openscap data' do
skip unless OpenscapResult.openscap_available?
expect(@image.openscap_result).to be
expect(@image.openscap_result.binary_blob.md5).to eq('d1f1857281573cd777b31d76e8529dc9')
expect(@image.openscap_result.openscap_rule_results.count).to eq(213)
end
end
context "#current_job_timeout" do
it "checks for timeout in Settings" do
stub_settings_merge(:container_scanning => {:scanning_job_timeout => '15.minutes'})
expect(@job.send(:current_job_timeout)).to eq(900)
stub_settings_merge(:container_scanning => {:scanning_job_timeout => 600})
expect(@job.send(:current_job_timeout)).to eq(600)
end
end
context "using provider options" do
def create_pod_definition
allow_any_instance_of(described_class).to receive_messages(:kubernetes_client => MockKubeClient.new)
kc = @job.kubernetes_client
secret_name = kc.get_service_account[:imagePullSecrets][0][:name]
@job.send(:pod_definition, secret_name)
end
it 'should add correct environment variables from options' do
att_name = 'http_proxy'
my_value = "MY_TEST_VALUE"
@ems.update(:options => { :image_inspector_options => {att_name.to_sym => my_value} })
pod = create_pod_definition
expect(pod[:spec][:containers][0][:env][0][:name]).to eq(att_name.upcase)
expect(pod[:spec][:containers][0][:env][0][:value]).to eq(my_value)
end
it 'should send cve_url from options' do
cve_url_value = "get_cve_from_here.com"
@ems.update(:options => { :image_inspector_options => {:cve_url => cve_url_value} })
pod = create_pod_definition
expect(pod[:spec][:containers][0][:command]
.select { |cmd| cmd.starts_with?("--cve-url=") }.first.split('=').last).to eq(cve_url_value)
end
end
it 'should send correct dockercfg secrets' do
allow_any_instance_of(described_class).to receive_messages(:kubernetes_client => MockKubeClient.new)
kc = @job.kubernetes_client
secret_name = kc.get_service_account[:imagePullSecrets][0][:name]
pod = @job.send(:pod_definition, secret_name)
expect(pod[:spec][:containers][0][:command]).to include(
"--dockercfg=" + described_class::INSPECTOR_ADMIN_SECRET_PATH + secret_name + "/.dockercfg")
expect(pod[:spec][:containers][0][:volumeMounts]).to include(
Kubeclient::Resource.new(
:name => "inspector-admin-secret",
:mountPath => described_class::INSPECTOR_ADMIN_SECRET_PATH + secret_name,
:readOnly => true))
expect(pod[:spec][:volumes]).to include(
Kubeclient::Resource.new(
:name => "inspector-admin-secret",
:secret => {:secretName => secret_name}))
end
context 'when the job is called with a non existing image' do
before(:each) do
@image.delete
end
it 'should report the error' do
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'error'
expect(@job.message).to eq "no image found"
end
end
context 'when create pod throws exception' do
before(:each) do
allow_any_instance_of(MockKubeClient).to receive(:create_pod) do |_instance, *_args|
raise KubeException.new(0, 'error', nil)
end
end
it 'should report the error' do
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'error'
expect(@job.message).to eq "pod creation for [management-infra/manageiq-img-scan-#{@job.guid[0..4]}] failed"
end
end
context 'when getting the service account throws exception' do
before(:each) do
allow_any_instance_of(MockKubeClient).to receive(:get_service_account) do |_instance, *_args|
raise KubeException.new(0, 'error', nil)
end
end
it 'should report the error' do
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'error'
expect(@job.message).to eq "getting inspector-admin secret failed"
end
end
context 'when given a non docker image' do
before(:each) do
allow_any_instance_of(@image.class).to receive(:image_ref) do
'rocket://3629a651e6c11d7435937bdf41da11cf87863c03f2587fa788cf5cbfe8a11b9a'
end
end
it 'should fail' do
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'error'
expect(@job.message).to eq "cannot analyze non docker images"
end
end
context 'when the image tag points to a different image' do
MODIFIED_IMAGE_ID = '0d071bb732e1e3eb1e01629600c9b6c23f2b26b863b5321335f564c8f018c452'.freeze
before(:each) do
allow_any_instance_of(described_class).to receive_messages(
:image_inspector_client => MockImageInspectorClient.new(MODIFIED_IMAGE_ID))
end
it 'should check for repo_digests' do
allow_any_instance_of(described_class).to receive_messages(:collect_compliance_data) unless OpenscapResult.openscap_available?
allow_any_instance_of(described_class).to receive_messages(
:image_inspector_client => MockImageInspectorClient.new(MODIFIED_IMAGE_ID, IMAGE_ID))
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'ok'
end
it 'should report the error' do
@job.signal(:start)
expect(@job.state).to eq 'finished'
expect(@job.status).to eq 'error'
expect(@job.message).to eq "cannot analyze image #{IMAGE_NAME} with id #{IMAGE_ID[0..11]}:"\
" detected ids were #{MODIFIED_IMAGE_ID[0..11]}"
end
end
context '#verify_scanned_image_id' do
DOCKER_DAEMON_IMAGE_ID = '123456'.freeze
before(:each) do
@job.options[:docker_image_id] = IMAGE_ID
@job.options[:image_full_name] = IMAGE_NAME
end
it 'should report the error when the scanned Id is different than the Image Id' do
msg = @job.verify_scanned_image_id(OpenStruct.new(:Id => DOCKER_DAEMON_IMAGE_ID))
expect(msg).to eq "cannot analyze image #{IMAGE_NAME} with id #{IMAGE_ID[0..11]}:"\
" detected ids were #{DOCKER_DAEMON_IMAGE_ID[0..11]}"
end
context 'checking RepoDigests' do
DOCKER_IMAGE_ID = "image_name@sha256:digest654321abcdef".freeze
OTHER_REPOD = "OTHER_REPOD".freeze
before(:each) do
@job.options[:docker_image_id] = DOCKER_IMAGE_ID
@job.options[:image_full_name] = "docker-pullable://" + DOCKER_IMAGE_ID
end
it 'checks that the Id is in RepoDigests' do
msg = @job.verify_scanned_image_id(OpenStruct.new(:Id => DOCKER_DAEMON_IMAGE_ID,
:RepoDigests => [DOCKER_IMAGE_ID],
))
expect(msg).to eq nil
end
it 'checks all the RepoDigests' do
msg = @job.verify_scanned_image_id(OpenStruct.new(:Id => DOCKER_DAEMON_IMAGE_ID,
:RepoDigests => [OTHER_REPOD, DOCKER_IMAGE_ID],
))
expect(msg).to eq nil
end
it 'compares RepoDigests hash part only' do
# in case the image didn't have a defined registry
msg = @job.verify_scanned_image_id(OpenStruct.new(:Id => DOCKER_DAEMON_IMAGE_ID,
:RepoDigests => ["reponame/" + DOCKER_IMAGE_ID],
))
expect(msg).to eq nil
end
it 'reports all attempted IDs' do
# in case the image didn't have a defined registry
msg = @job.verify_scanned_image_id(OpenStruct.new(:Id => DOCKER_DAEMON_IMAGE_ID,
:RepoDigests => [OTHER_REPOD],
))
expect(msg).to eq "cannot analyze image docker-pullable://#{DOCKER_IMAGE_ID} with id #{DOCKER_IMAGE_ID[0..11]}:"\
" detected ids were #{DOCKER_DAEMON_IMAGE_ID[0..11]}, #{OTHER_REPOD}"
end
end
end
end
end