Skip to content

Commit

Permalink
Merge pull request #1471 from RoadieHQ/sc-21523-sanitiza-aws-tags-more
Browse files Browse the repository at this point in the history
Sanitize AWS tags as labels
  • Loading branch information
Xantier authored Jul 10, 2024
2 parents 9684eb1 + ace4e5a commit acf01d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-readers-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/catalog-backend-module-aws': patch
---

Add label value/key sanitization to remove trailing unsupported characters.
11 changes: 11 additions & 0 deletions plugins/backend/catalog-backend-module-aws/src/utils/tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ describe('labelsFromTags and ownerFromTags', () => {
).toBe(true);
});

it('should handle a url with a succeeding slash as a value', () => {
const tags = [
{ Key: 'tag:one:two', Value: 'https://blha.com/blahblah/' },
];
const result = labelsFromTags(tags);
expect(result).toEqual({ tag_one_two: 'https---blha.com-blahblah' });
expect(
KubernetesValidatorFunctions.isValidLabelValue(result.tag_one_two),
).toBe(true);
});

it('should ignore keys without values in an object of tags', () => {
const tags = { 'tag:one': 'value1', 'tag:two': undefined };
// @ts-ignore
Expand Down
13 changes: 11 additions & 2 deletions plugins/backend/catalog-backend-module-aws/src/utils/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const TAG_DOMAIN = 'domain';
const dependencyTags = [TAG_DEPENDENCY_OF, TAG_DEPENDS_ON];
const relationshipTags = [TAG_SYSTEM, TAG_DOMAIN];

function stripTrailingChar(str: string, chr: string) {
return str.endsWith(chr) ? str.slice(0, -1) : str;
}

export const labelsFromTags = (tags?: Tag[] | Record<string, string>) => {
if (!tags) {
return {};
Expand All @@ -45,10 +49,15 @@ export const labelsFromTags = (tags?: Tag[] | Record<string, string>) => {
)
.reduce((acc: Record<string, string>, tag) => {
if (tag.Key && tag.Value) {
const key = tag.Key.replaceAll(':', '_').replaceAll('/', '-');
acc[key] = tag.Value.replaceAll('/', '-')
let key = tag.Key.replaceAll(':', '_')
.replaceAll('/', '-')
.substring(0, 63);
key = stripTrailingChar(stripTrailingChar(key, '-'), '_');
let val = tag.Value.replaceAll('/', '-')
.replaceAll(':', '-')
.substring(0, 63);
val = stripTrailingChar(val, '-');
acc[key] = val;
}
return acc;
}, {});
Expand Down

0 comments on commit acf01d9

Please sign in to comment.