| 1 | // Read/write runtime settings stored in data/settings.json. |
| 2 | // Env vars are still authoritative on boot; settings.json overrides for hot-edit. |
| 3 | const express = require('express'); |
| 4 | const fs = require('fs'); |
| 5 | const path = require('path'); |
| 6 | |
| 7 | const router = express.Router(); |
| 8 | const FILE = path.resolve(__dirname, '..', 'data', 'settings.json'); |
| 9 | |
| 10 | const DEFAULTS = () => ({ |
| 11 | relayUrl: process.env.AGENT_RELAY_URL || process.env.LITELLM_URL || 'http://localhost:3201', |
| 12 | relayKey: process.env.LITELLM_MASTER_KEY || process.env.RELAY_KEY || 'relay-internal-2026', |
| 13 | model: process.env.CLONAGENT_MODEL || 'claude-opus-4-7', |
| 14 | publicUrl: process.env.PUBLIC_URL || 'https://clonagent.utopiaia.com', |
| 15 | cron: process.env.POLL_CRON || '* * * * *', |
| 16 | integrations: { |
| 17 | giteaUrl: process.env.GITEA_URL_INTERNAL || 'http://localhost:3200', |
| 18 | giteaToken: process.env.GITEA_TOKEN || '', |
| 19 | giteaUser: process.env.GITEA_ADMIN_USER || '', |
| 20 | pgContainer: process.env.PG_CONTAINER || 'postgres-keycloak', |
| 21 | pgSuperUser: process.env.PG_SUPER_USER || 'keycloak', |
| 22 | pgSuperDb: process.env.PG_SUPER_DB || 'keycloak', |
| 23 | }, |
| 24 | auth: { |
| 25 | mode: process.env.AUTH_MODE || 'basic', // 'none' | 'basic' | 'oidc' |
| 26 | allowRegistration: process.env.ALLOW_REGISTRATION !== '0', // open by default |
| 27 | basic: { |
| 28 | users: [], // legacy — migrated to users.json on boot |
| 29 | }, |
| 30 | oidc: { |
| 31 | issuerUrl: process.env.OIDC_ISSUER_URL || 'https://keycloak.utopiaia.com/realms/kikocisbot', |
| 32 | clientId: process.env.OIDC_CLIENT_ID || 'clonagent', |
| 33 | clientSecret: process.env.OIDC_CLIENT_SECRET || '', |
| 34 | redirectUri: process.env.OIDC_REDIRECT_URI || 'https://clonagent.utopiaia.com/api/auth/oidc/callback', |
| 35 | }, |
| 36 | }, |
| 37 | }); |
| 38 | |
| 39 | function read() { |
| 40 | let saved = {}; |
| 41 | try { saved = JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch {} |
| 42 | const def = DEFAULTS(); |
| 43 | return { |
| 44 | ...def, |
| 45 | ...saved, |
| 46 | integrations: { ...def.integrations, ...(saved.integrations || {}) }, |
| 47 | auth: { |
| 48 | ...def.auth, |
| 49 | ...(saved.auth || {}), |
| 50 | basic: { ...def.auth.basic, ...(saved.auth?.basic || {}) }, |
| 51 | oidc: { ...def.auth.oidc, ...(saved.auth?.oidc || {}) }, |
| 52 | }, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | function write(patch) { |
| 57 | const next = { ...read() }; |
| 58 | for (const k of Object.keys(patch)) { |
| 59 | if (k === 'auth' && typeof patch.auth === 'object') { |
| 60 | next.auth = { |
| 61 | ...next.auth, |
| 62 | ...patch.auth, |
| 63 | basic: { ...next.auth.basic, ...(patch.auth.basic || {}) }, |
| 64 | oidc: { ...next.auth.oidc, ...(patch.auth.oidc || {}) }, |
| 65 | }; |
| 66 | } else if (k === 'integrations' && typeof patch.integrations === 'object') { |
| 67 | next.integrations = { ...next.integrations, ...patch.integrations }; |
| 68 | } else { |
| 69 | next[k] = patch[k]; |
| 70 | } |
| 71 | } |
| 72 | fs.mkdirSync(path.dirname(FILE), { recursive: true }); |
| 73 | fs.writeFileSync(FILE, JSON.stringify(next, null, 2)); |
| 74 | // Propagate to env so other modules pick it up |
| 75 | if (patch.relayUrl) process.env.LITELLM_URL = patch.relayUrl; |
| 76 | if (patch.relayKey) process.env.LITELLM_MASTER_KEY = patch.relayKey; |
| 77 | if (patch.model) process.env.CLONAGENT_MODEL = patch.model; |
| 78 | if (patch.publicUrl) process.env.PUBLIC_URL = patch.publicUrl; |
| 79 | if (patch.cron) process.env.POLL_CRON = patch.cron; |
| 80 | // Reset OIDC client on auth change |
| 81 | if (patch.auth) { |
| 82 | try { require('../lib/auth').resetOidc(); } catch {} |
| 83 | } |
| 84 | return next; |
| 85 | } |
| 86 | |
| 87 | function maskSecrets(s) { |
| 88 | if (!s) return s; |
| 89 | return s.replace(/^(.{4}).*(.{2})$/, '$1…$2'); |
| 90 | } |
| 91 | |
| 92 | function safeView(s) { |
| 93 | return { |
| 94 | ...s, |
| 95 | relayKey: maskSecrets(s.relayKey), |
| 96 | integrations: { |
| 97 | ...s.integrations, |
| 98 | giteaToken: maskSecrets(s.integrations?.giteaToken), |
| 99 | }, |
| 100 | auth: { |
| 101 | ...s.auth, |
| 102 | basic: { |
| 103 | users: (s.auth?.basic?.users || []).map(u => ({ |
| 104 | username: u.username, name: u.name, email: u.email, |
| 105 | // never expose passwordHash |
| 106 | })), |
| 107 | }, |
| 108 | oidc: { |
| 109 | ...s.auth?.oidc, |
| 110 | clientSecret: maskSecrets(s.auth?.oidc?.clientSecret), |
| 111 | }, |
| 112 | }, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | router.get('/', (req, res) => { |
| 117 | if (!req.session?.user?.isAdmin) return res.status(403).json({ error: 'admin only' }); |
| 118 | res.json(safeView(read())); |
| 119 | }); |
| 120 | |
| 121 | router.patch('/', (req, res) => { |
| 122 | if (!req.session?.user?.isAdmin) return res.status(403).json({ error: 'admin only' }); |
| 123 | const patch = req.body || {}; |
| 124 | // Don't overwrite secrets with their masked values |
| 125 | if (patch.relayKey && patch.relayKey.includes('…')) delete patch.relayKey; |
| 126 | if (patch.auth?.oidc?.clientSecret && patch.auth.oidc.clientSecret.includes('…')) |
| 127 | delete patch.auth.oidc.clientSecret; |
| 128 | if (patch.integrations?.giteaToken && patch.integrations.giteaToken.includes('…')) |
| 129 | delete patch.integrations.giteaToken; |
| 130 | res.json(safeView(write(patch))); |
| 131 | }); |
| 132 | |
| 133 | module.exports = { router, read, write }; |