From d2624b2fc2c7f483c12a36779e635b85b6a24e26 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Sun, 1 Nov 2026 12:29:13 +0100 Subject: [PATCH] NetBox proxy: one route per method, beside the UI's "GET /" A bare "/netbox/" pattern conflicts with "GET /" in ServeMux (neither is more specific), and the region panicked at start once ipam.url carried a path. The proxy is registered per method now, and a test builds the routes with it. The lab install script's own API calls use the new /netbox/ base. Co-Authored-By: Claude Opus 5.5 --- cmd/rollcall/nbproxy_test.go | 15 +++++++++++++++ cmd/rollcall/server.go | 7 +++++-- lab/netbox/install.sh | 4 ++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/rollcall/nbproxy_test.go b/cmd/rollcall/nbproxy_test.go index 1a76a42..84f713d 100644 --- a/cmd/rollcall/nbproxy_test.go +++ b/cmd/rollcall/nbproxy_test.go @@ -69,3 +69,18 @@ func TestNetBoxNextLinkBasePath(t *testing.T) { t.Fatalf("%v %d %v", err, len(got), paths) } } + +// the proxy's routes sit beside the UI's "GET /" (a bare path pattern panicked at start) +func TestNetBoxProxyRoutes(t *testing.T) { + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "nb "+r.Method+" "+r.URL.Path) })) + defer up.Close() + a := &App{cfg: &Config{IPAM: IPAMConfig{URL: up.URL + "/netbox/"}, AdminHTTP: "allow"}, ipam: &netBox{base: up.URL + "/netbox", client: up.Client()}} + h := a.routes() + for _, m := range []string{"GET", "POST"} { + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(m, "/netbox/dcim/", nil)) + if rec.Code != http.StatusUnauthorized { // admins only + t.Fatalf("%s without admin: %d %s", m, rec.Code, rec.Body) + } + } +} diff --git a/cmd/rollcall/server.go b/cmd/rollcall/server.go index 2f78b9f..3bb56b7 100644 --- a/cmd/rollcall/server.go +++ b/cmd/rollcall/server.go @@ -287,8 +287,11 @@ func (a *App) routes() http.Handler { mux.HandleFunc("DELETE /api/v1/racks", a.adminAuth(a.handleRacks)) mux.HandleFunc("POST /api/v1/racks/positions", a.adminAuth(a.handleRackPositions)) mux.HandleFunc("GET /api/v1/cabling", a.adminAuth(a.handleCabling)) - if p := a.netboxUIPath(); p != "" { - mux.Handle(p+"/", a.adminAuth(a.netboxProxy().ServeHTTP)) + if p := a.netboxUIPath(); p != "" { // per method: a bare path would clash with "GET /" below + h := a.adminAuth(a.netboxProxy().ServeHTTP) + for _, m := range []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} { + mux.HandleFunc(m+" "+p+"/", h) + } } mux.HandleFunc("POST /api/v1/switches", a.adminAuth(a.handleSwitches)) mux.HandleFunc("DELETE /api/v1/switches", a.adminAuth(a.handleSwitches)) diff --git a/lab/netbox/install.sh b/lab/netbox/install.sh index 5174e0a..83696a1 100644 --- a/lab/netbox/install.sh +++ b/lab/netbox/install.sh @@ -144,7 +144,7 @@ print("rollcall objects ready") # Rollcall's API token (v2), made once through the API with its password if ! grep -q '^NETBOX_TOKEN_ROLLCALL=' $S; then - curl -fsS http://127.0.0.1:8001/api/users/tokens/provision/ -H "Host: $MYIP" \ + curl -fsS http://127.0.0.1:8001/netbox/api/users/tokens/provision/ -H "Host: $MYIP" \ -H 'Content-Type: application/json' -H 'Accept: application/json' \ -d "{\"username\":\"rollcall\",\"password\":\"$ROLLCALL_PASS\",\"description\":\"rollcall region\",\"write_enabled\":true}" | python3 -c 'import json,sys;d=json.load(sys.stdin);print("NETBOX_TOKEN_ROLLCALL=nbt_%s.%s"%(d["key"],d["token"]))' >> $S @@ -153,4 +153,4 @@ fi echo "--- ready" systemctl is-active postgresql valkey-server netbox netbox-rq caddy | paste -sd' ' . $S -curl -fsS -o /dev/null -w "rollcall token: %{http_code}\n" -H "Authorization: Bearer $NETBOX_TOKEN_ROLLCALL" http://$MYIP/api/ipam/prefixes/ +curl -fsS -o /dev/null -w "rollcall token: %{http_code}\n" -H "Authorization: Bearer $NETBOX_TOKEN_ROLLCALL" http://$MYIP/netbox/api/ipam/prefixes/