cdklabs / cdk-ssm-documents

Apache License 2.0
27 stars 5 forks source link

StringList encoded incorrectly in the simulation #74

Open mackensen opened 7 months ago

mackensen commented 7 months ago

I have this block of code that gets the security groups from an EFS mount target and then creates a new mount target using that value:

    this.restoreDocument.addStep(new AwsApiStep(this, 'DescribeSourceMountTargetSecurityGroups', {
      service: AwsService.EFS,
      pascalCaseApi: 'DescribeMountTargetSecurityGroups',
      apiParams: {
        MountTargetId: '{{ DescribeSourceMountTargets.SourceMountTargetId }}',
      },
      outputs: [
        {
          outputType: DataTypeEnum.STRING_LIST,
          name: 'SourceSecurityGroups',
          selector: '$.SecurityGroups',
        }
      ]
    }));
...
    this.restoreDocument.addStep(new AwsApiStep(this, 'CreateMountTargetAZ1', {
      service: AwsService.EFS,
      pascalCaseApi: 'CreateMountTarget',
      apiParams: {
        FileSystemId: '{{ DescribeTargetFileSystem.TargetFileSystemId }}',
        SubnetId: '{{ GetParameterPrivateSubnetAZ1.PrivateSubnetAZ1 }}',
        SecurityGroups: '{{ DescribeSourceMountTargetSecurityGroups.SourceSecurityGroups }}',
      },
      outputs: []
    }));

This deploys via CDK without any issues and works. However, the simulator throws an exception. I've mocked the target creation:

    awsInvoker.whenThen(
      {
        awsApi: 'createMountTarget',
        awsParams: {
          SecurityGroups: 'sg-XXX',
          FileSystemId: 'fs-YYY,
          SubnetId: 'subnet-ZZZ'
        },
        service: AwsService.EFS,
      },
      {}
    );

I added some debugging which revealed it's JSON.parse() throwing the exception, on this (invalid) generated string:

{"FileSystemId":"fs-YYY","SubnetId":"subnet-ZZZ","SecurityGroups":"["sg-XXX"]"}

Full log:

    SyntaxError: Expected ',' or '}' after property value in JSON at position 97
        at JSON.parse (<anonymous>)
        at DictFormat.resolveToDict (../node_modules/@cdklabs/cdk-ssm-documents/src/interface/variables/string-variable.ts:134:4)
        at AwsApiSimulation.executeStep (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation/aws-api-simulation.ts:39:44)
        at AutomationStepSimulation.tryExecute (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:233:42)
        at AutomationStepSimulation.executeWithRetries (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:215:21)
        at AutomationStepSimulation.invokeWithFallback (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:117:29)
        at AutomationStepSimulation.invoke (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:107:17)
        at AutomationStepSimulation.invokeWithFallback (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:122:80)
        at AutomationStepSimulation.invoke (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:107:17)
        at AutomationStepSimulation.invokeWithFallback (../node_modules/@cdklabs/cdk-ssm-documents/src/simulation/automation-step-simulation.ts:122:80)

Any guidance would be appreciated. Thanks for all the excellent work on this project!

mackensen commented 7 months ago

In limited manual testing with the simulation only removing this block from src/interface/variables/string-variable.ts resolved the issue:

        inputCopy = inputCopy.replace(match, JSON.stringify(replacement));
        inputCopy = inputCopy.replace(match, JSON.stringify(replacement));

I don't know what the full implications of that change are.