summaryrefslogtreecommitdiff
path: root/utils/secret.ts
diff options
context:
space:
mode:
Diffstat (limited to 'utils/secret.ts')
-rw-r--r--utils/secret.ts27
1 files changed, 22 insertions, 5 deletions
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");
+ });
}
}