Skip to content

Commit

Permalink
fix(aws-stepfunctions): Validate StateMachine name
Browse files Browse the repository at this point in the history
closes aws#13289
  • Loading branch information
jonnekaunisto committed Mar 4, 2021
1 parent cbed348 commit 109419c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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('StateMachine name length must be between 1 and 80 characters');
}
if (stateMachineName.match('[0-9a-zA-Z\_\-]+')?.length != 1) {
throw new Error('StateMachine name must match [0-9a-zA-Z\_\-]+');
}
}

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
23 changes: 23 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,29 @@ 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,
});
};

// THEN
expect(() => {
createStateMachine('M'.repeat(81));
}).toThrow('StateMachine name length must be between 1 and 80 characters');

expect(() => {
createStateMachine('*');
}).toThrow('StateMachine name must match [0-9a-zA-Z\_\-]+');
});

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

0 comments on commit 109419c

Please sign in to comment.