Skip to content

Commit

Permalink
fix(stepfunctions): state machine name validation fails when tokens a…
Browse files Browse the repository at this point in the history
…re used. (aws#13970)

Name validation for state machines was added in aws#13387. The validation added was not considering 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 aws#13912


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jstrese authored and hollanddd committed Aug 26, 2021
1 parent ad7ac97 commit 69b7c3c
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 69b7c3c

Please sign in to comment.