This repository was archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontainerAnalysis.test.js
421 lines (374 loc) · 12.4 KB
/
containerAnalysis.test.js
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const {assert} = require('chai');
const {describe, it, before, after} = require('mocha');
const cp = require('child_process');
const {delay} = require('./util');
const uuid = require('uuid');
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const uuidVal = uuid.v4();
const noteId = `test-note-${uuidVal}`;
const resourceUrl = `gcr.io/test-project/test-image-${uuidVal}`;
const subscriptionId = `occurrence-subscription-${uuidVal}`;
const timeoutSeconds = 5;
const retries = 10;
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topicName = 'container-analysis-occurrences-v1';
let topic;
let projectId;
let formattedParent;
let formattedNoteName;
describe.skip('Note tests', () => {
before(async () => {
// define projectId and related vars
projectId = await client.getProjectId();
formattedParent = `projects/${projectId}`;
formattedNoteName = `projects/${projectId}/notes/${noteId}`;
});
after(async () => {
const [allOccurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `resourceUrl = "${resourceUrl}"`,
});
allOccurrences.forEach(async occurrence => {
await client.getGrafeasClient().deleteOccurrence({name: occurrence.name});
console.log(`deleted occurrence ${occurrence.name}`);
});
const [allNotes] = await client.getGrafeasClient().listNotes({
parent: formattedParent,
});
allNotes.forEach(async note => {
await client.getGrafeasClient().deleteNote({name: note.name});
console.log(`deleted note ${note.name}`);
});
});
it('should create a note', () => {
const output = execSync(`node createNote.js "${projectId}" "${noteId}"`);
assert.include(output, `Note ${formattedNoteName} created.`);
});
it('should get note', () => {
const output = execSync(`node getNote.js "${projectId}" "${noteId}"`);
assert.include(output, `Note name: ${formattedNoteName}`);
});
it('should create occurrence', () => {
const output = execSync(
`node createOccurrence.js "${projectId}" "${noteId}" "${projectId}" "${resourceUrl}"`
);
assert.include(output, 'Occurrence created');
});
it('should get occurrence', async () => {
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `resourceUrl = "${resourceUrl}"`,
});
assert(occurrences.length > 0);
const occurrence = occurrences[0];
const occurrenceId = occurrence.name.split('/')[3];
let output;
for (let i = 0; i < retries; i++) {
output = execSync(
`node getOccurrence.js "${projectId}" "${occurrenceId}"`
);
if (output.includes('Occurrence name:')) {
break;
}
}
assert.include(output, `Occurrence name: ${occurrence.name}`);
});
it('should get occurrences for note', () => {
let output;
for (let i = 0; i < retries; i++) {
output = execSync(
`node occurrencesForNote.js "${projectId}" "${noteId}"`
);
if (!output.includes('No occurrences found.')) {
break;
}
}
assert.include(output, 'Occurrences:');
});
it('should get occurrences for image', () => {
const output = execSync(
`node occurrencesForImage.js "${projectId}" "${resourceUrl}"`
);
assert.include(output, `Occurrences for ${resourceUrl}`);
});
it('should get discovery info for image', async () => {
const discoveryNoteRequest = {
parent: formattedParent,
noteId: `${noteId}-discovery`,
note: {
discovery: {
analysisKind: 'DISCOVERY',
},
},
};
await client.getGrafeasClient().createNote(discoveryNoteRequest);
const occurrenceRequest = {
parent: formattedParent,
occurrence: {
noteName: `${formattedNoteName}-discovery`,
resourceUri: resourceUrl,
discovery: {
analysisStatus: 'FINISHED_SUCCESS',
},
},
};
await client.getGrafeasClient().createOccurrence(occurrenceRequest);
const output = execSync(
`node getDiscoveryInfo "${projectId}" "${resourceUrl}"`
);
assert.include(output, `Discovery Occurrences for ${resourceUrl}`);
});
it('should get high severity vulnerabilities for image', async () => {
const criticalNoteReq = {
parent: formattedParent,
noteId: `${noteId}-critical`,
note: {
vulnerability: {
severity: 'CRITICAL',
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersionStart: {
kind: 'MINIMUM',
},
affectedVersionEnd: {
kind: 'MAXIMUM',
},
},
],
},
},
};
await client.getGrafeasClient().createNote(criticalNoteReq);
const criticalOccurrenceReq = {
parent: formattedParent,
occurrence: {
noteName: `${formattedNoteName}-critical`,
resourceUri: resourceUrl,
vulnerability: {
effective_severity: 'CRITICAL',
packageIssue: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
};
await client.getGrafeasClient().createOccurrence(criticalOccurrenceReq);
const output = execSync(
`node highVulnerabilitiesForImage "${projectId}" "${resourceUrl}"`
);
assert.include(output, `High Severity Vulnerabilities for ${resourceUrl}`);
});
it('should get all vulnerabilites for image', () => {
const output = execSync(
`node vulnerabilityOccurrencesForImage "${projectId}" "${resourceUrl}"`
);
assert.include(output, `All Vulnerabilities for ${resourceUrl}`);
});
it('should delete occurrence', async () => {
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `resourceUrl = "${resourceUrl}"`,
});
assert(occurrences.length > 0);
const occurrence = occurrences[0];
const occurrenceId = occurrence.name.split('/')[3];
const output = execSync(
`node deleteOccurrence.js "${projectId}" "${occurrenceId}"`
);
assert.include(output, 'Occurrence deleted:');
});
it('should delete note', () => {
const output = execSync(`node deleteNote.js "${projectId}" "${noteId}" `);
assert.include(output, `Note ${formattedNoteName} deleted.`);
// Sometimes the delete note test is failing with the error:
// Error: 5 NOT_FOUND: note with ID "test-note-${uuid}" for project
// ${projectId} does not exist.
});
});
describe.skip('polling', () => {
before(async () => {
// define project id and related vars
projectId = await client.getProjectId();
formattedParent = `projects/${projectId}`;
formattedNoteName = `projects/${projectId}/notes/${noteId}`;
const discoveryNoteRequest = {
parent: formattedParent,
noteId: `${noteId}-discovery-polling`,
note: {
discovery: {
analysisKind: 'DISCOVERY',
},
},
};
await client.getGrafeasClient().createNote(discoveryNoteRequest);
const occurrenceRequest = {
parent: formattedParent,
occurrence: {
noteName: `${formattedNoteName}-discovery-polling`,
resourceUri: resourceUrl,
discovery: {
analysisStatus: 'FINISHED_SUCCESS',
},
},
};
await client.getGrafeasClient().createOccurrence(occurrenceRequest);
});
after(async () => {
const [discoveryOccurrences] = await client
.getGrafeasClient()
.listNoteOccurrences({
name: `${formattedNoteName}-discovery-polling`,
});
discoveryOccurrences.forEach(async occurrence => {
await client.getGrafeasClient().deleteOccurrence({name: occurrence.name});
});
await client
.getGrafeasClient()
.deleteNote({name: `${formattedNoteName}-discovery-polling`});
});
it('should successfully poll latest discovery occurrence', () => {
const output = execSync(
`node pollDiscoveryOccurrenceFinished.js "${projectId}" "${resourceUrl}" "${timeoutSeconds}"`
);
assert.include(output, 'Found discovery occurrence');
});
});
describe.skip('pubsub', () => {
before(async () => {
// define project id and related vars
projectId = await client.getProjectId();
formattedParent = `projects/${projectId}`;
formattedNoteName = `projects/${projectId}/notes/${noteId}`;
topic = pubsub.topic(topicName);
});
describe('occurrences from pubsub subscription', () => {
it('should get count of occurrences from pubsub topic', async function () {
this.retries(3);
await delay(this.test);
try {
// attempt to create topic if missing
await pubsub.createTopic(topicName);
} catch (err) {
console.log(`topic creation failed: ${topicName} ${err.message}`);
if (!err.details.includes('Resource already exists')) {
throw err;
}
}
try {
await topic.createSubscription(subscriptionId);
} catch (err) {
console.log(
`subscription creation failed: ${subscriptionId} ${err.message}`
);
if (!err.details.includes('Resource already exists')) {
throw err;
}
}
const pubSubNoteReq = {
parent: formattedParent,
noteId: `${noteId}-pubsub`,
note: {
vulnerability: {
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersionStart: {
kind: 'MINIMUM',
},
affectedVersionEnd: {
kind: 'MAXIMUM',
},
},
],
},
},
};
await client.getGrafeasClient().createNote(pubSubNoteReq);
const occurrenceCount = 3;
const pubSubOccurrenceReq = {
parent: formattedParent,
occurrence: {
noteName: `${formattedNoteName}-pubsub`,
resourceUri: resourceUrl,
vulnerability: {
packageIssue: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
};
// empty subscription
execSync(
`node occurrencePubSub.js "${projectId}" "${subscriptionId}" "${timeoutSeconds}"`
);
// create test occurrences
for (let i = 0; i < occurrenceCount; i++) {
const [pubSubOccurrence] = await client
.getGrafeasClient()
.createOccurrence(pubSubOccurrenceReq);
await client
.getGrafeasClient()
.deleteOccurrence({name: pubSubOccurrence.name});
}
const output = execSync(
`node occurrencePubSub.js "${projectId}" "${subscriptionId}" "${timeoutSeconds}"`
);
// ensure that our occcurences were enqueued:
assert.match(output, /Polled [1-9]+ occurrences/);
});
it('should delete the pubsub subscription', async function () {
this.retries(3);
await delay(this.test);
try {
await client
.getGrafeasClient()
.deleteNote({name: `${formattedNoteName}-pubsub`});
} catch (err) {
assert.fail(err);
}
try {
await pubsub.subscription(subscriptionId).delete();
} catch (err) {
assert.fail(err);
}
});
});
});