|
1 | 1 | import * as path from 'path';
|
2 | 2 | import { Match, Template } from '../../assertions';
|
3 |
| -import { App, CfnResource, CustomResourceProvider, CustomResourceProviderRuntime, Stack } from '../../core'; |
| 3 | +import { App, AspectPriority, Aspects, CfnResource, CustomResourceProvider, CustomResourceProviderRuntime, Stack } from '../../core'; |
4 | 4 | import * as iam from '../lib';
|
5 | 5 |
|
6 | 6 | let app: App;
|
@@ -166,3 +166,44 @@ test('unapply inherited boundary from a user: order 2', () => {
|
166 | 166 | PermissionsBoundary: Match.absent(),
|
167 | 167 | });
|
168 | 168 | });
|
| 169 | + |
| 170 | +test.each([ |
| 171 | + [undefined, false, 'OVERRIDDEN'], |
| 172 | + [AspectPriority.MUTATING, false, 'OVERRIDDEN'], |
| 173 | + [AspectPriority.MUTATING, true, 'OVERRIDDEN'], |
| 174 | + // custom DEFAULT, builtin MUTATING: custom wins and override is not applied |
| 175 | + [undefined, true, 'BASE'], |
| 176 | +])('overriding works if base PB is applied using Aspect with prio %p (feature flag %p)', (basePrio, featureFlag, winner) => { |
| 177 | + // When a custom aspect is used to apply a permissions boundary, and the built-in APIs to override it, |
| 178 | + // the override still works. |
| 179 | + |
| 180 | + if (featureFlag !== undefined) { |
| 181 | + app = new App({ context: { '@aws-cdk/core:aspectPrioritiesMutating': featureFlag } }); |
| 182 | + stack = new Stack(app, 'Stack'); |
| 183 | + } |
| 184 | + |
| 185 | + // GIVEN |
| 186 | + Aspects.of(stack).add({ |
| 187 | + visit(node) { |
| 188 | + if (node instanceof CfnResource && node.cfnResourceType === 'AWS::IAM::Role') { |
| 189 | + node.addPropertyOverride('PermissionsBoundary', 'BASE'); |
| 190 | + } |
| 191 | + }, |
| 192 | + }, { |
| 193 | + priority: basePrio, |
| 194 | + }); |
| 195 | + |
| 196 | + const role = new iam.Role(stack, 'Role', { |
| 197 | + assumedBy: new iam.AnyPrincipal(), |
| 198 | + }); |
| 199 | + |
| 200 | + // WHEN |
| 201 | + iam.PermissionsBoundary.of(role).apply({ |
| 202 | + managedPolicyArn: 'OVERRIDDEN', |
| 203 | + }); |
| 204 | + |
| 205 | + // THEN |
| 206 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { |
| 207 | + PermissionsBoundary: winner, |
| 208 | + }); |
| 209 | +}); |
0 commit comments