-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSysdigPostureFetchComponent.tsx
More file actions
397 lines (352 loc) · 11.8 KB
/
Copy pathSysdigPostureFetchComponent.tsx
File metadata and controls
397 lines (352 loc) · 11.8 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
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
/*
* Copyright 2023 The Backstage Authors
*
* 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
*
* http://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.
*/
import { useMemo } from 'react';
import { Table, TableColumn, Progress } from '@backstage/core-components';
import useAsync from 'react-use/lib/useAsync';
import Alert from '@mui/material/Alert';
import { useEntity, MissingAnnotationEmptyState } from '@backstage/plugin-catalog-react';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { sysdigApiRef } from '../../api';
import {
// annotations
SYSDIG_EXTERNALDNS_ANNOTATION,
SYSDIG_DISTRIBUTION_ANNOTATION,
SYSDIG_LABELS_ANNOTATION,
SYSDIG_RESOURCE_NAME_ANNOTATION,
SYSDIG_RESOURCE_ORIGIN_ANNOTATION,
SYSDIG_RESOURCE_TYPE_ANNOTATION,
SYSDIG_NODE_TYPE_ANNOTATION,
SYSDIG_OS_NAME_ANNOTATION,
SYSDIG_OS_IMAGE_ANNOTATION,
SYSDIG_PLATFORM_ANNOTATION,
SYSDIG_ACCOUNT_ANNOTATION,
SYSDIG_ORGANIZATION_ANNOTATION,
SYSDIG_PROJECT_ANNOTATION,
SYSDIG_REGION_ANNOTATION,
SYSDIG_AZURE_SUBSCRIPTION_ANNOTATION,
SYSDIG_VERSION_ANNOTATION,
SYSDIG_ZONE_ANNOTATION,
SYSDIG_CATEGORY_ANNOTATION,
SYSDIG_INTEGRATION_NAME_ANNOTATION,
SYSDIG_LOCATION_ANNOTATION,
SYSDIG_REPOSITORY_ANNOTATION,
SYSDIG_SOURCE_TYPE_ANNOTATION,
SYSDIG_CLUSTER_NAME_ANNOTATION,
SYSDIG_NAMESPACE_ANNOTATION,
SYSDIG_CUSTOM_FILTER_ANNOTATION,
// methods
getFailed,
getPassed,
getDetails,
getGauge,
getScope,
getResourceName,
getTitleWithBacklink,
getBacklink
} from '../../lib'
type PostureScan = {
configuration: string,
id: string,
keyValueConfigs: [
{
groupName: string,
values: {
key: string,
value: string
}
}
],
labels: [
string
],
lastSeen: string,
metadata: {
Account: string,
Organization: string,
Region: string
},
name: string,
platform: string,
postureControlSummary: [
{
acceptedControls: number,
failedControls: number,
name: string,
policyId: string
}
],
posturePolicySummary: {
passPercentage: number,
policies: [
{
id: string,
name: string,
pass: boolean
}
]
},
resourceOrigin: string,
type: string,
zones: [
{
id: string,
name: string
}
]
};
type DenseTableProps = {
postureScans: PostureScan[];
title: JSX.Element;
};
// Example image response from Sysdig scanning API
/*
"data": [
{
"configuration": "metadata: annotations: meta.helm.sh/release-name: iac-collector meta.helm.sh/release-namespace: default labels: app.kubernetes.io/managed-by: Helm skaffold.dev/run-id: 57984887-4121-4d34-aa82-c8291300205f name: secure-iac-collector-sa namespace: sysdig",
"id": "62e348b71acd7be14a4bdfcc",
"keyValueConfigs": [
{
"groupName": "Kubernetes",
"values": {
"key": "Kubelet --anonymous-auth",
"value": "false"
}
}
],
"labels": [
"app.kubernetes.io/managed-by:Helm"
],
"lastSeen": "1660742138",
"metadata": {
"Account": "746213592136",
"Organization": "o-k53g78fd13e9",
"Region": "us-east-1"
},
"name": "aws-bucket1",
"platform": "AWS",
"postureControlSummary": [
{
"acceptedControls": 3,
"failedControls": 5,
"name": "CIS Amazon Web Services Foundations Benchmark",
"policyId": "15"
}
],
"posturePolicySummary": {
"passPercentage": 50,
"policies": [
{
"id": "15",
"name": "CIS Amazon Web Services Foundations Benchmark",
"pass": false
}
]
},
"resourceOrigin": "Deployed",
"type": "AWS_S3_BUCKET_ACL",
"zones": [
{
"id": "1",
"name": "Entire Infrastructure"
}
]
},
...
*/
export const DenseTable = ({ postureScans, title }: DenseTableProps) => {
const columns: TableColumn[] = [
{ title: 'Name', field: 'name', width: "10%" },
{ title: 'Scope', field: 'scope', width: "10%" },
{ title: 'Score', field: 'score', width: "10%" },
{ title: 'Failed Policies', field: 'failed', width: "25%" },
{ title: 'Passed Policies', field: 'passed', width: "25%" },
{ title: 'Details', field: 'details', width: "2%" }
];
const data = postureScans.filter(scan => { return scan.posturePolicySummary !== null })
.flatMap(scan => {
return {
name: getResourceName(scan.name, scan.type, scan.platform, scan.resourceOrigin),
scope: getScope(scan.metadata),
score: getGauge(scan.posturePolicySummary.passPercentage),
failed: getFailed(scan.posturePolicySummary.policies),
passed: getPassed(scan.posturePolicySummary.policies),
details: getDetails(scan)
};
});
return (
<Table
title={title}
options={{ search: true, paging: false }}
// sortby
columns={columns}
data={data}
/>
);
};
export const SysdigPostureFetchComponent = () => {
const { entity } = useEntity();
const backendUrl = useApi(configApiRef).getString('backend.baseUrl');
const sysdigApiClient = useApi(sysdigApiRef);
const endpoint: string | undefined = useApi(configApiRef).getOptionalString("sysdig.endpoint");
const backlink_config: string | undefined = useApi(configApiRef).getOptionalString("sysdig.backlink");
const annotations = entity.metadata.annotations;
const { filter, backlink, hasSysdigAnnotations } = useMemo(() => {
let currentFilter = '?filter=';
let currentBacklink = getBacklink(endpoint, backlink_config, "inventory");
let name: string | undefined;
let hasAnnotations = false;
if (annotations) {
if (SYSDIG_CUSTOM_FILTER_ANNOTATION in annotations) {
currentFilter += annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION];
hasAnnotations = true;
} else {
const filters: string[] = [];
if (SYSDIG_EXTERNALDNS_ANNOTATION in annotations) {
name = annotations[SYSDIG_EXTERNALDNS_ANNOTATION];
filters.push(`externalDNS="${name}"`);
}
if (SYSDIG_DISTRIBUTION_ANNOTATION in annotations) {
name = annotations[SYSDIG_DISTRIBUTION_ANNOTATION];
filters.push(`distribution="${name}"`);
}
if (SYSDIG_LABELS_ANNOTATION in annotations) {
name = annotations[SYSDIG_LABELS_ANNOTATION];
filters.push(`labels="${name}"`);
}
if (SYSDIG_RESOURCE_NAME_ANNOTATION in annotations) {
name = annotations[SYSDIG_RESOURCE_NAME_ANNOTATION];
filters.push(`name="${name}"`);
}
if (SYSDIG_RESOURCE_ORIGIN_ANNOTATION in annotations) {
name = annotations[SYSDIG_RESOURCE_ORIGIN_ANNOTATION];
filters.push(`resourceOrigin="${name}"`);
}
if (SYSDIG_RESOURCE_TYPE_ANNOTATION in annotations) {
name = annotations[SYSDIG_RESOURCE_TYPE_ANNOTATION];
filters.push(`type="${name}"`);
}
if (SYSDIG_NODE_TYPE_ANNOTATION in annotations) {
name = annotations[SYSDIG_NODE_TYPE_ANNOTATION];
filters.push(`nodeType="${name}"`);
}
if (SYSDIG_OS_NAME_ANNOTATION in annotations) {
name = annotations[SYSDIG_OS_NAME_ANNOTATION];
filters.push(`osName="${name}"`);
}
if (SYSDIG_OS_IMAGE_ANNOTATION in annotations) {
name = annotations[SYSDIG_OS_IMAGE_ANNOTATION];
filters.push(`osImage="${name}"`);
}
if (SYSDIG_PLATFORM_ANNOTATION in annotations) {
name = annotations[SYSDIG_PLATFORM_ANNOTATION];
filters.push(`platform="${name}"`);
}
if (SYSDIG_ACCOUNT_ANNOTATION in annotations) {
name = annotations[SYSDIG_ACCOUNT_ANNOTATION];
filters.push(`account="${name}"`);
}
if (SYSDIG_ORGANIZATION_ANNOTATION in annotations) {
name = annotations[SYSDIG_ORGANIZATION_ANNOTATION];
filters.push(`organization="${name}"`);
}
if (SYSDIG_PROJECT_ANNOTATION in annotations) {
name = annotations[SYSDIG_PROJECT_ANNOTATION];
filters.push(`project="${name}"`);
}
if (SYSDIG_REGION_ANNOTATION in annotations) {
name = annotations[SYSDIG_REGION_ANNOTATION];
filters.push(`region="${name}"`);
}
if (SYSDIG_AZURE_SUBSCRIPTION_ANNOTATION in annotations) {
name = annotations[SYSDIG_AZURE_SUBSCRIPTION_ANNOTATION];
filters.push(`subscription="${name}"`);
}
if (SYSDIG_VERSION_ANNOTATION in annotations) {
name = annotations[SYSDIG_VERSION_ANNOTATION];
filters.push(`version="${name}"`);
}
if (SYSDIG_ZONE_ANNOTATION in annotations) {
name = annotations[SYSDIG_ZONE_ANNOTATION];
filters.push(`zone="${name}"`);
}
if (SYSDIG_CATEGORY_ANNOTATION in annotations) {
name = annotations[SYSDIG_CATEGORY_ANNOTATION];
filters.push(`category="${name}"`);
}
if (SYSDIG_INTEGRATION_NAME_ANNOTATION in annotations) {
name = annotations[SYSDIG_INTEGRATION_NAME_ANNOTATION];
filters.push(`integrationName="${name}"`);
}
if (SYSDIG_LOCATION_ANNOTATION in annotations) {
name = annotations[SYSDIG_LOCATION_ANNOTATION];
filters.push(`location="${name}"`);
}
if (SYSDIG_REPOSITORY_ANNOTATION in annotations) {
name = annotations[SYSDIG_REPOSITORY_ANNOTATION];
filters.push(`repository="${name}"`);
}
if (SYSDIG_SOURCE_TYPE_ANNOTATION in annotations) {
name = annotations[SYSDIG_SOURCE_TYPE_ANNOTATION];
filters.push(`sourceType="${name}"`);
}
if (SYSDIG_CLUSTER_NAME_ANNOTATION in annotations) {
name = annotations[SYSDIG_CLUSTER_NAME_ANNOTATION];
filters.push(`cluster="${name}"`);
}
if (SYSDIG_NAMESPACE_ANNOTATION in annotations) {
name = annotations[SYSDIG_NAMESPACE_ANNOTATION];
filters.push(`namespace="${name}"`);
}
if (filters.length === 0) {
return { filter: '', backlink: '', hasSysdigAnnotations: false }; // No Sysdig annotations
}
hasAnnotations = true;
currentFilter += filters.join(' and ');
currentBacklink += currentFilter;
}
}
return { filter: currentFilter, backlink: currentBacklink, hasSysdigAnnotations: hasAnnotations };
}, [annotations, endpoint, backlink_config]);
const { value, loading, error } = useAsync(async (): Promise<PostureScan[]> => {
if (!hasSysdigAnnotations) {
return []; // No Sysdig annotations, so no data to fetch
}
const data = await sysdigApiClient.fetchInventory(filter);
return data.data;
}, [sysdigApiClient, filter, hasSysdigAnnotations]);
if (!hasSysdigAnnotations) {
return (
<MissingAnnotationEmptyState
annotation={[
SYSDIG_RESOURCE_NAME_ANNOTATION,
SYSDIG_RESOURCE_TYPE_ANNOTATION,
SYSDIG_PLATFORM_ANNOTATION,
SYSDIG_ACCOUNT_ANNOTATION,
SYSDIG_REGION_ANNOTATION,
SYSDIG_CUSTOM_FILTER_ANNOTATION
]}
readMoreUrl="https://github.com/sysdiglabs/backstage-plugin-sysdig#how-to-annotate-services"
/>
);
}
if (loading) {
return <Progress />;
}
if (error) {
return <Alert severity="error">{error.message}</Alert>;
}
return <DenseTable postureScans={value || []} title={getTitleWithBacklink("Posture Overview", backlink) || []} />;
};