From aac94459a1e586cf6108c7f27808fcf8132b2321 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Tue, 15 Oct 2024 11:44:57 +1100 Subject: [PATCH] chore: vault commands are now using vaultNameParser --- src/utils/parsers.ts | 49 +++++++++++++++++++++++++------- src/vaults/CommandClone.ts | 6 +++- src/vaults/CommandCreate.ts | 7 ++++- src/vaults/CommandDelete.ts | 7 ++++- src/vaults/CommandLog.ts | 7 ++++- src/vaults/CommandPermissions.ts | 3 +- src/vaults/CommandPull.ts | 6 +++- src/vaults/CommandRename.ts | 13 +++++++-- src/vaults/CommandShare.ts | 6 +++- src/vaults/CommandUnshare.ts | 6 +++- src/vaults/CommandVersion.ts | 7 ++++- 11 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/utils/parsers.ts b/src/utils/parsers.ts index cb163f69..2f322b93 100644 --- a/src/utils/parsers.ts +++ b/src/utils/parsers.ts @@ -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_]*)?$/; @@ -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 @@ -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 [:][=]`, + `${secretPath} is not of the format [:][=]`, ); } - 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?] { @@ -213,12 +240,14 @@ function parseEnvArgs( } export { + vaultNameRegex, secretPathRegex, secretPathValueRegex, environmentVariableRegex, validateParserToArgParser, validateParserToArgListParser, parseCoreCount, + parseVaultName, parseSecretPath, parseSecretPathValue, parseSecretPathEnv, diff --git a/src/vaults/CommandClone.ts b/src/vaults/CommandClone.ts index 23508dff..0dd7a059 100644 --- a/src/vaults/CommandClone.ts +++ b/src/vaults/CommandClone.ts @@ -11,7 +11,11 @@ class CommandClone extends CommandPolykey { super(...args); this.name('clone'); this.description('Clone a Vault from Another Node'); - this.argument('', 'Name or Id of the vault to be cloned'); + this.argument( + '', + 'Name of the vault to be cloned', + binParsers.parseVaultName, + ); this.argument( '', 'Id of the node to clone the vault from', diff --git a/src/vaults/CommandCreate.ts b/src/vaults/CommandCreate.ts index 4624955b..163b37ac 100644 --- a/src/vaults/CommandCreate.ts +++ b/src/vaults/CommandCreate.ts @@ -4,6 +4,7 @@ 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) { @@ -11,7 +12,11 @@ class CommandCreate extends CommandPolykey { this.name('create'); this.aliases(['touch']); this.description('Create a new Vault'); - this.argument('', 'Name of the new vault to be created'); + this.argument( + '', + 'Name of the new vault to be created', + binParsers.parseVaultName, + ); this.addOption(binOptions.nodeId); this.addOption(binOptions.clientHost); this.addOption(binOptions.clientPort); diff --git a/src/vaults/CommandDelete.ts b/src/vaults/CommandDelete.ts index ff3e905c..8c06b6c0 100644 --- a/src/vaults/CommandDelete.ts +++ b/src/vaults/CommandDelete.ts @@ -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) { super(...args); this.name('delete'); this.description('Delete an Existing Vault'); - this.argument('', 'Name of the vault to be deleted'); + this.argument( + '', + 'Name of the vault to be deleted', + binParsers.parseVaultName, + ); this.addOption(binOptions.nodeId); this.addOption(binOptions.clientHost); this.addOption(binOptions.clientPort); diff --git a/src/vaults/CommandLog.ts b/src/vaults/CommandLog.ts index bd4ab644..7e2e5b77 100644 --- a/src/vaults/CommandLog.ts +++ b/src/vaults/CommandLog.ts @@ -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) { super(...args); this.name('log'); this.description('Get the Version History of a Vault'); - this.argument('', 'Name of the vault to obtain the log from'); + this.argument( + '', + 'Name of the vault to obtain the log from', + binParsers.parseVaultName, + ); this.addOption(binOptions.commitId); this.addOption(binOptions.depth); this.addOption(binOptions.nodeId); diff --git a/src/vaults/CommandPermissions.ts b/src/vaults/CommandPermissions.ts index bd0437b8..d69add09 100644 --- a/src/vaults/CommandPermissions.ts +++ b/src/vaults/CommandPermissions.ts @@ -3,6 +3,7 @@ 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) { @@ -10,7 +11,7 @@ class CommandPermissions extends CommandPolykey { this.name('permissions'); this.alias('perms'); this.description('Sets the permissions of a vault for Node Ids'); - this.argument('', 'Name or ID of the vault'); + this.argument('', 'Name of the vault to be pulled into'); + this.argument( + '', + 'Name of the vault to be pulled into', + binParsers.parseVaultName, + ); this.argument( '[targetNodeId]', '(Optional) target node to pull from', diff --git a/src/vaults/CommandRename.ts b/src/vaults/CommandRename.ts index 34f1f2b3..63af2e7d 100644 --- a/src/vaults/CommandRename.ts +++ b/src/vaults/CommandRename.ts @@ -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) { super(...args); this.name('rename'); this.description('Rename an Existing Vault'); - this.argument('', 'Name of the vault to be renamed'); - this.argument('', 'New name of the vault'); + this.argument( + '', + 'Name of the vault to be renamed', + binParsers.parseVaultName, + ); + this.argument( + '', + 'New name of the vault', + binParsers.parseVaultName, + ); this.addOption(binOptions.nodeId); this.addOption(binOptions.clientHost); this.addOption(binOptions.clientPort); diff --git a/src/vaults/CommandShare.ts b/src/vaults/CommandShare.ts index b6e86ad3..a6572567 100644 --- a/src/vaults/CommandShare.ts +++ b/src/vaults/CommandShare.ts @@ -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('', 'Name of the vault to be shared'); + this.argument( + '', + 'Name of the vault to be shared', + binParsers.parseVaultName, + ); this.argument( '', 'Id of the node to share to', diff --git a/src/vaults/CommandUnshare.ts b/src/vaults/CommandUnshare.ts index e791f170..65ab2d5e 100644 --- a/src/vaults/CommandUnshare.ts +++ b/src/vaults/CommandUnshare.ts @@ -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('', 'Name of the vault to be unshared'); + this.argument( + '', + 'Name of the vault to be unshared', + binParsers.parseVaultName, + ); this.argument( '', 'Id of the node to unshare with', diff --git a/src/vaults/CommandVersion.ts b/src/vaults/CommandVersion.ts index 327bfd18..8eb5cb7e 100644 --- a/src/vaults/CommandVersion.ts +++ b/src/vaults/CommandVersion.ts @@ -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) { super(...args); this.name('version'); this.description('Set a Vault to a Particular Version in its History'); - this.argument('', 'Name of the vault to change the version of'); + this.argument( + '', + 'Name of the vault to change the version of', + binParsers.parseVaultName, + ); this.argument('', 'Id of the commit that will be changed to'); this.addOption(binOptions.nodeId); this.addOption(binOptions.clientHost);