Skip to content

Commit

Permalink
feat: adding secrets remove handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 10, 2024
1 parent 8490111 commit 02e3049
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/client/callers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -153,6 +154,7 @@ const clientManifest = {
vaultsSecretsNew,
vaultsSecretsNewDir,
vaultsSecretsRename,
vaultsSecretsRemove,
vaultsSecretsStat,
vaultsVersion,
};
Expand Down Expand Up @@ -233,6 +235,7 @@ export {
vaultsSecretsNew,
vaultsSecretsNewDir,
vaultsSecretsRename,
vaultsSecretsRemove,
vaultsSecretsStat,
vaultsVersion,
};
12 changes: 12 additions & 0 deletions src/client/callers/vaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type VaultsSecretsRemove from '../handlers/VaultsSecretsRemove';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsRemove>;

const vaultsSecretsRemove = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default vaultsSecretsRemove;
51 changes: 51 additions & 0 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
@@ -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<SecretRemoveMessage>,
ClientRPCResponseResult<SuccessMessage>
> {
public handle = async (
input: ClientRPCRequestParams<SecretRemoveMessage>,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
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;
3 changes: 3 additions & 0 deletions src/client/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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),
};
Expand Down Expand Up @@ -275,6 +277,7 @@ export {
VaultsSecretsNew,
VaultsSecretsNewDir,
VaultsSecretsRename,
VaultsSecretsRemove,
VaultsSecretsStat,
VaultsVersion,
};
10 changes: 10 additions & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,16 @@ type SecretPathMessage = {
secretName: string;
};

type SecretPathsMessage = {
secretNames: Array<string>;
}

type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage;

type SecretRemoveMessage = VaultIdentifierMessage & SecretPathsMessage & {
recursive: boolean;
};

// Contains binary content as a binary string 'toString('binary')'
type ContentMessage = {
secretContent: string;
Expand Down Expand Up @@ -415,7 +423,9 @@ export type {
VaultsVersionMessage,
VaultsLatestVersionMessage,
SecretPathMessage,
SecretPathsMessage,
SecretIdentifierMessage,
SecretRemoveMessage,
ContentMessage,
SecretContentMessage,
SecretMkdirMessage,
Expand Down

0 comments on commit 02e3049

Please sign in to comment.