From 02e3049427bb36b1a0485741381ba1a607360fd4 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Tue, 10 Sep 2024 13:14:44 +1000 Subject: [PATCH] feat: adding secrets remove handler --- src/client/callers/index.ts | 3 ++ src/client/callers/vaultsSecretsRemove.ts | 12 +++++ src/client/handlers/VaultsSecretsRemove.ts | 51 ++++++++++++++++++++++ src/client/handlers/index.ts | 3 ++ src/client/types.ts | 10 +++++ 5 files changed, 79 insertions(+) create mode 100644 src/client/callers/vaultsSecretsRemove.ts create mode 100644 src/client/handlers/VaultsSecretsRemove.ts diff --git a/src/client/callers/index.ts b/src/client/callers/index.ts index d89931efd..b04b29a44 100644 --- a/src/client/callers/index.ts +++ b/src/client/callers/index.ts @@ -72,6 +72,7 @@ import vaultsSecretsMkdir from './vaultsSecretsMkdir'; import vaultsSecretsNew from './vaultsSecretsNew'; import vaultsSecretsNewDir from './vaultsSecretsNewDir'; import vaultsSecretsRename from './vaultsSecretsRename'; +import vaultsSecretsRemove from './vaultsSecretsRemove'; import vaultsSecretsStat from './vaultsSecretsStat'; import vaultsVersion from './vaultsVersion'; @@ -153,6 +154,7 @@ const clientManifest = { vaultsSecretsNew, vaultsSecretsNewDir, vaultsSecretsRename, + vaultsSecretsRemove, vaultsSecretsStat, vaultsVersion, }; @@ -233,6 +235,7 @@ export { vaultsSecretsNew, vaultsSecretsNewDir, vaultsSecretsRename, + vaultsSecretsRemove, vaultsSecretsStat, vaultsVersion, }; diff --git a/src/client/callers/vaultsSecretsRemove.ts b/src/client/callers/vaultsSecretsRemove.ts new file mode 100644 index 000000000..e546b43ac --- /dev/null +++ b/src/client/callers/vaultsSecretsRemove.ts @@ -0,0 +1,12 @@ +import type { HandlerTypes } from '@matrixai/rpc'; +import type VaultsSecretsRemove from '../handlers/VaultsSecretsRemove'; +import { UnaryCaller } from '@matrixai/rpc'; + +type CallerTypes = HandlerTypes; + +const vaultsSecretsRemove = new UnaryCaller< + CallerTypes['input'], + CallerTypes['output'] +>(); + +export default vaultsSecretsRemove; diff --git a/src/client/handlers/VaultsSecretsRemove.ts b/src/client/handlers/VaultsSecretsRemove.ts new file mode 100644 index 000000000..a49eb4191 --- /dev/null +++ b/src/client/handlers/VaultsSecretsRemove.ts @@ -0,0 +1,51 @@ +import type { DB } from '@matrixai/db'; +import type { + ClientRPCRequestParams, + ClientRPCResponseResult, + SuccessMessage, + SecretRemoveMessage, +} from '../types'; +import type VaultManager from '../../vaults/VaultManager'; +import { UnaryHandler } from '@matrixai/rpc'; +import * as vaultsUtils from '../../vaults/utils'; +import * as vaultsErrors from '../../vaults/errors'; +import * as vaultOps from '../../vaults/VaultOps'; + +class VaultsSecretsRemove extends UnaryHandler< + { + vaultManager: VaultManager; + db: DB; + }, + ClientRPCRequestParams, + ClientRPCResponseResult +> { + public handle = async ( + input: ClientRPCRequestParams, + ): Promise> => { + const { vaultManager, db } = this.container; + await db.withTransactionF(async (tran) => { + const vaultIdFromName = await vaultManager.getVaultId( + input.nameOrId, + tran, + ); + const vaultId = + vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId); + if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined(); + await vaultManager.withVaults( + [vaultId], + async (vault) => { + for (const secretName of input.secretNames) { + await vaultOps.deleteSecret(vault, secretName, { + recursive: input.recursive, + }); + } + }, + tran, + ); + }); + + return { success: true }; + }; +} + +export default VaultsSecretsRemove; diff --git a/src/client/handlers/index.ts b/src/client/handlers/index.ts index a6aebaceb..207c2c804 100644 --- a/src/client/handlers/index.ts +++ b/src/client/handlers/index.ts @@ -89,6 +89,7 @@ import VaultsSecretsMkdir from './VaultsSecretsMkdir'; import VaultsSecretsNew from './VaultsSecretsNew'; import VaultsSecretsNewDir from './VaultsSecretsNewDir'; import VaultsSecretsRename from './VaultsSecretsRename'; +import VaultsSecretsRemove from './VaultsSecretsRemove'; import VaultsSecretsStat from './VaultsSecretsStat'; import VaultsVersion from './VaultsVersion'; @@ -193,6 +194,7 @@ const serverManifest = (container: { vaultsSecretsNew: new VaultsSecretsNew(container), vaultsSecretsNewDir: new VaultsSecretsNewDir(container), vaultsSecretsRename: new VaultsSecretsRename(container), + VaultsSecretsRemove: new VaultsSecretsRemove(container), vaultsSecretsStat: new VaultsSecretsStat(container), vaultsVersion: new VaultsVersion(container), }; @@ -275,6 +277,7 @@ export { VaultsSecretsNew, VaultsSecretsNewDir, VaultsSecretsRename, + VaultsSecretsRemove, VaultsSecretsStat, VaultsVersion, }; diff --git a/src/client/types.ts b/src/client/types.ts index 0b125b18d..9b09a80bb 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -304,8 +304,16 @@ type SecretPathMessage = { secretName: string; }; +type SecretPathsMessage = { + secretNames: Array; +} + type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage; +type SecretRemoveMessage = VaultIdentifierMessage & SecretPathsMessage & { + recursive: boolean; +}; + // Contains binary content as a binary string 'toString('binary')' type ContentMessage = { secretContent: string; @@ -415,7 +423,9 @@ export type { VaultsVersionMessage, VaultsLatestVersionMessage, SecretPathMessage, + SecretPathsMessage, SecretIdentifierMessage, + SecretRemoveMessage, ContentMessage, SecretContentMessage, SecretMkdirMessage,