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.
Merge pull request #1 from Lucretius/hook-into-vault
Hook into vault
- Loading branch information
Showing
7 changed files
with
227 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM node:slim | ||
|
||
MAINTAINER Team Lucretius | ||
|
||
ADD package.json /tmp/package.json | ||
RUN cd /tmp && npm install | ||
RUN mkdir -p /app/ && cp -a /tmp/node_modules /app/ | ||
|
||
RUN npm install --silent -g webpack | ||
|
||
ADD . /app | ||
WORKDIR /app | ||
|
||
RUN webpack && npm run build | ||
|
||
EXPOSE 8000 | ||
|
||
CMD ["npm", "run", "serve"] |
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,47 @@ | ||
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var path = require('path'); | ||
var axios = require('axios'); | ||
var _ = require('lodash'); | ||
var routeHandler = require('./src/routeHandler'); | ||
|
||
const PORT = 8000; | ||
|
||
var app = express(); | ||
app.set('view engine','.html'); | ||
app.use('/assets', express.static('dist')); | ||
|
||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
// parse application/json | ||
app.use(bodyParser.json()); | ||
|
||
app.use((req, res, next) => { | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | ||
next(); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Vault UI listening on: ${PORT}`); | ||
}); | ||
|
||
|
||
app.post('/login', (req,res) => { | ||
routeHandler.login(req, res); | ||
}); | ||
|
||
app.get('/listsecrets', (req, res) => { | ||
routeHandler.listSecrets(req, res); | ||
}); | ||
|
||
app.get('/secret', (req, res) => { | ||
routeHandler.getSecret(req, res); | ||
}) | ||
|
||
app.get('/') | ||
|
||
app.get('*', function(req, res) { | ||
res.sendFile(path.join(__dirname,'/index.html')); | ||
}); |
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 | ||
} | ||
})(); |