From 1e9983fc01b95eeed8c9cd3abbd6f55dc09b4ab7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 5 Dec 2026 14:18:19 -0800 Subject: [PATCH] Cover CA rotation in the Kubernetes status writer --- .../core/ops/kubernetes_ca_rotation_test.go | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 seaweed-block/core/ops/kubernetes_ca_rotation_test.go diff --git a/seaweed-block/core/ops/kubernetes_ca_rotation_test.go b/seaweed-block/core/ops/kubernetes_ca_rotation_test.go new file mode 100644 index 0000000000..f7f2737427 --- /dev/null +++ b/seaweed-block/core/ops/kubernetes_ca_rotation_test.go @@ -0,0 +1,75 @@ +package ops + +import ( + "context" + "crypto/tls" + "encoding/pem" + "net" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/auth/authtest" +) + +// CertificatePool also gates this HTTP writer, not only RPC constructors. +func TestKubernetesStatusCARotationUsesTheVerifiedChain(t *testing.T) { + var goodCalls, badCalls atomic.Int64 + good := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "PATCH" || r.Header.Get("Authorization") != "Bearer fixture-token" { + t.Errorf("unexpected writer request %s %q", r.Method, r.Header.Get("Authorization")) + } + goodCalls.Add(1) + w.WriteHeader(http.StatusOK) + })) + defer good.Close() + dir := t.TempDir() + ca := authtest.NewCA(t, "unused-rotation") + expired := ca.Issue(t, "expired-alternative", time.Now().Add(-time.Hour)) + bundle := append(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: good.Certificate().Raw}), expired.CertPEM...) + caPath, tokenPath := filepath.Join(dir, "ca.pem"), filepath.Join(dir, "token") + if err := os.WriteFile(caPath, bundle, 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(tokenPath, []byte("fixture-token"), 0o600); err != nil { + t.Fatal(err) + } + clientFor := func(address string) *KubernetesStatusClient { + host, port, err := net.SplitHostPort(strings.TrimPrefix(address, "https://")) + if err != nil { + t.Fatal(err) + } + client, err := newKubernetesStatusClient(host, port, tokenPath, caPath) + if err != nil { + t.Fatalf("unused expired entry blocks HTTP writer construction: %v", err) + } + t.Cleanup(client.HTTPClient.CloseIdleConnections) + return client + } + write := func(client *KubernetesStatusClient) error { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + return client.WriteClusterStatus(ctx, OperatorObjectRef{Namespace: "default", Name: "demo"}, SwBlockClusterCRDStatus{}) + } + if err := write(clientFor(good.URL)); err != nil { + t.Fatalf("valid HTTP chain beside unused expired entry: %v", err) + } + if goodCalls.Load() != 1 { + t.Fatalf("status PATCH was not witnessed: calls=%d", goodCalls.Load()) + } + bad := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { badCalls.Add(1); w.WriteHeader(http.StatusOK) })) + bad.TLS = &tls.Config{Certificates: []tls.Certificate{expired.TLS}, MinVersion: tls.VersionTLS12} + bad.StartTLS() + defer bad.Close() + if err := write(clientFor(bad.URL)); err == nil { + t.Fatal("selected expired HTTP certificate accepted") + } + if badCalls.Load() != 0 { + t.Fatalf("invalid selected chain reached status writer: calls=%d", badCalls.Load()) + } +}