This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
generated from JupiterOne-Archives/graph-template-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFalconAPIClient.ts
278 lines (258 loc) · 8.8 KB
/
FalconAPIClient.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
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
import { URLSearchParams } from 'url';
import {
DiscoverApplication,
DiscoverApplicationIdentifier,
Device,
DeviceIdentifier,
OAuth2Token,
PreventionPolicy,
QueryParams,
ResourcesResponse,
Vulnerability,
ZTA_Score,
ZeroTrustAssessment,
} from './types';
import { IntegrationLogger } from '@jupiterone/integration-sdk-core';
import { CrowdStrikeApiGateway } from './CrowdStrikeApiGateway';
export const DEFAULT_ATTEMPT_OPTIONS = {
maxAttempts: 5,
delay: 30_000,
timeout: 180_000,
factor: 2,
};
export const BUFFER_RE_AUTHETICATION_TIME = 60; //seconds
export type FalconAPIClientConfig = {
logger: IntegrationLogger;
crowdStrikeApiGateway: CrowdStrikeApiGateway;
};
export type FalconAPIResourceIterationCallback<T> = (
resources: T[],
) => boolean | void | Promise<boolean | void>;
export class FalconAPIClient {
private logger: IntegrationLogger;
private crowdStrikeApiGateway: CrowdStrikeApiGateway;
constructor({ logger, crowdStrikeApiGateway }: FalconAPIClientConfig) {
this.logger = logger;
this.crowdStrikeApiGateway = crowdStrikeApiGateway;
}
public async authenticate(): Promise<OAuth2Token> {
return this.crowdStrikeApiGateway.authenticate();
}
/**
* Iterates the detected devices by listing the AIDs and then fetching the
* device details, providing pages of the collection to the provided callback.
*
* The scroll API is used because it has no limitation on the number of
* records it will return. However, note the scroll offset value expires after
* 2 minutes. The device details request time combined with the callback
* processing time, per page, must be less.
*
* @returns Promise
*/
public async iterateDevices(input: {
callback: FalconAPIResourceIterationCallback<Device>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<DeviceIdentifier>({
callback: async (deviceIds) => {
if (deviceIds.length) {
// If the scroll lists _no_ recent devices, we don't want to send a malformed request to https://api.crowdstrike.com/devices/entities/devices/v1?
return input.callback(await this.fetchDevices(deviceIds));
}
},
query: input.query,
resourcePath: '/devices/queries/devices-scroll/v1',
});
}
/**
* Iterates through hidden devices.
* Beta - Docs are unclear how hidden devices are different than devices.
* @param input
*/
public async iterateHiddenDevices(input: {
callback: FalconAPIResourceIterationCallback<Device>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<DeviceIdentifier>({
callback: async (deviceIds) => {
if (deviceIds.length) {
this.logger.info(
{ hiddenDevicesCount: deviceIds.length },
`Found hidden devices.`,
);
// If the scroll lists _no_ recent devices, we don't want to send a malformed request to https://api.crowdstrike.com/devices/entities/devices/v1?
return input.callback(await this.fetchDevices(deviceIds));
}
},
query: input.query,
resourcePath: '/devices/queries/devices-hidden/v1',
});
}
/**
* Iterates the known device vulnerabilities, providing pages
* of the collection based on the provided query to the provided callback.
*
* @param input
* @returns Promise
*/
public async iterateVulnerabilities(input: {
callback: FalconAPIResourceIterationCallback<Vulnerability>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<Vulnerability>({
callback: input.callback,
query: input.query,
resourcePath: '/spotlight/combined/vulnerabilities/v1',
});
}
/**
* Iterates the known ZTA Scores
* https://falconpy.io/Service-Collections/Zero-Trust-Assessment.html#getassessmentsbyscorev1
* @param input
* @returns Promise
*/
public async iterateZeroTrustAssessment(input: {
callback: FalconAPIResourceIterationCallback<ZeroTrustAssessment>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<ZTA_Score>({
query: input.query,
callback: async (ztaIdScores) => {
let ids: string[] = [];
if (ztaIdScores.length) ids = ztaIdScores.map((score) => score.aid);
const chunkSize = 25; // This is not strictly necessary, but should make it faster, since we would have x1/40 calls
for (let i = 0; i < ids.length; i += chunkSize) {
await input.callback(
await this.fetchZTADetails(ids.slice(i, i + chunkSize)),
);
}
},
resourcePath: '/zero-trust-assessment/queries/assessments/v1',
});
}
/**
* Iterates the known device applications, providing pages
* of the collection based on the provided query to the provided callback.
*
* @param input
* @returns Promise
*/
public async iterateApplications(input: {
callback: FalconAPIResourceIterationCallback<DiscoverApplication>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<DiscoverApplicationIdentifier>(
{
callback: async (appsIds) => {
if (appsIds.length) {
return input.callback(await this.fetchApplications(appsIds));
}
},
query: input.query,
resourcePath: '/discover/queries/applications/v1',
},
);
}
/**
* Iterates prevention policies using the "combined" API, providing pages of
* the collection to the provided callback.
*
* @returns Promise
*/
public async iteratePreventionPolicies(input: {
callback: FalconAPIResourceIterationCallback<PreventionPolicy>;
query?: QueryParams;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<PreventionPolicy>({
callback: input.callback,
query: input.query,
resourcePath: '/policy/combined/prevention/v1',
});
}
/**
* Iterates prevention policy member ids, providing pages of the collection
* to the provided callback. Based on the provided policy id.
* @param input
*/
public async iteratePreventionPolicyMemberIds(input: {
query?: QueryParams;
callback: FalconAPIResourceIterationCallback<DeviceIdentifier>;
policyId: string;
}): Promise<void> {
return this.crowdStrikeApiGateway.paginateResources<DeviceIdentifier>({
callback: input.callback,
resourcePath: '/policy/queries/prevention-members/v1',
query: {
...input.query,
id: input.policyId,
},
});
}
private async fetchDevices(ids: string[]): Promise<Device[]> {
const availabilityZone = this.crowdStrikeApiGateway.getAvailabilityZone();
const response =
await this.crowdStrikeApiGateway.executeAPIRequestWithRetries<
ResourcesResponse<Device>
>(
`https://api.${availabilityZone}crowdstrike.com/devices/entities/devices/v2`,
{
method: 'POST',
body: JSON.stringify({ ids }),
headers: {
'Content-Type': 'application/json',
accept: 'application/json',
},
},
);
return response.resources;
}
/***
* ZTA details
* https://falconpy.io/Service-Collections/Zero-Trust-Assessment.html#getassessmentv1
*/
private async fetchZTADetails(ids: string[]): Promise<ZeroTrustAssessment[]> {
const searchParams = new URLSearchParams();
for (const id of ids) {
searchParams.append('ids', id);
}
const availabilityZone = this.crowdStrikeApiGateway.getAvailabilityZone();
const response =
await this.crowdStrikeApiGateway.executeAPIRequestWithRetries<
ResourcesResponse<ZeroTrustAssessment>
>(
`https://api.${availabilityZone}crowdstrike.com/zero-trust-assessment/entities/assessments/v1?` +
searchParams,
{
method: 'GET',
headers: {
accept: 'application/json',
},
},
);
return response.resources;
}
/**
* Discover Service - applications.
* Swagger: https://assets.falcon.us-2.crowdstrike.com/support/api/swagger-us2.html#/discover/get-applications
*/
private async fetchApplications(
ids: string[],
): Promise<DiscoverApplication[]> {
const availabilityZone = this.crowdStrikeApiGateway.getAvailabilityZone();
const queryParams = ids.map((id) => `ids=${id}`).join('&');
const response =
await this.crowdStrikeApiGateway.executeAPIRequestWithRetries<
ResourcesResponse<DiscoverApplication>
>(
`https://api.${availabilityZone}crowdstrike.com/discover/entities/applications/v1?${queryParams}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
accept: 'application/json',
},
},
);
return response.resources;
}
}