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

Allow all parameters to be from SSM #141

Merged
merged 4 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
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
83 changes: 52 additions & 31 deletions src/constructs/core/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
GuAmiParameter,
GuArnParameter,
GuInstanceTypeParameter,
GuParameter,
GuS3ObjectArnParameter,
GuSSMParameter,
GuStackParameter,
GuStageParameter,
GuStringParameter,
Expand All @@ -18,6 +18,57 @@ import {
} from "./parameters";
import type { GuStack } from "./stack";

describe("The GuParameter class", () => {
it("sets the type as passed through by default", () => {
const stack = simpleGuStackForTesting();

new GuParameter(stack, "Parameter", { type: "Boolean" });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
Type: "Boolean",
});
});

it("wraps the type with SSM utility is fromSSM is true", () => {
const stack = simpleGuStackForTesting();

new GuParameter(stack, "Parameter", { type: "Boolean", fromSSM: true });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
Type: "AWS::SSM::Parameter::Value<Boolean>",
});
});

it("defaults to string if SSM is true but no type provided", () => {
const stack = simpleGuStackForTesting();

new GuParameter(stack, "Parameter", { fromSSM: true });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
Type: "AWS::SSM::Parameter::Value<String>",
});
});

it("passes through other values without modification", () => {
const stack = simpleGuStackForTesting();

new GuParameter(stack, "Parameter", { type: "Boolean", fromSSM: true, description: "This is a test" });
Copy link
Member

Choose a reason for hiding this comment

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

We might want to make type an enum or validate it at some point, for a later PR though.


const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
Type: "AWS::SSM::Parameter::Value<Boolean>",
Description: "This is a test",
});
});
});

describe("The GuStringParameter class", () => {
it("should set the type to string", () => {
const stack = simpleGuStackForTesting();
Expand Down Expand Up @@ -101,36 +152,6 @@ describe("The GuInstanceTypeParameter class", () => {
});
});

describe("The GuSSMParameter class", () => {
it("should combine default, override and prop values", () => {
const stack = simpleGuStackForTesting();

new GuSSMParameter(stack, "Parameter", { description: "This is a test" });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
NoEcho: true,
Type: "AWS::SSM::Parameter::Value<String>",
Description: "This is a test",
});
});

it("let's you override default props", () => {
const stack = simpleGuStackForTesting();

new GuSSMParameter(stack, "Parameter", { noEcho: false, description: "This is a test" });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.Parameter).toEqual({
NoEcho: false,
Type: "AWS::SSM::Parameter::Value<String>",
Description: "This is a test",
});
});
});

describe("The GuSubnetListParameter class", () => {
it("should combine override and prop values", () => {
const stack = simpleGuStackForTesting();
Expand Down
19 changes: 7 additions & 12 deletions src/constructs/core/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { CfnParameter } from "@aws-cdk/core";
import { RegexPattern, Stage, Stages } from "../../constants";
import type { GuStack } from "./stack";

export type GuParameterProps = CfnParameterProps;
export interface GuParameterProps extends CfnParameterProps {
fromSSM?: boolean;
}

export type GuNoTypeParameterProps = Omit<GuParameterProps, "type">;

export class GuParameter extends CfnParameter {
constructor(scope: GuStack, id: string, props: GuParameterProps) {
super(scope, id, props);
super(scope, id, {
...props,
type: props.fromSSM ? `AWS::SSM::Parameter::Value<${props.type ?? "String"}>` : props.type,
});
}
}

Expand Down Expand Up @@ -51,16 +56,6 @@ export class GuInstanceTypeParameter extends GuParameter {
}
}

export class GuSSMParameter extends GuParameter {
constructor(scope: GuStack, id: string, props: GuNoTypeParameterProps) {
super(scope, id, {
noEcho: true,
...props,
type: "AWS::SSM::Parameter::Value<String>",
});
}
}

export class GuSubnetListParameter extends GuParameter {
constructor(scope: GuStack, id: string, props: GuNoTypeParameterProps) {
super(scope, id, { ...props, type: "List<AWS::EC2::Subnet::Id>" });
Expand Down
5 changes: 3 additions & 2 deletions src/constructs/iam/policies/log-shipping.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Effect, PolicyStatement } from "@aws-cdk/aws-iam";
import type { GuStack } from "../../core";
import { GuSSMParameter } from "../../core";
import { GuStringParameter } from "../../core";
import type { GuPolicyProps } from "./base-policy";
import { GuPolicy } from "./base-policy";

export class GuLogShippingPolicy extends GuPolicy {
constructor(scope: GuStack, id: string = "GuLogShippingPolicy", props?: GuPolicyProps) {
super(scope, id, { ...props });

const loggingStreamNameParam = new GuSSMParameter(scope, "LoggingStreamName", {
const loggingStreamNameParam = new GuStringParameter(scope, "LoggingStreamName", {
description: "SSM parameter containing the Name (not ARN) on the kinesis stream",
default: "/account/services/logging.stream.name",
fromSSM: true,
});

this.addStatements(
Expand Down
1 change: 0 additions & 1 deletion src/patterns/__snapshots__/instance-role.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ Object {
"LoggingStreamName": Object {
"Default": "/account/services/logging.stream.name",
"Description": "SSM parameter containing the Name (not ARN) on the kinesis stream",
"NoEcho": true,
"Type": "AWS::SSM::Parameter::Value<String>",
},
"Stack": Object {
Expand Down