-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (47 loc) · 1.35 KB
/
Copy pathindex.js
File metadata and controls
56 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const app = express();
const port = 3000;
require('dotenv').config();
app.use(express.json());
const { DefaultAzureCredential } = require('@azure/identity');
const { SecretClient } = require('@azure/keyvault-secrets');
const credential = new DefaultAzureCredential();
const client = new SecretClient(process.env.KEY_VAULT_URI, credential);
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/secret', async (req, res) => {
try {
const secret = await client.getSecret('testsecret');
res.send(secret.value);
} catch (error) {
console.log(error);
res.send(error);
}
});
app.get('/multilinesecret', async (req, res) => {
try {
const secret = await client.getSecret('MultilineSecret');
const parsedSecret = JSON.parse(secret.value);
res.json(parsedSecret);
} catch (error) {
console.log(error);
res.send(error);
}
});
app.post('/secret', async (req, res) => {
try {
const { secretName, secretValue } = req.body;
if (!secretName || !secretValue) {
res.status(400).send('Please provide a secret name and value');
}
await client.setSecret(secretName, secretValue);
res.send('Secret created');
} catch (error) {
console.log(error);
res.send(error);
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});