Merge pull request #1376 from bdashore3/staging

Secrets: Add find endpoint to retrieve a secret value (with conditions)
This commit is contained in:
Cohee
2023-11-20 19:12:24 +02:00
committed by GitHub
2 changed files with 41 additions and 0 deletions

View File

@ -116,6 +116,23 @@ export async function readSecretState() {
}
}
export async function findSecret(key) {
try {
const response = await fetch('/api/secrets/find', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ key }),
});
if (response.ok) {
const data = await response.json();
return data.value
}
} catch {
console.error('Could not find secret value: ', key);
}
}
function authorizeOpenRouter() {
const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(location.origin)}`;
location.href = openRouterUrl;

View File

@ -190,6 +190,30 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500);
}
});
app.post('/api/secrets/find', jsonParser, (request, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
if (!allowKeysExposure) {
console.error('Cannot fetch secrets unless allowKeysExposure in config.conf is set to true');
return response.sendStatus(403);
}
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);
}
});
}
module.exports = {