5
0
Fork 0
mirror of https://github.com/hashicorp/vault-action.git synced 2025-11-07 07:06:56 +00:00
vault-action/scripts/parse.js
2023-07-05 12:30:19 -05:00

43 lines
1.1 KiB
JavaScript

// This script is used by the e2e tests in build.yml to test that we can
// successfully parse JSON string data into JS objects
try {
let inputs = [
process.env.JSONSTRING,
process.env.JSONSTRINGMULTILINE,
];
let names = [
"test-json-string",
"test-json-string-multiline",
];
let i = 0;
inputs.forEach(input => {
console.log(`processing: ${names[i]}`)
i++;
input = (input || '').trim();
if (!input) {
throw new Error(`missing input`);
}
// If the string doesn't start with a JSON object character, it is probably
// base64-encoded.
if (!input.startsWith('{')) {
let str = input.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4) str += '=';
input = Buffer.from(str, 'base64').toString('utf8');
}
try {
const creds = JSON.parse(input);
console.log('success!')
return creds;
} catch (err) {
throw new Error(`error parsing: ${err}`);
}
})
} catch (error) {
throw new Error(`error in parse.js: ${err}`);
}