Skip to content

Commit

Permalink
feat: added 'VaultsSecretsWriteFile'
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 27, 2024
1 parent 387e2e3 commit a859295
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/client/handlers/VaultsSecretsWriteFile.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,
SecretContentMessage,
SuccessMessage,
} 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 VaultsSecretsWriteFile extends UnaryHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretContentMessage>,
ClientRPCResponseResult<SuccessMessage>
> {
public handle = async (
input: ClientRPCRequestParams<SecretContentMessage>,
): 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();
}
const secretContent = Buffer.from(input.secretContent, 'binary');
await vaultManager.withVaults(
[vaultId],
async (vault) => {
await vaultOps.updateSecret(vault, input.secretName, secretContent);
},
tran,
);
});
return {
success: true,
};
};
}

export default VaultsSecretsWriteFile;

0 comments on commit a859295

Please sign in to comment.