Skip to content

Commit ba146ea

Browse files
author
Irene Alvarado
committed
Add boolean string to completely mask source string
1 parent 0a33cd6 commit ba146ea

4 files changed

Lines changed: 46 additions & 28 deletions

File tree

dist/index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ const CommonConfigSchema = z.object({
233233
const HTTPConfigSchema = z
234234
.object({
235235
http_url: z.string(),
236-
mask: z.string().optional()
236+
mask: z.string().optional() // string array of secrets or boolean
237237
})
238238
.merge(CommonConfigSchema);
239239
const SQLConfigSchema = z
@@ -254,7 +254,7 @@ function getConfig() {
254254
'postprocess',
255255
];
256256
keys.forEach(k => {
257-
const v = core.getInput(k);
257+
const v = core.getInput(k); // getInput always returns a string
258258
if (v) {
259259
raw[k] = v;
260260
}
@@ -451,19 +451,29 @@ async function run() {
451451
core.startGroup('Fetch data');
452452
let filename = '';
453453
let source;
454-
let sourceStripped = '';
454+
let shouldMask = false; // by default we don't mask the source
455+
let sourceMasked = '';
455456
if (config_1.isHTTPConfig(config)) {
456457
filename = await http_1.default(config);
457458
source = config.http_url;
458-
// if including a mask config then we strip out secrets from the http_url
459-
sourceStripped = source;
459+
// if including a mask config then we can strip out secrets from the http_url
460+
sourceMasked = source; // if no secrets to mask then this is just source
460461
if (config.mask) {
461-
core.info('Masking http url');
462-
const maskArray = JSON.parse(config.mask);
463-
maskArray.forEach((secretToMask) => {
464-
const regex = new RegExp(secretToMask, "g");
465-
sourceStripped = sourceStripped.replace(regex, "***");
466-
});
462+
if (config.mask === 'true' || config.mask === 'false') { // mask param is a string
463+
shouldMask = JSON.parse(config.mask); // convert to boolean
464+
}
465+
else {
466+
try {
467+
const maskArray = JSON.parse(config.mask);
468+
maskArray.forEach((secretToMask) => {
469+
const regex = new RegExp(secretToMask, "g");
470+
sourceMasked = sourceMasked.replace(regex, "***");
471+
});
472+
}
473+
catch (error) {
474+
core.setFailed('Mask param formatted incorrectly. It should be a string array OR a "true" or "false" string.');
475+
}
476+
}
467477
}
468478
}
469479
else if (config_1.isSQLConfig(config)) {
@@ -510,8 +520,8 @@ async function run() {
510520
core.debug(`git adding ${filename}…`);
511521
await exec_1.exec('git', ['add', filename]);
512522
const bytes = await git_1.diff(filename);
513-
// core.setOutput('delta_bytes', bytes)
514-
editedFiles.push({ name: filename, deltaBytes: bytes, source: sourceStripped });
523+
const source = shouldMask ? {} : { source: sourceMasked };
524+
editedFiles.push({ name: filename, deltaBytes: bytes, ...source });
515525
}
516526
core.endGroup();
517527
core.startGroup('Committing new data');

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type CommonConfig = z.infer<typeof CommonConfigSchema>
1313
const HTTPConfigSchema = z
1414
.object({
1515
http_url: z.string(),
16-
mask: z.string().optional()
16+
mask: z.string().optional() // string array of secrets or boolean
1717
})
1818
.merge(CommonConfigSchema)
1919
export type HTTPConfig = z.infer<typeof HTTPConfigSchema>
@@ -40,7 +40,7 @@ export function getConfig(): Config {
4040
'postprocess',
4141
]
4242
keys.forEach(k => {
43-
const v = core.getInput(k)
43+
const v = core.getInput(k) // getInput always returns a string
4444
if (v) {
4545
raw[k] = v
4646
}

src/main.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,28 @@ async function run(): Promise<void> {
2222
core.startGroup('Fetch data')
2323
let filename = ''
2424
let source
25-
let sourceStripped = ''
25+
let shouldMask = false // by default we don't mask the source
26+
let sourceMasked = ''
2627
if (isHTTPConfig(config)) {
2728
filename = await fetchHTTP(config)
2829
source = config.http_url
2930

30-
// if including a mask config then we strip out secrets from the http_url
31-
sourceStripped = source
31+
// if including a mask config then we can strip out secrets from the http_url
32+
sourceMasked = source // if no secrets to mask then this is just source
3233
if (config.mask) {
33-
core.info('Masking http url')
34-
35-
const maskArray: string[] = JSON.parse(config.mask)
36-
maskArray.forEach((secretToMask: string) => {
37-
const regex = new RegExp(secretToMask, "g")
38-
sourceStripped = sourceStripped.replace(regex, "***")
39-
})
34+
if (config.mask === 'true' || config.mask === 'false') { // mask param is a string
35+
shouldMask = JSON.parse(config.mask) // convert to boolean
36+
} else {
37+
try {
38+
const maskArray: string[] = JSON.parse(config.mask)
39+
maskArray.forEach((secretToMask: string) => {
40+
const regex = new RegExp(secretToMask, "g")
41+
sourceMasked = sourceMasked.replace(regex, "***")
42+
})
43+
} catch(error) {
44+
core.setFailed('Mask param formatted incorrectly. It should be a string array OR a "true" or "false" string.')
45+
}
46+
}
4047
}
4148
} else if (isSQLConfig(config)) {
4249
filename = await fetchSQL(config)
@@ -93,8 +100,9 @@ async function run(): Promise<void> {
93100
core.debug(`git adding ${filename}…`)
94101
await exec('git', ['add', filename])
95102
const bytes = await diff(filename)
96-
// core.setOutput('delta_bytes', bytes)
97-
editedFiles.push({ name: filename, deltaBytes: bytes, source: sourceStripped })
103+
104+
const source = shouldMask ? {} : { source: sourceMasked }
105+
editedFiles.push({ name: filename, deltaBytes: bytes, ...source })
98106
}
99107
core.endGroup()
100108

0 commit comments

Comments
 (0)