-
Notifications
You must be signed in to change notification settings - Fork 3
/
TaskObject.js
215 lines (189 loc) · 7.92 KB
/
TaskObject.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
const addItemToMapArrayValue = (map, key, item) => {
const arr = map.get(key) ?? []
arr.push(item)
map.set(key, arr)
}
export default class TaskObject {
/** @type {Map<string, ApiAsset>} */
#assetNameMap
/** @type {Map<string, ApiAsset[]} */
#cklHostnameMap
/** @type {Map<string, string[]} */
#benchmarkIdMap
/** @type {ParseResult[]} */
parsedResults
/** @type {ApiAsset[]} */
apiAssets
/** @type {ApiStig[]} */
apiStigs
/** @type {any[]} */
sourceRefs
/**
* @param {Object} TaskObjectParam
* @param {ApiAsset[]} TaskObjectParam.apiAssets
* @param {ApiStig[]} TaskObjectParam.apiStigs
* @param {ParseResult[]} TaskObjectParam.parsedResults
* @param {TaskObjectOptions} TaskObjectParam.options
*/
constructor({ apiAssets = [], apiStigs = [], parsedResults = [], options = {} }) {
// An array of results from the parsers
this.parsedResults = parsedResults
// An array of assets from the API
this.apiAssets = apiAssets
// Create Map for the assets, key:apiAsset.name, value: apiAsset
this.#assetNameMap = new Map()
// Create Map for the cklHostnames, key:apiAsset.metadata.cklHostName, value:apiAsset[]
this.#cklHostnameMap = new Map()
// An array of any parseResult.sourceRef
this.sourceRefs = parsedResults.filter( parseResult => parseResult.sourceRef !== undefined )
for (const apiAsset of apiAssets) {
// Change apiAsset.stigs from an array of stig objects to an array of benchmarkId strings
apiAsset.stigs = apiAsset.stigs.map(stig => stig.benchmarkId)
this.#assetNameMap.set(apiAsset.name.toLowerCase(), apiAsset)
if (apiAsset.metadata?.cklHostName) {
addItemToMapArrayValue(
this.#cklHostnameMap,
apiAsset.metadata.cklHostName.toLowerCase(),
apiAsset
)
}
}
// A Map() of the installed benchmarkIds return by the API
// key: benchmarkId, value: array of revisionStr
this.#benchmarkIdMap = new Map(apiStigs.map(stig => [stig.benchmarkId, stig.revisionStrs]))
// An array of accumulated errors
this.errors = []
// A Map() of assets to be processed by the writer
this.taskAssets = this.#createTaskAssets(options)
}
#findAssetFromParsedTarget(target) {
// If there's no target.metadata.cklHostName, return the apiAsset (if any) matching the target.name
if (!target.metadata.cklHostName) {
return this.#assetNameMap.get(target.name.toLowerCase())
}
// get the array of apiAssets (if any) having the given target.metadata.cklHostName
const matchedByCklHostname = this.#cklHostnameMap.get(target.metadata.cklHostName.toLowerCase())
// return null if no matches
if (!matchedByCklHostname) return null
// find the first apiAsset that matches all the CKL metadata , or null
const matchedByAllCklMetadata = matchedByCklHostname.find(
asset => asset.metadata.cklWebDbInstance?.toLowerCase() === target.metadata.cklWebDbInstance?.toLowerCase()
&& asset.metadata.cklWebDbSite?.toLowerCase() === target.metadata.cklWebDbSite?.toLowerCase())
if (!matchedByAllCklMetadata) return null
return matchedByAllCklMetadata
}
#createTaskAssets(options) {
// taskAssets is a Map() keyed by lowercase asset name (or CKL metadata), the value is an object:
// {
// knownAsset: false, // does the asset need to be created
// assetProps: null, // an Asset object suitable for put/post to the API
// hasNewAssignment: false, // are there new STIG assignments?
// newAssignments: [], // any new assignments
// checklists: new Map(), // the vetted result checklists, a Map() keyed by benchmarkId
// checklistsIgnored: [], // the ignored checklists
// reviews: [] // the vetted reviews
// }
/** @type {Map<string, TaskAssetValue} */
const taskAssets = new Map()
for (const parsedResult of this.parsedResults) {
// Generate mapping key
let mapKey, tMeta = parsedResult.target.metadata
if (!tMeta.cklHostName) {
mapKey = parsedResult.target.name.toLowerCase()
}
else {
mapKey = `${tMeta.cklHostName}-${tMeta.cklWebDbSite ?? 'NA'}-${tMeta.cklWebDbInstance ?? 'NA'}`
}
// Try to find the asset in apiAssets
const apiAsset = this.#findAssetFromParsedTarget(parsedResult.target)
if (!apiAsset && !options.createObjects) {
// Bail if the asset doesn't exist and we shouldn't create it
/** @type {TaskObjectError} */
const error = {
message: `Asset does not exist for target and createObjects is false`,
target: parsedResult.target,
sourceRef: parsedResult.sourceRef
}
this.errors.push(error)
continue
}
// Try to find the target in our Map()
/** @type {TaskAssetValue} */
let taskAsset = taskAssets.get(mapKey)
if (!taskAsset) {
// This is our first encounter with this assetName, initialize Map() value
taskAsset = {
knownAsset: false,
assetProps: null, // an object suitable for put/post to the API
hasNewAssignment: false,
newAssignments: [],
checklists: new Map(), // the vetted result checklists
checklistsIgnored: [], // the ignored checklists
sourceRefs: [] // the sourceRefs from each parsedResult for this Asset
}
if (!apiAsset) {
// The asset does not exist in the API. Set assetProps from this parseResult.
if (!tMeta.cklHostName) {
taskAsset.assetProps = { ...parsedResult.target, collectionId: options.collectionId, stigs: [] }
}
else {
taskAsset.assetProps = { ...parsedResult.target, name: mapKey, collectionId: options.collectionId, stigs: [] }
}
}
else {
// The asset exists in the API. Set assetProps from the apiAsset.
taskAsset.knownAsset = true
taskAsset.assetProps = apiAsset
}
// Insert the asset into taskAssets
taskAssets.set(mapKey, taskAsset)
}
// add any parsedResult.sourceRef to this asset's sourceRefs
parsedResult.sourceRef !== undefined && taskAsset.sourceRefs.push(parsedResult.sourceRef)
// Helper functions
const stigIsInstalled = ({ benchmarkId, revisionStr }) => {
const revisionStrs = this.#benchmarkIdMap.get(benchmarkId)
if (revisionStrs) {
return revisionStr && options.strictRevisionCheck ? revisionStrs.includes(revisionStr) : true
}
else {
return false
}
}
const stigIsAssigned = ({ benchmarkId }) => {
return taskAsset.assetProps.stigs.includes(benchmarkId)
}
const assignStig = (benchmarkId) => {
if (!stigIsAssigned(benchmarkId)) {
taskAsset.hasNewAssignment = true
taskAsset.newAssignments.push(benchmarkId)
taskAsset.assetProps.stigs.push(benchmarkId)
}
}
const stigIsNewlyAssigned = (benchmarkId) => taskAsset.newAssignments.includes(benchmarkId)
// Vet the checklists in this parseResult
for (const checklist of parsedResult.checklists) {
if (stigIsInstalled(checklist)) {
if (stigIsAssigned(checklist)) {
checklist.newAssignment = stigIsNewlyAssigned(checklist.benchmarkId)
addItemToMapArrayValue(taskAsset.checklists, checklist.benchmarkId, checklist)
}
else if (options.createObjects) {
assignStig(checklist.benchmarkId)
checklist.newAssignment = true
addItemToMapArrayValue(taskAsset.checklists, checklist.benchmarkId, checklist)
}
else {
checklist.ignored = `STIG not assigned to Asset and createObjects is false.`
taskAsset.checklistsIgnored.push(checklist)
}
}
else {
checklist.ignored = `Not installed`
taskAsset.checklistsIgnored.push(checklist)
}
}
}
return taskAssets
}
}