feat: rethink how key retrevial is structued and add e2e test

This commit is contained in:
Richard Simpson
2019-09-20 16:59:05 -05:00
parent 9a7f009394
commit 0b17727b1c
10 changed files with 191 additions and 179 deletions

View File

@@ -2,7 +2,6 @@
// @ts-ignore
const core = require('@actions/core');
const got = require('got');
const jq = require('jsonpath');
async function exportSecrets() {
const vaultUrl = core.getInput('vaultUrl', { required: true });
@@ -12,21 +11,19 @@ async function exportSecrets() {
const keys = parseKeysInput(keysInput);
for (const key of keys) {
const [keyPath, { name, query }] = key;
const result = await got(`${vaultUrl}/secret/data/${keyPath}`, {
const [keyPath, { outputName, dataKey }] = key;
const result = await got(`${vaultUrl}/v1/secret/data/${keyPath}`, {
headers: {
'Accept': 'application/json',
'X-Vault-Token': vaultToken
}
});
const parsedResponse = JSON.parse(result.body);
let value = parsedResponse.data;
if (query) {
value = jq.value(value, query);
}
core.exportSecret(name, value);
core.debug(`${keyPath} => ${name}`);
const vaultKeyData = parsedResponse.data;
const versionData = vaultKeyData.data;
const value = versionData[dataKey];
core.exportSecret(outputName, value);
core.debug(`${keyPath} => ${outputName}`);
}
};
@@ -41,51 +38,41 @@ function parseKeysInput(keys) {
.map(key => key.trim())
.filter(key => key.length !== 0);
/** @type {Map<string, { name: string; query: string; }>} */
/** @type {Map<string, { outputName: string; dataKey: string; }>} */
const output = new Map();
for (const keyPair of keyPairs) {
let path = keyPair;
let mappedName = null;
let query = null;
let outputName = null;
const renameSigilIndex = keyPair.lastIndexOf('|');
if (renameSigilIndex > -1) {
path = keyPair.substring(0, renameSigilIndex).trim();
mappedName = keyPair.substring(renameSigilIndex + 1).trim();
outputName = keyPair.substring(renameSigilIndex + 1).trim();
if (mappedName.length < 1) {
if (outputName.length < 1) {
throw Error(`You must provide a value when mapping a secret to a name. Input: "${keyPair}"`);
}
}
const pathPair = path;
const querySigilIndex = pathPair.indexOf('>');
if (querySigilIndex > -1) {
path = pathPair.substring(0, querySigilIndex).trim();
query = pathPair.substring(querySigilIndex + 1).trim();
const pathParts = path
.split(/\s+/)
.map(part => part.trim())
.filter(part => part.length !== 0);
try {
const expression = jq.parse(query);
if (expression.length === 0) {
throw Error(`Invalid query expression provided "${query}" from "${keyPair}".`);
}
} catch (_) {
throw Error(`Invalid query expression provided "${query}" from "${keyPair}".`);
}
if (pathParts.length !== 2) {
throw Error(`You must provide a valid path and key. Input: "${keyPair}"`)
}
if (path.length === 0) {
throw Error(`You must provide a valid path. Input: "${keyPair}"`)
}
const [secretPath, dataKey] = pathParts;
// If we're not using a mapped name, normalize the key path into a variable name.
if (!mappedName) {
mappedName = normalizeKeyName(path);
if (!outputName) {
outputName = normalizeKeyName(dataKey);
}
output.set(path, {
name: mappedName,
query
output.set(secretPath, {
outputName,
dataKey
});
}
return output;