Review fixes before v0.4.0: NetBox on its own origin, pinned root refresh
CI / agent (push) Successful in 3s
CI / site (push) Successful in 2s
CI / go (push) Successful in 49s

From a review of everything since v0.3.0:

- NetBox's UI was served on Rollcall's origin, so a script in a NetBox
  page (stored XSS, a custom link) could read the Rollcall UI's stored
  admin token and call Rollcall's API with the admin certificate. It now
  has a listener of its own (ipam.ui_listen, default :8444): another
  origin. The Rollcall UI builds its NetBox links from that port.
- The proxy dropped X-Rollcall-User but not X-Rollcall_User, which a
  WSGI server reads as the same HTTP_X_ROLLCALL_USER; every spelling is
  dropped now.
- Deployed machines fetched the root bundle over TLS checked against the
  system's trust store: a public CA's certificate for the rack's name
  would have done. The refresh is pinned to the Rollcall roots installed
  on the machine and takes only CA certificates (tried on node001 and
  node002).
- A NetBox device type NetBox will not resize (0U while its devices have
  positions) no longer stops those machines' device sync; it is logged.
- A NetBox cable on a device whose machine is gone here rolled back all
  cable and switch sync; such cables are skipped.
- Switch names are looked up in NetBox 50 at a time (a large site would
  outgrow one URL).
- rack_units: "vendor model" wins over the bare model when both are set
  (map order chose before); cable precedence comments match the code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-11-01 15:35:45 +01:00
1 parent 6a7b158644
commit 0569ecf1cc
13 files changed
+138 -50

No files matched your search

