package cli import ( "net/http" "net/http/httptest" "net/netip" "testing" ) func TestTheMirrorAPIAnswersOnlyThisMachineUnlessTold(t *testing.T) { ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {}) call := func(h http.Handler, from string) int { r := httptest.NewRequest("DELETE", "/v1/models/m/v", nil) r.RemoteAddr = from + ":5000" w := httptest.NewRecorder() h.ServeHTTP(w, r) return w.Code } cp := []netip.Prefix{netip.MustParsePrefix("10.0.0.140/32")} // Nothing allowed and no TLS used to mean anyone: every node could delete // models. if got := call(apiGate(false, nil, ok), "10.0.0.141"); got != http.StatusForbidden { t.Errorf("a node reached an unconfigured mirror API: %d", got) } if got := call(apiGate(false, nil, ok), "127.0.0.1"); got != http.StatusOK { t.Errorf("the mirror's own machine was refused: %d", got) } if got := call(apiGate(false, cp, ok), "10.0.0.140"); got != http.StatusOK { t.Errorf("the allowed control plane was refused: %d", got) } if got := call(apiGate(false, cp, ok), "10.0.0.141"); got != http.StatusForbidden { t.Errorf("a node outside the allowlist got through: %d", got) } // With mutual TLS the certificate decides. if got := call(apiGate(true, nil, ok), "10.0.0.141"); got != http.StatusOK { t.Errorf("mTLS without an allowlist refused a client: %d", got) } }