@friggframework/devtools 2.0.0--canary.710.7cfcf12.0 → 2.0.0--canary.682.451c957.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/frigg-cli/README.md +30 -77
- package/frigg-cli/__tests__/unit/commands/deploy.test.js +24 -30
- 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/generate-command/index.js +0 -1
- package/frigg-cli/generate-iam-command.js +0 -1
- package/frigg-cli/index.js +4 -9
- package/frigg-cli/index.test.js +158 -0
- package/frigg-cli/init-command/backend-first-handler.js +554 -397
- 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/test/init-command.test.js +2 -9
- package/infrastructure/__tests__/management-api-gateway-routes.test.js +335 -0
- package/infrastructure/__tests__/postgres-config.test.js +835 -241
- package/infrastructure/__tests__/template-generation.test.js +8 -36
- package/infrastructure/docs/POSTGRES-CONFIGURATION.md +1 -21
- 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 +28 -17
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +61 -22
- 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/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/infrastructure/__tests__/scaffold-default-infrastructure.test.js +0 -148
|
@@ -1,117 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
* Source for a new integration class (`src/integrations/<Name>Integration.js`)
|
|
3
|
-
* that wraps one installed API module.
|
|
4
|
-
*
|
|
5
|
-
* It follows the current IntegrationBase contract: a static `Definition`
|
|
6
|
-
* (name, version, display, `modules: { <key>: { definition } }`) and event
|
|
7
|
-
* handlers registered on `this.events` in the constructor. Calls to the API
|
|
8
|
-
* module go through `this.<key>.api.<method>()`.
|
|
9
|
-
*/
|
|
1
|
+
const path = require('path');
|
|
10
2
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
if (/^[0-9]/.test(identifier)) {
|
|
23
|
-
identifier = `Module${identifier}`;
|
|
24
|
-
}
|
|
25
|
-
return identifier;
|
|
26
|
-
}
|
|
3
|
+
function getIntegrationTemplate(apiModuleName, backendPath, ApiClass) {
|
|
4
|
+
// Find the sample data method
|
|
5
|
+
const apiMethods = Object.getOwnPropertyNames(ApiClass.prototype);
|
|
6
|
+
const sampleDataMethod =
|
|
7
|
+
apiMethods.find(
|
|
8
|
+
(method) =>
|
|
9
|
+
method.toLowerCase().includes('search') ||
|
|
10
|
+
method.toLowerCase().includes('list') ||
|
|
11
|
+
method.toLowerCase().includes('get')
|
|
12
|
+
) || 'searchDeals';
|
|
27
13
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const words = String(moduleName)
|
|
31
|
-
.split(/[^A-Za-z0-9]+/)
|
|
32
|
-
.filter(Boolean);
|
|
33
|
-
if (words.length === 0) {
|
|
34
|
-
return 'module';
|
|
35
|
-
}
|
|
36
|
-
const key = words
|
|
37
|
-
.map((word, i) =>
|
|
38
|
-
i === 0
|
|
39
|
-
? word.charAt(0).toLowerCase() + word.slice(1)
|
|
40
|
-
: word.charAt(0).toUpperCase() + word.slice(1)
|
|
41
|
-
)
|
|
42
|
-
.join('');
|
|
43
|
-
return /^[0-9]/.test(key) ? `module${key}` : key;
|
|
44
|
-
}
|
|
14
|
+
return `const { get, IntegrationBase, Options } = require('@friggframework/core');
|
|
15
|
+
const { Definition: ${apiModuleName}Module, Config: defaultConfig } = require('@friggframework/api-module-${apiModuleName.toLowerCase()}');
|
|
45
16
|
|
|
46
|
-
|
|
17
|
+
class ${apiModuleName}Integration extends IntegrationBase {
|
|
18
|
+
static Config = {
|
|
19
|
+
name: defaultConfig.name || '${apiModuleName.toLowerCase()}',
|
|
20
|
+
version: '1.0.0',
|
|
21
|
+
supportedVersions: ['1.0.0'],
|
|
22
|
+
events: ['SEARCH_DEALS'],
|
|
23
|
+
};
|
|
47
24
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
className,
|
|
62
|
-
packageName,
|
|
63
|
-
moduleName,
|
|
64
|
-
label,
|
|
65
|
-
description,
|
|
66
|
-
detailsUrl,
|
|
67
|
-
icon,
|
|
68
|
-
categories,
|
|
69
|
-
}) {
|
|
70
|
-
const moduleKey = toModuleKey(moduleName);
|
|
71
|
-
const moduleVar = `${moduleKey}Module`;
|
|
72
|
-
const category = Array.isArray(categories) ? categories.join(', ') : '';
|
|
25
|
+
static Options =
|
|
26
|
+
new Options({
|
|
27
|
+
module: ${apiModuleName}Module,
|
|
28
|
+
integrations: [${apiModuleName}Module],
|
|
29
|
+
display: {
|
|
30
|
+
name: defaultConfig.displayName || '${apiModuleName}',
|
|
31
|
+
description: defaultConfig.description || 'Sales & CRM, Marketing',
|
|
32
|
+
category: defaultConfig.category || 'Sales & CRM, Marketing',
|
|
33
|
+
detailsUrl: defaultConfig.detailsUrl || 'https://www.${apiModuleName.toLowerCase()}.com',
|
|
34
|
+
icon: defaultConfig.icon || 'https://friggframework.org/assets/img/${apiModuleName.toLowerCase()}.jpeg',
|
|
35
|
+
},
|
|
36
|
+
hasUserConfig: true,
|
|
37
|
+
});
|
|
73
38
|
|
|
74
|
-
|
|
75
|
-
|
|
39
|
+
static modules = {
|
|
40
|
+
${apiModuleName.toLowerCase()}: ${apiModuleName}Module,
|
|
41
|
+
}
|
|
76
42
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
43
|
+
/**
|
|
44
|
+
* HANDLE EVENTS
|
|
45
|
+
*/
|
|
46
|
+
async receiveNotification(notifier, event, object = null) {
|
|
47
|
+
if (event === 'SEARCH_DEALS') {
|
|
48
|
+
return this.target.api.searchDeals(object);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
83
51
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
52
|
+
/**
|
|
53
|
+
* ALL CUSTOM/OPTIONAL METHODS FOR AN INTEGRATION MANAGER
|
|
54
|
+
*/
|
|
55
|
+
async getSampleData() {
|
|
56
|
+
const res = await this.target.api.${sampleDataMethod}();
|
|
57
|
+
return { data: res };
|
|
58
|
+
}
|
|
91
59
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
60
|
+
/**
|
|
61
|
+
* ALL REQUIRED METHODS FOR AN INTEGRATION MANAGER
|
|
62
|
+
*/
|
|
63
|
+
async onCreate(params) {
|
|
64
|
+
// Validate that we have all of the data we need
|
|
65
|
+
// Set integration status as makes sense. Default ENABLED
|
|
66
|
+
// TODO turn this into a validateConfig method/function
|
|
67
|
+
this.record.status = 'ENABLED';
|
|
68
|
+
await this.record.save();
|
|
69
|
+
return this.record;
|
|
70
|
+
}
|
|
98
71
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
72
|
+
async onUpdate(params) {
|
|
73
|
+
const newConfig = get(params, 'config');
|
|
74
|
+
const oldConfig = this.record.config;
|
|
75
|
+
// Just save whatever
|
|
76
|
+
this.record.markModified('config');
|
|
77
|
+
await this.record.save();
|
|
78
|
+
return this.validateConfig();
|
|
79
|
+
}
|
|
103
80
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// Map event names to handlers, e.g.
|
|
108
|
-
// GET_SAMPLE_DATA: { handler: this.getSampleData.bind(this) },
|
|
109
|
-
};
|
|
81
|
+
async getConfigOptions() {
|
|
82
|
+
const options = {}
|
|
83
|
+
return options;
|
|
110
84
|
}
|
|
111
85
|
}
|
|
112
86
|
|
|
113
|
-
module.exports = ${
|
|
114
|
-
`;
|
|
87
|
+
module.exports = ${apiModuleName}Integration;`;
|
|
115
88
|
}
|
|
116
89
|
|
|
117
|
-
module.exports = { getIntegrationTemplate
|
|
90
|
+
module.exports = { getIntegrationTemplate };
|
|
@@ -1,28 +1,12 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
2
|
const axios = require('axios');
|
|
3
3
|
const { logError } = require('./logger');
|
|
4
4
|
const { checkbox } = require('@inquirer/prompts');
|
|
5
5
|
|
|
6
|
-
const API_MODULE_PREFIX = '@friggframework/api-module-';
|
|
7
|
-
|
|
8
6
|
async function searchPackages(apiModuleName) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
'npm',
|
|
13
|
-
['search', `${API_MODULE_PREFIX}${apiModuleName}`, '--json'],
|
|
14
|
-
{ encoding: 'utf8' }
|
|
15
|
-
);
|
|
16
|
-
if (result.error) {
|
|
17
|
-
throw result.error;
|
|
18
|
-
}
|
|
19
|
-
if (result.status !== 0) {
|
|
20
|
-
throw new Error(`npm search failed: ${result.stderr || result.status}`);
|
|
21
|
-
}
|
|
22
|
-
// npm search is a full-text search; keep only Frigg API modules.
|
|
23
|
-
return JSON.parse(result.stdout || '[]').filter(
|
|
24
|
-
(pkg) => pkg && pkg.name && pkg.name.startsWith(API_MODULE_PREFIX)
|
|
25
|
-
);
|
|
7
|
+
const searchCommand = `npm search @friggframework/api-module-${apiModuleName} --json`;
|
|
8
|
+
const result = execSync(searchCommand, { encoding: 'utf8' });
|
|
9
|
+
return JSON.parse(result);
|
|
26
10
|
}
|
|
27
11
|
|
|
28
12
|
async function checkPackageExists(packageName) {
|
|
@@ -74,21 +58,6 @@ const searchAndSelectPackage = async (apiModuleName) => {
|
|
|
74
58
|
};
|
|
75
59
|
});
|
|
76
60
|
|
|
77
|
-
// Without a terminal to prompt on, take the exact match
|
|
78
|
-
// (`frigg install hubspot` -> @friggframework/api-module-hubspot).
|
|
79
|
-
if (!process.stdin.isTTY) {
|
|
80
|
-
const exact = `${API_MODULE_PREFIX}${apiModuleName}`;
|
|
81
|
-
if (filteredResults.some((pkg) => pkg.name === exact)) {
|
|
82
|
-
return [exact];
|
|
83
|
-
}
|
|
84
|
-
logError(
|
|
85
|
-
`No exact match for ${exact}, and there is no terminal to choose from: ${filteredResults
|
|
86
|
-
.map((pkg) => pkg.name)
|
|
87
|
-
.join(', ')}`
|
|
88
|
-
);
|
|
89
|
-
process.exit(1);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
61
|
const selectedPackages = await checkbox({
|
|
93
62
|
message: 'Select the packages to install:',
|
|
94
63
|
choices,
|
|
@@ -99,7 +68,6 @@ const searchAndSelectPackage = async (apiModuleName) => {
|
|
|
99
68
|
};
|
|
100
69
|
|
|
101
70
|
module.exports = {
|
|
102
|
-
API_MODULE_PREFIX,
|
|
103
71
|
validatePackageExists,
|
|
104
72
|
checkPackageExists,
|
|
105
73
|
searchPackages,
|
package/frigg-cli/package.json
CHANGED
|
@@ -123,19 +123,12 @@ describe('Init Command', () => {
|
|
|
123
123
|
);
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
it('
|
|
127
|
-
// The flags `frigg init` registers and initCommand forwards
|
|
128
|
-
// (see init-command/index.js). `yes` is the non-interactive flag
|
|
129
|
-
// (`--yes`: accept defaults, no prompts); `install: false` and
|
|
130
|
-
// `git: false` are what `--no-install` / `--no-git` produce. The
|
|
131
|
-
// old `frontend` option is gone: init scaffolds a backend only.
|
|
126
|
+
it('should pass options to BackendFirstHandler', async () => {
|
|
132
127
|
const options = {
|
|
133
128
|
force: true,
|
|
134
129
|
verbose: true,
|
|
135
130
|
mode: 'standalone',
|
|
136
|
-
|
|
137
|
-
install: false,
|
|
138
|
-
git: false
|
|
131
|
+
frontend: false
|
|
139
132
|
};
|
|
140
133
|
|
|
141
134
|
await initCommand(mockProjectName, options);
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Management API ↔ API Gateway route coverage
|
|
3
|
+
*
|
|
4
|
+
* The Management API (ADR-053) is served by the `auth` Lambda (v1, v2 and
|
|
5
|
+
* the OAuth redirect) and, for /api/meta, by the DB-free `health` Lambda.
|
|
6
|
+
* API Gateway HTTP APIs have no implicit catch-all: a request whose path
|
|
7
|
+
* matches no declared route gets a gateway 404 and never reaches Express.
|
|
8
|
+
* So every route those routers register must be covered by an httpApi event
|
|
9
|
+
* on the right function, and no more-specific route on another function may
|
|
10
|
+
* shadow it. Both the routers and the gateway routes come from core's route
|
|
11
|
+
* registry; this test proves the outcome end to end.
|
|
12
|
+
*
|
|
13
|
+
* The Express routes are read from the live routers (not a hard-coded
|
|
14
|
+
* list), so a route added to core later fails this test until the gateway
|
|
15
|
+
* routes follow.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Avoid AWS calls: hand the builders a fixed discovery result.
|
|
19
|
+
jest.mock('../domains/shared/resource-discovery', () => {
|
|
20
|
+
const actual = jest.requireActual('../domains/shared/resource-discovery');
|
|
21
|
+
return {
|
|
22
|
+
...actual,
|
|
23
|
+
gatherDiscoveredResources: jest.fn().mockResolvedValue({
|
|
24
|
+
defaultVpcId: 'vpc-123456',
|
|
25
|
+
vpcCidr: '172.31.0.0/16',
|
|
26
|
+
defaultSecurityGroupId: 'sg-123456',
|
|
27
|
+
privateSubnetId1: 'subnet-123456',
|
|
28
|
+
privateSubnetId2: 'subnet-789012',
|
|
29
|
+
publicSubnetId1: 'subnet-public-1',
|
|
30
|
+
publicSubnetId2: 'subnet-public-2',
|
|
31
|
+
defaultRouteTableId: 'rtb-123456',
|
|
32
|
+
defaultKmsKeyId:
|
|
33
|
+
'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012',
|
|
34
|
+
existingNatGatewayId: 'nat-default123',
|
|
35
|
+
auroraClusterEndpoint:
|
|
36
|
+
'test-cluster.cluster-abc123.us-east-1.rds.amazonaws.com',
|
|
37
|
+
auroraPort: 5432,
|
|
38
|
+
auroraEngine: 'aurora-postgresql',
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// The Management API routers load the app definition from the consuming
|
|
44
|
+
// backend; give them one so they can be built in isolation. The entity proxy
|
|
45
|
+
// is enabled so its route is checked too.
|
|
46
|
+
jest.mock('@friggframework/core/handlers/app-definition-loader', () => ({
|
|
47
|
+
loadAppDefinition: () => ({
|
|
48
|
+
integrations: [],
|
|
49
|
+
userConfig: {},
|
|
50
|
+
managementApi: { proxy: { enable: true } },
|
|
51
|
+
}),
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
const { composeServerlessDefinition } = require('../infrastructure-composer');
|
|
55
|
+
|
|
56
|
+
/** Routes registered on an Express router, including nested routers. */
|
|
57
|
+
function collectRoutes(router, routes = new Map()) {
|
|
58
|
+
const stack = (router._router || router).stack || [];
|
|
59
|
+
for (const layer of stack) {
|
|
60
|
+
if (layer.route) {
|
|
61
|
+
for (const method of Object.keys(layer.route.methods)) {
|
|
62
|
+
const route = {
|
|
63
|
+
method: method === '_all' ? 'ANY' : method.toUpperCase(),
|
|
64
|
+
path: layer.route.path,
|
|
65
|
+
};
|
|
66
|
+
routes.set(`${route.method} ${route.path}`, route);
|
|
67
|
+
}
|
|
68
|
+
} else if (layer.handle && Array.isArray(layer.handle.stack)) {
|
|
69
|
+
collectRoutes(layer.handle, routes);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return routes;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Builds a core router with DB_TYPE set: the repository factories pick an
|
|
77
|
+
* adapter from it (or the backend's app definition, which doesn't exist
|
|
78
|
+
* here). Nothing connects.
|
|
79
|
+
*/
|
|
80
|
+
function withDbType(build) {
|
|
81
|
+
const previousDbType = process.env.DB_TYPE;
|
|
82
|
+
process.env.DB_TYPE = 'postgresql';
|
|
83
|
+
try {
|
|
84
|
+
return build();
|
|
85
|
+
} finally {
|
|
86
|
+
if (previousDbType === undefined) delete process.env.DB_TYPE;
|
|
87
|
+
else process.env.DB_TYPE = previousDbType;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Every route the auth Lambda's router (core/handlers/routers/auth.js) serves. */
|
|
92
|
+
function getAuthLambdaRoutes() {
|
|
93
|
+
const { router } = withDbType(() =>
|
|
94
|
+
require('@friggframework/core/handlers/routers/auth')
|
|
95
|
+
);
|
|
96
|
+
return [...collectRoutes(router).values()];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Every route the health Lambda serves under /api/meta. */
|
|
100
|
+
function getMetaRoutes() {
|
|
101
|
+
const { router } = withDbType(() =>
|
|
102
|
+
require('@friggframework/core/handlers/routers/health')
|
|
103
|
+
);
|
|
104
|
+
return [...collectRoutes(router).values()].filter((r) =>
|
|
105
|
+
r.path.startsWith('/api/meta')
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function getGatewayRoutes(definition) {
|
|
110
|
+
const routes = [];
|
|
111
|
+
for (const [functionName, fn] of Object.entries(definition.functions)) {
|
|
112
|
+
for (const event of fn.events || []) {
|
|
113
|
+
if (!event.httpApi) continue;
|
|
114
|
+
routes.push({
|
|
115
|
+
functionName,
|
|
116
|
+
method: String(event.httpApi.method).toUpperCase(),
|
|
117
|
+
path: event.httpApi.path,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return routes;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const segmentsOf = (p) => p.split('/').filter(Boolean);
|
|
125
|
+
|
|
126
|
+
// Turn an Express path into a concrete request path, e.g.
|
|
127
|
+
// /api/entities/:entityId/options -> /api/entities/sample-entityId/options
|
|
128
|
+
const toConcretePath = (expressPath) =>
|
|
129
|
+
expressPath.replace(/:([A-Za-z0-9_]+)/g, 'sample-$1');
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* HTTP API path matching: literal segments match exactly, {param} matches
|
|
133
|
+
* one segment, a trailing {proxy+} greedily matches one or more segments.
|
|
134
|
+
* Returns null on no match, otherwise a specificity descriptor.
|
|
135
|
+
*/
|
|
136
|
+
function matchGatewayPath(gatewayPath, requestPath) {
|
|
137
|
+
const g = segmentsOf(gatewayPath);
|
|
138
|
+
const r = segmentsOf(requestPath);
|
|
139
|
+
let literals = 0;
|
|
140
|
+
for (let i = 0; i < g.length; i++) {
|
|
141
|
+
const seg = g[i];
|
|
142
|
+
if (/^\{[^}]+\+\}$/.test(seg)) {
|
|
143
|
+
if (i !== g.length - 1 || r.length <= i) return null;
|
|
144
|
+
return { greedy: true, literals, prefixLength: i };
|
|
145
|
+
}
|
|
146
|
+
if (i >= r.length) return null;
|
|
147
|
+
if (/^\{[^}]+\}$/.test(seg)) continue;
|
|
148
|
+
if (seg !== r[i]) return null;
|
|
149
|
+
literals++;
|
|
150
|
+
}
|
|
151
|
+
if (g.length !== r.length) return null;
|
|
152
|
+
return { greedy: false, literals, prefixLength: g.length };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Select the route API Gateway would invoke: a non-greedy match beats a
|
|
157
|
+
* greedy one, then more literal segments, then the longer greedy prefix,
|
|
158
|
+
* then an explicit method beats ANY.
|
|
159
|
+
*/
|
|
160
|
+
function selectGatewayRoute(gatewayRoutes, method, requestPath) {
|
|
161
|
+
const candidates = gatewayRoutes
|
|
162
|
+
.filter(
|
|
163
|
+
(r) => r.method === 'ANY' || method === 'ANY' || r.method === method
|
|
164
|
+
)
|
|
165
|
+
.map((r) => ({
|
|
166
|
+
route: r,
|
|
167
|
+
match: matchGatewayPath(r.path, requestPath),
|
|
168
|
+
}))
|
|
169
|
+
.filter((c) => c.match);
|
|
170
|
+
candidates.sort(
|
|
171
|
+
(a, b) =>
|
|
172
|
+
Number(a.match.greedy) - Number(b.match.greedy) ||
|
|
173
|
+
b.match.literals - a.match.literals ||
|
|
174
|
+
b.match.prefixLength - a.match.prefixLength ||
|
|
175
|
+
Number(a.route.method === 'ANY') - Number(b.route.method === 'ANY')
|
|
176
|
+
);
|
|
177
|
+
return candidates.length ? candidates[0].route : null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
describe('Management API routes are reachable through API Gateway', () => {
|
|
181
|
+
let gatewayRoutes;
|
|
182
|
+
let managementRoutes;
|
|
183
|
+
let metaRoutes;
|
|
184
|
+
const originalArgv = process.argv;
|
|
185
|
+
|
|
186
|
+
beforeAll(async () => {
|
|
187
|
+
process.argv = ['node', 'test'];
|
|
188
|
+
process.env.AWS_REGION = 'us-east-1';
|
|
189
|
+
// The builders narrate their progress; keep the test output quiet.
|
|
190
|
+
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
191
|
+
const warnSpy = jest
|
|
192
|
+
.spyOn(console, 'warn')
|
|
193
|
+
.mockImplementation(() => {});
|
|
194
|
+
try {
|
|
195
|
+
const definition = await composeServerlessDefinition({
|
|
196
|
+
name: 'route-coverage-app',
|
|
197
|
+
provider: 'aws',
|
|
198
|
+
usePrismaLambdaLayer: false,
|
|
199
|
+
database: { postgres: { enable: true } },
|
|
200
|
+
websockets: { enable: true },
|
|
201
|
+
adminScripts: [
|
|
202
|
+
class ExampleScript {
|
|
203
|
+
static Definition = { name: 'example-script' };
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
admin: { includeBuiltinReports: true },
|
|
207
|
+
integrations: [
|
|
208
|
+
{
|
|
209
|
+
Definition: {
|
|
210
|
+
name: 'acme',
|
|
211
|
+
webhooks: true,
|
|
212
|
+
extensions: {
|
|
213
|
+
inbound: {
|
|
214
|
+
extension: {
|
|
215
|
+
routes: [
|
|
216
|
+
{ path: '/events', method: 'POST' },
|
|
217
|
+
],
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
// Integration names share the /api/ namespace with the
|
|
224
|
+
// Management API; make sure they don't shadow it.
|
|
225
|
+
{ Definition: { name: 'entities' } },
|
|
226
|
+
],
|
|
227
|
+
});
|
|
228
|
+
gatewayRoutes = getGatewayRoutes(definition);
|
|
229
|
+
} finally {
|
|
230
|
+
logSpy.mockRestore();
|
|
231
|
+
warnSpy.mockRestore();
|
|
232
|
+
}
|
|
233
|
+
managementRoutes = getAuthLambdaRoutes();
|
|
234
|
+
metaRoutes = getMetaRoutes();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
afterAll(() => {
|
|
238
|
+
process.argv = originalArgv;
|
|
239
|
+
delete process.env.AWS_REGION;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('reads the Management API routes from the live auth and health routers', () => {
|
|
243
|
+
const paths = managementRoutes.map((r) => r.path);
|
|
244
|
+
expect(paths).toEqual(
|
|
245
|
+
expect.arrayContaining([
|
|
246
|
+
'/api/integrations',
|
|
247
|
+
'/api/authorize',
|
|
248
|
+
'/api/entity',
|
|
249
|
+
'/api/entity/options/:credentialId',
|
|
250
|
+
'/api/entities/:entityId',
|
|
251
|
+
'/api/integrations/redirect/:appId',
|
|
252
|
+
'/api/v2/integrations',
|
|
253
|
+
'/api/v2/entities/types/:entityType/requirements',
|
|
254
|
+
'/api/v2/entities/:entityId/proxy',
|
|
255
|
+
'/api/v2/credentials/:credentialId/reauthorize',
|
|
256
|
+
'/api/v2/authorize',
|
|
257
|
+
])
|
|
258
|
+
);
|
|
259
|
+
expect(metaRoutes.map((r) => r.path)).toEqual(
|
|
260
|
+
expect.arrayContaining(['/api/meta', '/api/meta/openapi/v2.json'])
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('routes /api/meta to the DB-free health function', () => {
|
|
265
|
+
const unreachable = metaRoutes
|
|
266
|
+
.map(({ method, path: requestPath }) => {
|
|
267
|
+
const selected = selectGatewayRoute(gatewayRoutes, method, requestPath);
|
|
268
|
+
return { route: `${method} ${requestPath}`, ok: selected?.functionName === 'health' };
|
|
269
|
+
})
|
|
270
|
+
.filter((r) => !r.ok)
|
|
271
|
+
.map((r) => r.route);
|
|
272
|
+
expect(unreachable).toEqual([]);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('declares no /api/v2 catch-all', () => {
|
|
276
|
+
expect(
|
|
277
|
+
gatewayRoutes.filter((r) => r.path === '/api/v2/{proxy+}' || r.path === '/api/v2')
|
|
278
|
+
).toEqual([]);
|
|
279
|
+
expect(selectGatewayRoute(gatewayRoutes, 'PATCH', '/api/v2/reports/daily')).toBeNull();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('routes every Management API route to the auth function', () => {
|
|
283
|
+
const unreachable = managementRoutes
|
|
284
|
+
.map(({ method, path: expressPath }) => {
|
|
285
|
+
const requestPath = toConcretePath(expressPath);
|
|
286
|
+
const selected = selectGatewayRoute(
|
|
287
|
+
gatewayRoutes,
|
|
288
|
+
method,
|
|
289
|
+
requestPath
|
|
290
|
+
);
|
|
291
|
+
return {
|
|
292
|
+
route: `${method} ${expressPath}`,
|
|
293
|
+
reachedBy: selected
|
|
294
|
+
? `${selected.method} ${selected.path} -> ${selected.functionName}`
|
|
295
|
+
: 'no API Gateway route',
|
|
296
|
+
ok: selected && selected.functionName === 'auth',
|
|
297
|
+
};
|
|
298
|
+
})
|
|
299
|
+
.filter((r) => !r.ok)
|
|
300
|
+
.map(({ route, reachedBy }) => `${route} (${reachedBy})`);
|
|
301
|
+
|
|
302
|
+
expect(unreachable).toEqual([]);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it.each([
|
|
306
|
+
['GET', '/api/v2/reports', 'reportRouter'],
|
|
307
|
+
['POST', '/api/v2/reports/daily/run', 'reportRouter'],
|
|
308
|
+
['GET', '/api/v2/reports/daily', 'reportRouter'],
|
|
309
|
+
['GET', '/admin/scripts/example-script', 'adminScriptRouter'],
|
|
310
|
+
['POST', '/api/acme-integration/webhooks', 'acmeWebhook'],
|
|
311
|
+
['POST', '/api/acme-integration/inbound/events', 'acme__inbound'],
|
|
312
|
+
['GET', '/api/acme-integration/custom', 'acme'],
|
|
313
|
+
['GET', '/api/entities-integration/custom', 'entities'],
|
|
314
|
+
['POST', '/user/login', 'user'],
|
|
315
|
+
['GET', '/health/ready', 'health'],
|
|
316
|
+
])('leaves %s %s on %s', (method, requestPath, functionName) => {
|
|
317
|
+
const selected = selectGatewayRoute(gatewayRoutes, method, requestPath);
|
|
318
|
+
expect(selected && selected.functionName).toBe(functionName);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('declares no method + path pair on more than one function', () => {
|
|
322
|
+
const seen = new Map();
|
|
323
|
+
const duplicates = [];
|
|
324
|
+
for (const r of gatewayRoutes) {
|
|
325
|
+
const key = `${r.method} ${r.path}`;
|
|
326
|
+
if (seen.has(key) && seen.get(key) !== r.functionName) {
|
|
327
|
+
duplicates.push(
|
|
328
|
+
`${key}: ${seen.get(key)} and ${r.functionName}`
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
seen.set(key, r.functionName);
|
|
332
|
+
}
|
|
333
|
+
expect(duplicates).toEqual([]);
|
|
334
|
+
});
|
|
335
|
+
});
|