NetBox through Rollcall: the admin certificate signs in to NetBox
With ipam.ui_login_secret_file, the proxy tells NetBox who the admin is (X-Rollcall-User: the certificate's admin name) together with a shared secret (X-Rollcall-Proxy-Secret). NetBox takes the user from that header (REMOTE_AUTH) and creates it on first use; the web server in front of NetBox drops the header from any request that lacks the secret or does not come from the region, so reaching NetBox directly claims no one. Both headers coming from a browser are always dropped by Rollcall; an admin signed in with a token instead of a certificate logs in to NetBox by hand. A secret under 16 characters or an unreadable file leaves the proxy off, with a log line. The lab NetBox install sets it up: REMOTE_AUTH settings, a guarded Caddyfile (readable by caddy only: the secret is in it), the secret in secrets.env, and a rollcall-admins group that may do everything. Rollcall's NetBox user may now view cables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a613014073
commit
44ac453bde
7 files changed
+146
-20
No files matched your search
+14
-13
@@ -37,19 +37,20 @@ import (
|
||||
)
|
||||
|
||||
type IPAMConfig struct {
|
||||
Provider string `json:"provider"` // "" = built-in only | netbox | infoblox
|
||||
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
|
||||
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
|
||||
NetworkView string `json:"network_view"` // Infoblox, default "default"
|
||||
DNSView string `json:"dns_view"` // Infoblox host records' DNS view, default "default"; "-" = IPAM only
|
||||
CAFile string `json:"ca_file"` // CA that signed the server certificate ("" = system roots)
|
||||
DNSZone string `json:"dns_zone"` // dns_name = <host>.<zone> unless the subnet has a domain
|
||||
Tag string `json:"tag"` // ownership tag, default "rollcall"
|
||||
MachineField string `json:"machine_field"` // custom field holding the machine ID, default "rollcall_machine"
|
||||
SyncInterval string `json:"sync_interval"` // reconcile period, default 5m
|
||||
Provider string `json:"provider"` // "" = built-in only | netbox | infoblox
|
||||
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)
|
||||
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
|
||||
NetworkView string `json:"network_view"` // Infoblox, default "default"
|
||||
DNSView string `json:"dns_view"` // Infoblox host records' DNS view, default "default"; "-" = IPAM only
|
||||
CAFile string `json:"ca_file"` // CA that signed the server certificate ("" = system roots)
|
||||
DNSZone string `json:"dns_zone"` // dns_name = <host>.<zone> unless the subnet has a domain
|
||||
Tag string `json:"tag"` // ownership tag, default "rollcall"
|
||||
MachineField string `json:"machine_field"` // custom field holding the machine ID, default "rollcall_machine"
|
||||
SyncInterval string `json:"sync_interval"` // reconcile period, default 5m
|
||||
// DeviceSync mirrors machines as NetBox devices (nbdevices.go); nil = off
|
||||
DeviceSync *DeviceSyncConfig `json:"device_sync,omitempty"`
|
||||
}
|
||||
|
||||
+29
-2
@@ -10,11 +10,22 @@ package main
|
||||
//
|
||||
// ui_url, when set, is where links to NetBox point instead (NetBox reached
|
||||
// directly); the proxy stays.
|
||||
//
|
||||
// Signing in with the admin certificate: with ui_login_secret_file, the
|
||||
// proxy tells NetBox who the admin is (X-Rollcall-User: the certificate's
|
||||
// admin name) together with the shared secret (X-Rollcall-Proxy-Secret).
|
||||
// NetBox takes the user from that header (REMOTE_AUTH_HEADER), and the web
|
||||
// server in front of NetBox must drop it from any request without the
|
||||
// secret, or anyone reaching NetBox directly could claim a user. Both
|
||||
// headers coming from the browser are always dropped here. An admin with a
|
||||
// token instead of a certificate signs in to NetBox by hand.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -42,16 +53,32 @@ func (a *App) netboxUILink() string {
|
||||
return a.netboxUIPath()
|
||||
}
|
||||
|
||||
func (a *App) netboxProxy() http.Handler {
|
||||
func (a *App) netboxProxy() (http.Handler, error) {
|
||||
nb := a.ipam.(*netBox)
|
||||
target, _ := url.Parse(nb.base)
|
||||
origin := target.Scheme + "://" + target.Host
|
||||
secret := ""
|
||||
if f := a.cfg.IPAM.UILoginFile; f != "" {
|
||||
b, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ipam ui_login_secret_file: %w", err)
|
||||
}
|
||||
if secret = strings.TrimSpace(string(b)); len(secret) < 16 {
|
||||
return nil, fmt.Errorf("ipam ui_login_secret_file: under 16 characters")
|
||||
}
|
||||
}
|
||||
return &httputil.ReverseProxy{
|
||||
Transport: nb.client.Transport,
|
||||
Rewrite: func(pr *httputil.ProxyRequest) {
|
||||
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")
|
||||
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)
|
||||
}
|
||||
pr.SetXForwarded()
|
||||
},
|
||||
ModifyResponse: func(resp *http.Response) error {
|
||||
@@ -60,5 +87,5 @@ func (a *App) netboxProxy() http.Handler {
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -32,10 +33,15 @@ func TestNetBoxProxy(t *testing.T) {
|
||||
}))
|
||||
defer up.Close()
|
||||
a := &App{cfg: &Config{IPAM: IPAMConfig{URL: up.URL + "/netbox/"}}, ipam: &netBox{base: up.URL + "/netbox", client: up.Client()}}
|
||||
h := a.netboxProxy()
|
||||
h, err := a.netboxProxy()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "https://rollcall.example/netbox/", nil)
|
||||
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
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusFound || rec.Header().Get("Location") != "/netbox/login/?next=/netbox/" {
|
||||
@@ -44,6 +50,9 @@ 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") != "" {
|
||||
t.Fatal("the browser's user header went to NetBox")
|
||||
}
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest("GET", "https://rollcall.example/netbox/dcim/sites/?q=lab", nil))
|
||||
if rec.Body.String() != "netbox page" || seen.URL.RequestURI() != "/netbox/dcim/sites/?q=lab" || seen.Header.Get("X-Forwarded-Host") != "rollcall.example" {
|
||||
@@ -84,3 +93,29 @@ func TestNetBoxProxyRoutes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// with the shared secret set, a request without an admin certificate signs no one in
|
||||
func TestNetBoxProxyLoginNeedsCertificate(t *testing.T) {
|
||||
var seen *http.Request
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { seen = r }))
|
||||
defer up.Close()
|
||||
f := t.TempDir() + "/secret"
|
||||
if err := os.WriteFile(f, []byte("0123456789abcdef0123\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a := &App{cfg: &Config{IPAM: IPAMConfig{URL: up.URL + "/netbox/", UILoginFile: f}}, ipam: &netBox{base: up.URL + "/netbox", client: up.Client()}}
|
||||
h, err := a.netboxProxy()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "https://rollcall.example/netbox/", nil))
|
||||
if seen.Header.Get("X-Rollcall-User") != "" || seen.Header.Get("X-Rollcall-Proxy-Secret") != "" {
|
||||
t.Fatal("signed in without a certificate")
|
||||
}
|
||||
if err := os.WriteFile(f, []byte("short"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := a.netboxProxy(); err == nil {
|
||||
t.Fatal("a short secret was taken")
|
||||
}
|
||||
}
|
||||
@@ -288,9 +288,13 @@ func (a *App) routes() http.Handler {
|
||||
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
|
||||
h := a.adminAuth(a.netboxProxy().ServeHTTP)
|
||||
for _, m := range []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} {
|
||||
mux.HandleFunc(m+" "+p+"/", h)
|
||||
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))
|
||||
|
||||
Reference in new issue
Block a user