Skip to content

Commit a99ee94

Browse files
committed
Merge pull request #288 from nodevault/copilot/fix-issue-with-vault-api
Strip trailing slash from endpoint to prevent malformed URIs
1 parent 68a10ca commit a99ee94

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ module.exports = (config = {}) => {
198198
client.namespace = config.namespace || process.env.VAULT_NAMESPACE;
199199
client.kubernetesPath = config.kubernetesPath || 'kubernetes';
200200

201+
client.endpoint = client.endpoint.replace(/\/$/, '');
202+
201203
const requestSchema = {
202204
type: 'object',
203205
properties: {

test/unit.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,64 @@ describe('node-vault', () => {
8787
});
8888
});
8989

90+
describe('client initialization', () => {
91+
it('should not trim endpoint if no trailing slash', () => {
92+
const defaultsStub = sinon.stub();
93+
const vaultConfig = {
94+
endpoint: 'http://localhost:8200',
95+
'request-promise': {
96+
defaults: defaultsStub,
97+
},
98+
};
99+
const vault = index(vaultConfig);
100+
vault.endpoint.should.equal('http://localhost:8200');
101+
});
102+
103+
it('should trim endpoint if trailing slash', () => {
104+
const defaultsStub = sinon.stub();
105+
const vaultConfig = {
106+
endpoint: 'http://localhost:8200/',
107+
'request-promise': {
108+
defaults: defaultsStub,
109+
},
110+
};
111+
const vault = index(vaultConfig);
112+
vault.endpoint.should.equal('http://localhost:8200');
113+
});
114+
115+
it('should not produce double slashes in request URI when endpoint has trailing slash', (done) => {
116+
const request = sinon.stub();
117+
const response = sinon.stub();
118+
response.statusCode = 200;
119+
request.returns({
120+
then(fn) {
121+
return fn(response);
122+
},
123+
catch(fn) {
124+
return fn();
125+
},
126+
});
127+
128+
const vault = index({
129+
endpoint: 'http://localhost:8200/',
130+
token: '123',
131+
'request-promise': {
132+
defaults: () => request,
133+
},
134+
});
135+
136+
vault.read('secret/hello')
137+
.then(() => {
138+
request.should.have.calledOnce();
139+
const uri = request.firstCall.args[0].uri;
140+
uri.should.equal('http://localhost:8200/v1/secret/hello');
141+
uri.should.not.contain('//v1');
142+
done();
143+
})
144+
.catch(done);
145+
});
146+
});
147+
90148
describe('client', () => {
91149
let request = null;
92150
let response = null;

0 commit comments

Comments
 (0)