|
| 1 | +import type PolykeyClient from 'polykey/dist/PolykeyClient'; |
| 2 | +import CommandPolykey from '../CommandPolykey'; |
| 3 | +import * as binUtils from '../utils'; |
| 4 | +import * as binOptions from '../utils/options'; |
| 5 | +import * as binParsers from '../utils/parsers'; |
| 6 | +import * as binProcessors from '../utils/processors'; |
| 7 | + |
| 8 | +class CommandGet extends CommandPolykey { |
| 9 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 10 | + super(...args); |
| 11 | + this.name('cat'); |
| 12 | + this.description( |
| 13 | + 'Concatenates secrets and prints them on the standard output', |
| 14 | + ); |
| 15 | + this.argument( |
| 16 | + '[secretPaths...]', |
| 17 | + 'Path to a secret to be retrieved, specified as <vaultName>:<directoryPath>', |
| 18 | + ); |
| 19 | + this.addOption(binOptions.nodeId); |
| 20 | + this.addOption(binOptions.clientHost); |
| 21 | + this.addOption(binOptions.clientPort); |
| 22 | + this.action(async (secretPaths, options) => { |
| 23 | + secretPaths = secretPaths.map((path: string) => |
| 24 | + binParsers.parseSecretPathValue(path), |
| 25 | + ); |
| 26 | + const { default: PolykeyClient } = await import( |
| 27 | + 'polykey/dist/PolykeyClient' |
| 28 | + ); |
| 29 | + const clientOptions = await binProcessors.processClientOptions( |
| 30 | + options.nodePath, |
| 31 | + options.nodeId, |
| 32 | + options.clientHost, |
| 33 | + options.clientPort, |
| 34 | + this.fs, |
| 35 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 36 | + ); |
| 37 | + const meta = await binProcessors.processAuthentication( |
| 38 | + options.passwordFile, |
| 39 | + this.fs, |
| 40 | + ); |
| 41 | + |
| 42 | + let pkClient: PolykeyClient; |
| 43 | + this.exitHandlers.handlers.push(async () => { |
| 44 | + if (pkClient != null) await pkClient.stop(); |
| 45 | + }); |
| 46 | + try { |
| 47 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 48 | + nodeId: clientOptions.nodeId, |
| 49 | + host: clientOptions.clientHost, |
| 50 | + port: clientOptions.clientPort, |
| 51 | + options: { |
| 52 | + nodePath: options.nodePath, |
| 53 | + }, |
| 54 | + logger: this.logger.getChild(PolykeyClient.name), |
| 55 | + }); |
| 56 | + if (secretPaths.length === 0) { |
| 57 | + await new Promise<void>((resolve, reject) => { |
| 58 | + const cleanup = () => { |
| 59 | + process.stdin.removeListener('data', dataHandler); |
| 60 | + process.stdin.removeListener('error', errorHandler); |
| 61 | + process.stdin.removeListener('end', endHandler); |
| 62 | + }; |
| 63 | + const dataHandler = (data: Buffer) => { |
| 64 | + process.stdout.write(data); |
| 65 | + }; |
| 66 | + const errorHandler = (err: Error) => { |
| 67 | + cleanup(); |
| 68 | + reject(err); |
| 69 | + }; |
| 70 | + const endHandler = () => { |
| 71 | + cleanup(); |
| 72 | + resolve(); |
| 73 | + }; |
| 74 | + process.stdin.on('data', dataHandler); |
| 75 | + process.stdin.once('error', errorHandler); |
| 76 | + process.stdin.once('end', endHandler); |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + await binUtils.retryAuthentication(async (auth) => { |
| 81 | + const response = await pkClient.rpcClient.methods.vaultsSecretsGet(); |
| 82 | + await (async () => { |
| 83 | + const writer = response.writable.getWriter(); |
| 84 | + let first = true; |
| 85 | + for (const [vaultName, secretPath] of secretPaths) { |
| 86 | + await writer.write({ |
| 87 | + nameOrId: vaultName, |
| 88 | + secretName: secretPath, |
| 89 | + metadata: first |
| 90 | + ? { ...auth, options: { continueOnError: true } } |
| 91 | + : undefined, |
| 92 | + }); |
| 93 | + first = false; |
| 94 | + } |
| 95 | + await writer.close(); |
| 96 | + })(); |
| 97 | + for await (const chunk of response.readable) { |
| 98 | + if (chunk.error) process.stderr.write(chunk.error); |
| 99 | + else process.stdout.write(chunk.secretContent); |
| 100 | + } |
| 101 | + }, meta); |
| 102 | + } finally { |
| 103 | + if (pkClient! != null) await pkClient.stop(); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export default CommandGet; |
0 commit comments