Skip to content

Commit

Permalink
feat: adding secrets rm
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 10, 2024
1 parent bd3e8b4 commit afbd9a2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 76 deletions.
69 changes: 0 additions & 69 deletions src/secrets/CommandDelete.ts

This file was deleted.

82 changes: 82 additions & 0 deletions src/secrets/CommandRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binParsers from '../utils/parsers';
import * as binProcessors from '../utils/processors';

class CommandDelete extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('rm');
this.description('Delete a Secret from a specified Vault');
this.argument(
'<secretPaths>',
'Path to one or more secret to be deleted, each specified as <vaultName>:<directoryPath>',
binParsers.parseSecretPathValue,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
this.addOption(binOptions.recursive);
this.action(async (secretPaths, options) => {
// secretPaths = secretPaths.map((path: string) =>
// binParsers.parseSecretPathValue(path),
// );
const { default: PolykeyClient } = await import(
'polykey/dist/PolykeyClient'
);
const clientOptions = await binProcessors.processClientOptions(
options.nodePath,
options.nodeId,
options.clientHost,
options.clientPort,
this.fs,
this.logger.getChild(binProcessors.processClientOptions.name),
);
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);

let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
});
try {
pkClient = await PolykeyClient.createPolykeyClient({
nodeId: clientOptions.nodeId,
host: clientOptions.clientHost,
port: clientOptions.clientPort,
options: { nodePath: options.nodePath },
logger: this.logger.getChild(PolykeyClient.name),
});
await binUtils.retryAuthentication(async (auth) => {
try {
await pkClient.rpcClient.methods.vaultsSecretsRemove({
metadata: auth,
nameOrId: secretPaths[0],
secretNames: [secretPaths[1]],
options: { recursive: options.recursive },
});
} catch(e) {
console.error(e);
throw e;
}
// for (const secretPath of secretPaths) {
// await pkClient.rpcClient.methods.vaultsSecretsRemove({
// metadata: auth,
// nameOrId: secretPath[0],
// secretNames: [secretPath[1]],
// options: { recursive: options.recursive },
// });
// }
}, meta);
} finally {
if (pkClient! != null) await pkClient.stop();
}
});
}
}

export default CommandDelete;
4 changes: 2 additions & 2 deletions src/secrets/CommandSecrets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CommandCreate from './CommandCreate';
import CommandDelete from './CommandDelete';
import CommandRemove from './CommandRemove';
import CommandDir from './CommandDir';
import CommandEdit from './CommandEdit';
import CommandEnv from './CommandEnv';
Expand All @@ -17,7 +17,7 @@ class CommandSecrets extends CommandPolykey {
this.name('secrets');
this.description('Secrets Operations');
this.addCommand(new CommandCreate(...args));
this.addCommand(new CommandDelete(...args));
this.addCommand(new CommandRemove(...args));
this.addCommand(new CommandDir(...args));
this.addCommand(new CommandEdit(...args));
this.addCommand(new CommandEnv(...args));
Expand Down
6 changes: 6 additions & 0 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ const order = new commander.Option(
.choices(['asc', 'desc'])
.default('asc');

const recursive = new commander.Option(
'-r --recursive',
'If enabled, specified directories will be removed along with their contents',
).default(false);

export {
nodePath,
format,
Expand Down Expand Up @@ -343,4 +348,5 @@ export {
events,
limit,
order,
recursive,
};
10 changes: 5 additions & 5 deletions tests/secrets/delete.test.ts → tests/secrets/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('commandDeleteSecret', () => {
});
});

test('should delete secrets', async () => {
test('should remove secrets', async () => {
const vaultName = 'Vault2' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

Expand All @@ -51,14 +51,14 @@ describe('commandDeleteSecret', () => {
expect(list.sort()).toStrictEqual(['MySecret']);
});

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

const result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
env: { PK_PASSWORD: password },
cwd: dataDir,
});
console.log(result.stderr);
console.log(result.stdout);
expect(result.exitCode).toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
Expand Down

0 comments on commit afbd9a2

Please sign in to comment.