-
Notifications
You must be signed in to change notification settings - Fork 28
/
Collection.js
1080 lines (1002 loc) · 36.6 KB
/
Collection.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const writer = require('../utils/writer')
const config = require('../utils/config')
const escape = require('../utils/escape')
const CollectionService = require(`../service/CollectionService`)
const AssetService = require(`../service/AssetService`)
const STIGService = require(`../service/STIGService`)
const Serialize = require(`../utils/serializers`)
const Security = require('../utils/accessLevels')
const SmError = require('../utils/error')
const Archiver = require('archiver')
const {XMLBuilder} = require("fast-xml-parser")
const {escapeForXml} = require('../utils/escape')
module.exports.defaultSettings = {
fields: {
detail: {
enabled: 'always',
required: 'always'
},
comment: {
enabled: 'findings',
required: 'findings'
}
},
status: {
canAccept: true,
resetCriteria: 'result',
minAcceptGrant: 3
},
history: {
maxReviews: 15
}
}
module.exports.createCollection = async function createCollection (req, res, next) {
try {
const projection = req.query.projection
const elevate = req.query.elevate
const body = req.body
if ( elevate || req.userObject.privileges.canCreateCollection ) {
if (!hasUniqueGrants(body.grants)) {
throw new SmError.UnprocessableError('Duplicate user in grant array')
}
try {
const response = await CollectionService.createCollection( body, projection, req.userObject, res.svcStatus)
res.status(201).json(response)
}
catch (err) {
// This is MySQL specific, should abstract
if (err.code === 'ER_DUP_ENTRY') {
throw new SmError.UnprocessableError('Duplicate name exists.')
}
else {
throw err
}
}
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.deleteCollection = async function deleteCollection (req, res, next) {
try {
const elevate = req.query.elevate
const collectionId = req.params.collectionId
const projection = req.query.projection
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (elevate || (collectionGrant?.accessLevel === 4)) {
const response = await CollectionService.deleteCollection(collectionId, projection, elevate, req.userObject)
res.json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.exportCollections = async function exportCollections (projection, elevate, userObject) {
try {
return await CollectionService.getCollections( {}, projection, elevate, userObject )
}
catch (err) {
next(err)
}
}
module.exports.getChecklistByCollectionStig = async function getChecklistByCollectionStig (req, res, next) {
try {
const collectionId = req.params.collectionId
const benchmarkId = req.params.benchmarkId
const revisionStr = req.params.revisionStr
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if ( collectionGrant ) {
const response = await CollectionService.getChecklistByCollectionStig(collectionId, benchmarkId, revisionStr, req.userObject )
res.json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getCollection = async function getCollection (req, res, next) {
try {
const collectionId = req.params.collectionId
const projection = req.query.projection
const elevate = req.query.elevate
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (collectionGrant || elevate ) {
const response = await CollectionService.getCollection(collectionId, projection, elevate, req.userObject )
res.status(typeof response === 'undefined' ? 204 : 200).json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getCollections = async function getCollections (req, res, next) {
try {
const projection = req.query.projection
const elevate = req.query.elevate
const name = req.query.name
const nameMatch = req.query['name-match']
const metadata = req.query.metadata
const response = await CollectionService.getCollections({
name: name,
nameMatch: nameMatch,
metadata: metadata
}, projection, elevate, req.userObject)
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.getFindingsByCollection = async function getFindingsByCollection (req, res, next) {
try {
const collectionId = req.params.collectionId
const aggregator = req.query.aggregator
const benchmarkId = req.query.benchmarkId
const assetId = req.query.assetId
const acceptedOnly = req.query.acceptedOnly
const projection = req.query.projection
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (collectionGrant) {
const response = await CollectionService.getFindingsByCollection( collectionId, aggregator, benchmarkId, assetId, acceptedOnly, projection, req.userObject )
res.json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getPoamByCollection = async function getFindingsByCollection (req, res, next) {
try {
const collectionId = req.params.collectionId
const aggregator = req.query.aggregator
const benchmarkId = req.query.benchmarkId
const assetId = req.query.assetId
const acceptedOnly = req.query.acceptedOnly
const defaults = {
date: req.query.date,
office: req.query.office,
status: req.query.status
}
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (collectionGrant) {
const response = await CollectionService.getFindingsByCollection( collectionId, aggregator, benchmarkId, assetId, acceptedOnly,
[
'rulesWithDiscussion',
'groups',
'assets',
'stigs',
'ccis'
], req.userObject )
const po = Serialize.poamObjectFromFindings(response, defaults)
const xlsx = await Serialize.xlsxFromPoamObject(po)
let collectionName = collectionGrant.collection.name
writer.writeInlineFile( res, xlsx, `POAM-${collectionName}.xlsx`, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getStatusByCollection = async function getStatusByCollection (req, res, next) {
try {
const collectionId = req.params.collectionId
const benchmarkIds = req.query.benchmarkId
const assetIds = req.query.assetId
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (collectionGrant) {
const response = await CollectionService.getStatusByCollection( collectionId, assetIds, benchmarkIds, req.userObject )
res.json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getStigAssetsByCollectionUser = async function getStigAssetsByCollectionUser (req, res, next) {
try {
const collectionId = req.params.collectionId
const userId = req.params.userId
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if ( collectionGrant?.accessLevel >= 3 ) {
const response = await CollectionService.getStigAssetsByCollectionUser(collectionId, userId, req.userObject )
res.json(response)
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.getStigsByCollection = async function getStigsByCollection (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const labelIds = req.query.labelId
const labelNames = req.query.labelName
const labelMatch = req.query.labelMatch
const projections = req.query.projection
const response = await CollectionService.getStigsByCollection({collectionId, labelIds, labelNames, labelMatch, projections, userObject: req.userObject})
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.getStigByCollection = async function getStigByCollection (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const benchmarkId = req.params.benchmarkId
const projections = req.query.projection
const response = await CollectionService.getStigsByCollection({collectionId, projections, userObject: req.userObject, benchmarkId})
if (!response[0]) {
res.status(204)
}
res.json(response[0])
}
catch (err) {
next(err)
}
}
module.exports.replaceCollection = async function replaceCollection (req, res, next) {
try {
const elevate = req.query.elevate
const {collectionId, collectionGrant} = getCollectionInfoAndCheckPermission(req, Security.ACCESS_LEVEL.Manage, true)
const projection = req.query.projection
const body = req.body
if (!hasUniqueGrants(body.grants)) {
throw new SmError.UnprocessableError('Duplicate user in grant array')
}
const existingGrants = (await CollectionService.getCollection(collectionId, ['grants'], false, req.userObject ))
?.grants
.map(g => ({userId: g.user.userId, accessLevel: g.accessLevel}))
if (!elevate && (collectionGrant.accessLevel !== Security.ACCESS_LEVEL.Owner && !requestedOwnerGrantsMatchExisting(body.grants, existingGrants))) {
throw new SmError.PrivilegeError('Cannot create or modify owner grants.')
}
let response = await CollectionService.replaceCollection(collectionId, body, projection, req.userObject, res.svcStatus)
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.setStigAssetsByCollectionUser = async function setStigAssetsByCollectionUser (req, res, next) {
try {
const collectionId = req.params.collectionId
const userId = req.params.userId
const stigAssets = req.body
const collectionGrant = req.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if ( collectionGrant?.accessLevel >= 3 ) {
const collectionResponse = await CollectionService.getCollection(collectionId, ['grants'], false, req.userObject )
if (collectionResponse.grants.filter( grant => grant.accessLevel === 1 && grant.user.userId === userId).length > 0) {
await CollectionService.setStigAssetsByCollectionUser(collectionId, userId, stigAssets, res.svcStatus )
const getResponse = await CollectionService.getStigAssetsByCollectionUser(collectionId, userId, req.userObject )
res.json(getResponse)
}
else {
throw new SmError.NotFoundError('User not found in this Collection with accessLevel === 1.')
}
}
else {
throw new SmError.PrivilegeError()
}
}
catch (err) {
next(err)
}
}
module.exports.updateCollection = async function updateCollection (req, res, next) {
try {
const elevate = req.query.elevate
const {collectionId, collectionGrant} = getCollectionInfoAndCheckPermission(req, Security.ACCESS_LEVEL.Manage, true)
const projection = req.query.projection
const body = req.body
if (body.grants) {
if (!hasUniqueGrants(body.grants)) {
throw new SmError.UnprocessableError('Duplicate user in grant array')
}
const existingGrants = (await CollectionService.getCollection(collectionId, ['grants'], false, req.userObject ))
?.grants
.map(g => ({userId: g.user.userId, accessLevel: g.accessLevel}))
if (!elevate && (collectionGrant.accessLevel !== Security.ACCESS_LEVEL.Owner && !requestedOwnerGrantsMatchExisting(body.grants, existingGrants))) {
throw new SmError.PrivilegeError('Cannot create or modify owner grants.')
}
}
let response = await CollectionService.replaceCollection(collectionId, body, projection, req.userObject, res.svcStatus)
res.json(response)
}
catch (err) {
next(err)
}
}
function hasUniqueGrants(requestedGrants) {
const requestedUsers = {}
for (const grant of requestedGrants) {
if (requestedUsers[grant.userId]) return false
requestedUsers[grant.userId] = true
}
return true
}
function requestedOwnerGrantsMatchExisting(requestedGrants, existingGrants) {
const accumulateOwners = (accumulator, currentValue) => {
if (currentValue.accessLevel === Security.ACCESS_LEVEL.Owner) accumulator.push(currentValue.userId)
return accumulator
}
const haveSameSet = (a, b) => {
return a.every(item => b.includes(item)) && b.every(item => a.includes(item))
}
const existingOwners = existingGrants.reduce(accumulateOwners, [])
const requestedOwners = requestedGrants.reduce(accumulateOwners, [])
if ( existingOwners.length !== requestedOwners.length || !haveSameSet(existingOwners, requestedOwners)) {
return false
}
return true
}
function getCollectionIdAndCheckPermission(request, minimumAccessLevel = Security.ACCESS_LEVEL.Manage, allowElevate = false) {
let collectionId = request.params.collectionId
const elevate = request.query.elevate
const collectionGrant = request.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (!( (allowElevate && elevate) || (collectionGrant?.accessLevel >= minimumAccessLevel) )) {
throw new SmError.PrivilegeError()
}
return collectionId
}
function getCollectionInfoAndCheckPermission(request, minimumAccessLevel = Security.ACCESS_LEVEL.Manage, allowElevate = false) {
let collectionId = request.params.collectionId
const elevate = request.query.elevate
const collectionGrant = request.userObject.collectionGrants.find( g => g.collection.collectionId === collectionId )
if (!( (allowElevate && elevate) || (collectionGrant?.accessLevel >= minimumAccessLevel) )) {
throw new SmError.PrivilegeError()
}
return {collectionId, collectionGrant}
}
module.exports.getCollectionIdAndCheckPermission = getCollectionIdAndCheckPermission
module.exports.getCollectionMetadata = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let result = await CollectionService.getCollectionMetadata(collectionId, req.userObject)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.patchCollectionMetadata = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let metadata = req.body
await CollectionService.patchCollectionMetadata(collectionId, metadata)
let result = await CollectionService.getCollectionMetadata(collectionId)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.putCollectionMetadata = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let body = req.body
await CollectionService.putCollectionMetadata( collectionId, body)
let result = await CollectionService.getCollectionMetadata(collectionId)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.getCollectionMetadataKeys = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let result = await CollectionService.getCollectionMetadataKeys(collectionId, req.userObject)
if (!result) {
throw new SmError.NotFoundError('metadata keys not found')
}
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.getCollectionMetadataValue = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let key = req.params.key
let result = await CollectionService.getCollectionMetadataValue(collectionId, key, req.userObject)
if (!result) {
throw new SmError.NotFoundError('metadata key not found')
}
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.putCollectionMetadataValue = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let key = req.params.key
let value = req.body
await CollectionService.putCollectionMetadataValue(collectionId, key, value)
res.status(204).send()
}
catch (err) {
next(err)
}
}
module.exports.deleteCollectionMetadataKey = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req)
let key = req.params.key
await CollectionService.deleteCollectionMetadataKey(collectionId, key, req.userObject)
res.status(204).send()
}
catch (err) {
next(err)
}
}
module.exports.deleteReviewHistoryByCollection = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Manage)
const retentionDate = req.query.retentionDate
const assetId = req.query.assetId
let result = await CollectionService.deleteReviewHistoryByCollection(collectionId, retentionDate, assetId)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.getReviewHistoryByCollection = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Full)
const startDate = req.query.startDate
const endDate = req.query.endDate
const assetId = req.query.assetId
const ruleId = req.query.ruleId
const status = req.query.status
let result = await CollectionService.getReviewHistoryByCollection(collectionId, startDate, endDate, assetId, ruleId, status)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.getReviewHistoryStatsByCollection = async function (req, res, next) {
try {
let collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Full)
const startDate = req.query.startDate
const endDate = req.query.endDate
const assetId = req.query.assetId
const ruleId = req.query.ruleId
const status = req.query.status
const projection = req.query.projection
let result = await CollectionService.getReviewHistoryStatsByCollection(collectionId, startDate, endDate, assetId, ruleId, status, projection)
res.json(result)
}
catch (err) {
next(err)
}
}
module.exports.getCollectionLabels = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const response = await CollectionService.getCollectionLabels( collectionId, req.userObject )
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.createCollectionLabel = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Manage)
const labelId = await CollectionService.createCollectionLabel( collectionId, req.body )
const response = await CollectionService.getCollectionLabelById( collectionId, labelId, req.userObject )
res.status(201).json(response)
}
catch (err) {
next(err)
}
}
module.exports.getCollectionLabelById = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const response = await CollectionService.getCollectionLabelById( collectionId, req.params.labelId, req.userObject )
if (!response) {
throw new SmError.NotFoundError()
}
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.patchCollectionLabelById = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Manage)
const affectedRows = await CollectionService.patchCollectionLabelById( collectionId, req.params.labelId, req.body )
if (affectedRows === 0) {
throw new SmError.NotFoundError()
}
const response = await CollectionService.getCollectionLabelById( collectionId, req.params.labelId, req.userObject )
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.deleteCollectionLabelById = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Manage)
const affectedRows = await CollectionService.deleteCollectionLabelById(collectionId, req.params.labelId)
if (affectedRows === 0) {
throw new SmError.NotFoundError()
}
res.status(204).end()
}
catch (err) {
next(err)
}
}
module.exports.getAssetsByCollectionLabelId = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const response = await CollectionService.getAssetsByCollectionLabelId( collectionId, req.params.labelId, req.userObject )
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.putAssetsByCollectionLabelId = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req)
const labelId = req.params.labelId
const assetIds = req.body
let collection = await CollectionService.getCollection( collectionId, ['assets','labels'], false, req.userObject)
if (!collection.labels.find( l => l.labelId === labelId)) {
throw new SmError.PrivilegeError('The labelId is not associated with this Collection.')
}
let collectionAssets = collection.assets.map( a => a.assetId)
if (assetIds.every( a => collectionAssets.includes(a))) {
await CollectionService.putAssetsByCollectionLabelId( collectionId, labelId, assetIds, res.svcStatus )
const response = await CollectionService.getAssetsByCollectionLabelId( collectionId, req.params.labelId, req.userObject )
res.json(response)
}
else {
throw new SmError.PrivilegeError('One or more assetId is not a Collection member.')
}
}
catch (err) {
next(err)
}
}
module.exports.postCklArchiveByCollection = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req)
const mode = req.query.mode || 'mono'
const parsedRequest = await processAssetStigRequests (req.body, collectionId, mode, req.userObject)
await postArchiveByCollection({
format: `ckl-${mode}`,
req,
res,
parsedRequest
})
}
catch (err) {
next(err)
}
}
module.exports.postCklbArchiveByCollection = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req)
const mode = req.query.mode || 'mono'
const parsedRequest = await processAssetStigRequests (req.body, collectionId, mode, req.userObject)
await postArchiveByCollection({
format: `cklb-${mode}`,
req,
res,
parsedRequest
})
}
catch (err) {
next(err)
}
}
module.exports.postXccdfArchiveByCollection = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req)
const parsedRequest = await processAssetStigRequests (req.body, collectionId, 'mono', req.userObject)
await postArchiveByCollection({
format: 'xccdf',
req,
res,
parsedRequest
})
}
catch (err) {
next(err)
}
}
async function postArchiveByCollection ({format = 'ckl-mono', req, res, parsedRequest}) {
req.noCompression = true
const builder = new XMLBuilder({
attributeNamePrefix : "@_",
textNodeName : "#text",
ignoreAttributes: format.startsWith('ckl-'),
cdataTagName: "__cdata",
cdataPositionChar: "\\c",
format: true,
indentBy: " ",
supressEmptyNode: format === 'xccdf',
processEntities: false,
tagValueProcessor: escapeForXml,
attrValueProcessor: escapeForXml
})
const zip = Archiver('zip', {zlib: {level: 9}})
const attachmentName = escape.escapeFilename(`${parsedRequest.collection.name}-${format.startsWith('ckl-') ?
'CKL' : format.startsWith('cklb-') ? 'CKLB' : 'XCCDF'}.zip`)
res.attachment(attachmentName)
zip.pipe(res)
const manifest = {
started: new Date().toISOString(),
finished: '',
errorCount: 0,
errors: [],
memberCount: 0,
members: [],
requestParams: {
collection: parsedRequest.collection,
assetStigs: req.body
}
}
zip.on('error', function (e) {
manifest.errors.push({message: e.message, stack: e.stack})
manifest.errorCount += 1
})
for (const arg of parsedRequest.assetStigArguments) {
try {
let response
switch (format) {
case 'ckl-mono':
case 'ckl-multi':
response = await AssetService.cklFromAssetStigs(arg.assetId, arg.stigs)
break
case 'cklb-mono':
case 'cklb-multi':
response = await AssetService.cklbFromAssetStigs(arg.assetId, arg.stigs)
break
case 'xccdf':
response = await AssetService.xccdfFromAssetStig(arg.assetId, arg.stigs[0].benchmarkId, arg.stigs[0].revisionStr)
}
let data
if (response.xmlJs) {
data = `<?xml version="1.0" encoding="UTF-8"?>\n<!-- STIG Manager ${config.version} -->\n<!-- Classification: ${config.settings.setClassification} -->\n`
data += builder.build(response.xmlJs)
}
else {
data = JSON.stringify(response.cklb)
}
let filename = arg.assetName
if (format === 'ckl-mono' || format === 'cklb-mono' || format === 'xccdf') {
filename += `-${arg.stigs[0].benchmarkId}-${response.revisionStrResolved}`
}
filename += `${format === 'xccdf' ? '-xccdf.xml' : format.startsWith('ckl-') ? '.ckl' : '.cklb'}`
filename = escape.escapeFilename(filename)
zip.append(data, {name: filename})
manifest.members.push(filename)
manifest.memberCount += 1
}
catch (e) {
arg.error = {message: e.message, stack: e.stack}
manifest.errors.push(arg)
manifest.errorCount += 1
}
}
manifest.finished = new Date().toISOString()
manifest.members.sort((a,b) => a.localeCompare(b))
zip.append(JSON.stringify(manifest, null, 2), {name: '_manifest.json'})
await zip.finalize()
}
module.exports.getUnreviewedAssetsByCollection = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const benchmarkId = req.query.benchmarkId
const assetId = req.query.assetId
const severities = req.query.severity || []
const labelIds = req.query.labelId || []
const labelNames = req.query.labelName || []
const projections = req.query.projection || []
const response = await CollectionService.getUnreviewedAssetsByCollection( {
collectionId,
benchmarkId,
assetId,
labelIds,
labelNames,
severities,
projections,
userObject: req.userObject
})
res.json(response)
}
catch (err) {
next(err)
}
}
module.exports.getUnreviewedRulesByCollection = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Restricted)
const benchmarkId = req.query.benchmarkId
const ruleId = req.query.ruleId
const severities = req.query.severity || []
const labelIds = req.query.labelId || []
const labelNames = req.query.labelName || []
const projections = req.query.projection || []
const response = await CollectionService.getUnreviewedRulesByCollection( {
collectionId,
benchmarkId,
ruleId,
severities,
labelIds,
labelNames,
projections,
userObject: req.userObject
})
res.json(response)
}
catch (err) {
next(err)
}
}
// for the archive streaming endpoints
async function processAssetStigRequests (assetStigRequests, collectionId, mode = 'mono', userObject) {
const assetStigArguments = []
let collectionName
// Pre-fetch the available revisions of STIGs that were accompanied by a requested revision
// Build a Set of the requested STIGs that were accomapnied by a requested revision
const requestedStigRevisionsSet = assetStigRequests.reduce((acc, value) => {
if (value.stigs) {
for (const item of value.stigs) {
if (typeof item !== 'string') {
acc.add(item.benchmarkId)
}
}
}
return acc
}, new Set())
const requestedStigRevisionsArray = [...requestedStigRevisionsSet]
// Create an object that can have benchmarkId properties and values of revisionStr arrays
let availableRevisions = {}
if (requestedStigRevisionsArray.length) {
availableRevisions = await STIGService.getRevisionStrsByBenchmarkIds(requestedStigRevisionsArray)
}
// iterate through the request
for (const requested of assetStigRequests) {
const assetId = requested.assetId
// Try to fetch asset as this user.
const assetResponse = await AssetService.getAsset(assetId, ['stigs'], false, userObject )
// Does user have a grant permitting access to the asset?
if (!assetResponse) {
throw new SmError.PrivilegeError()
}
// Is asset a member of collectionId?
if (assetResponse.collection.collectionId !== collectionId) {
throw new SmError.UnprocessableError(`Asset id ${assetId} is not a member of Collection id ${collectionId}.`)
}
if (!collectionName) { collectionName = assetResponse.collection.name } // will be identical for other assets
// Does the asset have STIG assignments?
if (assetResponse.stigs.length === 0) {
throw new SmError.UnprocessableError(`Asset id ${assetId} has no STIG assignments.`)
}
// create Set with keys being the asset's benchmarkId assignments
const assignedStigsSet = new Set(assetResponse.stigs.map( stig => stig.benchmarkId))
// create Map with keys being the requested benchmarkIds for the asset and values being an array of requested revisionStrs for that benchmarkId
const requestedRevisionsMap = new Map()
if (!requested.stigs) {
// request doesn't specify STIGs, so create keys for each assigned benchmarkId and set each value to an array containing the default revision string
for (const stig of assetResponse.stigs) {
requestedRevisionsMap.set(stig.benchmarkId, [stig.revisionStr])
}
}
else {
// request includes specific STIGs
for (const stig of requested.stigs) {
if (typeof stig === 'string' && assignedStigsSet.has(stig)) {
// value is a benchmarkId string that matches an available STIG mapping
// get already requested revisions for this STIG or any empty array
const revisions = requestedRevisionsMap.get(stig) ?? []
// add the default revision string to the requested revisions
revisions.push(assetResponse.stigs.find( assetStig => assetStig.benchmarkId === stig).revisionStr)
// update the Map
requestedRevisionsMap.set(stig, revisions)
}
else if ((stig.revisionStr === 'latest' && assignedStigsSet.has(stig.benchmarkId)) ||
(assignedStigsSet.has(stig.benchmarkId) && availableRevisions[stig.benchmarkId].includes(stig.revisionStr))) {
// value is an object that matches an available STIG/Revision mapping
// get already requested revisions for this STIG or any empty array
const revisions = requestedRevisionsMap.get(stig.benchmarkId) ?? []
// add this requested revision string to the requested revisions
revisions.push(stig.revisionStr)
// update the Map
requestedRevisionsMap.set(stig.benchmarkId, revisions)
}
else {
throw new SmError.UnprocessableError(`Asset id ${assetId} is not mapped to ${JSON.stringify(stig)}.`)
}
}
}
// For generating individual filenames
const assetName = assetResponse.name
if (mode === 'mono') {
// XCCDF and mono CKLs
for (const entry of requestedRevisionsMap) {
for (const revisionStr of entry[1]) {
assetStigArguments.push({
assetId,
assetName,
stigs: [{benchmarkId: entry[0], revisionStr}]
})
}
}
}
else {
// multi-STIG CKLs
const stigsParam = []
for (const entry of requestedRevisionsMap) {
for (const revisionStr of entry[1]) {
stigsParam.push({benchmarkId: entry[0], revisionStr})
}
}
assetStigArguments.push({
assetId,
assetName,
stigs: stigsParam
})
}
}
return {
collection: {
collectionId,
name: collectionName,
},
assetStigArguments
}
}
module.exports.writeStigPropsByCollectionStig = async function (req, res, next) {
try {
const collectionId = getCollectionIdAndCheckPermission(req, Security.ACCESS_LEVEL.Manage)
const benchmarkId = req.params.benchmarkId
const assetIds = req.body.assetIds
const defaultRevisionStr = req.body.defaultRevisionStr
const existingRevisions = await STIGService.getRevisionsByBenchmarkId(benchmarkId, req.userObject)
//if defaultRevisionStr is present, check that specified revision is valid for the benchmark
if (defaultRevisionStr && defaultRevisionStr !== "latest" && existingRevisions.find(benchmark => benchmark.revisionStr === defaultRevisionStr) === undefined) {
throw new SmError.UnprocessableError("The revisionStr is is not valid for the specified benchmarkId")
}
// The OAS layer mandated if assetIds is absent then defaultRevisionStr must be present
// we do not permit setting the default revision of an unassigned STIG
if (!assetIds && !await CollectionService.doesCollectionIncludeStig({collectionId, benchmarkId})) {
throw new SmError.UnprocessableError('Cannot set the default revision of a benchmarkId that has no mapped Assets')
}
if (assetIds && assetIds.length === 0 && defaultRevisionStr) {
throw new SmError.UnprocessableError('Cannot set the default revision of a benchmarkId and also remove all mapped Assets')
}
if (assetIds?.length) {
const collectionHasAssets = await CollectionService.doesCollectionIncludeAssets({
collectionId,
assetIds
})
if (!collectionHasAssets) {
throw new SmError.PrivilegeError('One or more assetId is not a Collection member.')
}
}
await CollectionService.writeStigPropsByCollectionStig( {
collectionId,
benchmarkId,
assetIds,
defaultRevisionStr,
svcStatus: res.svcStatus
})
const response = await CollectionService.getStigsByCollection({collectionId, userObject: req.userObject, benchmarkId})
if (response[0]) {
res.json(response[0])
}
else {