Skip to content

Commit 8684fbc

Browse files
committed
fix: added raw type to outputFormatter so that it is possible to show the secret content verbatim, pk secrets get now returns just the secret content and --env is removed
1 parent 54114c5 commit 8684fbc

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

src/bin/secrets/CommandGet.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ class CommandGet extends CommandPolykey {
1515
'Path to where the secret to be retrieved, specified as <vaultName>:<directoryPath>',
1616
parsers.parseSecretPath,
1717
);
18-
this.option(
19-
'-e, --env',
20-
'Wrap the secret in an environment variable declaration',
21-
);
2218
this.addOption(binOptions.nodeId);
2319
this.addOption(binOptions.clientHost);
2420
this.addOption(binOptions.clientPort);
@@ -54,7 +50,6 @@ class CommandGet extends CommandPolykey {
5450
port: clientOptions.clientPort,
5551
logger: this.logger.getChild(PolykeyClient.name),
5652
});
57-
const isEnv: boolean = options.env ?? false;
5853
const secretMessage = new secretsPB.Secret();
5954
const vaultMessage = new vaultsPB.Vault();
6055
vaultMessage.setNameOrId(secretPath[0]);
@@ -64,29 +59,13 @@ class CommandGet extends CommandPolykey {
6459
(auth) => pkClient.grpcClient.vaultsSecretsGet(secretMessage, auth),
6560
meta,
6661
);
67-
const secretContent = Buffer.from(response.getSecretContent_asU8())
68-
.toString('utf-8')
69-
.trim();
70-
if (isEnv) {
71-
process.stdout.write(
72-
binUtils.outputFormatter({
73-
type: options.format === 'json' ? 'json' : 'list',
74-
data: [
75-
`Export ${secretMessage
76-
.getSecretName()
77-
.toUpperCase()
78-
.replace('-', '_')}='${secretContent}'`,
79-
],
80-
}),
81-
);
82-
} else {
83-
process.stdout.write(
84-
binUtils.outputFormatter({
85-
type: options.format === 'json' ? 'json' : 'list',
86-
data: [`${secretMessage.getSecretName()}: ${secretContent}`],
87-
}),
88-
);
89-
}
62+
const secretContent = response.getSecretContent_asU8();
63+
process.stdout.write(
64+
binUtils.outputFormatter({
65+
type: 'raw',
66+
data: secretContent,
67+
}),
68+
);
9069
} finally {
9170
if (pkClient! != null) await pkClient.stop();
9271
}

src/bin/utils/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function verboseToLogLevel(c: number = 0): LogLevel {
2626
}
2727

2828
type OutputObject =
29+
| {
30+
type: 'raw';
31+
data: string | Uint8Array;
32+
}
2933
| {
3034
type: 'list';
3135
data: Array<string>;
@@ -47,9 +51,11 @@ type OutputObject =
4751
data: Error;
4852
};
4953

50-
function outputFormatter(msg: OutputObject): string {
54+
function outputFormatter(msg: OutputObject): string | Uint8Array {
5155
let output = '';
52-
if (msg.type === 'list') {
56+
if (msg.type === 'raw') {
57+
return msg.data;
58+
} else if (msg.type === 'list') {
5359
for (let elem in msg.data) {
5460
// Empty string for null or undefined values
5561
if (elem == null) {

tests/bin/secrets/secrets.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('CLI secrets', () => {
134134
env: {},
135135
cwd: dataDir,
136136
});
137-
expect(result.stdout).toBe('MySecret: this is the secret\n');
137+
expect(result.stdout).toBe('this is the secret');
138138
expect(result.exitCode).toBe(0);
139139
},
140140
);

0 commit comments

Comments
 (0)