feat: rethink how key retrevial is structued and add e2e test
This commit is contained in:
59
action.js
59
action.js
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user