Skip to content

Commit 70fdf90

Browse files
committed
feat: implement the account ID masking function
Implement the maskAccountId function, which is responsible for replacing instances of the AWS account ID found in the taskcat_output logs with "***", masking them before publishing the artifacts. This commit also introduces unit tests covering the new function. Associated issue: #2
1 parent 1bf89d4 commit 70fdf90

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

__tests__/post-entrypoint.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { TaskcatArtifactManager } from "../src/post-entrypoint";
2+
import { readFileSync, writeFileSync } from "fs";
3+
import { sync } from "glob";
4+
5+
jest.mock("fs");
6+
jest.mock("glob");
7+
8+
describe("the maskAccountId() function", () => {
9+
const taskcatArtifactManager: TaskcatArtifactManager = new TaskcatArtifactManager();
10+
11+
const mockedReadFileSync = (readFileSync as unknown) as jest.MockedFunction<
12+
typeof readFileSync
13+
>;
14+
const mockedWriteFileSync = (writeFileSync as unknown) as jest.MockedFunction<
15+
typeof writeFileSync
16+
>;
17+
const mockedGlobSync = (sync as unknown) as jest.MockedFunction<typeof sync>;
18+
19+
it("should not modify the `tasckat_output/` files if the AWS account ID has not been printed in the logs", () => {
20+
expect.assertions(1);
21+
jest.clearAllMocks();
22+
23+
const filePath = "taskcat_outputs/";
24+
const fileContents = "abcd1234";
25+
const awsAccountId = "1234567890";
26+
27+
mockedGlobSync.mockReturnValue(["taskcat_outputs/test.txt"]);
28+
mockedReadFileSync.mockReturnValue(fileContents);
29+
30+
taskcatArtifactManager.maskAccountId(awsAccountId, filePath);
31+
32+
expect(mockedWriteFileSync).not.toHaveBeenCalled();
33+
});
34+
35+
it("should throw an exception if the AWS account ID has been passed as an empty string", () => {
36+
expect.assertions(2);
37+
jest.clearAllMocks();
38+
39+
const filePath = "taskcat_outputs/test.txt";
40+
const awsAccountId = "";
41+
42+
expect(() => {
43+
taskcatArtifactManager.maskAccountId(awsAccountId, filePath);
44+
}).toThrow(Error);
45+
46+
expect(mockedWriteFileSync).not.toHaveBeenCalled();
47+
});
48+
49+
it("should mask the AWS account ID from the `taskcat_outputs/` files if any references are found in the logs", () => {
50+
expect.assertions(1);
51+
jest.clearAllMocks();
52+
53+
const filePath = "taskcat_outputs/test.txt";
54+
const fileContents = "abcd1234 1234567890";
55+
const awsAccountId = "1234567890";
56+
57+
mockedGlobSync.mockReturnValue(["taskcat_outputs/test.txt"]);
58+
mockedReadFileSync.mockImplementation(path => {
59+
switch (path) {
60+
case filePath:
61+
return fileContents;
62+
}
63+
});
64+
65+
taskcatArtifactManager.maskAccountId(awsAccountId, "taskcat_outputs/");
66+
67+
expect(mockedWriteFileSync).toHaveBeenCalledWith(
68+
filePath,
69+
"abcd1234 ***",
70+
"utf-8"
71+
);
72+
});
73+
});

src/post-entrypoint.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ReplaceInFileConfig, sync } from "replace-in-file";
2+
3+
/**
4+
* Manages the artifacts generated by the taskcat GitHub Action.
5+
*/
6+
class TaskcatArtifactManager {
7+
/**
8+
* Masks the AWS account ID from the taskcat_output logs.
9+
*
10+
* @throws {@link Error} Thrown if the AWS account ID is an empty string.
11+
*
12+
* @param awsAccountId - the AWS account ID to mask in the logs.
13+
* @param filePath - the file path to the `taskcat_outputs` directory.
14+
*/
15+
public maskAccountId(awsAccountId: string, filePath: string): void {
16+
if (awsAccountId === "") {
17+
throw new Error();
18+
}
19+
20+
const replaceOptions: ReplaceInFileConfig = {
21+
files: filePath,
22+
from: awsAccountId,
23+
to: "***"
24+
};
25+
26+
sync(replaceOptions);
27+
}
28+
}
29+
30+
export { TaskcatArtifactManager };

0 commit comments

Comments
 (0)