This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Using express backend server to make calls to the Vault API to bypass CORs - Adding login, list secrets, and get secrets calls
- Loading branch information
DJ Enriquez
committed
Nov 8, 2016
1 parent
1ae4d09
commit 4e8ec55
Showing
5 changed files
with
166 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
var axios = require('axios'); | ||
var _ = require('lodash'); | ||
|
||
/* Returned body | ||
"auth": { | ||
"renewable": true, | ||
"lease_duration": 2764800, | ||
"metadata": { | ||
"username": "vishalnayak", | ||
"org": "hashicorp" | ||
}, | ||
"policies": [ | ||
"default", | ||
"dev-policy" | ||
], | ||
"accessor": "f93c4b2d-18b6-2b50-7a32-0fecf88237b8", | ||
"client_token": "1977fceb-3bfa-6c71-4d1f-b64af98ac018" | ||
} | ||
*/ | ||
var login = function (req, res) { | ||
let creds = _.get(req, "body.Creds"); | ||
|
||
let endpoint = ''; | ||
let body = {} | ||
|
||
switch (creds.Type.toLowerCase()) { | ||
case 'github': | ||
endpoint = '/v1/auth/github/login'; | ||
body = { | ||
token: creds.Token | ||
}; | ||
} | ||
axios.post(`${_.get(req, "body.VaultUrl")}${endpoint}`, body) | ||
.then((resp) => { | ||
res.json(resp.data.auth); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack); | ||
}); | ||
}; | ||
|
||
/* Returned body | ||
{ | ||
"auth": null, | ||
"data": { | ||
"keys": ["foo", "foo/"] | ||
}, | ||
"lease_duration": 2764800, | ||
"lease_id": "", | ||
"renewable": false | ||
} | ||
*/ | ||
var listSecrets = function (req, res) { | ||
let endpoint = '/v1/secret?list=true'; | ||
let vaultAddr = decodeURI(req.query['vaultaddr']); | ||
let config = { headers : { 'X-Vault-Token': decodeURI(req.query['token']) } } | ||
console.log(`${vaultAddr}${endpoint}`); | ||
axios.get(`${vaultAddr}${endpoint}`, config) | ||
.then((resp) => { | ||
res.json(resp.data); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack); | ||
}); | ||
} | ||
/* Returned body | ||
{ | ||
"foo": "bar" | ||
} | ||
Query params 'secret' and 'vaultaddr' must go through encodeURI() | ||
*/ | ||
var getSecret = function (req, res) { | ||
let endpoint = `/v1/secret/${decodeURI(req.query['secret'])}`; | ||
let vaultAddr = decodeURI(req.query['vaultaddr']); | ||
let config = { headers : { 'X-Vault-Token': req.query['token'] } } | ||
|
||
axios.get(`${vaultAddr}${endpoint}`, config) | ||
.then((resp) => { | ||
res.json(resp.data.data); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack); | ||
}); | ||
} | ||
|
||
module.exports = (function () { | ||
return { | ||
login: login, | ||
listSecrets: listSecrets, | ||
getSecret: getSecret | ||
} | ||
})(); |