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

feat: Mask CurrentVersion everywhere #32

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ exports[`ignore current version 1`] = `
},
"Type": "AWS::Lambda::Function",
},
"FunctionAliascurrentBA7922F5": {
"Properties": {
"FunctionName": {
"Ref": "Function76856677",
},
"FunctionVersion": {
"Fn::GetAtt": [
"FunctionCurrentVersion4E2B2261xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Version",
],
},
"Name": "current",
},
"Type": "AWS::Lambda::Alias",
},
"FunctionCurrentVersion4E2B2261xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx": {
"Properties": {
"FunctionName": {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 32 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,39 @@ export const toMatchCdkSnapshot = function (
return propertyMatchers ? matcher(stack, propertyMatchers) : matcher(stack);
};

const maskCurrentVersionRefs = (tree: Record<string, unknown>): 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)) {
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<string, unknown>);
}
} else if (typeof value === "object" && value !== null) {
maskCurrentVersionRefs(value as Record<string, unknown>);
}
}
};
Expand Down Expand Up @@ -120,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);
}

Expand Down