Skip to content

Commit

Permalink
chore: vault commands are now using vaultNameParser
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 15, 2024
1 parent 607e58a commit aac9445
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 21 deletions.
49 changes: 39 additions & 10 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
import * as networkUtils from 'polykey/dist/network/utils';
import * as nodesUtils from 'polykey/dist/nodes/utils';

const secretPathRegex = /^([\w-]+)(?::([^\0\\=]+))?$/;
const vaultNameRegex = /^([\w-.]+)$/;
const secretPathRegex = /^([^\0\\=]+)?$/;
const secretPathValueRegex = /^([a-zA-Z_][\w]+)?$/;
const environmentVariableRegex = /^([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;

Expand Down Expand Up @@ -65,12 +66,23 @@ function parseCoreCount(v: string): number | undefined {
}
}

function parseVaultName(vaultName: string): string {
if (!vaultNameRegex.test(vaultName)) {
throw new commander.InvalidArgumentError(
`${vaultName} is not a valid vault name`,
);
}
// Make sure we don't accidentally return garbage data
return vaultName.match(vaultNameRegex)![1];
}

// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
// If 'vault1', ['vault1, undefined] is returned
// If 'vault1:', an error is thrown
// If 'a/b/c', an error is thrown
// Splits out everything after an `=` separator
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
// If 'vault1:', an error is thrown
// If 'a/b/c', an error is thrown
// Splits out everything after an `=` separator
// Calculate contents after the `=` separator
const lastEqualIndex = secretPath.lastIndexOf('=');
const splitSecretPath =
lastEqualIndex === -1
Expand All @@ -80,13 +92,28 @@ function parseSecretPath(secretPath: string): [string, string?, string?] {
lastEqualIndex === -1
? undefined
: secretPath.substring(lastEqualIndex + 1);
if (!secretPathRegex.test(splitSecretPath)) {
// The colon character `:` is prohibited in vaultName, so it's first occurence
// means that this is the delimiter between vaultName and secretPath.
const colonIndex = splitSecretPath.indexOf(':');
// Calculate contents before the `=` separator
const vaultNamePart =
colonIndex === -1
? splitSecretPath
: splitSecretPath.substring(0, colonIndex);
const secretPathPart =
colonIndex === -1 ? undefined : splitSecretPath.substring(colonIndex + 1);

if (secretPathPart && !secretPathRegex.test(secretPathPart)) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>[:<directoryPath>][=<value>]`,
`${secretPath} is not of the format <vaultName>[:<secretPath>][=<value>]`,
);
}
const [, vaultName, directoryPath] = splitSecretPath.match(secretPathRegex)!;
return [vaultName, directoryPath, value];
const parsedVaultName = parseVaultName(vaultNamePart);
const parsedSecretPath =
secretPathPart == null
? undefined
: secretPathPart.match(secretPathRegex)![1];
return [parsedVaultName, parsedSecretPath, value];
}

function parseSecretPathValue(secretPath: string): [string, string, string?] {
Expand Down Expand Up @@ -213,12 +240,14 @@ function parseEnvArgs(
}

export {
vaultNameRegex,
secretPathRegex,
secretPathValueRegex,
environmentVariableRegex,
validateParserToArgParser,
validateParserToArgListParser,
parseCoreCount,
parseVaultName,
parseSecretPath,
parseSecretPathValue,
parseSecretPathEnv,
Expand Down
6 changes: 5 additions & 1 deletion src/vaults/CommandClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class CommandClone extends CommandPolykey {
super(...args);
this.name('clone');
this.description('Clone a Vault from Another Node');
this.argument('<vaultNameOrId>', 'Name or Id of the vault to be cloned');
this.argument(
'<vaultName>',
'Name of the vault to be cloned',
binParsers.parseVaultName,
);
this.argument(
'<nodeId>',
'Id of the node to clone the vault from',
Expand Down
7 changes: 6 additions & 1 deletion src/vaults/CommandCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

class CommandCreate extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('create');
this.aliases(['touch']);
this.description('Create a new Vault');
this.argument('<vaultName>', 'Name of the new vault to be created');
this.argument(
'<vaultName>',
'Name of the new vault to be created',
binParsers.parseVaultName,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
Expand Down
7 changes: 6 additions & 1 deletion src/vaults/CommandDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

class CommandDelete extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('delete');
this.description('Delete an Existing Vault');
this.argument('<vaultName>', 'Name of the vault to be deleted');
this.argument(
'<vaultName>',
'Name of the vault to be deleted',
binParsers.parseVaultName,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
Expand Down
7 changes: 6 additions & 1 deletion src/vaults/CommandLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

class CommandLog extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('log');
this.description('Get the Version History of a Vault');
this.argument('<vaultName>', 'Name of the vault to obtain the log from');
this.argument(
'<vaultName>',
'Name of the vault to obtain the log from',
binParsers.parseVaultName,
);
this.addOption(binOptions.commitId);
this.addOption(binOptions.depth);
this.addOption(binOptions.nodeId);
Expand Down
3 changes: 2 additions & 1 deletion src/vaults/CommandPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as binProcessors from '../utils/processors';
import * as binUtils from '../utils';
import CommandPolykey from '../CommandPolykey';
import * as binOptions from '../utils/options';
import * as binParsers from '../utils/parsers';

class CommandPermissions extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('permissions');
this.alias('perms');
this.description('Sets the permissions of a vault for Node Ids');
this.argument('<vaultName>', 'Name or ID of the vault');
this.argument('<vaultName', 'Name of the vault', binParsers.parseVaultName);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
Expand Down
6 changes: 5 additions & 1 deletion src/vaults/CommandPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class CommandPull extends CommandPolykey {
super(...args);
this.name('pull');
this.description('Pull a Vault from Another Node');
this.argument('<vaultNameOrId>', 'Name of the vault to be pulled into');
this.argument(
'<vaultName>',
'Name of the vault to be pulled into',
binParsers.parseVaultName,
);
this.argument(
'[targetNodeId]',
'(Optional) target node to pull from',
Expand Down
13 changes: 11 additions & 2 deletions src/vaults/CommandRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

class CommandRename extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('rename');
this.description('Rename an Existing Vault');
this.argument('<vaultName>', 'Name of the vault to be renamed');
this.argument('<newVaultName>', 'New name of the vault');
this.argument(
'<vaultName>',
'Name of the vault to be renamed',
binParsers.parseVaultName,
);
this.argument(
'<newVaultName>',
'New name of the vault',
binParsers.parseVaultName,
);
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
Expand Down
6 changes: 5 additions & 1 deletion src/vaults/CommandShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class CommandShare extends CommandPolykey {
super(...args);
this.name('share');
this.description('Set the Permissions of a Vault for a Node');
this.argument('<vaultName>', 'Name of the vault to be shared');
this.argument(
'<vaultName>',
'Name of the vault to be shared',
binParsers.parseVaultName,
);
this.argument(
'<nodeId>',
'Id of the node to share to',
Expand Down
6 changes: 5 additions & 1 deletion src/vaults/CommandUnshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class CommandUnshare extends CommandPolykey {
super(...args);
this.name('unshare');
this.description('Unset the Permissions of a Vault for a Node');
this.argument('<vaultName>', 'Name of the vault to be unshared');
this.argument(
'<vaultName>',
'Name of the vault to be unshared',
binParsers.parseVaultName,
);
this.argument(
'<nodeId>',
'Id of the node to unshare with',
Expand Down
7 changes: 6 additions & 1 deletion src/vaults/CommandVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

class CommandVersion extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('version');
this.description('Set a Vault to a Particular Version in its History');
this.argument('<vaultName>', 'Name of the vault to change the version of');
this.argument(
'<vaultName>',
'Name of the vault to change the version of',
binParsers.parseVaultName,
);
this.argument('<versionId>', 'Id of the commit that will be changed to');
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
Expand Down

0 comments on commit aac9445

Please sign in to comment.