@friggframework/devtools 2.0.0--canary.701.32f702a.0 → 2.0.0--canary.662.6d3931c.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/frigg-cli/README.md +31 -123
- package/frigg-cli/__tests__/unit/commands/build.test.js +0 -3
- package/frigg-cli/__tests__/unit/commands/deploy.test.js +24 -33
- package/frigg-cli/__tests__/unit/commands/doctor.test.js +6 -10
- package/frigg-cli/__tests__/unit/commands/generate-iam.test.js +0 -18
- package/frigg-cli/__tests__/unit/commands/install.test.js +341 -601
- package/frigg-cli/__tests__/unit/commands/ui.test.js +30 -31
- package/frigg-cli/__tests__/unit/dependencies.test.js +2 -6
- package/frigg-cli/build-command/index.js +0 -6
- package/frigg-cli/deploy-command/index.js +0 -6
- package/frigg-cli/generate-command/index.js +0 -1
- package/frigg-cli/generate-iam-command.js +0 -1
- package/frigg-cli/index.js +5 -28
- package/frigg-cli/index.test.js +158 -0
- package/frigg-cli/init-command/backend-first-handler.js +551 -395
- package/frigg-cli/init-command/index.js +35 -82
- package/frigg-cli/install-command/commit-changes.js +8 -40
- package/frigg-cli/install-command/environment-variables.js +0 -15
- package/frigg-cli/install-command/index.js +21 -96
- package/frigg-cli/install-command/install-package.js +4 -77
- package/frigg-cli/install-command/integration-file.js +11 -20
- package/frigg-cli/install-command/template.js +75 -102
- package/frigg-cli/install-command/validate-package.js +4 -36
- package/frigg-cli/package.json +1 -1
- package/frigg-cli/start-command/index.js +0 -4
- package/frigg-cli/start-command/start-command.test.js +0 -3
- package/frigg-cli/test/init-command.test.js +2 -9
- package/infrastructure/__tests__/postgres-config.test.js +835 -241
- package/infrastructure/__tests__/template-generation.test.js +8 -36
- package/infrastructure/create-frigg-infrastructure.js +1 -4
- package/infrastructure/docs/POSTGRES-CONFIGURATION.md +98 -80
- package/infrastructure/domains/admin-scripts/admin-script-builder.js +2 -6
- package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +0 -30
- package/infrastructure/domains/database/aurora-builder.js +2 -13
- package/infrastructure/domains/database/aurora-builder.test.js +0 -22
- package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +8 -12
- package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +7 -19
- package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +8 -12
- package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +18 -17
- package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +2 -12
- package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +1 -8
- package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +2 -4
- package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +0 -10
- package/infrastructure/domains/networking/vpc-builder.js +8 -18
- package/infrastructure/domains/networking/vpc-builder.test.js +12 -26
- package/infrastructure/domains/security/iam-generator.js +5 -112
- package/infrastructure/domains/security/iam-generator.test.js +0 -94
- package/infrastructure/domains/shared/cloudformation-discovery.test.js +6 -36
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +2 -26
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +1 -34
- package/infrastructure/infrastructure-composer.test.js +794 -268
- package/infrastructure/integration.test.js +381 -0
- package/infrastructure/scripts/build-prisma-layer.test.js +7 -28
- package/infrastructure/scripts/build-time-discovery.test.js +3 -18
- package/package.json +8 -10
- package/frigg-cli/__tests__/unit/commands/init.test.js +0 -688
- package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +0 -92
- package/frigg-cli/init-command/deployment-modes.js +0 -7
- package/frigg-cli/init-command/safe-path.js +0 -58
- package/frigg-cli/init-command/scaffold-dependencies.js +0 -202
- package/frigg-cli/install-command/app-definition.js +0 -174
- package/frigg-cli/install-command/resolve-module.js +0 -69
- package/frigg-cli/templates/backend/README.md +0 -267
- package/frigg-cli/templates/backend/env.example +0 -20
- package/frigg-cli/templates/backend/gitignore +0 -10
- package/frigg-cli/templates/backend/index.js +0 -114
- package/frigg-cli/templates/backend/infrastructure.js +0 -20
- package/frigg-cli/validate-command/__tests__/init-validation.test.js +0 -63
- package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +0 -133
- package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +0 -443
- package/frigg-cli/validate-command/__tests__/validate-command.test.js +0 -262
- package/frigg-cli/validate-command/__tests__/validate-module-command.test.js +0 -119
- package/frigg-cli/validate-command/checks/config.js +0 -504
- package/frigg-cli/validate-command/checks/integrations.js +0 -274
- package/frigg-cli/validate-command/fixtures/bad-module/index.js +0 -18
- package/frigg-cli/validate-command/fixtures/fathom-module/definition.js +0 -76
- package/frigg-cli/validate-command/fixtures/fathom-module/index.js +0 -6
- package/frigg-cli/validate-command/format-report.js +0 -60
- package/frigg-cli/validate-command/index.js +0 -151
- package/frigg-cli/validate-command/load-app-definition.js +0 -105
- package/frigg-cli/validate-command/load-module-definition.js +0 -64
- package/frigg-cli/validate-command/schema-issues.js +0 -154
- package/frigg-cli/validate-command/validate-app-definition.js +0 -60
- package/frigg-cli/validate-command/validate-module-definition.js +0 -46
- package/infrastructure/__tests__/management-api-gateway-routes.test.js +0 -281
- package/infrastructure/__tests__/scaffold-default-infrastructure.test.js +0 -148
- package/infrastructure/app-definition-schema-contract.test.js +0 -629
- package/infrastructure/create-frigg-infrastructure.test.js +0 -51
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Load the `Definition` exported by an API module's `./index.js`
|
|
6
|
-
* (`{ Api, Definition }`). Used by `frigg validate --module`.
|
|
7
|
-
*
|
|
8
|
-
* @param {object} [params]
|
|
9
|
-
* @param {string} [params.cwd]
|
|
10
|
-
* @returns {{ ok: true, definition: object, indexPath: string }
|
|
11
|
-
* | { ok: false, issue: object }}
|
|
12
|
-
*/
|
|
13
|
-
function loadModuleDefinition({ cwd = process.cwd() } = {}) {
|
|
14
|
-
const indexPath = path.join(cwd, 'index.js');
|
|
15
|
-
if (!fs.existsSync(indexPath)) {
|
|
16
|
-
return {
|
|
17
|
-
ok: false,
|
|
18
|
-
issue: {
|
|
19
|
-
severity: 'error',
|
|
20
|
-
code: 'module-index-not-found',
|
|
21
|
-
pointer: '',
|
|
22
|
-
message: `No API module index.js found at ${indexPath}.`,
|
|
23
|
-
hint: 'Run `frigg validate --module` from the module directory (the one that exports `{ Api, Definition }`).',
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let mod;
|
|
29
|
-
try {
|
|
30
|
-
const resolved = require.resolve(indexPath);
|
|
31
|
-
delete require.cache[resolved];
|
|
32
|
-
mod = require(indexPath);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
return {
|
|
35
|
-
ok: false,
|
|
36
|
-
issue: {
|
|
37
|
-
severity: 'error',
|
|
38
|
-
code: 'load-failed',
|
|
39
|
-
pointer: '',
|
|
40
|
-
message: `${indexPath} failed to load: ${error.message}`,
|
|
41
|
-
hint: 'Fix the error above; the module must load cleanly.',
|
|
42
|
-
stack: error.stack,
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const definition = mod && mod.Definition;
|
|
48
|
-
if (!definition || typeof definition !== 'object') {
|
|
49
|
-
return {
|
|
50
|
-
ok: false,
|
|
51
|
-
issue: {
|
|
52
|
-
severity: 'error',
|
|
53
|
-
code: 'definition-not-exported',
|
|
54
|
-
pointer: '',
|
|
55
|
-
message: `${indexPath} does not export a Definition object.`,
|
|
56
|
-
hint: 'Export `{ Api, Definition }` from index.js.',
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return { ok: true, definition, indexPath };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = { loadModuleDefinition };
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turn Ajv errors from the app-definition schema into validation issues:
|
|
3
|
-
* { severity, code, pointer, message, hint }. The pointer is a JSON pointer
|
|
4
|
-
* into the app definition (RFC 6901).
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
function appendPointer(pointer, key) {
|
|
8
|
-
const escaped = String(key).replace(/~/g, '~0').replace(/\//g, '~1');
|
|
9
|
-
return `${pointer}/${escaped}`;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function schemaNodeAt(schema, schemaPath) {
|
|
13
|
-
// schemaPath looks like "#/properties/vpc/properties/management/enum"
|
|
14
|
-
const parts = schemaPath.replace(/^#\/?/, '').split('/').filter(Boolean);
|
|
15
|
-
parts.pop(); // drop the keyword
|
|
16
|
-
let node = schema;
|
|
17
|
-
for (const part of parts) {
|
|
18
|
-
if (!node) return undefined;
|
|
19
|
-
node = node[part.replace(/~1/g, '/').replace(/~0/g, '~')];
|
|
20
|
-
}
|
|
21
|
-
return node;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function knownKeys(schemaNode) {
|
|
25
|
-
return Object.keys(schemaNode?.properties || {});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function closest(word, candidates) {
|
|
29
|
-
const distance = (a, b) => {
|
|
30
|
-
const dp = Array.from({ length: a.length + 1 }, (_, i) => [i]);
|
|
31
|
-
for (let j = 1; j <= b.length; j++) dp[0][j] = j;
|
|
32
|
-
for (let i = 1; i <= a.length; i++) {
|
|
33
|
-
for (let j = 1; j <= b.length; j++) {
|
|
34
|
-
dp[i][j] = Math.min(
|
|
35
|
-
dp[i - 1][j] + 1,
|
|
36
|
-
dp[i][j - 1] + 1,
|
|
37
|
-
dp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1)
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return dp[a.length][b.length];
|
|
42
|
-
};
|
|
43
|
-
let best = null;
|
|
44
|
-
for (const candidate of candidates) {
|
|
45
|
-
const d = distance(word.toLowerCase(), candidate.toLowerCase());
|
|
46
|
-
if (
|
|
47
|
-
d <= Math.max(2, Math.floor(candidate.length / 3)) &&
|
|
48
|
-
(!best || d < best.d)
|
|
49
|
-
) {
|
|
50
|
-
best = { candidate, d };
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return best ? best.candidate : null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function toIssue(error, schema) {
|
|
57
|
-
const pointer = error.instancePath || '';
|
|
58
|
-
const node = schemaNodeAt(schema, error.schemaPath || '');
|
|
59
|
-
|
|
60
|
-
switch (error.keyword) {
|
|
61
|
-
case 'additionalProperties': {
|
|
62
|
-
const key = error.params.additionalProperty;
|
|
63
|
-
const keys = knownKeys(node);
|
|
64
|
-
const suggestion = closest(key, keys);
|
|
65
|
-
return {
|
|
66
|
-
severity: 'error',
|
|
67
|
-
code: 'unknown-key',
|
|
68
|
-
pointer: appendPointer(pointer, key),
|
|
69
|
-
message: `Unknown key "${key}"${
|
|
70
|
-
pointer ? ` in ${pointer}` : ''
|
|
71
|
-
}: nothing in Frigg reads it.`,
|
|
72
|
-
hint: suggestion
|
|
73
|
-
? `Did you mean "${suggestion}"?`
|
|
74
|
-
: keys.length
|
|
75
|
-
? `Known keys: ${keys.join(', ')}.`
|
|
76
|
-
: 'Remove it.',
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
case 'required':
|
|
80
|
-
return {
|
|
81
|
-
severity: 'error',
|
|
82
|
-
code: 'missing-key',
|
|
83
|
-
pointer: appendPointer(pointer, error.params.missingProperty),
|
|
84
|
-
message: `Missing required key "${error.params.missingProperty}".`,
|
|
85
|
-
hint: node?.properties?.[error.params.missingProperty]
|
|
86
|
-
?.description,
|
|
87
|
-
};
|
|
88
|
-
case 'enum':
|
|
89
|
-
return {
|
|
90
|
-
severity: 'error',
|
|
91
|
-
code: 'invalid-value',
|
|
92
|
-
pointer,
|
|
93
|
-
message: `${pointer || 'Value'} is ${JSON.stringify(
|
|
94
|
-
error.data
|
|
95
|
-
)}, which is not an allowed value.`,
|
|
96
|
-
hint: `Use one of: ${error.params.allowedValues
|
|
97
|
-
.map((v) => JSON.stringify(v))
|
|
98
|
-
.join(', ')}.`,
|
|
99
|
-
};
|
|
100
|
-
case 'type':
|
|
101
|
-
return {
|
|
102
|
-
severity: 'error',
|
|
103
|
-
code: 'invalid-type',
|
|
104
|
-
pointer,
|
|
105
|
-
message: `${pointer || 'Value'} must be ${
|
|
106
|
-
error.params.type
|
|
107
|
-
}, got ${error.data === null ? 'null' : typeof error.data}.`,
|
|
108
|
-
hint: node?.description,
|
|
109
|
-
};
|
|
110
|
-
default:
|
|
111
|
-
return {
|
|
112
|
-
severity: 'error',
|
|
113
|
-
code: `schema-${error.keyword}`,
|
|
114
|
-
pointer,
|
|
115
|
-
message: `${pointer || 'The app definition'} ${error.message}.`,
|
|
116
|
-
hint: node?.description,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* @param {Array<object>|null} errors Ajv errors
|
|
123
|
-
* @param {object} schema the app-definition schema
|
|
124
|
-
* @returns {Array<object>} issues, one per distinct pointer+code
|
|
125
|
-
*/
|
|
126
|
-
function schemaErrorsToIssues(errors, schema) {
|
|
127
|
-
if (!errors) return [];
|
|
128
|
-
// oneOf/anyOf failures repeat as one error per branch plus a summary:
|
|
129
|
-
// keep the summary only.
|
|
130
|
-
const oneOfPointers = new Set(
|
|
131
|
-
errors
|
|
132
|
-
.filter((e) => e.keyword === 'oneOf' || e.keyword === 'anyOf')
|
|
133
|
-
.map((e) => e.instancePath)
|
|
134
|
-
);
|
|
135
|
-
const seen = new Set();
|
|
136
|
-
const issues = [];
|
|
137
|
-
for (const error of errors) {
|
|
138
|
-
if (
|
|
139
|
-
error.keyword !== 'oneOf' &&
|
|
140
|
-
error.keyword !== 'anyOf' &&
|
|
141
|
-
oneOfPointers.has(error.instancePath)
|
|
142
|
-
) {
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
const issue = toIssue(error, schema);
|
|
146
|
-
const key = `${issue.code}:${issue.pointer}`;
|
|
147
|
-
if (seen.has(key)) continue;
|
|
148
|
-
seen.add(key);
|
|
149
|
-
issues.push(issue);
|
|
150
|
-
}
|
|
151
|
-
return issues;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
module.exports = { schemaErrorsToIssues, appendPointer };
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validate an app definition: the JSON Schema (keys, types, enums) plus the
|
|
3
|
-
* semantic checks the schema cannot express (ADR-051). Pure: no I/O.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
validateAppDefinition,
|
|
8
|
-
applyAppDefinitionDefaults,
|
|
9
|
-
getSchema,
|
|
10
|
-
} = require('@friggframework/schemas');
|
|
11
|
-
const { schemaErrorsToIssues } = require('./schema-issues');
|
|
12
|
-
const { checkIntegrations } = require('./checks/integrations');
|
|
13
|
-
const { checkConfig } = require('./checks/config');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @param {object} params
|
|
17
|
-
* @param {object} params.definition the `Definition` exported by index.js
|
|
18
|
-
* @param {string} [params.stage] the stage being built or deployed
|
|
19
|
-
* @param {object} [params.env] environment variables (default process.env)
|
|
20
|
-
* @returns {{ errors: object[], warnings: object[] }}
|
|
21
|
-
*/
|
|
22
|
-
function validateDefinition({ definition, stage, env = process.env }) {
|
|
23
|
-
const schemaResult = validateAppDefinition(definition);
|
|
24
|
-
// Integration entries are classes: checkIntegrations reports them more
|
|
25
|
-
// precisely than the schema's view of them can.
|
|
26
|
-
const issues = schemaErrorsToIssues(
|
|
27
|
-
schemaResult.errors,
|
|
28
|
-
getSchema('app-definition')
|
|
29
|
-
).filter((issue) => !issue.pointer.startsWith('/integrations/'));
|
|
30
|
-
|
|
31
|
-
let semantic = [];
|
|
32
|
-
if (definition && typeof definition === 'object') {
|
|
33
|
-
semantic = [
|
|
34
|
-
...checkIntegrations(definition),
|
|
35
|
-
...checkConfig({
|
|
36
|
-
definition: applyAppDefinitionDefaults(definition),
|
|
37
|
-
raw: definition,
|
|
38
|
-
stage,
|
|
39
|
-
env,
|
|
40
|
-
}),
|
|
41
|
-
];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// One issue per pointer and code; the schema's wins.
|
|
45
|
-
const seen = new Set(issues.map((i) => `${i.code}:${i.pointer}`));
|
|
46
|
-
for (const issue of semantic) {
|
|
47
|
-
const key = `${issue.code}:${issue.pointer}`;
|
|
48
|
-
if (!seen.has(key)) {
|
|
49
|
-
seen.add(key);
|
|
50
|
-
issues.push(issue);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
errors: issues.filter((i) => i.severity === 'error'),
|
|
56
|
-
warnings: issues.filter((i) => i.severity !== 'error'),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
module.exports = { validateDefinition };
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validate an API module Definition: the JSON Schema plus the structural
|
|
3
|
-
* checks `checkModuleDefinition` runs for modules listed on an app
|
|
4
|
-
* (ADR-051). Pure: no I/O.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const {
|
|
8
|
-
validateApiModuleDefinition,
|
|
9
|
-
getSchema,
|
|
10
|
-
} = require('@friggframework/schemas');
|
|
11
|
-
const { schemaErrorsToIssues } = require('./schema-issues');
|
|
12
|
-
const { checkModuleDefinition } = require('./checks/integrations');
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @param {object} params
|
|
16
|
-
* @param {object} params.definition the `Definition` exported by the module
|
|
17
|
-
* @returns {{ errors: object[], warnings: object[] }}
|
|
18
|
-
*/
|
|
19
|
-
function validateModuleDefinition({ definition }) {
|
|
20
|
-
const schemaResult = validateApiModuleDefinition(definition);
|
|
21
|
-
const issues = schemaErrorsToIssues(
|
|
22
|
-
schemaResult.errors,
|
|
23
|
-
getSchema('api-module-definition')
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
const structural =
|
|
27
|
-
definition && typeof definition === 'object'
|
|
28
|
-
? checkModuleDefinition(definition, '')
|
|
29
|
-
: [];
|
|
30
|
-
|
|
31
|
-
const seen = new Set(issues.map((i) => `${i.code}:${i.pointer}`));
|
|
32
|
-
for (const issue of structural) {
|
|
33
|
-
const key = `${issue.code}:${issue.pointer}`;
|
|
34
|
-
if (!seen.has(key)) {
|
|
35
|
-
seen.add(key);
|
|
36
|
-
issues.push(issue);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
errors: issues.filter((i) => i.severity === 'error'),
|
|
42
|
-
warnings: issues.filter((i) => i.severity !== 'error'),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = { validateModuleDefinition };
|
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Management API ↔ API Gateway route coverage
|
|
3
|
-
*
|
|
4
|
-
* The Management API is an Express app (core's createIntegrationRouter)
|
|
5
|
-
* served by the `auth` Lambda. API Gateway HTTP APIs have no implicit
|
|
6
|
-
* catch-all: a request whose path matches no declared route gets a gateway
|
|
7
|
-
* 404 and never reaches Express. So every route the integration router
|
|
8
|
-
* registers must be covered by an httpApi event on the `auth` function, and
|
|
9
|
-
* no more-specific route on another function may shadow it.
|
|
10
|
-
*
|
|
11
|
-
* The Express routes are read from the live router (not a hard-coded list),
|
|
12
|
-
* so a route added to core later fails this test until the gateway routes
|
|
13
|
-
* are updated alongside it.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// Avoid AWS calls: hand the builders a fixed discovery result.
|
|
17
|
-
jest.mock('../domains/shared/resource-discovery', () => {
|
|
18
|
-
const actual = jest.requireActual('../domains/shared/resource-discovery');
|
|
19
|
-
return {
|
|
20
|
-
...actual,
|
|
21
|
-
gatherDiscoveredResources: jest.fn().mockResolvedValue({
|
|
22
|
-
defaultVpcId: 'vpc-123456',
|
|
23
|
-
vpcCidr: '172.31.0.0/16',
|
|
24
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
25
|
-
privateSubnetId1: 'subnet-123456',
|
|
26
|
-
privateSubnetId2: 'subnet-789012',
|
|
27
|
-
publicSubnetId1: 'subnet-public-1',
|
|
28
|
-
publicSubnetId2: 'subnet-public-2',
|
|
29
|
-
defaultRouteTableId: 'rtb-123456',
|
|
30
|
-
defaultKmsKeyId:
|
|
31
|
-
'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012',
|
|
32
|
-
existingNatGatewayId: 'nat-default123',
|
|
33
|
-
auroraClusterEndpoint:
|
|
34
|
-
'test-cluster.cluster-abc123.us-east-1.rds.amazonaws.com',
|
|
35
|
-
auroraPort: 5432,
|
|
36
|
-
auroraEngine: 'aurora-postgresql',
|
|
37
|
-
}),
|
|
38
|
-
};
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// createIntegrationRouter loads the app definition from the consuming
|
|
42
|
-
// backend; give it an empty one so the router can be built in isolation.
|
|
43
|
-
jest.mock('@friggframework/core/handlers/app-definition-loader', () => ({
|
|
44
|
-
loadAppDefinition: () => ({ integrations: [], userConfig: {} }),
|
|
45
|
-
}));
|
|
46
|
-
|
|
47
|
-
const { composeServerlessDefinition } = require('../infrastructure-composer');
|
|
48
|
-
|
|
49
|
-
// Routes the auth Lambda's handler (core/handlers/routers/auth.js) adds on
|
|
50
|
-
// top of createIntegrationRouter().
|
|
51
|
-
const AUTH_HANDLER_EXTRA_ROUTES = [
|
|
52
|
-
{ method: 'GET', path: '/api/integrations/redirect/:appId' },
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
function getIntegrationRouterRoutes() {
|
|
56
|
-
const { createIntegrationRouter } = require('@friggframework/core');
|
|
57
|
-
// The repository factories pick an adapter from DB_TYPE (or the backend's
|
|
58
|
-
// app definition, which doesn't exist here). Nothing connects.
|
|
59
|
-
const previousDbType = process.env.DB_TYPE;
|
|
60
|
-
process.env.DB_TYPE = 'postgresql';
|
|
61
|
-
let app;
|
|
62
|
-
try {
|
|
63
|
-
app = createIntegrationRouter();
|
|
64
|
-
} finally {
|
|
65
|
-
if (previousDbType === undefined) delete process.env.DB_TYPE;
|
|
66
|
-
else process.env.DB_TYPE = previousDbType;
|
|
67
|
-
}
|
|
68
|
-
const stack = (app._router || app.router).stack;
|
|
69
|
-
const routes = [];
|
|
70
|
-
for (const layer of stack) {
|
|
71
|
-
if (!layer.route) continue;
|
|
72
|
-
for (const method of Object.keys(layer.route.methods)) {
|
|
73
|
-
routes.push({
|
|
74
|
-
method: method === '_all' ? 'ANY' : method.toUpperCase(),
|
|
75
|
-
path: layer.route.path,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return routes;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getGatewayRoutes(definition) {
|
|
83
|
-
const routes = [];
|
|
84
|
-
for (const [functionName, fn] of Object.entries(definition.functions)) {
|
|
85
|
-
for (const event of fn.events || []) {
|
|
86
|
-
if (!event.httpApi) continue;
|
|
87
|
-
routes.push({
|
|
88
|
-
functionName,
|
|
89
|
-
method: String(event.httpApi.method).toUpperCase(),
|
|
90
|
-
path: event.httpApi.path,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return routes;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const segmentsOf = (p) => p.split('/').filter(Boolean);
|
|
98
|
-
|
|
99
|
-
// Turn an Express path into a concrete request path, e.g.
|
|
100
|
-
// /api/entities/:entityId/options -> /api/entities/sample-entityId/options
|
|
101
|
-
const toConcretePath = (expressPath) =>
|
|
102
|
-
expressPath.replace(/:([A-Za-z0-9_]+)/g, 'sample-$1');
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* HTTP API path matching: literal segments match exactly, {param} matches
|
|
106
|
-
* one segment, a trailing {proxy+} greedily matches one or more segments.
|
|
107
|
-
* Returns null on no match, otherwise a specificity descriptor.
|
|
108
|
-
*/
|
|
109
|
-
function matchGatewayPath(gatewayPath, requestPath) {
|
|
110
|
-
const g = segmentsOf(gatewayPath);
|
|
111
|
-
const r = segmentsOf(requestPath);
|
|
112
|
-
let literals = 0;
|
|
113
|
-
for (let i = 0; i < g.length; i++) {
|
|
114
|
-
const seg = g[i];
|
|
115
|
-
if (/^\{[^}]+\+\}$/.test(seg)) {
|
|
116
|
-
if (i !== g.length - 1 || r.length <= i) return null;
|
|
117
|
-
return { greedy: true, literals, prefixLength: i };
|
|
118
|
-
}
|
|
119
|
-
if (i >= r.length) return null;
|
|
120
|
-
if (/^\{[^}]+\}$/.test(seg)) continue;
|
|
121
|
-
if (seg !== r[i]) return null;
|
|
122
|
-
literals++;
|
|
123
|
-
}
|
|
124
|
-
if (g.length !== r.length) return null;
|
|
125
|
-
return { greedy: false, literals, prefixLength: g.length };
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Select the route API Gateway would invoke: a non-greedy match beats a
|
|
130
|
-
* greedy one, then more literal segments, then the longer greedy prefix,
|
|
131
|
-
* then an explicit method beats ANY.
|
|
132
|
-
*/
|
|
133
|
-
function selectGatewayRoute(gatewayRoutes, method, requestPath) {
|
|
134
|
-
const candidates = gatewayRoutes
|
|
135
|
-
.filter(
|
|
136
|
-
(r) => r.method === 'ANY' || method === 'ANY' || r.method === method
|
|
137
|
-
)
|
|
138
|
-
.map((r) => ({
|
|
139
|
-
route: r,
|
|
140
|
-
match: matchGatewayPath(r.path, requestPath),
|
|
141
|
-
}))
|
|
142
|
-
.filter((c) => c.match);
|
|
143
|
-
candidates.sort(
|
|
144
|
-
(a, b) =>
|
|
145
|
-
Number(a.match.greedy) - Number(b.match.greedy) ||
|
|
146
|
-
b.match.literals - a.match.literals ||
|
|
147
|
-
b.match.prefixLength - a.match.prefixLength ||
|
|
148
|
-
Number(a.route.method === 'ANY') - Number(b.route.method === 'ANY')
|
|
149
|
-
);
|
|
150
|
-
return candidates.length ? candidates[0].route : null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
describe('Management API routes are reachable through API Gateway', () => {
|
|
154
|
-
let gatewayRoutes;
|
|
155
|
-
let managementRoutes;
|
|
156
|
-
const originalArgv = process.argv;
|
|
157
|
-
|
|
158
|
-
beforeAll(async () => {
|
|
159
|
-
process.argv = ['node', 'test'];
|
|
160
|
-
process.env.AWS_REGION = 'us-east-1';
|
|
161
|
-
// The builders narrate their progress; keep the test output quiet.
|
|
162
|
-
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
163
|
-
const warnSpy = jest
|
|
164
|
-
.spyOn(console, 'warn')
|
|
165
|
-
.mockImplementation(() => {});
|
|
166
|
-
try {
|
|
167
|
-
const definition = await composeServerlessDefinition({
|
|
168
|
-
name: 'route-coverage-app',
|
|
169
|
-
provider: 'aws',
|
|
170
|
-
usePrismaLambdaLayer: false,
|
|
171
|
-
database: { postgres: { enable: true } },
|
|
172
|
-
websockets: { enable: true },
|
|
173
|
-
adminScripts: [
|
|
174
|
-
class ExampleScript {
|
|
175
|
-
static Definition = { name: 'example-script' };
|
|
176
|
-
},
|
|
177
|
-
],
|
|
178
|
-
admin: { includeBuiltinReports: true },
|
|
179
|
-
integrations: [
|
|
180
|
-
{
|
|
181
|
-
Definition: {
|
|
182
|
-
name: 'acme',
|
|
183
|
-
webhooks: true,
|
|
184
|
-
extensions: {
|
|
185
|
-
inbound: {
|
|
186
|
-
extension: {
|
|
187
|
-
routes: [
|
|
188
|
-
{ path: '/events', method: 'POST' },
|
|
189
|
-
],
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
// Integration names share the /api/ namespace with the
|
|
196
|
-
// Management API; make sure they don't shadow it.
|
|
197
|
-
{ Definition: { name: 'entities' } },
|
|
198
|
-
],
|
|
199
|
-
});
|
|
200
|
-
gatewayRoutes = getGatewayRoutes(definition);
|
|
201
|
-
} finally {
|
|
202
|
-
logSpy.mockRestore();
|
|
203
|
-
warnSpy.mockRestore();
|
|
204
|
-
}
|
|
205
|
-
managementRoutes = [
|
|
206
|
-
...getIntegrationRouterRoutes(),
|
|
207
|
-
...AUTH_HANDLER_EXTRA_ROUTES,
|
|
208
|
-
];
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
afterAll(() => {
|
|
212
|
-
process.argv = originalArgv;
|
|
213
|
-
delete process.env.AWS_REGION;
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('reads the Management API routes from the core integration router', () => {
|
|
217
|
-
const paths = managementRoutes.map((r) => r.path);
|
|
218
|
-
expect(paths).toEqual(
|
|
219
|
-
expect.arrayContaining([
|
|
220
|
-
'/api/integrations',
|
|
221
|
-
'/api/authorize',
|
|
222
|
-
'/api/entity',
|
|
223
|
-
'/api/entity/options/:credentialId',
|
|
224
|
-
'/api/entities/:entityId',
|
|
225
|
-
])
|
|
226
|
-
);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('routes every Management API route to the auth function', () => {
|
|
230
|
-
const unreachable = managementRoutes
|
|
231
|
-
.map(({ method, path: expressPath }) => {
|
|
232
|
-
const requestPath = toConcretePath(expressPath);
|
|
233
|
-
const selected = selectGatewayRoute(
|
|
234
|
-
gatewayRoutes,
|
|
235
|
-
method,
|
|
236
|
-
requestPath
|
|
237
|
-
);
|
|
238
|
-
return {
|
|
239
|
-
route: `${method} ${expressPath}`,
|
|
240
|
-
reachedBy: selected
|
|
241
|
-
? `${selected.method} ${selected.path} -> ${selected.functionName}`
|
|
242
|
-
: 'no API Gateway route',
|
|
243
|
-
ok: selected && selected.functionName === 'auth',
|
|
244
|
-
};
|
|
245
|
-
})
|
|
246
|
-
.filter((r) => !r.ok)
|
|
247
|
-
.map(({ route, reachedBy }) => `${route} (${reachedBy})`);
|
|
248
|
-
|
|
249
|
-
expect(unreachable).toEqual([]);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it.each([
|
|
253
|
-
['GET', '/api/v2/reports', 'reportRouter'],
|
|
254
|
-
['POST', '/api/v2/reports/daily/run', 'reportRouter'],
|
|
255
|
-
['GET', '/admin/scripts/example-script', 'adminScriptRouter'],
|
|
256
|
-
['POST', '/api/acme-integration/webhooks', 'acmeWebhook'],
|
|
257
|
-
['POST', '/api/acme-integration/inbound/events', 'acme__inbound'],
|
|
258
|
-
['GET', '/api/acme-integration/custom', 'acme'],
|
|
259
|
-
['GET', '/api/entities-integration/custom', 'entities'],
|
|
260
|
-
['POST', '/user/login', 'user'],
|
|
261
|
-
['GET', '/health/ready', 'health'],
|
|
262
|
-
])('leaves %s %s on %s', (method, requestPath, functionName) => {
|
|
263
|
-
const selected = selectGatewayRoute(gatewayRoutes, method, requestPath);
|
|
264
|
-
expect(selected && selected.functionName).toBe(functionName);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
it('declares no method + path pair on more than one function', () => {
|
|
268
|
-
const seen = new Map();
|
|
269
|
-
const duplicates = [];
|
|
270
|
-
for (const r of gatewayRoutes) {
|
|
271
|
-
const key = `${r.method} ${r.path}`;
|
|
272
|
-
if (seen.has(key) && seen.get(key) !== r.functionName) {
|
|
273
|
-
duplicates.push(
|
|
274
|
-
`${key}: ${seen.get(key)} and ${r.functionName}`
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
seen.set(key, r.functionName);
|
|
278
|
-
}
|
|
279
|
-
expect(duplicates).toEqual([]);
|
|
280
|
-
});
|
|
281
|
-
});
|