Add a phpIPAM import to the IP Addresses page
New "Sync from phpIPAM" action on IP Addresses, alongside the existing
"Sync from Tailscale" / "Sync from Proxmox" ones and built the same way:
a new integration type (config in-app, URL + API app ID + app token,
credentials encrypted at rest) that this page pulls from on demand.
Deliberately import-only, not a full integration -- no dedicated page,
dashboard widget, or nav entry, since that's all this was asked for.
Auth is phpIPAM's static "App token" method: create an API app under
Administration -> API with its security set to "SSL with App token", and
its one-time code goes straight in as the `token` header (also sent as
`phpipam-token`, in case a given version expects that name instead) --
no login call, no token to renew. The user/password "User token" method
isn't implemented.
Addresses are read the standard way: GET /subnets/, then GET
/subnets/{id}/addresses/ for each, rather than assuming a single
"all addresses" endpoint exists on every version. phpIPAM wraps every
response as {code, success, data} -- including an empty result: a subnet
with nothing in it answers success:false, message:"No addresses found"
rather than success:true, data:[]. That's read as "nothing here", not a
failure; anything else with success:false throws with phpIPAM's own
message. One subnet failing outright (e.g. the app lacks permission on
it) is skipped with a note rather than aborting the whole sync. Every
address field is read defensively -- optional, independently
type-checked -- so a field phpIPAM renames or drops in some version
leaves that value blank instead of breaking the import.
Imported entries: label from hostname or description, "phpIPAM" as
vendor, the subnet's own description (or its CIDR, if it has none) as
location, and description/note/MAC folded into notes. Existing sync
plumbing (upsertSyncedEntry) gained a location parameter so this and any
future sync can set it; the two existing syncs pass null, unchanged.
Endpoints, the token header, and the address/subnet field names are
cross-checked against phpIPAM's own published API documentation. Not
verified against a live instance -- there wasn't one available while
building this, so if a real sync comes back empty or with the wrong
fields, that's the next thing to check.
Verified with 23 backend checks against a fake phpIPAM server matching
that documented shape (the empty-subnet quirk, a subnet that fails
outright, malformed/missing fields, a non-JSON response) and the real
route (added/updated/skipped counts, a manually-entered IP never
overwritten, roles, no-enabled-integration, upstream failure surfaced
per-integration rather than as a 500, audit entries) plus a browser check
of the real IP Addresses page against the real routers: the sync button,
its result message, re-syncing (updates rather than duplicates), the
manual entry staying untouched, and the viewer view.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf7f73f6b6
commit
70ba60c7da
14 files changed
+281
-13
No files matched your search
@@ -10,6 +10,7 @@ import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createTailscaleAdapter } from "../integrations/tailscale/adapter.js";
|
||||
import { createProxmoxAdapter } from "../integrations/proxmox/adapter.js";
|
||||
import { createPhpIpamAdapter } from "../integrations/phpipam/adapter.js";
|
||||
|
||||
export const ipamRouter = Router();
|
||||
|
||||
@@ -141,17 +142,18 @@ async function upsertSyncedEntry(
|
||||
label: string,
|
||||
vendor: string,
|
||||
notes: string | null,
|
||||
location: string | null,
|
||||
source: string,
|
||||
): Promise<"added" | "updated" | "skipped"> {
|
||||
const [existing] = await db.select().from(ipamEntries).where(eq(ipamEntries.ipAddress, ip)).limit(1);
|
||||
if (!existing) {
|
||||
await db.insert(ipamEntries).values({ ipAddress: ip, label, vendor, notes, source });
|
||||
await db.insert(ipamEntries).values({ ipAddress: ip, label, vendor, notes, location, source });
|
||||
return "added";
|
||||
}
|
||||
if (existing.source === source) {
|
||||
await db
|
||||
.update(ipamEntries)
|
||||
.set({ label, notes, updatedAt: new Date().toISOString() })
|
||||
.set({ label, notes, location, updatedAt: new Date().toISOString() })
|
||||
.where(eq(ipamEntries.id, existing.id));
|
||||
return "updated";
|
||||
}
|
||||
@@ -193,7 +195,7 @@ ipamRouter.post("/sync-tailscale", requireRole("operator"), asyncHandler(async (
|
||||
const label = device.label || device.hostname || ip;
|
||||
const notes = device.os ? `OS: ${device.os}` : null;
|
||||
|
||||
const result = await upsertSyncedEntry(ip, label, "Tailscale", notes, "tailscale");
|
||||
const result = await upsertSyncedEntry(ip, label, "Tailscale", notes, null, "tailscale");
|
||||
if (result === "added") added++;
|
||||
else if (result === "updated") updated++;
|
||||
else {
|
||||
@@ -258,7 +260,7 @@ ipamRouter.post("/sync-proxmox", requireRole("operator"), asyncHandler(async (re
|
||||
const notes = `Proxmox ${guest.type === "qemu" ? "VM" : "LXC"} #${guest.vmid} on ${guest.node}`;
|
||||
|
||||
for (const ip of detail.ipAddresses) {
|
||||
const result = await upsertSyncedEntry(ip, label, "Proxmox", notes, "proxmox");
|
||||
const result = await upsertSyncedEntry(ip, label, "Proxmox", notes, null, "proxmox");
|
||||
if (result === "added") added++;
|
||||
else if (result === "updated") updated++;
|
||||
else {
|
||||
@@ -278,3 +280,56 @@ ipamRouter.post("/sync-proxmox", requireRole("operator"), asyncHandler(async (re
|
||||
|
||||
res.json({ added, updated, skipped, skippedIps, errors });
|
||||
}));
|
||||
|
||||
ipamRouter.post("/sync-phpipam", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const phpIpamIntegrations = await db
|
||||
.select()
|
||||
.from(integrations)
|
||||
.where(and(eq(integrations.type, "phpipam"), eq(integrations.enabled, true)));
|
||||
|
||||
if (phpIpamIntegrations.length === 0) {
|
||||
return res.status(400).json({ error: "no_phpipam_integration" });
|
||||
}
|
||||
|
||||
let added = 0;
|
||||
let updated = 0;
|
||||
let skipped = 0;
|
||||
const skippedIps: string[] = [];
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const integration of phpIpamIntegrations) {
|
||||
const loaded = await loadIntegrationConfig(integration.id);
|
||||
if (!loaded || loaded.integration.type !== "phpipam") continue;
|
||||
|
||||
let addresses;
|
||||
try {
|
||||
const adapter = createPhpIpamAdapter(loaded.config as { url: string; appId: string; token: string; insecure?: boolean });
|
||||
addresses = await adapter.listAddresses();
|
||||
} catch (err) {
|
||||
errors.push(`${integration.name}: ${err instanceof Error ? err.message : String(err)}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const addr of addresses) {
|
||||
const label = addr.hostname || addr.description || addr.ip;
|
||||
const notes = [addr.description, addr.note, addr.mac ? `MAC ${addr.mac}` : null].filter((v): v is string => !!v).join(" — ") || null;
|
||||
|
||||
const result = await upsertSyncedEntry(addr.ip, label, "phpIPAM", notes, addr.subnetLabel, "phpipam");
|
||||
if (result === "added") added++;
|
||||
else if (result === "updated") updated++;
|
||||
else {
|
||||
skipped++;
|
||||
skippedIps.push(addr.ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "ipam",
|
||||
action: "sync_phpipam",
|
||||
detail: { added, updated, skipped },
|
||||
});
|
||||
|
||||
res.json({ added, updated, skipped, skippedIps, errors });
|
||||
}));
|
||||
Reference in new issue
Block a user