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
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
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 fahsion", which is very confusing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fashion

*/
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 Construct).node // constructs v10
|| !!(x as any)[sym])); // constructs v3
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how production software looks like!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to the constructs library? Feels like it might be a better place to put this code, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. Don't forget to add it to both versions :)

23 changes: 16 additions & 7 deletions packages/aws-cdk/lib/api/cxapp/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,32 @@ export async function globEnvironmentsFromStacks(stacks: StackCollection, enviro
return environments;
}

export function parseEnvironmentDescriptor(spec: string): cxapi.Environment | undefined {
const parts = spec.replace(/^aws:\/\//, '').split('/');
if (parts.length !== 2) {
return undefined;
}

return {
name: spec,
account: parts[0],
region: parts[1],
};
}

/**
* Given a set of "<account>/<region>" strings, construct environments for them
*/
export function environmentsFromDescriptors(envSpecs: string[]): cxapi.Environment[] {
const ret = new Array<cxapi.Environment>();

for (const spec of envSpecs) {
const parts = spec.replace(/^aws:\/\//, '').split('/');
if (parts.length !== 2) {
const env = parseEnvironmentDescriptor(spec);
if (!env) {
throw new Error(`Expected environment name in format 'aws://<account>/<region>', got: ${spec}`);
}

ret.push({
name: spec,
account: parts[0],
region: parts[1],
});
ret.push(env);
}

return ret;
Expand Down