From ae04e061a333d437e304883fb5c7f352338d83e6 Mon Sep 17 00:00:00 2001 From: exoego Date: Fri, 23 Feb 2024 10:22:17 +0900 Subject: [PATCH 1/3] Test CurrentVersion in array --- src/__tests__/__snapshots__/index.test.ts.snap | 15 +++++++++++++++ src/__tests__/index.test.ts | 1 + 2 files changed, 16 insertions(+) diff --git a/src/__tests__/__snapshots__/index.test.ts.snap b/src/__tests__/__snapshots__/index.test.ts.snap index 09aaa0f..45361f5 100644 --- a/src/__tests__/__snapshots__/index.test.ts.snap +++ b/src/__tests__/__snapshots__/index.test.ts.snap @@ -137,6 +137,21 @@ exports[`ignore current version 1`] = ` }, "Type": "AWS::Lambda::Function", }, + "FunctionAliascurrentBA7922F5": { + "Properties": { + "FunctionName": { + "Ref": "Function76856677", + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FunctionCurrentVersion4E2B226151c1faf27d0d163dcd41fb04818fca79", + "Version", + ], + }, + "Name": "current", + }, + "Type": "AWS::Lambda::Alias", + }, "FunctionCurrentVersion4E2B2261xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx": { "Properties": { "FunctionName": { diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index a527a66..d888472 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -107,6 +107,7 @@ test("ignore current version", () => { handler: "index.handler", }); lf.currentVersion.grantInvoke(new AccountPrincipal("123456789012")); + lf.addAlias("current", {}); new CfnOutput(stack, "CurrentVersion", { value: lf.currentVersion.functionArn, From 41655e2a59cd32a4de7b0a36c19e09bd0f82e4b3 Mon Sep 17 00:00:00 2001 From: exoego Date: Fri, 23 Feb 2024 10:26:50 +0900 Subject: [PATCH 2/3] Mask CurrentVersion literal in Array too --- .../__snapshots__/index.test.ts.snap | 2 +- src/index.ts | 33 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/__tests__/__snapshots__/index.test.ts.snap b/src/__tests__/__snapshots__/index.test.ts.snap index 45361f5..3ee953b 100644 --- a/src/__tests__/__snapshots__/index.test.ts.snap +++ b/src/__tests__/__snapshots__/index.test.ts.snap @@ -144,7 +144,7 @@ exports[`ignore current version 1`] = ` }, "FunctionVersion": { "Fn::GetAtt": [ - "FunctionCurrentVersion4E2B226151c1faf27d0d163dcd41fb04818fca79", + "FunctionCurrentVersion4E2B2261xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Version", ], }, diff --git a/src/index.ts b/src/index.ts index 74c1633..2e4d2e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,15 +62,32 @@ export const toMatchCdkSnapshot = function ( return propertyMatchers ? matcher(stack, propertyMatchers) : matcher(stack); }; -const maskCurrentVersionRefs = (tree: Record): void => { - for (const [key, value] of Object.entries(tree)) { - if (key === "Ref" && typeof value === "string") { - const match = currentVersionRegex.exec(value); - if (match) { - tree[key] = `${match[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; +const maskCurrentVersionRefs = (tree: unknown): void => { + if (tree == null) { + return; + } + if (Array.isArray(tree)) { + for (let i = 0; i < tree.length; i++) { + const value = tree[i]; + if (typeof value === "string") { + const match = currentVersionRegex.exec(value); + if (match) { + tree[i] = `${match[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; + } + } else if (typeof value === "object") { + maskCurrentVersionRefs(value); + } + } + } else if (typeof tree === "object") { + for (const [key, value] of Object.entries(tree)) { + if (key === "Ref" && typeof value === "string") { + const match = currentVersionRegex.exec(value); + if (match) { + tree[key] = `${match[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; + } + } else if (typeof value === "object") { + maskCurrentVersionRefs(value as Record); } - } else if (typeof value === "object" && value !== null) { - maskCurrentVersionRefs(value as Record); } } }; From 94c38bba3956b8bd6495cbf1fb270cb5dcda8fe8 Mon Sep 17 00:00:00 2001 From: exoego Date: Fri, 23 Feb 2024 13:25:07 +0900 Subject: [PATCH 3/3] Mask current version in all keys and values --- src/index.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2e4d2e6..39486d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,10 +80,17 @@ const maskCurrentVersionRefs = (tree: unknown): void => { } } else if (typeof tree === "object") { for (const [key, value] of Object.entries(tree)) { - if (key === "Ref" && typeof value === "string") { - const match = currentVersionRegex.exec(value); - if (match) { - tree[key] = `${match[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; + const keyMatch = currentVersionRegex.exec(key); + if (keyMatch) { + const newKey = `${keyMatch[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; + tree[newKey] = value; + delete tree[key]; + } + + if (typeof value === "string") { + const valueMatch = currentVersionRegex.exec(value); + if (valueMatch) { + tree[key] = `${valueMatch[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; } } else if (typeof value === "object") { maskCurrentVersionRefs(value as Record); @@ -137,15 +144,6 @@ const convertStack = (stack: Stack, options: Options = {}) => { } if (ignoreCurrentVersion && template.Resources) { - for (const [key, resource] of Object.entries(template.Resources)) { - const match = currentVersionRegex.exec(key); - if (match) { - const newKey = `${match[1]}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; - template.Resources[newKey] = resource; - delete template.Resources[key]; - } - } - maskCurrentVersionRefs(template); }