Skip to content

Commit

Permalink
wip: working on adding unix-like ls for secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 6, 2024
1 parent 76b4cf2 commit 65537d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/client/handlers/VaultsSecretsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
SecretNameMessage,
SecretFilesList,
VaultIdentifierMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import { ServerHandler } from '@matrixai/rpc';
import { UnaryHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as vaultOps from '../../vaults/VaultOps';
import { fileTree } from '@/vaults';
import { ContentNode, TreeNode } from '@/vaults/types';

class VaultsSecretsList extends ServerHandler<
class VaultsSecretsList extends UnaryHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<VaultIdentifierMessage>,
ClientRPCResponseResult<SecretNameMessage>
ClientRPCResponseResult<SecretFilesList>
> {
public async *handle(
public handle = async (
input: ClientRPCRequestParams<VaultIdentifierMessage>,
_cancel,
_meta,
ctx,
): AsyncGenerator<ClientRPCResponseResult<SecretNameMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
): Promise<ClientRPCResponseResult<SecretFilesList>> => {
const { vaultManager, db } = this.container;
const secrets = await db.withTransactionF(async (tran) => {
return await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
input.nameOrId,
tran,
Expand All @@ -40,18 +37,41 @@ class VaultsSecretsList extends ServerHandler<
return await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vaultOps.listSecrets(vault);
const data = await vault.readF(async (fs) => {
const fileTreeGen = fileTree.globWalk({
fs,
basePath: vault.vaultDataDir, // NOTE: check if this is correct
yieldStats: true,
yieldRoot: false,
yieldFiles: true,
yieldParents: true,
yieldDirectories: true,
});
const data: Array<TreeNode | ContentNode | Uint8Array> = []
const parserTransform = fileTree.parserTransformStreamFactory();
const serializedStream = fileTree.serializerStreamFactory(
fs,
fileTreeGen,
false,
)
const outputStream = serializedStream.pipeThrough(parserTransform);
for await (const output of outputStream) {
data.push(output)
}
return data
});

const files = data
.filter((v) => v instanceof Uint8Array)
.map((v) => Buffer.from(v as Uint8Array).toString());
return {
secretFilesList: files,
};
},
tran,
);
});
for (const secret of secrets) {
if (ctx.signal.aborted) throw ctx.signal.reason;
yield {
secretName: secret,
};
}
}
};
}

export default VaultsSecretsList;
7 changes: 7 additions & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ type VaultsLatestVersionMessage = {

// Secrets

type SecretFilesList = {
secretFilesList: Array<string>;
}

// NOTE: we used to use SecretNameMessage before. do we need to keep it? look into this.

type SecretNameMessage = {
secretName: string;
};
Expand Down Expand Up @@ -410,6 +416,7 @@ export type {
VaultsScanMessage,
VaultsVersionMessage,
VaultsLatestVersionMessage,
SecretFilesList,
SecretNameMessage,
SecretIdentifierMessage,
ContentMessage,
Expand Down

0 comments on commit 65537d7

Please sign in to comment.