@friggframework/devtools 2.0.0-next.44 → 2.0.0-next.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/infrastructure/ARCHITECTURE.md +487 -0
  2. package/infrastructure/HEALTH.md +468 -0
  3. package/infrastructure/README.md +51 -0
  4. package/infrastructure/__tests__/postgres-config.test.js +914 -0
  5. package/infrastructure/__tests__/template-generation.test.js +687 -0
  6. package/infrastructure/create-frigg-infrastructure.js +1 -1
  7. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +630 -0
  8. package/infrastructure/{DEPLOYMENT-INSTRUCTIONS.md → docs/deployment-instructions.md} +3 -3
  9. package/infrastructure/{IAM-POLICY-TEMPLATES.md → docs/iam-policy-templates.md} +9 -10
  10. package/infrastructure/domains/database/aurora-builder.js +809 -0
  11. package/infrastructure/domains/database/aurora-builder.test.js +950 -0
  12. package/infrastructure/domains/database/aurora-discovery.js +87 -0
  13. package/infrastructure/domains/database/aurora-discovery.test.js +188 -0
  14. package/infrastructure/domains/database/aurora-resolver.js +210 -0
  15. package/infrastructure/domains/database/aurora-resolver.test.js +347 -0
  16. package/infrastructure/domains/database/migration-builder.js +633 -0
  17. package/infrastructure/domains/database/migration-builder.test.js +294 -0
  18. package/infrastructure/domains/database/migration-resolver.js +163 -0
  19. package/infrastructure/domains/database/migration-resolver.test.js +337 -0
  20. package/infrastructure/domains/health/application/ports/IPropertyReconciler.js +164 -0
  21. package/infrastructure/domains/health/application/ports/IResourceDetector.js +129 -0
  22. package/infrastructure/domains/health/application/ports/IResourceImporter.js +142 -0
  23. package/infrastructure/domains/health/application/ports/IStackRepository.js +131 -0
  24. package/infrastructure/domains/health/application/ports/index.js +26 -0
  25. package/infrastructure/domains/health/application/use-cases/__tests__/execute-resource-import-use-case.test.js +679 -0
  26. package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +167 -0
  27. package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +1130 -0
  28. package/infrastructure/domains/health/application/use-cases/execute-resource-import-use-case.js +221 -0
  29. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.js +152 -0
  30. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.test.js +343 -0
  31. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.js +535 -0
  32. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.test.js +376 -0
  33. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.js +213 -0
  34. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +441 -0
  35. package/infrastructure/domains/health/docs/ACME-DEV-DRIFT-ANALYSIS.md +267 -0
  36. package/infrastructure/domains/health/docs/BUILD-VS-DEPLOYED-TEMPLATE-ANALYSIS.md +324 -0
  37. package/infrastructure/domains/health/docs/ORPHAN-DETECTION-ANALYSIS.md +386 -0
  38. package/infrastructure/domains/health/docs/SPEC-CLEANUP-COMMAND.md +1419 -0
  39. package/infrastructure/domains/health/docs/TDD-IMPLEMENTATION-SUMMARY.md +391 -0
  40. package/infrastructure/domains/health/docs/TEMPLATE-COMPARISON-IMPLEMENTATION.md +551 -0
  41. package/infrastructure/domains/health/domain/entities/issue.js +299 -0
  42. package/infrastructure/domains/health/domain/entities/issue.test.js +528 -0
  43. package/infrastructure/domains/health/domain/entities/property-mismatch.js +108 -0
  44. package/infrastructure/domains/health/domain/entities/property-mismatch.test.js +275 -0
  45. package/infrastructure/domains/health/domain/entities/resource.js +159 -0
  46. package/infrastructure/domains/health/domain/entities/resource.test.js +432 -0
  47. package/infrastructure/domains/health/domain/entities/stack-health-report.js +306 -0
  48. package/infrastructure/domains/health/domain/entities/stack-health-report.test.js +601 -0
  49. package/infrastructure/domains/health/domain/services/__tests__/health-score-percentage-based.test.js +380 -0
  50. package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +971 -0
  51. package/infrastructure/domains/health/domain/services/__tests__/import-template-generator.test.js +1150 -0
  52. package/infrastructure/domains/health/domain/services/__tests__/logical-id-mapper.test.js +672 -0
  53. package/infrastructure/domains/health/domain/services/__tests__/template-parser.test.js +496 -0
  54. package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +419 -0
  55. package/infrastructure/domains/health/domain/services/health-score-calculator.js +248 -0
  56. package/infrastructure/domains/health/domain/services/health-score-calculator.test.js +504 -0
  57. package/infrastructure/domains/health/domain/services/import-progress-monitor.js +195 -0
  58. package/infrastructure/domains/health/domain/services/import-template-generator.js +435 -0
  59. package/infrastructure/domains/health/domain/services/logical-id-mapper.js +345 -0
  60. package/infrastructure/domains/health/domain/services/mismatch-analyzer.js +234 -0
  61. package/infrastructure/domains/health/domain/services/mismatch-analyzer.test.js +431 -0
  62. package/infrastructure/domains/health/domain/services/property-mutability-config.js +382 -0
  63. package/infrastructure/domains/health/domain/services/template-parser.js +245 -0
  64. package/infrastructure/domains/health/domain/services/update-progress-monitor.js +192 -0
  65. package/infrastructure/domains/health/domain/value-objects/health-score.js +138 -0
  66. package/infrastructure/domains/health/domain/value-objects/health-score.test.js +267 -0
  67. package/infrastructure/domains/health/domain/value-objects/property-mutability.js +161 -0
  68. package/infrastructure/domains/health/domain/value-objects/property-mutability.test.js +198 -0
  69. package/infrastructure/domains/health/domain/value-objects/resource-state.js +167 -0
  70. package/infrastructure/domains/health/domain/value-objects/resource-state.test.js +196 -0
  71. package/infrastructure/domains/health/domain/value-objects/stack-identifier.js +192 -0
  72. package/infrastructure/domains/health/domain/value-objects/stack-identifier.test.js +262 -0
  73. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-cfn-tagged.test.js +312 -0
  74. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-multi-stack.test.js +367 -0
  75. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +432 -0
  76. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.js +784 -0
  77. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +1133 -0
  78. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.js +565 -0
  79. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +554 -0
  80. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.js +318 -0
  81. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.test.js +398 -0
  82. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.js +777 -0
  83. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.test.js +580 -0
  84. package/infrastructure/domains/integration/integration-builder.js +397 -0
  85. package/infrastructure/domains/integration/integration-builder.test.js +593 -0
  86. package/infrastructure/domains/integration/integration-resolver.js +170 -0
  87. package/infrastructure/domains/integration/integration-resolver.test.js +369 -0
  88. package/infrastructure/domains/integration/websocket-builder.js +69 -0
  89. package/infrastructure/domains/integration/websocket-builder.test.js +195 -0
  90. package/infrastructure/domains/networking/vpc-builder.js +1829 -0
  91. package/infrastructure/domains/networking/vpc-builder.test.js +1262 -0
  92. package/infrastructure/domains/networking/vpc-discovery.js +177 -0
  93. package/infrastructure/domains/networking/vpc-discovery.test.js +350 -0
  94. package/infrastructure/domains/networking/vpc-resolver.js +324 -0
  95. package/infrastructure/domains/networking/vpc-resolver.test.js +501 -0
  96. package/infrastructure/domains/parameters/ssm-builder.js +79 -0
  97. package/infrastructure/domains/parameters/ssm-builder.test.js +189 -0
  98. package/infrastructure/domains/parameters/ssm-discovery.js +84 -0
  99. package/infrastructure/domains/parameters/ssm-discovery.test.js +210 -0
  100. package/infrastructure/{iam-generator.js → domains/security/iam-generator.js} +2 -2
  101. package/infrastructure/domains/security/kms-builder.js +366 -0
  102. package/infrastructure/domains/security/kms-builder.test.js +374 -0
  103. package/infrastructure/domains/security/kms-discovery.js +80 -0
  104. package/infrastructure/domains/security/kms-discovery.test.js +177 -0
  105. package/infrastructure/domains/security/kms-resolver.js +96 -0
  106. package/infrastructure/domains/security/kms-resolver.test.js +216 -0
  107. package/infrastructure/domains/shared/base-builder.js +112 -0
  108. package/infrastructure/domains/shared/base-resolver.js +186 -0
  109. package/infrastructure/domains/shared/base-resolver.test.js +305 -0
  110. package/infrastructure/domains/shared/builder-orchestrator.js +212 -0
  111. package/infrastructure/domains/shared/builder-orchestrator.test.js +213 -0
  112. package/infrastructure/domains/shared/cloudformation-discovery-v2.js +334 -0
  113. package/infrastructure/domains/shared/cloudformation-discovery.js +375 -0
  114. package/infrastructure/domains/shared/cloudformation-discovery.test.js +590 -0
  115. package/infrastructure/domains/shared/environment-builder.js +119 -0
  116. package/infrastructure/domains/shared/environment-builder.test.js +247 -0
  117. package/infrastructure/domains/shared/providers/aws-provider-adapter.js +544 -0
  118. package/infrastructure/domains/shared/providers/aws-provider-adapter.test.js +377 -0
  119. package/infrastructure/domains/shared/providers/azure-provider-adapter.stub.js +93 -0
  120. package/infrastructure/domains/shared/providers/cloud-provider-adapter.js +136 -0
  121. package/infrastructure/domains/shared/providers/gcp-provider-adapter.stub.js +82 -0
  122. package/infrastructure/domains/shared/providers/provider-factory.js +108 -0
  123. package/infrastructure/domains/shared/providers/provider-factory.test.js +170 -0
  124. package/infrastructure/domains/shared/resource-discovery.js +192 -0
  125. package/infrastructure/domains/shared/resource-discovery.test.js +552 -0
  126. package/infrastructure/domains/shared/types/app-definition.js +205 -0
  127. package/infrastructure/domains/shared/types/discovery-result.js +106 -0
  128. package/infrastructure/domains/shared/types/discovery-result.test.js +258 -0
  129. package/infrastructure/domains/shared/types/index.js +46 -0
  130. package/infrastructure/domains/shared/types/resource-ownership.js +108 -0
  131. package/infrastructure/domains/shared/types/resource-ownership.test.js +101 -0
  132. package/infrastructure/domains/shared/utilities/base-definition-factory.js +380 -0
  133. package/infrastructure/domains/shared/utilities/base-definition-factory.js.bak +338 -0
  134. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +248 -0
  135. package/infrastructure/domains/shared/utilities/handler-path-resolver.js +134 -0
  136. package/infrastructure/domains/shared/utilities/handler-path-resolver.test.js +268 -0
  137. package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +55 -0
  138. package/infrastructure/domains/shared/utilities/prisma-layer-manager.test.js +138 -0
  139. package/infrastructure/{env-validator.js → domains/shared/validation/env-validator.js} +2 -1
  140. package/infrastructure/domains/shared/validation/env-validator.test.js +173 -0
  141. package/infrastructure/esbuild.config.js +53 -0
  142. package/infrastructure/infrastructure-composer.js +87 -0
  143. package/infrastructure/{serverless-template.test.js → infrastructure-composer.test.js} +115 -24
  144. package/infrastructure/scripts/build-prisma-layer.js +553 -0
  145. package/infrastructure/scripts/build-prisma-layer.test.js +102 -0
  146. package/infrastructure/{build-time-discovery.js → scripts/build-time-discovery.js} +80 -48
  147. package/infrastructure/{build-time-discovery.test.js → scripts/build-time-discovery.test.js} +5 -4
  148. package/layers/prisma/nodejs/package.json +8 -0
  149. package/management-ui/server/utils/cliIntegration.js +1 -1
  150. package/management-ui/server/utils/environment/awsParameterStore.js +29 -18
  151. package/package.json +11 -11
  152. package/frigg-cli/.eslintrc.js +0 -141
  153. package/frigg-cli/__tests__/unit/commands/build.test.js +0 -251
  154. package/frigg-cli/__tests__/unit/commands/db-setup.test.js +0 -548
  155. package/frigg-cli/__tests__/unit/commands/install.test.js +0 -400
  156. package/frigg-cli/__tests__/unit/commands/ui.test.js +0 -346
  157. package/frigg-cli/__tests__/unit/utils/database-validator.test.js +0 -366
  158. package/frigg-cli/__tests__/unit/utils/error-messages.test.js +0 -304
  159. package/frigg-cli/__tests__/unit/utils/prisma-runner.test.js +0 -486
  160. package/frigg-cli/__tests__/utils/mock-factory.js +0 -270
  161. package/frigg-cli/__tests__/utils/prisma-mock.js +0 -194
  162. package/frigg-cli/__tests__/utils/test-fixtures.js +0 -463
  163. package/frigg-cli/__tests__/utils/test-setup.js +0 -287
  164. package/frigg-cli/build-command/index.js +0 -65
  165. package/frigg-cli/db-setup-command/index.js +0 -193
  166. package/frigg-cli/deploy-command/index.js +0 -175
  167. package/frigg-cli/generate-command/__tests__/generate-command.test.js +0 -301
  168. package/frigg-cli/generate-command/azure-generator.js +0 -43
  169. package/frigg-cli/generate-command/gcp-generator.js +0 -47
  170. package/frigg-cli/generate-command/index.js +0 -332
  171. package/frigg-cli/generate-command/terraform-generator.js +0 -555
  172. package/frigg-cli/generate-iam-command.js +0 -118
  173. package/frigg-cli/index.js +0 -75
  174. package/frigg-cli/index.test.js +0 -158
  175. package/frigg-cli/init-command/backend-first-handler.js +0 -756
  176. package/frigg-cli/init-command/index.js +0 -93
  177. package/frigg-cli/init-command/template-handler.js +0 -143
  178. package/frigg-cli/install-command/backend-js.js +0 -33
  179. package/frigg-cli/install-command/commit-changes.js +0 -16
  180. package/frigg-cli/install-command/environment-variables.js +0 -127
  181. package/frigg-cli/install-command/environment-variables.test.js +0 -136
  182. package/frigg-cli/install-command/index.js +0 -54
  183. package/frigg-cli/install-command/install-package.js +0 -13
  184. package/frigg-cli/install-command/integration-file.js +0 -30
  185. package/frigg-cli/install-command/logger.js +0 -12
  186. package/frigg-cli/install-command/template.js +0 -90
  187. package/frigg-cli/install-command/validate-package.js +0 -75
  188. package/frigg-cli/jest.config.js +0 -124
  189. package/frigg-cli/package.json +0 -54
  190. package/frigg-cli/start-command/index.js +0 -149
  191. package/frigg-cli/start-command/start-command.test.js +0 -297
  192. package/frigg-cli/test/init-command.test.js +0 -180
  193. package/frigg-cli/test/npm-registry.test.js +0 -319
  194. package/frigg-cli/ui-command/index.js +0 -154
  195. package/frigg-cli/utils/app-resolver.js +0 -319
  196. package/frigg-cli/utils/backend-path.js +0 -25
  197. package/frigg-cli/utils/database-validator.js +0 -161
  198. package/frigg-cli/utils/error-messages.js +0 -257
  199. package/frigg-cli/utils/npm-registry.js +0 -167
  200. package/frigg-cli/utils/prisma-runner.js +0 -280
  201. package/frigg-cli/utils/process-manager.js +0 -199
  202. package/frigg-cli/utils/repo-detection.js +0 -405
  203. package/infrastructure/aws-discovery.js +0 -1176
  204. package/infrastructure/aws-discovery.test.js +0 -1220
  205. package/infrastructure/serverless-template.js +0 -2074
  206. /package/infrastructure/{WEBSOCKET-CONFIGURATION.md → docs/WEBSOCKET-CONFIGURATION.md} +0 -0
  207. /package/infrastructure/{GENERATE-IAM-DOCS.md → docs/generate-iam-command.md} +0 -0
  208. /package/infrastructure/{iam-generator.test.js → domains/security/iam-generator.test.js} +0 -0
  209. /package/infrastructure/{frigg-deployment-iam-stack.yaml → domains/security/templates/frigg-deployment-iam-stack.yaml} +0 -0
  210. /package/infrastructure/{iam-policy-basic.json → domains/security/templates/iam-policy-basic.json} +0 -0
  211. /package/infrastructure/{iam-policy-full.json → domains/security/templates/iam-policy-full.json} +0 -0
  212. /package/infrastructure/{run-discovery.js → scripts/run-discovery.js} +0 -0
