Files
MIKROTIK-RMM/backend/src/services/deviceCreation.ts
T
2GT-RichandClaude Opus 5 7b994ad935 feat(sites): one install, many networks (v0.24.0-beta)
Closes #130. Multi-site, not multi-tenancy: a site is a named collection of
devices whose clients, events, topology and dashboards are viewed separately.
Sites separate what you see, not who you are — the admin/manage/read-only
roles are unchanged and every user sees every site. Per-site permissions
would have multiplied the work for a need nobody stated.

Two measurements decided the design.

Every device-derived table hangs off devices(id) — 25 of 50 tables, 35
foreign keys — so a single site_id column on devices scopes clients, events,
topology, backups and the rest transitively. No other table needed one.

The frontend has 162 useQuery call sites across 57 files. Threading a site
through each would be a large change that leaves a permanent trap: every
future query another chance to forget. The site is injected in the existing
axios interceptor instead, beside the auth token, so a new query is scoped by
default. Only 23 collection routes needed filtering; the 131 per-device
routes are already scoped by their device.

Existing installs are untouched. Migration creates one "Default Site", adopts
every device into it, and a single-site install never meets the concept. An
absent X-Site-Id header means unscoped, so existing API tokens and scripts
behave exactly as before.

Polling stays fleet-wide. Sites are a view over data already gathered, not a
change to how it is gathered — a device does not stop being monitored because
you are looking elsewhere. Rogue-AP detection scopes the scans but keeps the
"these are ours" BSSID set fleet-wide, or your own AP in another site reads
as an evil twin. Bulk actions do obey the selector: an operation that
silently spanned two customers is the blast radius the wave guards exist to
contain.

Four rounds of defects came out of testing the UI rather than the API, and
the first is the one worth recording. Switching sites called
queryClient.clear(), which empties the cache and reviews as correct. It
isn't: clear() destroys each query but a *mounted* observer keeps its own
reference and last result, so every rendered page went on showing the
previous site's data. The QueryClient is now keyed on the site, which
remounts the tree against an empty cache. Three tests pin the old behaviour
down so the shortcut cannot return.

The others: an unscoped fallback path behind the Connected Clients chart;
Config Health, TX retries and the activity feed reading fleet-wide; and
site_id added to the device list query but none of the other four that build
a device payload, so the Site field could not read its own value and appeared
to reject every change while the writes were landing. That last one is now
structural — DEVICE_BASE_COLUMNS is shared by all five, with a test that
fails when a device payload is hand-rolled, and which found two further
instances while being written.

Frontend tests were never run by CI despite vitest being configured and six
suites existing; they now run in both the workflow and ci-preflight.

Verified against a live four-device fleet: the migration left every figure
identical, an empty site returns zero from every read endpoint, moving a
device carries its history with it, and deleting a site with devices is
refused.

Minor bump rather than patch: sites change the shape of the product rather
than adding to it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 12:02:00 -07:00

250 lines
8.2 KiB
TypeScript

