Skip to content

Commit

Permalink
feat: updating behavior of secrets get
Browse files Browse the repository at this point in the history
feat: concatenates secrets from multiple vaults

feat: updated RPC handlers taking multiple secret paths to use duplex streams

chore: updated tests

chore: updated metadata assignment

chore: simplified tests

chore: separated tests for deleting directories recursively
  • Loading branch information
aryanjassal committed Sep 23, 2024
1 parent 8f099a8 commit acf03b3
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 229 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 { UnaryCaller } from '@matrixai/rpc';
import { DuplexCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsGet>;

const vaultsSecretsGet = new UnaryCaller<
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
53 changes: 29 additions & 24 deletions src/client/handlers/VaultsSecretsGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,48 @@ import type {
SecretIdentifierMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { UnaryHandler } 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 UnaryHandler<
class VaultsSecretsGet extends DuplexHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretIdentifierMessage>,
ClientRPCResponseResult<ContentMessage>
> {
public handle = async (
input: ClientRPCRequestParams<SecretIdentifierMessage>,
): Promise<ClientRPCResponseResult<ContentMessage>> => {
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;
return 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();
yield* db.withTransactionG(async function* (tran): AsyncGenerator<
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 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();
const content: Buffer = await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vaultOps.getSecret(vault, secretName);
},
tran,
);

yield { secretContent: content.toString('binary') };
}
const secretContent = await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vaultOps.getSecret(vault, input.secretName);
},
tran,
);
return {
secretContent: secretContent.toString('binary'),
};
});
};
}
Expand Down
26 changes: 18 additions & 8 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@ 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 = undefined;
for await (const secretRemoveMessage of input) {
if (metadata == null) metadata = secretRemoveMessage.metadata ?? {};
secretNames.push([
secretRemoveMessage.nameOrId,
secretRemoveMessage.secretName,
]);
}
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 @@ -40,7 +50,7 @@ class VaultsSecretsRemove extends UnaryHandler<
[vaultId],
async (vault) => {
await vaultOps.deleteSecret(vault, secretNames, {
recursive: input.options?.recursive,
recursive: metadata?.options?.recursive,
});
},
tran,
Expand Down
8 changes: 0 additions & 8 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,6 @@ type SecretPathMessage = {

type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage;

type SecretRemoveMessage = {
secretNames: Array<Array<string>>;
options?: {
recursive?: boolean;
};
};

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

0 comments on commit acf03b3

Please sign in to comment.