From b241180aa85ad81f4ee0dca9bf3c0429916a6a18 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sun, 11 May 2025 15:36:49 -0700 Subject: Significantly improve traceability and minor fixes. --- utils/logger.ts | 6 ++++++ utils/mod.ts | 1 + utils/run.ts | 2 +- utils/secret.ts | 27 ++++++++++++++++++++++----- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 utils/logger.ts (limited to 'utils') diff --git a/utils/logger.ts b/utils/logger.ts new file mode 100644 index 0000000..e36d249 --- /dev/null +++ b/utils/logger.ts @@ -0,0 +1,6 @@ +export const loggerWithPrefix = (prefixSupplier: () => string) => { + return { + log: (...args: unknown[]) => console.log(prefixSupplier(), ...args), + error: (...args: unknown[]) => console.error(prefixSupplier(), ...args), + }; +}; diff --git a/utils/mod.ts b/utils/mod.ts index 8c5dc45..4e907df 100644 --- a/utils/mod.ts +++ b/utils/mod.ts @@ -1,3 +1,4 @@ +export * from "./logger.ts"; export * from "./env.ts"; export * from "./run.ts"; export * from "./secret.ts"; diff --git a/utils/run.ts b/utils/run.ts index f3ce3d3..f06ef97 100644 --- a/utils/run.ts +++ b/utils/run.ts @@ -15,7 +15,7 @@ export const getStdout = async ( const stdoutText = new TextDecoder().decode(stdout); const stderrText = new TextDecoder().decode(stderr); - if (code !== 0) throw new Error(`Command failed: ${cmd}\n${stderrText}`); + if (code !== 0) throw new Error(`Command failed\n${stderrText}`); return stdoutText; }; diff --git a/utils/secret.ts b/utils/secret.ts index 8860998..eb2054b 100644 --- a/utils/secret.ts +++ b/utils/secret.ts @@ -1,5 +1,8 @@ -import { getRequiredEnv, getStdout } from "./mod.ts"; +import { getRequiredEnv, getStdout, loggerWithPrefix } from "./mod.ts"; +const logger = loggerWithPrefix(() => + `[${new Date().toISOString()}] [BitwardenSession]` +); export class BitwardenSession { private readonly sessionInitializer: Promise; @@ -8,14 +11,25 @@ export class BitwardenSession { this.sessionInitializer = getStdout( `bw config server ${server} --quiet`, - ).then(() => getStdout(`bw login --apikey --quiet`)) - .then(() => getStdout(`bw unlock --passwordenv BW_PASSWORD --raw`)) - .then((session) => session.trim()); + ) + .then(() => { + logger.log("Logging in via API"); + return getStdout(`bw login --apikey --quiet`); + }) + .then(() => { + logger.log("Unlocking vault in session"); + return getStdout(`bw unlock --passwordenv BW_PASSWORD --raw`); + }) + .then((session) => { + logger.log(`Session ${session}`); + return session.trim(); + }); } public async getItem( secretName: string, ): Promise { + logger.log(`Finding secret ${secretName}`); return await this.sessionInitializer.then((session) => getStdout(`bw list items`, { env: { @@ -26,6 +40,7 @@ export class BitwardenSession { items.find(({ name }: { name: string }) => name === secretName) ).then((item) => { if (!item) throw new Error("Could not find bitwarden item " + secretName); + logger.log(`Found secret: ${secretName}`); return item; }); } @@ -33,7 +48,9 @@ export class BitwardenSession { async close(): Promise { return await this.sessionInitializer.then((session) => getStdout(`bw lock`, { env: { BW_SESSION: session } }) - ).then(() => {}); + ).then(() => { + logger.log("Locked session"); + }); } } -- cgit v1.2.3-70-g09d2