Allow unused CA alternatives during certificate rotation

This commit is contained in:
pingqiu committed 2026-12-05 14:11:57 -08:00
1 parent 0c0f241ce1
commit eaac405ece
3 files changed
+53 -1

No files matched your search

+14 -1
View File
@@ -11,6 +11,8 @@ import (
// any dial. The same provenance is used by RPC and HTTP client constructors.
func CertificatePool(raw []byte, now time.Time) (*x509.CertPool, error) {
count := 0
current := 0
var dateErr error
for rest := raw; len(rest) > 0; {
block, remaining := pem.Decode(rest)
if block == nil {
@@ -25,7 +27,11 @@ func CertificatePool(raw []byte, now time.Time) (*x509.CertPool, error) {
return nil, LocalCredential(err)
}
if err := ValidateCertificate(cert, now); err != nil {
return nil, err
if dateErr == nil {
dateErr = err
}
} else {
current++
}
count++
}
@@ -33,6 +39,13 @@ func CertificatePool(raw []byte, now time.Time) (*x509.CertPool, error) {
if count == 0 || !pool.AppendCertsFromPEM(raw) {
return nil, LocalCredential(fmt.Errorf("CA bundle holds no valid PEM certificate"))
}
// Roots are alternatives during rotation, not one certificate chain.
// An unused stale root does not invalidate a current one. TLS still
// verifies the dates of the chain it actually selects. Preserve the
// local configuration refusal when the bundle has no current entry.
if current == 0 {
return nil, dateErr
}
return pool, nil
}
@@ -17,12 +17,45 @@ import (
"time"
"github.com/seaweedfs/seaweed-block/core/auth/authtest"
"github.com/seaweedfs/seaweed-block/core/rpc/answer"
"github.com/seaweedfs/seaweed-block/core/rpc/link"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
func TestMasterLinkCARotationRejectsAllInvalidRootsLocally(t *testing.T) {
now := time.Now()
dir := t.TempDir()
good := authtest.NewCA(t, "own-certificate")
cert, key := good.Issue(t, "operator", now.Add(time.Hour)).WriteFiles(t, dir, "own")
expired, _, _ := datedRotationRoot(t, dir, now.Add(-2*time.Hour), now.Add(-time.Hour))
future, _, _ := datedRotationRoot(t, dir, now.Add(time.Hour), now.Add(2*time.Hour))
path := filepath.Join(dir, "all-invalid.pem")
if err := os.WriteFile(path, append(expired, future...), 0o600); err != nil {
t.Fatal(err)
}
files := link.TLSFiles{Cert: cert, Key: key, CA: path}
for _, row := range []struct {
name string
build func() error
}{
{"client", func() error { _, err := link.ClientCredentials(files, ""); return err }},
{"server", func() error { _, err := link.ServerCredentials(files); return err }},
} {
t.Run(row.name, func(t *testing.T) {
err := row.build()
if err == nil {
t.Fatal("all-invalid bundle accepted locally")
}
policy, check := answer.PolicyOf(err)
if check != nil || policy.Exit != 78 || policy.Retryable || policy.Retry != answer.Never {
t.Fatalf("all-invalid bundle policy=%+v check=%v err=%v", policy, check, err)
}
})
}
}
// A CA bundle is a set of alternatives. An unused old/future root must not
// prevent startup or a valid mTLS call; selecting that root must still fail.
func TestMasterLinkCARotationUsesTheVerifiedChain(t *testing.T) {
+6
View File
@@ -16,6 +16,12 @@ transport status. A gRPC `Unavailable` after a request may have been sent is
before proceeding. It is never proof that a new non-idempotent operation is
safe. Typed server refusals retain their own contract.
CA bundles can retain unused expired or not-yet-valid certificates during
rotation when the bundle also contains a certificate valid at the current
time. TLS still checks the chain it selects. A bundle with no currently valid
certificate, or an expired local identity certificate, remains a local
configuration refusal (exit 78).
One global projection gives these exits:
| Failure class | Exit | Same-operation action |