diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/logger.ts | 6 | ||||
-rw-r--r-- | utils/mod.ts | 1 | ||||
-rw-r--r-- | utils/run.ts | 2 | ||||
-rw-r--r-- | utils/secret.ts | 27 |
4 files changed, 30 insertions, 6 deletions
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<string>; @@ -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<T extends LoginItem | SecureNote>( secretName: string, ): Promise<T> { + 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<void> { return await this.sessionInitializer.then((session) => getStdout(`bw lock`, { env: { BW_SESSION: session } }) - ).then(() => {}); + ).then(() => { + logger.log("Locked session"); + }); } } |