Skip to content

Commit

Permalink
fix(aws-stepfunctions): allow tokens in state machine names
Browse files Browse the repository at this point in the history
Name validation for state machines was added in aws#13387. The validation did not take into consideration that the name may contain an unresolved token. This change skips name validation if there is an unresolved token as it does not make sense to attempt to validate something that is unknown. This change also allows a few more special characters as they are allowed through the AWS Console.

fixes aws#13946
  • Loading branch information
jstrese committed Apr 3, 2021
1 parent c5a2add commit aeb2c91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
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

0 comments on commit aeb2c91

Please sign in to comment.