-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathintegration_reader.ts
487 lines (448 loc) · 15.7 KB
/
integration_reader.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
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import path from 'path';
import semver from 'semver';
import { validateTemplate } from '../validators';
import { FileSystemDataAdaptor } from './fs_data_adaptor';
import { CatalogDataAdaptor, IntegrationPart } from './catalog_data_adaptor';
import { foldResults, pruneConfig } from './utils';
/**
* The Integration class represents the data for Integration Templates.
* It is backed by the repository file system.
* It includes accessor methods for integration configs, as well as helpers for nested components.
*/
export class IntegrationReader {
reader: CatalogDataAdaptor;
directory: string;
name: string;
constructor(directory: string, reader?: CatalogDataAdaptor) {
this.directory = directory;
this.name = path.basename(directory);
this.reader = reader ?? new FileSystemDataAdaptor(directory);
}
/**
* Retrieve data from correct source regardless of if reader is config-localized or not.
*
* TODO refactor to assemble filename from `type` instead of requiring caller to format it.
*
* @param item An item which may have data in it.
* @param fileParams Information about the file to read if the config is not localized.
* @param format How to package the returned data.
* If 'json', return `object | object[]`. If 'binary', return `Buffer`.
* @returns A result with the data, with a format based on the format field.
*/
private async fetchDataOrReadFile(
item: { data?: string },
fileParams: { filename: string; type?: IntegrationPart },
format: 'json'
): Promise<Result<object | object[]>>;
private async fetchDataOrReadFile(
item: { data?: string },
fileParams: { filename: string; type?: IntegrationPart },
format: 'binary'
): Promise<Result<Buffer>>;
private async fetchDataOrReadFile(
item: { data?: string },
fileParams: { filename: string; type?: IntegrationPart },
format: 'json' | 'binary'
): Promise<Result<object | object[] | Buffer>> {
if (this.reader.isConfigLocalized) {
if (!item.data) {
return {
ok: false,
error: new Error(
'The config for the provided reader is localized, but no data field is present. ' +
JSON.stringify(item)
),
};
}
try {
if (format === 'json') {
return { ok: true, value: JSON.parse(item.data) };
} else {
return { ok: true, value: Buffer.from(item.data, 'base64') };
}
} catch (error) {
return { ok: false, error };
}
}
if (format === 'json') {
return this.reader.readFile(fileParams.filename, fileParams.type);
} else {
return this.reader.readFileRaw(fileParams.filename, fileParams.type);
}
}
private async readAsset(
asset: IntegrationAsset | SerializedIntegrationAsset
): Promise<Result<SerializedIntegrationAsset>> {
const filename = `${asset.name}-${asset.version}.${asset.extension}`;
const fileParams = { filename, type: 'assets' as const };
if (['json', 'ndjson'].includes(asset.extension)) {
const maybeObject = await this.fetchDataOrReadFile(
asset as { data?: string },
fileParams,
'json'
);
if (!maybeObject.ok) {
return maybeObject;
}
return { ok: true, value: { ...asset, data: JSON.stringify(maybeObject.value) } };
} else {
const maybeBuffer = await this.fetchDataOrReadFile(
asset as { data?: string },
fileParams,
'binary'
);
if (!maybeBuffer.ok) {
return maybeBuffer;
}
return {
ok: true,
value: { ...asset, data: maybeBuffer.value.toString('utf8') },
};
}
}
/**
* Get the latest version of the integration available.
* This method relies on the fact that integration configs have their versions in their name.
* Any files that don't match the config naming convention will be ignored.
*
* @returns A string with the latest version, or null if no versions are available.
*/
async getLatestVersion(): Promise<string | null> {
const versions = await this.reader.findIntegrationVersions();
if (!versions.ok) {
return null;
}
if (versions.value.length === 0) {
return null;
}
// Sort descending
versions.value.sort(semver.rcompare);
return versions.value[0];
}
// Get config without pruning or validation.
private async getRawConfig(
version?: string
): Promise<Result<IntegrationConfig | SerializedIntegration>> {
if ((await this.reader.getDirectoryType()) !== 'integration') {
return {
ok: false,
error: new Error(`${this.directory} is not a valid integration directory`),
};
}
const maybeVersion: string | null = version ? version : await this.getLatestVersion();
if (maybeVersion === null) {
return {
ok: false,
error: new Error(`No valid config matching version ${version} is available`),
};
}
const configFile = `${this.name}-${maybeVersion}.json`;
// Even config-localized readers must support config-read.
const config = await this.reader.readFile(configFile);
if (!config.ok) {
return config;
}
return validateTemplate(config.value);
}
/**
* Get the configuration of the current integration.
*
* @param version The version of the config to retrieve.
* @returns The config if a valid config matching the version is present, otherwise null.
*/
async getConfig(version?: string): Promise<Result<IntegrationConfig>> {
const maybeConfig = await this.getRawConfig(version);
if (!maybeConfig.ok) {
return maybeConfig;
}
return validateTemplate(pruneConfig(maybeConfig.value));
}
/**
* Retrieve assets associated with the integration.
* This method greedily retrieves all assets.
* If an asset is invalid, an error result is returned.
*
* @param version The version of the integration to retrieve assets for.
* @returns A result containing the parsed assets.
*/
async getAssets(version?: string): Promise<Result<ParsedIntegrationAsset[]>> {
const configResult = await this.getRawConfig(version);
if (!configResult.ok) {
return configResult;
}
const config = configResult.value;
const resultValue: ParsedIntegrationAsset[] = [];
for (const asset of config.assets) {
const serializedResult = await this.readAsset(asset);
if (!serializedResult.ok) {
return serializedResult;
}
switch (asset.type) {
case 'savedObjectBundle':
resultValue.push({
type: 'savedObjectBundle',
workflows: asset.workflows,
data: JSON.parse(serializedResult.value.data),
});
break;
case 'query':
resultValue.push({
type: 'query',
workflows: asset.workflows,
query: serializedResult.value.data,
language: asset.extension,
});
break;
}
}
return { ok: true, value: resultValue };
}
/**
* Retrieve sample data associated with the integration.
* If the version is invalid, an error is thrown.
* If the sample data is invalid, null will be returned
*
* @param version The version of the integration to retrieve assets for.
* @returns An object containing a list of sample data with adjusted timestamps.
*/
async getSampleData(
version?: string
): Promise<
Result<{
sampleData: object[] | null;
}>
> {
const configResult = await this.getRawConfig(version);
if (!configResult.ok) {
return configResult;
}
const config = configResult.value;
const resultValue: { sampleData: object[] | null } = { sampleData: null };
if (config.sampleData) {
const jsonContent: Result<object | object[]> = await this.fetchDataOrReadFile(
config.sampleData as { data?: string },
{ filename: config.sampleData.path, type: 'data' },
'json'
);
if (!jsonContent.ok) {
return jsonContent;
}
for (const value of jsonContent.value as object[]) {
if (!('@timestamp' in value)) {
continue;
}
// Randomly scatter timestamps across last 10 minutes
// Assume for now that the ordering of events isn't important, can change to a sequence if needed
// Also doesn't handle fields like `observedTimestamp` if present
const newTime = new Date(
Date.now() - Math.floor(Math.random() * 1000 * 60 * 10)
).toISOString();
Object.assign(value, { '@timestamp': newTime });
if ('observedTimestamp' in value) {
Object.assign(value, { observedTimestamp: newTime });
}
}
resultValue.sampleData = jsonContent.value as object[];
}
return { ok: true, value: resultValue };
}
/**
* Retrieve schema data associated with the integration.
* This method greedily retrieves all mappings and schemas.
* It's assumed that a valid version will be provided.
* If the version is invalid, an error is thrown.
* If a schema is invalid, an error will be thrown.
*
* @param version The version of the integration to retrieve assets for.
* @returns An object containing the different types of assets.
*/
async getSchemas(
version?: string
): Promise<
Result<{
mappings: { [key: string]: unknown };
}>
> {
const configResult = await this.getRawConfig(version);
if (!configResult.ok) {
return configResult;
}
const config = configResult.value;
const resultValue: { mappings: { [key: string]: object } } = {
mappings: {},
};
for (const component of config.components) {
const schemaFile = `${component.name}-${component.version}.mapping.json`;
const schema = await this.fetchDataOrReadFile(
component as { data?: string },
{ filename: schemaFile, type: 'schemas' },
'json'
);
if (!schema.ok) {
return schema;
}
resultValue.mappings[component.name] = schema.value;
}
return { ok: true, value: resultValue };
}
/**
* Retrieves the data for a static file associated with the integration.
*
* @param staticPath The path of the static to retrieve.
* @returns A buffer with the static's data if present, otherwise null.
*/
async getStatic(staticPath: string): Promise<Result<Buffer>> {
// Statics were originally designed to read straight from file system,
// so we use direct access if possible.
if (!this.reader.isConfigLocalized) {
return await this.reader.readFileRaw(staticPath, 'static');
}
// Otherwise, we need to search for the right static, by checking each version.
const versions = await this.reader.findIntegrationVersions();
if (!versions.ok) {
return versions;
}
for (const version of versions.value) {
const config = await this.getRawConfig(version);
if (!config.ok || !config.value.statics) {
continue;
}
const statics = config.value.statics;
if (statics.logo?.path === staticPath) {
if (!('data' in statics.logo)) {
return { ok: false, error: new Error('Localized config missing static data') };
}
return { ok: true, value: Buffer.from((statics.logo as { data: string }).data, 'base64') };
}
if (statics?.darkModeLogo?.path === staticPath) {
if (!('data' in statics.darkModeLogo)) {
return { ok: false, error: new Error('Localized config missing static data') };
}
return {
ok: true,
value: Buffer.from((statics.darkModeLogo as { data: string }).data, 'base64'),
};
}
for (const iterStatic of [...(statics?.gallery ?? []), ...(statics?.darkModeGallery ?? [])]) {
if (iterStatic.path === staticPath) {
if (!('data' in iterStatic)) {
return { ok: false, error: new Error('Localized config missing static data') };
}
return { ok: true, value: Buffer.from((iterStatic as { data: string }).data, 'base64') };
}
}
}
return {
ok: false,
error: new Error(`Static not found: ${staticPath}`, { code: 'ENOENT' } as ErrorOptions),
};
}
private async serializeStaticAsset(asset: StaticAsset): Promise<Result<SerializedStaticAsset>> {
const data = await this.getStatic(asset.path);
if (!data.ok) {
return data;
}
return {
ok: true,
value: {
...asset,
data: data.value.toString('base64'),
},
};
}
private async serializeStatics(
statics: IntegrationStatics
): Promise<Result<SerializedIntegrationStatics>> {
const serialized: SerializedIntegrationStatics = {};
if (statics.logo) {
const serializeResult = await this.serializeStaticAsset(statics.logo);
serialized.logo = serializeResult.value;
}
if (statics.darkModeLogo) {
const serializeResult = await this.serializeStaticAsset(statics.darkModeLogo);
serialized.darkModeLogo = serializeResult.value;
}
if (statics.gallery) {
const results = await Promise.all(
statics.gallery.map((asset) => this.serializeStaticAsset(asset))
);
const foldedResult = foldResults(results);
serialized.gallery = foldedResult.value;
}
if (statics.darkModeGallery) {
const results = await Promise.all(
statics.darkModeGallery.map((asset) => this.serializeStaticAsset(asset))
);
const foldedResult = foldResults(results);
serialized.darkModeGallery = foldedResult.value;
}
return {
ok: true,
value: serialized,
};
}
/**
* Serialize the referenced integration as a flat JSON object.
* Useful for normalizing the format for sending to other locations.
* This method implements the serialization scheme expected by `JsonCatalogDataAdaptor`.
*
* @param version The version of the integration to serialize.
* @returns A large object which includes all of the integration's data.
*/
async serialize(version?: string): Promise<Result<SerializedIntegration>> {
const configResult = await this.getRawConfig(version);
if (!configResult.ok) {
return configResult;
}
// Type cast safety: all serializable properties must have the 'data' field.
// The remainder of the method is populating all such fields.
const config = configResult.value as SerializedIntegration;
const componentResults = await Promise.all(
config.components.map((component) =>
this.fetchDataOrReadFile(
component,
{ filename: `${component.name}-${component.version}.mapping.json`, type: 'schemas' },
'json'
)
)
);
const componentsResult = foldResults(componentResults);
if (!componentsResult.ok) {
return componentsResult;
}
config.components = config.components.map((component, idx) => {
return {
...component,
data: JSON.stringify(componentsResult.value[idx]),
};
});
const assetResults = await Promise.all(config.assets.map((asset) => this.readAsset(asset)));
const assets = foldResults(assetResults);
if (!assets.ok) {
return assets;
}
config.assets = assets.value;
if (config.statics) {
const staticsResult = await this.serializeStatics(config.statics);
if (!staticsResult.ok) {
return staticsResult;
}
config.statics = staticsResult.value;
}
if (config.sampleData) {
const dataResult = await this.getSampleData(version);
if (!dataResult.ok) {
return dataResult;
}
config.sampleData = {
...config.sampleData,
data: JSON.stringify(dataResult.value.sampleData),
};
}
return { ok: true, value: config };
}
}