/
types.ts
1057 lines (899 loc) · 35.4 KB
/
types.ts
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { AssertionError } from 'assert';
import * as cfnspec from '@aws-cdk/cfnspec';
import * as chalk from 'chalk';
import { DiffableCollection } from '../diffable';
import { ManagedPolicyAttachment, ManagedPolicyJson } from '../iam/managed-policy';
import { parseLambdaPermission, parseStatements, Statement, StatementJson } from '../iam/statement';
import { RuleJson, SecurityGroupRule } from '../network/security-group-rule';
import { renderIntrinsics } from '../render-intrinsics';
import { deepRemoveUndefined, dropIfEmpty, flatMap, makeComparator } from '../util';
import { deepEqual } from './util';
export type PropertyMap = {[key: string]: any };
/** Semantic differences between two CloudFormation templates. */
export class TemplateDiff implements ITemplateDiff {
public awsTemplateFormatVersion?: Difference<string>;
public description?: Difference<string>;
public transform?: Difference<string>;
public conditions: DifferenceCollection<Condition, ConditionDifference>;
public mappings: DifferenceCollection<Mapping, MappingDifference>;
public metadata: DifferenceCollection<Metadata, MetadataDifference>;
public outputs: DifferenceCollection<Output, OutputDifference>;
public parameters: DifferenceCollection<Parameter, ParameterDifference>;
public resources: DifferenceCollection<Resource, ResourceDifference>;
/** The differences in unknown/unexpected parts of the template */
public unknown: DifferenceCollection<any, Difference<any>>;
/**
* Changes to IAM policies
*/
public readonly iamChanges: IamChanges;
/**
* Changes to Security Group ingress and egress rules
*/
public readonly securityGroupChanges: SecurityGroupChanges;
constructor(args: ITemplateDiff) {
if (args.awsTemplateFormatVersion !== undefined) {
this.awsTemplateFormatVersion = args.awsTemplateFormatVersion;
}
if (args.description !== undefined) {
this.description = args.description;
}
if (args.transform !== undefined) {
this.transform = args.transform;
}
this.conditions = args.conditions || new DifferenceCollection({});
this.mappings = args.mappings || new DifferenceCollection({});
this.metadata = args.metadata || new DifferenceCollection({});
this.outputs = args.outputs || new DifferenceCollection({});
this.parameters = args.parameters || new DifferenceCollection({});
this.resources = args.resources || new DifferenceCollection({});
this.unknown = args.unknown || new DifferenceCollection({});
this.iamChanges = new IamChanges({
propertyChanges: this.scrutinizablePropertyChanges(IamChanges.IamPropertyScrutinies),
resourceChanges: this.scrutinizableResourceChanges(IamChanges.IamResourceScrutinies),
});
this.securityGroupChanges = new SecurityGroupChanges({
egressRulePropertyChanges: this.scrutinizablePropertyChanges([cfnspec.schema.PropertyScrutinyType.EgressRules]),
ingressRulePropertyChanges: this.scrutinizablePropertyChanges([cfnspec.schema.PropertyScrutinyType.IngressRules]),
egressRuleResourceChanges: this.scrutinizableResourceChanges([cfnspec.schema.ResourceScrutinyType.EgressRuleResource]),
ingressRuleResourceChanges: this.scrutinizableResourceChanges([cfnspec.schema.ResourceScrutinyType.IngressRuleResource]),
});
}
public get differenceCount() {
let count = 0;
if (this.awsTemplateFormatVersion !== undefined) {
count += 1;
}
if (this.description !== undefined) {
count += 1;
}
if (this.transform !== undefined) {
count += 1;
}
count += this.conditions.differenceCount;
count += this.mappings.differenceCount;
count += this.metadata.differenceCount;
count += this.outputs.differenceCount;
count += this.parameters.differenceCount;
count += this.resources.differenceCount;
count += this.unknown.differenceCount;
return count;
}
public get isEmpty(): boolean {
return this.differenceCount === 0;
}
/**
* Return true if any of the permissions objects involve a broadening of permissions
*/
public get permissionsBroadened(): boolean {
return this.iamChanges.permissionsBroadened || this.securityGroupChanges.rulesAdded;
}
/**
* Return true if any of the permissions objects have changed
*/
public get permissionsAnyChanges(): boolean {
return this.iamChanges.hasChanges || this.securityGroupChanges.hasChanges;
}
/**
* Return all property changes of a given scrutiny type
*
* We don't just look at property updates; we also look at resource additions and deletions (in which
* case there is no further detail on property values), and resource type changes.
*/
private scrutinizablePropertyChanges(scrutinyTypes: cfnspec.schema.PropertyScrutinyType[]): PropertyChange[] {
const ret = new Array<PropertyChange>();
for (const [resourceLogicalId, resourceChange] of Object.entries(this.resources.changes)) {
if (!resourceChange) { continue; }
const props = cfnspec.scrutinizablePropertyNames(resourceChange.newResourceType!, scrutinyTypes);
for (const propertyName of props) {
ret.push({
resourceLogicalId,
propertyName,
resourceType: resourceChange.resourceType,
scrutinyType: cfnspec.propertySpecification(resourceChange.resourceType, propertyName).ScrutinyType!,
oldValue: resourceChange.oldProperties && resourceChange.oldProperties[propertyName],
newValue: resourceChange.newProperties && resourceChange.newProperties[propertyName],
});
}
}
return ret;
}
/**
* Return all resource changes of a given scrutiny type
*
* We don't just look at resource updates; we also look at resource additions and deletions (in which
* case there is no further detail on property values), and resource type changes.
*/
private scrutinizableResourceChanges(scrutinyTypes: cfnspec.schema.ResourceScrutinyType[]): ResourceChange[] {
const ret = new Array<ResourceChange>();
const scrutinizableTypes = new Set(cfnspec.scrutinizableResourceTypes(scrutinyTypes));
for (const [resourceLogicalId, resourceChange] of Object.entries(this.resources.changes)) {
if (!resourceChange) { continue; }
const commonProps = {
oldProperties: resourceChange.oldProperties,
newProperties: resourceChange.newProperties,
resourceLogicalId,
};
// Even though it's not physically possible in CFN, let's pretend to handle a change of 'Type'.
if (resourceChange.resourceTypeChanged) {
// Treat as DELETE+ADD
if (scrutinizableTypes.has(resourceChange.oldResourceType!)) {
ret.push({
...commonProps,
newProperties: undefined,
resourceType: resourceChange.oldResourceType!,
scrutinyType: cfnspec.resourceSpecification(resourceChange.oldResourceType!).ScrutinyType!,
});
}
if (scrutinizableTypes.has(resourceChange.newResourceType!)) {
ret.push({
...commonProps,
oldProperties: undefined,
resourceType: resourceChange.newResourceType!,
scrutinyType: cfnspec.resourceSpecification(resourceChange.newResourceType!).ScrutinyType!,
});
}
} else {
if (scrutinizableTypes.has(resourceChange.resourceType)) {
ret.push({
...commonProps,
resourceType: resourceChange.resourceType,
scrutinyType: cfnspec.resourceSpecification(resourceChange.resourceType).ScrutinyType!,
});
}
}
}
return ret;
}
}
/**
* A change in property values
*
* Not necessarily an update, it could be that there used to be no value there
* because there was no resource, and now there is (or vice versa).
*
* Therefore, we just contain plain values and not a PropertyDifference<any>.
*/
export interface PropertyChange {
/**
* Logical ID of the resource where this property change was found
*/
resourceLogicalId: string;
/**
* Type of the resource
*/
resourceType: string;
/**
* Scrutiny type for this property change
*/
scrutinyType: cfnspec.schema.PropertyScrutinyType;
/**
* Name of the property that is changing
*/
propertyName: string;
/**
* The old property value
*/
oldValue?: any;
/**
* The new property value
*/
newValue?: any;
}
/**
* A resource change
*
* Either a creation, deletion or update.
*/
export interface ResourceChange {
/**
* Logical ID of the resource where this property change was found
*/
resourceLogicalId: string;
/**
* Scrutiny type for this resource change
*/
scrutinyType: cfnspec.schema.ResourceScrutinyType;
/**
* The type of the resource
*/
resourceType: string;
/**
* The old properties value (might be undefined in case of creation)
*/
oldProperties?: PropertyMap;
/**
* The new properties value (might be undefined in case of deletion)
*/
newProperties?: PropertyMap;
}
export interface IDifference<ValueType> {
readonly oldValue: ValueType | undefined;
readonly newValue: ValueType | undefined;
readonly isDifferent: boolean;
readonly isAddition: boolean;
readonly isRemoval: boolean;
readonly isUpdate: boolean;
}
/**
* Models an entity that changed between two versions of a CloudFormation template.
*/
export class Difference<ValueType> implements IDifference<ValueType> {
/**
* Whether this is an actual different or the values are actually the same
*
* isDifferent => (isUpdate | isRemoved | isUpdate)
*/
public readonly isDifferent: boolean;
/**
* @param oldValue the old value, cannot be equal (to the sense of +deepEqual+) to +newValue+.
* @param newValue the new value, cannot be equal (to the sense of +deepEqual+) to +oldValue+.
*/
constructor(public readonly oldValue: ValueType | undefined, public readonly newValue: ValueType | undefined) {
if (oldValue === undefined && newValue === undefined) {
throw new AssertionError({ message: 'oldValue and newValue are both undefined!' });
}
this.isDifferent = !deepEqual(oldValue, newValue);
}
/** @returns +true+ if the element is new to the template. */
public get isAddition(): boolean {
return this.oldValue === undefined;
}
/** @returns +true+ if the element was removed from the template. */
public get isRemoval(): boolean {
return this.newValue === undefined;
}
/** @returns +true+ if the element was already in the template and is updated. */
public get isUpdate(): boolean {
return this.oldValue !== undefined
&& this.newValue !== undefined;
}
}
export class PropertyDifference<ValueType> extends Difference<ValueType> {
public readonly changeImpact?: ResourceImpact;
constructor(oldValue: ValueType | undefined, newValue: ValueType | undefined, args: { changeImpact?: ResourceImpact }) {
super(oldValue, newValue);
this.changeImpact = args.changeImpact;
}
}
export class DifferenceCollection<V, T extends IDifference<V>> {
constructor(private readonly diffs: { [logicalId: string]: T }) {}
public get changes(): { [logicalId: string]: T } {
return onlyChanges(this.diffs);
}
public get differenceCount(): number {
return Object.values(this.changes).length;
}
public get(logicalId: string): T {
const ret = this.diffs[logicalId];
if (!ret) { throw new Error(`No object with logical ID '${logicalId}'`); }
return ret;
}
public get logicalIds(): string[] {
return Object.keys(this.changes);
}
/**
* Returns a new TemplateDiff which only contains changes for which `predicate`
* returns `true`.
*/
public filter(predicate: (diff: T | undefined) => boolean): DifferenceCollection<V, T> {
const newChanges: { [logicalId: string]: T } = { };
for (const id of Object.keys(this.changes)) {
const diff = this.changes[id];
if (predicate(diff)) {
newChanges[id] = diff;
}
}
return new DifferenceCollection<V, T>(newChanges);
}
/**
* Invokes `cb` for all changes in this collection.
*
* Changes will be sorted as follows:
* - Removed
* - Added
* - Updated
* - Others
*
* @param cb
*/
public forEachDifference(cb: (logicalId: string, change: T) => any): void {
const removed = new Array<{ logicalId: string, change: T }>();
const added = new Array<{ logicalId: string, change: T }>();
const updated = new Array<{ logicalId: string, change: T }>();
const others = new Array<{ logicalId: string, change: T }>();
for (const logicalId of this.logicalIds) {
const change: T = this.changes[logicalId]!;
if (change.isAddition) {
added.push({ logicalId, change });
} else if (change.isRemoval) {
removed.push({ logicalId, change });
} else if (change.isUpdate) {
updated.push({ logicalId, change });
} else if (change.isDifferent) {
others.push({ logicalId, change });
}
}
removed.forEach(v => cb(v.logicalId, v.change));
added.forEach(v => cb(v.logicalId, v.change));
updated.forEach(v => cb(v.logicalId, v.change));
others.forEach(v => cb(v.logicalId, v.change));
}
}
/**
* Arguments expected by the constructor of +TemplateDiff+, extracted as an interface for the sake
* of (relative) conciseness of the constructor's signature.
*/
export interface ITemplateDiff {
awsTemplateFormatVersion?: IDifference<string>;
description?: IDifference<string>;
transform?: IDifference<string>;
conditions?: DifferenceCollection<Condition, ConditionDifference>;
mappings?: DifferenceCollection<Mapping, MappingDifference>;
metadata?: DifferenceCollection<Metadata, MetadataDifference>;
outputs?: DifferenceCollection<Output, OutputDifference>;
parameters?: DifferenceCollection<Parameter, ParameterDifference>;
resources?: DifferenceCollection<Resource, ResourceDifference>;
unknown?: DifferenceCollection<any, IDifference<any>>;
}
export type Condition = any;
export class ConditionDifference extends Difference<Condition> {
// TODO: define specific difference attributes
}
export type Mapping = any;
export class MappingDifference extends Difference<Mapping> {
// TODO: define specific difference attributes
}
export type Metadata = any;
export class MetadataDifference extends Difference<Metadata> {
// TODO: define specific difference attributes
}
export type Output = any;
export class OutputDifference extends Difference<Output> {
// TODO: define specific difference attributes
}
export type Parameter = any;
export class ParameterDifference extends Difference<Parameter> {
// TODO: define specific difference attributes
}
export enum ResourceImpact {
/** The existing physical resource will be updated */
WILL_UPDATE = 'WILL_UPDATE',
/** A new physical resource will be created */
WILL_CREATE = 'WILL_CREATE',
/** The existing physical resource will be replaced */
WILL_REPLACE = 'WILL_REPLACE',
/** The existing physical resource may be replaced */
MAY_REPLACE = 'MAY_REPLACE',
/** The existing physical resource will be destroyed */
WILL_DESTROY = 'WILL_DESTROY',
/** The existing physical resource will be removed from CloudFormation supervision */
WILL_ORPHAN = 'WILL_ORPHAN',
/** There is no change in this resource */
NO_CHANGE = 'NO_CHANGE',
}
/**
* This function can be used as a reducer to obtain the resource-level impact of a list
* of property-level impacts.
* @param one the current worst impact so far.
* @param two the new impact being considered (can be undefined, as we may not always be
* able to determine some peroperty's impact).
*/
function worstImpact(one: ResourceImpact, two?: ResourceImpact): ResourceImpact {
if (!two) { return one; }
const badness = {
[ResourceImpact.NO_CHANGE]: 0,
[ResourceImpact.WILL_UPDATE]: 1,
[ResourceImpact.WILL_CREATE]: 2,
[ResourceImpact.WILL_ORPHAN]: 3,
[ResourceImpact.MAY_REPLACE]: 4,
[ResourceImpact.WILL_REPLACE]: 5,
[ResourceImpact.WILL_DESTROY]: 6,
};
return badness[one] > badness[two] ? one : two;
}
export interface Resource {
Type: string;
Properties?: { [name: string]: any };
[key: string]: any;
}
/**
* Change to a single resource between two CloudFormation templates
*
* This class can be mutated after construction.
*/
export class ResourceDifference implements IDifference<Resource> {
/**
* Whether this resource was added
*/
public readonly isAddition: boolean;
/**
* Whether this resource was removed
*/
public readonly isRemoval: boolean;
/** Property-level changes on the resource */
private readonly propertyDiffs: { [key: string]: PropertyDifference<any> };
/** Changes to non-property level attributes of the resource */
private readonly otherDiffs: { [key: string]: Difference<any> };
/** The resource type (or old and new type if it has changed) */
private readonly resourceTypes: { readonly oldType?: string, readonly newType?: string };
constructor(
public readonly oldValue: Resource | undefined,
public readonly newValue: Resource | undefined,
args: {
resourceType: { oldType?: string, newType?: string },
propertyDiffs: { [key: string]: PropertyDifference<any> },
otherDiffs: { [key: string]: Difference<any> }
},
) {
this.resourceTypes = args.resourceType;
this.propertyDiffs = args.propertyDiffs;
this.otherDiffs = args.otherDiffs;
this.isAddition = oldValue === undefined;
this.isRemoval = newValue === undefined;
}
public get oldProperties(): PropertyMap | undefined {
return this.oldValue && this.oldValue.Properties;
}
public get newProperties(): PropertyMap | undefined {
return this.newValue && this.newValue.Properties;
}
/**
* Whether this resource was modified at all
*/
public get isDifferent(): boolean {
return this.differenceCount > 0 || this.oldResourceType !== this.newResourceType;
}
/**
* Whether the resource was updated in-place
*/
public get isUpdate(): boolean {
return this.isDifferent && !this.isAddition && !this.isRemoval;
}
public get oldResourceType(): string | undefined {
return this.resourceTypes.oldType;
}
public get newResourceType(): string | undefined {
return this.resourceTypes.newType;
}
/**
* All actual property updates
*/
public get propertyUpdates(): { [key: string]: PropertyDifference<any> } {
return onlyChanges(this.propertyDiffs);
}
/**
* All actual "other" updates
*/
public get otherChanges(): { [key: string]: Difference<any> } {
return onlyChanges(this.otherDiffs);
}
/**
* Return whether the resource type was changed in this diff
*
* This is not a valid operation in CloudFormation but to be defensive we're going
* to be aware of it anyway.
*/
public get resourceTypeChanged(): boolean {
return (this.resourceTypes.oldType !== undefined
&& this.resourceTypes.newType !== undefined
&& this.resourceTypes.oldType !== this.resourceTypes.newType);
}
/**
* Return the resource type if it was unchanged
*
* If the resource type was changed, it's an error to call this.
*/
public get resourceType(): string {
if (this.resourceTypeChanged) {
throw new Error('Cannot get .resourceType, because the type was changed');
}
return this.resourceTypes.oldType || this.resourceTypes.newType!;
}
/**
* Replace a PropertyChange in this object
*
* This affects the property diff as it is summarized to users, but it DOES
* NOT affect either the "oldValue" or "newValue" values; those still contain
* the actual template values as provided by the user (they might still be
* used for downstream processing).
*/
public setPropertyChange(propertyName: string, change: PropertyDifference<any>) {
this.propertyDiffs[propertyName] = change;
}
public get changeImpact(): ResourceImpact {
// Check the Type first
if (this.resourceTypes.oldType !== this.resourceTypes.newType) {
if (this.resourceTypes.oldType === undefined) { return ResourceImpact.WILL_CREATE; }
if (this.resourceTypes.newType === undefined) {
return this.oldValue!.DeletionPolicy === 'Retain'
? ResourceImpact.WILL_ORPHAN
: ResourceImpact.WILL_DESTROY;
}
return ResourceImpact.WILL_REPLACE;
}
// Base impact (before we mix in the worst of the property impacts);
// WILL_UPDATE if we have "other" changes, NO_CHANGE if there are no "other" changes.
const baseImpact = Object.keys(this.otherChanges).length > 0 ? ResourceImpact.WILL_UPDATE : ResourceImpact.NO_CHANGE;
return Object.values(this.propertyDiffs)
.map(elt => elt.changeImpact)
.reduce(worstImpact, baseImpact);
}
/**
* Count of actual differences (not of elements)
*/
public get differenceCount(): number {
return Object.values(this.propertyUpdates).length
+ Object.values(this.otherChanges).length;
}
/**
* Invoke a callback for each actual difference
*/
public forEachDifference(cb: (type: 'Property' | 'Other', name: string, value: Difference<any> | PropertyDifference<any>) => any) {
for (const key of Object.keys(this.propertyUpdates).sort()) {
cb('Property', key, this.propertyUpdates[key]);
}
for (const key of Object.keys(this.otherChanges).sort()) {
cb('Other', key, this.otherDiffs[key]);
}
}
}
export function isPropertyDifference<T>(diff: Difference<T>): diff is PropertyDifference<T> {
return (diff as PropertyDifference<T>).changeImpact !== undefined;
}
/**
* Filter a map of IDifferences down to only retain the actual changes
*/
function onlyChanges<V, T extends IDifference<V>>(xs: {[key: string]: T}): {[key: string]: T} {
const ret: { [key: string]: T } = {};
for (const [key, diff] of Object.entries(xs)) {
if (diff.isDifferent) {
ret[key] = diff;
}
}
return ret;
}
export interface IamChangesProps {
propertyChanges: PropertyChange[];
resourceChanges: ResourceChange[];
}
/**
* Changes to IAM statements
*/
export class IamChanges {
public static IamPropertyScrutinies = [
cfnspec.schema.PropertyScrutinyType.InlineIdentityPolicies,
cfnspec.schema.PropertyScrutinyType.InlineResourcePolicy,
cfnspec.schema.PropertyScrutinyType.ManagedPolicies,
];
public static IamResourceScrutinies = [
cfnspec.schema.ResourceScrutinyType.ResourcePolicyResource,
cfnspec.schema.ResourceScrutinyType.IdentityPolicyResource,
cfnspec.schema.ResourceScrutinyType.LambdaPermission,
];
public readonly statements = new DiffableCollection<Statement>();
public readonly managedPolicies = new DiffableCollection<ManagedPolicyAttachment>();
constructor(props: IamChangesProps) {
for (const propertyChange of props.propertyChanges) {
this.readPropertyChange(propertyChange);
}
for (const resourceChange of props.resourceChanges) {
this.readResourceChange(resourceChange);
}
this.statements.calculateDiff();
this.managedPolicies.calculateDiff();
}
public get hasChanges() {
return this.statements.hasChanges || this.managedPolicies.hasChanges;
}
/**
* Return whether the changes include broadened permissions
*
* Permissions are broadened if positive statements are added or
* negative statements are removed, or if managed policies are added.
*/
public get permissionsBroadened(): boolean {
return this.statements.additions.some(s => !s.isNegativeStatement)
|| this.statements.removals.some(s => s.isNegativeStatement)
|| this.managedPolicies.hasAdditions;
}
/**
* Return a summary table of changes
*/
public summarizeStatements(): string[][] {
const ret: string[][] = [];
const header = ['', 'Resource', 'Effect', 'Action', 'Principal', 'Condition'];
// First generate all lines, then sort on Resource so that similar resources are together
for (const statement of this.statements.additions) {
const renderedStatement = statement.render();
ret.push([
'+',
renderedStatement.resource,
renderedStatement.effect,
renderedStatement.action,
renderedStatement.principal,
renderedStatement.condition,
].map(s => chalk.green(s)));
}
for (const statement of this.statements.removals) {
const renderedStatement = statement.render();
ret.push([
chalk.red('-'),
renderedStatement.resource,
renderedStatement.effect,
renderedStatement.action,
renderedStatement.principal,
renderedStatement.condition,
].map(s => chalk.red(s)));
}
// Sort by 2nd column
ret.sort(makeComparator((row: string[]) => [row[1]]));
ret.splice(0, 0, header);
return ret;
}
public summarizeManagedPolicies(): string[][] {
const ret: string[][] = [];
const header = ['', 'Resource', 'Managed Policy ARN'];
for (const att of this.managedPolicies.additions) {
ret.push([
'+',
att.identityArn,
att.managedPolicyArn,
].map(s => chalk.green(s)));
}
for (const att of this.managedPolicies.removals) {
ret.push([
'-',
att.identityArn,
att.managedPolicyArn,
].map(s => chalk.red(s)));
}
// Sort by 2nd column
ret.sort(makeComparator((row: string[]) => [row[1]]));
ret.splice(0, 0, header);
return ret;
}
/**
* Return a machine-readable version of the changes.
* This is only used in tests.
*
* @internal
*/
public _toJson(): IamChangesJson {
return deepRemoveUndefined({
statementAdditions: dropIfEmpty(this.statements.additions.map(s => s._toJson())),
statementRemovals: dropIfEmpty(this.statements.removals.map(s => s._toJson())),
managedPolicyAdditions: dropIfEmpty(this.managedPolicies.additions.map(s => s._toJson())),
managedPolicyRemovals: dropIfEmpty(this.managedPolicies.removals.map(s => s._toJson())),
});
}
private readPropertyChange(propertyChange: PropertyChange) {
switch (propertyChange.scrutinyType) {
case cfnspec.schema.PropertyScrutinyType.InlineIdentityPolicies:
// AWS::IAM::{ Role | User | Group }.Policies
this.statements.addOld(...this.readIdentityPolicies(propertyChange.oldValue, propertyChange.resourceLogicalId));
this.statements.addNew(...this.readIdentityPolicies(propertyChange.newValue, propertyChange.resourceLogicalId));
break;
case cfnspec.schema.PropertyScrutinyType.InlineResourcePolicy:
// Any PolicyDocument on a resource (including AssumeRolePolicyDocument)
this.statements.addOld(...this.readResourceStatements(propertyChange.oldValue, propertyChange.resourceLogicalId));
this.statements.addNew(...this.readResourceStatements(propertyChange.newValue, propertyChange.resourceLogicalId));
break;
case cfnspec.schema.PropertyScrutinyType.ManagedPolicies:
// Just a list of managed policies
this.managedPolicies.addOld(...this.readManagedPolicies(propertyChange.oldValue, propertyChange.resourceLogicalId));
this.managedPolicies.addNew(...this.readManagedPolicies(propertyChange.newValue, propertyChange.resourceLogicalId));
break;
}
}
private readResourceChange(resourceChange: ResourceChange) {
switch (resourceChange.scrutinyType) {
case cfnspec.schema.ResourceScrutinyType.IdentityPolicyResource:
// AWS::IAM::Policy
this.statements.addOld(...this.readIdentityPolicyResource(resourceChange.oldProperties));
this.statements.addNew(...this.readIdentityPolicyResource(resourceChange.newProperties));
break;
case cfnspec.schema.ResourceScrutinyType.ResourcePolicyResource:
// AWS::*::{Bucket,Queue,Topic}Policy
this.statements.addOld(...this.readResourcePolicyResource(resourceChange.oldProperties));
this.statements.addNew(...this.readResourcePolicyResource(resourceChange.newProperties));
break;
case cfnspec.schema.ResourceScrutinyType.LambdaPermission:
this.statements.addOld(...this.readLambdaStatements(resourceChange.oldProperties));
this.statements.addNew(...this.readLambdaStatements(resourceChange.newProperties));
break;
}
}
/**
* Parse a list of policies on an identity
*/
private readIdentityPolicies(policies: any, logicalId: string): Statement[] {
if (policies === undefined) { return []; }
const appliesToPrincipal = 'AWS:${' + logicalId + '}';
return flatMap(policies, (policy: any) => {
// check if the Policy itself is not an intrinsic, like an Fn::If
const unparsedStatement = policy.PolicyDocument?.Statement
? policy.PolicyDocument.Statement
: policy;
return defaultPrincipal(appliesToPrincipal, parseStatements(renderIntrinsics(unparsedStatement)));
});
}
/**
* Parse an IAM::Policy resource
*/
private readIdentityPolicyResource(properties: any): Statement[] {
if (properties === undefined) { return []; }
properties = renderIntrinsics(properties);
const principals = (properties.Groups || []).concat(properties.Users || []).concat(properties.Roles || []);
return flatMap(principals, (principal: string) => {
const ref = 'AWS:' + principal;
return defaultPrincipal(ref, parseStatements(properties.PolicyDocument.Statement));
});
}
private readResourceStatements(policy: any, logicalId: string): Statement[] {
if (policy === undefined) { return []; }
const appliesToResource = '${' + logicalId + '.Arn}';
return defaultResource(appliesToResource, parseStatements(renderIntrinsics(policy.Statement)));
}
/**
* Parse an AWS::*::{Bucket,Topic,Queue}policy
*/
private readResourcePolicyResource(properties: any): Statement[] {
if (properties === undefined) { return []; }
properties = renderIntrinsics(properties);
const policyKeys = Object.keys(properties).filter(key => key.indexOf('Policy') > -1);
// Find the key that identifies the resource(s) this policy applies to
const resourceKeys = Object.keys(properties).filter(key => !policyKeys.includes(key) && !key.endsWith('Name'));
let resources = resourceKeys.length === 1 ? properties[resourceKeys[0]] : ['???'];
// For some resources, this is a singleton string, for some it's an array
if (!Array.isArray(resources)) {
resources = [resources];
}
return flatMap(resources, (resource: string) => {
return defaultResource(resource, parseStatements(properties[policyKeys[0]].Statement));
});
}
private readManagedPolicies(policyArns: any, logicalId: string): ManagedPolicyAttachment[] {
if (!policyArns) { return []; }
const rep = '${' + logicalId + '}';
return ManagedPolicyAttachment.parseManagedPolicies(rep, renderIntrinsics(policyArns));
}
private readLambdaStatements(properties?: PropertyMap): Statement[] {
if (!properties) { return []; }
return [parseLambdaPermission(renderIntrinsics(properties))];
}
}
/**
* Set an undefined or wildcarded principal on these statements
*/
function defaultPrincipal(principal: string, statements: Statement[]) {
statements.forEach(s => s.principals.replaceEmpty(principal));
statements.forEach(s => s.principals.replaceStar(principal));
return statements;
}
/**
* Set an undefined or wildcarded resource on these statements
*/
function defaultResource(resource: string, statements: Statement[]) {
statements.forEach(s => s.resources.replaceEmpty(resource));
statements.forEach(s => s.resources.replaceStar(resource));
return statements;
}
export interface IamChangesJson {
statementAdditions?: StatementJson[];
statementRemovals?: StatementJson[];
managedPolicyAdditions?: ManagedPolicyJson[];
managedPolicyRemovals?: ManagedPolicyJson[];
}
export interface SecurityGroupChangesProps {
ingressRulePropertyChanges: PropertyChange[];
ingressRuleResourceChanges: ResourceChange[];
egressRuleResourceChanges: ResourceChange[];
egressRulePropertyChanges: PropertyChange[];
}
/**
* Changes to IAM statements
*/
export class SecurityGroupChanges {
public readonly ingress = new DiffableCollection<SecurityGroupRule>();
public readonly egress = new DiffableCollection<SecurityGroupRule>();
constructor(props: SecurityGroupChangesProps) {
// Group rules
for (const ingressProp of props.ingressRulePropertyChanges) {
this.ingress.addOld(...this.readInlineRules(ingressProp.oldValue, ingressProp.resourceLogicalId));
this.ingress.addNew(...this.readInlineRules(ingressProp.newValue, ingressProp.resourceLogicalId));
}
for (const egressProp of props.egressRulePropertyChanges) {
this.egress.addOld(...this.readInlineRules(egressProp.oldValue, egressProp.resourceLogicalId));
this.egress.addNew(...this.readInlineRules(egressProp.newValue, egressProp.resourceLogicalId));
}
// Rule resources
for (const ingressRes of props.ingressRuleResourceChanges) {
this.ingress.addOld(...this.readRuleResource(ingressRes.oldProperties));
this.ingress.addNew(...this.readRuleResource(ingressRes.newProperties));
}
for (const egressRes of props.egressRuleResourceChanges) {
this.egress.addOld(...this.readRuleResource(egressRes.oldProperties));
this.egress.addNew(...this.readRuleResource(egressRes.newProperties));
}
this.ingress.calculateDiff();
this.egress.calculateDiff();
}
public get hasChanges() {
return this.ingress.hasChanges || this.egress.hasChanges;
}
/**
* Return a summary table of changes
*/
public summarize(): string[][] {
const ret: string[][] = [];
const header = ['', 'Group', 'Dir', 'Protocol', 'Peer'];
const inWord = 'In';
const outWord = 'Out';
// Render a single rule to the table (curried function so we can map it across rules easily--thank you JavaScript!)
const renderRule = (plusMin: string, inOut: string) => (rule: SecurityGroupRule) => [
plusMin,
rule.groupId,