Skip to content

Commit 4d8705b

Browse files
committed
wip: current code
[ci skip]
1 parent 87c9b57 commit 4d8705b

File tree

4 files changed

+83
-20
lines changed

4 files changed

+83
-20
lines changed

src/secrets/CommandEdit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ class CommandEdit extends CommandPolykey {
118118
editorProc.removeListener('error', onError);
119119
editorProc.removeListener('close', onClose);
120120
};
121-
const onError = (err: Error) => {
121+
const onError = (e: Error) => {
122122
cleanup();
123123
const error = new errors.ErrorPolykeyCLIEditSecret(
124124
`Failed to run command ${process.env.EDITOR}`,
125-
{ cause: err },
125+
{ cause: e },
126126
);
127127
reject(error);
128128
};

src/secrets/CommandEnv.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type PolykeyClient from 'polykey/dist/PolykeyClient';
22
import path from 'path';
33
import os from 'os';
4+
import commander from 'commander';
45
import * as utils from 'polykey/dist/utils';
6+
import CommandPolykey from '../CommandPolykey';
57
import * as binProcessors from '../utils/processors';
68
import * as binUtils from '../utils';
79
import * as binErrors from '../errors';
8-
import CommandPolykey from '../CommandPolykey';
910
import * as binOptions from '../utils/options';
1011
import * as binParsers from '../utils/parsers';
1112

@@ -14,7 +15,7 @@ class CommandEnv extends CommandPolykey {
1415
super(...args);
1516
this.name('env');
1617
this.description(
17-
`Run a command with the given secrets and env variables using process replacement. If no command is specified then the variables are printed to stdout in the format specified by env-format.`,
18+
`Run a command with the given secrets and env variables. If no command is specified then the variables are printed to stdout in the format specified by env-format.`,
1819
);
1920
this.addOption(binOptions.nodeId);
2021
this.addOption(binOptions.clientHost);
@@ -37,11 +38,17 @@ class CommandEnv extends CommandPolykey {
3738
// The parser sets the default value for args. If no arguments are
3839
// provided, then args is an empty array []. This is different from the
3940
// expected structure. This block ensure that the structure is preserved.
41+
// Do the same for options.preserveNewline.
4042
args = args[0] == null && args[1] == null ? [[], []] : args;
43+
options.preserveNewline = options.preserveNewline ?? [];
4144

42-
// There needs to be at least one argument or preserved argument provided
45+
// There needs to be at least one argument or preserved argument
46+
// provided. Otherwise, print the help text and error out.
4347
if (args[0].length === 0 && options.preserveNewline.length === 0) {
44-
this.help();
48+
this.outputHelp()
49+
throw new commander.InvalidArgumentError(
50+
'You must provide at least one secret path.',
51+
);
4552
}
4653
// Remove the -- from the command arguments
4754
args[1]?.shift();

src/utils/parsers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ function parseEnvArgs(
208208
[],
209209
[],
210210
];
211+
console.error('val', value)
212+
console.error('cur', current)
211213
if (current[1].length === 0) {
212214
// Parse a secret path
213215
if (value !== '--') {
@@ -220,11 +222,6 @@ function parseEnvArgs(
220222
// Otherwise we just have the cmd args
221223
current[1].push(value);
222224
}
223-
if (current[0].length === 0 && current[1].length > 0) {
224-
throw new commander.InvalidArgumentError(
225-
'You must provide at least 1 secret path',
226-
);
227-
}
228225
return current;
229226
}
230227

tests/secrets/env.test.ts

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,11 @@ describe('commandEnv', () => {
669669
});
670670
test('newlines in secrets are untouched', async () => {
671671
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
672+
const secretName = 'SECRET';
673+
const secretContent =
674+
'this is a secret\nit has multiple lines\n\nand some more lines';
672675
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
673-
await vaultOps.addSecret(
674-
vault,
675-
'SECRET',
676-
'this is a secret\nit has multiple lines\n',
677-
);
676+
await vaultOps.addSecret(vault, secretName, secretContent);
678677
});
679678
const command = [
680679
'secrets',
@@ -683,7 +682,35 @@ describe('commandEnv', () => {
683682
dataDir,
684683
'--env-format',
685684
'unix',
686-
`${vaultName}:SECRET`,
685+
`${vaultName}:${secretName}`,
686+
'--',
687+
'node',
688+
'-e',
689+
'console.log(JSON.stringify(process.env))',
690+
];
691+
const result = await testUtils.pkExec(command, {
692+
env: { PK_PASSWORD: password },
693+
});
694+
expect(result.exitCode).toBe(0);
695+
const jsonOut = JSON.parse(result.stdout);
696+
expect(jsonOut[secretName]).toBe(secretContent);
697+
});
698+
// TODO: fastcheck
699+
test('single trailing newline is automatically removed', async () => {
700+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
701+
const secretName = 'SECRET';
702+
const secretContent = 'this is a secret\n'; // Simulating Unix file ending
703+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
704+
await vaultOps.addSecret(vault, secretName, secretContent);
705+
});
706+
const command = [
707+
'secrets',
708+
'env',
709+
'-np',
710+
dataDir,
711+
'--env-format',
712+
'unix',
713+
`${vaultName}:${secretName}`,
687714
'--',
688715
'node',
689716
'-e',
@@ -694,7 +721,39 @@ describe('commandEnv', () => {
694721
});
695722
expect(result.exitCode).toBe(0);
696723
const jsonOut = JSON.parse(result.stdout);
697-
expect(jsonOut['SECRET']).toBe('this is a secret\nit has multiple lines\n');
724+
// Remove last character
725+
expect(jsonOut[secretName]).toBe(secretContent.slice(0, -1));
726+
});
727+
// TODO: fastcheck
728+
test('trailing newlines are preserved with option', async () => {
729+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
730+
const secretName = 'SECRET';
731+
const secretContent = 'this is a secret\n';
732+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
733+
await vaultOps.addSecret(vault, secretName, secretContent);
734+
});
735+
const command = [
736+
'secrets',
737+
'env',
738+
'-np',
739+
dataDir,
740+
'--env-format',
741+
'unix',
742+
'--preserve-newline',
743+
`${vaultName}:${secretName}`,
744+
'--',
745+
'node',
746+
'-e',
747+
'console.log(JSON.stringify(process.env))',
748+
];
749+
const result = await testUtils.pkExec(command, {
750+
env: { PK_PASSWORD: password },
751+
});
752+
console.error(result.stderr)
753+
expect(result.exitCode).toBe(0);
754+
const jsonOut = JSON.parse(result.stdout);
755+
// Remove last character
756+
expect(jsonOut[secretName]).toBe(secretContent);
698757
});
699758
test.prop([
700759
testUtils.secretPathEnvArrayArb,
@@ -741,10 +800,10 @@ describe('commandEnv', () => {
741800
'--',
742801
'someCommand',
743802
];
744-
const result1 = await testUtils.pkExec(command, {
803+
const result = await testUtils.pkExec(command, {
745804
env: { PK_PASSWORD: password },
746805
});
747-
expect(result1.exitCode).toBe(64);
806+
expect(result.exitCode).toBe(64);
748807
});
749808
test('should output all secrets without explicit secret path', async () => {
750809
const vaultId1 = await polykeyAgent.vaultManager.createVault(

0 commit comments

Comments
 (0)