Skip to content

Commit 6aca501

Browse files
authored
Adding ability to supply an HTTP Agent (#125)
* adding ability to pass custom options to request-promise
1 parent b9e794f commit 6aca501

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,25 @@ Now you are able to run all of the other [examples]:
9393
node example/policies.js
9494
```
9595

96-
[![Backers](https://opencollective.com/node-vault/tiers/backers.svg?avatarHeight=80&width=600)](https://opencollective.com/node-vault/contribute)
96+
##Connecting to vault through a bastion host
97+
To connect to a vault server in a private network with a bastion host, you'll need to first open a connection:
98+
```bash
99+
ssh -D <socks4Port> bastion.example.com
100+
```
101+
102+
```javascript
103+
const SocksProxyAgent = require('socks-proxy-agent');
104+
const agent = new SocksProxyAgent(`socks://127.0.0.1:${socks4Port}`, true);
105+
const options = {
106+
apiVersion: 'v1',
107+
rpOptions: {
108+
agent,
109+
},
110+
};
97111

112+
const vault = require('node-vault')(options);
113+
```
114+
[![Backers](https://opencollective.com/node-vault/tiers/backers.svg?avatarHeight=80&width=600)](https://opencollective.com/node-vault/contribute)
98115
[examples]: https://github.com/kr1sp1n/node-vault/tree/master/example
99116
[docker-compose.yml]: https://github.com/kr1sp1n/node-vault/tree/master/docker-compose.yml
100117
[Vault]: https://vaultproject.io/

src/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let debug = require('debug')('node-vault');
44
let tv4 = require('tv4');
55
let commands = require('./commands.js');
66
let mustache = require('mustache');
7-
const rp = require('request-promise-native');
7+
let rp = require('request-promise-native');
88

99
class VaultError extends Error {}
1010

@@ -24,12 +24,21 @@ module.exports = (config = {}) => {
2424
tv4 = config.tv4 || tv4;
2525
commands = config.commands || commands;
2626
mustache = config.mustache || mustache;
27-
const requestPromise = (config['request-promise'] || rp).defaults({
27+
28+
const rpDefaults = {
2829
json: true,
2930
resolveWithFullResponse: true,
3031
simple: false,
3132
strictSSL: !process.env.VAULT_SKIP_VERIFY,
32-
});
33+
};
34+
35+
if (config.rpDefaults) {
36+
Object.keys(config.rpDefaults).forEach(key => {
37+
rpDefaults[key] = config.rpDefaults[key];
38+
});
39+
}
40+
41+
rp = (config['request-promise'] || rp).defaults(rpDefaults);
3342
const client = {};
3443

3544
function handleVaultResponse(response) {
@@ -88,7 +97,7 @@ module.exports = (config = {}) => {
8897
options.uri = uri;
8998
debug(options.method, uri);
9099
if (options.json) debug(options.json);
91-
return requestPromise(options).then(client.handleVaultResponse);
100+
return rp(options).then(client.handleVaultResponse);
92101
};
93102

94103
client.help = (path, requestOptions) => {

test/unit.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ describe('node-vault', () => {
4343
});
4444
});
4545

46+
it('should set additional values for request library', () => {
47+
const defaultsStub = sinon.stub();
48+
49+
index({
50+
'request-promise': {
51+
defaults: defaultsStub,
52+
},
53+
rpDefaults: {
54+
fakeArgument: 1,
55+
},
56+
});
57+
58+
defaultsStub.should.be.calledOnce();
59+
defaultsStub.should.be.calledWithExactly({
60+
json: true,
61+
simple: false,
62+
resolveWithFullResponse: true,
63+
strictSSL: true,
64+
fakeArgument: 1,
65+
});
66+
});
67+
4668
it('should disable ssl security based on vault environment variable', () => {
4769
const defaultsStub = sinon.stub();
4870

0 commit comments

Comments
 (0)