Skip to content

Commit

Permalink
feat: changed parsers to allow using vault name without secret path
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 15, 2024
1 parent 4a75142 commit 607e58a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/secrets/CommandCat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CommandGet extends CommandPolykey {
for (const [vaultName, secretPath] of secretPaths) {
await writer.write({
nameOrId: vaultName,
secretName: secretPath,
secretName: secretPath ?? '/',
metadata: first
? { ...auth, options: { continueOnError: true } }
: undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/secrets/CommandEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CommandEdit extends CommandPolykey {
this.argument(
'<secretPath>',
'Path to the secret to be edited, specified as <vaultName>:<directoryPath>',
binParsers.parseSecretPathValue,
binParsers.parseSecretPath,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
Expand Down Expand Up @@ -68,7 +68,7 @@ class CommandEdit extends CommandPolykey {
const writer = res.writable.getWriter();
await writer.write({
nameOrId: secretPath[0],
secretName: secretPath[1],
secretName: secretPath[1] ?? '/',
metadata: auth,
});
await writer.close();
Expand Down
2 changes: 1 addition & 1 deletion src/secrets/CommandList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CommandList extends CommandPolykey {
this.argument(
'<directoryPath>',
'Directory to list files from, specified as <vaultName>[:<path>]',
binParsers.parseSecretPathOptional,
binParsers.parseSecretPath,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
Expand Down
2 changes: 1 addition & 1 deletion src/secrets/CommandRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CommandRemove extends CommandPolykey {
this.addOption(binOptions.recursive);
this.action(async (secretPaths, options) => {
secretPaths = secretPaths.map((path: string) =>
binParsers.parseSecretPathValue(path),
binParsers.parseSecretPath(path),
);
const { default: PolykeyClient } = await import(
'polykey/dist/PolykeyClient'
Expand Down
2 changes: 1 addition & 1 deletion src/secrets/CommandStat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommandStat extends CommandPolykey {
this.argument(
'<secretPath>',
'Path to where the secret, specified as <vaultName>:<directoryPath>',
binParsers.parseSecretPathValue,
binParsers.parseSecretPath,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
Expand Down
2 changes: 1 addition & 1 deletion src/secrets/CommandWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommandWrite extends CommandPolykey {
this.argument(
'<secretPath>',
'Path to the secret, specified as <vaultName>:<directoryPath>',
binParsers.parseSecretPathValue,
binParsers.parseSecretPath,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
Expand Down
31 changes: 14 additions & 17 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ function parseCoreCount(v: string): number | undefined {
}
}

function parseSecretPathOptional(
secretPath: string,
): [string, string?, string?] {
function parseSecretPath(secretPath: string): [string, string?, string?] {
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
// If 'vault1', ['vault1, undefined] is returned
// splits out everything after an `=` separator
// If 'vault1:', an error is thrown
// If 'a/b/c', an error is thrown
// Splits out everything after an `=` separator
const lastEqualIndex = secretPath.lastIndexOf('=');
const splitSecretPath =
lastEqualIndex === -1
Expand All @@ -89,25 +89,18 @@ function parseSecretPathOptional(
return [vaultName, directoryPath, value];
}

function parseSecretPath(secretPath: string): [string, string, string?] {
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
// If 'vault1', an error is thrown
const [vaultName, secretName, value] = parseSecretPathOptional(secretPath);
if (secretName === undefined) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
);
}
return [vaultName, secretName, value];
}

function parseSecretPathValue(secretPath: string): [string, string, string?] {
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
if (value != null && !secretPathValueRegex.test(value)) {
throw new commander.InvalidArgumentError(
`${value} is not a valid value name`,
);
}
if (directoryPath == null) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
);
}
return [vaultName, directoryPath, value];
}

Expand All @@ -118,6 +111,11 @@ function parseSecretPathEnv(secretPath: string): [string, string, string?] {
`${value} is not a valid environment variable name`,
);
}
if (directoryPath == null) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
);
}
return [vaultName, directoryPath, value];
}

Expand Down Expand Up @@ -221,7 +219,6 @@ export {
validateParserToArgParser,
validateParserToArgListParser,
parseCoreCount,
parseSecretPathOptional,
parseSecretPath,
parseSecretPathValue,
parseSecretPathEnv,
Expand Down

0 comments on commit 607e58a

Please sign in to comment.