-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
import fs from "fs";
const client = new SecretManagerServiceClient();
async function getSecret(smPath) {
// smPath = sm://projects/${gcpSecretManagerId}/secrets/${secretVariableId}
const smName = smPath.replace("sm://", "");
return await client.accessSecretVersion({
name: `${smName}/versions/latest`,
});
}
async function replaceAsync(str, regex, asyncFn) {
const promises = [];
str.replace(regex, (match, ...args) => {
const promise = asyncFn(match, ...args);
promises.push(promise);
});
const data = await Promise.all(promises);
return str.replace(regex, () => data.shift());
}
async function mainAsync() {
const inputArgIndex = process.argv.indexOf("-i");
const outputArgIndex = process.argv.indexOf("-o");
const inputFileName = process.argv[inputArgIndex + 1];
const outputFileName = process.argv[outputArgIndex + 1];
const inputFile = fs.readFileSync(inputFileName, "utf8");
const outputFile = await replaceAsync(
inputFile,
/sm:\/\/[^"]+/g,
async (smPath) => {
const [secret] = await getSecret(smPath);
return secret.payload.data.toString();
}
);
fs.writeFileSync(outputFileName, outputFile);
}
function main() {
mainAsync()
.catch((err) => {
console.error("Command Failed. Error:"+err);
});
}
main();