Skip to content

Commit

Permalink
chore: handles removing secrets of multiple vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 13, 2024
1 parent 04f0df0 commit d3e7b7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
39 changes: 23 additions & 16 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,29 @@ class VaultsSecretsRemove extends UnaryHandler<
): 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) => {
await vaultOps.deleteSecret(vault, input.secretNames, {
recursive: input.options?.recursive,
});
},
tran,
);
// Create a record of secrets to be removed grouped by vault names
const vaultGroups: Record<string, string[]> = {};
input.secretNames.forEach(([vaultName, secretName]) => {
if (vaultGroups[vaultName] == null) {
vaultGroups[vaultName] = [];
}
vaultGroups[vaultName].push(secretName);
});
// Deleting grouped secrets per vault
for (const [vaultName, secretName] of Object.entries(vaultGroups)) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName);
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined();
await vaultManager.withVaults(
[vaultId],
async (vault) => {
await vaultOps.deleteSecret(vault, secretName, {
recursive: input.options?.recursive,
});
},
tran,
);
}
});

return { success: true };
Expand Down
4 changes: 2 additions & 2 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ type SecretPathMessage = {

type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage;

type SecretRemoveMessage = VaultIdentifierMessage & {
secretNames: Array<string>;
type SecretRemoveMessage = {
secretNames: Array<Array<string>>;
options?: {
recursive?: boolean;
};
Expand Down
19 changes: 8 additions & 11 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1470,8 +1470,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
expect(secretContent).toStrictEqual(secret);
// Delete secret
const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({
nameOrId: vaultIdEncoded,
secretNames: [secret],
secretNames: [[vaultIdEncoded, secret]],
});
expect(deleteResponse.success).toBeTruthy();
// Check secret was deleted
Expand Down Expand Up @@ -1512,8 +1511,10 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
expect(getResponse2.secretContent).toStrictEqual(secret2);
// Delete secret
const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({
nameOrId: vaultIdEncoded,
secretNames: [secret1, secret2],
secretNames: [
[vaultIdEncoded, secret1],
[vaultIdEncoded, secret2],
],
});
expect(deleteResponse.success).toBeTruthy();
// Check secret was deleted
Expand Down Expand Up @@ -1555,17 +1556,13 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
// Delete secret
await testsUtils.expectRemoteError(
rpcClient.methods.vaultsSecretsRemove({
nameOrId: vaultsIdEncoded,
secretNames: [secretDirName],
secretNames: [[vaultsIdEncoded, secretDirName]],
}),
vaultsErrors.ErrorVaultsRecursive,
);
const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({
nameOrId: vaultsIdEncoded,
secretNames: [secretDirName],
options: {
recursive: true,
},
secretNames: [[vaultsIdEncoded, secretDirName]],
options: { recursive: true },
});
expect(deleteResponse.success).toBeTruthy();
// Check secret was deleted
Expand Down

0 comments on commit d3e7b7e

Please sign in to comment.