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): state machine name validation fails when tokens are used. #13970

Merged
merged 4 commits into from
Apr 8, 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
17 changes: 10 additions & 7 deletions packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as iam from '@aws-cdk/aws-iam';
import * as logs from '@aws-cdk/aws-logs';
import { Arn, Duration, IResource, Resource, Stack } from '@aws-cdk/core';
import { Arn, Duration, IResource, Resource, Stack, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { StateGraph } from './state-graph';
import { StatesMetrics } from './stepfunctions-canned-metrics.generated';
Expand Down Expand Up @@ -379,7 +379,7 @@ export class StateMachine extends StateMachineBase {
physicalName: props.stateMachineName,
});

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

Expand Down Expand Up @@ -431,11 +431,14 @@ export class StateMachine extends StateMachineBase {
}

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}`);
if (!Token.isUnresolved(stateMachineName)) {
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(/^[a-z0-9\+\!\@\.\(\)\-\=\_\']+$/i)) {
throw new Error(`State Machine name must match "^[a-z0-9+!@.()-=_']+$/i". Received: ${stateMachineName}`);
}
}
}

Expand Down
31 changes: 30 additions & 1 deletion packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,36 @@ describe('State Machine', () => {

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

test('State Machine with valid name', () => {
// GIVEN
const stack = new cdk.Stack();
const newStateMachine = new stepfunctions.StateMachine(stack, 'dummyStateMachineToken', {
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'dummyStateMachineTokenPass')),
});

// WHEN
const nameContainingToken = newStateMachine.stateMachineName + '-Name';
const validName = 'AWS-Stepfunctions_Name.Test(@aws-cdk+)!=\'1\'';

// THEN
expect(() => {
new stepfunctions.StateMachine(stack, 'TokenTest-StateMachine', {
stateMachineName: nameContainingToken,
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'TokenTest-StateMachinePass')),
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
}).not.toThrow();

expect(() => {
new stepfunctions.StateMachine(stack, 'ValidNameTest-StateMachine', {
stateMachineName: validName,
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'ValidNameTest-StateMachinePass')),
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
}).not.toThrow();
});

test('log configuration', () => {
Expand Down