Skip to content

Commit

Permalink
fix(cli): parameter values with multiple = symbols get truncated (#…
Browse files Browse the repository at this point in the history
…7226)

Using `cdk-deploy` with `--parameters` flag loses part of parameter string if it contains more than one `=` symbol

Why?

Because current code splits parameter string on "=" and only adds the 1st element to parameterMap as a value

Fixes #7246
  • Loading branch information
dziugasmikalkenas-home24 authored Apr 15, 2020
1 parent 7695f2b commit b7ddf5b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ async function initCommandLine() {
const parameterMap: { [name: string]: string | undefined } = {};
for (const parameter of args.parameters) {
if (typeof parameter === 'string') {
const keyValue = (parameter as string).split('=', 2);
parameterMap[keyValue[0]] = keyValue[1];
const keyValue = (parameter as string).split('=');
parameterMap[keyValue[0]] = keyValue.slice(1).join('=');
}
}
return await cli.deploy({
Expand Down
6 changes: 4 additions & 2 deletions packages/aws-cdk/test/api/deploy-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ test('correctly passes CFN parameters, ignoring ones with empty values', async (
resolvedEnvironment: mockResolvedEnvironment(),
parameters: {
A: 'A-value',
B: undefined,
C: '',
B: 'B=value',
C: undefined,
D: '',
},
});

// THEN
expect(cfnMocks.createChangeSet).toHaveBeenCalledWith(expect.objectContaining({
Parameters: [
{ ParameterKey: 'A', ParameterValue: 'A-value' },
{ ParameterKey: 'B', ParameterValue: 'B=value' },
]
}));
});
Expand Down
15 changes: 15 additions & 0 deletions packages/aws-cdk/test/integ/cli/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ class OtherParameterStack extends cdk.Stack {
}
}

class MultiParameterStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);

new sns.Topic(this, 'TopicParameter', {
topicName: new cdk.CfnParameter(this, 'TopicNameParam')
});
new sns.Topic(this, 'OtherTopicParameter', {
topicName: new cdk.CfnParameter(this, 'OtherTopicNameParam')
});
}
}

class OutputsStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);
Expand Down Expand Up @@ -236,6 +249,8 @@ new YourStack(app, `${stackPrefix}-test-2`);
// Deploy wildcard with parameters does ${stackPrefix}-param-test-*
new ParameterStack(app, `${stackPrefix}-param-test-1`);
new OtherParameterStack(app, `${stackPrefix}-param-test-2`);
// Deploy stack with multiple parameters
new MultiParameterStack(app, `${stackPrefix}-param-test-3`);
// Deploy stack with outputs does ${stackPrefix}-outputs-test-*
new OutputsStack(app, `${stackPrefix}-outputs-test-1`);
new AnotherOutputsStack(app, `${stackPrefix}-outputs-test-2`);
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-cdk/test/integ/cli/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ function prepare_fixture() {
function cleanup() {
cleanup_stack ${STACK_NAME_PREFIX}-test-1
cleanup_stack ${STACK_NAME_PREFIX}-test-2
cleanup_stack ${STACK_NAME_PREFIX}-param-test-1
cleanup_stack ${STACK_NAME_PREFIX}-param-test-2
cleanup_stack ${STACK_NAME_PREFIX}-param-test-3
cleanup_stack ${STACK_NAME_PREFIX}-iam-test
cleanup_stack ${STACK_NAME_PREFIX}-with-nested-stack
cleanup_stack ${STACK_NAME_PREFIX}-outputs-test-1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
set -euo pipefail
scriptdir=$(cd $(dirname $0) && pwd)
source ${scriptdir}/common.bash
# ----------------------------------------------------------

setup

paramVal1="${STACK_NAME_PREFIX}bazinga"
paramVal2="${STACK_NAME_PREFIX}=jagshemash"

stack_arn=$(cdk deploy -v ${STACK_NAME_PREFIX}-param-test-3 --parameters "TopicNameParam=${paramVal1}" --parameters "OtherTopicNameParam=${paramVal2}")
echo "Stack deployed successfully"

# verify that we only deployed a single stack (there's a single ARN in the output)
assert_lines "${stack_arn}" 1

# verify the number of resources in the stack
response_json=$(mktemp).json
aws cloudformation describe-stack-resources --stack-name ${stack_arn} > ${response_json}
resource_count=$(node -e "console.log(require('${response_json}').StackResources.length)")

# verify whether the stack has the same parameter values as we passed in cli
for (( i=0; i<$resource_count; i++ )); do
passedParameterVal=$(node -e "console.log(require('${response_json}').StackResources[$i].PhysicalResourceId.split(':').reverse()[0])")

if ! [[ "${passedParameterVal}" =~ ^(${paramVal1}|{$paramVal2})$ ]]; then
fail "expected stack to have parameter: ${passedParameterVal}"
fi
done;

# verify the number of resources in the stack
if [ "${resource_count}" -ne 2 ]; then
fail "stack has ${resource_count} resources, and we expected two"
fi

# destroy
cdk destroy -f ${STACK_NAME_PREFIX}-param-test-3


echo "✅ success"

0 comments on commit b7ddf5b

Please sign in to comment.