Skip to content

Commit

Permalink
fix: further namespace the id function
Browse files Browse the repository at this point in the history
Having `id` as a static function on `GuSSMParameter` class provides a more explicit namespace.
  • Loading branch information
akash1810 committed Apr 1, 2021
1 parent a28e0c2 commit f3a8656
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/constructs/core/ssm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@aws-cdk/assert/jest";
import { simpleGuStackForTesting } from "../../utils/test";
import { GuSSMIdentityParameter, GuSSMParameter, id } from "./ssm";
import { GuSSMIdentityParameter, GuSSMParameter } from "./ssm";

describe("SSM:", () => {
describe("The GuSSMIdentityParameter construct", () => {
Expand Down Expand Up @@ -48,17 +48,17 @@ describe("SSM:", () => {

describe("the id function", function () {
it("creates a unique ID given a string", function () {
const actual1 = id("NameOfConstruct", "some-parameter");
const actual2 = id("NameOfConstruct", "some-parameter");
const actual1 = GuSSMParameter.id("NameOfConstruct", "some-parameter");
const actual2 = GuSSMParameter.id("NameOfConstruct", "some-parameter");

expect(actual1).toMatch(/NameOfConstruct-someparameter/i);
expect(actual2).toMatch(/NameOfConstruct-someparameter/i);
expect(actual1).not.toEqual(actual2);
});

it("will substitute a CDK token for an acceptable string", function () {
const actual1 = id("NameOfConstruct", "${TOKEN}foobar");
const actual2 = id("NameOfConstruct", "${TOKEN}foobar");
const actual1 = GuSSMParameter.id("NameOfConstruct", "${TOKEN}foobar");
const actual2 = GuSSMParameter.id("NameOfConstruct", "${TOKEN}foobar");

expect(actual1).toMatch(/NameOfConstruct-token/i);
expect(actual2).toMatch(/NameOfConstruct-token/i);
Expand Down
30 changes: 16 additions & 14 deletions src/constructs/core/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ export interface GuSSMIdentityParameterProps extends GuSSMParameterProps, AppIde

const stripped = (str: string) => str.replace(/[-/]/g, "");

export function id(id: string, parameter: string): string {
// `performance.now()` provides a high resolution timer.
// We've seen tests failing in this area with `Date.now()`, the increased resolution should increase uniqueness.
// See https://stackoverflow.com/a/21120901/3868241
const now: string = performance.now().toString();
// We need to create UIDs for the resources in this construct, as otherwise CFN will not trigger the lambda on updates for resources that appear to be the same
const uid = now.substr(now.length - 4);
return parameter.toUpperCase().includes("TOKEN") ? `${id}-token-${uid}` : `${id}-${stripped(parameter)}-${uid}`;
}

export class GuSSMParameter extends Construct implements IGrantable {
private readonly customResource: CustomResource;
readonly grantPrincipal: IPrincipal;

static id = (prefix: string, parameter: string): string => {
// `performance.now()` provides a high resolution timer.
// We've seen tests failing in this area with `Date.now()`, the increased resolution should increase uniqueness.
// See https://stackoverflow.com/a/21120901/3868241
const now: string = performance.now().toString();
// We need to create UIDs for the resources in this construct, as otherwise CFN will not trigger the lambda on updates for resources that appear to be the same
const uid = now.substr(now.length - 4);
return parameter.toUpperCase().includes("TOKEN")
? `${prefix}-token-${uid}`
: `${prefix}-${stripped(parameter)}-${uid}`;
};

constructor(scope: GuStack, props: GuSSMParameterProps) {
super(scope, id("GuSSMParameter", props.parameter));
super(scope, GuSSMParameter.id("GuSSMParameter", props.parameter));
const { parameter } = props;

const provider = new SingletonFunction(scope, id("Provider", parameter), {
const provider = new SingletonFunction(scope, GuSSMParameter.id("Provider", parameter), {
code: Code.fromInline(readFileSync(join(__dirname, "/custom-resources/runtime/lambda.js")).toString()),
runtime: Runtime.NODEJS_12_X,
handler: "index.handler",
Expand All @@ -49,7 +51,7 @@ export class GuSSMParameter extends Construct implements IGrantable {

this.grantPrincipal = provider.grantPrincipal;

const policy = new Policy(scope, id("CustomResourcePolicy", parameter), {
const policy = new Policy(scope, GuSSMParameter.id("CustomResourcePolicy", parameter), {
statements: [
new PolicyStatement({
actions: ["ssm:getParameter"],
Expand All @@ -66,7 +68,7 @@ export class GuSSMParameter extends Construct implements IGrantable {
apiRequest: { Name: parameter, WithDecryption: props.secure },
};

this.customResource = new CustomResource(this, id("Resource", parameter), {
this.customResource = new CustomResource(this, GuSSMParameter.id("Resource", parameter), {
resourceType: "Custom::GuGetSSMParameter",
serviceToken: provider.functionArn,
pascalCaseProperties: false,
Expand Down

0 comments on commit f3a8656

Please sign in to comment.