Skip to content

Commit

Permalink
feat: allowing to pass in vault name without path for secrets env
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
aryanjassal committed Oct 18, 2024
1 parent 3d6ec55 commit cafaf3e
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 254 deletions.
1 change: 1 addition & 0 deletions src/secrets/CommandEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CommandEnv extends CommandPolykey {
binParsers.parseEnvArgs,
);
this.action(async (args: Array<string>, options) => {
console.error(args)
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
65 changes: 59 additions & 6 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ 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 = /^(?!.*[:])[ -~\t\n]*$/s;
const secretPathRegex = /^(?!.*[=])[ -~\t\n]*$/s;
const vaultNameSecretPathRegex = /^([\w-]+)(?::([^\0\\=]+))?$/;
const secretPathValueRegex = /^([a-zA-Z_][\w]+)?$/;
const environmentVariableRegex = /^([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;

Expand Down Expand Up @@ -80,15 +82,26 @@ function parseSecretPathOptional(
lastEqualIndex === -1
? undefined
: secretPath.substring(lastEqualIndex + 1);
if (!secretPathRegex.test(splitSecretPath)) {
if (!vaultNameSecretPathRegex.test(splitSecretPath)) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>[:<directoryPath>][=<value>]`,
);
}
const [, vaultName, directoryPath] = splitSecretPath.match(secretPathRegex)!;
const [, vaultName, directoryPath] = splitSecretPath.match(
vaultNameSecretPathRegex,
)!;
return [vaultName, directoryPath, value];
}

function parseVaultName(vaultName: string): string {
if (!vaultNameRegex.test(vaultName)) {
throw new commander.InvalidArgumentError(
`${vaultName} is not a valid vault name`,
);
}
return vaultName;
}

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
Expand All @@ -111,8 +124,40 @@ function parseSecretPathValue(secretPath: string): [string, string, string?] {
return [vaultName, directoryPath, value];
}

function parseSecretPathEnv(secretPath: string): [string, string, string?] {
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
function parseSecretPathEnv(secretPath: string): [string, string?, string?] {
// The colon character `:` is prohibited in vaultName, so it's first occurence
// means that this is the delimiter between vaultName and secretPath.
const colonIndex = secretPath.indexOf(':');
// If no colon exists, treat entire string as vault name
if (colonIndex === -1) {
return [parseVaultName(secretPath), undefined, undefined];
}
// Calculate contents before the `=` separator
const vaultNamePart = secretPath.substring(0, colonIndex);
const secretPathPart = secretPath.substring(colonIndex + 1);
// Calculate contents after the `=` separator
const equalIndex = secretPathPart.indexOf('=');
const splitSecretPath =
equalIndex === -1
? secretPathPart
: secretPathPart.substring(0, equalIndex);
const valueData =
equalIndex === -1 ? undefined : secretPathPart.substring(equalIndex + 1);
if (splitSecretPath != null && !secretPathRegex.test(splitSecretPath)) {
throw new commander.InvalidArgumentError(
`${secretPath} is not of the format <vaultName>[:<secretPath>][=<value>]`,
);
}
const parsedVaultName = parseVaultName(vaultNamePart);
const parsedSecretPath = splitSecretPath.match(secretPathRegex)?.[0];
const [vaultName, directoryPath, value] = [
parsedVaultName,
parsedSecretPath,
valueData,
];
console.error('vaultName', vaultName)
console.error('directoryPath', directoryPath)
console.error('value', value)
if (value != null && !environmentVariableRegex.test(value)) {
throw new commander.InvalidArgumentError(
`${value} is not a valid environment variable name`,
Expand Down Expand Up @@ -189,20 +234,26 @@ function parseEnvArgs(
value: string,
prev: [Array<[string, string, string?]>, Array<string>] | undefined,
): [Array<[string, string, string?]>, Array<string>] {
console.error('original value', value)
const current: [Array<[string, string, string?]>, Array<string>] = prev ?? [
[],
[],
];
if (current[1].length === 0) {
// Parse a secret path
try {
current[0].push(parseSecretPathEnv(value));
const [vaultName, secretPath, valueData] = parseSecretPathEnv(value);
const parsedSecretPath = secretPath == null ? '/' : secretPath;
console.error(`parsed. [${vaultName}, ${parsedSecretPath}, ${valueData}]`)
current[0].push([vaultName, parsedSecretPath, valueData]);
} catch (e) {
if (!(e instanceof commander.InvalidArgumentError)) throw e;
console.error('errored. value', value)
// If we get an invalid argument error then we switch over to parsing args verbatim
current[1].push(value);
}
} else {
console.error('added secret path. value', value)
// Otherwise we just have the cmd args
current[1].push(value);
}
Expand All @@ -215,13 +266,15 @@ function parseEnvArgs(
}

export {
vaultNameRegex,
secretPathRegex,
secretPathValueRegex,
environmentVariableRegex,
validateParserToArgParser,
validateParserToArgListParser,
parseCoreCount,
parseSecretPathOptional,
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
2 changes: 1 addition & 1 deletion tests/secrets/cat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('commandCatSecret', () => {
resolve(exitCode);
});
});
expect(exitCode).toStrictEqual(0);
expect(exitCode).toBe(0);
expect(stdout).toBe(stdinData);
});
});
Loading

0 comments on commit cafaf3e

Please sign in to comment.