mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Cover CA rotation in the Kubernetes status writer
This commit is contained in:
1 parent
eaac405ece
commit
1e9983fc01
1 file changed
+75
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user