forked from aws/aws-cdk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-formatter.ts
More file actions
420 lines (371 loc) · 12.2 KB
/
diff-formatter.ts
File metadata and controls
420 lines (371 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import { format } from 'node:util';
import type * as cxapi from '@aws-cdk/cloud-assembly-api';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import {
formatDifferences,
formatSecurityChanges,
fullDiff,
mangleLikeCloudFormation,
type ResourceDifference,
type TemplateDiff,
} from '@aws-cdk/cloudformation-diff';
import * as chalk from 'chalk';
import { PermissionChangeType } from '../../payloads';
import type { NestedStackTemplates } from '../cloudformation';
import { StringWriteStream } from '../streams';
/**
* Output of formatSecurityDiff
*/
interface FormatSecurityDiffOutput {
/**
* Complete formatted security diff
*/
readonly formattedDiff: string;
/**
* The type of permission changes in the security diff.
* The IoHost will use this to decide whether or not to print.
*/
readonly permissionChangeType: PermissionChangeType;
}
/**
* Output of formatStackDiff
*/
interface FormatStackDiffOutput {
/**
* Number of stacks with diff changes
*/
readonly numStacksWithChanges: number;
/**
* Complete formatted diff
*/
readonly formattedDiff: string;
/**
* The type of permission changes in the stack diff.
* The IoHost will use this to decide whether or not to print.
*/
readonly permissionChangeType: PermissionChangeType;
}
/**
* Props for the Diff Formatter
*/
interface DiffFormatterProps {
/**
* The relevant information for the Template that is being diffed.
* Includes the old/current state of the stack as well as the new state.
*/
readonly templateInfo: TemplateInfo;
}
/**
* Properties specific to formatting the stack diff
*/
interface FormatStackDiffOptions {
/**
* do not filter out AWS::CDK::Metadata or Rules
*
* @default false
*/
readonly strict?: boolean;
/**
* lines of context to use in arbitrary JSON diff
*
* @default 3
*/
readonly contextLines?: number;
/**
* silences \'There were no differences\' messages
*
* @default false
*/
readonly quiet?: boolean;
}
interface ReusableStackDiffOptions extends FormatStackDiffOptions {
}
/**
* Information on a template's old/new state
* that is used for diff.
*/
export interface TemplateInfo {
/**
* The old/existing template
*/
readonly oldTemplate: any;
/**
* The new template
*/
readonly newTemplate: cxapi.CloudFormationStackArtifact;
/**
* A CloudFormation ChangeSet to help the diff operation.
* Probably created via `createDiffChangeSet`.
*
* @default undefined
*/
readonly changeSet?: any;
/**
* Whether or not there are any imported resources
*
* @default false
*/
readonly isImport?: boolean;
/**
* Any nested stacks included in the template
*
* @default {}
*/
readonly nestedStacks?: {
[nestedStackLogicalId: string]: NestedStackTemplates;
};
/**
* Mappings of old locations to new locations. If these are provided,
* for all resources that were moved, their corresponding addition
* and removal lines will be augmented with the location they were
* moved fom and to, respectively.
*/
readonly mappings?: Record<string, string>;
}
/**
* Class for formatting the diff output
*/
export class DiffFormatter {
private readonly oldTemplate: any;
private readonly newTemplate: cxapi.CloudFormationStackArtifact;
private readonly stackName: string;
private readonly changeSet?: any;
private readonly nestedStacks: { [nestedStackLogicalId: string]: NestedStackTemplates } | undefined;
private readonly isImport: boolean;
private readonly mappings: Record<string, string>;
/**
* Stores the TemplateDiffs that get calculated in this DiffFormatter,
* indexed by the stack name.
*/
private _diffs: { [name: string]: TemplateDiff } = {};
constructor(props: DiffFormatterProps) {
this.oldTemplate = props.templateInfo.oldTemplate;
this.newTemplate = props.templateInfo.newTemplate;
this.stackName = props.templateInfo.newTemplate.displayName ?? props.templateInfo.newTemplate.stackName;
this.changeSet = props.templateInfo.changeSet;
this.nestedStacks = props.templateInfo.nestedStacks;
this.isImport = props.templateInfo.isImport ?? false;
this.mappings = props.templateInfo.mappings ?? {};
}
public get diffs() {
return this._diffs;
}
/**
* Get or creates the diff of a stack.
* If it creates the diff, it stores the result in a map for
* easier retrieval later.
*/
private diff(stackName?: string, oldTemplate?: any, mappings: Record<string, string> = {}) {
const realStackName = stackName ?? this.stackName;
if (!this._diffs[realStackName]) {
const templateDiff = fullDiff(
oldTemplate ?? this.oldTemplate,
this.newTemplate.template,
this.changeSet,
this.isImport,
);
const setMove = (change: ResourceDifference, direction: 'from' | 'to', location?: string)=> {
if (location != null) {
const [sourceStackName, sourceLogicalId] = location.split('.');
change.move = {
direction,
stackName: sourceStackName,
resourceLogicalId: sourceLogicalId,
};
}
};
templateDiff.resources.forEachDifference((id, change) => {
const location = `${realStackName}.${id}`;
if (change.isAddition && Object.values(mappings).includes(location)) {
setMove(change, 'from', Object.keys(mappings).find(k => mappings[k] === location));
} else if (change.isRemoval && Object.keys(mappings).includes(location)) {
setMove(change, 'to', mappings[location]);
}
});
this._diffs[realStackName] = templateDiff;
}
return this._diffs[realStackName];
}
/**
* Return whether the diff has security-impacting changes that need confirmation.
*
* If no stackName is given, then the root stack name is used.
*/
private permissionType(): PermissionChangeType {
const diff = this.diff();
if (diff.permissionsBroadened) {
return PermissionChangeType.BROADENING;
} else if (diff.permissionsAnyChanges) {
return PermissionChangeType.NON_BROADENING;
} else {
return PermissionChangeType.NONE;
}
}
/**
* Format the stack diff
*/
public formatStackDiff(options: FormatStackDiffOptions = {}): FormatStackDiffOutput {
return this.formatStackDiffHelper(
this.oldTemplate,
this.stackName,
this.nestedStacks,
options,
this.mappings,
);
}
private formatStackDiffHelper(
oldTemplate: any,
stackName: string,
nestedStackTemplates: { [nestedStackLogicalId: string]: NestedStackTemplates } | undefined,
options: ReusableStackDiffOptions,
mappings: Record<string, string> = {},
) {
let diff = this.diff(stackName, oldTemplate, mappings);
// The stack diff is formatted via `Formatter`, which takes in a stream
// and sends its output directly to that stream. To facilitate use of the
// global CliIoHost, we create our own stream to capture the output of
// `Formatter` and return the output as a string for the consumer of
// `formatStackDiff` to decide what to do with it.
const stream = new StringWriteStream();
let numStacksWithChanges = 0;
let formattedDiff = '';
let filteredChangesCount = 0;
try {
// must output the stack name if there are differences, even if quiet
if (stackName && (!options.quiet || !diff.isEmpty)) {
stream.write(format(`Stack ${chalk.bold(stackName)}\n`));
}
if (!options.quiet && this.isImport) {
stream.write('Parameters and rules created during migration do not affect resource configuration.\n');
}
// detect and filter out mangled characters from the diff
if (diff.differenceCount && !options.strict) {
const mangledNewTemplate = JSON.parse(mangleLikeCloudFormation(JSON.stringify(this.newTemplate.template)));
const mangledDiff = fullDiff(this.oldTemplate, mangledNewTemplate, this.changeSet);
filteredChangesCount = Math.max(0, diff.differenceCount - mangledDiff.differenceCount);
if (filteredChangesCount > 0) {
diff = mangledDiff;
}
}
// filter out 'AWS::CDK::Metadata' resources from the template
// filter out 'CheckBootstrapVersion' rules from the template
if (!options.strict) {
obscureDiff(diff);
}
if (!diff.isEmpty) {
numStacksWithChanges++;
// formatDifferences updates the stream with the formatted stack diff
formatDifferences(stream, diff, {
...logicalIdMapFromTemplate(this.oldTemplate),
...buildLogicalToPathMap(this.newTemplate),
}, options.contextLines);
} else if (!options.quiet) {
stream.write(chalk.green('There were no differences\n'));
}
if (filteredChangesCount > 0) {
stream.write(chalk.yellow(`Omitted ${filteredChangesCount} changes because they are likely mangled non-ASCII characters. Use --strict to print them.\n`));
}
} finally {
// store the stream containing a formatted stack diff
formattedDiff = stream.toString();
stream.end();
}
for (const nestedStackLogicalId of Object.keys(nestedStackTemplates ?? {})) {
if (!nestedStackTemplates) {
break;
}
const nestedStack = nestedStackTemplates[nestedStackLogicalId];
(this.newTemplate as any)._template = nestedStack.generatedTemplate;
const nextDiff = this.formatStackDiffHelper(
nestedStack.deployedTemplate,
nestedStack.physicalName ?? nestedStackLogicalId,
nestedStack.nestedStackTemplates,
options,
this.mappings,
);
numStacksWithChanges += nextDiff.numStacksWithChanges;
formattedDiff += nextDiff.formattedDiff;
}
return {
numStacksWithChanges,
formattedDiff,
permissionChangeType: this.permissionType(),
};
}
/**
* Format the security diff
*/
public formatSecurityDiff(): FormatSecurityDiffOutput {
const diff = this.diff();
// The security diff is formatted via `Formatter`, which takes in a stream
// and sends its output directly to that stream. To faciliate use of the
// global CliIoHost, we create our own stream to capture the output of
// `Formatter` and return the output as a string for the consumer of
// `formatSecurityDiff` to decide what to do with it.
const stream = new StringWriteStream();
stream.write(format(`Stack ${chalk.bold(this.stackName)}\n`));
try {
// formatSecurityChanges updates the stream with the formatted security diff
formatSecurityChanges(stream, diff, buildLogicalToPathMap(this.newTemplate));
} finally {
stream.end();
}
// store the stream containing a formatted stack diff
const formattedDiff = stream.toString();
return { formattedDiff, permissionChangeType: this.permissionType() };
}
}
function buildLogicalToPathMap(stack: cxapi.CloudFormationStackArtifact) {
const map: { [id: string]: string } = {};
for (const md of stack.findMetadataByType(cxschema.ArtifactMetadataEntryType.LOGICAL_ID)) {
map[md.data as string] = md.path;
}
return map;
}
function logicalIdMapFromTemplate(template: any) {
const ret: Record<string, string> = {};
for (const [logicalId, resource] of Object.entries(template.Resources ?? {})) {
const path = (resource as any)?.Metadata?.['aws:cdk:path'];
if (path) {
ret[logicalId] = path;
}
}
return ret;
}
/**
* Remove any template elements that we don't want to show users.
* This is currently:
* - AWS::CDK::Metadata resource
* - CheckBootstrapVersion Rule
*/
function obscureDiff(diff: TemplateDiff) {
if (diff.unknown) {
// see https://github.com/aws/aws-cdk/issues/17942
diff.unknown = diff.unknown.filter(change => {
if (!change) {
return true;
}
if (change.newValue?.CheckBootstrapVersion) {
return false;
}
if (change.oldValue?.CheckBootstrapVersion) {
return false;
}
return true;
});
}
if (diff.resources) {
diff.resources = diff.resources.filter(change => {
if (!change) {
return true;
}
if (change.newResourceType === 'AWS::CDK::Metadata') {
return false;
}
if (change.oldResourceType === 'AWS::CDK::Metadata') {
return false;
}
return true;
});
}
}