@friggframework/devtools 2.0.0-next.123 → 2.0.0-next.124

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.
Files changed (26) hide show
  1. package/frigg-cli/README.md +38 -1
  2. package/frigg-cli/__tests__/unit/commands/build.test.js +3 -0
  3. package/frigg-cli/__tests__/unit/commands/deploy.test.js +3 -0
  4. package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +92 -0
  5. package/frigg-cli/build-command/index.js +6 -0
  6. package/frigg-cli/deploy-command/index.js +6 -0
  7. package/frigg-cli/index.js +13 -1
  8. package/frigg-cli/init-command/backend-first-handler.js +48 -47
  9. package/frigg-cli/start-command/index.js +4 -0
  10. package/frigg-cli/start-command/start-command.test.js +3 -0
  11. package/frigg-cli/validate-command/__tests__/init-validation.test.js +63 -0
  12. package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +133 -0
  13. package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +443 -0
  14. package/frigg-cli/validate-command/__tests__/validate-command.test.js +262 -0
  15. package/frigg-cli/validate-command/checks/config.js +504 -0
  16. package/frigg-cli/validate-command/checks/integrations.js +274 -0
  17. package/frigg-cli/validate-command/format-report.js +58 -0
  18. package/frigg-cli/validate-command/index.js +107 -0
  19. package/frigg-cli/validate-command/load-app-definition.js +105 -0
  20. package/frigg-cli/validate-command/schema-issues.js +154 -0
  21. package/frigg-cli/validate-command/validate-app-definition.js +60 -0
  22. package/infrastructure/app-definition-schema-contract.test.js +629 -0
  23. package/infrastructure/create-frigg-infrastructure.js +4 -1
  24. package/infrastructure/create-frigg-infrastructure.test.js +51 -0
  25. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +60 -98
  26. package/package.json +7 -7