+18 -10
View File
@@ -3,8 +3,8 @@ package main
// Cabling: what each machine's ports are plugged into, and where the
// switches at the other end stand. A port's switch port comes from, first,
// the LLDP its commissioning saw (lldpcli's neighbours per interface): what
// is really there; then a cable recorded in NetBox on the machine's device
// (dcim interfaces with link peers); then one entered here. The BMC's port
// is really there; then one entered here; then a cable recorded in NetBox on
// the machine's device (dcim interfaces, through patch panels). The BMC's port
// is not seen by the host's LLDP: its cable (to the out-of-band switch) is
// NetBox's or entered here. When NetBox records another switch port than
// LLDP sees, the port says so (netbox_says).
@@ -220,8 +220,9 @@ func (s *Store) ReplaceNetBoxCables(ctx context.Context, cs []Cable) error {
return err
}
for _, c := range cs {
if _, err := tx.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) VALUES ($1,$2,$3,$4,'netbox') ON CONFLICT DO NOTHING`,
c.Machine, c.Port, c.Switch, c.SwitchPort); err != nil {
// a device whose rollcall_machine names no machine here (deleted, typed in) is skipped
if _, err := tx.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) SELECT $1,$2,$3,$4,'netbox'
WHERE EXISTS (SELECT 1 FROM machines WHERE id=$1) ON CONFLICT DO NOTHING`, c.Machine, c.Port, c.Switch, c.SwitchPort); err != nil {
return err
}
}
@@ -312,7 +313,7 @@ func (a *App) handleCabling(w http.ResponseWriter, r *http.Request) {
}
// mergeCables adds the recorded cables to the ports LLDP filled: a port
// without a neighbour takes NetBox's cable, else one entered here; the BMC's
// without a neighbour takes the one entered here, else NetBox's; the BMC's
// port is added when a cable is recorded for it. A NetBox cable that
// disagrees with LLDP is kept as netbox_says.
func mergeCables(ports []Port, cs []Cable) []Port {
@@ -447,13 +448,20 @@ func (a *App) syncSwitches(ctx context.Context, nb *netBox) error {
if len(names) == 0 {
return nil
}
q := url.Values{}
var list []string
for n := range names {
q.Add("name", n)
list = append(list, n)
}
devs, err := nb.all(ctx, "dcim/devices/", q)
if err != nil {
return err
sort.Strings(list)
var devs []obj
for len(list) > 0 { // 50 names per request: a large site would outgrow a URL
batch := list[:min(50, len(list))]
list = list[len(batch):]
got, err := nb.all(ctx, "dcim/devices/", url.Values{"name": batch})
if err != nil {
return err
}
devs = append(devs, got...)
}
for _, x := range switchPlaces(devs) {
if err := a.store.PutSwitch(ctx, x); err != nil {
+13 -4
View File
@@ -315,22 +315,31 @@ func (a *App) trustState() *rackTrustState {
}
// caRefreshFiles keep a deployed machine's roots current: the bundle comes
// over HTTPS from the rack, checked against the roots the machine trusts now,
// so a rotation reaches it during prepare (before the new root signs) and the
// old root leaves it after retire.
// over HTTPS from the rack, checked against the Rollcall roots the machine
// trusts now (pinned: a certificate from a public CA for the rack's name is
// not enough), so a rotation reaches it during prepare (before the new root
// signs) and the old root leaves it after retire. Only CA certificates are
// taken from the bundle.
func caRefreshFiles(tlsBase string) []seedFile {
script := `#!/bin/sh
# rollcall: keep the installation's roots current (written at deploy time)
set -eu
URL=` + shQuote(tlsBase+"/ca-bundle.pem") + `
T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
rc=0; curl -fsS --max-time 30 -o "$T/bundle" "$URL" || rc=$?
cat /usr/local/share/ca-certificates/rollcall-ca*.crt /etc/pki/ca-trust/source/anchors/rollcall-ca*.crt > "$T/pin" 2>/dev/null || true
[ -s "$T/pin" ] || { echo "rollcall: no Rollcall root installed here to check $URL with" >&2; exit 1; }
rc=0; curl -fsS --max-time 30 --cacert "$T/pin" -o "$T/bundle" "$URL" || rc=$?
if [ $rc -eq 60 ]; then # the server's certificate is from a root not trusted here
echo "rollcall: $URL is not trusted here: the root rotation went past prepare before this machine refreshed; install the bundle by hand" >&2; exit 1
fi
[ $rc -eq 0 ] || exit 0 # unreachable: the timer tries again
awk -v d="$T" '/-----BEGIN CERTIFICATE-----/{n++} n{print > (d "/c" n)} /-----END CERTIFICATE-----/{close(d "/c" n)}' "$T/bundle"
[ -f "$T/c1" ] || exit 0
if command -v openssl >/dev/null 2>&1; then
for c in "$T"/c[0-9]*; do
openssl x509 -in "$c" -noout -text 2>/dev/null | grep -q 'CA:TRUE' || { echo "rollcall: $URL sent a certificate that is not a CA; nothing changed" >&2; exit 1; }
done
fi
changed=
for dir in /usr/local/share/ca-certificates /etc/pki/ca-trust/source/anchors; do
[ -d "$dir" ] || continue
+4
View File
@@ -167,6 +167,10 @@ func TestCARefreshScript(t *testing.T) {
if len(fs) != 3 || !strings.Contains(fs[0].Content, "URL='https://10.213.78.1/ca-bundle.pem'") {
t.Fatalf("%+v", fs[0])
}
// the rack is checked against the Rollcall roots installed, not the system's; only CAs are taken
if !strings.Contains(fs[0].Content, `--cacert "$T/pin"`) || !strings.Contains(fs[0].Content, "CA:TRUE") {
t.Fatal("refresh is not pinned to the installed roots or takes any certificate")
}
f := filepath.Join(t.TempDir(), "s")
_ = os.WriteFile(f, []byte(fs[0].Content), 0o755)
if out, err := exec.Command("sh", "-n", f).CombinedOutput(); err != nil {
+6 -4
View File
@@ -79,10 +79,12 @@ func machineSize(m *Machine, override map[string]int) rackSize {
if ct := strings.ToLower(str(sys["chassis-type"])); strings.Contains(ct, "multi-system") || strings.Contains(ct, "blade") {
node = true
}
if model != "" {
for k, u := range override { // exact model, or "vendor model"
if strings.EqualFold(k, model) || strings.EqualFold(k, mfr+" "+model) {
return rackSize{U: u, Node: node, Source: "config"}
if model != "" { // "vendor model" before the bare model: both may be set, map order must not choose
for _, want := range []string{mfr + " " + model, model} {
for k, u := range override {
if strings.EqualFold(k, want) {
return rackSize{U: u, Node: node, Source: "config"}
}
}
}
}
+10 -1
View File
@@ -24,6 +24,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/netip"
"net/url"
@@ -41,6 +42,7 @@ type IPAMConfig struct {
URL string `json:"url"` // https://netbox.example; with NetBox's BASE_PATH (…/netbox/) its UI is served here too (nbproxy.go)
UIURL string `json:"ui_url"` // where "open in NetBox" links go when not through Rollcall
UILoginFile string `json:"ui_login_secret_file"` // shared with NetBox's front: the admin certificate signs in to NetBox (nbproxy.go)
UIListen string `json:"ui_listen"` // the NetBox UI's own HTTPS listener (its own origin), default ":8444"
TokenFile string `json:"token_file"` // API token (NetBox v2 "nbt_..." or v1)
User string `json:"user"` // Infoblox WAPI user
PasswordFile string `json:"password_file"` // Infoblox WAPI password
@@ -417,7 +419,14 @@ func (a *App) handleIPAM(w http.ResponseWriter, r *http.Request) {
a.ipamSt.mu.Lock()
last := a.ipamSt.last
a.ipamSt.mu.Unlock()
writeJSON(w, 200, obj{"provider": a.ipam.Name(), "url": a.cfg.IPAM.URL, "ui": a.netboxUILink(), "dns_zone": a.cfg.IPAM.DNSZone,
ui := obj{}
if a.cfg.IPAM.UIURL != "" {
ui["url"] = strings.TrimRight(a.cfg.IPAM.UIURL, "/")
} else if p := a.netboxUIPath(); p != "" && a.cfg.TLSListen != "" {
_, port, _ := net.SplitHostPort(a.cfg.IPAM.uiListen())
ui["port"], ui["path"] = port, p // the browser puts its own host in front
}
writeJSON(w, 200, obj{"provider": a.ipam.Name(), "url": a.cfg.IPAM.URL, "ui": ui, "dns_zone": a.cfg.IPAM.DNSZone,
"tag": a.cfg.IPAM.tag(), "machine_field": a.cfg.IPAM.field(), "last_sync": last})
}
+4 -1
View File
@@ -16,6 +16,7 @@ package main
import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"sort"
@@ -487,8 +488,10 @@ func (n *netBox) ensureType(ctx context.Context, mfr int64, model string, sz rac
if len(got) > 0 {
t := got[0]
if n.tagged(t) && sz.Source != "" && int(num(t["u_height"])) != uh {
// NetBox refuses a height its racked devices do not fit (0 while a device has
// a position, more units than are free): the type stays, the devices sync on
if err := n.do(ctx, "PATCH", fmt.Sprintf("dcim/device-types/%d/", nbID(t)), obj{"u_height": uh}, nil); err != nil {
return 0, fmt.Errorf("height of %s: %w", model, err)
log.Printf("netbox: device type %s stays %vU (wanted %dU): %v", model, t["u_height"], uh, err)
}
}
return nbID(t), nil
+26 -9
View File
@@ -3,8 +3,10 @@ package main
// The NetBox web UI through Rollcall, so it opens wherever Rollcall's does
// (a lab NetBox on a network the browser cannot reach, say). With NetBox as
// the IPAM and its url carrying a path (NetBox's BASE_PATH:
// "url": "https://netbox.example/netbox/"), that path on Rollcall is NetBox,
// for admins only (the admin certificate, as for the API). NetBox keeps its
// "url": "https://netbox.example/netbox/"), that path on Rollcall's own
// NetBox listener (ui_listen, default :8444: another origin than the
// Rollcall UI) is NetBox, for admins only (the admin certificate, as for
// the API). NetBox keeps its
// own login and session. Its CSRF_TRUSTED_ORIGINS must list Rollcall's
// address for its forms to work: the browser posts from there.
//
@@ -45,12 +47,24 @@ func (a *App) netboxUIPath() string {
return p
}
// netboxUILink: where the UI's "open in NetBox" links go
func (a *App) netboxUILink() string {
if a.cfg.IPAM.UIURL != "" {
return strings.TrimRight(a.cfg.IPAM.UIURL, "/")
func (c IPAMConfig) uiListen() string { return orDefault(c.UIListen, ":8444") }
// netboxUIHandler serves NetBox's UI on its own listener (ui_listen): its
// own origin, so a script in a NetBox page can neither read the Rollcall
// UI's storage (the admin token) nor call Rollcall's API as the admin. Only
// the proxy is there; "/" leads to it.
func (a *App) netboxUIHandler() (http.Handler, error) {
px, err := a.netboxProxy()
if err != nil {
return nil, err
}
return a.netboxUIPath()
p, mux := a.netboxUIPath(), http.NewServeMux()
h := a.adminAuth(px.ServeHTTP)
for _, m := range []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} {
mux.HandleFunc(m+" "+p+"/", h)
}
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, p+"/", http.StatusFound) })
return logging(http.NewCrossOriginProtection().Handler(mux)), nil
}
func (a *App) netboxProxy() (http.Handler, error) {
@@ -73,8 +87,11 @@ func (a *App) netboxProxy() (http.Handler, error) {
pr.Out.URL.Scheme, pr.Out.URL.Host = target.Scheme, target.Host
pr.Out.Host = target.Host
pr.Out.Header.Del("Authorization") // Rollcall's credentials are not NetBox's
pr.Out.Header.Del("X-Rollcall-User")
pr.Out.Header.Del("X-Rollcall-Proxy-Secret")
for k := range pr.Out.Header { // also X-Rollcall_User: the WSGI server makes HTTP_X_ROLLCALL_USER of both
if n := strings.ToLower(strings.ReplaceAll(k, "_", "-")); n == "x-rollcall-user" || n == "x-rollcall-proxy-secret" {
delete(pr.Out.Header, k)
}
}
if name := a.peerAdmin(pr.In); name != "" && secret != "" {
pr.Out.Header.Set("X-Rollcall-User", name)
pr.Out.Header.Set("X-Rollcall-Proxy-Secret", secret)
+13 -3
View File
@@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
@@ -42,6 +43,7 @@ func TestNetBoxProxy(t *testing.T) {
req.Header.Set("Authorization", "Bearer rollcall-admin")
req.Header.Set("X-Rollcall-User", "admin") // a browser claiming a NetBox user
req.Header.Set("X-Rollcall-Proxy-Secret", "guessed") // and the secret
req.Header["X-Rollcall_user"] = []string{"admin"} // spelled so the WSGI server still reads it as HTTP_X_ROLLCALL_USER
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusFound || rec.Header().Get("Location") != "/netbox/login/?next=/netbox/" {
@@ -50,7 +52,7 @@ func TestNetBoxProxy(t *testing.T) {
if seen.Header.Get("Authorization") != "" {
t.Fatal("Rollcall's credentials went to NetBox")
}
if seen.Header.Get("X-Rollcall-User") != "" || seen.Header.Get("X-Rollcall-Proxy-Secret") != "" {
if seen.Header.Get("X-Rollcall-User") != "" || seen.Header.Get("X-Rollcall-Proxy-Secret") != "" || len(seen.Header["X-Rollcall_user"]) != 0 {
t.Fatal("the browser's user header went to NetBox")
}
rec = httptest.NewRecorder()
@@ -79,12 +81,20 @@ func TestNetBoxNextLinkBasePath(t *testing.T) {
}
}
// the proxy's routes sit beside the UI's "GET /" (a bare path pattern panicked at start)
// NetBox's UI is on its own listener, admins only; the Rollcall UI's routes do not carry it
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()
h, err := a.netboxUIHandler()
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
a.routes().ServeHTTP(rec, httptest.NewRequest("GET", "/netbox/dcim/", nil))
if rec.Code == http.StatusUnauthorized || strings.Contains(rec.Body.String(), "nb GET") {
t.Fatalf("the Rollcall UI's origin serves NetBox: %d", rec.Code)
}
for _, m := range []string{"GET", "POST"} {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(m, "/netbox/dcim/", nil))
+13 -10
View File
@@ -287,16 +287,6 @@ 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 != "" { // per method: a bare path would clash with "GET /" below
if px, err := a.netboxProxy(); err != nil {
log.Printf("netbox ui: %v; not served", err)
} else {
h := a.adminAuth(px.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("POST /api/v1/cables", a.adminAuth(a.handleCables))
mux.HandleFunc("DELETE /api/v1/cables", a.adminAuth(a.handleCables))
@@ -908,6 +898,19 @@ func serve(cfgPath string) error {
log.Printf("HTTPS listener: %v", err)
}
}()
if app.netboxUIPath() != "" { // NetBox's UI on its own origin (nbproxy.go)
if nh, err := app.netboxUIHandler(); err != nil {
log.Printf("netbox ui: %v; not served", err)
} else {
nsrv := &http.Server{Addr: cfg.IPAM.uiListen(), Handler: nh, ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 2 * time.Minute, IdleTimeout: 2 * time.Minute, TLSConfig: tsrv.TLSConfig.Clone()}
go func() {
log.Printf("netbox ui on %s%s", cfg.IPAM.uiListen(), app.netboxUIPath())
if err := nsrv.ListenAndServeTLS("", ""); err != nil {
log.Printf("netbox ui listener: %v", err)
}
}()
}
}
}
srv := &http.Server{Addr: cfg.Listen, Handler: handler, ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 2 * time.Minute, IdleTimeout: 2 * time.Minute}
log.Printf("rollcall listening on %s (image=%s tftp=%s agent_token=%v)", cfg.Listen, cfg.ImageDir, cfg.TFTPRoot, cfg.AgentToken != "")
+11 -2
View File
@@ -2,9 +2,18 @@
// Rollcall web UI: a fleet view first (filters, selection, bulk actions), machine detail second.
const $ = (s, el = document) => el.querySelector(s);
const $$ = (s, el = document) => Array.from(el.querySelectorAll(s));
// nbUI: where NetBox's UI is: ui_url, or Rollcall's NetBox listener on this
// host (another port: its own origin), "" without one
function nbUI() {
const ui = S.ipam && S.ipam.provider === 'netbox' && S.ipam.ui; if (!ui) return '';
if (ui.url) return ui.url;
if (!ui.port) return '';
const u = new URL(location.href); u.port = ui.port; u.pathname = ui.path; u.search = ''; u.hash = '';
return u.href.replace(/\/$/, '');
}
// nbLink: "open in NetBox" for a NetBox page (path and query), when NetBox is the IPAM
function nbLink(path, label = 'NetBox ↗') {
const ui = S.ipam && S.ipam.provider === 'netbox' && S.ipam.ui;
const ui = nbUI();
return ui ? `<a class="nb-link" href="${esc(ui + path)}" target="_blank" rel="noopener" title="open in NetBox">${esc(label)}</a>` : '';
}
const nbQ = o => '?' + new URLSearchParams(Object.entries(o).filter(([, v]) => v)).toString();
@@ -88,7 +97,7 @@ async function refresh(full) {
[S.ips, S.ipam] = await Promise.all([api('GET', '/api/v1/ips'), api('GET', '/api/v1/ipam').catch(() => null)]);
}
$('#n-machines').textContent = machines.length;
const nb = $('#nav-netbox'), ui = S.ipam && S.ipam.provider === 'netbox' && S.ipam.ui; nb.style.display = ui ? '' : 'none'; if (ui) nb.href = ui + '/'; // .side a sets display: hidden would not hold
const nb = $('#nav-netbox'), ui = nbUI(); nb.style.display = ui ? '' : 'none'; if (ui) nb.href = ui + '/'; // .side a sets display: hidden would not hold
const active = S.jobs.filter(j => j.state === 'pending' || j.state === 'running').length;
$('#n-jobs').textContent = active ? `${active} active` : '';
$('#status').textContent = `updated ${new Date().toLocaleTimeString()}`;