@friggframework/devtools 2.0.0--canary.658.e11f3b0.0 → 2.0.0--canary.737.83b4e17.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 +115 -31
- package/frigg-cli/__tests__/unit/commands/build.test.js +3 -0
- package/frigg-cli/__tests__/unit/commands/deploy.test.js +33 -24
- package/frigg-cli/__tests__/unit/commands/doctor.test.js +10 -6
- package/frigg-cli/__tests__/unit/commands/generate-iam.test.js +18 -0
- package/frigg-cli/__tests__/unit/commands/init.test.js +688 -0
- package/frigg-cli/__tests__/unit/commands/install.test.js +601 -341
- package/frigg-cli/__tests__/unit/commands/ui.test.js +31 -30
- package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +92 -0
- package/frigg-cli/__tests__/unit/dependencies.test.js +6 -2
- package/frigg-cli/build-command/index.js +6 -0
- package/frigg-cli/deploy-command/index.js +6 -0
- package/frigg-cli/generate-command/index.js +1 -0
- package/frigg-cli/generate-iam-command.js +1 -0
- package/frigg-cli/index.js +22 -5
- package/frigg-cli/init-command/backend-first-handler.js +395 -551
- package/frigg-cli/init-command/deployment-modes.js +7 -0
- package/frigg-cli/init-command/index.js +82 -35
- package/frigg-cli/init-command/safe-path.js +58 -0
- package/frigg-cli/init-command/scaffold-dependencies.js +202 -0
- package/frigg-cli/install-command/app-definition.js +174 -0
- package/frigg-cli/install-command/commit-changes.js +40 -8
- package/frigg-cli/install-command/environment-variables.js +15 -0
- package/frigg-cli/install-command/index.js +96 -21
- package/frigg-cli/install-command/install-package.js +77 -4
- package/frigg-cli/install-command/integration-file.js +20 -11
- package/frigg-cli/install-command/resolve-module.js +69 -0
- package/frigg-cli/install-command/template.js +102 -75
- package/frigg-cli/install-command/validate-package.js +36 -4
- package/frigg-cli/package.json +1 -1
- package/frigg-cli/start-command/index.js +4 -0
- package/frigg-cli/start-command/start-command.test.js +3 -0
- package/frigg-cli/templates/backend/README.md +267 -0
- package/frigg-cli/templates/backend/env.example +20 -0
- package/frigg-cli/templates/backend/gitignore +10 -0
- package/frigg-cli/templates/backend/index.js +114 -0
- package/frigg-cli/templates/backend/infrastructure.js +20 -0
- package/frigg-cli/test/init-command.test.js +9 -2
- package/frigg-cli/validate-command/__tests__/init-validation.test.js +63 -0
- package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +133 -0
- package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +443 -0
- package/frigg-cli/validate-command/__tests__/validate-command.test.js +262 -0
- package/frigg-cli/validate-command/checks/config.js +504 -0
- package/frigg-cli/validate-command/checks/integrations.js +274 -0
- package/frigg-cli/validate-command/format-report.js +58 -0
- package/frigg-cli/validate-command/index.js +107 -0
- package/frigg-cli/validate-command/load-app-definition.js +105 -0
- package/frigg-cli/validate-command/schema-issues.js +154 -0
- package/frigg-cli/validate-command/validate-app-definition.js +60 -0
- package/infrastructure/README.md +32 -5
- package/infrastructure/__tests__/helpers/bundled-prisma-engine-excludes.js +39 -0
- package/infrastructure/__tests__/management-api-gateway-routes.test.js +284 -0
- package/infrastructure/__tests__/postgres-config.test.js +245 -836
- package/infrastructure/__tests__/scaffold-default-infrastructure.test.js +151 -0
- package/infrastructure/__tests__/scoped-environment.test.js +3 -0
- package/infrastructure/__tests__/ssm-preload-node-options.test.js +3 -0
- package/infrastructure/__tests__/template-generation.test.js +39 -8
- package/infrastructure/app-definition-schema-contract.test.js +634 -0
- package/infrastructure/create-frigg-infrastructure.js +4 -1
- package/infrastructure/create-frigg-infrastructure.test.js +51 -0
- package/infrastructure/docs/POSTGRES-CONFIGURATION.md +80 -98
- package/infrastructure/domains/admin-scripts/admin-script-builder.js +8 -2
- package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +52 -6
- package/infrastructure/domains/database/aurora-builder.js +13 -2
- package/infrastructure/domains/database/aurora-builder.test.js +22 -0
- package/infrastructure/domains/database/migration-builder.js +21 -2
- package/infrastructure/domains/database/migration-builder.test.js +61 -6
- package/infrastructure/domains/database/migration-engines.js +93 -0
- package/infrastructure/domains/database/migration-engines.test.js +142 -0
- package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +12 -8
- package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +19 -7
- package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +12 -8
- package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +17 -18
- package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +12 -2
- package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +8 -1
- package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +4 -2
- package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +10 -0
- package/infrastructure/domains/integration/integration-builder.js +2 -0
- package/infrastructure/domains/integration/integration-builder.test.js +22 -6
- package/infrastructure/domains/networking/vpc-builder.js +18 -8
- package/infrastructure/domains/networking/vpc-builder.test.js +26 -12
- package/infrastructure/domains/security/iam-generator.js +112 -5
- package/infrastructure/domains/security/iam-generator.test.js +94 -0
- package/infrastructure/domains/shared/cloudformation-discovery.test.js +36 -6
- package/infrastructure/domains/shared/environment-builder.js +31 -5
- package/infrastructure/domains/shared/environment-builder.test.js +93 -7
- package/infrastructure/domains/shared/lambda-architectures.js +113 -0
- package/infrastructure/domains/shared/lambda-architectures.test.js +164 -0
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +39 -3
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +85 -5
- package/infrastructure/domains/shared/utilities/nested-node-modules.js +6 -5
- package/infrastructure/domains/shared/utilities/nested-node-modules.test.js +20 -26
- package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +92 -12
- package/infrastructure/domains/shared/utilities/prisma-layer-manager.test.js +158 -6
- package/infrastructure/infrastructure-composer.js +12 -1
- package/infrastructure/infrastructure-composer.test.js +339 -794
- package/infrastructure/scripts/build-prisma-layer.js +136 -50
- package/infrastructure/scripts/build-prisma-layer.test.js +128 -7
- package/infrastructure/scripts/build-time-discovery.test.js +18 -3
- package/package.json +12 -10
- package/frigg-cli/index.test.js +0 -158
- package/infrastructure/integration.test.js +0 -381
|
@@ -35,8 +35,32 @@ jest.mock('./domains/shared/resource-discovery', () => {
|
|
|
35
35
|
};
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
// The Prisma layer build runs `npm install` into packages/devtools/layers/prisma;
|
|
39
|
+
// a unit test must never do that (it took ~40 s and rewrote tracked files).
|
|
40
|
+
jest.mock('./domains/shared/utilities/prisma-layer-manager', () => ({
|
|
41
|
+
ensurePrismaLayerExists: jest.fn().mockResolvedValue(undefined),
|
|
42
|
+
}));
|
|
43
|
+
jest.mock('./domains/database/migration-engines', () => ({
|
|
44
|
+
ensureMigrationEngines: jest.fn().mockResolvedValue(undefined),
|
|
45
|
+
}));
|
|
46
|
+
|
|
38
47
|
const { gatherDiscoveredResources } = require('./domains/shared/resource-discovery');
|
|
39
48
|
|
|
49
|
+
// The orchestrator logs each builder's validation errors, then throws one
|
|
50
|
+
// summary error; assert both.
|
|
51
|
+
async function expectValidationError(appDefinition, message) {
|
|
52
|
+
const logSpy = jest.spyOn(console, 'log');
|
|
53
|
+
try {
|
|
54
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
55
|
+
'Infrastructure validation failed'
|
|
56
|
+
);
|
|
57
|
+
const logged = logSpy.mock.calls.map((args) => args.join(' ')).join('\n');
|
|
58
|
+
expect(logged).toContain(message);
|
|
59
|
+
} finally {
|
|
60
|
+
logSpy.mockRestore();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
40
64
|
describe('composeServerlessDefinition', () => {
|
|
41
65
|
let mockIntegration;
|
|
42
66
|
|
|
@@ -65,21 +89,11 @@ describe('composeServerlessDefinition', () => {
|
|
|
65
89
|
process.argv = ['node', 'test'];
|
|
66
90
|
});
|
|
67
91
|
|
|
92
|
+
// Whether discovery actually talks to AWS (no features need it, or
|
|
93
|
+
// FRIGG_SKIP_AWS_DISCOVERY=true) is decided inside gatherDiscoveredResources
|
|
94
|
+
// by shouldRunDiscovery(); see domains/shared/resource-discovery.test.js.
|
|
95
|
+
// The composer always calls it, so these tests only check the call happens.
|
|
68
96
|
describe('AWS discovery gating', () => {
|
|
69
|
-
it('should skip AWS discovery when no features require it', async () => {
|
|
70
|
-
gatherDiscoveredResources.mockClear();
|
|
71
|
-
|
|
72
|
-
const appDefinition = {
|
|
73
|
-
integrations: [],
|
|
74
|
-
vpc: { enable: false },
|
|
75
|
-
encryption: { fieldLevelEncryptionMethod: 'aes' },
|
|
76
|
-
ssm: { enable: false },
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
await composeServerlessDefinition(appDefinition);
|
|
80
|
-
|
|
81
|
-
expect(gatherDiscoveredResources).not.toHaveBeenCalled();
|
|
82
|
-
});
|
|
83
97
|
|
|
84
98
|
it('should run AWS discovery when VPC features are enabled', async () => {
|
|
85
99
|
gatherDiscoveredResources.mockClear();
|
|
@@ -94,22 +108,6 @@ describe('composeServerlessDefinition', () => {
|
|
|
94
108
|
expect(gatherDiscoveredResources).toHaveBeenCalledTimes(1);
|
|
95
109
|
});
|
|
96
110
|
|
|
97
|
-
it('should skip AWS discovery when FRIGG_SKIP_AWS_DISCOVERY is set to true', async () => {
|
|
98
|
-
gatherDiscoveredResources.mockClear();
|
|
99
|
-
process.env.FRIGG_SKIP_AWS_DISCOVERY = 'true';
|
|
100
|
-
|
|
101
|
-
const appDefinition = {
|
|
102
|
-
integrations: [],
|
|
103
|
-
vpc: { enable: true },
|
|
104
|
-
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
105
|
-
ssm: { enable: true },
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
await composeServerlessDefinition(appDefinition);
|
|
109
|
-
|
|
110
|
-
expect(gatherDiscoveredResources).not.toHaveBeenCalled();
|
|
111
|
-
});
|
|
112
|
-
|
|
113
111
|
it('should run AWS discovery when FRIGG_SKIP_AWS_DISCOVERY is not set', async () => {
|
|
114
112
|
gatherDiscoveredResources.mockClear();
|
|
115
113
|
delete process.env.FRIGG_SKIP_AWS_DISCOVERY;
|
|
@@ -420,11 +418,15 @@ describe('composeServerlessDefinition', () => {
|
|
|
420
418
|
});
|
|
421
419
|
|
|
422
420
|
it('should respect provided security group IDs when supplied', async () => {
|
|
421
|
+
// User-supplied security groups are honoured for an existing VPC
|
|
422
|
+
// (vpc.management 'use-existing'); every ID is attached, not just the first.
|
|
423
423
|
const appDefinition = {
|
|
424
424
|
vpc: {
|
|
425
425
|
enable: true,
|
|
426
|
-
management: '
|
|
426
|
+
management: 'use-existing',
|
|
427
|
+
vpcId: 'vpc-123456',
|
|
427
428
|
securityGroupIds: ['sg-custom-1', 'sg-custom-2'],
|
|
429
|
+
subnets: { ids: ['subnet-123456', 'subnet-789012'] },
|
|
428
430
|
},
|
|
429
431
|
integrations: [],
|
|
430
432
|
};
|
|
@@ -438,27 +440,25 @@ describe('composeServerlessDefinition', () => {
|
|
|
438
440
|
});
|
|
439
441
|
|
|
440
442
|
it('should throw when discover mode finds no subnets and self-heal is disabled', async () => {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
),
|
|
448
|
-
};
|
|
449
|
-
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
443
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
444
|
+
createDiscoveryResponse({
|
|
445
|
+
privateSubnetId1: null,
|
|
446
|
+
privateSubnetId2: null,
|
|
447
|
+
})
|
|
448
|
+
);
|
|
450
449
|
|
|
451
450
|
const appDefinition = {
|
|
452
451
|
vpc: {
|
|
453
452
|
enable: true,
|
|
454
453
|
management: 'discover',
|
|
454
|
+
selfHeal: false,
|
|
455
455
|
subnets: { management: 'discover' },
|
|
456
456
|
},
|
|
457
457
|
integrations: [],
|
|
458
458
|
};
|
|
459
459
|
|
|
460
460
|
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
461
|
-
'No subnets discovered
|
|
461
|
+
'No subnets discovered. Enable vpc.selfHeal, set subnets.management to "create", or provide subnet IDs.'
|
|
462
462
|
);
|
|
463
463
|
});
|
|
464
464
|
|
|
@@ -492,6 +492,11 @@ describe('composeServerlessDefinition', () => {
|
|
|
492
492
|
});
|
|
493
493
|
|
|
494
494
|
it('should create route tables for subnets even without NAT Gateway management', async () => {
|
|
495
|
+
// A fresh account: nothing to reuse, so the stack builds its own routing.
|
|
496
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
497
|
+
createDiscoveryResponse({ existingNatGatewayId: null })
|
|
498
|
+
);
|
|
499
|
+
|
|
495
500
|
const appDefinition = {
|
|
496
501
|
vpc: {
|
|
497
502
|
enable: true,
|
|
@@ -510,7 +515,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
510
515
|
expect(result.resources.Resources.FriggLambdaRouteTable).toBeDefined();
|
|
511
516
|
|
|
512
517
|
// Verify subnet associations
|
|
513
|
-
expect(result.resources.Resources.
|
|
518
|
+
expect(result.resources.Resources.FriggPublicSubnet1RouteTableAssociation).toBeDefined();
|
|
514
519
|
expect(result.resources.Resources.FriggPrivateSubnet1RouteTableAssociation).toBeDefined();
|
|
515
520
|
expect(result.resources.Resources.FriggPrivateSubnet2RouteTableAssociation).toBeDefined();
|
|
516
521
|
});
|
|
@@ -524,8 +529,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
524
529
|
integrations: []
|
|
525
530
|
};
|
|
526
531
|
|
|
527
|
-
await
|
|
528
|
-
|
|
532
|
+
await expectValidationError(
|
|
533
|
+
appDefinition,
|
|
534
|
+
'vpc.vpcId is required when management="use-existing"'
|
|
529
535
|
);
|
|
530
536
|
});
|
|
531
537
|
|
|
@@ -553,9 +559,11 @@ describe('composeServerlessDefinition', () => {
|
|
|
553
559
|
|
|
554
560
|
const result = await composeServerlessDefinition(appDefinition);
|
|
555
561
|
|
|
556
|
-
|
|
557
|
-
expect(
|
|
558
|
-
expect(
|
|
562
|
+
const s3Endpoint = result.resources.Resources.FriggS3VPCEndpoint;
|
|
563
|
+
expect(s3Endpoint).toBeDefined();
|
|
564
|
+
expect(s3Endpoint.Type).toBe('AWS::EC2::VPCEndpoint');
|
|
565
|
+
expect(s3Endpoint.Properties.VpcId).toBe('vpc-123456');
|
|
566
|
+
expect(s3Endpoint.Properties.VpcEndpointType).toBe('Gateway');
|
|
559
567
|
});
|
|
560
568
|
|
|
561
569
|
it('should skip creating VPC endpoints when disabled explicitly', async () => {
|
|
@@ -570,9 +578,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
570
578
|
|
|
571
579
|
const result = await composeServerlessDefinition(appDefinition);
|
|
572
580
|
|
|
573
|
-
expect(result.resources.Resources.
|
|
574
|
-
expect(result.resources.Resources.
|
|
575
|
-
expect(result.resources.Resources.
|
|
581
|
+
expect(result.resources.Resources.FriggS3VPCEndpoint).toBeUndefined();
|
|
582
|
+
expect(result.resources.Resources.FriggKMSVPCEndpoint).toBeUndefined();
|
|
583
|
+
expect(result.resources.Resources.FriggSecretsManagerVPCEndpoint).toBeUndefined();
|
|
576
584
|
});
|
|
577
585
|
|
|
578
586
|
it('should add Secrets Manager endpoint only when enabled', async () => {
|
|
@@ -588,7 +596,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
588
596
|
|
|
589
597
|
const result = await composeServerlessDefinition(appDefinition);
|
|
590
598
|
|
|
591
|
-
expect(result.resources.Resources.
|
|
599
|
+
expect(result.resources.Resources.FriggSecretsManagerVPCEndpoint).toBeDefined();
|
|
600
|
+
expect(result.resources.Resources.FriggKMSVPCEndpoint).toBeDefined();
|
|
592
601
|
});
|
|
593
602
|
|
|
594
603
|
it('should allow Lambda security group access for VPC endpoints when security group is discovered', async () => {
|
|
@@ -602,7 +611,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
602
611
|
};
|
|
603
612
|
|
|
604
613
|
const result = await composeServerlessDefinition(appDefinition);
|
|
605
|
-
const endpointSg = result.resources.Resources.
|
|
614
|
+
const endpointSg = result.resources.Resources.FriggVPCEndpointSecurityGroup;
|
|
606
615
|
|
|
607
616
|
expect(endpointSg).toBeDefined();
|
|
608
617
|
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
@@ -611,81 +620,39 @@ describe('composeServerlessDefinition', () => {
|
|
|
611
620
|
FromPort: 443,
|
|
612
621
|
ToPort: 443,
|
|
613
622
|
SourceSecurityGroupId: 'sg-123456',
|
|
614
|
-
Description: 'HTTPS from Lambda
|
|
623
|
+
Description: 'HTTPS from Lambda'
|
|
615
624
|
}
|
|
616
625
|
]);
|
|
617
626
|
});
|
|
618
627
|
|
|
619
|
-
it('should
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
}));
|
|
625
|
-
|
|
628
|
+
it('should pass an imported Lambda security group through to the endpoint ingress rule', async () => {
|
|
629
|
+
// CloudFormation resolves Fn::ImportValue to the group ID at deploy
|
|
630
|
+
// time, so the endpoint SG can reference it directly (the old
|
|
631
|
+
// VPC-CIDR fallback for unresolvable groups is no longer needed).
|
|
632
|
+
const importedSg = { 'Fn::ImportValue': 'shared-lambda-security-group' };
|
|
626
633
|
const appDefinition = {
|
|
627
634
|
vpc: {
|
|
628
635
|
enable: true,
|
|
629
|
-
management: '
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
},
|
|
634
|
-
],
|
|
635
|
-
},
|
|
636
|
-
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
637
|
-
integrations: [],
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
641
|
-
const endpointSg = result.resources.Resources.VPCEndpointSecurityGroup;
|
|
642
|
-
|
|
643
|
-
expect(endpointSg).toBeDefined();
|
|
644
|
-
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
645
|
-
{
|
|
646
|
-
IpProtocol: 'tcp',
|
|
647
|
-
FromPort: 443,
|
|
648
|
-
ToPort: 443,
|
|
649
|
-
CidrIp: '172.31.0.0/16',
|
|
650
|
-
Description: 'HTTPS from VPC CIDR (fallback)',
|
|
651
|
-
},
|
|
652
|
-
]);
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
it('should fall back to default private ranges when neither Lambda security group nor VPC CIDR is available', async () => {
|
|
656
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
657
|
-
discoverResources: jest
|
|
658
|
-
.fn()
|
|
659
|
-
.mockResolvedValue(
|
|
660
|
-
createDiscoveryResponse({ vpcCidr: null })
|
|
661
|
-
),
|
|
662
|
-
}));
|
|
663
|
-
|
|
664
|
-
const appDefinition = {
|
|
665
|
-
vpc: {
|
|
666
|
-
enable: true,
|
|
667
|
-
management: 'discover',
|
|
668
|
-
securityGroupIds: [
|
|
669
|
-
{
|
|
670
|
-
'Fn::ImportValue': 'shared-lambda-security-group',
|
|
671
|
-
},
|
|
672
|
-
],
|
|
636
|
+
management: 'use-existing',
|
|
637
|
+
vpcId: 'vpc-123456',
|
|
638
|
+
securityGroupIds: [importedSg],
|
|
639
|
+
subnets: { ids: ['subnet-123456', 'subnet-789012'] },
|
|
673
640
|
},
|
|
674
641
|
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
675
642
|
integrations: [],
|
|
676
643
|
};
|
|
677
644
|
|
|
678
645
|
const result = await composeServerlessDefinition(appDefinition);
|
|
679
|
-
const endpointSg = result.resources.Resources.
|
|
646
|
+
const endpointSg = result.resources.Resources.FriggVPCEndpointSecurityGroup;
|
|
680
647
|
|
|
681
|
-
expect(
|
|
648
|
+
expect(result.provider.vpc.securityGroupIds).toEqual([importedSg]);
|
|
682
649
|
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
683
650
|
{
|
|
684
651
|
IpProtocol: 'tcp',
|
|
685
652
|
FromPort: 443,
|
|
686
653
|
ToPort: 443,
|
|
687
|
-
|
|
688
|
-
Description: 'HTTPS from
|
|
654
|
+
SourceSecurityGroupId: importedSg,
|
|
655
|
+
Description: 'HTTPS from Lambda',
|
|
689
656
|
},
|
|
690
657
|
]);
|
|
691
658
|
});
|
|
@@ -710,14 +677,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
710
677
|
FromPort: 443,
|
|
711
678
|
ToPort: 443,
|
|
712
679
|
SourceSecurityGroupId: { Ref: 'FriggLambdaSecurityGroup' },
|
|
713
|
-
Description: 'HTTPS from Lambda
|
|
714
|
-
},
|
|
715
|
-
{
|
|
716
|
-
IpProtocol: 'tcp',
|
|
717
|
-
FromPort: 443,
|
|
718
|
-
ToPort: 443,
|
|
719
|
-
CidrIp: '10.0.0.0/16',
|
|
720
|
-
Description: 'HTTPS from VPC CIDR (fallback)'
|
|
680
|
+
Description: 'HTTPS from Lambda'
|
|
721
681
|
}
|
|
722
682
|
]);
|
|
723
683
|
});
|
|
@@ -731,7 +691,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
731
691
|
const result = await composeServerlessDefinition(appDefinition);
|
|
732
692
|
|
|
733
693
|
expect(result.provider.vpc).toBeUndefined();
|
|
734
|
-
expect(result.resources.Resources.
|
|
694
|
+
expect(result.resources.Resources.FriggS3VPCEndpoint).toBeUndefined();
|
|
735
695
|
});
|
|
736
696
|
|
|
737
697
|
it('should not add VPC configuration when vpc is not defined', async () => {
|
|
@@ -745,116 +705,87 @@ describe('composeServerlessDefinition', () => {
|
|
|
745
705
|
});
|
|
746
706
|
});
|
|
747
707
|
|
|
708
|
+
// NAT placement rules (never reuse a NAT in a private subnet, self-heal,
|
|
709
|
+
// EIP reuse, route-table association repair) are VpcBuilder decisions and
|
|
710
|
+
// are covered in domains/networking/vpc-builder.test.js ('NAT Gateway
|
|
711
|
+
// handling') and vpc-resolver.test.js. These check the composed template.
|
|
748
712
|
describe('NAT Gateway behaviour', () => {
|
|
749
|
-
it('should
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
),
|
|
757
|
-
};
|
|
758
|
-
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
713
|
+
it('should route through a discovered NAT gateway without creating a new one', async () => {
|
|
714
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
715
|
+
createDiscoveryResponse({
|
|
716
|
+
existingNatGatewayId: 'nat-existing123',
|
|
717
|
+
natGatewayInPrivateSubnet: false,
|
|
718
|
+
})
|
|
719
|
+
);
|
|
759
720
|
|
|
760
|
-
const
|
|
721
|
+
const result = await composeServerlessDefinition({
|
|
761
722
|
vpc: {
|
|
762
723
|
enable: true,
|
|
763
724
|
management: 'discover',
|
|
764
725
|
natGateway: { management: 'discover' },
|
|
765
726
|
},
|
|
766
727
|
integrations: [],
|
|
767
|
-
};
|
|
768
|
-
|
|
769
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
728
|
+
});
|
|
770
729
|
|
|
771
730
|
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
772
|
-
expect(result.resources.Resources.
|
|
731
|
+
expect(result.resources.Resources.FriggPrivateRoute.Properties).toEqual({
|
|
732
|
+
RouteTableId: { Ref: 'FriggLambdaRouteTable' },
|
|
733
|
+
DestinationCidrBlock: '0.0.0.0/0',
|
|
734
|
+
NatGatewayId: 'nat-existing123',
|
|
735
|
+
});
|
|
773
736
|
});
|
|
774
737
|
|
|
775
|
-
it('should
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
),
|
|
780
|
-
};
|
|
781
|
-
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
738
|
+
it('should route through a provided NAT gateway ID', async () => {
|
|
739
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
740
|
+
createDiscoveryResponse({ existingNatGatewayId: null })
|
|
741
|
+
);
|
|
782
742
|
|
|
783
|
-
const
|
|
743
|
+
const result = await composeServerlessDefinition({
|
|
784
744
|
vpc: {
|
|
785
745
|
enable: true,
|
|
786
746
|
management: 'discover',
|
|
787
747
|
natGateway: { management: 'useExisting', id: 'nat-custom-001' },
|
|
788
748
|
},
|
|
789
749
|
integrations: [],
|
|
790
|
-
};
|
|
791
|
-
|
|
792
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
750
|
+
});
|
|
793
751
|
|
|
794
752
|
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
795
|
-
expect(result.resources.Resources.
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
it('should reuse existing elastic IP allocation when creating managed NAT', async () => {
|
|
799
|
-
const discoveryInstance = {
|
|
800
|
-
discoverResources: jest.fn().mockResolvedValue(
|
|
801
|
-
createDiscoveryResponse({
|
|
802
|
-
existingNatGatewayId: null,
|
|
803
|
-
existingElasticIpAllocationId: 'eip-alloc-123',
|
|
804
|
-
})
|
|
805
|
-
),
|
|
806
|
-
};
|
|
807
|
-
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
808
|
-
|
|
809
|
-
const appDefinition = {
|
|
810
|
-
vpc: {
|
|
811
|
-
enable: true,
|
|
812
|
-
management: 'discover',
|
|
813
|
-
natGateway: { management: 'createAndManage' },
|
|
814
|
-
selfHeal: true,
|
|
815
|
-
},
|
|
816
|
-
integrations: [],
|
|
817
|
-
};
|
|
818
|
-
|
|
819
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
820
|
-
|
|
821
|
-
expect(result.resources.Resources.FriggNATGatewayEIP).toBeUndefined();
|
|
822
|
-
expect(result.resources.Resources.FriggNATGateway.Properties.AllocationId).toBe(
|
|
823
|
-
'eip-alloc-123'
|
|
753
|
+
expect(result.resources.Resources.FriggPrivateRoute.Properties.NatGatewayId).toBe(
|
|
754
|
+
'nat-custom-001'
|
|
824
755
|
);
|
|
825
756
|
});
|
|
826
757
|
|
|
827
|
-
it('should create a public subnet
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
publicSubnetId: null,
|
|
832
|
-
internetGatewayId: null,
|
|
833
|
-
existingNatGatewayId: null,
|
|
834
|
-
})
|
|
835
|
-
),
|
|
836
|
-
};
|
|
837
|
-
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
758
|
+
it('should create a retained NAT gateway in a public subnet with createAndManage', async () => {
|
|
759
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
760
|
+
createDiscoveryResponse({ existingNatGatewayId: null })
|
|
761
|
+
);
|
|
838
762
|
|
|
839
|
-
const
|
|
763
|
+
const result = await composeServerlessDefinition({
|
|
840
764
|
vpc: {
|
|
841
765
|
enable: true,
|
|
842
766
|
management: 'discover',
|
|
843
767
|
natGateway: { management: 'createAndManage' },
|
|
844
|
-
selfHeal: true,
|
|
845
768
|
},
|
|
846
769
|
integrations: [],
|
|
847
|
-
};
|
|
848
|
-
|
|
849
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
770
|
+
});
|
|
850
771
|
|
|
851
|
-
|
|
852
|
-
expect(
|
|
772
|
+
const resources = result.resources.Resources;
|
|
773
|
+
expect(resources.FriggNATGateway.Type).toBe('AWS::EC2::NatGateway');
|
|
774
|
+
expect(resources.FriggNATGateway.DeletionPolicy).toBe('Retain');
|
|
775
|
+
expect(resources.FriggNATGateway.UpdateReplacePolicy).toBe('Retain');
|
|
776
|
+
expect(resources.FriggNATGateway.Properties.SubnetId).toBe('subnet-public-1');
|
|
777
|
+
expect(resources.FriggNATGatewayEIP).toBeDefined();
|
|
778
|
+
expect(resources.FriggPublicRoute).toBeDefined();
|
|
779
|
+
expect(resources.FriggPrivateRoute.Properties.NatGatewayId).toEqual({
|
|
780
|
+
Ref: 'FriggNATGateway',
|
|
781
|
+
});
|
|
853
782
|
});
|
|
854
783
|
});
|
|
855
784
|
|
|
856
785
|
describe('KMS Configuration', () => {
|
|
857
786
|
it('should add KMS configuration when encryption is enabled and key is found', async () => {
|
|
787
|
+
const keyArn =
|
|
788
|
+
'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012';
|
|
858
789
|
const appDefinition = {
|
|
859
790
|
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
860
791
|
integrations: []
|
|
@@ -862,7 +793,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
862
793
|
|
|
863
794
|
const result = await composeServerlessDefinition(appDefinition);
|
|
864
795
|
|
|
865
|
-
//
|
|
796
|
+
// IAM: the Lambda role uses the discovered key directly (no
|
|
797
|
+
// serverless-kms-grants plugin, which KmsBuilder no longer uses).
|
|
866
798
|
const kmsPermission = result.provider.iamRoleStatements.find(
|
|
867
799
|
statement => statement.Action.includes('kms:GenerateDataKey')
|
|
868
800
|
);
|
|
@@ -870,49 +802,26 @@ describe('composeServerlessDefinition', () => {
|
|
|
870
802
|
Effect: 'Allow',
|
|
871
803
|
Action: [
|
|
872
804
|
'kms:GenerateDataKey',
|
|
873
|
-
'kms:Decrypt'
|
|
805
|
+
'kms:Decrypt',
|
|
806
|
+
'kms:Encrypt',
|
|
807
|
+
'kms:DescribeKey'
|
|
874
808
|
],
|
|
875
|
-
Resource:
|
|
876
|
-
});
|
|
877
|
-
|
|
878
|
-
// Check environment variable
|
|
879
|
-
expect(result.provider.environment.KMS_KEY_ARN).toBe('arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012');
|
|
880
|
-
|
|
881
|
-
// Check plugin
|
|
882
|
-
expect(result.plugins).toContain('serverless-kms-grants');
|
|
883
|
-
|
|
884
|
-
// Check custom configuration
|
|
885
|
-
expect(result.custom.kmsGrants).toEqual({
|
|
886
|
-
kmsKeyId: 'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012'
|
|
809
|
+
Resource: keyArn
|
|
887
810
|
});
|
|
888
811
|
|
|
889
|
-
|
|
890
|
-
expect(result.
|
|
891
|
-
expect(result.
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
Properties: {
|
|
895
|
-
AliasName: 'alias/${self:service}-${self:provider.stage}-frigg-kms',
|
|
896
|
-
TargetKeyId: 'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012'
|
|
897
|
-
}
|
|
898
|
-
});
|
|
812
|
+
expect(result.provider.environment.KMS_KEY_ARN).toBe(keyArn);
|
|
813
|
+
expect(result.plugins).not.toContain('serverless-kms-grants');
|
|
814
|
+
expect(result.custom.kmsGrants).toBeUndefined();
|
|
815
|
+
// A discovered key is referenced, not re-declared in the stack.
|
|
816
|
+
expect(result.resources.Resources.FriggKMSKey).toBeUndefined();
|
|
899
817
|
});
|
|
900
818
|
|
|
901
819
|
it('should create new KMS key when encryption is enabled, no key found, and createResourceIfNoneFound is true', async () => {
|
|
902
|
-
//
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
privateSubnetId1: 'subnet-123456',
|
|
908
|
-
privateSubnetId2: 'subnet-789012',
|
|
909
|
-
publicSubnetId: 'subnet-public',
|
|
910
|
-
defaultRouteTableId: 'rtb-123456',
|
|
911
|
-
defaultKmsKeyId: null // No KMS key found
|
|
912
|
-
});
|
|
913
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
914
|
-
discoverResources: mockDiscoverResources
|
|
915
|
-
}));
|
|
820
|
+
// Key policy, rotation and alias details are covered in
|
|
821
|
+
// domains/security/kms-builder.test.js.
|
|
822
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
823
|
+
createDiscoveryResponse({ defaultKmsKeyId: null })
|
|
824
|
+
);
|
|
916
825
|
|
|
917
826
|
const appDefinition = {
|
|
918
827
|
encryption: {
|
|
@@ -924,116 +833,25 @@ describe('composeServerlessDefinition', () => {
|
|
|
924
833
|
|
|
925
834
|
const result = await composeServerlessDefinition(appDefinition);
|
|
926
835
|
|
|
927
|
-
|
|
928
|
-
expect(
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
UpdateReplacePolicy: 'Retain',
|
|
932
|
-
Properties: {
|
|
933
|
-
EnableKeyRotation: true,
|
|
934
|
-
Description: 'Frigg KMS key for field-level encryption',
|
|
935
|
-
KeyPolicy: {
|
|
936
|
-
Version: '2012-10-17',
|
|
937
|
-
Statement: [
|
|
938
|
-
{
|
|
939
|
-
Sid: 'AllowRootAccountAdmin',
|
|
940
|
-
Effect: 'Allow',
|
|
941
|
-
Principal: {
|
|
942
|
-
AWS: {
|
|
943
|
-
'Fn::Sub': 'arn:aws:iam::${AWS::AccountId}:root'
|
|
944
|
-
}
|
|
945
|
-
},
|
|
946
|
-
Action: 'kms:*',
|
|
947
|
-
Resource: '*'
|
|
948
|
-
},
|
|
949
|
-
{
|
|
950
|
-
Sid: 'AllowLambdaService',
|
|
951
|
-
Effect: 'Allow',
|
|
952
|
-
Principal: {
|
|
953
|
-
Service: 'lambda.amazonaws.com'
|
|
954
|
-
},
|
|
955
|
-
Action: [
|
|
956
|
-
'kms:GenerateDataKey',
|
|
957
|
-
'kms:Decrypt',
|
|
958
|
-
'kms:DescribeKey'
|
|
959
|
-
],
|
|
960
|
-
Resource: '*',
|
|
961
|
-
Condition: {
|
|
962
|
-
StringEquals: {
|
|
963
|
-
'kms:ViaService': 'lambda.us-east-1.amazonaws.com'
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
]
|
|
968
|
-
},
|
|
969
|
-
Tags: [
|
|
970
|
-
{
|
|
971
|
-
Key: 'Name',
|
|
972
|
-
Value: '${self:service}-${self:provider.stage}-frigg-kms-key'
|
|
973
|
-
},
|
|
974
|
-
{
|
|
975
|
-
Key: 'ManagedBy',
|
|
976
|
-
Value: 'Frigg'
|
|
977
|
-
},
|
|
978
|
-
{
|
|
979
|
-
Key: 'Purpose',
|
|
980
|
-
Value: 'Field-level encryption for Frigg application'
|
|
981
|
-
}
|
|
982
|
-
]
|
|
983
|
-
}
|
|
984
|
-
});
|
|
836
|
+
const key = result.resources.Resources.FriggKMSKey;
|
|
837
|
+
expect(key.Type).toBe('AWS::KMS::Key');
|
|
838
|
+
expect(key.DeletionPolicy).toBe('Retain');
|
|
839
|
+
expect(key.Properties.EnableKeyRotation).toBe(true);
|
|
985
840
|
|
|
986
|
-
|
|
987
|
-
expect(result.resources.Resources.FriggKMSKeyAlias).toBeDefined();
|
|
988
|
-
expect(result.resources.Resources.FriggKMSKeyAlias).toEqual({
|
|
989
|
-
Type: 'AWS::KMS::Alias',
|
|
990
|
-
DeletionPolicy: 'Retain',
|
|
991
|
-
Properties: {
|
|
992
|
-
AliasName: 'alias/${self:service}-${self:provider.stage}-frigg-kms',
|
|
993
|
-
TargetKeyId: { 'Fn::GetAtt': ['FriggKMSKey', 'Arn'] }
|
|
994
|
-
}
|
|
995
|
-
});
|
|
996
|
-
|
|
997
|
-
// Check IAM permissions for the new key
|
|
841
|
+
const keyRef = { 'Fn::GetAtt': ['FriggKMSKey', 'Arn'] };
|
|
998
842
|
const kmsPermission = result.provider.iamRoleStatements.find(
|
|
999
843
|
statement => statement.Action.includes('kms:GenerateDataKey')
|
|
1000
844
|
);
|
|
1001
|
-
expect(kmsPermission).toEqual(
|
|
1002
|
-
|
|
1003
|
-
Action: ['kms:GenerateDataKey', 'kms:Decrypt'],
|
|
1004
|
-
Resource: [{ 'Fn::GetAtt': ['FriggKMSKey', 'Arn'] }]
|
|
1005
|
-
});
|
|
1006
|
-
|
|
1007
|
-
// Check environment variable
|
|
1008
|
-
expect(result.provider.environment.KMS_KEY_ARN).toEqual({
|
|
1009
|
-
'Fn::GetAtt': ['FriggKMSKey', 'Arn']
|
|
1010
|
-
});
|
|
1011
|
-
|
|
1012
|
-
// Check plugin
|
|
1013
|
-
expect(result.plugins).toContain('serverless-kms-grants');
|
|
1014
|
-
|
|
1015
|
-
// Check custom configuration
|
|
1016
|
-
// When creating a new key, it should reference the CloudFormation resource
|
|
1017
|
-
expect(result.custom.kmsGrants).toEqual({
|
|
1018
|
-
kmsKeyId: { 'Fn::GetAtt': ['FriggKMSKey', 'Arn'] }
|
|
1019
|
-
});
|
|
845
|
+
expect(kmsPermission.Resource).toEqual(keyRef);
|
|
846
|
+
expect(result.provider.environment.KMS_KEY_ARN).toEqual(keyRef);
|
|
1020
847
|
});
|
|
1021
848
|
|
|
1022
|
-
it('should
|
|
1023
|
-
//
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
privateSubnetId1: 'subnet-123456',
|
|
1029
|
-
privateSubnetId2: 'subnet-789012',
|
|
1030
|
-
publicSubnetId: 'subnet-public',
|
|
1031
|
-
defaultRouteTableId: 'rtb-123456',
|
|
1032
|
-
defaultKmsKeyId: null // No KMS key found
|
|
1033
|
-
});
|
|
1034
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
1035
|
-
discoverResources: mockDiscoverResources
|
|
1036
|
-
}));
|
|
849
|
+
it('should fall back to the AWS_DISCOVERY_KMS_KEY_ID env reference when encryption is enabled, no key found, and createResourceIfNoneFound is false', async () => {
|
|
850
|
+
// KmsBuilder no longer fails the build: it points the app at the
|
|
851
|
+
// key ID the deploy environment provides (AWS_DISCOVERY_KMS_KEY_ID).
|
|
852
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
853
|
+
createDiscoveryResponse({ defaultKmsKeyId: null })
|
|
854
|
+
);
|
|
1037
855
|
|
|
1038
856
|
const appDefinition = {
|
|
1039
857
|
encryption: {
|
|
@@ -1043,27 +861,22 @@ describe('composeServerlessDefinition', () => {
|
|
|
1043
861
|
integrations: []
|
|
1044
862
|
};
|
|
1045
863
|
|
|
1046
|
-
await
|
|
1047
|
-
|
|
1048
|
-
|
|
864
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
865
|
+
|
|
866
|
+
// An explicit `false` keeps the legacy raw env reference (see
|
|
867
|
+
// kms-builder.test.js 'Error handling').
|
|
868
|
+
expect(result.resources.Resources.FriggKMSKey).toBeUndefined();
|
|
869
|
+
expect(result.provider.environment.KMS_KEY_ARN).toBe(
|
|
870
|
+
'${env:AWS_DISCOVERY_KMS_KEY_ID}'
|
|
1049
871
|
);
|
|
1050
872
|
});
|
|
1051
873
|
|
|
1052
|
-
it('should
|
|
1053
|
-
//
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
privateSubnetId1: 'subnet-123456',
|
|
1059
|
-
privateSubnetId2: 'subnet-789012',
|
|
1060
|
-
publicSubnetId: 'subnet-public',
|
|
1061
|
-
defaultRouteTableId: 'rtb-123456',
|
|
1062
|
-
defaultKmsKeyId: null // No KMS key found
|
|
1063
|
-
});
|
|
1064
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
1065
|
-
discoverResources: mockDiscoverResources
|
|
1066
|
-
}));
|
|
874
|
+
it('should fall back to the AWS_DISCOVERY_KMS_KEY_ID env reference when encryption is enabled, no key found, and createResourceIfNoneFound is not specified', async () => {
|
|
875
|
+
// KmsBuilder no longer fails the build: it points the app at the
|
|
876
|
+
// key ID the deploy environment provides (AWS_DISCOVERY_KMS_KEY_ID).
|
|
877
|
+
gatherDiscoveredResources.mockResolvedValue(
|
|
878
|
+
createDiscoveryResponse({ defaultKmsKeyId: null })
|
|
879
|
+
);
|
|
1067
880
|
|
|
1068
881
|
const appDefinition = {
|
|
1069
882
|
encryption: {
|
|
@@ -1073,10 +886,12 @@ describe('composeServerlessDefinition', () => {
|
|
|
1073
886
|
integrations: []
|
|
1074
887
|
};
|
|
1075
888
|
|
|
1076
|
-
await
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
889
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
890
|
+
|
|
891
|
+
const fallbackArn =
|
|
892
|
+
'arn:aws:kms:${self:provider.region}:${aws:accountId}:key/${env:AWS_DISCOVERY_KMS_KEY_ID}';
|
|
893
|
+
expect(result.resources.Resources.FriggKMSKey).toBeUndefined();
|
|
894
|
+
expect(result.provider.environment.KMS_KEY_ARN).toBe(fallbackArn);
|
|
1080
895
|
});
|
|
1081
896
|
|
|
1082
897
|
it('should not add KMS configuration when encryption is disabled', async () => {
|
|
@@ -1234,8 +1049,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
1234
1049
|
|
|
1235
1050
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1236
1051
|
|
|
1237
|
-
// Check integration function
|
|
1238
|
-
|
|
1052
|
+
// Check integration function (packaging/layers are covered by the
|
|
1053
|
+
// base-definition and integration-builder tests)
|
|
1054
|
+
expect(result.functions.testIntegration).toEqual(expect.objectContaining({
|
|
1239
1055
|
handler: 'node_modules/@friggframework/core/handlers/routers/integration-defined-routers.handlers.testIntegration.handler',
|
|
1240
1056
|
events: [{
|
|
1241
1057
|
httpApi: {
|
|
@@ -1243,7 +1059,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
1243
1059
|
method: 'ANY'
|
|
1244
1060
|
}
|
|
1245
1061
|
}]
|
|
1246
|
-
});
|
|
1062
|
+
}));
|
|
1247
1063
|
|
|
1248
1064
|
// Check SQS Queue
|
|
1249
1065
|
expect(result.resources.Resources.TestIntegrationQueue).toEqual({
|
|
@@ -1262,7 +1078,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
1262
1078
|
});
|
|
1263
1079
|
|
|
1264
1080
|
// Check Queue Worker
|
|
1265
|
-
expect(result.functions.testIntegrationQueueWorker).toEqual({
|
|
1081
|
+
expect(result.functions.testIntegrationQueueWorker).toEqual(expect.objectContaining({
|
|
1266
1082
|
handler: 'node_modules/@friggframework/core/handlers/workers/integration-defined-workers.handlers.testIntegration.queueWorker',
|
|
1267
1083
|
reservedConcurrency: 20,
|
|
1268
1084
|
events: [{
|
|
@@ -1274,8 +1090,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
1274
1090
|
functionResponseType: 'ReportBatchItemFailures'
|
|
1275
1091
|
}
|
|
1276
1092
|
}],
|
|
1277
|
-
|
|
1278
|
-
|
|
1093
|
+
// Lambda maximum; the queue's VisibilityTimeout stays above it
|
|
1094
|
+
timeout: 900
|
|
1095
|
+
}));
|
|
1279
1096
|
|
|
1280
1097
|
// Check environment variable
|
|
1281
1098
|
expect(result.provider.environment.TESTINTEGRATION_QUEUE_URL).toEqual({
|
|
@@ -1312,146 +1129,20 @@ describe('composeServerlessDefinition', () => {
|
|
|
1312
1129
|
const fs = require('fs');
|
|
1313
1130
|
const path = require('path');
|
|
1314
1131
|
|
|
1315
|
-
it('should
|
|
1132
|
+
it('should keep node_modules handler paths in offline mode', async () => {
|
|
1133
|
+
// serverless-offline resolves node_modules from the working
|
|
1134
|
+
// directory, so handler paths are no longer rewritten to ../node_modules.
|
|
1316
1135
|
process.argv = ['node', 'test', 'offline'];
|
|
1317
|
-
const existsSpy = jest.spyOn(fs, 'existsSync');
|
|
1318
|
-
const fallbackNodeModules = path.resolve(process.cwd(), '..', 'node_modules');
|
|
1319
|
-
existsSpy.mockImplementation((p) => p === fallbackNodeModules);
|
|
1320
|
-
|
|
1321
|
-
const appDefinition = { integrations: [] };
|
|
1322
|
-
|
|
1323
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1324
1136
|
|
|
1325
|
-
|
|
1326
|
-
expect(result.functions.auth.handler.startsWith('../node_modules/')).toBe(true);
|
|
1137
|
+
const result = await composeServerlessDefinition({ integrations: [] });
|
|
1327
1138
|
|
|
1328
|
-
|
|
1139
|
+
expect(result.functions.auth.handler).toBe(
|
|
1140
|
+
'node_modules/@friggframework/core/handlers/routers/auth.handler'
|
|
1141
|
+
);
|
|
1329
1142
|
});
|
|
1330
1143
|
});
|
|
1331
1144
|
|
|
1332
1145
|
describe('NAT Gateway Management', () => {
|
|
1333
|
-
it('should handle NAT Gateway with createAndManage mode', async () => {
|
|
1334
|
-
const appDefinition = {
|
|
1335
|
-
vpc: {
|
|
1336
|
-
enable: true,
|
|
1337
|
-
management: 'discover',
|
|
1338
|
-
natGateway: {
|
|
1339
|
-
management: 'createAndManage'
|
|
1340
|
-
}
|
|
1341
|
-
},
|
|
1342
|
-
integrations: []
|
|
1343
|
-
};
|
|
1344
|
-
|
|
1345
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1346
|
-
|
|
1347
|
-
expect(result.resources.Resources.FriggLambdaRouteTable).toBeDefined();
|
|
1348
|
-
expect(result.resources.Resources.FriggNATRoute).toBeDefined();
|
|
1349
|
-
});
|
|
1350
|
-
|
|
1351
|
-
it('should mark managed NAT Gateway resources for retention', async () => {
|
|
1352
|
-
const appDefinition = {
|
|
1353
|
-
vpc: {
|
|
1354
|
-
enable: true,
|
|
1355
|
-
management: 'discover',
|
|
1356
|
-
natGateway: { management: 'createAndManage' },
|
|
1357
|
-
selfHeal: true,
|
|
1358
|
-
},
|
|
1359
|
-
integrations: [],
|
|
1360
|
-
};
|
|
1361
|
-
|
|
1362
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1363
|
-
|
|
1364
|
-
expect(result.resources.Resources.FriggNATGateway).toBeDefined();
|
|
1365
|
-
expect(result.resources.Resources.FriggNATGateway.DeletionPolicy).toBe('Retain');
|
|
1366
|
-
expect(result.resources.Resources.FriggNATGateway.UpdateReplacePolicy).toBe('Retain');
|
|
1367
|
-
});
|
|
1368
|
-
|
|
1369
|
-
it('should handle NAT Gateway with discover mode', async () => {
|
|
1370
|
-
// Mock discovery to return existing NAT Gateway
|
|
1371
|
-
const { AWSDiscovery } = require('./aws-discovery');
|
|
1372
|
-
const mockDiscoverResources = jest.fn().mockResolvedValue({
|
|
1373
|
-
defaultVpcId: 'vpc-123456',
|
|
1374
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1375
|
-
privateSubnetId1: 'subnet-123456',
|
|
1376
|
-
privateSubnetId2: 'subnet-789012',
|
|
1377
|
-
publicSubnetId: 'subnet-public',
|
|
1378
|
-
defaultRouteTableId: 'rtb-123456',
|
|
1379
|
-
defaultKmsKeyId: null,
|
|
1380
|
-
existingNatGatewayId: 'nat-existing123'
|
|
1381
|
-
});
|
|
1382
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
1383
|
-
discoverResources: mockDiscoverResources
|
|
1384
|
-
}));
|
|
1385
|
-
|
|
1386
|
-
const appDefinition = {
|
|
1387
|
-
vpc: {
|
|
1388
|
-
enable: true,
|
|
1389
|
-
management: 'discover',
|
|
1390
|
-
natGateway: {
|
|
1391
|
-
management: 'discover'
|
|
1392
|
-
}
|
|
1393
|
-
},
|
|
1394
|
-
integrations: []
|
|
1395
|
-
};
|
|
1396
|
-
|
|
1397
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1398
|
-
|
|
1399
|
-
expect(result.resources.Resources.FriggNATRoute).toBeDefined();
|
|
1400
|
-
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId).toBe('nat-existing123');
|
|
1401
|
-
});
|
|
1402
|
-
|
|
1403
|
-
it('should handle NAT Gateway with useExisting mode and provided ID', async () => {
|
|
1404
|
-
const appDefinition = {
|
|
1405
|
-
vpc: {
|
|
1406
|
-
enable: true,
|
|
1407
|
-
management: 'discover',
|
|
1408
|
-
natGateway: {
|
|
1409
|
-
management: 'useExisting',
|
|
1410
|
-
id: 'nat-custom456'
|
|
1411
|
-
}
|
|
1412
|
-
},
|
|
1413
|
-
integrations: []
|
|
1414
|
-
};
|
|
1415
|
-
|
|
1416
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1417
|
-
|
|
1418
|
-
expect(result.resources.Resources.FriggNATRoute).toBeDefined();
|
|
1419
|
-
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId).toBe('nat-custom456');
|
|
1420
|
-
});
|
|
1421
|
-
|
|
1422
|
-
it('should throw error when NAT Gateway not found in discover mode', async () => {
|
|
1423
|
-
// Mock discovery to return no NAT Gateway
|
|
1424
|
-
const { AWSDiscovery } = require('./aws-discovery');
|
|
1425
|
-
const mockDiscoverResources = jest.fn().mockResolvedValue({
|
|
1426
|
-
defaultVpcId: 'vpc-123456',
|
|
1427
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1428
|
-
privateSubnetId1: 'subnet-123456',
|
|
1429
|
-
privateSubnetId2: 'subnet-789012',
|
|
1430
|
-
publicSubnetId: 'subnet-public',
|
|
1431
|
-
defaultRouteTableId: 'rtb-123456',
|
|
1432
|
-
defaultKmsKeyId: null,
|
|
1433
|
-
existingNatGatewayId: null // No NAT Gateway
|
|
1434
|
-
});
|
|
1435
|
-
AWSDiscovery.mockImplementation(() => ({
|
|
1436
|
-
discoverResources: mockDiscoverResources
|
|
1437
|
-
}));
|
|
1438
|
-
|
|
1439
|
-
const appDefinition = {
|
|
1440
|
-
vpc: {
|
|
1441
|
-
enable: true,
|
|
1442
|
-
management: 'discover',
|
|
1443
|
-
natGateway: {
|
|
1444
|
-
management: 'discover'
|
|
1445
|
-
}
|
|
1446
|
-
},
|
|
1447
|
-
integrations: []
|
|
1448
|
-
};
|
|
1449
|
-
|
|
1450
|
-
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
1451
|
-
'No existing NAT Gateway found in discovery mode'
|
|
1452
|
-
);
|
|
1453
|
-
});
|
|
1454
|
-
|
|
1455
1146
|
it('should enable self-healing when selfHeal is true', async () => {
|
|
1456
1147
|
const appDefinition = {
|
|
1457
1148
|
vpc: {
|
|
@@ -1489,15 +1180,13 @@ describe('composeServerlessDefinition', () => {
|
|
|
1489
1180
|
|
|
1490
1181
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1491
1182
|
|
|
1492
|
-
// VPC
|
|
1183
|
+
// VPC (plus the interface endpoints KMS needs from private subnets)
|
|
1493
1184
|
expect(result.provider.vpc).toBeDefined();
|
|
1494
|
-
|
|
1495
|
-
expect(result.resources.Resources.
|
|
1185
|
+
expect(result.resources.Resources.FriggS3VPCEndpoint).toBeDefined();
|
|
1186
|
+
expect(result.resources.Resources.FriggKMSVPCEndpoint).toBeDefined();
|
|
1496
1187
|
|
|
1497
1188
|
// KMS
|
|
1498
|
-
expect(result.plugins).toContain('serverless-kms-grants');
|
|
1499
1189
|
expect(result.provider.environment.KMS_KEY_ARN).toBeDefined();
|
|
1500
|
-
expect(result.custom.kmsGrants).toBeDefined();
|
|
1501
1190
|
|
|
1502
1191
|
// SSM (enabled without offload markers -> broad grant only, no offload env vars)
|
|
1503
1192
|
const ssmPermission = result.provider.iamRoleStatements.find(
|
|
@@ -1510,14 +1199,12 @@ describe('composeServerlessDefinition', () => {
|
|
|
1510
1199
|
expect(result.functions.testIntegration).toBeDefined();
|
|
1511
1200
|
expect(result.resources.Resources.TestIntegrationQueue).toBeDefined();
|
|
1512
1201
|
|
|
1513
|
-
//
|
|
1202
|
+
// No feature adds a Serverless plugin of its own
|
|
1514
1203
|
expect(result.plugins).toEqual([
|
|
1515
|
-
'serverless-
|
|
1516
|
-
'serverless-dotenv-plugin',
|
|
1204
|
+
'serverless-esbuild',
|
|
1517
1205
|
'serverless-offline-sqs',
|
|
1518
1206
|
'serverless-offline',
|
|
1519
1207
|
'@friggframework/serverless-plugin',
|
|
1520
|
-
'serverless-kms-grants'
|
|
1521
1208
|
]);
|
|
1522
1209
|
});
|
|
1523
1210
|
|
|
@@ -1538,10 +1225,13 @@ describe('composeServerlessDefinition', () => {
|
|
|
1538
1225
|
|
|
1539
1226
|
// VPC and KMS should be present
|
|
1540
1227
|
expect(result.provider.vpc).toBeDefined();
|
|
1541
|
-
expect(result.
|
|
1228
|
+
expect(result.provider.environment.KMS_KEY_ARN).toBeDefined();
|
|
1542
1229
|
|
|
1543
1230
|
// SSM should not be present
|
|
1544
|
-
|
|
1231
|
+
const ssmPermission = result.provider.iamRoleStatements.find(
|
|
1232
|
+
statement => statement.Action && statement.Action.includes('ssm:GetParameter')
|
|
1233
|
+
);
|
|
1234
|
+
expect(ssmPermission).toBeUndefined();
|
|
1545
1235
|
expect(result.provider.environment.SSM_PARAMETER_PREFIX).toBeUndefined();
|
|
1546
1236
|
});
|
|
1547
1237
|
});
|
|
@@ -1565,10 +1255,14 @@ describe('composeServerlessDefinition', () => {
|
|
|
1565
1255
|
expect(result.functions.user).toBeDefined();
|
|
1566
1256
|
expect(result.functions.health).toBeDefined();
|
|
1567
1257
|
|
|
1568
|
-
// Check default plugins
|
|
1569
|
-
|
|
1570
|
-
expect(result.plugins).
|
|
1571
|
-
|
|
1258
|
+
// Check default plugins (esbuild replaced serverless-jetpack; env
|
|
1259
|
+
// vars come from the app definition, not serverless-dotenv-plugin)
|
|
1260
|
+
expect(result.plugins).toEqual([
|
|
1261
|
+
'serverless-esbuild',
|
|
1262
|
+
'serverless-offline-sqs',
|
|
1263
|
+
'serverless-offline',
|
|
1264
|
+
'@friggframework/serverless-plugin',
|
|
1265
|
+
]);
|
|
1572
1266
|
});
|
|
1573
1267
|
|
|
1574
1268
|
it('should always include default IAM permissions', async () => {
|
|
@@ -1598,8 +1292,14 @@ describe('composeServerlessDefinition', () => {
|
|
|
1598
1292
|
|
|
1599
1293
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1600
1294
|
|
|
1601
|
-
expect(result.provider.environment
|
|
1602
|
-
|
|
1295
|
+
expect(result.provider.environment).toEqual(
|
|
1296
|
+
expect.objectContaining({
|
|
1297
|
+
STAGE: '${self:provider.stage}',
|
|
1298
|
+
FRIGG_STAGE: '${self:provider.stage}',
|
|
1299
|
+
FRIGG_STACK: '${self:service}',
|
|
1300
|
+
FRIGG_REGION: '${self:provider.region}',
|
|
1301
|
+
})
|
|
1302
|
+
);
|
|
1603
1303
|
});
|
|
1604
1304
|
});
|
|
1605
1305
|
|
|
@@ -1656,306 +1356,77 @@ describe('composeServerlessDefinition', () => {
|
|
|
1656
1356
|
});
|
|
1657
1357
|
});
|
|
1658
1358
|
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
const mockInstance = new mockDiscovery();
|
|
1664
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1665
|
-
defaultVpcId: 'vpc-123456',
|
|
1666
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1667
|
-
privateSubnetId1: 'subnet-private1',
|
|
1668
|
-
privateSubnetId2: 'subnet-private2',
|
|
1669
|
-
publicSubnetId: 'subnet-public',
|
|
1670
|
-
existingNatGatewayId: 'nat-in-private',
|
|
1671
|
-
natGatewayInPrivateSubnet: true, // CRITICAL: NAT is in WRONG subnet
|
|
1672
|
-
existingElasticIpAllocationId: 'eipalloc-123'
|
|
1673
|
-
});
|
|
1674
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1675
|
-
|
|
1676
|
-
const appDefinition = {
|
|
1677
|
-
vpc: {
|
|
1678
|
-
enable: true,
|
|
1679
|
-
natGateway: { management: 'createAndManage' },
|
|
1680
|
-
selfHeal: false
|
|
1681
|
-
},
|
|
1682
|
-
integrations: []
|
|
1683
|
-
};
|
|
1684
|
-
|
|
1685
|
-
// Should throw error because NAT is in private subnet
|
|
1686
|
-
await expect(composeServerlessDefinition(appDefinition))
|
|
1687
|
-
.rejects
|
|
1688
|
-
.toThrow('CRITICAL: NAT Gateway is in PRIVATE subnet');
|
|
1689
|
-
});
|
|
1690
|
-
|
|
1691
|
-
it('should create NEW NAT in PUBLIC subnet when existing NAT is in private subnet with selfHeal', async () => {
|
|
1692
|
-
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
1693
|
-
const mockInstance = new mockDiscovery();
|
|
1694
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1695
|
-
defaultVpcId: 'vpc-123456',
|
|
1696
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1697
|
-
privateSubnetId1: 'subnet-private1',
|
|
1698
|
-
privateSubnetId2: 'subnet-private2',
|
|
1699
|
-
publicSubnetId: 'subnet-public',
|
|
1700
|
-
existingNatGatewayId: 'nat-in-private',
|
|
1701
|
-
natGatewayInPrivateSubnet: true, // NAT is in WRONG subnet
|
|
1702
|
-
existingElasticIpAllocationId: 'eipalloc-123'
|
|
1703
|
-
});
|
|
1704
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1705
|
-
|
|
1706
|
-
const appDefinition = {
|
|
1707
|
-
vpc: {
|
|
1708
|
-
enable: true,
|
|
1709
|
-
natGateway: { management: 'createAndManage' },
|
|
1710
|
-
selfHeal: true
|
|
1711
|
-
},
|
|
1712
|
-
integrations: []
|
|
1713
|
-
};
|
|
1714
|
-
|
|
1715
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1716
|
-
|
|
1717
|
-
// MUST create new NAT Gateway (not reuse the one in private subnet)
|
|
1718
|
-
expect(result.resources.Resources.FriggNATGateway).toBeDefined();
|
|
1719
|
-
|
|
1720
|
-
// MUST be placed in PUBLIC subnet
|
|
1721
|
-
const natSubnet = result.resources.Resources.FriggNATGateway.Properties.SubnetId;
|
|
1722
|
-
expect(natSubnet).toEqual('subnet-public');
|
|
1723
|
-
|
|
1724
|
-
// MUST create new EIP (cannot reuse the one associated with wrong NAT)
|
|
1725
|
-
expect(result.resources.Resources.FriggNATGatewayEIP).toBeDefined();
|
|
1726
|
-
});
|
|
1727
|
-
|
|
1728
|
-
it('should create public subnet for NAT when none exists', async () => {
|
|
1729
|
-
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
1730
|
-
const mockInstance = new mockDiscovery();
|
|
1731
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1732
|
-
defaultVpcId: 'vpc-123456',
|
|
1733
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1734
|
-
privateSubnetId1: 'subnet-private1',
|
|
1735
|
-
privateSubnetId2: 'subnet-private2',
|
|
1736
|
-
publicSubnetId: null, // NO PUBLIC SUBNET EXISTS
|
|
1737
|
-
existingNatGatewayId: null
|
|
1738
|
-
});
|
|
1739
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1740
|
-
|
|
1741
|
-
const appDefinition = {
|
|
1742
|
-
vpc: {
|
|
1743
|
-
enable: true,
|
|
1744
|
-
natGateway: { management: 'createAndManage' },
|
|
1745
|
-
selfHeal: true
|
|
1746
|
-
},
|
|
1747
|
-
integrations: []
|
|
1748
|
-
};
|
|
1749
|
-
|
|
1750
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1751
|
-
|
|
1752
|
-
// MUST create public subnet
|
|
1753
|
-
expect(result.resources.Resources.FriggPublicSubnet).toBeDefined();
|
|
1754
|
-
expect(result.resources.Resources.FriggPublicSubnet.Properties.MapPublicIpOnLaunch).toBe(true);
|
|
1755
|
-
|
|
1756
|
-
// NAT Gateway MUST be in the newly created public subnet
|
|
1757
|
-
expect(result.resources.Resources.FriggNATGateway.Properties.SubnetId)
|
|
1758
|
-
.toEqual({ Ref: 'FriggPublicSubnet' });
|
|
1759
|
-
});
|
|
1760
|
-
|
|
1761
|
-
it('should reuse CORRECTLY placed NAT Gateway in public subnet', async () => {
|
|
1762
|
-
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
1763
|
-
const mockInstance = new mockDiscovery();
|
|
1764
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1765
|
-
defaultVpcId: 'vpc-123456',
|
|
1766
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1767
|
-
privateSubnetId1: 'subnet-private1',
|
|
1768
|
-
privateSubnetId2: 'subnet-private2',
|
|
1769
|
-
publicSubnetId: 'subnet-public',
|
|
1770
|
-
existingNatGatewayId: 'nat-good',
|
|
1771
|
-
natGatewayInPrivateSubnet: false, // NAT is CORRECTLY in public subnet
|
|
1772
|
-
existingElasticIpAllocationId: 'eipalloc-123'
|
|
1773
|
-
});
|
|
1774
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1775
|
-
|
|
1776
|
-
const appDefinition = {
|
|
1777
|
-
vpc: {
|
|
1778
|
-
enable: true,
|
|
1779
|
-
natGateway: { management: 'createAndManage' },
|
|
1780
|
-
selfHeal: true
|
|
1781
|
-
},
|
|
1782
|
-
integrations: []
|
|
1783
|
-
};
|
|
1784
|
-
|
|
1785
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1786
|
-
|
|
1787
|
-
// Should NOT create new NAT Gateway (reuse the good one)
|
|
1788
|
-
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
1789
|
-
expect(result.resources.Resources.FriggNATGatewayEIP).toBeUndefined();
|
|
1790
|
-
|
|
1791
|
-
// Should use existing NAT in routes
|
|
1792
|
-
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId)
|
|
1793
|
-
.toEqual('nat-good');
|
|
1794
|
-
});
|
|
1795
|
-
|
|
1796
|
-
it('should fix route table associations to prevent NAT misconfiguration', async () => {
|
|
1797
|
-
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
1798
|
-
const mockInstance = new mockDiscovery();
|
|
1799
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1800
|
-
defaultVpcId: 'vpc-123456',
|
|
1801
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1802
|
-
privateSubnetId1: 'subnet-private1',
|
|
1803
|
-
privateSubnetId2: 'subnet-private2',
|
|
1804
|
-
publicSubnetId: 'subnet-public',
|
|
1805
|
-
existingNatGatewayId: 'nat-good',
|
|
1806
|
-
natGatewayInPrivateSubnet: false
|
|
1807
|
-
});
|
|
1808
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1809
|
-
|
|
1810
|
-
const appDefinition = {
|
|
1811
|
-
vpc: {
|
|
1812
|
-
enable: true,
|
|
1813
|
-
natGateway: { management: 'discover' },
|
|
1814
|
-
selfHeal: true
|
|
1815
|
-
},
|
|
1816
|
-
integrations: []
|
|
1817
|
-
};
|
|
1818
|
-
|
|
1819
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1820
|
-
|
|
1821
|
-
// Should create route table to fix associations
|
|
1822
|
-
expect(result.resources.Resources.FriggLambdaRouteTable).toBeDefined();
|
|
1823
|
-
|
|
1824
|
-
// Should create NAT route pointing to good NAT
|
|
1825
|
-
expect(result.resources.Resources.FriggNATRoute).toBeDefined();
|
|
1826
|
-
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId)
|
|
1827
|
-
.toEqual('nat-good');
|
|
1828
|
-
|
|
1829
|
-
// Should associate private subnets with correct route table
|
|
1830
|
-
expect(result.resources.Resources.FriggSubnet1RouteAssociation).toBeDefined();
|
|
1831
|
-
expect(result.resources.Resources.FriggSubnet2RouteAssociation).toBeDefined();
|
|
1832
|
-
});
|
|
1833
|
-
|
|
1834
|
-
it('should handle EIP already associated error by reusing existing NAT', async () => {
|
|
1835
|
-
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
1836
|
-
const mockInstance = new mockDiscovery();
|
|
1837
|
-
mockInstance.discoverResources = jest.fn().mockResolvedValue({
|
|
1838
|
-
defaultVpcId: 'vpc-123456',
|
|
1839
|
-
defaultSecurityGroupId: 'sg-123456',
|
|
1840
|
-
privateSubnetId1: 'subnet-private1',
|
|
1841
|
-
privateSubnetId2: 'subnet-private2',
|
|
1842
|
-
publicSubnetId: 'subnet-public',
|
|
1843
|
-
existingNatGatewayId: 'nat-existing',
|
|
1844
|
-
natGatewayInPrivateSubnet: false, // NAT is correctly placed
|
|
1845
|
-
existingElasticIpAllocationId: 'eipalloc-inuse',
|
|
1846
|
-
elasticIpAlreadyAssociated: true // EIP is already in use
|
|
1847
|
-
});
|
|
1848
|
-
mockDiscovery.mockImplementation(() => mockInstance);
|
|
1849
|
-
|
|
1850
|
-
const appDefinition = {
|
|
1851
|
-
vpc: {
|
|
1852
|
-
enable: true,
|
|
1853
|
-
natGateway: { management: 'createAndManage' },
|
|
1854
|
-
selfHeal: true
|
|
1855
|
-
},
|
|
1856
|
-
integrations: []
|
|
1857
|
-
};
|
|
1858
|
-
|
|
1859
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1860
|
-
|
|
1861
|
-
// Should NOT create new NAT or EIP (reuse existing)
|
|
1862
|
-
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
1863
|
-
expect(result.resources.Resources.FriggNATGatewayEIP).toBeUndefined();
|
|
1864
|
-
|
|
1865
|
-
// Should use existing NAT
|
|
1866
|
-
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId)
|
|
1867
|
-
.toEqual('nat-existing');
|
|
1868
|
-
});
|
|
1869
|
-
});
|
|
1359
|
+
// 'CRITICAL: NAT Gateway MUST be in PUBLIC Subnet' scenarios moved with the
|
|
1360
|
+
// NAT logic into VpcBuilder: see domains/networking/vpc-builder.test.js
|
|
1361
|
+
// ('NAT Gateway handling': 'should create new NAT when existing is in
|
|
1362
|
+
// private subnet', 'should reuse existing elastic IP allocation', ...).
|
|
1870
1363
|
|
|
1364
|
+
// The single manually-invoked dbMigrate function was replaced by an async
|
|
1365
|
+
// migration pipeline (3bae110e): dbMigrationRouter (HTTP) queues jobs on
|
|
1366
|
+
// DbMigrationQueue for dbMigrationWorker. Details: migration-builder.test.js.
|
|
1871
1367
|
describe('Database Migration Lambda', () => {
|
|
1872
|
-
it('should include
|
|
1873
|
-
const
|
|
1874
|
-
name: 'test-app',
|
|
1875
|
-
integrations: []
|
|
1876
|
-
};
|
|
1877
|
-
|
|
1878
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1368
|
+
it('should include the migration router and worker in all deployments', async () => {
|
|
1369
|
+
const result = await composeServerlessDefinition({ integrations: [] });
|
|
1879
1370
|
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1371
|
+
expect(result.functions.dbMigrationRouter.handler).toBe(
|
|
1372
|
+
'node_modules/@friggframework/core/handlers/routers/db-migration.handler'
|
|
1373
|
+
);
|
|
1374
|
+
expect(result.functions.dbMigrationWorker.handler).toBe(
|
|
1883
1375
|
'node_modules/@friggframework/core/handlers/workers/db-migration.handler'
|
|
1884
1376
|
);
|
|
1377
|
+
expect(result.resources.Resources.DbMigrationQueue).toBeDefined();
|
|
1378
|
+
expect(result.resources.Resources.FriggMigrationStatusBucket).toBeDefined();
|
|
1885
1379
|
});
|
|
1886
1380
|
|
|
1887
|
-
it('should
|
|
1888
|
-
const
|
|
1889
|
-
|
|
1890
|
-
};
|
|
1891
|
-
|
|
1892
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1893
|
-
|
|
1894
|
-
const dbMigrate = result.functions.dbMigrate;
|
|
1895
|
-
|
|
1896
|
-
// Check timeout (5 minutes for long migrations)
|
|
1897
|
-
expect(dbMigrate.timeout).toBe(300);
|
|
1898
|
-
|
|
1899
|
-
// Check memory allocation (extra for Prisma CLI)
|
|
1900
|
-
expect(dbMigrate.memorySize).toBe(512);
|
|
1381
|
+
it('should run migrations one at a time from the queue', async () => {
|
|
1382
|
+
const result = await composeServerlessDefinition({ integrations: [] });
|
|
1383
|
+
const worker = result.functions.dbMigrationWorker;
|
|
1901
1384
|
|
|
1902
|
-
|
|
1903
|
-
expect(
|
|
1904
|
-
expect(
|
|
1385
|
+
expect(worker.reservedConcurrency).toBe(1);
|
|
1386
|
+
expect(worker.timeout).toBe(900);
|
|
1387
|
+
expect(worker.events).toEqual([
|
|
1388
|
+
{
|
|
1389
|
+
sqs: {
|
|
1390
|
+
arn: { 'Fn::GetAtt': ['DbMigrationQueue', 'Arn'] },
|
|
1391
|
+
batchSize: 1,
|
|
1392
|
+
},
|
|
1393
|
+
},
|
|
1394
|
+
]);
|
|
1905
1395
|
});
|
|
1906
1396
|
|
|
1907
|
-
it('should
|
|
1908
|
-
const
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1913
|
-
|
|
1914
|
-
const dbMigrate = result.functions.dbMigrate;
|
|
1397
|
+
it('should expose the router under /admin/db-migrate', async () => {
|
|
1398
|
+
const result = await composeServerlessDefinition({ integrations: [] });
|
|
1399
|
+
const paths = result.functions.dbMigrationRouter.events.map(
|
|
1400
|
+
(e) => `${e.httpApi.method} ${e.httpApi.path}`
|
|
1401
|
+
);
|
|
1915
1402
|
|
|
1916
|
-
|
|
1917
|
-
|
|
1403
|
+
expect(paths).toEqual(
|
|
1404
|
+
expect.arrayContaining([
|
|
1405
|
+
'POST /admin/db-migrate',
|
|
1406
|
+
'GET /admin/db-migrate/status',
|
|
1407
|
+
])
|
|
1408
|
+
);
|
|
1918
1409
|
});
|
|
1919
1410
|
|
|
1920
|
-
it('should include
|
|
1921
|
-
const
|
|
1922
|
-
vpc: {
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
},
|
|
1926
|
-
integrations: []
|
|
1927
|
-
};
|
|
1928
|
-
|
|
1929
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1930
|
-
|
|
1931
|
-
// dbMigrate should exist with VPC configuration
|
|
1932
|
-
expect(result.functions.dbMigrate).toBeDefined();
|
|
1411
|
+
it('should include the migration functions with VPC enabled', async () => {
|
|
1412
|
+
const result = await composeServerlessDefinition({
|
|
1413
|
+
vpc: { enable: true },
|
|
1414
|
+
integrations: [],
|
|
1415
|
+
});
|
|
1933
1416
|
|
|
1934
|
-
|
|
1417
|
+
expect(result.functions.dbMigrationWorker).toBeDefined();
|
|
1418
|
+
expect(result.functions.dbMigrationRouter).toBeDefined();
|
|
1935
1419
|
expect(result.provider.vpc).toBeDefined();
|
|
1936
1420
|
});
|
|
1937
1421
|
|
|
1938
|
-
it('should
|
|
1939
|
-
const
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
},
|
|
1944
|
-
database: {
|
|
1945
|
-
postgres: {
|
|
1946
|
-
enable: true,
|
|
1947
|
-
management: 'discover'
|
|
1948
|
-
}
|
|
1949
|
-
},
|
|
1950
|
-
integrations: []
|
|
1951
|
-
};
|
|
1952
|
-
|
|
1953
|
-
const result = await composeServerlessDefinition(appDefinition);
|
|
1422
|
+
it('should omit the migration pipeline when postgres is explicitly disabled', async () => {
|
|
1423
|
+
const result = await composeServerlessDefinition({
|
|
1424
|
+
database: { postgres: { enable: false }, mongoDB: { enable: true } },
|
|
1425
|
+
integrations: [],
|
|
1426
|
+
});
|
|
1954
1427
|
|
|
1955
|
-
|
|
1956
|
-
expect(result.functions.
|
|
1957
|
-
expect(result.provider.environment.DB_TYPE).toBe('postgresql');
|
|
1958
|
-
expect(result.provider.environment.DATABASE_URL).toBeDefined();
|
|
1428
|
+
expect(result.functions.dbMigrationWorker).toBeUndefined();
|
|
1429
|
+
expect(result.functions.dbMigrationRouter).toBeUndefined();
|
|
1959
1430
|
});
|
|
1960
1431
|
});
|
|
1961
1432
|
|
|
@@ -1984,7 +1455,10 @@ describe('composeServerlessDefinition', () => {
|
|
|
1984
1455
|
integrations: [invalidIntegration]
|
|
1985
1456
|
};
|
|
1986
1457
|
|
|
1987
|
-
await
|
|
1458
|
+
await expectValidationError(
|
|
1459
|
+
appDefinition,
|
|
1460
|
+
'Integration at index 0 is missing Definition or name'
|
|
1461
|
+
);
|
|
1988
1462
|
});
|
|
1989
1463
|
|
|
1990
1464
|
it('should handle integration with missing name', async () => {
|
|
@@ -1995,9 +1469,80 @@ describe('composeServerlessDefinition', () => {
|
|
|
1995
1469
|
integrations: [invalidIntegration]
|
|
1996
1470
|
};
|
|
1997
1471
|
|
|
1998
|
-
await
|
|
1472
|
+
await expectValidationError(
|
|
1473
|
+
appDefinition,
|
|
1474
|
+
'Integration at index 0 is missing Definition or name'
|
|
1475
|
+
);
|
|
1476
|
+
});
|
|
1477
|
+
});
|
|
1478
|
+
});
|
|
1479
|
+
|
|
1480
|
+
describe('Lambda architecture (architecture)', () => {
|
|
1481
|
+
const appDefinition = (architecture) => ({
|
|
1482
|
+
name: 'arch-app',
|
|
1483
|
+
integrations: [{ Definition: { name: 'crm' } }],
|
|
1484
|
+
...(architecture && { architecture }),
|
|
1485
|
+
});
|
|
1486
|
+
|
|
1487
|
+
beforeEach(() => {
|
|
1488
|
+
process.argv = ['node', 'test'];
|
|
1489
|
+
gatherDiscoveredResources.mockResolvedValue(createDiscoveryResponse());
|
|
1490
|
+
});
|
|
1491
|
+
|
|
1492
|
+
it('an explicit x86_64 only adds provider.architecture and the layer compatibleArchitectures to the unset template', async () => {
|
|
1493
|
+
const unset = await composeServerlessDefinition(appDefinition());
|
|
1494
|
+
const explicit = await composeServerlessDefinition(appDefinition('x86_64'));
|
|
1495
|
+
|
|
1496
|
+
expect(explicit.provider.architecture).toBe('x86_64');
|
|
1497
|
+
expect(explicit.layers.prisma.compatibleArchitectures).toEqual(['x86_64']);
|
|
1498
|
+
|
|
1499
|
+
delete explicit.provider.architecture;
|
|
1500
|
+
delete explicit.layers.prisma.compatibleArchitectures;
|
|
1501
|
+
expect(explicit).toEqual(unset);
|
|
1502
|
+
});
|
|
1503
|
+
|
|
1504
|
+
it('fetches the migration worker engines for the deployed architecture before packaging', async () => {
|
|
1505
|
+
const { ensureMigrationEngines } = require('./domains/database/migration-engines');
|
|
1506
|
+
|
|
1507
|
+
await composeServerlessDefinition(appDefinition('arm64'));
|
|
1508
|
+
|
|
1509
|
+
expect(ensureMigrationEngines).toHaveBeenCalledWith({
|
|
1510
|
+
prismaBinaryTarget: 'linux-arm64-openssl-3.0.x',
|
|
1999
1511
|
});
|
|
2000
1512
|
});
|
|
1513
|
+
|
|
1514
|
+
it('fetches no migration engines when there is no migration worker (PostgreSQL disabled)', async () => {
|
|
1515
|
+
const { ensureMigrationEngines } = require('./domains/database/migration-engines');
|
|
1516
|
+
ensureMigrationEngines.mockClear();
|
|
1517
|
+
|
|
1518
|
+
await composeServerlessDefinition({
|
|
1519
|
+
...appDefinition('arm64'),
|
|
1520
|
+
database: { postgres: { enable: false }, mongoDB: { enable: true } },
|
|
1521
|
+
});
|
|
1522
|
+
|
|
1523
|
+
expect(ensureMigrationEngines).not.toHaveBeenCalled();
|
|
1524
|
+
});
|
|
1525
|
+
|
|
1526
|
+
it('builds the Prisma layer for the engine of the deployed architecture', async () => {
|
|
1527
|
+
const { ensurePrismaLayerExists } = require('./domains/shared/utilities/prisma-layer-manager');
|
|
1528
|
+
|
|
1529
|
+
await composeServerlessDefinition(appDefinition('arm64'));
|
|
1530
|
+
|
|
1531
|
+
expect(ensurePrismaLayerExists).toHaveBeenCalledWith({
|
|
1532
|
+
databaseConfig: {},
|
|
1533
|
+
prismaBinaryTarget: 'linux-arm64-openssl-3.0.x',
|
|
1534
|
+
});
|
|
1535
|
+
});
|
|
1536
|
+
|
|
1537
|
+
it('arm64 sets provider.architecture, which osls applies to every function, and the layer compatibleArchitectures', async () => {
|
|
1538
|
+
const result = await composeServerlessDefinition(appDefinition('arm64'));
|
|
1539
|
+
|
|
1540
|
+
expect(result.provider.architecture).toBe('arm64');
|
|
1541
|
+
expect(result.layers.prisma.compatibleArchitectures).toEqual(['arm64']);
|
|
1542
|
+
for (const fn of Object.values(result.functions)) {
|
|
1543
|
+
expect(fn.architecture).toBeUndefined();
|
|
1544
|
+
}
|
|
1545
|
+
});
|
|
2001
1546
|
});
|
|
2002
1547
|
|
|
2003
1548
|
describe('applySsmPreloadNodeOptions', () => {
|