Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added export option to secrets env #342 #348

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/secrets/CommandEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CommandEnv extends CommandPolykey {
this.addOption(binOptions.envFormat);
this.addOption(binOptions.envInvalid);
this.addOption(binOptions.envDuplicate);
this.addOption(binOptions.envExport);
this.addOption(binOptions.preserveNewline);
this.argument(
'<args...>',
Expand Down Expand Up @@ -280,45 +281,50 @@ class CommandEnv extends CommandPolykey {
switch (format) {
case 'unix':
{
// Formatting as a .env file
let data = '';
for (const [key, value] of Object.entries(envp)) {
data += `${key}='${value}'\n`;
if (options.envExport) {
data += `export ${key}='${value}'\n`;
} else {
data += `${key}='${value}'\n`;
}
aryanjassal marked this conversation as resolved.
Show resolved Hide resolved
}
process.stdout.write(
binUtils.outputFormatter({
type: 'raw',
data,
data: data,
}),
);
}
break;
case 'cmd':
{
// Formatting as a .bat file for windows cmd
let data = '';
for (const [key, value] of Object.entries(envp)) {
data += `set "${key}=${value}"\n`;
}
process.stdout.write(
binUtils.outputFormatter({
type: 'raw',
data,
data: data,
}),
);
}
break;
case 'powershell':
{
// Formatting as a .bat file for windows cmd
let data = '';
for (const [key, value] of Object.entries(envp)) {
data += `\$env:${key} = '${value}'\n`;
if (options.envExport) {
data += `$env:${key} = '${value}'\n`;
} else {
data += `$${key} = '${value}'\n`;
}
}
process.stdout.write(
binUtils.outputFormatter({
type: 'raw',
data,
data: data,
}),
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ const envDuplicate = new commander.Option(
.choices(['keep', 'overwrite', 'warn', 'error'])
.default('overwrite');

const envExport = new commander.Option(
'-ee --env-export',
'If enabled, exports the set environment variables to child environments',
).default(false);

const discoveryMonitor = new commander.Option(
'--monitor',
'Enabling monitoring will cause discover to output discovery events as they happen and will exit once all children are processed',
Expand Down Expand Up @@ -363,6 +368,7 @@ export {
envFormat,
envInvalid,
envDuplicate,
envExport,
discoveryMonitor,
seekStart,
seekEnd,
Expand Down
37 changes: 33 additions & 4 deletions tests/secrets/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ describe('commandEnv', () => {
env: { PK_PASSWORD: password },
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`$env:SECRET1 = 'this is the secret1'`);
expect(result.stdout).toContain(`$env:SECRET2 = 'this is the secret2'`);
expect(result.stdout).toContain(`$env:SECRET3 = 'this is the secret3'`);
expect(result.stdout).toContain(`$env:SECRET4 = 'this is the secret4'`);
expect(result.stdout).toContain(`$SECRET1 = 'this is the secret1'`);
expect(result.stdout).toContain(`$SECRET2 = 'this is the secret2'`);
expect(result.stdout).toContain(`$SECRET3 = 'this is the secret3'`);
expect(result.stdout).toContain(`$SECRET4 = 'this is the secret4'`);
});
test('should output json format', async () => {
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
Expand Down Expand Up @@ -924,4 +924,33 @@ describe('commandEnv', () => {
expect(result.stdout).toContain("SECRET3='this is the secret3'");
expect(result.stdout).toContain("SECRET4='this is the secret4'");
});
test.each(['unix', 'cmd', 'powershell'])(
'should export all secrets in %s format to child processes',
async (format) => {
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, 'SECRET1', 'this is the secret1');
});
const command = [
'secrets',
'env',
'-np',
dataDir,
'--env-format',
format,
'--env-export',
`${vaultName}:SECRET1`,
];
const result = await testUtils.pkExec(command, {
env: { PK_PASSWORD: password },
});
expect(result.exitCode).toBe(0);
const formatResult = {
unix: `export SECRET1='this is the secret1'\n`,
cmd: `set "SECRET1=this is the secret1"\n`,
powershell: `$env:SECRET1 = 'this is the secret1'\n`,
};
expect(result.stdout).toEqual(formatResult[format]);
},
);
});