Created
November 9, 2018 09:21
-
-
Save yurenju/c9b9e67824662796bb130a609d6d165a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
process.env.DEBUG = "node-vault"; // switch on debug mode | |
require("dotenv").config(); | |
const mysql = require("promise-mysql"); | |
const Vault = require("node-vault"); | |
const { VAULT_TOKEN } = process.env; | |
const vault = Vault({ token: VAULT_TOKEN }); | |
let credential; | |
async function issueCredential() { | |
credential = await vault.read("database/creds/my-role"); | |
const { username, password } = credential.data; | |
const leaseDuration = credential.lease_duration; | |
lease_id = credential.lease_id; | |
const info = [ | |
`Got new credential!`, | |
` username: ${username}`, | |
` password: ${password}`, | |
` lease duration: ${leaseDuration}` | |
]; | |
console.log(info.join("\n")); | |
global.setTimeout(() => { | |
console.log(`Credential will expire in ${leaseDuration / 2} seconds, rotate it.`); | |
issueCredential(); | |
}, (leaseDuration * 1000) / 2); | |
} | |
async function gracefulShutdown() { | |
console.info("SIGTERM signal received."); | |
await vault.revoke({ lease_id: credential.lease_id }); | |
process.exit(0); | |
} | |
async function loop() { | |
try { | |
const { username, password } = credential.data; | |
const conn = await mysql.createConnection({ | |
host: "localhost", | |
user: username, | |
password: password | |
}); | |
const result = await conn.query("SELECT USER()"); | |
console.log(`Current user: ${result[0]["USER()"].split("@")[0]}`); | |
conn.end(); | |
} catch (e) { | |
console.error(e.sqlMessage); | |
issueCredential(); | |
} | |
} | |
function main() { | |
issueCredential(); | |
global.setInterval(loop, 1000); | |
process.on("SIGTERM", gracefulShutdown); | |
} | |
if (require.main === module) { | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment