Skip to content

Instantly share code, notes, and snippets.

@khle
Last active August 25, 2021 23:03
Show Gist options
  • Save khle/35106e4330c535d2320ff178e0caf898 to your computer and use it in GitHub Desktop.
Save khle/35106e4330c535d2320ff178e0caf898 to your computer and use it in GitHub Desktop.
app.js
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
const { DefaultAzureCredential } = require('@azure/identity')
const credentials = new DefaultAzureCredential()
//console.log(credentials)
const { SecretClient } = require('@azure/keyvault-secrets')
//Build the URL to reach your key vault
const vaultName = 'MyStageAzureSecretVault'
const keyVaultUrl = `https://${vaultName}.vault.azure.net`
// Then, create our secrets client and connect to the service
const secretClient = new SecretClient(keyVaultUrl, credentials)
const secretNames = [
'DatabasePassword',
'DatabaseUserId'
]
const promises = secretNames.map(secretName =>
secretClient.getSecret(secretName)
)
Promise.all(promises).then(results => {
results.forEach(result => {
//Optionally add as properties to process.env so they can be available elsewhere
process.env[result.name] = result.value
})
//Make sure
//console.log(process.env.DatabasePassword)
//console.log(process.env.DatabaseUserId)
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment