Skip to content

Commit 3d6dc7f

Browse files
committed
Defaults to ssh-agent instead of privateKeyPath
1 parent 66d9453 commit 3d6dc7f

File tree

3 files changed

+116
-35
lines changed

3 files changed

+116
-35
lines changed

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ For detailed information on how configuration of plugins works, please refer to
6262

6363
### username (`required`)
6464

65-
The user for the ssh connection
65+
The user for the ssh connection.
6666

6767
*Default:* `undefined`
6868

6969
### host (`required`)
7070

71-
The server to connect to
71+
The server to connect to.
7272

7373
*Default:* `undefined`
7474

7575
### dstPort
7676

77-
The port to forward from the server
77+
The port to forward from the server.
7878

7979
*Default:* `6379`
8080

@@ -86,31 +86,63 @@ The host to forward to on the destination server.
8686

8787
### srcPort
8888

89-
The local port for the forwarding
89+
The local port for the forwarding.
9090

9191
*Default:* a random port between `49151` and `65535`
9292

9393
### privateKeyPath
9494

95-
The local path to your ssh private key
95+
The local path to your ssh private key.
9696

97-
*Default:* `~/.ssh/id_rsa`
97+
*Default:* null
98+
99+
### password
100+
101+
Authorization string for the ssh connection.
102+
103+
*Default:* null
98104

99105
### tunnelClient
100106

101107
The client used to create the ssh tunnel. This allows the user the ability to use their own client for uploading instead of the one provided by this plugin.
102108

103109
*Default:* the tunnel provided by `tunnel-ssh`
104110

105-
## Running Tests
111+
## Authorization
106112

