Skip to content

Commit

Permalink
feat: updated RPC handlers taking multiple secret paths to use duplex…
Browse files Browse the repository at this point in the history
… streams
  • Loading branch information
aryanjassal committed Sep 20, 2024
1 parent d777f92 commit 39972fc
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/client/callers/vaultsSecretsGet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type VaultsSecretsGet from '../handlers/VaultsSecretsGet';
import { ServerCaller } from '@matrixai/rpc';
import { DuplexCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsGet>;

const vaultsSecretsGet = new ServerCaller<
const vaultsSecretsGet = new DuplexCaller<
CallerTypes['input'],
CallerTypes['output']
>();
Expand Down
4 changes: 2 additions & 2 deletions src/client/callers/vaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type VaultsSecretsRemove from '../handlers/VaultsSecretsRemove';
import { UnaryCaller } from '@matrixai/rpc';
import { ClientCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsRemove>;

const vaultsSecretsRemove = new UnaryCaller<
const vaultsSecretsRemove = new ClientCaller<
CallerTypes['input'],
CallerTypes['output']
>();
Expand Down
44 changes: 23 additions & 21 deletions src/client/handlers/VaultsSecretsGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,53 @@ import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
ContentMessage,
SecretManyPathMessage,
SecretIdentifierMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { ServerHandler } from '@matrixai/rpc';
import { DuplexHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as vaultOps from '../../vaults/VaultOps';

class VaultsSecretsGet extends ServerHandler<
class VaultsSecretsGet extends DuplexHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretManyPathMessage>,
ClientRPCRequestParams<SecretIdentifierMessage>,
ClientRPCResponseResult<ContentMessage>
> {
public async *handle(
input: ClientRPCRequestParams<SecretManyPathMessage>,
): AsyncGenerator<ClientRPCResponseResult<ContentMessage>, void, void> {
public handle = async function* (
input: AsyncIterable<ClientRPCRequestParams<SecretIdentifierMessage>>,
_cancel,
_meta,
ctx,
): AsyncGenerator<ClientRPCResponseResult<ContentMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { vaultManager, db } = this.container;
yield* db.withTransactionG(async function* (tran): AsyncGenerator<
ContentMessage,
void,
void
ClientRPCResponseResult<ContentMessage>
> {
if (ctx.signal.aborted) throw ctx.signal.reason;
// As we need to preserve the order of parameters, we need to loop over
// them individually, as grouping them would make them go out of order.
for (const [vaultName, secretName] of input.secretNames) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName);
for await (const secretIdentiferMessage of input) {
const { nameOrId, secretName } = secretIdentiferMessage;
const vaultIdFromName = await vaultManager.getVaultId(nameOrId, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined();
yield* vaultManager.withVaultsG(
const content: Buffer = await vaultManager.withVaults(
[vaultId],
async function* (vault): AsyncGenerator<ContentMessage, void, void> {
yield {
secretContent: (
await vaultOps.getSecret(vault, secretName)
).toString('binary'),
};
async (vault) => {
return await vaultOps.getSecret(vault, secretName);
},
tran,
);

yield { secretContent: content.toString('binary') };
}
});
}
};
}

export default VaultsSecretsGet;
31 changes: 23 additions & 8 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,46 @@ import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
SuccessMessage,
SecretRemoveMessage,
SecretIdentifierMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { UnaryHandler } from '@matrixai/rpc';
import { ClientHandler } 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<
class VaultsSecretsRemove extends ClientHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretRemoveMessage>,
ClientRPCRequestParams<SecretIdentifierMessage>,
ClientRPCResponseResult<SuccessMessage>
> {
public handle = async (
input: ClientRPCRequestParams<SecretRemoveMessage>,
input: AsyncIterable<ClientRPCRequestParams<SecretIdentifierMessage>>,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { vaultManager, db } = this.container;
// Create a record of secrets to be removed, grouped by vault names
const vaultGroups: Record<string, string[]> = {};
input.secretNames.forEach(([vaultName, secretName]) => {
const vaultGroups: Record<string, Array<string>> = {};
const secretNames: Array<[string, string]> = [];
let metadata: any = {};
let first = true;
for await (const secretRemoveMessage of input) {
if (first) metadata = secretRemoveMessage.metadata;
secretNames.push([
secretRemoveMessage.nameOrId,
secretRemoveMessage.secretName,
]);
first = false;
}
secretNames.forEach(([vaultName, secretName]) => {
if (vaultGroups[vaultName] == null) {
vaultGroups[vaultName] = [];
}
vaultGroups[vaultName].push(secretName);
});

await db.withTransactionF(async (tran) => {
for (const [vaultName, secretNames] of Object.entries(vaultGroups)) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
Expand All @@ -39,8 +51,11 @@ class VaultsSecretsRemove extends UnaryHandler<
await vaultManager.withVaults(
[vaultId],
async (vault) => {
// console.log('in here', metadata);
// console.log('options', metadata?.options);
// console.log('recursive', metadata?.options?.recursive);
await vaultOps.deleteSecret(vault, secretNames, {
recursive: input.options?.recursive,
recursive: metadata?.options?.recursive,
});
},
tran,
Expand Down
14 changes: 0 additions & 14 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,8 @@ type SecretPathMessage = {
secretName: string;
};

// The secrets must be in a format like:
// [ [vaultName, secretPath], [vaultName, secretPath ] ]
type SecretManyPathMessage = {
secretNames: Array<Array<string>>;
};

type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage;

type SecretRemoveMessage = SecretManyPathMessage & {
options?: {
recursive?: boolean;
};
};

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

0 comments on commit 39972fc

Please sign in to comment.