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(ssm): correctly deduplicate parameter names #3183

Merged
merged 7 commits into from
Jul 3, 2019
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
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ssm/lib/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import iam = require('@aws-cdk/aws-iam');
import {
CfnDynamicReference, CfnDynamicReferenceService, CfnParameter,
Construct, ContextProvider, Fn, IConstruct, IResource, Resource, Stack, Token, ConstructNode
Construct, ContextProvider, Fn, IConstruct, IResource, Resource, Stack, Token
} from '@aws-cdk/core';
import cxapi = require('@aws-cdk/cx-api');
import ssm = require('./ssm.generated');
Expand Down Expand Up @@ -372,7 +372,7 @@ function _assertValidValue(value: string, allowedPattern: string): void {
}

function makeIdentityForImportedValue(parameterName: string) {
return ConstructNode.sanitizeId(`SsmParameterValue:${parameterName}:C96584B6-F00A-464E-AD19-53AFF4B05118`);
return `SsmParameterValue:${parameterName}:C96584B6-F00A-464E-AD19-53AFF4B05118`;
}

function arnForParameterName(scope: IConstruct, parameterName: string): string {
Expand Down
55 changes: 14 additions & 41 deletions packages/@aws-cdk/core/lib/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ export class ConstructNode {
*/
public static readonly PATH_SEP = '/';

/**
* Return a sanitized version of an arbitrary string, so it can be used as an ID
*/
public static sanitizeId(id: string) {
// Cannot have PATH_SEPs in the ID
return id.replace(/\//g, '-');
}

/**
* Synthesizes a CloudAssembly from a construct tree.
* @param root The root of the construct tree.
Expand Down Expand Up @@ -137,7 +129,7 @@ export class ConstructNode {
constructor(private readonly host: Construct, scope: IConstruct, id: string) {
id = id || ''; // if undefined, convert to empty string

this.id = id;
this.id = sanitizeId(id);
this.scope = scope;

// We say that scope is required, but root scopes will bypass the type
Expand All @@ -147,20 +139,13 @@ export class ConstructNode {
throw new Error('Only root constructs may have an empty name');
}

if (ConstructNode.sanitizeId(id) !== id) {
throw new Error('Invalid characters in id, use ConstructNode.sanitizeId(): ' + id);
}

// Has side effect so must be very last thing in constructor
scope.node.addChild(host, this.id);
} else {
// This is a root construct.
this.id = id;
}

// escape any path separators so they don't wreck havoc
this.id = this._escapePathSeparator(this.id);

if (Token.isUnresolved(id)) {
throw new Error(`Cannot use tokens in construct ID: ${id}`);
}
Expand All @@ -186,25 +171,13 @@ export class ConstructNode {
}

/**
* Return a descendant by path, or undefined
*
* Note that if the original ID of the construct you are looking for contained
* a '/', then it would have been replaced by '--'.
* Return a direct child by id, or undefined
*
* @param path Relative path of a direct or indirect child
* @returns a child by path or undefined if not found.
* @param id Identifier of direct child
* @returns the child if found, or undefined
*/
public tryFindChild(path: string): IConstruct | undefined {
if (path.startsWith(ConstructNode.PATH_SEP)) {
throw new Error('Path must be relative');
}
const parts = path.split(ConstructNode.PATH_SEP);

let curr: IConstruct | undefined = this.host;
while (curr != null && parts.length > 0) {
curr = curr.node._children[parts.shift()!];
}
return curr;
public tryFindChild(id: string): IConstruct | undefined {
return this._children[sanitizeId(id)];
}

/**
Expand Down Expand Up @@ -539,14 +512,6 @@ export class ConstructNode {
this.invokedAspects.push(aspect);
}
}

/**
* If the construct ID contains a path separator, it is replaced by double dash (`--`).
*/
private _escapePathSeparator(id: string) {
if (!id) { return id; }
return id.split(ConstructNode.PATH_SEP).join('--');
}
}

/**
Expand Down Expand Up @@ -727,3 +692,11 @@ function ignore(_x: any) {
// Import this _after_ everything else to help node work the classes out in the correct order...

import { Reference } from './reference';

/**
* Return a sanitized version of an arbitrary string, so it can be used as an ID
*/
function sanitizeId(id: string) {
// Escape path seps as double dashes
return id.replace(/\//g, '--');
Copy link
Contributor

Choose a reason for hiding this comment

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

Use PATH_SEP

}
6 changes: 0 additions & 6 deletions packages/@aws-cdk/core/test/test.construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ export = {
test.done();
},

'constructs names cannot have a slash in them'(test: Test) {
const root = new Root();
test.throws(() => new Construct(root, 'hello/bye'));
test.done();
},

'construct.name returns the name of the construct'(test: Test) {
const t = createTree();

Expand Down