Skip to content

Commit

Permalink
feat: concatenates secrets from multiple vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 17, 2024
1 parent e44a713 commit af985a8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 152 deletions.
35 changes: 14 additions & 21 deletions src/client/handlers/VaultsSecretsGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
ContentMessage,
SecretCatMessage,
SecretManyPathMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { ServerHandler } from '@matrixai/rpc';
Expand All @@ -16,41 +16,34 @@ class VaultsSecretsGet extends ServerHandler<
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretCatMessage>,
ClientRPCRequestParams<SecretManyPathMessage>,
ClientRPCResponseResult<ContentMessage>
> {
public async *handle(
input: ClientRPCRequestParams<SecretCatMessage>,
input: ClientRPCRequestParams<SecretManyPathMessage>,
): AsyncGenerator<ClientRPCResponseResult<ContentMessage>, void, void> {
const { vaultManager, db } = this.container;

yield* db.withTransactionG(async function* (tran): AsyncGenerator<
ContentMessage,
void,
void
> {
const vaultIdFromName = await vaultManager.getVaultId(
input.nameOrId,
tran,
);
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
yield* vaultManager.withVaultsG(
[vaultId],
async function* (vault): AsyncGenerator<ContentMessage, void, void> {
for (const secretName of input.secretNames) {
for (const [vaultName, secretName] of input.secretNames) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName);
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined();
yield* vaultManager.withVaultsG(
[vaultId],
async function* (vault): AsyncGenerator<ContentMessage, void, void> {
yield {
secretContent: (
await vaultOps.getSecret(vault, secretName)
).toString('binary'),
};
}
},
tran,
);
},
tran,
);
}
});
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,20 @@ 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 = {
secretNames: Array<Array<string>>;
type SecretRemoveMessage = SecretManyPathMessage & {
options?: {
recursive?: boolean;
};
};

type SecretCatMessage = VaultIdentifierMessage & {
secretNames: Array<string>;
};

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

0 comments on commit af985a8

Please sign in to comment.