Skip to content

Commit 37a9a58

Browse files
committed
chore: vault commands are now using vaultNameParser
1 parent b11d39b commit 37a9a58

11 files changed

+96
-21
lines changed

src/utils/parsers.ts

+39-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
88
import * as networkUtils from 'polykey/dist/network/utils';
99
import * as nodesUtils from 'polykey/dist/nodes/utils';
1010

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

@@ -65,12 +66,23 @@ function parseCoreCount(v: string): number | undefined {
6566
}
6667
}
6768

69+
function parseVaultName(vaultName: string): string {
70+
if (!vaultNameRegex.test(vaultName)) {
71+
throw new commander.InvalidArgumentError(
72+
`${vaultName} is not a valid vault name`,
73+
);
74+
}
75+
// Make sure we don't accidentally return garbage data
76+
return vaultName.match(vaultNameRegex)![1];
77+
}
78+
79+
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
80+
// If 'vault1', ['vault1, undefined] is returned
81+
// If 'vault1:', an error is thrown
82+
// If 'a/b/c', an error is thrown
83+
// Splits out everything after an `=` separator
6884
function parseSecretPath(secretPath: string): [string, string?, string?] {
69-
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
70-
// If 'vault1', ['vault1, undefined] is returned
71-
// If 'vault1:', an error is thrown
72-
// If 'a/b/c', an error is thrown
73-
// Splits out everything after an `=` separator
85+
// Calculate contents after the `=` separator
7486
const lastEqualIndex = secretPath.lastIndexOf('=');
7587
const splitSecretPath =
7688
lastEqualIndex === -1
@@ -80,13 +92,28 @@ function parseSecretPath(secretPath: string): [string, string?, string?] {
8092
lastEqualIndex === -1
8193
? undefined
8294
: secretPath.substring(lastEqualIndex + 1);
83-
if (!secretPathRegex.test(splitSecretPath)) {
95+
// The colon character `:` is prohibited in vaultName, so it's first occurence
96+
// means that this is the delimiter between vaultName and secretPath.
97+
const colonIndex = splitSecretPath.indexOf(':');
98+
// Calculate contents before the `=` separator
99+
const vaultNamePart =
100+
colonIndex === -1
101+
? splitSecretPath
102+
: splitSecretPath.substring(0, colonIndex);
103+
const secretPathPart =
104+
colonIndex === -1 ? undefined : splitSecretPath.substring(colonIndex + 1);
105+
106+
if (secretPathPart && !secretPathRegex.test(secretPathPart)) {
84107
throw new commander.InvalidArgumentError(
85-
`${secretPath} is not of the format <vaultName>[:<directoryPath>][=<value>]`,
108+
`${secretPath} is not of the format <vaultName>[:<secretPath>][=<value>]`,
86109
);
87110
}
88-
const [, vaultName, directoryPath] = splitSecretPath.match(secretPathRegex)!;
89-
return [vaultName, directoryPath, value];
111+
const parsedVaultName = parseVaultName(vaultNamePart);
112+
const parsedSecretPath =
113+
secretPathPart == null
114+
? undefined
115+
: secretPathPart.match(secretPathRegex)![1];
116+
return [parsedVaultName, parsedSecretPath, value];
90117
}
91118

92119
function parseSecretPathValue(secretPath: string): [string, string, string?] {
@@ -213,12 +240,14 @@ function parseEnvArgs(
213240
}
214241

215242
export {
243+
vaultNameRegex,
216244
secretPathRegex,
217245
secretPathValueRegex,
218246
environmentVariableRegex,
219247
validateParserToArgParser,
220248
validateParserToArgListParser,
221249
parseCoreCount,
250+
parseVaultName,
222251
parseSecretPath,
223252
parseSecretPathValue,
224253
parseSecretPathEnv,

src/vaults/CommandClone.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class CommandClone extends CommandPolykey {
1111
super(...args);
1212
this.name('clone');
1313
this.description('Clone a Vault from Another Node');
14-
this.argument('<vaultNameOrId>', 'Name or Id of the vault to be cloned');
14+
this.argument(
15+
'<vaultName>',
16+
'Name of the vault to be cloned',
17+
binParsers.parseVaultName,
18+
);
1519
this.argument(
1620
'<nodeId>',
1721
'Id of the node to clone the vault from',

src/vaults/CommandCreate.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import CommandPolykey from '../CommandPolykey';
44
import * as binUtils from '../utils';
55
import * as binOptions from '../utils/options';
66
import * as binProcessors from '../utils/processors';
7+
import * as binParsers from '../utils/parsers';
78

89
class CommandCreate extends CommandPolykey {
910
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
1011
super(...args);
1112
this.name('create');
1213
this.aliases(['touch']);
1314
this.description('Create a new Vault');
14-
this.argument('<vaultName>', 'Name of the new vault to be created');
15+
this.argument(
16+
'<vaultName>',
17+
'Name of the new vault to be created',
18+
binParsers.parseVaultName,
19+
);
1520
this.addOption(binOptions.nodeId);
1621
this.addOption(binOptions.clientHost);
1722
this.addOption(binOptions.clientPort);

src/vaults/CommandDelete.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import CommandPolykey from '../CommandPolykey';
33
import * as binUtils from '../utils';
44
import * as binOptions from '../utils/options';
55
import * as binProcessors from '../utils/processors';
6+
import * as binParsers from '../utils/parsers';
67

78
class CommandDelete extends CommandPolykey {
89
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
910
super(...args);
1011
this.name('delete');
1112
this.description('Delete an Existing Vault');
12-
this.argument('<vaultName>', 'Name of the vault to be deleted');
13+
this.argument(
14+
'<vaultName>',
15+
'Name of the vault to be deleted',
16+
binParsers.parseVaultName,
17+
);
1318
this.addOption(binOptions.nodeId);
1419
this.addOption(binOptions.clientHost);
1520
this.addOption(binOptions.clientPort);

src/vaults/CommandLog.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import CommandPolykey from '../CommandPolykey';
44
import * as binUtils from '../utils';
55
import * as binOptions from '../utils/options';
66
import * as binProcessors from '../utils/processors';
7+
import * as binParsers from '../utils/parsers';
78

89
class CommandLog extends CommandPolykey {
910
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
1011
super(...args);
1112
this.name('log');
1213
this.description('Get the Version History of a Vault');
13-
this.argument('<vaultName>', 'Name of the vault to obtain the log from');
14+
this.argument(
15+
'<vaultName>',
16+
'Name of the vault to obtain the log from',
17+
binParsers.parseVaultName,
18+
);
1419
this.addOption(binOptions.commitId);
1520
this.addOption(binOptions.depth);
1621
this.addOption(binOptions.nodeId);

src/vaults/CommandPermissions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import * as binProcessors from '../utils/processors';
33
import * as binUtils from '../utils';
44
import CommandPolykey from '../CommandPolykey';
55
import * as binOptions from '../utils/options';
6+
import * as binParsers from '../utils/parsers';
67

78
class CommandPermissions extends CommandPolykey {
89
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
910
super(...args);
1011
this.name('permissions');
1112
this.alias('perms');
1213
this.description('Sets the permissions of a vault for Node Ids');
13-
this.argument('<vaultName>', 'Name or ID of the vault');
14+
this.argument('<vaultName', 'Name of the vault', binParsers.parseVaultName);
1415
this.addOption(binOptions.nodeId);
1516
this.addOption(binOptions.clientHost);
1617
this.addOption(binOptions.clientPort);

src/vaults/CommandPull.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class CommandPull extends CommandPolykey {
1111
super(...args);
1212
this.name('pull');
1313
this.description('Pull a Vault from Another Node');
14-
this.argument('<vaultNameOrId>', 'Name of the vault to be pulled into');
14+
this.argument(
15+
'<vaultName>',
16+
'Name of the vault to be pulled into',
17+
binParsers.parseVaultName,
18+
);
1519
this.argument(
1620
'[targetNodeId]',
1721
'(Optional) target node to pull from',

src/vaults/CommandRename.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ import CommandPolykey from '../CommandPolykey';
33
import * as binUtils from '../utils';
44
import * as binOptions from '../utils/options';
55
import * as binProcessors from '../utils/processors';
6+
import * as binParsers from '../utils/parsers';
67

78
class CommandRename extends CommandPolykey {
89
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
910
super(...args);
1011
this.name('rename');
1112
this.description('Rename an Existing Vault');
12-
this.argument('<vaultName>', 'Name of the vault to be renamed');
13-
this.argument('<newVaultName>', 'New name of the vault');
13+
this.argument(
14+
'<vaultName>',
15+
'Name of the vault to be renamed',
16+
binParsers.parseVaultName,
17+
);
18+
this.argument(
19+
'<newVaultName>',
20+
'New name of the vault',
21+
binParsers.parseVaultName,
22+
);
1423
this.addOption(binOptions.nodeId);
1524
this.addOption(binOptions.clientHost);
1625
this.addOption(binOptions.clientPort);

src/vaults/CommandShare.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class CommandShare extends CommandPolykey {
1111
super(...args);
1212
this.name('share');
1313
this.description('Set the Permissions of a Vault for a Node');
14-
this.argument('<vaultName>', 'Name of the vault to be shared');
14+
this.argument(
15+
'<vaultName>',
16+
'Name of the vault to be shared',
17+
binParsers.parseVaultName,
18+
);
1519
this.argument(
1620
'<nodeId>',
1721
'Id of the node to share to',

src/vaults/CommandUnshare.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class CommandUnshare extends CommandPolykey {
1111
super(...args);
1212
this.name('unshare');
1313
this.description('Unset the Permissions of a Vault for a Node');
14-
this.argument('<vaultName>', 'Name of the vault to be unshared');
14+
this.argument(
15+
'<vaultName>',
16+
'Name of the vault to be unshared',
17+
binParsers.parseVaultName,
18+
);
1519
this.argument(
1620
'<nodeId>',
1721
'Id of the node to unshare with',

src/vaults/CommandVersion.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import CommandPolykey from '../CommandPolykey';
33
import * as binUtils from '../utils';
44
import * as binOptions from '../utils/options';
55
import * as binProcessors from '../utils/processors';
6+
import * as binParsers from '../utils/parsers';
67

78
class CommandVersion extends CommandPolykey {
89
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
910
super(...args);
1011
this.name('version');
1112
this.description('Set a Vault to a Particular Version in its History');
12-
this.argument('<vaultName>', 'Name of the vault to change the version of');
13+
this.argument(
14+
'<vaultName>',
15+
'Name of the vault to change the version of',
16+
binParsers.parseVaultName,
17+
);
1318
this.argument('<versionId>', 'Id of the commit that will be changed to');
1419
this.addOption(binOptions.nodeId);
1520
this.addOption(binOptions.clientHost);

0 commit comments

Comments
 (0)