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); require('./src/endpoints/presets').registerEndpoints(app, jsonParser);
// Secrets managemenet // Secrets managemenet
require('./src/endpoints/secrets').registerEndpoints(app, jsonParser); app.use('/api/secrets', require('./src/endpoints/secrets').router);
// Thumbnail generation // Thumbnail generation
require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser); require('./src/endpoints/thumbnails').registerEndpoints(app, jsonParser);

View File

@ -1,7 +1,9 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const express = require('express');
const { getConfigValue } = require('../util'); const { getConfigValue } = require('../util');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser } = require('../express-common');
const SECRETS_FILE = path.join(process.cwd(), './secrets.json'); const SECRETS_FILE = path.join(process.cwd(), './secrets.json');
const SECRET_KEYS = { const SECRET_KEYS = {
@ -143,14 +145,9 @@ function getAllSecrets() {
return secrets; return secrets;
} }
/** const router = express.Router();
* Registers endpoints for the secret management API
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/secrets/write', jsonParser, (request, response) => { router.post('/write', jsonParser, (request, response) => {
const key = request.body.key; const key = request.body.key;
const value = request.body.value; const value = request.body.value;
@ -158,8 +155,7 @@ function registerEndpoints(app, jsonParser) {
return response.send('ok'); return response.send('ok');
}); });
app.post('/api/secrets/read', jsonParser, (_, response) => { router.post('/read', jsonParser, (_, response) => {
try { try {
const state = readSecretState(); const state = readSecretState();
return response.send(state); return response.send(state);
@ -169,7 +165,7 @@ function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/secrets/view', jsonParser, async (_, response) => { router.post('/view', jsonParser, async (_, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false); const allowKeysExposure = getConfigValue('allowKeysExposure', false);
if (!allowKeysExposure) { if (!allowKeysExposure) {
@ -191,7 +187,7 @@ function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/secrets/find', jsonParser, (request, response) => { router.post('/find', jsonParser, (request, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false); const allowKeysExposure = getConfigValue('allowKeysExposure', false);
if (!allowKeysExposure) { if (!allowKeysExposure) {
@ -214,7 +210,6 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
}
module.exports = { module.exports = {
writeSecret, writeSecret,
@ -222,6 +217,6 @@ module.exports = {
readSecretState, readSecretState,
migrateSecrets, migrateSecrets,
getAllSecrets, getAllSecrets,
registerEndpoints,
SECRET_KEYS, SECRET_KEYS,
router,
}; };