Skip to content

Commit

Permalink
feat: removed Secrets/CommandGet and added tests
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
aryanjassal committed Sep 17, 2024
1 parent 77800f9 commit 2bf097a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 157 deletions.
11 changes: 6 additions & 5 deletions src/secrets/CommandCat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class CommandGet extends CommandPolykey {
this.name('cat');
this.description('Retrieve one or more Secrets from a Vault');
this.argument(
'<secretPath>',
'<secretPath...>',
'Path to a secret to be retrieved, specified as <vaultName>:<directoryPath>',
binParsers.parseSecretPathValue,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
this.action(async (secretPath, options) => {
this.action(async (secretPaths, options) => {
secretPaths = secretPaths.map((path: string) =>
binParsers.parseSecretPathValue(path),
);
const { default: PolykeyClient } = await import(
'polykey/dist/PolykeyClient'
);
Expand Down Expand Up @@ -53,8 +55,7 @@ class CommandGet extends CommandPolykey {
(auth) =>
pkClient.rpcClient.methods.vaultsSecretsGet({
metadata: auth,
nameOrId: secretPath[0],
secretNames: [secretPath[1]],
secretNames: secretPaths,
}),
meta,
);
Expand Down
86 changes: 0 additions & 86 deletions src/secrets/CommandGet.ts

This file was deleted.

72 changes: 67 additions & 5 deletions tests/secrets/cat.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VaultName } from 'polykey/dist/vaults/types';
import type { VaultId, VaultName } from 'polykey/dist/vaults/types';
import path from 'path';
import fs from 'fs';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
Expand All @@ -7,7 +7,7 @@ import { vaultOps } from 'polykey/dist/vaults';
import * as keysUtils from 'polykey/dist/keys/utils';
import * as testUtils from '../utils';

describe('commandGetSecret', () => {
describe('commandCatSecret', () => {
const password = 'password';
const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]);
let dataDir: string;
Expand Down Expand Up @@ -41,21 +41,83 @@ describe('commandGetSecret', () => {
});
});

test('should retrieve secrets', async () => {
test('should retrieve a secret', async () => {
const vaultName = 'Vault3' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, 'MySecret', 'this is the secret');
});

command = ['secrets', 'get', '-np', dataDir, `${vaultName}:MySecret`];
command = ['secrets', 'cat', '-np', dataDir, `${vaultName}:MySecret`];

const result = await testUtils.pkStdio([...command], {
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
expect(result.stdout).toBe('this is the secret');
expect(result.exitCode).toBe(0);
});

test('should concatenate multiple secrets', async () => {
const vaultName = 'Vault3' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretNames = ['secret-name1', 'secret-name2'];

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
for (const secretName of secretNames) {
await vaultOps.addSecret(vault, secretName, secretName);
}
});

const secretPaths = secretNames.map((v) => `${vaultName}:${v}`);
command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
expect(result.stdout).toBe(secretNames.join(''));
expect(result.exitCode).toBe(0);
});

test('should concatenate secrets from multiple vaults', async () => {
const vaultNames = ['Vault3-1', 'Vault3-2'];
const vaultIds: Array<VaultId> = [];
for (const vaultName of vaultNames) {
vaultIds.push(await polykeyAgent.vaultManager.createVault(vaultName));
}
const secretNames = ['secret-name1', 'secret-name2', 'secret-name3'];
const secretVaultNames = [
[0, secretNames[0]],
[1, secretNames[1]],
[0, secretNames[2]],
];

// Add secrets to vault specified by vault index
await polykeyAgent.vaultManager.withVaults(vaultIds, async (...vaults) => {
for (const [vaultIndex, secretName] of secretVaultNames) {
await vaultOps.addSecret(
vaults[vaultIndex],
secretName as string,
secretName as string,
);
}
});

// Convert vault index and secret path to `vaultName:secretPath`
const secretPaths = secretVaultNames.map(
(v) => `${vaultNames[v[0]]}:${v[1]}`,
);
command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
expect(result.stdout).toBe(secretNames.join(''));
expect(result.exitCode).toBe(0);
});

test.todo('should return stdin as-is');
});
61 changes: 0 additions & 61 deletions tests/secrets/get.test.ts

This file was deleted.

0 comments on commit 2bf097a

Please sign in to comment.