-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdata-source.js
More file actions
887 lines (821 loc) · 30 KB
/
Copy pathdata-source.js
File metadata and controls
887 lines (821 loc) · 30 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
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
import { t } from '@lingui/macro'
import { aql } from 'arangojs'
import {
loadDomainByDomain,
loadDomainByKey,
loadDomainConnectionsByOrgId,
loadDomainConnectionsByUserId,
loadDkimSelectorsByDomainId,
} from './loaders'
export class DomainDataSource {
constructor({ query, userKey, i18n, language, cleanseInput, loginRequiredBool, transaction, collections }) {
this._query = query
this._userKey = userKey
this._i18n = i18n
this._transaction = transaction
this._collections = collections
this.byDomain = loadDomainByDomain({ query, userKey, i18n })
this.byKey = loadDomainByKey({ query, userKey, i18n })
this.connectionsByOrgId = loadDomainConnectionsByOrgId({
query,
userKey,
language,
cleanseInput,
i18n,
auth: { loginRequiredBool },
})
this.connectionsByUserId = loadDomainConnectionsByUserId({
query,
userKey,
cleanseInput,
i18n,
auth: { loginRequiredBool },
})
this.dkimSelectorsByDomainId = loadDkimSelectorsByDomainId({ query, userKey, i18n })
}
async create({ insertDomain, org, tags, assetState }) {
const trx = await this._transaction(this._collections)
let domainCursor
try {
domainCursor = await trx.step(
() => this._query`
UPSERT { domain: ${insertDomain.domain} }
INSERT ${insertDomain}
UPDATE { }
IN domains
RETURN NEW
`,
)
} catch (err) {
console.error(`Transaction step error occurred for user: ${this._userKey} when inserting new domain: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
let insertedDomain
try {
insertedDomain = await domainCursor.next()
} catch (err) {
console.error(`Cursor error occurred for user: ${this._userKey} when inserting new domain: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
try {
await trx.step(
() => this._query`
WITH claims
INSERT {
_from: ${org._id},
_to: ${insertedDomain._id},
tags: ${tags},
assetState: ${assetState},
firstSeen: ${new Date().toISOString()},
} INTO claims
`,
)
} catch (err) {
console.error(`Transaction step error occurred for user: ${this._userKey} when inserting new domain edge: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Transaction commit error occurred while user: ${this._userKey} was creating domain: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
this.byDomain.clear(insertDomain.domain)
return this.byDomain.load(insertDomain.domain)
}
async favourite({ domain, user }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
() => this._query`
WITH favourites
INSERT {
_from: ${user._id},
_to: ${domain._id},
} INTO favourites
`,
)
} catch (err) {
console.error(`Transaction step error occurred for user: ${this._userKey} when inserting new domain edge: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to favourite domain. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Transaction commit error occurred while user: ${this._userKey} was creating domain: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to favourite domain. Please try again.`))
}
}
async isFavouritedByUser({ domainId, userId }) {
let checkDomainCursor
try {
checkDomainCursor = await this._query`
WITH domains
FOR v, e IN 1..1 ANY ${domainId} favourites
FILTER e._from == ${userId}
RETURN e
`
} catch (err) {
console.error(`Database error occurred while running check to see if domain already favourited: ${err}`)
throw new Error(this._i18n._(t`Unable to favourite domain. Please try again.`))
}
let checkUserDomain
try {
checkUserDomain = await checkDomainCursor.next()
} catch (err) {
console.error(`Cursor error occurred while running check to see if domain already favourited: ${err}`)
throw new Error(this._i18n._(t`Unable to favourite domain. Please try again.`))
}
return typeof checkUserDomain !== 'undefined'
}
async organizationsClaimingDomain({ domainId, domainName }) {
let countCursor
try {
countCursor = await this._query`
WITH claims, domains, organizations
FOR v, e IN 1..1 ANY ${domainId} claims
RETURN v
`
} catch (err) {
console.error(
`Database error occurred for user: ${this._userKey}, when counting domain claims for domain: ${domainName || domainId}, error: ${err}`,
)
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
return {
organizations: await countCursor.all(),
count: countCursor.count,
}
}
async hasOwnershipClaim({ orgId, domainId, domainName }) {
let dmarcCountCursor
try {
dmarcCountCursor = await this._query`
WITH domains, organizations, ownership
FOR v IN 1..1 OUTBOUND ${orgId} ownership
FILTER v._id == ${domainId}
RETURN true
`
} catch (err) {
console.error(
`Database error occurred for user: ${this._userKey}, when counting ownership claims for domain: ${domainName}, error: ${err}`,
)
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
return dmarcCountCursor.count === 1
}
async organizationAlreadyClaimsDomainName({ orgId, domainName }) {
let checkDomainCursor
try {
checkDomainCursor = await this._query`
WITH claims, domains, organizations
LET domainIds = (FOR domain IN domains FILTER domain.domain == ${domainName} RETURN { id: domain._id })
FOR domainId IN domainIds
LET domainEdges = (FOR v, e IN 1..1 ANY domainId.id claims RETURN { _from: e._from })
FOR domainEdge IN domainEdges
LET org = DOCUMENT(domainEdge._from)
FILTER org._id == ${orgId}
RETURN true
`
} catch (err) {
console.error(`Database error occurred while running check to see if domain already exists in an org: ${err}`)
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
let checkOrgDomain
try {
checkOrgDomain = await checkDomainCursor.next()
} catch (err) {
console.error(`Cursor error occurred while running check to see if domain already exists in an org: ${err}`)
throw new Error(this._i18n._(t`Unable to create domain. Please try again.`))
}
return typeof checkOrgDomain !== 'undefined'
}
async organizationHasClaim({ orgId, domainId, domainKey }) {
let countCursor
try {
countCursor = await this._query`
WITH claims, domains, organizations
FOR v, e IN 1..1 ANY ${domainId} claims
FILTER e._from == ${orgId}
RETURN e
`
} catch (err) {
console.error(
`Database error occurred while user: ${this._userKey} attempted to update domain: ${domainKey}, error: ${err}`,
)
throw new Error(this._i18n._(t`Unable to update domain. Please try again.`))
}
return countCursor.count > 0
}
async loadClaimByOrgAndDomain({ orgId, domainId }) {
let claimCursor
try {
claimCursor = await this._query`
WITH claims
FOR claim IN claims
FILTER claim._from == ${orgId} && claim._to == ${domainId}
RETURN MERGE({ id: claim._key, _type: "claim" }, claim)
`
} catch (err) {
console.error(`Database error occurred when user: ${this._userKey} running loadDomainByKey: ${err}`)
return undefined
}
if (typeof claimCursor?.next !== 'function') {
return undefined
}
try {
return await claimCursor.next()
} catch (err) {
console.error(`Cursor error occurred when user: ${this._userKey} running loadDomainByKey: ${err}`)
return undefined
}
}
async loadClaimForOrgByDomainKey({ orgId, domainKey }) {
let checkClaimCursor
try {
checkClaimCursor = await this._query`
WITH claims, domains, organizations
FOR v, e IN 1..1 ANY ${orgId} claims
FILTER v._key == ${domainKey}
RETURN { claim: e, domain: v.domain }
`
} catch (err) {
console.error(`Database error occurred while running check to see if domain already exists in an org: ${err}`)
return undefined
}
try {
return await checkClaimCursor.next()
} catch (err) {
console.error(`Cursor error occurred while running check to see if domain already exists in an org: ${err}`)
return undefined
}
}
async loadClaimsForOrgByFilters({ orgId, filters, search }) {
let domainFilters = aql``
if (typeof filters !== 'undefined') {
filters.forEach(({ filterCategory, comparison, filterValue }) => {
if (comparison === '==') {
comparison = aql`==`
} else {
comparison = aql`!=`
}
if (filterCategory === 'dmarc-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.dmarc ${comparison} ${filterValue}
`
} else if (filterCategory === 'dkim-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.dkim ${comparison} ${filterValue}
`
} else if (filterCategory === 'https-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.https ${comparison} ${filterValue}
`
} else if (filterCategory === 'spf-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.spf ${comparison} ${filterValue}
`
} else if (filterCategory === 'ciphers-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.ciphers ${comparison} ${filterValue}
`
} else if (filterCategory === 'curves-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.curves ${comparison} ${filterValue}
`
} else if (filterCategory === 'hsts-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.hsts ${comparison} ${filterValue}
`
} else if (filterCategory === 'policy-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.policy ${comparison} ${filterValue}
`
} else if (filterCategory === 'protocols-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.protocols ${comparison} ${filterValue}
`
} else if (filterCategory === 'certificates-status') {
domainFilters = aql`
${domainFilters}
FILTER v.status.certificates ${comparison} ${filterValue}
`
} else if (filterCategory === 'tags') {
if (filterValue === 'archived') {
domainFilters = aql`
${domainFilters}
FILTER v.archived ${comparison} true
`
} else if (filterValue === 'nxdomain') {
domainFilters = aql`
${domainFilters}
FILTER v.rcode ${comparison} "NXDOMAIN"
`
} else if (filterValue === 'blocked') {
domainFilters = aql`
${domainFilters}
FILTER v.blocked ${comparison} true
`
} else if (filterValue === 'wildcard-sibling') {
domainFilters = aql`
${domainFilters}
FILTER v.wildcardSibling ${comparison} true
`
} else if (filterValue === 'wildcard-entry') {
domainFilters = aql`
${domainFilters}
FILTER v.wildcardEntry ${comparison} true
`
} else if (filterValue === 'scan-pending') {
domainFilters = aql`
${domainFilters}
FILTER v.webScanPending ${comparison} true
`
} else if (filterValue === 'has-entrust-certificate') {
domainFilters = aql`
${domainFilters}
FILTER v.hasEntrustCertificate ${comparison} true
`
} else if (filterValue === 'cve-detected') {
domainFilters = aql`
${domainFilters}
FILTER v.cveDetected ${comparison} true
`
} else {
domainFilters = aql`
${domainFilters}
FILTER POSITION( e.tags, ${filterValue}) ${comparison} true
`
}
} else if (filterCategory === 'asset-state') {
domainFilters = aql`
${domainFilters}
FILTER e.assetState ${comparison} ${filterValue}
`
} else if (filterCategory === 'guidance-tag') {
domainFilters = aql`
${domainFilters}
FILTER POSITION(negativeTags, ${filterValue}) ${comparison} true
`
}
})
}
let searchString = aql``
if (typeof search !== 'undefined' && search !== '') {
searchString = aql`FILTER LOWER(v.domain) LIKE LOWER(${search})`
}
let checkClaimsCursor
try {
checkClaimsCursor = await this._query`
WITH claims, domains, organizations
FOR v, e IN 1..1 ANY ${orgId} claims
${domainFilters}
${searchString}
RETURN { claim: e, domain: v.domain }
`
} catch (err) {
console.error(`Database error occurred while running check to see if domain already exists in an org: ${err}`)
throw new Error(this._i18n._(t`Unable to update domains. Please try again.`))
}
let checkClaims
try {
checkClaims = await checkClaimsCursor.all()
} catch (err) {
console.error(`Cursor error occurred while running check to see if domain already exists in an org: ${err}`)
throw new Error(this._i18n._(t`Unable to update domains. Please try again.`))
}
if (typeof checkClaims === 'undefined') {
throw new Error(this._i18n._(t`Unable to update domains. Please try again.`))
}
return checkClaims
}
async unfavourite({ domain, user }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
() => this._query`
WITH favourites, domains, users
LET domainEdges = (FOR v, e IN 1..1 INBOUND ${domain._id} favourites RETURN { _key: e._key, _from: e._from, _to: e._to })
LET edgeKeys = (
FOR domainEdge IN domainEdges
FILTER domainEdge._to == ${domain._id}
FILTER domainEdge._from == ${user._id}
RETURN domainEdge._key
)
FOR edgeKey IN edgeKeys
REMOVE edgeKey IN favourites
OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(`Transaction step error occurred for user: ${this._userKey} when removing domain edge: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to unfavourite domain. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Transaction commit error occurred while user: ${this._userKey} was unfavouriting domain: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to unfavourite domain. Please try again.`))
}
}
async update({ domain, org, domainToInsert, claimToInsert }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
async () =>
await this._query`
WITH domains
UPSERT { _key: ${domain._key} }
INSERT ${domainToInsert}
UPDATE ${domainToInsert}
IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: ${this._userKey} attempted to update domain: ${domain._key}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to update domain. Please try again.`))
}
try {
await trx.step(
async () =>
await this._query`
WITH claims
UPSERT { _from: ${org._id}, _to: ${domain._id} }
INSERT ${claimToInsert}
UPDATE ${claimToInsert}
IN claims
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: ${this._userKey} attempted to update domain edge, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to update domain edge. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred when user: ${this._userKey} attempted to update domain: ${domain._key}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to update domain. Please try again.`))
}
this.byKey.clear(domain._key)
return this.byKey.load(domain._key)
}
async updateClaim({ claim, claimToInsert }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
async () =>
await this._query`
WITH claims
UPSERT { _key: ${claim._key} }
INSERT ${claimToInsert}
UPDATE ${claimToInsert}
IN claims
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: ${this._userKey} attempted to update domain edge, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to update domain. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Transaction commit error occurred while user: ${this._userKey} was updating domain claim: ${err}`)
await trx.abort()
throw new Error(this._i18n._(t`Unable to update domain. Please try again.`))
}
}
async remove({ domain, org, orgsClaimingDomain, hasOwnership }) {
const trx = await this._transaction(this._collections)
if (hasOwnership) {
try {
await trx.step(
() => this._query`
WITH ownership, organizations, domains, dmarcSummaries, domainsToDmarcSummaries
LET dmarcSummaryEdges = (
FOR v, e IN 1..1 OUTBOUND ${domain._id} domainsToDmarcSummaries
RETURN { edgeKey: e._key, dmarcSummaryId: e._to }
)
LET removeDmarcSummaryEdges = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
REMOVE dmarcSummaryEdge.edgeKey IN domainsToDmarcSummaries
OPTIONS { waitForSync: true }
)
LET removeDmarcSummary = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
LET key = PARSE_IDENTIFIER(dmarcSummaryEdge.dmarcSummaryId).key
REMOVE key IN dmarcSummaries
OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing dmarc summary data for user: ${this._userKey} while attempting to remove domain: ${domain.domain}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
try {
await trx.step(
() => this._query`
WITH ownership, organizations, domains
LET domainEdges = (
FOR v, e IN 1..1 INBOUND ${domain._id} ownership
REMOVE e._key IN ownership
OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing ownership data for user: ${this._userKey} while attempting to remove domain: ${domain.domain}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
}
if (orgsClaimingDomain <= 1) {
try {
await trx.step(async () => {
await this._query`
WITH web, webScan, domains
FOR webV, domainsWebEdge IN 1..1 OUTBOUND ${domain._id} domainsWeb
LET removeWebScansQuery = (
FOR webScanV, webToWebScansV In 1..1 OUTBOUND webV._id webToWebScans
REMOVE webScanV IN webScan
REMOVE webToWebScansV IN webToWebScans
OPTIONS { waitForSync: true }
)
REMOVE webV IN web
REMOVE domainsWebEdge IN domainsWeb
OPTIONS { waitForSync: true }
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove web data for ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
try {
await trx.step(async () => {
await this._query`
WITH dns, domains
FOR dnsV, domainsDNSEdge IN 1..1 OUTBOUND ${domain._id} domainsDNS
REMOVE dnsV IN dns
REMOVE domainsDNSEdge IN domainsDNS
OPTIONS { waitForSync: true }
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove DNS data for ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
try {
await trx.step(async () => {
await this._query`
WITH favourites, domains
FOR fav IN favourites
FILTER fav._to == ${domain._id}
REMOVE fav IN favourites
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove favourites for ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
try {
await trx.step(async () => {
await this._query`
FOR e IN domainsToSelectors
FILTER e._from == ${domain._id}
REMOVE e IN domainsToSelectors
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove DKIM selectors for ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
try {
await trx.step(async () => {
await this._query`
FOR claim IN claims
FILTER claim._to == ${domain._id}
REMOVE claim IN claims
REMOVE ${domain} IN domains
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove domain ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
} else {
try {
await trx.step(async () => {
await this._query`
WITH claims, domains, organizations
LET domainEdges = (FOR v, e IN 1..1 INBOUND ${domain._id} claims RETURN { _key: e._key, _from: e._from, _to: e._to })
LET edgeKeys = (
FOR domainEdge IN domainEdges
FILTER domainEdge._to == ${domain._id}
FILTER domainEdge._from == ${org._id}
RETURN domainEdge._key
)
FOR edgeKey IN edgeKeys
REMOVE edgeKey IN claims
OPTIONS { waitForSync: true }
`
})
} catch (err) {
console.error(
`Trx step error occurred while user: ${this._userKey} attempted to remove claim for ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
}
try {
await trx.commit()
} catch (err) {
console.error(
`Trx commit error occurred while user: ${this._userKey} attempted to remove ${domain.domain} in org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to remove domain. Please try again.`))
}
}
async ignoreCve({ domain, ignoredCve, newIgnoredCves }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
async () =>
await this._query`
UPSERT { _key: ${domain._key} }
INSERT ${{ ignoredCves: newIgnoredCves }}
UPDATE ${{ ignoredCves: newIgnoredCves }}
IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to ignore CVE "${ignoredCve}" on domain "${domain._key}", error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to ignore CVE. Please try again.`))
}
let currentDomainVulnerabilitiesCursor
try {
currentDomainVulnerabilitiesCursor = await trx.step(
() => this._query`
FOR finding IN additionalFindings
FILTER finding.domain == ${domain._id}
LIMIT 1
FOR wc IN finding.webComponents
FILTER LENGTH(wc.WebComponentCves) > 0
FOR vuln IN wc.WebComponentCves
FILTER vuln.Cve NOT IN ${newIgnoredCves}
RETURN DISTINCT vuln.Cve
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to ignore CVE "${ignoredCve}" on domain "${domain._key}" when getting current CVEs, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to ignore CVE. Please try again.`))
}
try {
await trx.step(
() => this._query`
UPDATE { _key: ${domain._key}, cveDetected: ${currentDomainVulnerabilitiesCursor.count > 0} } IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to ignore CVE "${ignoredCve}" on domain "${domain._key}" when updating domain, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to ignore CVE. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred when user: "${this._userKey}" attempted to ignore CVE "${ignoredCve}" on domain "${domain._key}", error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to ignore CVE. Please try again.`))
}
this.byKey.clear(domain._key)
return this.byKey.load(domain._key)
}
async unignoreCve({ domain, ignoredCve, newIgnoredCves }) {
const trx = await this._transaction(this._collections)
try {
await trx.step(
async () =>
await this._query`
UPSERT { _key: ${domain._key} }
INSERT ${{ ignoredCves: newIgnoredCves }}
UPDATE ${{ ignoredCves: newIgnoredCves }}
IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to unignore CVE "${ignoredCve}" on domain "${domain._key}", error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to stop ignoring CVE. Please try again.`))
}
let currentDomainVulnerabilitiesCursor
try {
currentDomainVulnerabilitiesCursor = await trx.step(
() => this._query`
FOR finding IN additionalFindings
FILTER finding.domain == ${domain._id}
LIMIT 1
FOR wc IN finding.webComponents
FILTER LENGTH(wc.WebComponentCves) > 0
FOR vuln IN wc.WebComponentCves
FILTER vuln.Cve NOT IN ${newIgnoredCves}
RETURN DISTINCT vuln.Cve
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to unignore CVE "${ignoredCve}" on domain "${domain._key}" when getting current CVEs, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to stop ignoring CVE. Please try again.`))
}
try {
await trx.step(
() => this._query`
UPDATE { _key: ${domain._key}, cveDetected: ${currentDomainVulnerabilitiesCursor.count > 0} } IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: "${this._userKey}" attempted to unignore CVE "${ignoredCve}" on domain "${domain._key}" when updating domain, error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to stop ignoring CVE. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred when user: "${this._userKey}" attempted to unignore CVE "${ignoredCve}" on domain "${domain._key}", error: ${err}`,
)
await trx.abort()
throw new Error(this._i18n._(t`Unable to stop ignoring CVE. Please try again.`))
}
this.byKey.clear(domain._key)
return this.byKey.load(domain._key)
}
}