107-
- `npm test`
113+
ember-cli-deploy-ssh-tunnel uses the [tunnel-ssh](https://github.com/Finanzchef24-GmbH/tunnel-ssh) module to provide the SSH tunnel. Two options exist to configure tunnel-ssh from ember-cli-deploy-ssh-tunnel: `privateKeyPath` and `password`. By default, we assume you have created a public and private key and added it to ssh-agent as described in the [default GitHub setup](https://help.github.com/articles/generating-ssh-keys/).
108114

109-
[1]: http://ember-cli.github.io/ember-cli-deploy/plugins "Plugin Documentation"
115+
If no authentication information is delivered to tunnel-ssh, it will [default to using ssh-agent](https://github.com/Finanzchef24-GmbH/tunnel-ssh), so it will default to using the default id_rsa keys generated as described in the GitHub article. This includes password-protected SSH keys. If you would like to use a different SSH key, set the `privateKeyPath` option:
116+
117+
```js
118+
ENV['ssh-tunnel'] = {
119+
username: 'yourname',
120+
host: 'yourserver',
121+
privateKeyPath: '~/.ssh/another_key_rsa'
122+
};
123+
```
110124

125+
If you just want to use a password to tunnel, you can specify that as an option (we recommend using environmental variables in an .env file):
126+
127+
```js
128+
ENV['ssh-tunnel'] = {
129+
username: 'yourname',
130+
host: 'yourserver',
131+
password: process.env.SSH_PASSWORD
132+
};
133+
```
134+
135+
NOTE: at this time, this plugin does not support setting a path to `privateKeyPath` to a key that has been encrypted with a password.
136+
137+
## Running Tests
138+
139+
1. `npm install`
140+
2. `npm test`
111141

112142
## Thanks to:
113143

114144
@lukemelia and @achambers and the other folks from the ember-cli-deploy project.
115145

116146
@tim-evans for the original implementation in [ember-deploy-redis](https://github.com/LevelbossMike/ember-deploy-redis)
147+
148+
[1]: http://ember-cli.github.io/ember-cli-deploy/plugins "Plugin Documentation"

index.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* jshint node: true */
22
'use strict';
33

4-
var Promise = require('ember-cli/lib/ext/promise');
5-
var fs = require('fs');
4+
var Promise = require('ember-cli/lib/ext/promise');
5+
var fs = require('fs');
66
var tunnelSsh = require('tunnel-ssh');
7-
var untildify = require('untildify');
7+
var untildify = require('untildify');
88

99
var DeployPluginBase = require('ember-cli-deploy-plugin');
1010

@@ -24,7 +24,6 @@ module.exports = {
2424
var range = MAX_PORT_NUMBER - MIN_PORT_NUMBER + 1;
2525
return Math.floor(Math.random() * range) + MIN_PORT_NUMBER;
2626
},
27-
privateKeyPath: '~/.ssh/id_rsa',
2827
tunnelClient: function(context) {
2928
// if you want to provide your own ssh client to be used instead of one from this plugin
3029
return context.tunnelClient || tunnelSsh;
@@ -45,18 +44,21 @@ module.exports = {
4544
dstPort: this.readConfig('dstPort'),
4645
dstHost: this.readConfig('dstHost'),
4746
username: this.readConfig('username'),
48-
localPort: srcPort,
49-
privateKey: this.readConfig('privateKeyPath')
47+
localPort: srcPort
5048
};
5149

52-
if (sshConfig.privateKey) {
53-
sshConfig.privateKey = fs.readFileSync(untildify(sshConfig.privateKey));
54-
}
55-
50+
var password = this.readConfig('password');
51+
var privateKey = this.readConfig('privateKeyPath');
5652
var tunnel = this.readConfig('tunnelClient');
5753

58-
return new Promise(function (resolve, reject) {
59-
var sshTunnel = tunnel(sshConfig, function (error /*, result */) {
54+
if (password) {
55+
sshConfig.password = password;
56+
} else if (privateKey) {
57+
sshConfig.privateKey = fs.readFileSync(untildify(privateKey));
58+
}
59+
60+
return new Promise(function(resolve, reject) {
61+
var sshTunnel = tunnel(sshConfig, function(error /*, result */) {
6062
if (error) {
6163
reject(error);
6264
} else {

tests/unit/index-nodetest.js

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
var Promise = require('ember-cli/lib/ext/promise');
44
var assert = require('ember-cli/tests/helpers/assert');
5-
var fs = require('fs');
6-
var path = require('path');
5+
var fs = require('fs');
6+
var path = require('path');
77

88
describe('ssh-tunnel plugin', function() {
99
var subject, mockUi;
@@ -15,7 +15,8 @@ describe('ssh-tunnel plugin', function() {
1515
write: function() { },
1616
writeLine: function(message) {
1717
this.messages.push(message);
18-
}
18+
},
19+
verbose: true
1920
};
2021
});
2122

@@ -60,7 +61,7 @@ describe('ssh-tunnel plugin', function() {
6061
assert.ok(true); // it didn't throw
6162
});
6263

63-
describe('without providing config', function () {
64+
describe('without providing config', function() {
6465
var plugin, context, config;
6566

6667
beforeEach(function() {
@@ -70,7 +71,7 @@ describe('ssh-tunnel plugin', function() {
7071
});
7172

7273
it('raises about missing required config', function() {
73-
config = { };
74+
config = {};
7475
context = {
7576
ui: mockUi,
7677
config: config
@@ -81,9 +82,8 @@ describe('ssh-tunnel plugin', function() {
8182
});
8283
var messages = mockUi.messages.reduce(function(previous, current) {
8384
if (/- Missing required config:\s.*/.test(current)) {
84-
previous.push(current);
85+
previous.push(current);
8586
}
86-
8787
return previous;
8888
}, []);
8989
assert.equal(messages.length, 1);
@@ -108,26 +108,73 @@ previous.push(current);
108108

109109
return previous;
110110
}, []);
111-
assert.equal(messages.length, 5);
111+
assert.equal(messages.length, 4);
112112
});
113113

114114
it('adds default config to the config object', function() {
115+
config = {
116+
'ssh-tunnel': {
117+
host: 'example.com',
118+
username: 'ghedamat'
119+
}
120+
}
115121
context = {
116122
ui: mockUi,
117-
config: {
118-
'ssh-tunnel': {
119-
host: 'example.com',
120-
username: 'ghedamat'
121-
}
122-
}
123+
config: config
123124
};
124125
plugin.beforeHook(context);
125126
plugin.configure(context);
126127
assert.isDefined(config['ssh-tunnel'].dstPort);
127128
assert.isDefined(config['ssh-tunnel'].dstHost);
128129
assert.isDefined(config['ssh-tunnel'].srcPort);
129130
assert.isDefined(config['ssh-tunnel'].tunnelClient);
131+
});
132+
});
133+
134+
describe('with custom authentication provided', function () {
135+
var plugin, context, config;
136+
137+
beforeEach(function() {
138+
plugin = subject.createDeployPlugin({
139+
name: 'ssh-tunnel'
140+
});
141+
});
142+
143+
it('uses a password', function() {
144+
config = {
145+
'ssh-tunnel': {
146+
host: 'example.com',
147+
username: 'example',
148+
password: 'secret'
149+
}
150+
}
151+
context = {
152+
ui: mockUi,
153+
config: config
154+
}
155+
plugin.beforeHook(context);
156+
plugin.configure(context);
157+
assert.isDefined(config['ssh-tunnel'].password);
158+
assert.isUndefined(config['ssh-tunnel'].privateKeyPath);
159+
});
160+
161+
it('uses a key path', function() {
162+
config = {
163+
'ssh-tunnel': {
164+
host: 'example.com',
165+
username: 'example',
166+
privateKeyPath: '~/.ssh/id_rsa'
167+
}
168+
}
169+
context = {
170+
ui: mockUi,
171+
config: config
172+
}
173+
plugin.beforeHook(context);
174+
plugin.configure(context);
175+
assert.isUndefined(config['ssh-tunnel'].password);
130176
assert.isDefined(config['ssh-tunnel'].privateKeyPath);
177+
assert.equal(config['ssh-tunnel'].privateKeyPath, '~/.ssh/id_rsa');
131178
});
132179
});
133180
});

0 commit comments

Comments
 (0)