@friggframework/devtools 2.0.0--canary.731.ffe61d3.0 → 2.0.0--canary.658.130b4ec.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 +31 -115
- package/frigg-cli/__tests__/unit/commands/build.test.js +0 -3
- package/frigg-cli/__tests__/unit/commands/deploy.test.js +24 -33
- package/frigg-cli/__tests__/unit/commands/doctor.test.js +6 -10
- package/frigg-cli/__tests__/unit/commands/generate-iam.test.js +0 -18
- package/frigg-cli/__tests__/unit/commands/install.test.js +341 -601
- package/frigg-cli/__tests__/unit/commands/ui.test.js +30 -31
- package/frigg-cli/__tests__/unit/dependencies.test.js +2 -6
- package/frigg-cli/build-command/index.js +0 -6
- package/frigg-cli/deploy-command/index.js +0 -6
- package/frigg-cli/generate-command/index.js +0 -1
- package/frigg-cli/generate-iam-command.js +0 -1
- package/frigg-cli/index.js +5 -22
- package/frigg-cli/index.test.js +158 -0
- package/frigg-cli/init-command/backend-first-handler.js +551 -395
- package/frigg-cli/init-command/index.js +35 -82
- package/frigg-cli/install-command/commit-changes.js +8 -40
- package/frigg-cli/install-command/environment-variables.js +0 -15
- package/frigg-cli/install-command/index.js +21 -96
- package/frigg-cli/install-command/install-package.js +4 -77
- package/frigg-cli/install-command/integration-file.js +11 -20
- package/frigg-cli/install-command/template.js +75 -102
- package/frigg-cli/install-command/validate-package.js +4 -36
- package/frigg-cli/package.json +1 -1
- package/frigg-cli/start-command/index.js +0 -4
- package/frigg-cli/start-command/start-command.test.js +0 -3
- package/frigg-cli/test/init-command.test.js +2 -9
- package/infrastructure/README.md +5 -32
- package/infrastructure/__tests__/postgres-config.test.js +836 -245
- package/infrastructure/__tests__/scoped-environment.test.js +0 -3
- package/infrastructure/__tests__/ssm-preload-node-options.test.js +0 -3
- package/infrastructure/__tests__/template-generation.test.js +8 -39
- package/infrastructure/create-frigg-infrastructure.js +1 -4
- package/infrastructure/docs/POSTGRES-CONFIGURATION.md +98 -80
- package/infrastructure/domains/admin-scripts/admin-script-builder.js +2 -8
- package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +6 -52
- package/infrastructure/domains/database/aurora-builder.js +2 -13
- package/infrastructure/domains/database/aurora-builder.test.js +0 -22
- package/infrastructure/domains/database/migration-builder.js +2 -21
- package/infrastructure/domains/database/migration-builder.test.js +6 -61
- 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/integration/integration-builder.js +0 -2
- package/infrastructure/domains/integration/integration-builder.test.js +6 -22
- 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/environment-builder.js +5 -31
- package/infrastructure/domains/shared/environment-builder.test.js +7 -93
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +3 -39
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +5 -85
- package/infrastructure/domains/shared/utilities/nested-node-modules.js +5 -6
- package/infrastructure/domains/shared/utilities/nested-node-modules.test.js +26 -20
- package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +12 -92
- package/infrastructure/domains/shared/utilities/prisma-layer-manager.test.js +6 -158
- package/infrastructure/infrastructure-composer.js +1 -12
- package/infrastructure/infrastructure-composer.test.js +794 -339
- package/infrastructure/integration.test.js +381 -0
- package/infrastructure/scripts/build-prisma-layer.js +50 -136
- package/infrastructure/scripts/build-prisma-layer.test.js +7 -128
- package/infrastructure/scripts/build-time-discovery.test.js +3 -18
- package/package.json +10 -12
- package/frigg-cli/__tests__/unit/commands/init.test.js +0 -688
- package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +0 -92
- package/frigg-cli/init-command/deployment-modes.js +0 -7
- package/frigg-cli/init-command/safe-path.js +0 -58
- package/frigg-cli/init-command/scaffold-dependencies.js +0 -202
- package/frigg-cli/install-command/app-definition.js +0 -174
- package/frigg-cli/install-command/resolve-module.js +0 -69
- package/frigg-cli/templates/backend/README.md +0 -267
- package/frigg-cli/templates/backend/env.example +0 -20
- package/frigg-cli/templates/backend/gitignore +0 -10
- package/frigg-cli/templates/backend/index.js +0 -114
- package/frigg-cli/templates/backend/infrastructure.js +0 -20
- package/frigg-cli/validate-command/__tests__/init-validation.test.js +0 -63
- package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +0 -133
- package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +0 -443
- package/frigg-cli/validate-command/__tests__/validate-command.test.js +0 -262
- package/frigg-cli/validate-command/checks/config.js +0 -504
- package/frigg-cli/validate-command/checks/integrations.js +0 -274
- package/frigg-cli/validate-command/format-report.js +0 -58
- package/frigg-cli/validate-command/index.js +0 -107
- package/frigg-cli/validate-command/load-app-definition.js +0 -105
- package/frigg-cli/validate-command/schema-issues.js +0 -154
- package/frigg-cli/validate-command/validate-app-definition.js +0 -60
- package/infrastructure/__tests__/helpers/bundled-prisma-engine-excludes.js +0 -39
- package/infrastructure/__tests__/management-api-gateway-routes.test.js +0 -284
- package/infrastructure/__tests__/scaffold-default-infrastructure.test.js +0 -151
- package/infrastructure/app-definition-schema-contract.test.js +0 -634
- package/infrastructure/create-frigg-infrastructure.test.js +0 -51
- package/infrastructure/domains/database/migration-engines.js +0 -93
- package/infrastructure/domains/database/migration-engines.test.js +0 -142
- package/infrastructure/domains/shared/lambda-architectures.js +0 -113
- package/infrastructure/domains/shared/lambda-architectures.test.js +0 -164
|
@@ -35,32 +35,8 @@ 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
|
-
|
|
47
38
|
const { gatherDiscoveredResources } = require('./domains/shared/resource-discovery');
|
|
48
39
|
|
|
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
|
-
|
|
64
40
|
describe('composeServerlessDefinition', () => {
|
|
65
41
|
let mockIntegration;
|
|
66
42
|
|
|
@@ -89,11 +65,21 @@ describe('composeServerlessDefinition', () => {
|
|
|
89
65
|
process.argv = ['node', 'test'];
|
|
90
66
|
});
|
|
91
67
|
|
|
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.
|
|
96
68
|
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
|
+
});
|
|
97
83
|
|
|
98
84
|
it('should run AWS discovery when VPC features are enabled', async () => {
|
|
99
85
|
gatherDiscoveredResources.mockClear();
|
|
@@ -108,6 +94,22 @@ describe('composeServerlessDefinition', () => {
|
|
|
108
94
|
expect(gatherDiscoveredResources).toHaveBeenCalledTimes(1);
|
|
109
95
|
});
|
|
110
96
|
|
|
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
|
+
|
|
111
113
|
it('should run AWS discovery when FRIGG_SKIP_AWS_DISCOVERY is not set', async () => {
|
|
112
114
|
gatherDiscoveredResources.mockClear();
|
|
113
115
|
delete process.env.FRIGG_SKIP_AWS_DISCOVERY;
|
|
@@ -418,15 +420,11 @@ describe('composeServerlessDefinition', () => {
|
|
|
418
420
|
});
|
|
419
421
|
|
|
420
422
|
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: '
|
|
427
|
-
vpcId: 'vpc-123456',
|
|
426
|
+
management: 'discover',
|
|
428
427
|
securityGroupIds: ['sg-custom-1', 'sg-custom-2'],
|
|
429
|
-
subnets: { ids: ['subnet-123456', 'subnet-789012'] },
|
|
430
428
|
},
|
|
431
429
|
integrations: [],
|
|
432
430
|
};
|
|
@@ -440,25 +438,27 @@ describe('composeServerlessDefinition', () => {
|
|
|
440
438
|
});
|
|
441
439
|
|
|
442
440
|
it('should throw when discover mode finds no subnets and self-heal is disabled', async () => {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
441
|
+
const discoveryInstance = {
|
|
442
|
+
discoverResources: jest.fn().mockResolvedValue(
|
|
443
|
+
createDiscoveryResponse({
|
|
444
|
+
privateSubnetId1: null,
|
|
445
|
+
privateSubnetId2: null,
|
|
446
|
+
})
|
|
447
|
+
),
|
|
448
|
+
};
|
|
449
|
+
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
449
450
|
|
|
450
451
|
const appDefinition = {
|
|
451
452
|
vpc: {
|
|
452
453
|
enable: true,
|
|
453
454
|
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 and subnets.management is "discover". Either enable vpc.selfHeal, set subnets.management to "create", or provide subnet IDs.'
|
|
462
462
|
);
|
|
463
463
|
});
|
|
464
464
|
|
|
@@ -492,11 +492,6 @@ 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
|
-
|
|
500
495
|
const appDefinition = {
|
|
501
496
|
vpc: {
|
|
502
497
|
enable: true,
|
|
@@ -515,7 +510,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
515
510
|
expect(result.resources.Resources.FriggLambdaRouteTable).toBeDefined();
|
|
516
511
|
|
|
517
512
|
// Verify subnet associations
|
|
518
|
-
expect(result.resources.Resources.
|
|
513
|
+
expect(result.resources.Resources.FriggPublicSubnetRouteTableAssociation).toBeDefined();
|
|
519
514
|
expect(result.resources.Resources.FriggPrivateSubnet1RouteTableAssociation).toBeDefined();
|
|
520
515
|
expect(result.resources.Resources.FriggPrivateSubnet2RouteTableAssociation).toBeDefined();
|
|
521
516
|
});
|
|
@@ -529,9 +524,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
529
524
|
integrations: []
|
|
530
525
|
};
|
|
531
526
|
|
|
532
|
-
await
|
|
533
|
-
|
|
534
|
-
'vpc.vpcId is required when management="use-existing"'
|
|
527
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
528
|
+
'VPC management is set to "use-existing" but no vpcId was provided'
|
|
535
529
|
);
|
|
536
530
|
});
|
|
537
531
|
|
|
@@ -559,11 +553,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
559
553
|
|
|
560
554
|
const result = await composeServerlessDefinition(appDefinition);
|
|
561
555
|
|
|
562
|
-
|
|
563
|
-
expect(
|
|
564
|
-
expect(
|
|
565
|
-
expect(s3Endpoint.Properties.VpcId).toBe('vpc-123456');
|
|
566
|
-
expect(s3Endpoint.Properties.VpcEndpointType).toBe('Gateway');
|
|
556
|
+
expect(result.resources.Resources.VPCEndpointS3).toBeDefined();
|
|
557
|
+
expect(result.resources.Resources.VPCEndpointS3.Type).toBe('AWS::EC2::VPCEndpoint');
|
|
558
|
+
expect(result.resources.Resources.VPCEndpointS3.Properties.VpcId).toBe('vpc-123456');
|
|
567
559
|
});
|
|
568
560
|
|
|
569
561
|
it('should skip creating VPC endpoints when disabled explicitly', async () => {
|
|
@@ -578,9 +570,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
578
570
|
|
|
579
571
|
const result = await composeServerlessDefinition(appDefinition);
|
|
580
572
|
|
|
581
|
-
expect(result.resources.Resources.
|
|
582
|
-
expect(result.resources.Resources.
|
|
583
|
-
expect(result.resources.Resources.
|
|
573
|
+
expect(result.resources.Resources.VPCEndpointS3).toBeUndefined();
|
|
574
|
+
expect(result.resources.Resources.VPCEndpointKMS).toBeUndefined();
|
|
575
|
+
expect(result.resources.Resources.VPCEndpointSecretsManager).toBeUndefined();
|
|
584
576
|
});
|
|
585
577
|
|
|
586
578
|
it('should add Secrets Manager endpoint only when enabled', async () => {
|
|
@@ -596,8 +588,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
596
588
|
|
|
597
589
|
const result = await composeServerlessDefinition(appDefinition);
|
|
598
590
|
|
|
599
|
-
expect(result.resources.Resources.
|
|
600
|
-
expect(result.resources.Resources.FriggKMSVPCEndpoint).toBeDefined();
|
|
591
|
+
expect(result.resources.Resources.VPCEndpointSecretsManager).toBeDefined();
|
|
601
592
|
});
|
|
602
593
|
|
|
603
594
|
it('should allow Lambda security group access for VPC endpoints when security group is discovered', async () => {
|
|
@@ -611,7 +602,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
611
602
|
};
|
|
612
603
|
|
|
613
604
|
const result = await composeServerlessDefinition(appDefinition);
|
|
614
|
-
const endpointSg = result.resources.Resources.
|
|
605
|
+
const endpointSg = result.resources.Resources.VPCEndpointSecurityGroup;
|
|
615
606
|
|
|
616
607
|
expect(endpointSg).toBeDefined();
|
|
617
608
|
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
@@ -620,39 +611,81 @@ describe('composeServerlessDefinition', () => {
|
|
|
620
611
|
FromPort: 443,
|
|
621
612
|
ToPort: 443,
|
|
622
613
|
SourceSecurityGroupId: 'sg-123456',
|
|
623
|
-
Description: 'HTTPS from Lambda'
|
|
614
|
+
Description: 'HTTPS from Lambda security group'
|
|
624
615
|
}
|
|
625
616
|
]);
|
|
626
617
|
});
|
|
627
618
|
|
|
628
|
-
it('should
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
619
|
+
it('should fall back to VPC CIDR when Lambda security group identifier cannot be resolved', async () => {
|
|
620
|
+
AWSDiscovery.mockImplementation(() => ({
|
|
621
|
+
discoverResources: jest
|
|
622
|
+
.fn()
|
|
623
|
+
.mockResolvedValue(createDiscoveryResponse()),
|
|
624
|
+
}));
|
|
625
|
+
|
|
633
626
|
const appDefinition = {
|
|
634
627
|
vpc: {
|
|
635
628
|
enable: true,
|
|
636
|
-
management: '
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
629
|
+
management: 'discover',
|
|
630
|
+
securityGroupIds: [
|
|
631
|
+
{
|
|
632
|
+
'Fn::ImportValue': 'shared-lambda-security-group',
|
|
633
|
+
},
|
|
634
|
+
],
|
|
640
635
|
},
|
|
641
636
|
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
642
637
|
integrations: [],
|
|
643
638
|
};
|
|
644
639
|
|
|
645
640
|
const result = await composeServerlessDefinition(appDefinition);
|
|
646
|
-
const endpointSg = result.resources.Resources.
|
|
641
|
+
const endpointSg = result.resources.Resources.VPCEndpointSecurityGroup;
|
|
647
642
|
|
|
648
|
-
expect(
|
|
643
|
+
expect(endpointSg).toBeDefined();
|
|
649
644
|
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
650
645
|
{
|
|
651
646
|
IpProtocol: 'tcp',
|
|
652
647
|
FromPort: 443,
|
|
653
648
|
ToPort: 443,
|
|
654
|
-
|
|
655
|
-
Description: 'HTTPS from
|
|
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
|
+
],
|
|
673
|
+
},
|
|
674
|
+
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
675
|
+
integrations: [],
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
679
|
+
const endpointSg = result.resources.Resources.VPCEndpointSecurityGroup;
|
|
680
|
+
|
|
681
|
+
expect(endpointSg).toBeDefined();
|
|
682
|
+
expect(endpointSg.Properties.SecurityGroupIngress).toEqual([
|
|
683
|
+
{
|
|
684
|
+
IpProtocol: 'tcp',
|
|
685
|
+
FromPort: 443,
|
|
686
|
+
ToPort: 443,
|
|
687
|
+
CidrIp: '172.31.0.0/16',
|
|
688
|
+
Description: 'HTTPS from default VPC range',
|
|
656
689
|
},
|
|
657
690
|
]);
|
|
658
691
|
});
|
|
@@ -677,7 +710,14 @@ describe('composeServerlessDefinition', () => {
|
|
|
677
710
|
FromPort: 443,
|
|
678
711
|
ToPort: 443,
|
|
679
712
|
SourceSecurityGroupId: { Ref: 'FriggLambdaSecurityGroup' },
|
|
680
|
-
Description: 'HTTPS from Lambda'
|
|
713
|
+
Description: 'HTTPS from Lambda security group'
|
|
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)'
|
|
681
721
|
}
|
|
682
722
|
]);
|
|
683
723
|
});
|
|
@@ -691,7 +731,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
691
731
|
const result = await composeServerlessDefinition(appDefinition);
|
|
692
732
|
|
|
693
733
|
expect(result.provider.vpc).toBeUndefined();
|
|
694
|
-
expect(result.resources.Resources.
|
|
734
|
+
expect(result.resources.Resources.VPCEndpointS3).toBeUndefined();
|
|
695
735
|
});
|
|
696
736
|
|
|
697
737
|
it('should not add VPC configuration when vpc is not defined', async () => {
|
|
@@ -705,87 +745,116 @@ describe('composeServerlessDefinition', () => {
|
|
|
705
745
|
});
|
|
706
746
|
});
|
|
707
747
|
|
|
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.
|
|
712
748
|
describe('NAT Gateway behaviour', () => {
|
|
713
|
-
it('should
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
749
|
+
it('should reuse discovered NAT gateway in discover mode without creating new resources', async () => {
|
|
750
|
+
const discoveryInstance = {
|
|
751
|
+
discoverResources: jest.fn().mockResolvedValue(
|
|
752
|
+
createDiscoveryResponse({
|
|
753
|
+
existingNatGatewayId: 'nat-existing123',
|
|
754
|
+
natGatewayInPrivateSubnet: false,
|
|
755
|
+
})
|
|
756
|
+
),
|
|
757
|
+
};
|
|
758
|
+
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
720
759
|
|
|
721
|
-
const
|
|
760
|
+
const appDefinition = {
|
|
722
761
|
vpc: {
|
|
723
762
|
enable: true,
|
|
724
763
|
management: 'discover',
|
|
725
764
|
natGateway: { management: 'discover' },
|
|
726
765
|
},
|
|
727
766
|
integrations: [],
|
|
728
|
-
}
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
729
770
|
|
|
730
771
|
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
731
|
-
expect(result.resources.Resources.
|
|
732
|
-
RouteTableId: { Ref: 'FriggLambdaRouteTable' },
|
|
733
|
-
DestinationCidrBlock: '0.0.0.0/0',
|
|
734
|
-
NatGatewayId: 'nat-existing123',
|
|
735
|
-
});
|
|
772
|
+
expect(result.resources.Resources.FriggNATRoute).toBeDefined();
|
|
736
773
|
});
|
|
737
774
|
|
|
738
|
-
it('should
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
775
|
+
it('should reference provided NAT gateway when management set to useExisting', async () => {
|
|
776
|
+
const discoveryInstance = {
|
|
777
|
+
discoverResources: jest.fn().mockResolvedValue(
|
|
778
|
+
createDiscoveryResponse({ existingNatGatewayId: null })
|
|
779
|
+
),
|
|
780
|
+
};
|
|
781
|
+
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
742
782
|
|
|
743
|
-
const
|
|
783
|
+
const appDefinition = {
|
|
744
784
|
vpc: {
|
|
745
785
|
enable: true,
|
|
746
786
|
management: 'discover',
|
|
747
787
|
natGateway: { management: 'useExisting', id: 'nat-custom-001' },
|
|
748
788
|
},
|
|
749
789
|
integrations: [],
|
|
750
|
-
}
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
751
793
|
|
|
752
794
|
expect(result.resources.Resources.FriggNATGateway).toBeUndefined();
|
|
753
|
-
expect(result.resources.Resources.
|
|
754
|
-
'nat-custom-001'
|
|
755
|
-
);
|
|
795
|
+
expect(result.resources.Resources.FriggNATRoute.Properties.NatGatewayId).toBe('nat-custom-001');
|
|
756
796
|
});
|
|
757
797
|
|
|
758
|
-
it('should
|
|
759
|
-
|
|
760
|
-
|
|
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'
|
|
761
824
|
);
|
|
825
|
+
});
|
|
762
826
|
|
|
763
|
-
|
|
827
|
+
it('should create a public subnet when discovery provides none', async () => {
|
|
828
|
+
const discoveryInstance = {
|
|
829
|
+
discoverResources: jest.fn().mockResolvedValue(
|
|
830
|
+
createDiscoveryResponse({
|
|
831
|
+
publicSubnetId: null,
|
|
832
|
+
internetGatewayId: null,
|
|
833
|
+
existingNatGatewayId: null,
|
|
834
|
+
})
|
|
835
|
+
),
|
|
836
|
+
};
|
|
837
|
+
AWSDiscovery.mockImplementation(() => discoveryInstance);
|
|
838
|
+
|
|
839
|
+
const appDefinition = {
|
|
764
840
|
vpc: {
|
|
765
841
|
enable: true,
|
|
766
842
|
management: 'discover',
|
|
767
843
|
natGateway: { management: 'createAndManage' },
|
|
844
|
+
selfHeal: true,
|
|
768
845
|
},
|
|
769
846
|
integrations: [],
|
|
770
|
-
}
|
|
847
|
+
};
|
|
771
848
|
|
|
772
|
-
const
|
|
773
|
-
|
|
774
|
-
expect(resources.
|
|
775
|
-
expect(resources.
|
|
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
|
-
});
|
|
849
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
850
|
+
|
|
851
|
+
expect(result.resources.Resources.FriggPublicSubnet).toBeDefined();
|
|
852
|
+
expect(result.resources.Resources.FriggPublicRouteTable).toBeDefined();
|
|
782
853
|
});
|
|
783
854
|
});
|
|
784
855
|
|
|
785
856
|
describe('KMS Configuration', () => {
|
|
786
857
|
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';
|
|
789
858
|
const appDefinition = {
|
|
790
859
|
encryption: { fieldLevelEncryptionMethod: 'kms' },
|
|
791
860
|
integrations: []
|
|
@@ -793,8 +862,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
793
862
|
|
|
794
863
|
const result = await composeServerlessDefinition(appDefinition);
|
|
795
864
|
|
|
796
|
-
// IAM
|
|
797
|
-
// serverless-kms-grants plugin, which KmsBuilder no longer uses).
|
|
865
|
+
// Check IAM permissions
|
|
798
866
|
const kmsPermission = result.provider.iamRoleStatements.find(
|
|
799
867
|
statement => statement.Action.includes('kms:GenerateDataKey')
|
|
800
868
|
);
|
|
@@ -802,26 +870,49 @@ describe('composeServerlessDefinition', () => {
|
|
|
802
870
|
Effect: 'Allow',
|
|
803
871
|
Action: [
|
|
804
872
|
'kms:GenerateDataKey',
|
|
805
|
-
'kms:Decrypt'
|
|
806
|
-
'kms:Encrypt',
|
|
807
|
-
'kms:DescribeKey'
|
|
873
|
+
'kms:Decrypt'
|
|
808
874
|
],
|
|
809
|
-
Resource:
|
|
875
|
+
Resource: ['arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012']
|
|
810
876
|
});
|
|
811
877
|
|
|
812
|
-
|
|
813
|
-
expect(result.
|
|
814
|
-
|
|
815
|
-
//
|
|
816
|
-
expect(result.
|
|
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'
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
// Check KMS Alias resource is created for discovered key
|
|
890
|
+
expect(result.resources.Resources.FriggKMSKeyAlias).toBeDefined();
|
|
891
|
+
expect(result.resources.Resources.FriggKMSKeyAlias).toEqual({
|
|
892
|
+
Type: 'AWS::KMS::Alias',
|
|
893
|
+
DeletionPolicy: 'Retain',
|
|
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
|
+
});
|
|
817
899
|
});
|
|
818
900
|
|
|
819
901
|
it('should create new KMS key when encryption is enabled, no key found, and createResourceIfNoneFound is true', async () => {
|
|
820
|
-
//
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
902
|
+
// Mock AWS discovery to return no KMS key
|
|
903
|
+
const { AWSDiscovery } = require('./aws-discovery');
|
|
904
|
+
const mockDiscoverResources = jest.fn().mockResolvedValue({
|
|
905
|
+
defaultVpcId: 'vpc-123456',
|
|
906
|
+
defaultSecurityGroupId: 'sg-123456',
|
|
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
|
+
}));
|
|
825
916
|
|
|
826
917
|
const appDefinition = {
|
|
827
918
|
encryption: {
|
|
@@ -833,25 +924,116 @@ describe('composeServerlessDefinition', () => {
|
|
|
833
924
|
|
|
834
925
|
const result = await composeServerlessDefinition(appDefinition);
|
|
835
926
|
|
|
836
|
-
|
|
837
|
-
expect(
|
|
838
|
-
|
|
839
|
-
|
|
927
|
+
// Check that KMS key resource was created with DeletionPolicy
|
|
928
|
+
expect(result.resources.Resources.FriggKMSKey).toEqual({
|
|
929
|
+
Type: 'AWS::KMS::Key',
|
|
930
|
+
DeletionPolicy: 'Retain',
|
|
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
|
+
});
|
|
840
985
|
|
|
841
|
-
|
|
986
|
+
// Check KMS Alias resource is created for the new key
|
|
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
|
|
842
998
|
const kmsPermission = result.provider.iamRoleStatements.find(
|
|
843
999
|
statement => statement.Action.includes('kms:GenerateDataKey')
|
|
844
1000
|
);
|
|
845
|
-
expect(kmsPermission
|
|
846
|
-
|
|
1001
|
+
expect(kmsPermission).toEqual({
|
|
1002
|
+
Effect: 'Allow',
|
|
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
|
+
});
|
|
847
1020
|
});
|
|
848
1021
|
|
|
849
|
-
it('should
|
|
850
|
-
//
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
1022
|
+
it('should throw error when encryption is enabled, no key found, and createResourceIfNoneFound is false', async () => {
|
|
1023
|
+
// Mock AWS discovery to return no KMS key
|
|
1024
|
+
const { AWSDiscovery } = require('./aws-discovery');
|
|
1025
|
+
const mockDiscoverResources = jest.fn().mockResolvedValue({
|
|
1026
|
+
defaultVpcId: 'vpc-123456',
|
|
1027
|
+
defaultSecurityGroupId: 'sg-123456',
|
|
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
|
+
}));
|
|
855
1037
|
|
|
856
1038
|
const appDefinition = {
|
|
857
1039
|
encryption: {
|
|
@@ -861,22 +1043,27 @@ describe('composeServerlessDefinition', () => {
|
|
|
861
1043
|
integrations: []
|
|
862
1044
|
};
|
|
863
1045
|
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
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}'
|
|
1046
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
1047
|
+
'KMS field-level encryption is enabled but no KMS key was found. ' +
|
|
1048
|
+
'Either provide an existing KMS key or set encryption.createResourceIfNoneFound to true to create a new key.'
|
|
871
1049
|
);
|
|
872
1050
|
});
|
|
873
1051
|
|
|
874
|
-
it('should
|
|
875
|
-
//
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
1052
|
+
it('should throw error when encryption is enabled, no key found, and createResourceIfNoneFound is not specified', async () => {
|
|
1053
|
+
// Mock AWS discovery to return no KMS key
|
|
1054
|
+
const { AWSDiscovery } = require('./aws-discovery');
|
|
1055
|
+
const mockDiscoverResources = jest.fn().mockResolvedValue({
|
|
1056
|
+
defaultVpcId: 'vpc-123456',
|
|
1057
|
+
defaultSecurityGroupId: 'sg-123456',
|
|
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
|
+
}));
|
|
880
1067
|
|
|
881
1068
|
const appDefinition = {
|
|
882
1069
|
encryption: {
|
|
@@ -886,12 +1073,10 @@ describe('composeServerlessDefinition', () => {
|
|
|
886
1073
|
integrations: []
|
|
887
1074
|
};
|
|
888
1075
|
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
expect(result.resources.Resources.FriggKMSKey).toBeUndefined();
|
|
894
|
-
expect(result.provider.environment.KMS_KEY_ARN).toBe(fallbackArn);
|
|
1076
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow(
|
|
1077
|
+
'KMS field-level encryption is enabled but no KMS key was found. ' +
|
|
1078
|
+
'Either provide an existing KMS key or set encryption.createResourceIfNoneFound to true to create a new key.'
|
|
1079
|
+
);
|
|
895
1080
|
});
|
|
896
1081
|
|
|
897
1082
|
it('should not add KMS configuration when encryption is disabled', async () => {
|
|
@@ -1049,9 +1234,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
1049
1234
|
|
|
1050
1235
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1051
1236
|
|
|
1052
|
-
// Check integration function
|
|
1053
|
-
|
|
1054
|
-
expect(result.functions.testIntegration).toEqual(expect.objectContaining({
|
|
1237
|
+
// Check integration function
|
|
1238
|
+
expect(result.functions.testIntegration).toEqual({
|
|
1055
1239
|
handler: 'node_modules/@friggframework/core/handlers/routers/integration-defined-routers.handlers.testIntegration.handler',
|
|
1056
1240
|
events: [{
|
|
1057
1241
|
httpApi: {
|
|
@@ -1059,7 +1243,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
1059
1243
|
method: 'ANY'
|
|
1060
1244
|
}
|
|
1061
1245
|
}]
|
|
1062
|
-
})
|
|
1246
|
+
});
|
|
1063
1247
|
|
|
1064
1248
|
// Check SQS Queue
|
|
1065
1249
|
expect(result.resources.Resources.TestIntegrationQueue).toEqual({
|
|
@@ -1078,7 +1262,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
1078
1262
|
});
|
|
1079
1263
|
|
|
1080
1264
|
// Check Queue Worker
|
|
1081
|
-
expect(result.functions.testIntegrationQueueWorker).toEqual(
|
|
1265
|
+
expect(result.functions.testIntegrationQueueWorker).toEqual({
|
|
1082
1266
|
handler: 'node_modules/@friggframework/core/handlers/workers/integration-defined-workers.handlers.testIntegration.queueWorker',
|
|
1083
1267
|
reservedConcurrency: 20,
|
|
1084
1268
|
events: [{
|
|
@@ -1090,9 +1274,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
1090
1274
|
functionResponseType: 'ReportBatchItemFailures'
|
|
1091
1275
|
}
|
|
1092
1276
|
}],
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
}));
|
|
1277
|
+
timeout: 600
|
|
1278
|
+
});
|
|
1096
1279
|
|
|
1097
1280
|
// Check environment variable
|
|
1098
1281
|
expect(result.provider.environment.TESTINTEGRATION_QUEUE_URL).toEqual({
|
|
@@ -1129,20 +1312,146 @@ describe('composeServerlessDefinition', () => {
|
|
|
1129
1312
|
const fs = require('fs');
|
|
1130
1313
|
const path = require('path');
|
|
1131
1314
|
|
|
1132
|
-
it('should
|
|
1133
|
-
// serverless-offline resolves node_modules from the working
|
|
1134
|
-
// directory, so handler paths are no longer rewritten to ../node_modules.
|
|
1315
|
+
it('should rewrite handler paths in offline mode', async () => {
|
|
1135
1316
|
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);
|
|
1136
1320
|
|
|
1137
|
-
const
|
|
1321
|
+
const appDefinition = { integrations: [] };
|
|
1138
1322
|
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
);
|
|
1323
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
1324
|
+
|
|
1325
|
+
expect(existsSpy).toHaveBeenCalled();
|
|
1326
|
+
expect(result.functions.auth.handler.startsWith('../node_modules/')).toBe(true);
|
|
1327
|
+
|
|
1328
|
+
existsSpy.mockRestore();
|
|
1142
1329
|
});
|
|
1143
1330
|
});
|
|
1144
1331
|
|
|
1145
1332
|
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
|
+
|
|
1146
1455
|
it('should enable self-healing when selfHeal is true', async () => {
|
|
1147
1456
|
const appDefinition = {
|
|
1148
1457
|
vpc: {
|
|
@@ -1180,13 +1489,15 @@ describe('composeServerlessDefinition', () => {
|
|
|
1180
1489
|
|
|
1181
1490
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1182
1491
|
|
|
1183
|
-
// VPC
|
|
1492
|
+
// VPC
|
|
1184
1493
|
expect(result.provider.vpc).toBeDefined();
|
|
1185
|
-
|
|
1186
|
-
expect(result.resources.Resources.
|
|
1494
|
+
// custom.vpc doesn't exist in the serverless template
|
|
1495
|
+
expect(result.resources.Resources.VPCEndpointS3).toBeDefined();
|
|
1187
1496
|
|
|
1188
1497
|
// KMS
|
|
1498
|
+
expect(result.plugins).toContain('serverless-kms-grants');
|
|
1189
1499
|
expect(result.provider.environment.KMS_KEY_ARN).toBeDefined();
|
|
1500
|
+
expect(result.custom.kmsGrants).toBeDefined();
|
|
1190
1501
|
|
|
1191
1502
|
// SSM (enabled without offload markers -> broad grant only, no offload env vars)
|
|
1192
1503
|
const ssmPermission = result.provider.iamRoleStatements.find(
|
|
@@ -1199,12 +1510,14 @@ describe('composeServerlessDefinition', () => {
|
|
|
1199
1510
|
expect(result.functions.testIntegration).toBeDefined();
|
|
1200
1511
|
expect(result.resources.Resources.TestIntegrationQueue).toBeDefined();
|
|
1201
1512
|
|
|
1202
|
-
//
|
|
1513
|
+
// All plugins should be present
|
|
1203
1514
|
expect(result.plugins).toEqual([
|
|
1204
|
-
'serverless-
|
|
1515
|
+
'serverless-jetpack',
|
|
1516
|
+
'serverless-dotenv-plugin',
|
|
1205
1517
|
'serverless-offline-sqs',
|
|
1206
1518
|
'serverless-offline',
|
|
1207
1519
|
'@friggframework/serverless-plugin',
|
|
1520
|
+
'serverless-kms-grants'
|
|
1208
1521
|
]);
|
|
1209
1522
|
});
|
|
1210
1523
|
|
|
@@ -1225,13 +1538,10 @@ describe('composeServerlessDefinition', () => {
|
|
|
1225
1538
|
|
|
1226
1539
|
// VPC and KMS should be present
|
|
1227
1540
|
expect(result.provider.vpc).toBeDefined();
|
|
1228
|
-
expect(result.
|
|
1541
|
+
expect(result.custom.kmsGrants).toBeDefined();
|
|
1229
1542
|
|
|
1230
1543
|
// SSM should not be present
|
|
1231
|
-
|
|
1232
|
-
statement => statement.Action && statement.Action.includes('ssm:GetParameter')
|
|
1233
|
-
);
|
|
1234
|
-
expect(ssmPermission).toBeUndefined();
|
|
1544
|
+
expect(result.provider.layers).toBeUndefined();
|
|
1235
1545
|
expect(result.provider.environment.SSM_PARAMETER_PREFIX).toBeUndefined();
|
|
1236
1546
|
});
|
|
1237
1547
|
});
|
|
@@ -1255,14 +1565,10 @@ describe('composeServerlessDefinition', () => {
|
|
|
1255
1565
|
expect(result.functions.user).toBeDefined();
|
|
1256
1566
|
expect(result.functions.health).toBeDefined();
|
|
1257
1567
|
|
|
1258
|
-
// Check default plugins
|
|
1259
|
-
|
|
1260
|
-
expect(result.plugins).
|
|
1261
|
-
|
|
1262
|
-
'serverless-offline-sqs',
|
|
1263
|
-
'serverless-offline',
|
|
1264
|
-
'@friggframework/serverless-plugin',
|
|
1265
|
-
]);
|
|
1568
|
+
// Check default plugins
|
|
1569
|
+
expect(result.plugins).toContain('serverless-jetpack');
|
|
1570
|
+
expect(result.plugins).toContain('serverless-dotenv-plugin');
|
|
1571
|
+
expect(result.plugins).toContain('@friggframework/serverless-plugin');
|
|
1266
1572
|
});
|
|
1267
1573
|
|
|
1268
1574
|
it('should always include default IAM permissions', async () => {
|
|
@@ -1292,14 +1598,8 @@ describe('composeServerlessDefinition', () => {
|
|
|
1292
1598
|
|
|
1293
1599
|
const result = await composeServerlessDefinition(appDefinition);
|
|
1294
1600
|
|
|
1295
|
-
expect(result.provider.environment).
|
|
1296
|
-
|
|
1297
|
-
STAGE: '${self:provider.stage}',
|
|
1298
|
-
FRIGG_STAGE: '${self:provider.stage}',
|
|
1299
|
-
FRIGG_STACK: '${self:service}',
|
|
1300
|
-
FRIGG_REGION: '${self:provider.region}',
|
|
1301
|
-
})
|
|
1302
|
-
);
|
|
1601
|
+
expect(result.provider.environment.STAGE).toBe('${opt:stage, "dev"}');
|
|
1602
|
+
expect(result.provider.environment.AWS_NODEJS_CONNECTION_REUSE_ENABLED).toBe(1);
|
|
1303
1603
|
});
|
|
1304
1604
|
});
|
|
1305
1605
|
|
|
@@ -1356,77 +1656,306 @@ describe('composeServerlessDefinition', () => {
|
|
|
1356
1656
|
});
|
|
1357
1657
|
});
|
|
1358
1658
|
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1659
|
+
describe('CRITICAL: NAT Gateway MUST be in PUBLIC Subnet', () => {
|
|
1660
|
+
it('should NEVER reuse NAT Gateway in private subnet - throw error when selfHeal disabled', async () => {
|
|
1661
|
+
// Mock NAT Gateway found in PRIVATE subnet (the original bug)
|
|
1662
|
+
const mockDiscovery = require('./aws-discovery').AWSDiscovery;
|
|
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
|
+
});
|
|
1363
1870
|
|
|
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.
|
|
1367
1871
|
describe('Database Migration Lambda', () => {
|
|
1368
|
-
it('should include
|
|
1369
|
-
const
|
|
1872
|
+
it('should include dbMigrate function in all deployments', async () => {
|
|
1873
|
+
const appDefinition = {
|
|
1874
|
+
name: 'test-app',
|
|
1875
|
+
integrations: []
|
|
1876
|
+
};
|
|
1370
1877
|
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
expect(result.functions.
|
|
1878
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
1879
|
+
|
|
1880
|
+
// Check dbMigrate function exists
|
|
1881
|
+
expect(result.functions.dbMigrate).toBeDefined();
|
|
1882
|
+
expect(result.functions.dbMigrate.handler).toBe(
|
|
1375
1883
|
'node_modules/@friggframework/core/handlers/workers/db-migration.handler'
|
|
1376
1884
|
);
|
|
1377
|
-
expect(result.resources.Resources.DbMigrationQueue).toBeDefined();
|
|
1378
|
-
expect(result.resources.Resources.FriggMigrationStatusBucket).toBeDefined();
|
|
1379
1885
|
});
|
|
1380
1886
|
|
|
1381
|
-
it('should
|
|
1382
|
-
const
|
|
1383
|
-
|
|
1887
|
+
it('should configure dbMigrate with correct settings', async () => {
|
|
1888
|
+
const appDefinition = {
|
|
1889
|
+
integrations: []
|
|
1890
|
+
};
|
|
1384
1891
|
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
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);
|
|
1901
|
+
|
|
1902
|
+
// Check description
|
|
1903
|
+
expect(dbMigrate.description).toContain('database migrations');
|
|
1904
|
+
expect(dbMigrate.description).toContain('Prisma');
|
|
1395
1905
|
});
|
|
1396
1906
|
|
|
1397
|
-
it('should
|
|
1398
|
-
const
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
);
|
|
1907
|
+
it('should not have HTTP events (manual invocation only)', async () => {
|
|
1908
|
+
const appDefinition = {
|
|
1909
|
+
integrations: []
|
|
1910
|
+
};
|
|
1402
1911
|
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
);
|
|
1912
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
1913
|
+
|
|
1914
|
+
const dbMigrate = result.functions.dbMigrate;
|
|
1915
|
+
|
|
1916
|
+
// Should have no events (manually invoked via AWS CLI)
|
|
1917
|
+
expect(dbMigrate.events).toBeUndefined();
|
|
1409
1918
|
});
|
|
1410
1919
|
|
|
1411
|
-
it('should include
|
|
1412
|
-
const
|
|
1413
|
-
vpc: {
|
|
1414
|
-
|
|
1415
|
-
|
|
1920
|
+
it('should include dbMigrate even with VPC enabled', async () => {
|
|
1921
|
+
const appDefinition = {
|
|
1922
|
+
vpc: {
|
|
1923
|
+
enable: true,
|
|
1924
|
+
management: 'discover'
|
|
1925
|
+
},
|
|
1926
|
+
integrations: []
|
|
1927
|
+
};
|
|
1416
1928
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1929
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
1930
|
+
|
|
1931
|
+
// dbMigrate should exist with VPC configuration
|
|
1932
|
+
expect(result.functions.dbMigrate).toBeDefined();
|
|
1933
|
+
|
|
1934
|
+
// Should have same VPC access as other functions
|
|
1419
1935
|
expect(result.provider.vpc).toBeDefined();
|
|
1420
1936
|
});
|
|
1421
1937
|
|
|
1422
|
-
it('should
|
|
1423
|
-
const
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1938
|
+
it('should include dbMigrate with database configuration', async () => {
|
|
1939
|
+
const appDefinition = {
|
|
1940
|
+
vpc: {
|
|
1941
|
+
enable: true,
|
|
1942
|
+
management: 'discover'
|
|
1943
|
+
},
|
|
1944
|
+
database: {
|
|
1945
|
+
postgres: {
|
|
1946
|
+
enable: true,
|
|
1947
|
+
management: 'discover'
|
|
1948
|
+
}
|
|
1949
|
+
},
|
|
1950
|
+
integrations: []
|
|
1951
|
+
};
|
|
1952
|
+
|
|
1953
|
+
const result = await composeServerlessDefinition(appDefinition);
|
|
1427
1954
|
|
|
1428
|
-
|
|
1429
|
-
expect(result.functions.
|
|
1955
|
+
// dbMigrate should exist alongside database configuration
|
|
1956
|
+
expect(result.functions.dbMigrate).toBeDefined();
|
|
1957
|
+
expect(result.provider.environment.DB_TYPE).toBe('postgresql');
|
|
1958
|
+
expect(result.provider.environment.DATABASE_URL).toBeDefined();
|
|
1430
1959
|
});
|
|
1431
1960
|
});
|
|
1432
1961
|
|
|
@@ -1455,10 +1984,7 @@ describe('composeServerlessDefinition', () => {
|
|
|
1455
1984
|
integrations: [invalidIntegration]
|
|
1456
1985
|
};
|
|
1457
1986
|
|
|
1458
|
-
await
|
|
1459
|
-
appDefinition,
|
|
1460
|
-
'Integration at index 0 is missing Definition or name'
|
|
1461
|
-
);
|
|
1987
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow('Invalid integration: missing Definition or name');
|
|
1462
1988
|
});
|
|
1463
1989
|
|
|
1464
1990
|
it('should handle integration with missing name', async () => {
|
|
@@ -1469,80 +1995,9 @@ describe('composeServerlessDefinition', () => {
|
|
|
1469
1995
|
integrations: [invalidIntegration]
|
|
1470
1996
|
};
|
|
1471
1997
|
|
|
1472
|
-
await
|
|
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',
|
|
1998
|
+
await expect(composeServerlessDefinition(appDefinition)).rejects.toThrow('Invalid integration: missing Definition or name');
|
|
1511
1999
|
});
|
|
1512
2000
|
});
|
|
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
|
-
});
|
|
1546
2001
|
});
|
|
1547
2002
|
|
|
1548
2003
|
describe('applySsmPreloadNodeOptions', () => {
|