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(stepfunctions): no validation on state machine name #13387

Merged
merged 4 commits into from
Mar 9, 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
15 changes: 14 additions & 1 deletion packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export enum LogLevel {
/**
* Log all errors
*/
ERROR= 'ERROR',
ERROR = 'ERROR',
/**
* Log fatal errors
*/
Expand Down Expand Up @@ -379,6 +379,10 @@ export class StateMachine extends StateMachineBase {
physicalName: props.stateMachineName,
});

if (props.stateMachineName != undefined) {
this.validateStateMachineName(props.stateMachineName);
}

this.role = props.role || new iam.Role(this, 'Role', {
assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
});
Expand Down Expand Up @@ -426,6 +430,15 @@ export class StateMachine extends StateMachineBase {
this.role.addToPrincipalPolicy(statement);
}

private validateStateMachineName(stateMachineName: string) {
if (stateMachineName.length < 1 || stateMachineName.length > 80) {
throw new Error(`State Machine name must be between 1 and 80 characters. Received: ${stateMachineName}`);
}
if (!stateMachineName.match('^[0-9a-zA-Z+!@._-]+$')) {
throw new Error(`State Machine name must match "^[0-9a-zA-Z+!@._-]+$". Received: ${stateMachineName}`);
}
}

private buildLoggingConfiguration(logOptions: LogOptions): CfnStateMachine.LoggingConfigurationProperty {
// https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
this.addToRolePolicy(new iam.PolicyStatement({
Expand Down
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ describe('State Machine', () => {

}),

test('State Machine with invalid name', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const createStateMachine = (name: string) => {
new stepfunctions.StateMachine(stack, name + 'StateMachine', {
stateMachineName: name,
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, name + 'Pass')),
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
};
const tooShortName = '';
const tooLongName = 'M'.repeat(81);
const invalidCharactersName = '*';

// THEN
expect(() => {
createStateMachine(tooShortName);
}).toThrow(`State Machine name must be between 1 and 80 characters. Received: ${tooShortName}`);

expect(() => {
createStateMachine(tooLongName);
}).toThrow(`State Machine name must be between 1 and 80 characters. Received: ${tooLongName}`);
Comment on lines +86 to +88
Copy link
Contributor

Choose a reason for hiding this comment

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

let's also add one more expect for a blank string to cover the other side of the bounds.


expect(() => {
createStateMachine(invalidCharactersName);
}).toThrow(`State Machine name must match "^[0-9a-zA-Z+!@._-]+$". Received: ${invalidCharactersName}`);
});

test('log configuration', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down