-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathhelpers.test.js
More file actions
258 lines (232 loc) · 7.97 KB
/
helpers.test.js
File metadata and controls
258 lines (232 loc) · 7.97 KB
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
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
const { scan, cascadingScan } = require("./helpers");
jest.setTimeout(10 * 1000);
describe("Kubernetes interaction tests", () => {
const mockK8sCRDApi = {
createNamespacedCustomObject: jest.fn(),
getNamespacedCustomObjectStatus: jest.fn(),
deleteNamespacedCustomObject: jest.fn(),
listNamespacedCustomObject: jest.fn(),
};
const mockK8sBatchApi = {
createNamespacedJob: jest.fn(),
deleteNamespacedJob: jest.fn(),
listNamespacedJob: jest.fn(),
};
const mockPodsApi = {
listNamespacedPod: jest.fn(),
readNamespacedPodLog: jest.fn(),
};
beforeEach(() => {
jest.clearAllMocks();
});
describe("scan function", () => {
it("should create a scan and return findings on successful completion", async () => {
const mockScanCreationResponse = require("./__testFiles__/mockScanCreationResponse.json");
const mockScanStatusResponse = require("./__testFiles__/mockScanStatusResponse.json");
mockK8sCRDApi.createNamespacedCustomObject.mockResolvedValue(
mockScanCreationResponse,
);
mockK8sCRDApi.getNamespacedCustomObjectStatus.mockResolvedValue(
mockScanStatusResponse,
);
const k8sApi = {
k8sCRDApi: mockK8sCRDApi,
k8sBatchApi: mockK8sBatchApi,
k8sPodsApi: mockPodsApi,
};
const findings = await scan(
"nmap-example",
"nmap",
[],
180,
[],
[],
[],
k8sApi,
);
expect(findings).toMatchInlineSnapshot(`
{
"categories": {
"Vulnerability": 24,
},
"count": 24,
"severities": {
"high": 24,
},
}
`);
expect(mockK8sCRDApi.createNamespacedCustomObject.mock.calls)
.toMatchInlineSnapshot(`
[
[
{
"body": {
"apiVersion": "execution.securecodebox.io/v1",
"kind": "Scan",
"metadata": {
"generateName": "nmap-example-",
},
"spec": {
"initContainers": [],
"parameters": [],
"scanType": "nmap",
"volumeMounts": [],
"volumes": [],
},
},
"group": "execution.securecodebox.io",
"namespace": "integration-tests",
"plural": "scans",
"version": "v1",
},
],
]
`);
expect(mockK8sCRDApi.getNamespacedCustomObjectStatus.mock.calls)
.toMatchInlineSnapshot(`
[
[
{
"group": "execution.securecodebox.io",
"name": "nmap-example-pw8vt",
"namespace": "integration-tests",
"plural": "scans",
"version": "v1",
},
],
[
{
"group": "execution.securecodebox.io",
"name": "nmap-example-pw8vt",
"namespace": "integration-tests",
"plural": "scans",
"version": "v1",
},
],
]
`);
});
it("should throw an error if the scan fails", async () => {
const mockScanCreationResponse = require("./__testFiles__/mockScanCreationResponse.json");
const mockScanStatusResponse_Errored = require("./__testFiles__/mockScanStatusResponse_Errored.json");
const mockListNamespacedJobResponse = require("./__testFiles__/mockListNamespacedJobResponse.json");
const mockListNamespacedPodResponse = require("./__testFiles__/mockListNamespacedPodResponse.json");
const mockReadNamespacedPodLogResponse = require("./__testFiles__/mockReadNamespacedPodLogResponse.json");
mockK8sCRDApi.createNamespacedCustomObject.mockResolvedValue(
mockScanCreationResponse,
);
mockK8sCRDApi.getNamespacedCustomObjectStatus.mockResolvedValue(
mockScanStatusResponse_Errored,
);
mockK8sBatchApi.listNamespacedJob.mockResolvedValue(
mockListNamespacedJobResponse,
);
mockPodsApi.listNamespacedPod.mockResolvedValue(
mockListNamespacedPodResponse,
);
mockPodsApi.readNamespacedPodLog.mockResolvedValue(
mockReadNamespacedPodLogResponse,
);
const k8sApi = {
k8sCRDApi: mockK8sCRDApi,
k8sBatchApi: mockK8sBatchApi,
k8sPodsApi: mockPodsApi,
};
return expect(
scan("nmap-example", "nmap", [], 180, [], [], [], k8sApi),
).rejects.toThrow('Scan failed with description "Mocked Error"');
});
});
describe("cascading scan function", () => {
it("should create a cascading scan and return findings on successful completion", async () => {
const mockScanCreationResponse = require("./__testFiles__/mockCascadingScanCreationResponse.json");
const mockScanStatusResponse = require("./__testFiles__/mockCascadingScanStatusResponse.json");
const mockListNamespacedCustomObjectResponse = require("./__testFiles__/mockCascadingListNamespacedCustomObject.json");
mockK8sCRDApi.createNamespacedCustomObject.mockResolvedValue(
mockScanCreationResponse,
);
mockK8sCRDApi.getNamespacedCustomObjectStatus.mockResolvedValue(
mockScanStatusResponse,
);
mockK8sCRDApi.listNamespacedCustomObject.mockResolvedValue(
mockListNamespacedCustomObjectResponse,
);
const k8sApi = {
k8sCRDApi: mockK8sCRDApi,
k8sBatchApi: mockK8sBatchApi,
k8sPodsApi: mockPodsApi,
};
const findings = await cascadingScan(
"nmap-dummy-ssh",
"nmap",
["-Pn", "-sV", "dummy-ssh.demo-targets.svc"],
{
nameCascade: "ncrack-ssh",
matchLabels: {
"securecodebox.io/invasive": "invasive",
"securecodebox.io/intensive": "high",
},
},
180,
k8sApi,
);
expect(findings).toMatchInlineSnapshot(`
{
"categories": {
"Discovered Credentials": 1,
},
"count": 1,
"severities": {
"high": 1,
},
}
`);
});
it("should throw an error if the scan fails", async () => {
const mockScanCreationResponse = require("./__testFiles__/mockCascadingScanCreationResponse.json");
const mockScanStatusResponse_Errored = require("./__testFiles__/mockCascadingScanStatusResponse_Errored.json");
const mockListNamespacedJobResponse = require("./__testFiles__/mockListNamespacedJobResponse.json");
const mockListNamespacedPodResponse = require("./__testFiles__/mockListNamespacedPodResponse.json");
const mockReadNamespacedPodLogResponse = require("./__testFiles__/mockReadNamespacedPodLogResponse.json");
mockK8sCRDApi.createNamespacedCustomObject.mockResolvedValue(
mockScanCreationResponse,
);
mockK8sCRDApi.getNamespacedCustomObjectStatus.mockResolvedValue(
mockScanStatusResponse_Errored,
);
mockK8sBatchApi.listNamespacedJob.mockResolvedValue(
mockListNamespacedJobResponse,
);
mockPodsApi.listNamespacedPod.mockResolvedValue(
mockListNamespacedPodResponse,
);
mockPodsApi.readNamespacedPodLog.mockResolvedValue(
mockReadNamespacedPodLogResponse,
);
const k8sApi = {
k8sCRDApi: mockK8sCRDApi,
k8sBatchApi: mockK8sBatchApi,
k8sPodsApi: mockPodsApi,
};
return expect(
cascadingScan(
"nmap-dummy-ssh",
"nmap",
["-Pn", "-sV", "dummy-ssh.demo-targets.svc"],
{
nameCascade: "ncrack-ssh",
matchLabels: {
"securecodebox.io/invasive": "invasive",
"securecodebox.io/intensive": "high",
},
},
180,
k8sApi,
),
).rejects.toThrow('Initial Scan failed with description "Mocked Error"');
});
});
});