Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ abstract class KeyBase extends Resource implements IKey {
resourceArns: [this.keyArn],
resourceSelfArns: crossEnvironment ? undefined : ['*'],
};
if (this.trustAccountIdentities) {
if (this.trustAccountIdentities && !crossEnvironment) {
return iam.Grant.addToPrincipalOrResource(grantOptions);
} else {
return iam.Grant.addToPrincipalAndResource({
Expand Down
100 changes: 100 additions & 0 deletions packages/@aws-cdk/aws-kms/test/key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,106 @@ describe('key policies', () => {
});
});

testFutureBehavior('grant for a principal in a different region', flags, cdk.App, (app) => {
const principalStack = new cdk.Stack(app, 'PrincipalStack', { env: { region: 'testregion1' } });
const principal = new iam.Role(principalStack, 'Role', {
assumedBy: new iam.AnyPrincipal(),
roleName: 'MyRolePhysicalName',
});

const keyStack = new cdk.Stack(app, 'KeyStack', { env: { region: 'testregion2' } });
const key = new kms.Key(keyStack, 'Key');

key.grantEncrypt(principal);

expect(keyStack).toHaveResourceLike('AWS::KMS::Key', {
KeyPolicy: {
Statement: [
{
// Default policy, unmodified
},
{
Action: [
'kms:Encrypt',
'kms:ReEncrypt*',
'kms:GenerateDataKey*',
],
Effect: 'Allow',
Principal: { AWS: { 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::', { Ref: 'AWS::AccountId' }, ':role/MyRolePhysicalName']] } },
Resource: '*',
},
],
Version: '2012-10-17',
},
});
expect(principalStack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: [
'kms:Encrypt',
'kms:ReEncrypt*',
'kms:GenerateDataKey*',
],
Effect: 'Allow',
Resource: '*',
},
],
Version: '2012-10-17',
},
});
});

testFutureBehavior('grant for a principal in a different account', flags, cdk.App, (app) => {
const principalStack = new cdk.Stack(app, 'PrincipalStack', { env: { account: '0123456789012' } });
const principal = new iam.Role(principalStack, 'Role', {
assumedBy: new iam.AnyPrincipal(),
roleName: 'MyRolePhysicalName',
});

const keyStack = new cdk.Stack(app, 'KeyStack', { env: { account: '111111111111' } });
const key = new kms.Key(keyStack, 'Key');

key.grantEncrypt(principal);

expect(keyStack).toHaveResourceLike('AWS::KMS::Key', {
KeyPolicy: {
Statement: [
{
// Default policy, unmodified
},
{
Action: [
'kms:Encrypt',
'kms:ReEncrypt*',
'kms:GenerateDataKey*',
],
Effect: 'Allow',
Principal: { AWS: { 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::0123456789012:role/MyRolePhysicalName']] } },
Resource: '*',
},
],
Version: '2012-10-17',
},
});
expect(principalStack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: [
'kms:Encrypt',
'kms:ReEncrypt*',
'kms:GenerateDataKey*',
],
Effect: 'Allow',
Resource: '*',
},
],
Version: '2012-10-17',
},
});
});

testFutureBehavior('additional key admins can be specified (with imported/immutable principal)', flags, cdk.App, (app) => {
const stack = new cdk.Stack(app);
const adminRole = iam.Role.fromRoleArn(stack, 'Admin', 'arn:aws:iam::123456789012:role/TrustedAdmin');
Expand Down