@@ -0,0 +1,504 @@
1
+ /**
2
+ * Semantic checks of the app definition: rules JSON Schema cannot express,
3
+ * each tied to what a builder or core does with the value (ADR-051).
4
+ *
5
+ * Every check receives the definition with the schema defaults applied
6
+ * (`definition`) and the definition as written (`raw`), so it can tell a
7
+ * value the app set from a default.
8
+ */
9
+
10
+ const { getSchema } = require('@friggframework/schemas');
11
+
12
+ const ENCRYPTION_BYPASS_STAGES = ['dev', 'test', 'local'];
13
+
14
+ function issue(severity, code, pointer, message, hint) {
15
+ return { severity, code, pointer, message, hint };
16
+ }
17
+
18
+ /** Keys the schema marks `deprecated` (nothing reads them) that the app sets. */
19
+ function checkDeprecatedKeys({ raw }) {
20
+ const issues = [];
21
+ const walk = (schemaNode, data, pointer) => {
22
+ if (
23
+ !schemaNode?.properties ||
24
+ data === null ||
25
+ typeof data !== 'object'
26
+ )
27
+ return;
28
+ for (const [key, child] of Object.entries(schemaNode.properties)) {
29
+ if (data[key] === undefined) continue;
30
+ const childPointer = `${pointer}/${key}`;
31
+ if (child.deprecated) {
32
+ const alias = key === 'password' && pointer === '/user';
33
+ issues.push(
34
+ issue(
35
+ 'warning',
36
+ alias ? 'deprecated-alias' : 'no-effect',
37
+ childPointer,
38
+ alias
39
+ ? 'user.password is the deprecated spelling of user.usePassword.'
40
+ : `${childPointer
41
+ .slice(1)
42
+ .replace(
43
+ /\//g,
44
+ '.'
45
+ )} is not read by Frigg: it has no effect.`,
46
+ alias ? 'Rename it to usePassword.' : 'Remove it.'
47
+ )
48
+ );
49
+ continue;
50
+ }
51
+ walk(child, data[key], childPointer);
52
+ }
53
+ };
54
+ walk(getSchema('app-definition'), raw, '');
55
+ return issues;
56
+ }
57
+
58
+ function checkManagement({ raw }) {
59
+ const issues = [];
60
+ const mode = raw.managementMode;
61
+ const vpc = raw.vpc || {};
62
+ const postgres = raw.database?.postgres || {};
63
+
64
+ if (mode === 'custom') {
65
+ issues.push(
66
+ issue(
67
+ 'warning',
68
+ 'no-effect',
69
+ '/managementMode',
70
+ "managementMode 'custom' behaves like 'discover'.",
71
+ "Use 'discover', or remove the key."
72
+ )
73
+ );
74
+ }
75
+ if (mode === 'managed' && raw.vpcIsolation === undefined) {
76
+ issues.push(
77
+ issue(
78
+ 'warning',
79
+ 'ambiguous-default',
80
+ '/vpcIsolation',
81
+ "managementMode 'managed' without vpcIsolation: the builders do not agree on a default (some treat it as 'shared', some as 'isolated').",
82
+ "Set vpcIsolation to 'isolated' (resources per stage) or 'shared'."
83
+ )
84
+ );
85
+ }
86
+ if (mode === 'managed') {
87
+ for (const [key, value] of [
88
+ ['management', vpc.management],
89
+ ['subnets/management', vpc.subnets?.management],
90
+ ['natGateway/management', vpc.natGateway?.management],
91
+ ['shareAcrossStages', vpc.shareAcrossStages],
92
+ ]) {
93
+ if (value !== undefined) {
94
+ issues.push(
95
+ issue(
96
+ 'warning',
97
+ 'conflicting-settings',
98
+ `/vpc/${key}`,
99
+ `managementMode 'managed' and vpc.${key.replace(
100
+ '/',
101
+ '.'
102
+ )} are both set. The build logs that it ignores vpc.${key.replace(
103
+ '/',
104
+ '.'
105
+ )}, but the VPC builder still applies it on top of the managed settings.`,
106
+ `Remove vpc.${key.replace(
107
+ '/',
108
+ '.'
109
+ )}, or drop managementMode 'managed'.`
110
+ )
111
+ );
112
+ }
113
+ }
114
+ }
115
+ if (
116
+ (mode === 'managed' || mode === 'existing') &&
117
+ postgres.management !== undefined &&
118
+ postgres.management !== 'external'
119
+ ) {
120
+ issues.push(
121
+ issue(
122
+ 'warning',
123
+ 'no-effect',
124
+ '/database/postgres/management',
125
+ `database.postgres.management is ignored when managementMode is '${mode}'.`,
126
+ 'Remove it.'
127
+ )
128
+ );
129
+ }
130
+
131
+ for (const [pointer, value, useful] of [
132
+ [
133
+ '/vpc/subnets/management',
134
+ vpc.subnets?.management,
135
+ "'create' or 'use-existing'",
136
+ ],
137
+ [
138
+ '/vpc/natGateway/management',
139
+ vpc.natGateway?.management,
140
+ "'createAndManage', or set natGateway.id",
141
+ ],
142
+ ]) {
143
+ if (['discover', 'create-new', 'existing'].includes(value)) {
144
+ issues.push(
145
+ issue(
146
+ 'warning',
147
+ 'no-effect',
148
+ pointer,
149
+ `${pointer
150
+ .slice(1)
151
+ .replace(
152
+ /\//g,
153
+ '.'
154
+ )} '${value}' has no effect: the VPC builder ignores it.`,
155
+ `Use ${useful}, or remove it.`
156
+ )
157
+ );
158
+ }
159
+ }
160
+
161
+ if (
162
+ vpc.enable === true &&
163
+ vpc.management === 'use-existing' &&
164
+ !vpc.vpcId &&
165
+ !vpc.ownership
166
+ ) {
167
+ issues.push(
168
+ issue(
169
+ 'error',
170
+ 'missing-key',
171
+ '/vpc/vpcId',
172
+ "vpc.management 'use-existing' needs vpc.vpcId; the VPC builder fails without it.",
173
+ 'Set vpc.vpcId (vpc-...).'
174
+ )
175
+ );
176
+ }
177
+ if (
178
+ vpc.enable === true &&
179
+ vpc.subnets?.management === 'use-existing' &&
180
+ !(Array.isArray(vpc.subnets.ids) && vpc.subnets.ids.length >= 2)
181
+ ) {
182
+ issues.push(
183
+ issue(
184
+ 'error',
185
+ 'missing-key',
186
+ '/vpc/subnets/ids',
187
+ "vpc.subnets.management 'use-existing' needs at least 2 subnet ids; the VPC builder fails without them.",
188
+ 'Set vpc.subnets.ids.'
189
+ )
190
+ );
191
+ }
192
+ if (
193
+ postgres.enable === true &&
194
+ postgres.management === 'use-existing' &&
195
+ !postgres.endpoint &&
196
+ mode !== 'managed' &&
197
+ mode !== 'existing'
198
+ ) {
199
+ issues.push(
200
+ issue(
201
+ 'error',
202
+ 'missing-key',
203
+ '/database/postgres/endpoint',
204
+ "database.postgres.management 'use-existing' needs database.postgres.endpoint; the Aurora builder fails without it.",
205
+ 'Set the endpoint host name.'
206
+ )
207
+ );
208
+ }
209
+ // 'external' is honoured in every mode (the Aurora builder checks it
210
+ // first). Under managementMode 'managed' or 'existing' the builder ignores
211
+ // 'use-existing' and still provisions or attaches a private cluster.
212
+ const postgresManagementIgnored = mode === 'managed' || mode === 'existing';
213
+ const provisionsAurora =
214
+ postgres.enable === true &&
215
+ postgres.management !== 'external' &&
216
+ (postgresManagementIgnored || postgres.management !== 'use-existing');
217
+ if (
218
+ provisionsAurora &&
219
+ vpc.enable !== true &&
220
+ postgres.publiclyAccessible !== true
221
+ ) {
222
+ issues.push(
223
+ issue(
224
+ 'warning',
225
+ 'unreachable-database',
226
+ '/vpc/enable',
227
+ 'Frigg provisions or discovers a private Aurora cluster, but the Lambda functions run outside a VPC and cannot reach it.',
228
+ postgresManagementIgnored
229
+ ? `Set vpc.enable: true, or use database.postgres.management: 'external' with a reachable DATABASE_URL (managementMode '${mode}' ignores 'use-existing').`
230
+ : "Set vpc.enable: true, or use database.postgres.management: 'use-existing' with a reachable endpoint (or 'external' with DATABASE_URL)."
231
+ )
232
+ );
233
+ }
234
+ return issues;
235
+ }
236
+
237
+ function checkDatabase({ raw }) {
238
+ const database = raw.database;
239
+ const enabled = ['postgres', 'mongoDB', 'documentDB'].filter(
240
+ (key) => database?.[key]?.enable === true
241
+ );
242
+ if (enabled.length === 0) {
243
+ return [
244
+ issue(
245
+ 'error',
246
+ 'no-database',
247
+ '/database',
248
+ 'No database is enabled. Core refuses to start without one (database.postgres, database.mongoDB or database.documentDB with enable: true).',
249
+ 'Add `database: { postgres: { enable: true } }` (or mongoDB / documentDB).'
250
+ ),
251
+ ];
252
+ }
253
+ if (enabled.length > 1) {
254
+ return [
255
+ issue(
256
+ 'warning',
257
+ 'several-databases',
258
+ '/database',
259
+ `Several databases are enabled (${enabled.join(
260
+ ', '
261
+ )}); core uses ${enabled[0]}.`,
262
+ 'Enable only the database the app uses.'
263
+ ),
264
+ ];
265
+ }
266
+ return [];
267
+ }
268
+
269
+ function checkEncryption({ raw, stage, env }) {
270
+ const issues = [];
271
+ const method = raw.encryption?.fieldLevelEncryptionMethod;
272
+ const environment = raw.environment || {};
273
+ const deployedStage = stage && !ENCRYPTION_BYPASS_STAGES.includes(stage);
274
+
275
+ if (method === 'aes') {
276
+ for (const key of ['AES_KEY_ID', 'AES_KEY']) {
277
+ if (environment[key] !== true) {
278
+ issues.push(
279
+ issue(
280
+ 'error',
281
+ 'missing-encryption-env',
282
+ `/environment/${key}`,
283
+ `fieldLevelEncryptionMethod 'aes' needs ${key} in the Lambda environment, and only keys listed in \`environment\` (as true) reach it. Without it, deployed stages store sensitive fields unencrypted.`,
284
+ `Add \`${key}: true\` to environment (it cannot be offloaded to SSM).`
285
+ )
286
+ );
287
+ } else if (deployedStage && !env[key]) {
288
+ issues.push(
289
+ issue(
290
+ 'warning',
291
+ 'missing-env-value',
292
+ `/environment/${key}`,
293
+ `${key} is not set in this shell; \`frigg deploy --stage ${stage}\` would deploy it empty.`,
294
+ `Export ${key} before deploying.`
295
+ )
296
+ );
297
+ }
298
+ }
299
+ }
300
+ if (
301
+ method === 'kms' &&
302
+ raw.managementMode !== 'managed' &&
303
+ raw.encryption?.createResourceIfNoneFound === undefined &&
304
+ !raw.encryption?.ownership
305
+ ) {
306
+ issues.push(
307
+ issue(
308
+ 'warning',
309
+ 'kms-key-source',
310
+ '/encryption/createResourceIfNoneFound',
311
+ 'With no KMS key found by discovery, KMS_KEY_ARN falls back to the AWS_DISCOVERY_KMS_KEY_ID environment variable.',
312
+ "Set createResourceIfNoneFound: true (or managementMode: 'managed') to let Frigg create the key."
313
+ )
314
+ );
315
+ }
316
+ if (method === undefined && deployedStage) {
317
+ issues.push(
318
+ issue(
319
+ 'warning',
320
+ 'no-field-encryption',
321
+ '/encryption/fieldLevelEncryptionMethod',
322
+ `No field-level encryption method: stage '${stage}' stores credentials and tokens unencrypted.`,
323
+ "Set encryption.fieldLevelEncryptionMethod to 'kms' (Frigg creates the key) or 'aes', or to 'none' to store them unencrypted on purpose."
324
+ )
325
+ );
326
+ }
327
+ if (
328
+ method !== undefined &&
329
+ method !== 'none' &&
330
+ stage &&
331
+ ENCRYPTION_BYPASS_STAGES.includes(stage)
332
+ ) {
333
+ issues.push(
334
+ issue(
335
+ 'warning',
336
+ 'encryption-bypassed',
337
+ '/encryption/fieldLevelEncryptionMethod',
338
+ `Core skips field-level encryption when STAGE is ${ENCRYPTION_BYPASS_STAGES.join(
339
+ ', '
340
+ )}; stage '${stage}' stores sensitive fields unencrypted even when deployed.`,
341
+ 'Deploy production data to another stage name (e.g. prod).'
342
+ )
343
+ );
344
+ }
345
+ return issues;
346
+ }
347
+
348
+ /**
349
+ * authModes.adopterJwt verifies tokens with user.jwtConfig.secret. The
350
+ * documented pattern is `secret: process.env.JWT_SECRET`, and build and
351
+ * deploy validate before `.env` is loaded, so an empty value only means the
352
+ * variable is unset in this shell; the Lambda may still receive it. A key
353
+ * missing from the definition is an error: nothing can supply it at runtime.
354
+ */
355
+ function checkJwtSecret(jwtConfig) {
356
+ const declared =
357
+ jwtConfig !== null &&
358
+ typeof jwtConfig === 'object' &&
359
+ Object.prototype.hasOwnProperty.call(jwtConfig, 'secret');
360
+ if (!declared) {
361
+ return [
362
+ issue(
363
+ 'error',
364
+ 'missing-key',
365
+ '/user/jwtConfig/secret',
366
+ 'authModes.adopterJwt needs user.jwtConfig.secret to verify tokens.',
367
+ 'Set user.jwtConfig: { secret: process.env.JWT_SECRET }.'
368
+ ),
369
+ ];
370
+ }
371
+ if (!jwtConfig.secret) {
372
+ return [
373
+ issue(
374
+ 'warning',
375
+ 'missing-env-value',
376
+ '/user/jwtConfig/secret',
377
+ 'user.jwtConfig.secret is empty here (its environment variable is not set in this shell). It must be set at runtime, or every adopter JWT is rejected.',
378
+ 'Make sure the variable (e.g. JWT_SECRET) reaches the deployed Lambdas: list it in `environment` and set it where you deploy.'
379
+ ),
380
+ ];
381
+ }
382
+ return [];
383
+ }
384
+
385
+ function checkUser({ definition, raw }) {
386
+ const issues = [];
387
+ const user = definition.user || {};
388
+ const authModes = user.authModes;
389
+
390
+ if (
391
+ user.individualUserRequired === false &&
392
+ user.organizationUserRequired !== true
393
+ ) {
394
+ issues.push(
395
+ issue(
396
+ 'error',
397
+ 'no-login',
398
+ '/user',
399
+ 'Neither individual nor organization users are required: POST /user/login always fails.',
400
+ 'Set user.individualUserRequired: true (the default) or user.organizationUserRequired: true.'
401
+ )
402
+ );
403
+ }
404
+ if (
405
+ raw.user?.organizationUserRequired === true &&
406
+ raw.user?.individualUserRequired === undefined
407
+ ) {
408
+ issues.push(
409
+ issue(
410
+ 'warning',
411
+ 'default-changed',
412
+ '/user/individualUserRequired',
413
+ 'individualUserRequired is not set, so it defaults to true: login and x-frigg headers look up an individual user first.',
414
+ 'For organization-only login, set user.individualUserRequired: false.'
415
+ )
416
+ );
417
+ }
418
+ if (authModes) {
419
+ const anyMode =
420
+ authModes.friggToken !== false ||
421
+ authModes.sharedSecret !== false ||
422
+ authModes.adopterJwt === true;
423
+ if (!anyMode) {
424
+ issues.push(
425
+ issue(
426
+ 'error',
427
+ 'no-auth-mode',
428
+ '/user/authModes',
429
+ 'Every authentication mode is disabled: no request can authenticate.',
430
+ 'Enable friggToken, sharedSecret or adopterJwt.'
431
+ )
432
+ );
433
+ }
434
+ if (authModes.adopterJwt === true) {
435
+ issues.push(...checkJwtSecret(raw.user?.jwtConfig));
436
+ }
437
+ if (
438
+ raw.user?.authModes?.sharedSecret === true &&
439
+ !raw.environment?.FRIGG_API_KEY
440
+ ) {
441
+ issues.push(
442
+ issue(
443
+ 'warning',
444
+ 'missing-env',
445
+ '/environment/FRIGG_API_KEY',
446
+ 'authModes.sharedSecret checks x-frigg-api-key against FRIGG_API_KEY, which is not listed in environment.',
447
+ "Add FRIGG_API_KEY: true (or 'ssm') to environment."
448
+ )
449
+ );
450
+ }
451
+ }
452
+ return issues;
453
+ }
454
+
455
+ function checkEnvironment({ raw }) {
456
+ const issues = [];
457
+ const environment = raw.environment || {};
458
+ if (raw.ssm?.enable !== true) {
459
+ for (const [key, value] of Object.entries(environment)) {
460
+ if (value === 'ssm') {
461
+ issues.push(
462
+ issue(
463
+ 'warning',
464
+ 'ssm-inactive',
465
+ `/environment/${key}`,
466
+ `${key} is marked 'ssm' but ssm.enable is not true, so it is passed in the Lambda environment instead.`,
467
+ 'Set ssm: { enable: true } to offload it.'
468
+ )
469
+ );
470
+ }
471
+ }
472
+ }
473
+ return issues;
474
+ }
475
+
476
+ const CHECKS = [
477
+ checkDeprecatedKeys,
478
+ checkManagement,
479
+ checkDatabase,
480
+ checkEncryption,
481
+ checkUser,
482
+ checkEnvironment,
483
+ ];
484
+
485
+ /**
486
+ * @param {object} context
487
+ * @param {object} context.definition app definition with defaults applied
488
+ * @param {object} context.raw app definition as written
489
+ * @param {string} [context.stage]
490
+ * @param {object} [context.env]
491
+ */
492
+ function checkConfig({ definition, raw, stage, env = process.env }) {
493
+ return CHECKS.flatMap((check) => check({ definition, raw, stage, env }));
494
+ }
495
+
496
+ module.exports = {
497
+ checkConfig,
498
+ checkDeprecatedKeys,
499
+ checkManagement,
500
+ checkDatabase,
501
+ checkEncryption,
502
+ checkUser,
503
+ checkEnvironment,
504
+ };