forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganization.js
More file actions
400 lines (385 loc) · 12.9 KB
/
Copy pathorganization.js
File metadata and controls
400 lines (385 loc) · 12.9 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
import { t } from '@lingui/macro'
import { GraphQLBoolean, GraphQLInt, GraphQLObjectType, GraphQLString, GraphQLList, GraphQLNonNull } from 'graphql'
import { connectionArgs, globalIdField } from 'graphql-relay'
import { organizationSummaryType } from './organization-summary'
import { nodeInterface } from '../../node'
import { Acronym, Slug } from '../../scalars'
import { affiliationUserOrder } from '../../affiliation/inputs'
import { affiliationConnection } from '../../affiliation/objects'
import { domainOrder, domainFilter } from '../../domain/inputs'
import { domainConnection } from '../../domain/objects'
import { logActivity } from '../../audit-logs'
import { OrderDirection } from '../../enums'
import { tagType } from '../../tags/objects'
export const organizationType = new GraphQLObjectType({
name: 'Organization',
fields: () => ({
id: globalIdField('organization'),
acronym: {
type: Acronym,
description: 'The organizations acronym.',
resolve: ({ acronym }) => acronym,
},
name: {
type: GraphQLString,
description: 'The full name of the organization.',
resolve: ({ name }) => name,
},
slug: {
type: Slug,
description: 'Slugified name of the organization.',
resolve: ({ slug }) => slug,
},
zone: {
type: GraphQLString,
description: 'The zone which the organization belongs to.',
resolve: ({ zone }) => zone,
},
sector: {
type: GraphQLString,
description: 'The sector which the organization belongs to.',
resolve: ({ sector }) => sector,
},
country: {
type: GraphQLString,
description: 'The country in which the organization resides.',
resolve: ({ country }) => country,
},
province: {
type: GraphQLString,
description: 'The province in which the organization resides.',
resolve: ({ province }) => province,
},
city: {
type: GraphQLString,
description: 'The city in which the organization resides.',
resolve: ({ city }) => city,
},
verified: {
type: GraphQLBoolean,
description: 'Whether the organization is a verified organization.',
resolve: ({ verified }) => verified,
},
externallyManaged: {
type: GraphQLBoolean,
description: 'Whether the organization is externally managed.',
resolve: ({ externallyManaged }) => externallyManaged,
},
externalId: {
type: GraphQLString,
description: 'String ID used to identify the organization in an external system.',
resolve: ({ externalId }) => externalId,
},
availableTags: {
type: new GraphQLList(tagType),
description: '',
args: {
includeGlobal: {
type: GraphQLBoolean,
description: '',
},
includePending: {
type: GraphQLBoolean,
description: '',
},
sortDirection: {
type: new GraphQLNonNull(OrderDirection),
description: 'The direction in which to sort the data.',
},
},
resolve: async (
{ _key },
args,
{ userKey, auth: { userRequired, loginRequiredBool, verifiedRequired }, loaders: { loadTagsByOrg } },
) => {
if (loginRequiredBool) {
const user = await userRequired()
verifiedRequired({ user })
}
const orgTags = await loadTagsByOrg({
orgId: _key,
...args,
})
console.debug(`User: ${userKey} successfully retrieved their org's tags.`)
return orgTags
},
},
summaries: {
type: organizationSummaryType,
description: 'Summaries based on scan types that are preformed on the given organizations domains.',
resolve: ({ summaries }) => summaries,
},
historicalSummaries: {
type: new GraphQLList(organizationSummaryType),
description: 'Historical summaries based on scan types that are performed on the given organizations domains.',
args: {
startDate: {
type: GraphQLString,
description: 'The start date for the returned data (YYYY-MM-DD).',
},
endDate: {
type: GraphQLString,
description: 'The end date for the returned data (YYYY-MM-DD).',
},
sortDirection: {
type: OrderDirection,
description: 'The direction in which to sort the data.',
},
limit: {
type: GraphQLInt,
description: 'The maximum amount of summaries to be returned.',
},
},
resolve: async (
{ _id },
args,
{
userKey,
auth: { userRequired, loginRequiredBool, verifiedRequired },
loaders: { loadOrganizationSummariesByPeriod },
},
) => {
if (loginRequiredBool) {
const user = await userRequired()
verifiedRequired({ user })
}
const historicalSummaries = await loadOrganizationSummariesByPeriod({
orgId: _id,
...args,
})
console.info(`User: ${userKey} successfully retrieved their organization summaries.`)
return historicalSummaries
},
},
domainCount: {
type: GraphQLInt,
description: 'The number of domains associated with this organization.',
resolve: ({ domainCount }) => domainCount,
},
toCsv: {
type: GraphQLString,
description:
'CSV formatted output of all domains in the organization including their email and web scan statuses.',
args: {
filters: {
type: new GraphQLList(domainFilter),
description: 'Filters used to limit domains returned.',
},
},
resolve: async (
{ _id },
args,
{
i18n,
userKey,
query,
transaction,
collections,
request: { ip },
auth: { checkPermission, userRequired, verifiedRequired },
loaders: { loadOrganizationDomainStatuses },
},
) => {
const user = await userRequired()
verifiedRequired({ user })
const permission = await checkPermission({ orgId: _id })
if (!['user', 'admin', 'owner', 'super_admin'].includes(permission)) {
console.error(
`User "${userKey}" attempted to retrieve CSV output for organization "${_id}". Permission: ${permission}`,
)
throw new Error(t`Permission Denied: Please contact organization user for help with retrieving this domain.`)
}
const domains = await loadOrganizationDomainStatuses({
orgId: _id,
...args,
})
const headers = [
'domain',
'ipAddresses',
'https',
'hsts',
'certificates',
'protocols',
'ciphers',
'curves',
'spf',
'dkim',
'dmarc',
'phase',
'tags',
'assetState',
'rcode',
'blocked',
'wildcardSibling',
'wildcardEntry',
'hasEntrustCertificate',
'top25Vulnerabilities',
]
let csvOutput = headers.join(',')
domains.forEach((domainDoc) => {
const csvLine = headers
.map((header) => {
if (['ipAddresses', 'tags', 'top25Vulnerabilities'].includes(header)) {
return `"${domainDoc[header]?.join('|') || []}"`
}
if (
['https', 'hsts', 'certificates', 'protocols', 'ciphers', 'curves', 'spf', 'dkim', 'dmarc'].includes(
header,
)
) {
return `"${domainDoc?.status[header]}"`
}
if (header === 'phase') {
switch (domainDoc[header]) {
case 'assess':
return i18n._(t`Assess`)
case 'deploy':
return i18n._(t`Deploy`)
case 'enforce':
return i18n._(t`Enforce`)
case 'maintain':
return i18n._(t`Maintain`)
default:
return ''
}
}
return `"${domainDoc[header]}"`
})
.join(',')
csvOutput += `\n${csvLine}`
})
// Get org names to use in activity log
let orgNamesCursor
try {
orgNamesCursor = await query`
LET org = DOCUMENT(organizations, ${_id})
RETURN {
"orgNameEN": org.orgDetails.en.name,
"orgNameFR": org.orgDetails.fr.name,
}
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} attempted to export org: ${_id}. Error while creating cursor for retrieving organization names. error: ${err}`,
)
throw new Error(i18n._(t`Unable to export organization. Please try again.`))
}
let orgNames
try {
orgNames = await orgNamesCursor.next()
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} attempted to export org: ${_id}. Error while retrieving organization names. error: ${err}`,
)
throw new Error(i18n._(t`Unable to export organization. Please try again.`))
}
await logActivity({
transaction,
collections,
query,
initiatedBy: {
id: user._key,
userName: user.userName,
role: permission,
ipAddress: ip,
},
action: 'export',
target: {
resource: {
en: orgNames.orgNameEN,
fr: orgNames.orgNameFR,
},
organization: {
id: _id,
name: orgNames.orgNameEN,
}, // name of resource being acted upon
resourceType: 'organization',
},
})
return csvOutput
},
},
domains: {
type: domainConnection.connectionType,
description: 'The domains which are associated with this organization.',
args: {
orderBy: {
type: domainOrder,
description: 'Ordering options for domain connections.',
},
ownership: {
type: GraphQLBoolean,
description: 'Limit domains to those that belong to an organization that has ownership.',
},
search: {
type: GraphQLString,
description: 'String used to search for domains.',
},
filters: {
type: new GraphQLList(domainFilter),
description: 'Filters used to limit domains returned.',
},
...connectionArgs,
},
resolve: async (
{ _id },
args,
{ auth: { checkPermission }, loaders: { loadDomainConnectionsByOrgId } },
) => {
// Check to see requesting users permission to the org is
const permission = await checkPermission({ orgId: _id })
const connections = await loadDomainConnectionsByOrgId({
orgId: _id,
permission,
...args,
})
return connections
},
},
affiliations: {
type: affiliationConnection.connectionType,
description: 'Organization affiliations to various users.',
args: {
orderBy: {
type: affiliationUserOrder,
description: 'Ordering options for affiliation connections.',
},
search: {
type: GraphQLString,
description: 'String used to search for affiliated users.',
},
includePending: {
type: GraphQLBoolean,
description: 'Exclude (false) or include only (true) pending affiliations in the results.',
},
...connectionArgs,
},
resolve: async (
{ _id },
args,
{ i18n, auth: { checkPermission, loginRequiredBool }, loaders: { loadAffiliationConnectionsByOrgId } },
) => {
const permission = await checkPermission({ orgId: _id })
if (['user', 'admin', 'owner', 'super_admin'].includes(permission) === false && loginRequiredBool) {
throw new Error(i18n._(t`Cannot query affiliations on organization without admin permission or higher.`))
}
const affiliations = await loadAffiliationConnectionsByOrgId({
orgId: _id,
...args,
})
return affiliations
},
},
userHasPermission: {
type: GraphQLBoolean,
description:
'Value that determines if a user is affiliated with an organization, whether through organization affiliation, verified affiliation, or through super admin status.',
resolve: async ({ _id }, _args, { auth: { checkPermission } }) => {
const permission = await checkPermission({ orgId: _id })
return ['user', 'admin', 'super_admin', 'owner'].includes(permission)
},
},
}),
interfaces: [nodeInterface],
description: 'Organization object containing information for a given Organization.',
})