Skip to content

Instantly share code, notes, and snippets.

@Westixy
Created April 30, 2021 10:32
Show Gist options
  • Save Westixy/51239f2e59c4310fcec48218128a6946 to your computer and use it in GitHub Desktop.
Save Westixy/51239f2e59c4310fcec48218128a6946 to your computer and use it in GitHub Desktop.
Vault tree / find script in js
# Usage: node vtree.mjs <backend>[/path] [MAX_CALL_TO_VAULT_PER_CICLE]
# example: node vtree.mjs kv-store/some-subfolder | grep mysecret
import { spawn } from "child_process";
const BASE_PATH = process.argv[2];
const MAX_PER_CICLE = process.argv[3] || 100;
const listVault = (basePath) =>
new Promise((res, rej) => {
const result = spawn("vault", ["kv", "list", "-format=json", basePath]);
let buf = "";
result.stdout.on("data", (data) => (buf += data));
result.stderr.on("data", (data) => {
process.stderr.write(`error: ${data}`);
});
result.on("close", (code) => {
if (code === 0) {
res(JSON.parse(buf).map((path) => `${basePath}${path}`));
} else {
rej(code);
}
});
});
const basePath = /\/$/.test(BASE_PATH) ? BASE_PATH : `${BASE_PATH}/`;
const rlist = await listVault(basePath).catch(console.error);
let list = rlist.filter((path) => /\/$/.test(path));
let secrets = rlist.filter((path) => !/\/$/.test(path));
while (list.length != 0) {
const paths = list
.splice(0, MAX_PER_CICLE)
.filter((path) => /\/$/.test(path));
const results = (
await Promise.all(paths.map((path) => listVault(path)))
).flat();
const newSecrets = results.filter((path) => !/\/$/.test(path));
const newDirs = results.filter((path) => /\/$/.test(path));
secrets.map((s) => console.log(s));
secrets.push(...newSecrets);
list.push(...newDirs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment