/
applicationCreation.ts
73 lines (65 loc) · 2.26 KB
/
applicationCreation.ts
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
import {
createDirectRelationship,
IntegrationStep,
IntegrationStepExecutionContext,
RelationshipClass,
} from '@jupiterone/integration-sdk-core';
import { createAPIClient } from '../client';
import { IntegrationConfig } from '../config';
import { accountFlagged } from '../okta/createOktaClient';
import { StepAnnouncer } from '../util/runningTimer';
import { Entities, Relationships, Steps } from './constants';
export async function buildUserCreatedApplication({
instance,
jobState,
logger,
}: IntegrationStepExecutionContext<IntegrationConfig>) {
let stepAnnouncer;
if (accountFlagged) {
stepAnnouncer = new StepAnnouncer(Steps.APPLICATION_CREATION, logger);
}
const apiClient = createAPIClient(instance.config, logger);
try {
await apiClient.iterateAppCreatedLogs(async (log) => {
if (!log.actor?.id || !(log.target?.length && log.target[0].id)) {
return;
}
const createdBy = log.actor.id;
const createdApp = log.target[0].id;
// Logs will contain all apps in the last 90 days, even if we've deleted them, so check before
// trying to create the relationship.
if (jobState.hasKey(createdBy) && jobState.hasKey(createdApp)) {
const createdByRelationship = createDirectRelationship({
_class: RelationshipClass.CREATED,
fromType: Entities.USER._type,
fromKey: createdBy,
toType: Entities.APPLICATION._type,
toKey: createdApp,
});
if (!jobState.hasKey(createdByRelationship._key)) {
await jobState.addRelationship(createdByRelationship);
} else {
logger.info(
{ createdByRelationship },
'Skipping relationship creation. Relationship already exists.',
);
}
}
});
} catch (err) {
logger.error({ err }, 'Error fetching app created logs');
}
if (accountFlagged) {
stepAnnouncer.finish();
}
}
export const applicationCreationSteps: IntegrationStep<IntegrationConfig>[] = [
{
id: Steps.APPLICATION_CREATION,
name: 'Build User Created Application Relationship',
entities: [],
relationships: [Relationships.USER_CREATED_APPLICATION],
dependsOn: [Steps.APPLICATIONS, Steps.USERS],
executionHandler: buildUserCreatedApplication,
},
];