@@ -0,0 +1,96 @@
1
+ /**
2
+ * KMS (Key Management Service) Resource Resolver
3
+ *
4
+ * Resolves KMS key ownership based on user intent and discovered resources.
5
+ *
6
+ * Ownership Resolution Logic:
7
+ * - User sets 'stack' → Create KMS key in CloudFormation stack
8
+ * - User sets 'external' → Use existing KMS key (discovered or env var)
9
+ * - User sets 'auto' (or unspecified):
10
+ * - If KMS key found in stack → Use stack resource (STACK)
11
+ * - If KMS key found externally → Use external resource (EXTERNAL)
12
+ * - If nothing found → Create in stack (STACK)
13
+ */
14
+
15
+ const BaseResourceResolver = require('../shared/base-resolver');
16
+ const { ResourceOwnership } = require('../shared/types');
17
+
18
+ class KmsResourceResolver extends BaseResourceResolver {
19
+ constructor() {
20
+ super();
21
+ }
22
+
23
+ /**
24
+ * Resolve KMS key ownership
25
+ * @param {Object} appDefinition - Application definition
26
+ * @param {Object} discovery - Structured discovery result
27
+ * @returns {Object} Ownership decision with metadata
28
+ */
29
+ resolveKey(appDefinition, discovery) {
30
+ // Get user intent from app definition
31
+ const userIntent = appDefinition.encryption?.ownership?.key || ResourceOwnership.AUTO;
32
+
33
+ // Check if KMS key exists in CloudFormation stack
34
+ const inStack = this.isInStack('FriggKMSKey', discovery);
35
+
36
+ if (userIntent === ResourceOwnership.STACK) {
37
+ // Explicit: Create/manage in stack
38
+ const stackResource = inStack ? this.findInStack('FriggKMSKey', discovery) : null;
39
+ return this.createStackDecision(
40
+ stackResource?.physicalId || null,
41
+ inStack ? 'Found FriggKMSKey in CloudFormation stack' : 'Will create FriggKMSKey in stack'
42
+ );
43
+ }
44
+
45
+ if (userIntent === ResourceOwnership.EXTERNAL) {
46
+ // Explicit: Use external key
47
+ const external = this.findExternal('AWS::KMS::Key', discovery);
48
+ if (!external) {
49
+ throw new Error(
50
+ 'ownership.key=external but no KMS key discovered. ' +
51
+ 'Provide defaultKmsKeyId in discoveredResources or set ownership.key=stack'
52
+ );
53
+ }
54
+ return this.createExternalDecision(
55
+ external.physicalId,
56
+ 'Using external KMS key per ownership.key=external'
57
+ );
58
+ }
59
+
60
+ // AUTO resolution
61
+ if (inStack) {
62
+ const stackResource = this.findInStack('FriggKMSKey', discovery);
63
+ return this.createStackDecision(
64
+ stackResource.physicalId,
65
+ 'Found FriggKMSKey in CloudFormation stack'
66
+ );
67
+ }
68
+
69
+ // Check for external KMS key
70
+ const external = this.findExternal('AWS::KMS::Key', discovery);
71
+ if (external) {
72
+ return this.createExternalDecision(
73
+ external.physicalId,
74
+ 'Found external KMS key via discovery'
75
+ );
76
+ }
77
+
78
+ // No KMS key found - create in stack
79
+ return this.createStackDecision(
80
+ null,
81
+ 'No existing KMS key - will create in stack'
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Resolve all KMS resources
87
+ * Convenience method for resolving all KMS resource ownership
88
+ */
89
+ resolveAll(appDefinition, discovery) {
90
+ return {
91
+ key: this.resolveKey(appDefinition, discovery),
92
+ };
93
+ }
94
+ }
95
+
96
+ module.exports = { KmsResourceResolver };
@@ -0,0 +1,216 @@
1
+ const { KmsResourceResolver } = require('./kms-resolver');
2
+ const { ResourceOwnership, createEmptyDiscoveryResult } = require('../shared/types');
3
+
4
+ describe('KmsResourceResolver', () => {
5
+ let resolver;
6
+
7
+ beforeEach(() => {
8
+ resolver = new KmsResourceResolver();
9
+ });
10
+
11
+ describe('resolveKey', () => {
12
+ describe('Explicit ownership intent', () => {
13
+ it('should respect ownership.key=stack when specified', () => {
14
+ const appDefinition = {
15
+ encryption: {
16
+ ownership: { key: 'stack' }
17
+ }
18
+ };
19
+ const discovery = createEmptyDiscoveryResult();
20
+
21
+ const decision = resolver.resolveKey(appDefinition, discovery);
22
+
23
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
24
+ expect(decision.physicalId).toBeNull();
25
+ expect(decision.reason).toContain('Will create FriggKMSKey in stack');
26
+ });
27
+
28
+ it('should respect ownership.key=external when KMS key discovered', () => {
29
+ const appDefinition = {
30
+ encryption: {
31
+ ownership: { key: 'external' }
32
+ }
33
+ };
34
+ const discovery = createEmptyDiscoveryResult();
35
+ discovery.external.push({
36
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/abcd-1234',
37
+ resourceType: 'AWS::KMS::Key',
38
+ source: 'aws-discovery'
39
+ });
40
+
41
+ const decision = resolver.resolveKey(appDefinition, discovery);
42
+
43
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
44
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/abcd-1234');
45
+ expect(decision.reason).toContain('external');
46
+ });
47
+
48
+ it('should error when ownership.key=external but no KMS key discovered', () => {
49
+ const appDefinition = {
50
+ encryption: {
51
+ ownership: { key: 'external' }
52
+ }
53
+ };
54
+ const discovery = createEmptyDiscoveryResult();
55
+
56
+ expect(() => resolver.resolveKey(appDefinition, discovery))
57
+ .toThrow('ownership.key=external but no KMS key discovered');
58
+ });
59
+ });
60
+
61
+ describe('Auto resolution (ownership.key=auto)', () => {
62
+ it('should use stack KMS key when found in CloudFormation', () => {
63
+ const appDefinition = {
64
+ encryption: {
65
+ ownership: { key: 'auto' }
66
+ }
67
+ };
68
+ const discovery = createEmptyDiscoveryResult();
69
+ discovery.fromCloudFormation = true;
70
+ discovery.stackManaged.push({
71
+ logicalId: 'FriggKMSKey',
72
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/stack-key',
73
+ resourceType: 'AWS::KMS::Key'
74
+ });
75
+
76
+ const decision = resolver.resolveKey(appDefinition, discovery);
77
+
78
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
79
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/stack-key');
80
+ expect(decision.reason).toContain('Found FriggKMSKey in CloudFormation stack');
81
+ });
82
+
83
+ it('should use external KMS key when found via discovery', () => {
84
+ const appDefinition = {
85
+ encryption: {
86
+ ownership: { key: 'auto' }
87
+ }
88
+ };
89
+ const discovery = createEmptyDiscoveryResult();
90
+ discovery.external.push({
91
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/external-key',
92
+ resourceType: 'AWS::KMS::Key',
93
+ source: 'aws-discovery'
94
+ });
95
+
96
+ const decision = resolver.resolveKey(appDefinition, discovery);
97
+
98
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
99
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/external-key');
100
+ expect(decision.reason).toContain('Found external KMS key via discovery');
101
+ });
102
+
103
+ it('should create new KMS key when none found', () => {
104
+ const appDefinition = {
105
+ encryption: {
106
+ ownership: { key: 'auto' }
107
+ }
108
+ };
109
+ const discovery = createEmptyDiscoveryResult();
110
+
111
+ const decision = resolver.resolveKey(appDefinition, discovery);
112
+
113
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
114
+ expect(decision.physicalId).toBeNull();
115
+ expect(decision.reason).toContain('No existing KMS key - will create in stack');
116
+ });
117
+ });
118
+
119
+ describe('Default behavior (no ownership specified)', () => {
120
+ it('should default to auto resolution', () => {
121
+ const appDefinition = {
122
+ encryption: {
123
+ fieldLevelEncryptionMethod: 'kms'
124
+ // No ownership specified
125
+ }
126
+ };
127
+ const discovery = createEmptyDiscoveryResult();
128
+
129
+ const decision = resolver.resolveKey(appDefinition, discovery);
130
+
131
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
132
+ expect(decision.reason).toContain('No existing KMS key - will create in stack');
133
+ });
134
+ });
135
+ });
136
+
137
+ describe('resolveAll', () => {
138
+ it('should return decisions for all KMS resources', () => {
139
+ const appDefinition = {
140
+ encryption: {
141
+ fieldLevelEncryptionMethod: 'kms'
142
+ }
143
+ };
144
+ const discovery = createEmptyDiscoveryResult();
145
+
146
+ const decisions = resolver.resolveAll(appDefinition, discovery);
147
+
148
+ expect(decisions).toHaveProperty('key');
149
+ expect(decisions.key.ownership).toBe(ResourceOwnership.STACK);
150
+ });
151
+ });
152
+
153
+ describe('Real-world scenarios', () => {
154
+ it('should handle managementMode=managed scenario (create KMS)', () => {
155
+ // In managed mode, we want to create KMS in stack
156
+ const appDefinition = {
157
+ managementMode: 'managed',
158
+ encryption: {
159
+ fieldLevelEncryptionMethod: 'kms',
160
+ ownership: { key: 'stack' }
161
+ }
162
+ };
163
+ const discovery = createEmptyDiscoveryResult();
164
+
165
+ const decision = resolver.resolveKey(appDefinition, discovery);
166
+
167
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
168
+ expect(decision.physicalId).toBeNull();
169
+ });
170
+
171
+ it('should handle existing stack KMS key (reuse)', () => {
172
+ // Stack already has KMS from previous deployment
173
+ const appDefinition = {
174
+ encryption: {
175
+ fieldLevelEncryptionMethod: 'kms',
176
+ ownership: { key: 'auto' }
177
+ }
178
+ };
179
+ const discovery = createEmptyDiscoveryResult();
180
+ discovery.fromCloudFormation = true;
181
+ discovery.stackManaged.push({
182
+ logicalId: 'FriggKMSKey',
183
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/existing-stack-key',
184
+ resourceType: 'AWS::KMS::Key'
185
+ });
186
+
187
+ const decision = resolver.resolveKey(appDefinition, discovery);
188
+
189
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
190
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/existing-stack-key');
191
+ });
192
+
193
+ it('should handle shared KMS key scenario (vpcIsolation=shared)', () => {
194
+ // Using shared infrastructure KMS key
195
+ const appDefinition = {
196
+ managementMode: 'managed',
197
+ vpcIsolation: 'shared',
198
+ encryption: {
199
+ fieldLevelEncryptionMethod: 'kms',
200
+ ownership: { key: 'auto' }
201
+ }
202
+ };
203
+ const discovery = createEmptyDiscoveryResult();
204
+ discovery.external.push({
205
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/shared-key',
206
+ resourceType: 'AWS::KMS::Key',
207
+ source: 'aws-discovery'
208
+ });
209
+
210
+ const decision = resolver.resolveKey(appDefinition, discovery);
211
+
212
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
213
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/shared-key');
214
+ });
215
+ });
216
+ });
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Base Infrastructure Builder Interface
3
+ *
4
+ * Domain Layer - Hexagonal Architecture
5
+ *
6
+ * This abstract class defines the contract for all infrastructure builders.
7
+ * Each infrastructure domain (VPC, KMS, Database, etc.) implements this interface.
8
+ *
9
+ * Benefits of Hexagonal Architecture:
10
+ * - Domain logic separated from infrastructure concerns
11
+ * - Easy to test in isolation
12
+ * - Dependency injection for cross-cutting concerns
13
+ * - Clear boundaries between domains
14
+ * - Enables parallel execution where dependencies allow
15
+ */
16
+
17
+ class InfrastructureBuilder {
18
+ /**
19
+ * Build infrastructure resources
20
+ *
21
+ * @param {Object} appDefinition - Application definition from user
22
+ * @param {Object} discoveredResources - Resources discovered from AWS
23
+ * @returns {Object} CloudFormation resources to add to template
24
+ * @throws {Error} If validation fails or build encounters errors
25
+ */
26
+ async build(appDefinition, discoveredResources) {
27
+ throw new Error('InfrastructureBuilder.build() must be implemented by subclass');
28
+ }
29
+
30
+ /**
31
+ * Validate configuration before building
32
+ *
33
+ * @param {Object} config - Configuration to validate
34
+ * @returns {Object} Validation result { valid: boolean, errors: string[] }
35
+ */
36
+ validate(config) {
37
+ throw new Error('InfrastructureBuilder.validate() must be implemented by subclass');
38
+ }
39
+
40
+ /**
41
+ * Check if this builder should execute
42
+ *
43
+ * @param {Object} appDefinition - Application definition
44
+ * @returns {boolean} True if builder should execute
45
+ */
46
+ shouldExecute(appDefinition) {
47
+ return false;
48
+ }
49
+
50
+ /**
51
+ * Get dependencies (other builders that must execute first)
52
+ *
53
+ * @returns {Array<string>} Array of builder names this depends on
54
+ */
55
+ getDependencies() {
56
+ return [];
57
+ }
58
+
59
+ /**
60
+ * Get builder name for logging and dependency resolution
61
+ *
62
+ * @returns {string} Builder name
63
+ */
64
+ getName() {
65
+ return this.constructor.name;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Value Object for validation results
71
+ */
72
+ class ValidationResult {
73
+ constructor(valid = true, errors = [], warnings = []) {
74
+ this.valid = valid;
75
+ this.errors = errors;
76
+ this.warnings = warnings;
77
+ }
78
+
79
+ addError(error) {
80
+ this.errors.push(error);
81
+ this.valid = false;
82
+ }
83
+
84
+ addWarning(warning) {
85
+ this.warnings.push(warning);
86
+ }
87
+
88
+ hasErrors() {
89
+ return this.errors.length > 0;
90
+ }
91
+
92
+ hasWarnings() {
93
+ return this.warnings.length > 0;
94
+ }
95
+
96
+ toString() {
97
+ let result = `Valid: ${this.valid}\n`;
98
+ if (this.errors.length > 0) {
99
+ result += `Errors:\n - ${this.errors.join('\n - ')}\n`;
100
+ }
101
+ if (this.warnings.length > 0) {
102
+ result += `Warnings:\n - ${this.warnings.join('\n - ')}\n`;
103
+ }
104
+ return result;
105
+ }
106
+ }
107
+
108
+ module.exports = {
109
+ InfrastructureBuilder,
110
+ ValidationResult,
111
+ };
112
+
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Base Resource Resolver
3
+ *
4
+ * Abstract base class for resource ownership resolution.
5
+ * Each builder has its own resolver (VpcResolver, AuroraResolver, etc.)
6
+ * that extends this base class.
7
+ *
8
+ * Resolver Layer - Hexagonal Architecture
9
+ */
10
+
11
+ const {
12
+ ResourceOwnership,
13
+ resolveOwnership,
14
+ findStackResource,
15
+ findExternalResource,
16
+ findAllExternalResources,
17
+ isResourceInStack
18
+ } = require('./types');
19
+
20
+ class BaseResourceResolver {
21
+ /**
22
+ * Find resource in CloudFormation stack
23
+ * @protected
24
+ * @param {string} logicalId - Logical resource ID
25
+ * @param {Object} discovery - Discovery result
26
+ * @returns {Object|null} Stack resource or null
27
+ */
28
+ findInStack(logicalId, discovery) {
29
+ return findStackResource(discovery, logicalId);
30
+ }
31
+
32
+ /**
33
+ * Find external resource by type
34
+ * @protected
35
+ * @param {string} resourceType - CloudFormation resource type
36
+ * @param {Object} discovery - Discovery result
37
+ * @returns {Object|null} External resource or null
38
+ */
39
+ findExternal(resourceType, discovery) {
40
+ return findExternalResource(discovery, resourceType);
41
+ }
42
+
43
+ /**
44
+ * Find all external resources by type
45
+ * @protected
46
+ * @param {Object} discovery - Discovery result
47
+ * @param {string} resourceType - CloudFormation resource type
48
+ * @returns {Object[]} Array of external resources
49
+ */
50
+ findAllExternalResources(discovery, resourceType) {
51
+ return findAllExternalResources(discovery, resourceType);
52
+ }
53
+
54
+ /**
55
+ * Check if resource is in stack
56
+ * @protected
57
+ * @param {string} logicalId - Logical resource ID
58
+ * @param {Object} discovery - Discovery result
59
+ * @returns {boolean}
60
+ */
61
+ isInStack(logicalId, discovery) {
62
+ return isResourceInStack(discovery, logicalId);
63
+ }
64
+
65
+ /**
66
+ * Validate that external resource IDs are provided when required
67
+ * @protected
68
+ * @param {*} resourceIds - Resource IDs to validate
69
+ * @param {string} resourceName - Name for error message
70
+ * @throws {Error} If resourceIds is not provided
71
+ */
72
+ requireExternalIds(resourceIds, resourceName) {
73
+ if (!resourceIds || (Array.isArray(resourceIds) && resourceIds.length === 0)) {
74
+ throw new Error(
75
+ `ownership='external' for ${resourceName} requires external.${resourceName} to be provided in app definition`
76
+ );
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Resolve ownership for a resource
82
+ * @protected
83
+ * @param {string} userIntent - User's ownership intent ('stack' | 'external' | 'auto')
84
+ * @param {string} logicalId - CloudFormation logical ID
85
+ * @param {string} resourceType - CloudFormation resource type
86
+ * @param {Object} discovery - Discovery result
87
+ * @returns {Object} Resource decision
88
+ */
89
+ resolveResourceOwnership(userIntent, logicalId, resourceType, discovery) {
90
+ // Use helper to work with both old flat structure and new structured
91
+ const structured = discovery._structured || discovery;
92
+
93
+ const inStack = this.isInStack(logicalId, structured);
94
+ const externalResource = this.findExternal(resourceType, structured);
95
+
96
+ const ownership = resolveOwnership(
97
+ userIntent || ResourceOwnership.AUTO,
98
+ inStack,
99
+ externalResource !== null
100
+ );
101
+
102
+ const stackResource = inStack ? this.findInStack(logicalId, structured) : null;
103
+
104
+ return {
105
+ ownership,
106
+ physicalId: stackResource?.physicalId || externalResource?.physicalId,
107
+ reason: this._buildReasonString(ownership, inStack, externalResource, userIntent),
108
+ metadata: {
109
+ logicalId,
110
+ resourceType,
111
+ userIntent: userIntent || 'auto',
112
+ inStack,
113
+ foundExternal: externalResource !== null
114
+ }
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Build human-readable reason string
120
+ * @private
121
+ */
122
+ _buildReasonString(ownership, inStack, externalResource, userIntent) {
123
+ if (userIntent === 'stack') {
124
+ return 'User explicitly specified ownership=stack';
125
+ }
126
+
127
+ if (userIntent === 'external') {
128
+ return 'User explicitly specified ownership=external';
129
+ }
130
+
131
+ // Auto-decided
132
+ if (ownership === ResourceOwnership.STACK) {
133
+ if (inStack) {
134
+ return 'Found in CloudFormation stack (must keep in template to avoid deletion)';
135
+ }
136
+ return 'No existing resource found - will create in stack';
137
+ }
138
+
139
+ if (ownership === ResourceOwnership.EXTERNAL) {
140
+ return 'Found external resource via discovery';
141
+ }
142
+
143
+ return 'Ownership resolved via auto-detection';
144
+ }
145
+
146
+ /**
147
+ * Create a resource decision for explicit external reference
148
+ * @protected
149
+ * @param {string|string[]} physicalIds - Physical resource ID(s)
150
+ * @param {string} reason - Reason string
151
+ * @returns {Object} Resource decision
152
+ */
153
+ createExternalDecision(physicalIds, reason = 'Using external resource reference') {
154
+ const ids = Array.isArray(physicalIds) ? physicalIds : [physicalIds];
155
+
156
+ return {
157
+ ownership: ResourceOwnership.EXTERNAL,
158
+ physicalId: ids[0],
159
+ physicalIds: ids,
160
+ reason,
161
+ metadata: {
162
+ source: 'user-provided'
163
+ }
164
+ };
165
+ }
166
+
167
+ /**
168
+ * Create a resource decision for stack-managed resource
169
+ * @protected
170
+ * @param {string} [physicalId] - Physical ID if resource already exists
171
+ * @param {string} reason - Reason string
172
+ * @returns {Object} Resource decision
173
+ */
174
+ createStackDecision(physicalId = null, reason = 'Managed by CloudFormation stack') {
175
+ return {
176
+ ownership: ResourceOwnership.STACK,
177
+ physicalId,
178
+ reason,
179
+ metadata: {
180
+ source: physicalId ? 'discovered' : 'new'
181
+ }
182
+ };
183
+ }
184
+ }
185
+
186
+ module.exports = BaseResourceResolver;