2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-03-07 22:44:57 +00:00
|
|
|
/* eslint-env node */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const execa = require('execa');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a vault keys file that can be imported in other scripts, that includes the unseal keys and the root token.
|
|
|
|
* @param unsealKeys an array of unseal keys, must contain at least one key
|
|
|
|
* @param rootToken the root token
|
|
|
|
* @param filePath optional file path, if not provided the default path of <cwd>/tests/helpers/vault-keys.js
|
|
|
|
* will be used.
|
|
|
|
*/
|
|
|
|
function writeKeysFile(unsealKeys, rootToken, filePath) {
|
|
|
|
if (filePath === undefined) {
|
|
|
|
filePath = path.join(process.cwd(), 'tests/helpers/vault-keys.js');
|
|
|
|
}
|
2022-11-09 23:15:31 +00:00
|
|
|
const keys = {};
|
2022-03-07 22:44:57 +00:00
|
|
|
keys.unsealKeys = unsealKeys;
|
|
|
|
keys.rootToken = rootToken;
|
|
|
|
|
|
|
|
fs.writeFile(filePath, `export default ${JSON.stringify(keys, null, 2)}`, (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the provided command and pipes the processes stdout and stderr to the terminal. Upon completion with
|
|
|
|
* success or error the child process will be cleaned up.
|
|
|
|
* @param command some command to run
|
|
|
|
* @param args some arguments for the command to run
|
|
|
|
* @param shareStd if true the sub process created by the command will share the stdout and stderr of the parent
|
|
|
|
* process
|
|
|
|
* @returns {*} The child_process for the executed command which is also a Promise.
|
|
|
|
*/
|
|
|
|
function run(command, args = [], shareStd = true) {
|
|
|
|
console.log(chalk.dim('$ ' + command + ' ' + args.join(' ')));
|
|
|
|
// cleanup means that execa will handle stopping the subprocess
|
|
|
|
// inherit all of the stdin/out/err so that testem still works as if you were running it directly
|
|
|
|
if (shareStd) {
|
|
|
|
return execa(command, args, { cleanup: true, stdin: 'inherit', stdout: 'inherit', stderr: 'inherit' });
|
|
|
|
}
|
2022-11-09 23:15:31 +00:00
|
|
|
const p = execa(command, args, { cleanup: true });
|
2022-03-07 22:44:57 +00:00
|
|
|
p.stdout.pipe(process.stdout);
|
|
|
|
p.stderr.pipe(process.stderr);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
writeKeysFile: writeKeysFile,
|
|
|
|
run: run,
|
|
|
|
};
|