diff options
Diffstat (limited to 'worker/secret.ts')
-rw-r--r-- | worker/secret.ts | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/worker/secret.ts b/worker/secret.ts index e3edb2d..e533b16 100644 --- a/worker/secret.ts +++ b/worker/secret.ts @@ -10,18 +10,21 @@ import { } from '@emprespresso/pengueno'; // -- <ISecret> -- -export interface LoginItem { +export interface SecretItem { + name: string; +} + +export interface LoginItem extends SecretItem { login: { username: string; password: string; }; } -export interface SecureNote { +export interface SecureNote extends SecretItem { notes: string; } -export type SecretItem = LoginItem | SecureNote; export interface IVault<TClient, TKey, TItemId> { unlock: (client: TClient) => Promise<IEither<Error, TKey>>; lock: (client: TClient, key: TKey) => Promise<IEither<Error, TKey>>; @@ -30,7 +33,7 @@ export interface IVault<TClient, TKey, TItemId> { } // -- </ISecret> -- -// -- <IVault> -- +// -- <Vault> -- type TClient = ITraceable<unknown, LogMetricTraceSupplier>; type TKey = string; type TItemId = string; @@ -38,9 +41,9 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { constructor(private readonly config: BitwardenConfig) {} public unlock(client: TClient) { - return client + const authed = client .move(this.config) - .bimap(TraceUtil.withMetricTrace(Bitwarden.loginMetric)) + .flatMap(TraceUtil.withMetricTrace(Bitwarden.loginMetric)) .flatMap((tConfig) => tConfig.move(`bw config server ${tConfig.get().server}`).map(getStdout)) .map(async (tEitherWithConfig) => { const eitherWithConfig = await tEitherWithConfig.get(); @@ -49,12 +52,9 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { tEitherWithConfig.move('bw login --apikey --quiet').map(getStdout).get(), ); }) - .peek(async (tEitherWithAuthd) => { - const eitherWithAuthd = await tEitherWithAuthd.get(); - return tEitherWithAuthd.trace.trace( - eitherWithAuthd.fold(({ isLeft }) => Bitwarden.loginMetric[isLeft ? 'failure' : 'success']), - ); - }) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(Bitwarden.loginMetric))); + const unlocked = authed + .flatMap(TraceUtil.withMetricTrace(Bitwarden.unlockVaultMetric)) .map(async (tEitherWithAuthd) => { const eitherWithAuthd = await tEitherWithAuthd.get(); tEitherWithAuthd.trace.trace('unlocking the secret vault~ (◕ᴗ◕✿)'); @@ -62,19 +62,14 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { tEitherWithAuthd.move('bw unlock --passwordenv BW_PASSWORD --raw').map(getStdout).get(), ); }) - .peek(async (tEitherWithSession) => { - const eitherWithAuthd = await tEitherWithSession.get(); - return tEitherWithSession.trace.trace( - eitherWithAuthd.fold(({ isLeft }) => Bitwarden.unlockVaultMetric[isLeft ? 'failure' : 'success']), - ); - }) - .get(); + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(Bitwarden.unlockVaultMetric))); + return unlocked.get(); } public fetchSecret<T extends SecretItem>(client: TClient, key: string, item: string): Promise<IEither<Error, T>> { return client .move(key) - .bimap(TraceUtil.withMetricTrace(Bitwarden.fetchSecretMetric)) + .flatMap(TraceUtil.withMetricTrace(Bitwarden.fetchSecretMetric)) .peek((tSession) => tSession.trace.trace(`looking for your secret ${item} (⑅˘꒳˘)`)) .flatMap((tSession) => tSession.move('bw list items').map((listCmd) => @@ -88,8 +83,7 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { tEitherItemsJson .get() .flatMap( - (itemsJson): IEither<Error, Array<T & { name: string }>> => - Either.fromFailable(() => JSON.parse(itemsJson)), + (itemsJson): IEither<Error, Array<T>> => Either.fromFailable(() => JSON.parse(itemsJson)), ) .flatMap((itemsList): IEither<Error, T> => { const secret = itemsList.find(({ name }) => name === item); @@ -100,19 +94,14 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { }), ), ) - .peek(async (tEitherWithSecret) => { - const eitherWithSecret = await tEitherWithSecret.get(); - return tEitherWithSecret.trace.trace( - eitherWithSecret.fold(({ isLeft }) => Bitwarden.fetchSecretMetric[isLeft ? 'failure' : 'success']), - ); - }) + .flatMapAsync(TraceUtil.promiseify(TraceUtil.traceResultingEither(Bitwarden.fetchSecretMetric))) .get(); } public lock(client: TClient, key: TKey) { return client .move(key) - .bimap(TraceUtil.withMetricTrace(Bitwarden.lockVaultMetric)) + .flatMap(TraceUtil.withMetricTrace(Bitwarden.lockVaultMetric)) .peek((tSession) => tSession.trace.trace(`taking care of locking the vault :3`)) .flatMap((tSession) => tSession.move('bw lock').map((lockCmd) => @@ -121,14 +110,14 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { }), ), ) - .peek(async (tEitherWithLocked) => { - const eitherWithLocked = await tEitherWithLocked.get(); - return eitherWithLocked.fold(({ isLeft }) => { - tEitherWithLocked.trace.trace(Bitwarden.lockVaultMetric[isLeft ? 'failure' : 'success']); - if (isLeft) return; - tEitherWithLocked.trace.trace('all locked up and secure now~ (。•̀ᴗ-)✧'); - }); - }) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(Bitwarden.lockVaultMetric))) + .peek( + TraceUtil.promiseify((tEitherWithLocked) => + tEitherWithLocked + .get() + .mapRight(() => tEitherWithLocked.trace.trace('all locked up and secure now~ (。•̀ᴗ-)✧')), + ), + ) .get(); } @@ -142,10 +131,10 @@ export class Bitwarden implements IVault<TClient, TKey, TItemId> { ); } - private static loginMetric = Metric.fromName('Bitwarden.login'); - private static unlockVaultMetric = Metric.fromName('Bitwarden.unlockVault'); - private static fetchSecretMetric = Metric.fromName('Bitwarden.fetchSecret'); - private static lockVaultMetric = Metric.fromName('Bitwarden.lock'); + private static loginMetric = Metric.fromName('Bitwarden.login').asResult(); + private static unlockVaultMetric = Metric.fromName('Bitwarden.unlockVault').asResult(); + private static fetchSecretMetric = Metric.fromName('Bitwarden.fetchSecret').asResult(); + private static lockVaultMetric = Metric.fromName('Bitwarden.lock').asResult(); } export interface BitwardenConfig { @@ -153,4 +142,4 @@ export interface BitwardenConfig { secret: string; clientId: string; } -// -- </IVault> -- +// -- </Vault> -- |