import { query, queryOne } from '../config/database';
import { encrypt, decrypt } from '../utils/crypto';
import { parsePort } from '../utils/parsePort';
import { safeConnectionError } from '../utils/safeClientError';
import { RouterOSClient } from './mikrotik/RouterOSClient';
import type { PollerService } from './PollerService';
import type { CredentialPresetRow } from '../routes/credentialPresets';
import { DEVICE_BASE_COLUMNS } from './deviceColumns';
export type CreateDeviceContext = {
/** JWT role of the caller ('admin' | 'operator' | 'viewer'). Preset use may be restricted for operators. */
requestingUserRole?: string;
/**
* Site the device should join (issue #130). A device with no site is invisible
* the moment any site is selected, so this must never be left unset: when the
* caller has no active site we fall back to the default site in SQL.
*/
siteId?: number | null;
};
async function loadCredentialPreset(
id: number | null | undefined,
ctx?: CreateDeviceContext
): Promise<{
api_username: string;
api_password: string;
api_port: number | null;
ssh_username: string | null;
ssh_password: string | null;
ssh_port: number | null;
} | null> {
if (id === null || id === undefined) return null;
const preset = await queryOne<CredentialPresetRow>(
`SELECT * FROM credential_presets WHERE id = $1`,
[id]
);
if (!preset) throw new Error(`Credential preset ${id} not found`);
const allowOp = (preset as CredentialPresetRow & { allow_operator_use?: boolean }).allow_operator_use !== false;
if (ctx?.requestingUserRole === 'operator' && !allowOp) {
const err = new Error('This credential preset is restricted to administrators');
(err as Error & { statusCode?: number }).statusCode = 403;
throw err;
}
return {
api_username: preset.api_username,
api_password: decrypt(preset.api_password_encrypted),
api_port: preset.api_port,
ssh_username: preset.ssh_username,
ssh_password: preset.ssh_password_encrypted ? decrypt(preset.ssh_password_encrypted) : null,
ssh_port: preset.ssh_port,
};
}
export interface CreateDeviceInput {
name?: string;
ip_address?: string;
device_type?: string;
notes?: string;
credential_preset_id?: number | null;
api_username?: string;
api_password?: string;
api_port?: unknown;
ssh_username?: string | null;
ssh_password?: string | null;
ssh_port?: unknown;
combine_with_device_id?: number;
force_replace_existing_by_serial?: boolean;
}
export type CreateDeviceResult =
| { ok: true; status: 200 | 201; body: Record<string, unknown> }
| { ok: false; status: number; body: Record<string, unknown> };
/**
* Shared device creation logic used by POST /api/devices and bulk-add worker.
*/
export async function createDeviceFromBody(
input: CreateDeviceInput,
pollerService: PollerService | null,
ctx?: CreateDeviceContext
): Promise<CreateDeviceResult> {
const {
name,
ip_address,
device_type = 'router',
notes,
credential_preset_id,
} = input;
let preset: Awaited<ReturnType<typeof loadCredentialPreset>>;
try {
preset = await loadCredentialPreset(credential_preset_id ?? null, ctx);
} catch (err) {
const status = (err as Error & { statusCode?: number }).statusCode ?? 400;
return { ok: false, status, body: { error: (err as Error).message } };
}
const api_username: string | undefined = preset?.api_username ?? input.api_username;
const api_password: string | undefined = preset?.api_password ?? input.api_password;
const api_port: number = preset?.api_port ?? parsePort(input.api_port, 8728);
const ssh_username: string | null = preset ? preset.ssh_username : (input.ssh_username ?? null);
const ssh_password: string | null = preset ? preset.ssh_password : (input.ssh_password ?? null);
const ssh_port: number = preset?.ssh_port ?? parsePort(input.ssh_port, 22);
const combineWithDeviceId =
typeof input.combine_with_device_id === 'number' ? input.combine_with_device_id : null;
const forceReplaceBySerial = input.force_replace_existing_by_serial === true;
if (!name || !ip_address || !api_username || !api_password) {
return {
ok: false,
status: 400,
body: { error: 'name, ip_address, api_username, api_password are required' },
};
}
const testClient = new RouterOSClient(ip_address, api_port, api_username, api_password, 10_000);
let detectedSerial: string | null;
try {
await testClient.connect();
const rb = await testClient.execute('/system/routerboard/print').catch(() => [] as Record<string, string>[]);
detectedSerial = (rb[0]?.['serial-number'] || '').trim() || null;
} catch (err) {
return {
ok: false,
status: 422,
body: { error: safeConnectionError('createDeviceFromBody', err) },
};
} finally {
testClient.disconnect();
}
if (detectedSerial) {
const existingBySerial = await queryOne<{
id: number;
name: string;
ip_address: string;
serial_number: string;
}>(
`SELECT id, name, ip_address, serial_number
FROM devices
WHERE serial_number = $1`,
[detectedSerial]
);
if (existingBySerial) {
const shouldCombine =
combineWithDeviceId != null && combineWithDeviceId === existingBySerial.id;
if (!shouldCombine && !forceReplaceBySerial) {
return {
ok: false,
status: 409,
body: {
error: 'duplicate_serial',
code: 'duplicate_serial',
existing_device: existingBySerial,
candidate: {
serial_number: detectedSerial,
identity: name,
ip_address,
},
},
};
}
const encryptedPass = encrypt(api_password);
const encryptedSshPass = ssh_password ? encrypt(ssh_password) : null;
await query(
`UPDATE devices SET
name=COALESCE($1,name),
ip_address=$2,
api_port=$3,
api_username=$4,
api_password_encrypted=$5,
ssh_port=$6,
ssh_username=COALESCE($7,ssh_username),
ssh_password_encrypted=COALESCE($8,ssh_password_encrypted),
device_type=COALESCE($9,device_type),
notes=COALESCE($10,notes),
updated_at=NOW()
WHERE id = $11`,
[
name,
ip_address,
api_port,
api_username,
encryptedPass,
ssh_port,
ssh_username,
encryptedSshPass,
device_type,
notes || null,
existingBySerial.id,
]
);
if (pollerService) {
await pollerService.scheduleDeviceSync(existingBySerial.id, 'full');
}
const updatedExisting = await queryOne(
`SELECT ${DEVICE_BASE_COLUMNS}, created_at FROM devices WHERE id = $1`,
[existingBySerial.id]
);
if (!updatedExisting) {
return {
ok: false,
status: 500,
body: {
error: 'Duplicate merge succeeded but device row could not be reloaded',
device_id: existingBySerial.id,
},
};
}
return {
ok: true,
status: 200,
body: { ...updatedExisting, merged_from_duplicate: true },
};
}
}
const encryptedPass = encrypt(api_password);
const encryptedSshPass = ssh_password ? encrypt(ssh_password) : null;
const rows = await query<{ id: number }>(
`INSERT INTO devices (name, ip_address, api_port, api_username, api_password_encrypted,
ssh_port, ssh_username, ssh_password_encrypted, device_type, notes, status,
site_id)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,'unknown',
COALESCE($11::int, (SELECT id FROM sites ORDER BY is_default DESC, id LIMIT 1)))
RETURNING id`,
[name, ip_address, api_port, api_username, encryptedPass,
ssh_port, ssh_username || null, encryptedSshPass, device_type, notes || null,
ctx?.siteId ?? null]
);
const newId = rows[0].id;
if (pollerService) {
await pollerService.scheduleDeviceSync(newId, 'full');
}
const device = await queryOne(
`SELECT ${DEVICE_BASE_COLUMNS}, created_at FROM devices WHERE id = $1`,
[newId]
);
return { ok: true, status: 201, body: device as Record<string, unknown> };
}