Files
claude-code-conduit/server/index.mjs
mikael-lovqvists-claude-agent ba8c0701f8 Refactor server config: single --config flag replaces --secrets/--mail-perms
- New server/config.mjs loads config.json, resolves secrets path relative
  to config dir, returns users/smtp/mail_perms_path/bind/port
- server/secrets.mjs removed (logic absorbed into config.mjs)
- smtp moves from secrets.json to config.json
- secrets.json now contains only users (pure credentials)
- config.example.json added as reference template
- .gitignore/.npmignore updated to cover config.json and mail-perms.json
- README updated with new setup and flags

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-17 23:16:30 +00:00

185 lines
5.3 KiB
JavaScript

import express from 'express';
import { actions } from './actions.mjs';
import { enqueue, get_entry, list_pending, resolve } from './queue.mjs';
import { load_config } from './config.mjs';
import { create_auth_middleware, check_can_approve } from './auth.mjs';
import { create_mailer } from './mailer.mjs';
import { exec as real_exec } from './helpers.mjs';
import { load_mail_perms } from './mail_perms.mjs';
function get_arg(argv, flag) {
const i = argv.indexOf(flag);
return i !== -1 ? argv[i + 1] : null;
}
function ts() {
return new Date().toLocaleTimeString();
}
const VERBOSE = process.argv.includes('--verbose');
const DRY_RUN = process.argv.includes('--dry-run');
const config_path = get_arg(process.argv, '--config') || process.env.CONDUIT_CONFIG;
let cfg;
try {
cfg = load_config(config_path);
} catch (err) {
console.error(`Fatal: ${err.message}`);
process.exit(1);
}
const { users, smtp, mail_perms_path } = cfg;
const PORT = process.env.CONDUIT_PORT || cfg.port || 3015;
const BIND = process.env.CONDUIT_BIND || cfg.bind || '127.0.0.1';
let mail_perm_store;
try {
mail_perm_store = load_mail_perms(mail_perms_path);
} catch (err) {
console.error(`Fatal: ${err.message}`);
process.exit(1);
}
if (!mail_perms_path) {
console.warn('Warning: mail_perms not set in config; mail permissions will not persist across restarts');
}
const real_mailer_send = create_mailer(smtp);
const app = express();
app.use(express.json({
verify: (req, _res, buf) => {
req.raw_body = buf.toString('utf8');
},
}));
app.use(create_auth_middleware(users));
app.use((req, _res, next) => {
const is_debug = req.method === 'GET' && req.path === '/queue';
if (!is_debug || VERBOSE) {
console.log(`[${ts()}] ${req.method} ${req.path}${req.conduit_user}`);
}
next();
});
function make_ctx(caller) {
const exec = DRY_RUN
? (bin, args) => console.log(`[${ts()}] [DRY-RUN] exec: ${bin} ${JSON.stringify(args)}`)
: real_exec;
const mailer_send = DRY_RUN
? async (to, subject) => console.log(`[${ts()}] [DRY-RUN] send-mail: to=${to} subject=${JSON.stringify(subject)}`)
: real_mailer_send;
return { caller, users, mail_perm_store, exec, mailer_send };
}
async function run_action(def, params, ctx) {
return def.handler(params, ctx);
}
function validate_params(action_def, params) {
const errors = [];
for (const p of action_def.params) {
if (p.required && (params[p.name] === undefined || params[p.name] === null)) {
errors.push(`Missing required param: ${p.name}`);
}
}
return errors;
}
// POST /action — main entry point
app.post('/action', async (req, res) => {
const { action, ...params } = req.body ?? {};
if (!action) {
return res.status(400).json({ error: "Missing 'action' field" });
}
const def = actions[action];
if (!def) {
return res.status(404).json({ error: `Unknown action: ${action}` });
}
const errors = validate_params(def, params);
if (errors.length) {
return res.status(400).json({ error: 'Invalid params', details: errors });
}
if (def.policy === 'auto-deny') {
return res.status(403).json({ status: 'denied', reason: 'Policy: auto-deny' });
}
const ctx = make_ctx(req.conduit_user);
if (def.policy === 'auto-accept') {
try {
const result = await run_action(def, params, ctx);
return res.json({ status: 'accepted', result });
} catch (err) {
return res.status(500).json({ status: 'error', error: err.message });
}
}
if (def.policy === 'queue') {
const id = enqueue(action, params, req.conduit_user);
return res.status(202).json({ status: 'queued', id });
}
});
// GET /queue — list pending items
app.get('/queue', (req, res) => {
res.json(list_pending());
});
// POST /queue/:id/approve — user approves a queued action
app.post('/queue/:id/approve', async (req, res) => {
const entry = get_entry(req.params.id);
if (!entry) {
return res.status(404).json({ error: 'Not found' });
}
if (entry.status !== 'pending') {
return res.status(409).json({ error: 'Already resolved' });
}
if (!check_can_approve(users, req.conduit_user, entry.submitted_by)) {
return res.status(403).json({ error: 'Not authorized to approve this entry' });
}
entry.resolved_by = req.conduit_user;
resolve(req.params.id, 'approved');
const ctx = make_ctx(entry.submitted_by);
const def = actions[entry.action];
try {
const result = await run_action(def, entry.params, ctx);
res.json({ status: 'approved', result });
} catch (err) {
res.status(500).json({ status: 'error', error: err.message });
}
});
// POST /queue/:id/deny — user denies a queued action
app.post('/queue/:id/deny', (req, res) => {
const entry = get_entry(req.params.id);
if (!entry) {
return res.status(404).json({ error: 'Not found' });
}
if (entry.status !== 'pending') {
return res.status(409).json({ error: 'Already resolved' });
}
if (!check_can_approve(users, req.conduit_user, entry.submitted_by)) {
return res.status(403).json({ error: 'Not authorized to deny this entry' });
}
entry.resolved_by = req.conduit_user;
resolve(req.params.id, 'denied');
res.json({ status: 'denied' });
});
app.listen(PORT, BIND, () => {
console.log(`claude-code-conduit server running on ${BIND}:${PORT}`);
console.log(`Workspace root: ${process.env.CONDUIT_ROOT || '/workspace'}`);
if (DRY_RUN) {
console.log('DRY-RUN mode enabled — actions will be logged but not executed');
}
});