forked from devsecopsmaturitymodel/DevSecOps-MaturityModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity-store.ts
More file actions
392 lines (343 loc) · 12.6 KB
/
activity-store.ts
File metadata and controls
392 lines (343 loc) · 12.6 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
import { appendHashElement } from '../util/ArrayHash';
import { IgnoreList } from './ignore-list';
import { MarkdownText } from './markdown-text';
export class ActivityFileMeta {
static DSOMM_PUBLISHER: string =
'https://github.com/devsecopsmaturitymodel/DevSecOps-MaturityModel-data';
version: string | null = null;
released: Date | null = null;
publisher: string | null = null;
constructor(meta: any) {
if (meta) {
this.version = meta.version || null;
this.released = meta.released ? new Date(meta.released) : null;
this.publisher = meta.publisher || null;
}
}
getDsommVersion(): string | null {
if (this.publisher && this.publisher.startsWith(ActivityFileMeta.DSOMM_PUBLISHER)) {
return this.version?.startsWith('v') ? this.version : `v${this.version}`;
}
return null;
}
getDsommReleaseDate(): Date | null {
if (this.publisher && this.publisher.startsWith(ActivityFileMeta.DSOMM_PUBLISHER)) {
return this.released;
}
return null;
}
}
export interface ActivityFile {
meta: ActivityFileMeta | null;
data: Data;
}
export type Data = Record<string, Category>;
export type Category = Record<string, Dimension>;
export type Dimension = Record<string, Activity>;
export interface Activity {
ignore: boolean;
uuid: string;
iconName: string;
category: string;
dimension: string;
level: number;
name: string;
description: MarkdownText;
risk: MarkdownText;
measure: MarkdownText;
tags: string[];
implementationGuide: MarkdownText;
difficultyOfImplementation: DifficultyOfImplementation;
usefulness: number;
knowledge: number;
resources: number;
time: number;
dependsOn: string[];
comments: MarkdownText;
implementation: Implementation[];
evidence: MarkdownText;
teamsEvidence: Object;
assessment: MarkdownText;
references: FrameworkReferences;
isImplemented: boolean;
teamsImplemented: Record<string, any>;
}
export interface FrameworkReferences {
iso27001_2017: string[];
iso27001_2022: string[];
samm2: string[];
openCRE: string[];
}
export interface Implementation {
name: string;
tags: string[];
url: string;
description: string;
expanded?: boolean; // Optional property for UI state
}
export interface DifficultyOfImplementation {
knowledge: number;
time: number;
resources: number;
}
const UUID = /([0-9a-f]{6,}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{6,})/i;
export class ActivityStore {
public data: Data = {};
private _activityList: Activity[] = [];
private _activityByName: Record<string, Activity> = {};
private _activityByUuid: Record<string, Activity> = {};
private _categoryNames: string[] = [];
private _allDimensions: Record<string, Activity[]> = {};
private _maxLevel: number = -1;
public getData(): Data {
return this.data;
}
public getAllActivities(): Activity[] {
return this._activityList;
}
public getAllActivitiesUpToLevel(maxLevel: number | null): Activity[] {
/* eslint-disable */
if (maxLevel == null)
return this._activityList;
else
return this._activityList.filter(a => a.level <= maxLevel);
/* eslint-enable */
}
public getAllCategoryNames(): string[] {
return this._categoryNames;
}
public getAllDimensionNames(): string[] {
return Object.keys(this._allDimensions);
}
public getActivity(uuid: string, name: string): Activity {
let activity: Activity = this.getActivityByUuid(uuid);
if (!activity) {
activity = this.getActivityByName(name);
}
return activity;
}
public getActivityByName(name: string): Activity {
return this._activityByName[name];
}
public getActivityByUuid(uuid: string): Activity {
return this._activityByUuid[uuid];
}
public getActivities(dimension: string, level: number): Activity[] {
let activities: Activity[] = this._allDimensions[dimension];
return Object.values(activities).filter(a => a.level == level);
}
public getMaxLevel(): number {
return this._maxLevel;
}
public addActivityFile(yaml: Data, errors: string[]) {
let activityList: Activity[] = [];
let ignoreList: IgnoreList = new IgnoreList();
this.prepareActivities(yaml, activityList, ignoreList);
this._maxLevel = -1;
if (this._activityList.length == 0) {
this._activityList = activityList;
this.buildLookups(activityList, this._activityByName, this._activityByUuid, errors);
this.data = yaml;
} else {
this.removeIgnoredActivities(ignoreList, this._activityList);
this.mergeActivities(activityList, this._activityList, errors);
// Reset lookup tables after merge
this._activityByName = {};
this._activityByUuid = {};
this.buildLookups(this._activityList, this._activityByName, this._activityByUuid, errors);
}
this.replaceDependsOnUUids(this._activityList, this._activityByUuid);
this.buildDataHierarchy(this._activityList);
this.buildDimensionList(this._activityList);
}
buildDimensionList(activityList: Activity[]) {
let categories = new Set<string>();
this._allDimensions = {};
for (let activity of activityList) {
categories.add(activity.category);
appendHashElement(this._allDimensions, activity.dimension, activity);
if (activity.level > this._maxLevel) this._maxLevel = activity.level;
}
this._categoryNames = Array.from(categories.keys());
}
buildDataHierarchy(activityList: Activity[]) {
let data: Data = {};
let categories: Category;
let dimensions: Dimension;
for (let activity of activityList) {
if (!data.hasOwnProperty(activity.category)) {
data[activity.category] = {};
}
categories = data[activity.category];
if (!categories.hasOwnProperty(activity.dimension)) {
categories[activity.dimension] = {};
}
dimensions = categories[activity.dimension];
dimensions[activity.name] = activity;
}
this.data = data;
}
/**
* Prepare activities loaded from a YAML file.
*
* Add category, dimension and activity name to activity object,
* unless ignored, then add it to the ignoreList
*/
prepareActivities(yaml: Data, activityList: Activity[], ignoreList: IgnoreList): void {
for (let categoryName in yaml) {
let category = yaml[categoryName];
for (let dimName in category) {
if (dimName == 'ignore') {
ignoreList.add('category', categoryName);
continue;
}
let dimension: Dimension = category[dimName];
for (let activityName in dimension) {
if (activityName == 'ignore') {
ignoreList.add('dimension', dimName);
continue;
}
let activity: Activity = dimension[activityName];
if (activity.ignore === true) {
if (activity.uuid) {
ignoreList.add('uuid', activity.uuid);
} else {
ignoreList.add('name', activityName);
}
continue;
}
// console.log(` - ${categoryName} -- ${dimName} -- ${activityName}`);
// Initiate markdown strings
activity.description = new MarkdownText(activity.description as unknown as string);
activity.risk = new MarkdownText(activity.risk as unknown as string);
activity.measure = new MarkdownText(activity.measure as unknown as string);
activity.comments = new MarkdownText(activity.comments as unknown as string);
activity.assessment = new MarkdownText(activity.assessment as unknown as string);
activity.evidence = new MarkdownText(activity.evidence as unknown as string);
// Rename properties to match the Activity interface
activity.category = categoryName;
activity.dimension = dimName;
activity.name = activityName;
if (activity.references) {
let references: any = activity.references;
if (references.hasOwnProperty('iso27001-2017')) {
references.iso27001_2017 = references['iso27001-2017'];
delete references['iso27001-2017'];
}
if (references.hasOwnProperty('iso27001-2022')) {
references.iso27001_2022 = references['iso27001-2022'];
delete references['iso27001-2022'];
}
}
activityList.push(activity);
}
}
}
}
removeIgnoredActivities(ignoreList: IgnoreList, activityList: Activity[]) {
if (ignoreList.isEmpty()) return;
let i: number = activityList.length - 1;
// Loop backwards to not tamper with index when removing items
while (i >= 0) {
if (ignoreList.hasActivity(activityList[i])) {
activityList.splice(i, 1); // Remove item from list
}
i--;
}
}
buildLookups(
activityList: Activity[],
activityByName: Record<string, Activity>,
activityByUuid: Record<string, Activity>,
errors: string[]
) {
for (let activity of activityList) {
this.addActivityLookup(activity, activityByName, activityByUuid, errors);
}
}
/**
* Substitute dependsOn UUIDs with activity names
*/
replaceDependsOnUUids(activityList: Activity[], activityByUuid: Record<string, Activity>) {
for (let activity of activityList) {
if (activity.dependsOn && activity.dependsOn.length > 0) {
for (let i = 0; i < activity.dependsOn.length; i++) {
if (activity.dependsOn[i].match(UUID)) {
if (activityByUuid.hasOwnProperty(activity.dependsOn[i])) {
activity.dependsOn[i] = activityByUuid[activity.dependsOn[i]].name;
}
}
}
}
}
}
addActivityLookup(
activity: Activity,
activityByName: Record<string, Activity>,
activityByUuid: Record<string, Activity>,
errors: string[]
): boolean {
let nameExists = activityByName.hasOwnProperty(activity.name);
let uuidExists = activityByUuid.hasOwnProperty(activity.uuid);
if (nameExists && uuidExists) {
errors.push(`Duplicate activity '${activity.name}' (${activity.uuid}). Please remove one from your activity yaml files.`) // eslint-disable-line
} else if (nameExists) {
errors.push(`Duplicate activity name '${activity.name}' (${activity.uuid} and ${activityByName[activity.name].uuid}). Please remove or rename one of the activities.`) // eslint-disable-line
} else if (uuidExists) {
errors.push(`Duplicate activity uuid '${activity.uuid}' ('${activity.name}' and '${activityByUuid[activity.uuid].name}').`) // eslint-disable-line
} else {
activityByName[activity.name] = activity;
activityByUuid[activity.uuid] = activity;
return true;
}
return false;
}
/**
* Merge new activities into list of existing activities.
* Override property by property if the new activity already exists.
* Identify existing by UUID (if present), otherwise by Name.
*
* If any errors are detected, return this by the error list.
*/
mergeActivities(newActivities: Activity[], existingData: Activity[], errors: string[]) {
for (let newActivity of newActivities) {
let foundExistingActivity: Activity | null = null;
// If newActivity lacks uuid, identify by name
if (!newActivity.uuid) {
if (this._activityByName.hasOwnProperty(newActivity.name)) {
foundExistingActivity = this._activityByName[newActivity.name];
}
} else {
// Identify by uuid
if (this._activityByUuid.hasOwnProperty(newActivity.uuid)) {
foundExistingActivity = this._activityByUuid[newActivity.uuid];
} else {
// The uuid is new, but verify that the name does not exist (i.e. double uuids)
if (this._activityByName.hasOwnProperty(newActivity.name)) {
errors.push(
`The activity '${newActivity.name}' exists with different uuids ` + // eslint-disable-line
`(${newActivity.uuid} and ${this._activityByName[newActivity.name].uuid})` // eslint-disable-line
);
}
}
}
if (foundExistingActivity == null) {
this.addActivity(newActivity, existingData);
} else {
this.updateActivity(newActivity, foundExistingActivity);
}
}
}
addActivity(newActivity: Activity, existingData: Activity[]) {
this._activityByName[newActivity.name] = newActivity;
this._activityByUuid[newActivity.uuid] = newActivity;
existingData.push(newActivity);
}
updateActivity(newActivity: Activity, existingActivity: Activity) {
if (newActivity.name != existingActivity.name)
this._activityByName[newActivity.name] = existingActivity;
if (newActivity.uuid != existingActivity.uuid)
this._activityByUuid[newActivity.uuid] = existingActivity;
Object.assign(existingActivity, newActivity);
}
}