Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kms): do not change the principal to root for imported resources in dependent Stacks #10299

Merged
merged 3 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as iam from '@aws-cdk/aws-iam';
import { Construct, IResource, RemovalPolicy, Resource, Stack } from '@aws-cdk/core';
import { Construct, IConstruct, IResource, RemovalPolicy, Resource, Stack } from '@aws-cdk/core';
import { Alias } from './alias';
import { CfnKey } from './kms.generated';

Expand Down Expand Up @@ -207,11 +207,18 @@ abstract class KeyBase extends Resource implements IKey {
* undefined otherwise
*/
private granteeStackDependsOnKeyStack(grantee: iam.IGrantable): string | undefined {
if (!(Construct.isConstruct(grantee))) {
const grantPrincipal = grantee.grantPrincipal;
if (!(Construct.isConstruct(grantPrincipal))) {
return undefined;
}
// this logic should only apply to newly created
// (= not imported) resources
if (!this.principalIsANewlyCreatedResource(grantPrincipal)) {
return undefined;
}
// return undefined;
const keyStack = Stack.of(this);
const granteeStack = Stack.of(grantee);
const granteeStack = Stack.of(grantPrincipal);
if (keyStack === granteeStack) {
return undefined;
}
Expand All @@ -220,6 +227,14 @@ abstract class KeyBase extends Resource implements IKey {
: undefined;
}

private principalIsANewlyCreatedResource(principal: IConstruct): boolean {
// yes, this sucks
// this is just a temporary stopgap to stem the bleeding while we work on a proper fix
return principal instanceof iam.Role ||
principal instanceof iam.User ||
principal instanceof iam.Group;
}

private isGranteeFromAnotherRegion(grantee: iam.IGrantable): boolean {
if (!(Construct.isConstruct(grantee))) {
return false;
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/pipelines/test/cross-environment-infra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ test('in a cross-account/cross-region setup, artifact bucket can be read by depl
})),
},
});

// And the key to go along with it
expect(supportStack).toHaveResourceLike('AWS::KMS::Key', {
KeyPolicy: {
Statement: arrayWith(objectLike({
Action: arrayWith('kms:Decrypt', 'kms:DescribeKey'),
Principal: {
AWS: {
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
stringLike('*-deploy-role-*'),
]],
},
},
})),
},
});
});

test('in a cross-account/same-region setup, artifact bucket can be read by deploy role', () => {
Expand Down