Make nmon easier to install and run without editing YAML by hand: - Settings: point-and-click forms over config.yaml (GET/PUT /api/v1/settings/form); the YAML editor moves to /settings/yaml. - First install: with no sign-in configured nmon starts in install mode, serves only the setup wizard (sign-in, basics, devices, alerting) and closes it for good once sign-in is saved. A self-signed certificate is generated when no TLS source is given. poller.interval becomes a config setting. - Reverse proxies: opt-in --http-listen plain-HTTP listener beside HTTPS; sign-in stays required. - Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets, template preview and test send. - Help: the operations, coverage and profile docs served at /help. - Polling: per-device "Poll now", a dry-run test poll returning an SNMP trace, and time-limited in-memory SNMP debug recording, in the UI and as `nmon device poll`. - Ansible: deploys into install mode when no sign-in is set, and no longer overwrites the config.yaml the wizard wrote. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
124 lines
5.2 KiB
TypeScript
124 lines
5.2 KiB
TypeScript
// Mock of the first-install wizard: GET /setup, the sign-in step, the recommended policies, completion, and the
|
|
// install-mode gate (every route outside the wizard's allow-list answers 503 "setup not finished"). The device
|
|
// list is the wizard's own: it starts empty, POST /devices adds a device that is "discovering" for two list reads
|
|
// and then discovered, so the live status can be watched.
|
|
import type { components } from '../../src/api/schema.d.ts'
|
|
import type { Reply } from './groups.ts'
|
|
|
|
type S = components['schemas']
|
|
|
|
export type SetupMode = 'install' | 'completed'
|
|
|
|
const ALLOWED = [
|
|
/^\/setup/,
|
|
/^\/meta$/,
|
|
/^\/settings\/form$/,
|
|
/^\/notify\/(presets|preview|test)$/,
|
|
/^\/devices(\/import|\/[^/]+)?$/,
|
|
/^\/alert-policies$/,
|
|
/^\/profiles$/,
|
|
/^\/secrets\/[^/]+$/,
|
|
]
|
|
|
|
const RECOMMENDED = ['device_down', 'device_warning', 'interface_errors', 'cpu_high', 'memory_high', 'disk_full'].map((r) => `recommended: ${r}`)
|
|
|
|
const problem = (status: number, title: string, detail: string, errors?: { message: string; location: string }[]): Reply => ({
|
|
status,
|
|
body: { type: 'about:blank', title, status, detail, ...(errors && { errors }) },
|
|
})
|
|
|
|
interface Added {
|
|
view: S['DeviceSummary']
|
|
reads: number
|
|
}
|
|
|
|
export class SetupMock {
|
|
private installing: boolean
|
|
private signedIn = false
|
|
private done: boolean
|
|
private policies = 0
|
|
private readonly added: Added[] = []
|
|
/** Every body POSTed to /setup/signin, so a spec can check what was sent. */
|
|
readonly signins: unknown[] = []
|
|
|
|
constructor(
|
|
mode: SetupMode | undefined,
|
|
private readonly template: () => S['DeviceView'],
|
|
) {
|
|
this.installing = mode === 'install'
|
|
this.done = mode !== 'install'
|
|
}
|
|
|
|
/** In install mode the browser has no session, so the page-level guard (CSRF, roles) must not run. */
|
|
get open(): boolean {
|
|
return this.installing
|
|
}
|
|
|
|
private state(): S['SetupState'] {
|
|
return {
|
|
install_mode: this.installing,
|
|
completed: this.done,
|
|
steps: this.installing ? { sign_in: this.signedIn, devices: this.added.length, receivers: 0, policies: this.policies } : { sign_in: false, devices: 0, receivers: 0, policies: 0 },
|
|
defaults: { break_glass_user: 'admin', min_signin_length: 12, oidc_secret_name: 'oidc-client', timezone: 'UTC', retention: { hot: '30d', fiveminute: '120d', long: '2y' }, poll_interval: '5m' },
|
|
}
|
|
}
|
|
|
|
route(method: string, p: string, json: () => unknown): Reply | undefined {
|
|
if (p === '/setup' && method === 'GET') return { status: 200, body: this.state() }
|
|
if (!this.installing) return /^\/setup\//.test(p) && method === 'POST' ? problem(409, 'Conflict', 'setup is closed') : undefined
|
|
if (!ALLOWED.some((re) => re.test(p))) return problem(503, 'Service Unavailable', 'setup not finished')
|
|
return this.handlers(json)[`${method} ${p}`]?.()
|
|
}
|
|
|
|
private handlers(json: () => unknown): Record<string, () => Reply> {
|
|
return {
|
|
'POST /setup/signin': () => this.signIn(json() as S['SetupSignInBody']),
|
|
'POST /setup/recommended-policies': () => this.recommend(),
|
|
'POST /setup/complete': () => this.complete(),
|
|
'GET /devices': () => this.list(),
|
|
'POST /devices': () => this.addDevice(json() as S['AddDeviceBody']),
|
|
}
|
|
}
|
|
|
|
private signIn(b: S['SetupSignInBody']): Reply {
|
|
this.signins.push(b)
|
|
if (b.password.length < 12) return problem(422, 'Unprocessable Entity', 'password: too short', [{ message: 'password: too short', location: 'body.password' }])
|
|
this.signedIn = true
|
|
return { status: 200, body: { break_glass_user: b.username, oidc: !!b.oidc } satisfies S['SetupSignIn'] }
|
|
}
|
|
|
|
private recommend(): Reply {
|
|
const created = this.policies === 0 ? RECOMMENDED : []
|
|
this.policies = RECOMMENDED.length
|
|
return { status: 200, body: { created, existing: created.length ? [] : RECOMMENDED } satisfies S['SetupPolicies'] }
|
|
}
|
|
|
|
private complete(): Reply {
|
|
if (!this.signedIn) return problem(409, 'Conflict', 'set up a sign-in method first: setup cannot finish without one')
|
|
this.installing = false
|
|
this.done = true
|
|
return { status: 200, body: { sign_in_url: '/auth/breakglass' } satisfies S['SetupComplete'] }
|
|
}
|
|
|
|
private list(): Reply {
|
|
const items = this.added.map((a) => {
|
|
a.reads++
|
|
const ready = a.reads > 2
|
|
return { ...a.view, profile: ready ? 'generic' : '', interfaces_total: ready ? 8 : 0, reachability: ready ? ('up' as const) : ('unknown' as const) }
|
|
})
|
|
return { status: 200, body: { items, page: 1, per_page: 100, total: items.length } satisfies S['DeviceList'] }
|
|
}
|
|
|
|
private addDevice(b: S['AddDeviceBody']): Reply {
|
|
const addr = b.addresses[0] ?? ''
|
|
if (addr.endsWith('.99')) return problem(422, 'Unprocessable Entity', `device did not answer SNMP at ${addr}`)
|
|
const view = { ...this.template(), name: b.name, slug: `${b.name.replace(/[^a-z0-9]/gi, '-')}-wiz1`, mgmt_ip: addr, profile: 'generic' }
|
|
const summary: S['DeviceSummary'] = {
|
|
slug: view.slug, name: view.name, kind: 'network', site: '', role: '', mgmt_ip: addr, profile: '', reachability: 'unknown', poll_state: 'ok',
|
|
last_seen: null, interfaces_total: 0, interfaces_down: 0, warnings: [],
|
|
}
|
|
this.added.push({ view: summary, reads: 0 })
|
|
return { status: 201, body: view }
|
|
}
|
|
}
|