Repository navigation
Expand file tree
/
Copy pathvalidateIntegrationManifests.js
More file actions
637 lines (599 loc) · 21.5 KB
/
Copy pathvalidateIntegrationManifests.js
File metadata and controls
637 lines (599 loc) · 21.5 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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
const { existsSync, readFileSync } = require('fs');
const Handlebars = require('handlebars');
const Ajv = require('ajv');
const _ = require('lodash');
const { getManifests } = require('./__utils__');
const jsonEscape = require('../utils/json-escape');
const schema = require('../manifest.schema.json');
const flagContext = require('../sample-context/flag/update-all-environments');
const projectContext = require('../sample-context/project/update');
const environmentContext = require('../sample-context/environment/update');
const memberContext = require('../sample-context/member/update');
const OAUTH_INTEGRATIONS = [
'appdynamics',
'sentry',
'servicenow',
'servicenow-normal',
'servicenow-app',
'custom-approvals-oauth',
]; // add oauth integrations here
const INTEGRATIONS_WITH_DUP_OAUTH_KEYS = [
'servicenow',
'servicenow-normal',
'servicenow-app',
]; // these are integrations we expect to have duplicate oauth integration keys for various reasons
var parse = require('url-parse');
const getFormVariableContext = formVariables => {
const endpointContext = {};
if (formVariables) {
formVariables.forEach(formVariable => {
switch (formVariable.type) {
case 'string':
endpointContext[formVariable.key] = formVariable.key;
break;
case 'boolean':
endpointContext[formVariable.key] = true;
break;
case 'uri':
endpointContext[formVariable.key] = `https://${formVariable.key}.com`;
break;
case 'enum':
endpointContext[formVariable.key] = formVariable.key;
break;
case 'dynamicEnum':
endpointContext[formVariable.key] = formVariable.key;
break;
case 'generated':
endpointContext[formVariable.key] = formVariable.key;
break;
}
});
}
return endpointContext;
};
const getOAuthContext = requiresOAuth => {
if (requiresOAuth) {
const oauthContext = {
baseURI: '$OAUTH_BASE_URI',
accessToken: '$OAUTH_ACCESS_TOKEN',
};
return oauthContext;
}
return null;
};
const isJSONTemplate = templateFilename => {
const lowercase = templateFilename.toLowerCase();
return lowercase.endsWith('.json') || lowercase.endsWith('.json.hbs');
};
const manifests = getManifests();
const ajv = new Ajv();
const validate = ajv.compile(schema);
const getTemplate = path => {
const templateString = readFileSync(path, { encoding: 'utf-8' });
const template = Handlebars.compile(templateString, {
strict: true,
});
return template;
};
const getFullContext = (manifest, context, isJSON) => {
const formVariables = _.get(manifest, 'formVariables', null);
const requiresOAuth = _.get(manifest, 'requiresOAuth', null);
const endpointContext = getFormVariableContext(formVariables);
endpointContext.oauth = getOAuthContext(requiresOAuth);
const fullContext = isJSON
? jsonEscape(Object.assign({}, context))
: Object.assign({}, context);
fullContext.formVariables = endpointContext;
return fullContext;
};
describe('All integrations', () => {
test.each(manifests)('Validate %s/manifest.json', (key, manifest) => {
const res = validate(manifest);
expect(validate.errors).toBe(null);
expect(res).toBe(true);
});
test.each(manifests)('Icons exist for %s', (key, manifest) => {
expect(existsSync(`./integrations/${key}/${manifest.icons.square}`)).toBe(
true
);
expect(
existsSync(`./integrations/${key}/${manifest.icons.horizontal}`)
).toBe(true);
});
test.each(manifests)('Overview ends in a period for %s', (key, manifest) => {
expect(_.get(manifest, 'overview', '').endsWith('.')).toBe(true);
});
test.each(manifests)(
'Referenced form variables exist for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
const requiresOAuth = _.get(manifest, 'requiresOAuth', null);
const endpoint = _.get(
manifest,
'capabilities.auditLogEventsHook.endpoint',
null
);
if (endpoint) {
const endpointContext = getFormVariableContext(formVariables);
endpointContext.context = flagContext;
endpointContext.oauth = getOAuthContext(requiresOAuth);
const urlTemplate = Handlebars.compile(endpoint.url, {
strict: true,
});
expect(
() => urlTemplate(endpointContext),
`${key}: request URL template must render successfully`
).not.toThrow();
endpoint.headers.forEach(header => {
const headerTemplate = Handlebars.compile(header.value, {
strict: true,
});
expect(
() => headerTemplate(endpointContext),
`${key}: request header (${header.name}) value template must render successfully`
).not.toThrow();
});
const hmacSignatureConfig = _.get(
manifest,
'capabilities.auditLogEventsHook.endpoint.hmacSignature',
null
);
if (hmacSignatureConfig) {
let secretField;
for (const formVariable of formVariables) {
if (
formVariable.key === hmacSignatureConfig.hmacSecretFormVariableKey
) {
secretField = formVariable;
}
}
expect(secretField).not.toBe(undefined);
expect(secretField.type).toBe('string');
expect(secretField.isSecret).toBe(true);
}
}
}
);
test.each(manifests)(
'OAuth parameters are correctly set and accessible for %s',
(key, manifest) => {
const requiresOAuth = _.get(manifest, 'requiresOAuth', null);
if (requiresOAuth) {
expect(OAUTH_INTEGRATIONS).toEqual(expect.arrayContaining([key]));
const oauthContext = getOAuthContext(requiresOAuth);
expect(oauthContext).not.toBe(null);
}
}
);
test('oauthIntegrationKey must be globally unique across manifests', async () => {
const allOAuthKeys = manifests
.map(
([key, manifest]) =>
!INTEGRATIONS_WITH_DUP_OAUTH_KEYS.includes(key) &&
_.get(manifest, 'oauthIntegrationKey', null)
)
.filter(key => !!key);
const isArrayUnique = arr => new Set(arr).size === arr.length;
expect(isArrayUnique(allOAuthKeys)).toBeTruthy();
});
test.each(manifests)(
'includeErrorResponseBody is only true if endpoint domain is static for %s',
(key, manifest) => {
const includeErrorResponseBody = _.get(
manifest,
'capabilities.auditLogEventsHook.includeErrorResponseBody',
null
);
// if there is no host domain replacement in the `endpoint` field then we are fine
const endpointUrlTemplate = _.get(
manifest,
'capabilities.auditLogEventsHook.endpoint.url',
null
);
if (endpointUrlTemplate && includeErrorResponseBody) {
if (endpointUrlTemplate.startsWith('{{')) {
const varName = endpointUrlTemplate.substring(
2,
endpointUrlTemplate.indexOf('}')
);
// if there is and that variable is an enum or has some form of validation, we are fine
const formVariables = _.get(manifest, 'formVariables', null);
for (let i = 0; i < formVariables.length; i++) {
if (formVariables[i].key == varName) {
expect(formVariables[i].type).toBe('enum');
expect(formVariables[i].allowedValues).not.toBe(null);
}
}
}
// we also want to check that there are no dynamic subdomains, ex. signalfx
const hostname = parse(endpointUrlTemplate, true).hostname;
if (hostname.includes('{{')) {
const regexp = /{{(\w+)}}/g;
const dynamicHostVars = [...hostname.matchAll(regexp)];
const formVariables = _.get(manifest, 'formVariables', null);
for (let i = 0; i < formVariables.length; i++) {
for (let j = 0; j < dynamicHostVars.length; j++) {
if (formVariables[i].key.lower() == dynamicHostVars[j][1]) {
expect(formVariables[i].type).toBe('enum');
expect(formVariables[i].allowedValues).not.toBe(null);
}
}
}
}
}
}
);
test.each(manifests)(
'defaultValue types match the type of the formVariable for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (!_.isUndefined(formVariable.defaultValue)) {
expect(
formVariable.isOptional,
'"defaultValue" is only valid if "isOptional" is true. Use "placeholder" if you do not want the variable to be optional.'
).toBe(true);
if (
formVariable.type === 'string' ||
formVariable.type === 'uri' ||
formVariable.type === 'enum' ||
formVariable.type === 'dynamicEnum'
) {
expect(_.isString(formVariable.defaultValue)).toBe(true);
} else if (formVariable.type === 'boolean') {
expect(_.isBoolean(formVariable.defaultValue)).toBe(true);
}
}
});
}
}
);
test.each(manifests)(
'defaultValue types match the type of the approvalFormVariable for %s',
(key, manifest) => {
const formVariables = _.get(
manifest,
'capabilities.approval.approvalFormVariables',
null
);
if (formVariables) {
formVariables.forEach(formVariable => {
if (!_.isUndefined(formVariable.defaultValue)) {
expect(
formVariable.isOptional,
'"defaultValue" is only valid if "isOptional" is true. Use "placeholder" if you do not want the variable to be optional.'
).toBe(true);
if (
formVariable.type === 'string' ||
formVariable.type === 'uri' ||
formVariable.type === 'enum' ||
formVariable.type === 'dynamicEnum'
) {
expect(_.isString(formVariable.defaultValue)).toBe(true);
} else if (formVariable.type === 'boolean') {
expect(_.isBoolean(formVariable.defaultValue)).toBe(true);
}
}
});
}
}
);
test.each(manifests)(
'defaultValue types match the type of the environmentFormVariable for %s',
(key, manifest) => {
const formVariables = _.get(
manifest,
'capabilities.approval.environmentFormVariables',
null
);
if (formVariables) {
formVariables.forEach(formVariable => {
if (!_.isUndefined(formVariable.defaultValue)) {
expect(
formVariable.isOptional,
'"defaultValue" is only valid if "isOptional" is true. Use "placeholder" if you do not want the variable to be optional.'
).toBe(true);
if (
formVariable.type === 'string' ||
formVariable.type === 'uri' ||
formVariable.type === 'enum' ||
formVariable.type === 'dynamicEnum'
) {
expect(_.isString(formVariable.defaultValue)).toBe(true);
} else if (formVariable.type === 'boolean') {
expect(_.isBoolean(formVariable.defaultValue)).toBe(true);
}
}
});
}
}
);
test.each(manifests)(
'defaultValue is always provided when isOptional true for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (
formVariable.isOptional &&
formVariable.type !== 'multiselect' &&
formVariable.type !== 'environmentSelector'
) {
expect(
formVariable.defaultValue,
'"isOptional" is only valid if "defaultValue" is provided.'
).toBeDefined();
}
});
}
}
);
test.each(manifests)(
'unrelated properties are not set on multiselect formVariables for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (formVariable.type === 'multiselect') {
expect(formVariable.defaultValue).not.toBeDefined();
expect(formVariable.dynamicOptions).not.toBeDefined();
expect(formVariable.allowedValues).not.toBeDefined();
expect(formVariable.isSecret).not.toBeDefined();
}
});
}
}
);
test.each(manifests)(
'multiselectOptions is always provided when type is multiselect for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (formVariable.type === 'multiselect') {
expect(formVariable.multiselectOptions).toBeDefined();
}
});
}
}
);
test.each(manifests)(
'unrelated properties are not set on environmentSelector formVariables for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (formVariable.type === 'environmentSelector') {
expect(formVariable.multiselectOptions).not.toBeDefined();
expect(formVariable.multiselectDefaultOptions).not.toBeDefined();
expect(formVariable.defaultValue).not.toBeDefined();
expect(formVariable.dynamicOptions).not.toBeDefined();
expect(formVariable.allowedValues).not.toBeDefined();
expect(formVariable.isSecret).not.toBeDefined();
}
});
}
}
);
test.each(manifests)(
'no non-string formVariables have been set to isSecret for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (formVariable.type != 'string') {
expect(formVariable.isSecret).not.toBe(true);
}
});
}
}
);
test.each(manifests)(
'dynamicOptions is specified on dynamicEnum formVariable fields for %s',
(key, manifest) => {
const formVariables = _.get(manifest, 'formVariables', null);
if (formVariables) {
formVariables.forEach(formVariable => {
if (formVariable.type == 'dynamicEnum') {
expect(formVariable.dynamicOptions).toBeDefined();
}
if (formVariable.dynamicOptions) {
expect(formVariable.type).toBe('dynamicEnum');
}
});
}
}
);
test.each(manifests)(
'at least one externalCapability has been defined if no manifest capabilities for %s',
(key, manifest) => {
const integrationsWithNoCapabilities = ['aiconfig-test-run'];
const capabilities = _.get(manifest, 'capabilities', null);
if (!capabilities && !integrationsWithNoCapabilities.includes(key)) {
const otherCapabilities = _.get(manifest, 'otherCapabilities', null);
expect(otherCapabilities).not.toBeNull();
expect(otherCapabilities.length).toBeGreaterThan(0);
}
}
);
test.each(manifests)(
'Templates can be successfully rendered for %s',
(key, manifest) => {
const useStandardWebhookPayload = _.get(
manifest,
'capabilities.auditLogEventsHook.useStandardWebhookPayload',
false
);
const flagTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.flag',
null
);
const projectTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.project',
null
);
const environmentTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.environment',
null
);
const defaultTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.default',
null
);
const validationTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.validation',
null
);
const memberTemplatePath = _.get(
manifest,
'capabilities.auditLogEventsHook.templates.member',
null
);
if (useStandardWebhookPayload) {
const templatePaths = [
flagTemplatePath,
projectTemplatePath,
environmentTemplatePath,
defaultTemplatePath,
validationTemplatePath,
memberTemplatePath,
];
for (const path of templatePaths) {
expect(path).toBe(null);
expect(existsSync(`./integrations/${key}/templates`)).toBe(false);
}
}
if (flagTemplatePath) {
const path = `./integrations/${key}/${flagTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(flagTemplatePath);
const fullContext = getFullContext(manifest, flagContext, isJSON);
expect(
() => template(fullContext),
`${key}: flag template must render successfully`
).not.toThrow();
// Validate json templates render to valid json
if (isJSON) {
const rendered = template(fullContext);
expect(
() => JSON.parse(rendered),
`${key} .json templates must render valid JSON\n${rendered}`
).not.toThrow();
}
}
if (projectTemplatePath) {
const path = `./integrations/${key}/${projectTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(projectTemplatePath);
const fullContext = getFullContext(manifest, projectContext, isJSON);
expect(
() => template(fullContext),
`${key}: project template must render successfully`
).not.toThrow();
// Validate json templates render to valid json
if (isJSON) {
const rendered = template(fullContext);
expect(
() => JSON.parse(rendered),
`${key} project .json templates must render valid JSON\n${rendered}`
).not.toThrow();
}
}
if (environmentTemplatePath) {
const path = `./integrations/${key}/${environmentTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(environmentTemplatePath);
const fullContext = getFullContext(
manifest,
environmentContext,
isJSON
);
expect(
() => template(fullContext),
`${key}: environment template must render successfully`
).not.toThrow();
// Validate json templates render to valid json
if (isJSON) {
const rendered = template(fullContext);
expect(
() => JSON.parse(rendered),
`${key} environment .json templates must render valid JSON\n${rendered}`
).not.toThrow();
}
}
if (defaultTemplatePath) {
const path = `./integrations/${key}/${defaultTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(defaultTemplatePath);
const fullContext = getFullContext(manifest, flagContext, isJSON);
expect(
() => template(fullContext),
`${key}: default template must render successfully`
).not.toThrow();
// Validate json templates render to valid json
if (isJSON) {
const rendered = template(fullContext);
expect(
() => JSON.parse(rendered),
`${key} default .json templates must render valid JSON\n${rendered}`
).not.toThrow();
}
}
if (validationTemplatePath) {
const path = `./integrations/${key}/${validationTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(validationTemplatePath);
const fullContext = getFullContext(
manifest,
environmentContext,
isJSON
);
expect(
() => template(fullContext),
`${key}: validation template must render successfully`
).not.toThrow();
// Validation templates must not render an empty string
const rendered = template(fullContext);
expect(
rendered.trim(),
`${key} validation template must not render an empty string`
).not.toEqual('');
}
if (memberTemplatePath) {
const path = `./integrations/${key}/${memberTemplatePath}`;
expect(existsSync(path)).toBe(true);
const template = getTemplate(path);
const isJSON = isJSONTemplate(memberTemplatePath);
const fullContext = getFullContext(manifest, memberContext, isJSON);
expect(
() => template(fullContext),
`${key}: member template must render successfully`
).not.toThrow();
// member templates must not render an empty string
const rendered = template(fullContext);
expect(
rendered.trim(),
`${key} member template must not render an empty string`
).not.toEqual('');
// Validate json templates render to valid json
if (isJSON) {
expect(
() => JSON.parse(rendered),
`${key} member .json templates must render valid JSON\n${rendered}`
).not.toThrow();
}
}
}
);
});