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

chore(kms): cross-stack usage detection depends on NPM tree #15580

Merged
merged 4 commits into from
Jul 16, 2021
Merged
Changes from all 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
23 changes: 20 additions & 3 deletions packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ abstract class KeyBase extends Resource implements IKey {
*/
private granteeStackDependsOnKeyStack(grantee: iam.IGrantable): string | undefined {
const grantPrincipal = grantee.grantPrincipal;
if (!(grantPrincipal instanceof Construct)) {
if (!isConstruct(grantPrincipal)) {
return undefined;
}
// this logic should only apply to newly created
Expand Down Expand Up @@ -229,7 +229,7 @@ abstract class KeyBase extends Resource implements IKey {
}

private isGranteeFromAnotherRegion(grantee: iam.IGrantable): boolean {
if (!(grantee instanceof Construct)) {
if (!isConstruct(grantee)) {
return false;
}
const bucketStack = Stack.of(this);
Expand All @@ -238,7 +238,7 @@ abstract class KeyBase extends Resource implements IKey {
}

private isGranteeFromAnotherAccount(grantee: iam.IGrantable): boolean {
if (!(grantee instanceof Construct)) {
if (!isConstruct(grantee)) {
return false;
}
const bucketStack = Stack.of(this);
Expand Down Expand Up @@ -675,3 +675,20 @@ export class Key extends KeyBase {
}));
}
}

/**
* Whether the given object is a Construct
*
* Normally we'd do `x instanceof Construct`, but that is not robust against
* multiple copies of the `constructs` library on disk. This can happen
* when upgrading and downgrading between v2 and v1, and in the use of CDK
* Pipelines is going to an error that says "Can't use Pipeline/Pipeline/Role in
* a cross-environment fashion", which is very confusing.
*/
function isConstruct(x: any): x is Construct {
const sym = Symbol.for('constructs.Construct.node');
return (typeof x === 'object' && x &&
(x instanceof Construct // happy fast case
|| !!(x as any).node // constructs v10
|| !!(x as any)[sym])); // constructs v3
}