Skip to content

Commit 2848e25

Browse files
committed
chore: allow secrets env to be used without vaultname
1 parent 6ed1feb commit 2848e25

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/secrets/CommandCat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class CommandGet extends CommandPolykey {
9898
if (chunk.error) process.stderr.write(chunk.error);
9999
else process.stdout.write(chunk.secretContent);
100100
}
101+
process.stderr.write("\n");
101102
}, meta);
102103
} finally {
103104
if (pkClient! != null) await pkClient.stop();

src/utils/parsers.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,13 @@ function parseSecretPathValue(secretPath: string): [string, string, string?] {
125125
return [vaultName, directoryPath, value];
126126
}
127127

128-
function parseSecretPathEnv(secretPath: string): [string, string, string?] {
128+
function parseSecretPathEnv(secretPath: string): [string, string?, string?] {
129129
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
130130
if (value != null && !environmentVariableRegex.test(value)) {
131131
throw new commander.InvalidArgumentError(
132132
`${value} is not a valid environment variable name`,
133133
);
134134
}
135-
if (directoryPath == null) {
136-
throw new commander.InvalidArgumentError(
137-
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
138-
);
139-
}
140135
return [vaultName, directoryPath, value];
141136
}
142137

@@ -215,7 +210,9 @@ function parseEnvArgs(
215210
if (current[1].length === 0) {
216211
// Parse a secret path
217212
try {
218-
current[0].push(parseSecretPathEnv(value));
213+
const [vaultName, secretPath, valueData] = parseSecretPathEnv(value);
214+
const parsedSecretPath = secretPath == null ? '/' : secretPath;
215+
current[0].push([vaultName, parsedSecretPath, valueData]);
219216
} catch (e) {
220217
if (!(e instanceof commander.InvalidArgumentError)) throw e;
221218
// If we get an invalid argument error then we switch over to parsing args verbatim

tests/secrets/write.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,41 @@ describe('commandWriteFile', () => {
9595
});
9696
},
9797
);
98+
test.prop([stdinArb], { numRuns: 1 })(
99+
'should fail writing when secret path is not specified',
100+
async (stdinData) => {
101+
const vaultName = genVaultName();
102+
await polykeyAgent.vaultManager.createVault(vaultName);
103+
command = [
104+
'secrets',
105+
'write',
106+
'-np',
107+
dataDir,
108+
vaultName
109+
];
110+
111+
const childProcess = await testUtils.pkSpawn(
112+
command,
113+
{
114+
env: { PK_PASSWORD: password },
115+
cwd: dataDir,
116+
},
117+
logger,
118+
);
119+
// The conditions of stdin being null will not be met in the test, so we
120+
// don't have to worry about the fields being null.
121+
childProcess.stdin!.write(stdinData);
122+
childProcess.stdin!.end();
123+
const exitCode = await new Promise((resolve) => {
124+
childProcess.once('exit', (code) => {
125+
const exitCode = code ?? -255;
126+
childProcess.removeAllListeners('data');
127+
resolve(exitCode);
128+
});
129+
});
130+
expect(exitCode).not.toBe(0);
131+
},
132+
);
98133
test('should overwrite secret', async () => {
99134
const vaultName = 'vault' as VaultName;
100135
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

0 commit comments

Comments
 (0)