Use Express router for secrets endpoint

This commit is contained in:
valadaptive 2023-12-04 12:55:13 -05:00
parent 414c9bd5fb
commit 091255d451
2 changed files with 55 additions and 60 deletions

View File

@ -3585,7 +3585,7 @@ require('./src/endpoints/tokenizers').registerEndpoints(app, jsonParser);
require('./src/endpoints/presets').registerEndpoints(app, jsonParser);
// Secrets managemenet
require('./src/endpoints/secrets').registerEndpoints(app, jsonParser);
app.use('/api/secrets', require('./src/endpoints/secrets').router);
// Thumbnail generation
require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser);

View File

@ -1,7 +1,9 @@
const fs = require('fs');
const path = require('path');
const express = require('express');
const { getConfigValue } = require('../util');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser } = require('../express-common');
const SECRETS_FILE = path.join(process.cwd(), './secrets.json');
const SECRET_KEYS = {
@ -143,78 +145,71 @@ function getAllSecrets() {
return secrets;
}
/**
* Registers endpoints for the secret management API
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
const router = express.Router();
app.post('/api/secrets/write', jsonParser, (request, response) => {
const key = request.body.key;
const value = request.body.value;
router.post('/write', jsonParser, (request, response) => {
const key = request.body.key;
const value = request.body.value;
writeSecret(key, value);
return response.send('ok');
});
writeSecret(key, value);
return response.send('ok');
});
app.post('/api/secrets/read', jsonParser, (_, response) => {
router.post('/read', jsonParser, (_, response) => {
try {
const state = readSecretState();
return response.send(state);
} catch (error) {
console.error(error);
return response.send({});
}
});
try {
const state = readSecretState();
return response.send(state);
} catch (error) {
console.error(error);
return response.send({});
}
});
router.post('/view', jsonParser, async (_, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
app.post('/api/secrets/view', jsonParser, async (_, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
if (!allowKeysExposure) {
console.error('secrets.json could not be viewed unless the value of allowKeysExposure in config.yaml is set to true');
return response.sendStatus(403);
}
if (!allowKeysExposure) {
console.error('secrets.json could not be viewed unless the value of allowKeysExposure in config.yaml is set to true');
return response.sendStatus(403);
try {
const secrets = getAllSecrets();
if (!secrets) {
return response.sendStatus(404);
}
try {
const secrets = getAllSecrets();
return response.send(secrets);
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
if (!secrets) {
return response.sendStatus(404);
}
router.post('/find', jsonParser, (request, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
return response.send(secrets);
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
if (!allowKeysExposure) {
console.error('Cannot fetch secrets unless allowKeysExposure in config.yaml is set to true');
return response.sendStatus(403);
}
app.post('/api/secrets/find', jsonParser, (request, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
const key = request.body.key;
if (!allowKeysExposure) {
console.error('Cannot fetch secrets unless allowKeysExposure in config.yaml is set to true');
return response.sendStatus(403);
try {
const secret = readSecret(key);
if (!secret) {
response.sendStatus(404);
}
const key = request.body.key;
try {
const secret = readSecret(key);
if (!secret) {
response.sendStatus(404);
}
return response.send({ value: secret });
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
}
return response.send({ value: secret });
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
module.exports = {
writeSecret,
@ -222,6 +217,6 @@ module.exports = {
readSecretState,
migrateSecrets,
getAllSecrets,
registerEndpoints,
SECRET_KEYS,
router,
};