-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from MatrixAI/feature-unix-cat
Adding `secrets cat` command and removing `secrets get` command
- Loading branch information
Showing
10 changed files
with
330 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sha256-bKqMvZvc//Fo63MbPtkkXyLpCKNHyA0SKFy/Ts/fJLw= | ||
sha256-sQ2HofpSd7lMbycLqKH84lHTamWIt8TwPVyddkC+be8= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type PolykeyClient from 'polykey/dist/PolykeyClient'; | ||
import CommandPolykey from '../CommandPolykey'; | ||
import * as binUtils from '../utils'; | ||
import * as binOptions from '../utils/options'; | ||
import * as binParsers from '../utils/parsers'; | ||
import * as binProcessors from '../utils/processors'; | ||
|
||
class CommandGet extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('cat'); | ||
this.description( | ||
'Concatenates secrets and prints them on the standard output', | ||
); | ||
this.argument( | ||
'[secretPaths...]', | ||
'Path to a secret to be retrieved, specified as <vaultName>:<directoryPath>', | ||
); | ||
this.addOption(binOptions.nodeId); | ||
this.addOption(binOptions.clientHost); | ||
this.addOption(binOptions.clientPort); | ||
this.action(async (secretPaths, options) => { | ||
secretPaths = secretPaths.map((path: string) => | ||
binParsers.parseSecretPathValue(path), | ||
); | ||
const { default: PolykeyClient } = await import( | ||
'polykey/dist/PolykeyClient' | ||
); | ||
const clientOptions = await binProcessors.processClientOptions( | ||
options.nodePath, | ||
options.nodeId, | ||
options.clientHost, | ||
options.clientPort, | ||
this.fs, | ||
this.logger.getChild(binProcessors.processClientOptions.name), | ||
); | ||
const meta = await binProcessors.processAuthentication( | ||
options.passwordFile, | ||
this.fs, | ||
); | ||
|
||
let pkClient: PolykeyClient; | ||
this.exitHandlers.handlers.push(async () => { | ||
if (pkClient != null) await pkClient.stop(); | ||
}); | ||
try { | ||
pkClient = await PolykeyClient.createPolykeyClient({ | ||
nodeId: clientOptions.nodeId, | ||
host: clientOptions.clientHost, | ||
port: clientOptions.clientPort, | ||
options: { | ||
nodePath: options.nodePath, | ||
}, | ||
logger: this.logger.getChild(PolykeyClient.name), | ||
}); | ||
if (secretPaths.length === 0) { | ||
await new Promise<void>((resolve, reject) => { | ||
const cleanup = () => { | ||
process.stdin.removeListener('data', dataHandler); | ||
process.stdin.removeListener('error', errorHandler); | ||
process.stdin.removeListener('end', endHandler); | ||
}; | ||
const dataHandler = (data: Buffer) => { | ||
process.stdout.write(data); | ||
}; | ||
const errorHandler = (err: Error) => { | ||
cleanup(); | ||
reject(err); | ||
}; | ||
const endHandler = () => { | ||
cleanup(); | ||
resolve(); | ||
}; | ||
process.stdin.on('data', dataHandler); | ||
process.stdin.once('error', errorHandler); | ||
process.stdin.once('end', endHandler); | ||
}); | ||
return; | ||
} | ||
await binUtils.retryAuthentication(async (auth) => { | ||
const response = await pkClient.rpcClient.methods.vaultsSecretsGet(); | ||
await (async () => { | ||
const writer = response.writable.getWriter(); | ||
let first = true; | ||
for (const [vaultName, secretPath] of secretPaths) { | ||
await writer.write({ | ||
nameOrId: vaultName, | ||
secretName: secretPath, | ||
metadata: first | ||
? { ...auth, options: { continueOnError: true } } | ||
: undefined, | ||
}); | ||
first = false; | ||
} | ||
await writer.close(); | ||
})(); | ||
for await (const chunk of response.readable) { | ||
if (chunk.error) process.stderr.write(chunk.error); | ||
else process.stdout.write(chunk.secretContent); | ||
} | ||
}, meta); | ||
} finally { | ||
if (pkClient! != null) await pkClient.stop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default CommandGet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.