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

(cli): tags with an equals sign in the value only use the value of the string prior to the equals sign #21003

Closed
peterwoodworth opened this issue Jul 5, 2022 · 3 comments · Fixed by #27130
Assignees
Labels
bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p1 package/tools Related to AWS CDK Tools or CLI

Comments

@peterwoodworth
Copy link
Contributor

Describe the bug

If you need a tag with a key value pair such as

Key : MyKey
Value : MyValue=Something

The stack will have a key value pair that shows

Key: MyKey
Value: MyValue

Expected Behavior

I would expect the value of the tag to be MyValue=Something

Current Behavior

Doesn't read the full tag value

Reproduction Steps

Try to create a stack with a tag that has an equals sign in the value

Possible Solution

The code which parses tags in the command line will split when it sees an equals sign, and will only use the second split value as the tag value, when it should be everything after the first split

private static parseStringTagsListToObject(argTags: string[] | undefined): Tag[] | undefined {
if (argTags === undefined) { return undefined; }
if (argTags.length === 0) { return undefined; }
const nonEmptyTags = argTags.filter(t => t !== '');
if (nonEmptyTags.length === 0) { return []; }
const tags: Tag[] = [];
for (const assignment of nonEmptyTags) {
const parts = assignment.split('=', 2);
if (parts.length === 2) {
debug('CLI argument tags: %s=%s', parts[0], parts[1]);
tags.push({
Key: parts[0],
Value: parts[1],
});
} else {
warning('Tags argument is not an assignment (key=value): %s', assignment);
}
}
return tags.length > 0 ? tags : undefined;
}

Additional Information/Context

No response

CDK CLI Version

.

Framework Version

No response

Node.js Version

.

OS

.

Language

Typescript

Language Version

No response

Other information

No response

@peterwoodworth peterwoodworth added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 5, 2022
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jul 5, 2022
@peterwoodworth peterwoodworth added good first issue Related to contributions. See CONTRIBUTING.md p2 effort/small Small work item – less than a day of effort and removed package/tools Related to AWS CDK Tools or CLI needs-triage This issue or PR still needs to be triaged. labels Jul 5, 2022
@nvcaine
Copy link

nvcaine commented Jul 6, 2022

Hi Peter,

I encountered the same problem while attempting to tag the resources created in the bootstrapping phase of the deployment process, specifically the S3 bucket created by CDK prior to actually deploying the stack(s).

Here is a workaround:

First, a custom name is used for the bootstrap the bucket. That can be done with the following command:

cdk bootstrap aws://{ACCOUNT}/{REGION} --toolkit-bucket-name {CUSTOM BUCKET NAME}

Now that the bucket has a name, its ARN can be determined. Finally the AWS resourcegroupstaggingapi can be used to tag resources:

aws resourcegroupstaggingapi tag-resources --cli-input-json file://{PATH TO TAGS FILE}.json --region {REGION}

We found the most convenient way is to use a JSON file with the following structure:

{
    "ResourceARNList": ["arn:aws:s3:::{CUSTOM BUCKET NAME}"],
    "Tags": {
        "{TAG KEY}": "{SOME FIELD}={SOME VALUE}",
        ...
    }
}

This approach works for preserving values which contain =, but you have to do the bootstrapping phase in two steps.

Have a good day,
Romi

@seungholee-dev
Copy link

Hi, @peterwoodworth
I am a new contributor to this project and can I try working on this?

@mergify mergify bot closed this as completed in #27130 Sep 19, 2023
mergify bot pushed a commit that referenced this issue Sep 19, 2023
When overriding tags from the CLI the tag key-value string is split by an equal sign and the left and right parts are used for key and value respectively. The documentation for the [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) states that the second parameter sets a limit of the returned values after execution:
> If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

So basically if we have `foo=bar=test` and we apply a split with a limit of 2 (`"foo=bar=test".split("=", 2)`) we will get the following array `["foo", "bar"]` instead of the expected one`["foo", "bar=test"]`. 

There has been the same problem with the context values and since it is the same problem the solution is also the same: #5773 

With this fix the `context` and `tags` get the same behaviour when parsing the passed values.

Closes #21003 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

HBobertz pushed a commit that referenced this issue Sep 19, 2023
When overriding tags from the CLI the tag key-value string is split by an equal sign and the left and right parts are used for key and value respectively. The documentation for the [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) states that the second parameter sets a limit of the returned values after execution:
> If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

So basically if we have `foo=bar=test` and we apply a split with a limit of 2 (`"foo=bar=test".split("=", 2)`) we will get the following array `["foo", "bar"]` instead of the expected one`["foo", "bar=test"]`. 

There has been the same problem with the context values and since it is the same problem the solution is also the same: #5773 

With this fix the `context` and `tags` get the same behaviour when parsing the passed values.

Closes #21003 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p1 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
4 participants