Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented wildcard selector (based on #238) #488

Merged
merged 18 commits into from
Sep 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored out selectAndAppendResults
keattang committed Sep 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 4c238e24530e2ea745bc492122b7d6f8a417c8b0
65 changes: 47 additions & 18 deletions src/secrets.js
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ const { normalizeOutputKey } = require('./utils');
*/
async function getSecrets(secretRequests, client) {
const responseCache = new Map();
const results = [];
let results = [];

for (const secretRequest of secretRequests) {
let { path, selector } = secretRequest;
@@ -48,8 +48,9 @@ async function getSecrets(secretRequests, client) {
}
}

body = JSON.parse(body);

if (selector === wildcard) {
body = JSON.parse(body);
let keys = body.data;
if (body.data["data"] != undefined) {
keys = keys.data;
@@ -91,22 +92,13 @@ async function getSecrets(secretRequests, client) {

}
else {
if (!selector.match(/.*[\.].*/)) {
selector = '"' + selector + '"'
}
selector = "data." + selector
body = JSON.parse(body)
if (body.data["data"] != undefined) {
selector = "data." + selector
}


const value = await selectData(body, selector);
results.push({
request: secretRequest,
value,
cachedResponse
});
results = await selectAndAppendResults(
selector,
body,
cachedResponse,
secretRequest,
results
);
}
}
return results;
@@ -134,6 +126,43 @@ async function selectData(data, selector) {
return result;
}

/**
* Uses selectData with the selector to get the value and then appends it to the
* results. Returns a new array with all of the results.
* @param {string} selector
* @param {object} body
* @param {object} cachedResponse
* @param {TRequest} secretRequest
* @param {SecretResponse<TRequest>[]} results
* @return {Promise<SecretResponse<TRequest>[]>}
*/
const selectAndAppendResults = async (
selector,
body,
cachedResponse,
secretRequest,
results
) => {
if (!selector.match(/.*[\.].*/)) {
selector = '"' + selector + '"';
}
selector = "data." + selector;

if (body.data["data"] != undefined) {
selector = "data." + selector;
}

const value = await selectData(body, selector);
return [
...results,
{
request: secretRequest,
value,
cachedResponse,
},
];
};

module.exports = {
getSecrets,
selectData