make output more forgiving and shore up selectors

This commit is contained in:
Richard Simpson 2020-04-11 23:49:29 -05:00
parent 5099ea65b2
commit fc73c8c906
5 changed files with 88 additions and 31 deletions

View file

@ -136,18 +136,14 @@ function parseSecretsInput(secretsInput) {
/** @type {any} */
const selectorAst = jsonata(selector).ast();
if (selectorAst.type !== "path") {
throw Error(`Invalid path selector.`);
}
if ((selectorAst.steps > 1 || selectorAst.steps[0].stages) && !outputVarName) {
if ((selectorAst.type !== "path" || selectorAst.steps[0].stages) && !outputVarName) {
throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`);
}
let envVarName = outputVarName;
if (!outputVarName) {
outputVarName = selector;
envVarName = normalizeOutputKey(outputVarName);
outputVarName = normalizeOutputKey(selector);
envVarName = normalizeOutputKey(selector, true);
}
output.push({
@ -161,11 +157,17 @@ function parseSecretsInput(secretsInput) {
}
/**
* Replaces any forward-slash characters to
* Replaces any dot chars to __ and removes non-ascii charts
* @param {string} dataKey
* @param {boolean=} isEnvVar
*/
function normalizeOutputKey(dataKey) {
return dataKey.replace('/', '__').replace('.', '__').replace(/[^\w-]/, '').toUpperCase();
function normalizeOutputKey(dataKey, isEnvVar = false) {
let outputKey = dataKey
.replace('.', '__').replace(/[^\p{L}\p{N}_-]/gu, '');
if (isEnvVar) {
outputKey = outputKey.toUpperCase();
}
return outputKey;
}
/**

View file

@ -87,7 +87,7 @@ describe('parseSecretsInput', () => {
outputVarName: 'SOME_C',
envVarName: 'SOME_C',
});
})
});
});
describe('parseHeaders', () => {
@ -190,6 +190,18 @@ describe('exportSecrets', () => {
expect(core.setOutput).toBeCalledWith('key', '1');
});
it('intl secret retrieval', async () => {
mockInput('测试 测试');
mockVaultData({
测试: 1
});
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('测试', '1');
expect(core.setOutput).toBeCalledWith('测试', '1');
});
it('mapped secret retrieval', async () => {
mockInput('test key|TEST_NAME');
mockVaultData({
@ -233,4 +245,16 @@ describe('exportSecrets', () => {
expect(core.exportVariable).toBeCalledWith('KEY', '1');
expect(core.setOutput).toBeCalledWith('key', '1');
});
it('nested secret retrieval', async () => {
mockInput('test key.value');
mockVaultData({
key: { value: 1 }
});
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('KEY__VALUE', '1');
expect(core.setOutput).toBeCalledWith('key__value', '1');
});
});

View file

@ -55,11 +55,15 @@ async function getSecrets(secretRequests, client) {
* @param {string} selector
*/
function selectData(data, selector) {
let result = JSON.stringify(jsonata(selector).evaluate(data));
const ata = jsonata(selector);
let result = JSON.stringify(ata.evaluate(data));
// Compat for custom engines
if (!result && !selector.includes('.') && selector !== 'data' && 'data' in data) {
if (!result && ata.ast().type === "path" && ata.ast()['steps'].length === 1 && selector !== 'data' && 'data' in data) {
result = JSON.stringify(jsonata(`data.${selector}`).evaluate(data));
} else if (!result) {
throw Error(`Unable to retrieve result for ${selector}. No match data was found. Double check your Key or Selector.`);
}
if (result.startsWith(`"`)) {
result = result.substring(1, result.length - 1);
}