feat(router): confirm DexaFlow modes and expose the paired admin interface
This commit is contained in:
@@ -48,6 +48,10 @@ func recoveryPeer(state tls.ConnectionState, coreID string) bool {
|
||||
|
||||
func (p *Pairing) recoveryHandler() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/v1/router/admin" {
|
||||
p.routerAdmin(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
p.mu.Lock()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"nodedc.local/mission-core/node-agent/internal/routers"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (p *Pairing) routerAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
var body routers.AdminRequest
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2*1048576))
|
||||
decoder.DisallowUnknownFields()
|
||||
if r.Method != "POST" || err != nil || r.TLS == nil || decoder.Decode(&body) != nil || !body.Valid() {
|
||||
http.Error(w, "Invalid request", 400)
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
b := p.state.Binding
|
||||
allowed := p.state.Phase == "paired" && b != nil && p.networkPeerLocked(host) && recoveryPeer(*r.TLS, b.CoreID) && body.BindingID == b.BindingID
|
||||
collector := p.Routers
|
||||
p.mu.Unlock()
|
||||
if !allowed {
|
||||
http.Error(w, "Binding unavailable", 403)
|
||||
return
|
||||
}
|
||||
if collector == nil {
|
||||
http.Error(w, "Router unavailable", 503)
|
||||
return
|
||||
}
|
||||
if body.Method == "POST" {
|
||||
// Extend only an authenticated admin request, not the shared listener.
|
||||
if err := http.NewResponseController(w).SetWriteDeadline(time.Now().Add(100 * time.Second)); err != nil {
|
||||
http.Error(w, "Router response deadline unavailable", 503)
|
||||
return
|
||||
}
|
||||
}
|
||||
result, err := collector.Admin(r.Context(), body)
|
||||
if err != nil {
|
||||
http.Error(w, "Router request failed", 502)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"nodedc.local/mission-core/node-agent/internal/routers"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type adminDeadlineRecorder struct {
|
||||
*httptest.ResponseRecorder
|
||||
deadline time.Time
|
||||
}
|
||||
|
||||
func (w *adminDeadlineRecorder) SetWriteDeadline(t time.Time) error { w.deadline = t; return nil }
|
||||
|
||||
func TestRouterAdminLongDeadlineRequiresAuthenticatedWrite(t *testing.T) {
|
||||
p, client := recoveryFixture(t)
|
||||
p.Routers = routers.Open(t.TempDir())
|
||||
cert, _ := x509.ParseCertificate(client.Certificate[0])
|
||||
for _, tc := range []struct {
|
||||
binding, method string
|
||||
status int
|
||||
extended bool
|
||||
}{
|
||||
{"old", "POST", 403, false},
|
||||
{p.state.Binding.BindingID, "GET", 502, false},
|
||||
{p.state.Binding.BindingID, "POST", 502, true},
|
||||
} {
|
||||
body, _ := json.Marshal(map[string]any{"binding_id": tc.binding, "device_id": "router", "method": tc.method, "path": "/cgi-bin/dexaflow_manual"})
|
||||
r := httptest.NewRequest("POST", "/v1/router/admin", strings.NewReader(string(body)))
|
||||
r.RemoteAddr = "100.64.10.5:42000"
|
||||
r.TLS = &tls.ConnectionState{VerifiedChains: [][]*x509.Certificate{{cert}}, PeerCertificates: []*x509.Certificate{cert}}
|
||||
w := &adminDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()}
|
||||
p.recoveryHandler().ServeHTTP(w, r)
|
||||
if w.Code != tc.status || !w.deadline.IsZero() != tc.extended {
|
||||
t.Fatalf("%s: code %d deadline %v", tc.method, w.Code, w.deadline)
|
||||
}
|
||||
if tc.extended && time.Until(w.deadline) < 90*time.Second {
|
||||
t.Fatal("native command inherits the short shared-listener deadline")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterAdminRequiresCurrentBinding(t *testing.T) {
|
||||
p, client := recoveryFixture(t)
|
||||
body := map[string]any{"binding_id": "old", "device_id": "router", "method": "GET", "path": "/"}
|
||||
if out := recoveryCall(p, client, "/v1/router/admin", body); out.Code != 403 {
|
||||
t.Fatal(out.Code)
|
||||
}
|
||||
body["binding_id"] = p.state.Binding.BindingID
|
||||
if out := recoveryCall(p, client, "/v1/router/admin", body); out.Code != 503 {
|
||||
t.Fatal(out.Code)
|
||||
}
|
||||
if e := p.cancel(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if out := recoveryCall(p, client, "/v1/router/admin", body); out.Code != 403 {
|
||||
t.Fatal(out.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user