From 5768f962502ab03f2ded8960534e980af86a5860 Mon Sep 17 00:00:00 2001 From: Siebe Vanden Eynden Date: Wed, 21 Aug 2019 11:20:13 +0200 Subject: [PATCH] feat: Added ssh config array to remote server --- README.md | 2 +- packages/ssh-pool/README.md | 12 ++++++++++++ packages/ssh-pool/src/Connection.js | 2 ++ packages/ssh-pool/src/Connection.test.js | 19 +++++++++++++++++++ packages/ssh-pool/src/commands/ssh.js | 6 ++++++ packages/ssh-pool/src/commands/ssh.test.js | 6 ++++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 987b4fa..1d46617 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ The server can use the shorthand syntax or an object: - `user@host`: user and host - `user@host:4000`: user, host and port -- `{ user, host, port }`: an object +- `{ user, host, port, extraSshOptions }`: an object ### Shipit Deploy configuration diff --git a/packages/ssh-pool/README.md b/packages/ssh-pool/README.md index a55b632..f5bb02f 100755 --- a/packages/ssh-pool/README.md +++ b/packages/ssh-pool/README.md @@ -58,6 +58,18 @@ new Connection({ port: 4000, }, }) + +// When defined as an object you can add extra ssh parameters +new Connection({ + remote: { + user: 'user', + host: 'localhost', + port: 4000, + extraSshOptions: { + ServerAliveInterval: '30', + } + }, +}) ``` The log method is used to log output directly: diff --git a/packages/ssh-pool/src/Connection.js b/packages/ssh-pool/src/Connection.js index cb1dc6b..8a4e1d3 100644 --- a/packages/ssh-pool/src/Connection.js +++ b/packages/ssh-pool/src/Connection.js @@ -301,6 +301,7 @@ class Connection { key: this.options.key, strict: this.options.strict, tty: this.options.tty, + extraSshOptions: this.remote.extraSshOptions, verbosityLevel: this.options.verbosityLevel, remote: formatRemote(this.remote), command: formatRawCommand({ command, asUser: this.options.asUser }), @@ -328,6 +329,7 @@ class Connection { port: this.remote.port, key: this.options.key, strict: this.options.strict, + extraSshOptions: this.remote.extraSshOptions, tty: this.options.tty, }) diff --git a/packages/ssh-pool/src/Connection.test.js b/packages/ssh-pool/src/Connection.test.js index 9bda24f..86045bb 100644 --- a/packages/ssh-pool/src/Connection.test.js +++ b/packages/ssh-pool/src/Connection.test.js @@ -140,6 +140,25 @@ describe('Connection', () => { ) }) + it('should use extra ssh options on remote if present', async () => { + connection = new Connection({ + remote: { + host: 'host', + user: 'user', + extraSshOptions: { + ExtraOption: 'option', + SshForwardAgent: 'forward', + } + }, + }) + await connection.run('my-command -x') + expect(exec).toHaveBeenCalledWith( + 'ssh -o ExtraOption=option -o SshForwardAgent=forward user@host "my-command -x"', + { maxBuffer: 1024000 }, + expect.any(Function), + ) + }) + it('should use port and key if both are present', async () => { connection = new Connection({ remote: 'user@host:12345', diff --git a/packages/ssh-pool/src/commands/ssh.js b/packages/ssh-pool/src/commands/ssh.js index ff6d9e2..2bd1dce 100644 --- a/packages/ssh-pool/src/commands/ssh.js +++ b/packages/ssh-pool/src/commands/ssh.js @@ -12,6 +12,7 @@ export function formatSshCommand({ remote, cwd, command, + extraSshOptions, verbosityLevel, }) { let args = ['ssh'] @@ -33,6 +34,11 @@ export function formatSshCommand({ if (tty) args = [...args, '-tt'] if (port) args = [...args, '-p', port] if (key) args = [...args, '-i', key] + if(extraSshOptions && typeof extraSshOptions === 'object') { + Object.keys(extraSshOptions).forEach((sshOptionsKey) => { + args = [...args, '-o', `${sshOptionsKey}=${extraSshOptions[sshOptionsKey]}`] + }) + } if (strict !== undefined) args = [...args, '-o', `StrictHostKeyChecking=${strict}`] if (remote) args = [...args, remote] diff --git a/packages/ssh-pool/src/commands/ssh.test.js b/packages/ssh-pool/src/commands/ssh.test.js index dff095c..eb39d63 100644 --- a/packages/ssh-pool/src/commands/ssh.test.js +++ b/packages/ssh-pool/src/commands/ssh.test.js @@ -29,6 +29,12 @@ describe('ssh', () => { ) }) + it('should support extra ssh options', () => { + expect(formatSshCommand({ extraSshOptions: {ExtraOption: 'test option', MoreOptions: 'more option'} })).toBe( + 'ssh -o ExtraOption=test option -o MoreOptions=more option', + ) + }) + it('should support remote', () => { expect( formatSshCommand({