forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign-organizations-domains.js
More file actions
254 lines (213 loc) · 6.86 KB
/
Copy pathalign-organizations-domains.js
File metadata and controls
254 lines (213 loc) · 6.86 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
const { removeEdges } = require('./helpers')
const { addOrganizationsDomains } = require('./add-organizations-domains')
const alignOrganizationsDomains = async ({ db, query, data }) => {
// Check if organizations in database are in JSON data
// remove organization, affiliations, user affiliations, claims, ownerships if not in JSON data
const organizationsInDb = await (
await query`FOR org IN organizations RETURN org`
).all()
for (const organization of organizationsInDb) {
console.log(
`Checking database organization "${organization.orgDetails.en.name}" against JSON data`,
)
if (['super-admin', 'sa'].includes(organization.orgDetails.en.slug)) {
console.log(
`Super admin organization "${organization.orgDetails.en.name}" selected, skipping`,
)
continue
}
const orgFoundInJsonData = Object.keys(data).find((organizationKey) => {
return (
data[organizationKey].acronym_en === organization.orgDetails.en.acronym
)
})
if (orgFoundInJsonData) {
// Organization found, continue
console.log(
`Organization "${organization.orgDetails.en.name}" found in JSON data, skipping`,
)
continue
}
// <----------------- Organization not found, remove organization and all edges for org ---------------->
console.log(
`Organization "${organization.orgDetails.en.name}" not found in JSON data, removing`,
)
// remove claims
await removeEdges({
db,
vertexSelectorId: organization._id,
direction: 'ANY',
edgeCollection: 'claims',
})
console.log(`Claims removed from "${organization.orgDetails.en.name}"`)
// remove ownerships
await removeEdges({
db,
vertexSelectorId: organization._id,
direction: 'ANY',
edgeCollection: 'ownership',
})
console.log(
`Organizations removed from "${organization.orgDetails.en.name}"`,
)
// remove affiliations
await removeEdges({
db,
vertexSelectorId: organization._id,
direction: 'ANY',
edgeCollection: 'affiliations',
})
console.log(
`Affiliations removed from "${organization.orgDetails.en.name}"`,
)
// remove organization
await (await query`REMOVE ${organization._key} IN organizations`).all()
console.log(
`Organization "${organization.orgDetails.en.name}" removed from database`,
)
}
console.log('Done handling organization removal')
// <------------------------------ done removing organizations ------------------------------------>
// Check if domains in database are in JSON data
// remove domain, claims, ownership, scan data, summaries, etc if not in JSON data
console.log('Starting domain/claim removal')
// loop over domains in DB
const domainsInDb = await (
await query`FOR domain IN domains RETURN domain`
).all()
// format to:
// { domain1: ['ORG-ACRO1'], domain2: ['ORG-ACRO1'] }
const orgsClaimingDomainAcronyms = Object.keys(data).reduce(
(prev, currentKey) => {
data[currentKey].domains.forEach((domain) => {
if (prev[domain] == null) {
prev[domain] = [data[currentKey].acronym_en]
return
}
prev[domain].push(data[currentKey].acronym_en)
})
return prev
},
{},
)
for (const domain of domainsInDb) {
// remove domain claims which don't exist in JSON data
const claimRemovalParams = {
domainId: domain._id,
}
const jsonOrgDomainClaimsString =
JSON.stringify(orgsClaimingDomainAcronyms[domain.domain]) ||
JSON.stringify([])
await (
await db.query(
`
WITH organizations
FOR v, e IN 1..1 ANY @domainId claims
FILTER v.orgDetails.en.acronym NOT IN ${jsonOrgDomainClaimsString}
REMOVE e IN claims`,
claimRemovalParams,
)
).all()
if (orgsClaimingDomainAcronyms[domain.domain]) {
// Domain found, don't delete
console.log(
`Claims for domain "${domain.domain}" found in JSON data, not deleting domain`,
)
delete orgsClaimingDomainAcronyms[domain.domain]
continue
}
// <-------------------- Domain not found, remove the follow: ---------------------->
// remove:
// claims edges
// ownership edges
// domainsDKIM edges and vertices
// domainsDMARC edges and vertices
// domainsHTTPS edges and vertices
// domainsSPF edges and vertices
// domainsSSL edges and vertices
// domainsToDmarcSummaries edges and vertices
// the domain itself
console.log(
`Domain "${domain.domain}" not found in JSON data, removing domain and domain data`,
)
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'claims',
direction: 'ANY',
})
console.log('claims removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'ownership',
direction: 'ANY',
})
console.log('ownership removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsToDmarcSummaries',
direction: 'ANY',
vertexCollection: 'dmarcSummaries',
removeVertices: true,
})
console.log('dmarcSummaries removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsSSL',
direction: 'ANY',
vertexCollection: 'ssl',
removeVertices: true,
})
console.log('ssl removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsSPF',
direction: 'ANY',
vertexCollection: 'spf',
removeVertices: true,
})
console.log('spf removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsHTTPS',
direction: 'ANY',
vertexCollection: 'https',
removeVertices: true,
})
console.log('https removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsDMARC',
direction: 'ANY',
vertexCollection: 'dmarc',
removeVertices: true,
})
console.log('dmarc removed')
await removeEdges({
db,
vertexSelectorId: domain._id,
edgeCollection: 'domainsDKIM',
direction: 'ANY',
vertexCollection: 'dkim',
removeVertices: true,
})
console.log('dkim removed')
await (await query`REMOVE ${domain._key} IN domains`).all()
console.log('domain removed')
}
// <------------------------------ done removing domains ------------------------------------>
// Add organizations and domains from JSON data
// addOrganizationsDomains() can add new organizations and domains, as well as add
// claims to newly created or already existing organizations/domains
// Add organizations and domains
await addOrganizationsDomains({ db, query, data })
}
module.exports = {
alignOrganizationsDomains,
}