@friggframework/devtools 2.0.0--canary.474.082077e.0 → 2.0.0--canary.474.884529c.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.
@@ -0,0 +1,397 @@
1
+ /**
2
+ * AWSPropertyReconciler - AWS Property Drift Reconciliation Adapter
3
+ *
4
+ * Infrastructure Adapter - Hexagonal Architecture
5
+ *
6
+ * Implements IPropertyReconciler port for AWS.
7
+ * Handles property drift reconciliation via template or resource updates.
8
+ *
9
+ * Lazy-loads AWS SDK to minimize cold start time and memory usage.
10
+ */
11
+
12
+ const IPropertyReconciler = require('../../application/ports/IPropertyReconciler');
13
+
14
+ // Lazy-loaded AWS SDK clients
15
+ let CloudFormationClient, UpdateStackCommand, GetTemplateCommand;
16
+ let EC2Client, ModifyVpcAttributeCommand;
17
+
18
+ /**
19
+ * Lazy load CloudFormation SDK
20
+ */
21
+ function loadCloudFormation() {
22
+ if (!CloudFormationClient) {
23
+ const cfModule = require('@aws-sdk/client-cloudformation');
24
+ CloudFormationClient = cfModule.CloudFormationClient;
25
+ UpdateStackCommand = cfModule.UpdateStackCommand;
26
+ GetTemplateCommand = cfModule.GetTemplateCommand;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Lazy load EC2 SDK
32
+ */
33
+ function loadEC2() {
34
+ if (!EC2Client) {
35
+ const ec2Module = require('@aws-sdk/client-ec2');
36
+ EC2Client = ec2Module.EC2Client;
37
+ ModifyVpcAttributeCommand = ec2Module.ModifyVpcAttributeCommand;
38
+ }
39
+ }
40
+
41
+ class AWSPropertyReconciler extends IPropertyReconciler {
42
+ /**
43
+ * Resource types that support reconciliation
44
+ * @private
45
+ */
46
+ static SUPPORTED_TYPES = {
47
+ 'AWS::EC2::VPC': {
48
+ templateUpdate: true,
49
+ resourceUpdate: true,
50
+ recommendedMode: 'template',
51
+ limitations: ['Some VPC properties require resource replacement (e.g., CidrBlock)'],
52
+ },
53
+ 'AWS::EC2::Subnet': {
54
+ templateUpdate: true,
55
+ resourceUpdate: false,
56
+ recommendedMode: 'template',
57
+ limitations: ['Most Subnet properties are immutable'],
58
+ },
59
+ 'AWS::EC2::SecurityGroup': {
60
+ templateUpdate: true,
61
+ resourceUpdate: true,
62
+ recommendedMode: 'template',
63
+ limitations: ['Rule changes may cause brief connectivity interruption'],
64
+ },
65
+ 'AWS::EC2::RouteTable': {
66
+ templateUpdate: true,
67
+ resourceUpdate: false,
68
+ recommendedMode: 'template',
69
+ limitations: ['Route changes require CloudFormation update'],
70
+ },
71
+ 'AWS::RDS::DBCluster': {
72
+ templateUpdate: true,
73
+ resourceUpdate: false,
74
+ recommendedMode: 'template',
75
+ limitations: ['Many DBCluster properties require specific update windows'],
76
+ },
77
+ 'AWS::KMS::Key': {
78
+ templateUpdate: true,
79
+ resourceUpdate: false,
80
+ recommendedMode: 'template',
81
+ limitations: ['Key policy changes must be done via CloudFormation'],
82
+ },
83
+ };
84
+
85
+ /**
86
+ * Create AWS Property Reconciler
87
+ *
88
+ * @param {Object} [config={}]
89
+ * @param {string} [config.region] - AWS region (defaults to AWS_REGION env var)
90
+ */
91
+ constructor(config = {}) {
92
+ super();
93
+ this.region = config.region || process.env.AWS_REGION || 'us-east-1';
94
+ this.cfClient = null;
95
+ this.ec2Client = null;
96
+ }
97
+
98
+ /**
99
+ * Get or create CloudFormation client
100
+ * @private
101
+ */
102
+ _getCFClient() {
103
+ if (!this.cfClient) {
104
+ loadCloudFormation();
105
+ this.cfClient = new CloudFormationClient({ region: this.region });
106
+ }
107
+ return this.cfClient;
108
+ }
109
+
110
+ /**
111
+ * Get or create EC2 client
112
+ * @private
113
+ */
114
+ _getEC2Client() {
115
+ if (!this.ec2Client) {
116
+ loadEC2();
117
+ this.ec2Client = new EC2Client({ region: this.region });
118
+ }
119
+ return this.ec2Client;
120
+ }
121
+
122
+ /**
123
+ * Check if a property mismatch can be auto-fixed
124
+ */
125
+ async canReconcile(mismatch) {
126
+ // Immutable properties cannot be reconciled (require replacement)
127
+ if (mismatch.requiresReplacement()) {
128
+ return false;
129
+ }
130
+
131
+ // Mutable and conditional properties can be reconciled
132
+ // Note: CONDITIONAL may require additional validation, but we treat it as reconcilable
133
+ return true;
134
+ }
135
+
136
+ /**
137
+ * Reconcile a single property mismatch
138
+ */
139
+ async reconcileProperty({ stackIdentifier, logicalId, mismatch, mode = 'template' }) {
140
+ if (mode === 'template') {
141
+ return await this._reconcileViaTemplate({
142
+ stackIdentifier,
143
+ logicalId,
144
+ mismatch,
145
+ });
146
+ } else {
147
+ return await this._reconcileViaResource({
148
+ stackIdentifier,
149
+ logicalId,
150
+ mismatch,
151
+ });
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Reconcile multiple property mismatches for a resource
157
+ */
158
+ async reconcileMultipleProperties({
159
+ stackIdentifier,
160
+ logicalId,
161
+ mismatches,
162
+ mode = 'template',
163
+ }) {
164
+ const results = [];
165
+ let reconciledCount = 0;
166
+ let failedCount = 0;
167
+
168
+ for (const mismatch of mismatches) {
169
+ try {
170
+ const result = await this.reconcileProperty({
171
+ stackIdentifier,
172
+ logicalId,
173
+ mismatch,
174
+ mode,
175
+ });
176
+
177
+ results.push(result);
178
+ if (result.success) {
179
+ reconciledCount++;
180
+ } else {
181
+ failedCount++;
182
+ }
183
+ } catch (error) {
184
+ results.push({
185
+ success: false,
186
+ mode,
187
+ propertyPath: mismatch.propertyPath,
188
+ message: error.message,
189
+ });
190
+ failedCount++;
191
+ }
192
+ }
193
+
194
+ return {
195
+ reconciledCount,
196
+ failedCount,
197
+ results,
198
+ message: `Reconciled ${reconciledCount} of ${mismatches.length} properties`,
199
+ };
200
+ }
201
+
202
+ /**
203
+ * Preview property reconciliation without applying changes
204
+ */
205
+ async previewReconciliation({ stackIdentifier, logicalId, mismatch, mode = 'template' }) {
206
+ const canReconcile = await this.canReconcile(mismatch);
207
+
208
+ const warnings = [];
209
+ if (mismatch.requiresReplacement()) {
210
+ warnings.push('Property is immutable - requires resource replacement');
211
+ }
212
+
213
+ let impact = '';
214
+ if (mode === 'template') {
215
+ impact = 'Will update CloudFormation template to match actual resource state';
216
+ } else {
217
+ impact = 'Will update cloud resource to match template definition';
218
+ }
219
+
220
+ return {
221
+ canReconcile,
222
+ mode,
223
+ propertyPath: mismatch.propertyPath,
224
+ currentValue: mismatch.expectedValue,
225
+ proposedValue: mismatch.actualValue,
226
+ impact,
227
+ warnings,
228
+ };
229
+ }
230
+
231
+ /**
232
+ * Update CloudFormation template property
233
+ */
234
+ async updateTemplateProperty({ stackIdentifier, logicalId, propertyPath, newValue }) {
235
+ const client = this._getCFClient();
236
+
237
+ // Get current template
238
+ const getTemplateCommand = new GetTemplateCommand({
239
+ StackName: stackIdentifier.stackName,
240
+ TemplateStage: 'Original',
241
+ });
242
+
243
+ const templateResponse = await client.send(getTemplateCommand);
244
+ const template = JSON.parse(templateResponse.TemplateBody);
245
+
246
+ // Update property in template
247
+ const pathParts = propertyPath.split('.');
248
+ let current = template.Resources[logicalId];
249
+
250
+ for (let i = 0; i < pathParts.length - 1; i++) {
251
+ if (!current[pathParts[i]]) {
252
+ current[pathParts[i]] = {};
253
+ }
254
+ current = current[pathParts[i]];
255
+ }
256
+
257
+ const lastPart = pathParts[pathParts.length - 1];
258
+ current[lastPart] = newValue;
259
+
260
+ // Update stack with new template
261
+ const updateCommand = new UpdateStackCommand({
262
+ StackName: stackIdentifier.stackName,
263
+ TemplateBody: JSON.stringify(template),
264
+ });
265
+
266
+ const updateResponse = await client.send(updateCommand);
267
+
268
+ return {
269
+ success: true,
270
+ changeSetId: updateResponse.StackId,
271
+ message: 'Template property updated successfully',
272
+ };
273
+ }
274
+
275
+ /**
276
+ * Update cloud resource property directly
277
+ */
278
+ async updateResourceProperty({ resourceType, physicalId, region, propertyPath, newValue }) {
279
+ // Only VPC properties are supported for direct resource updates in this implementation
280
+ if (resourceType === 'AWS::EC2::VPC') {
281
+ return await this._updateVpcProperty({ physicalId, propertyPath, newValue });
282
+ }
283
+
284
+ throw new Error(`Resource type ${resourceType} updates not supported`);
285
+ }
286
+
287
+ /**
288
+ * Get reconciliation strategy for a resource type
289
+ */
290
+ async getReconciliationStrategy(resourceType) {
291
+ if (!(resourceType in AWSPropertyReconciler.SUPPORTED_TYPES)) {
292
+ throw new Error(`Resource type ${resourceType} not supported`);
293
+ }
294
+
295
+ const config = AWSPropertyReconciler.SUPPORTED_TYPES[resourceType];
296
+
297
+ return {
298
+ supportsTemplateUpdate: config.templateUpdate,
299
+ supportsResourceUpdate: config.resourceUpdate,
300
+ recommendedMode: config.recommendedMode,
301
+ limitations: config.limitations,
302
+ };
303
+ }
304
+
305
+ // ========================================
306
+ // Private Helper Methods
307
+ // ========================================
308
+
309
+ /**
310
+ * Reconcile via template update
311
+ * @private
312
+ */
313
+ async _reconcileViaTemplate({ stackIdentifier, logicalId, mismatch }) {
314
+ await this.updateTemplateProperty({
315
+ stackIdentifier,
316
+ logicalId,
317
+ propertyPath: mismatch.propertyPath,
318
+ newValue: mismatch.actualValue,
319
+ });
320
+
321
+ return {
322
+ success: true,
323
+ mode: 'template',
324
+ propertyPath: mismatch.propertyPath,
325
+ oldValue: mismatch.expectedValue,
326
+ newValue: mismatch.actualValue,
327
+ message: 'Template updated to match actual resource state',
328
+ };
329
+ }
330
+
331
+ /**
332
+ * Reconcile via resource update
333
+ * @private
334
+ */
335
+ async _reconcileViaResource({ stackIdentifier, logicalId, mismatch }) {
336
+ // This is a simplified implementation
337
+ // In production, would need resource type detection and proper API calls
338
+
339
+ // For now, only support VPC properties
340
+ if (!mismatch.propertyPath.includes('EnableDns')) {
341
+ throw new Error('Resource property update not supported for this property');
342
+ }
343
+
344
+ // Mock resource update (in real implementation, would use EC2 API)
345
+ const client = this._getEC2Client();
346
+ const command = new ModifyVpcAttributeCommand({
347
+ VpcId: 'vpc-placeholder',
348
+ EnableDnsSupport: { Value: mismatch.expectedValue },
349
+ });
350
+
351
+ await client.send(command);
352
+
353
+ return {
354
+ success: true,
355
+ mode: 'resource',
356
+ propertyPath: mismatch.propertyPath,
357
+ oldValue: mismatch.actualValue,
358
+ newValue: mismatch.expectedValue,
359
+ message: 'Resource updated to match template definition',
360
+ };
361
+ }
362
+
363
+ /**
364
+ * Update VPC property directly
365
+ * @private
366
+ */
367
+ async _updateVpcProperty({ physicalId, propertyPath, newValue }) {
368
+ const client = this._getEC2Client();
369
+
370
+ // Map property paths to VPC attribute names
371
+ if (propertyPath === 'Properties.EnableDnsSupport') {
372
+ const command = new ModifyVpcAttributeCommand({
373
+ VpcId: physicalId,
374
+ EnableDnsSupport: { Value: newValue },
375
+ });
376
+
377
+ await client.send(command);
378
+ } else if (propertyPath === 'Properties.EnableDnsHostnames') {
379
+ const command = new ModifyVpcAttributeCommand({
380
+ VpcId: physicalId,
381
+ EnableDnsHostnames: { Value: newValue },
382
+ });
383
+
384
+ await client.send(command);
385
+ } else {
386
+ throw new Error(`Property ${propertyPath} cannot be updated directly`);
387
+ }
388
+
389
+ return {
390
+ success: true,
391
+ message: `VPC property ${propertyPath} updated successfully`,
392
+ updatedAt: new Date(),
393
+ };
394
+ }
395
+ }
396
+
397
+ module.exports = AWSPropertyReconciler;