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 <noreply@anthropic.com>
This commit is contained in:
1 parent
cc1245da1e
commit
d2624b2fc2
3 files changed
+22
-4
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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/
|
||||
Reference in new issue
Block a user