feat(blockvolume): mutual TLS on the master links

Phase 0, S4 (volume side): --master-tls-cert, --master-tls-key and
--master-ca put the volume host's master link and the GC epoch pull on
mutual TLS (the filer links are unchanged). The start is refused when the
node certificate's CN differs from --server-id, when the set is partial,
or together with --allow-external-status-bind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-24 08:46:37 -08:00
1 parent e125c52624
commit 2b9d446d57
8 files changed
+122 -10

No files matched your search

@@ -9,6 +9,7 @@ import (
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
"github.com/seaweedfs/seaweed-block/core/rpc/control"
"github.com/seaweedfs/seaweed-block/core/rpc/link"
"google.golang.org/grpc"
)
// Automatic GC stays default-off until restart-safe unknown-CAS settlement and
@@ -16,7 +17,7 @@ import (
// The CLI fixes the maximum cut/copy lifetime at five minutes and the HEAD
// hydration reader at thirty seconds. A one-minute mark fits strictly inside
// the one-hour grace. No elapsed TTL alone authorizes capture-intent expiry.
func newChunkMaintenance(cat *chunkstore.Catalog, blobs chunkstore.BlobDeleter, collect bool, master string) (func(context.Context, string) error, func() error, error) {
func newChunkMaintenance(cat *chunkstore.Catalog, blobs chunkstore.BlobDeleter, collect bool, master string, masterTLS grpc.DialOption) (func(context.Context, string) error, func() error, error) {
closeFn := func() error { return nil }
cfg := chunkstore.GCConfig{Grace: defaultChunkGCGrace, IntentTTL: defaultChunkIntentTTL}
if collect {
@@ -26,7 +27,7 @@ func newChunkMaintenance(cat *chunkstore.Catalog, blobs chunkstore.BlobDeleter,
if defaultChunkGCGrace <= max(defaultChunkCutTimeout, chunkstore.DefaultOpDeadline)+defaultChunkMaintenanceTimeout {
return nil, closeFn, errors.New("automatic GC grace does not cover cut/copy and mark bounds")
}
conn, err := link.Dial(master)
conn, err := link.DialMaster(master, masterTLS)
if err != nil {
return nil, closeFn, err
}
@@ -76,7 +76,7 @@ func TestMaintenanceRetentionFailureNeverStartsGC(t *testing.T) {
}
now = time.Now()
}
pass, closeFn, err := newChunkMaintenance(cat, maintenanceBlobs{}, true, "unused-master:1234")
pass, closeFn, err := newChunkMaintenance(cat, maintenanceBlobs{}, true, "unused-master:1234", nil)
if err != nil {
t.Fatal(err)
}
@@ -103,7 +103,7 @@ func TestMaintenanceDefaultRetainNeedsNoEpochClient(t *testing.T) {
if _, err = cat.EnsureVolume(context.Background(), "v", chunkstore.Geometry{NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 8}); err != nil {
t.Fatal(err)
}
pass, closeFn, err := newChunkMaintenance(cat, nil, false, "")
pass, closeFn, err := newChunkMaintenance(cat, nil, false, "", nil)
if err != nil {
t.Fatal(err)
}
@@ -18,6 +18,7 @@ import (
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
"github.com/seaweedfs/seaweed-block/core/hostlimit"
"github.com/seaweedfs/seaweed-block/core/storage"
"google.golang.org/grpc"
)
const (
@@ -162,7 +163,7 @@ type chunkWiring struct {
// newChunkWiring dials the filer and refuses unless it is the only filer of
// its group (chunkstore.CheckSingleFiler).
func newChunkWiring(c chunkFlags, master ...string) (chunkWiring, error) {
func newChunkWiring(c chunkFlags, masterTLS grpc.DialOption, master ...string) (chunkWiring, error) {
client, err := chunkstore.DialSWFS(chunkstore.SWFSConfig{
FilerGRPC: c.filer,
Collection: c.collection,
@@ -186,7 +187,7 @@ func newChunkWiring(c chunkFlags, master ...string) (chunkWiring, error) {
if len(master) > 0 {
masterAddr = master[0]
}
maintenance, closeMaintenance, err := newChunkMaintenance(cat, client, c.automaticGC, masterAddr)
maintenance, closeMaintenance, err := newChunkMaintenance(cat, client, c.automaticGC, masterAddr, masterTLS)
if err != nil {
_ = client.Close()
return chunkWiring{}, err
+17 -2
View File
@@ -33,6 +33,7 @@ import (
"github.com/seaweedfs/seaweed-block/core/recovery"
"github.com/seaweedfs/seaweed-block/core/replication"
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
"github.com/seaweedfs/seaweed-block/core/rpc/link"
"github.com/seaweedfs/seaweed-block/core/storage"
"github.com/seaweedfs/seaweed-block/core/storeident"
"github.com/seaweedfs/seaweed-block/core/transport"
@@ -72,6 +73,7 @@ type flags struct {
// External status bind is off by default. Node-loss gates opt in so
// blockmaster can probe surviving replicas across Kubernetes nodes.
allowExternalStatusBind bool
masterTLS link.TLSFiles // the master link's mutual TLS (phase 0)
runtimeRebuildEndpoint bool
// iSCSI frontend flags. iscsiListen is the TCP address the iSCSI
@@ -180,6 +182,9 @@ func parseFlags(args []string) (flags, error) {
fs.Int64Var(&f.memoryLimitMiB, "memory-limit-mib", 4*storage.DefaultCutPayloadMemoryBudget/(1<<20), "process Go soft memory limit in MiB (0 preserves GOMEMLIMIT); explicit GOMEMLIMIT is preserved unless this flag is set")
fs.StringVar(&f.masterAddr, "master", "", "master gRPC address (required)")
fs.StringVar(&f.serverID, "server-id", "", "this server identity (required)")
fs.StringVar(&f.masterTLS.Cert, "master-tls-cert", "", "this node's certificate (PEM) for mutual TLS to blockmaster; its CN must equal --server-id")
fs.StringVar(&f.masterTLS.Key, "master-tls-key", "", "the key of --master-tls-cert (PEM)")
fs.StringVar(&f.masterTLS.CA, "master-ca", "", "CA (PEM) blockmaster's certificate must verify against")
fs.StringVar(&f.volumeID, "volume-id", "", "served volume (required)")
fs.StringVar(&f.replicaID, "replica-id", "", "served replica (required)")
fs.StringVar(&f.dataAddr, "data-addr", "", "data-path address (required)")
@@ -293,6 +298,9 @@ func parseFlags(args []string) (flags, error) {
if f.version {
return f, nil
}
if err := checkMasterTLS(f); err != nil {
return flags{}, err
}
if f.volumeLockDirTestOnly != "" && os.Getenv("INVOCATION_ID") != "" {
return flags{}, fmt.Errorf("--%s is test-only and refused under systemd (INVOCATION_ID is set): the one-process-per-host guarantee needs %s", volumeLockDirTestOnlyFlag, durable.HostVolumeLockDir)
}
@@ -617,7 +625,14 @@ func runVolume(f flags, exits *processExit) int {
defer stopProfile()
readyCh := make(chan adapter.AssignmentInfo, 16)
h, err := volume.New(hostConfig(f, readyCh))
dialTLS, err := masterDialTLS(f)
if err != nil {
fmt.Fprintln(os.Stderr, "blockvolume:", err)
return 1
}
hc := hostConfig(f, readyCh)
hc.MasterTLS = dialTLS
h, err := volume.New(hc)
if err != nil {
fmt.Fprintln(os.Stderr, "blockvolume:", err)
return 1
@@ -689,7 +704,7 @@ func runVolume(f flags, exits *processExit) int {
var chunkErr error
var chunk chunkWiring
if cfg.Impl == durable.ImplWALStoreChunk {
chunk, chunkErr = newChunkWiring(f.chunk, f.masterAddr)
chunk, chunkErr = newChunkWiring(f.chunk, dialTLS, f.masterAddr)
cfg.ChunkCaptureInterval = f.chunk.captureInterval
cfg.ChunkCutTimeout = defaultChunkCutTimeout
cfg.ChunkMaintenanceInterval = f.chunk.maintenanceInterval
@@ -0,0 +1,48 @@
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/seaweedfs/seaweed-block/core/rpc/link"
"google.golang.org/grpc"
)
// checkMasterTLS checks the master link's mutual-TLS flags at start (phase
// 0): all three files load; the node certificate's CN equals --server-id
// (one node certificate per server id; the master enforces the same rule
// through the role file); and an authenticated volume never listens off
// loopback.
func checkMasterTLS(f flags) error {
if !f.masterTLS.Enabled() {
return nil
}
if err := f.masterTLS.Check(); err != nil {
return fmt.Errorf("--master-tls-cert/--master-tls-key/--master-ca: %w", err)
}
pair, err := tls.LoadX509KeyPair(f.masterTLS.Cert, f.masterTLS.Key)
if err != nil {
return err
}
leaf, err := x509.ParseCertificate(pair.Certificate[0])
if err != nil {
return fmt.Errorf("--master-tls-cert %s: %w", f.masterTLS.Cert, err)
}
if leaf.Subject.CommonName != f.serverID {
return fmt.Errorf("--master-tls-cert %s has CN=%q but --server-id is %q: a node certificate names its server id; issue one with CN=%s", f.masterTLS.Cert, leaf.Subject.CommonName, f.serverID, f.serverID)
}
if f.allowExternalStatusBind {
return fmt.Errorf("--allow-external-status-bind cannot be combined with --master-tls-cert: an authenticated volume's status listener stays on loopback")
}
return nil
}
// masterDialTLS is the master link's dial option: mutual TLS when the flags
// name the files, nil (development-mode plaintext) otherwise.
func masterDialTLS(f flags) (grpc.DialOption, error) {
if !f.masterTLS.Enabled() {
return nil, nil
}
return link.ClientCredentials(f.masterTLS, "")
}
@@ -0,0 +1,44 @@
package main
import (
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/auth/authtest"
)
// Phase 0: the volume's master-link TLS flags are checked at start: a node
// certificate whose CN differs from --server-id, a partial set, or TLS with
// --allow-external-status-bind refuses the start with a cure line.
func TestMasterTLSFlags(t *testing.T) {
ca := authtest.NewCA(t, "ca")
dir := t.TempDir()
caPath := ca.WriteCA(t, dir)
good, goodKey := ca.Issue(t, "m02", time.Now().Add(time.Hour)).WriteFiles(t, dir, "m02") // --server-id=m02 in the base args
other, otherKey := ca.Issue(t, "s9", time.Now().Add(time.Hour)).WriteFiles(t, dir, "s9")
base := phase150RequiredBlockvolumeArgs()
for _, c := range []struct {
name string
args []string
want string // "" = accepted
}{
{"no TLS (development mode)", nil, ""},
{"CN equals --server-id", []string{"--master-tls-cert", good, "--master-tls-key", goodKey, "--master-ca", caPath}, ""},
{"CN differs from --server-id", []string{"--master-tls-cert", other, "--master-tls-key", otherKey, "--master-ca", caPath}, `CN="s9" but --server-id is "m02"`},
{"no CA", []string{"--master-tls-cert", good, "--master-tls-key", goodKey}, "a certificate, its key and a CA"},
{"with --allow-external-status-bind", []string{"--master-tls-cert", good, "--master-tls-key", goodKey, "--master-ca", caPath, "--allow-external-status-bind"}, "cannot be combined"},
} {
f, err := parseFlags(append(append([]string{}, base...), c.args...))
switch {
case c.want == "" && err != nil:
t.Errorf("%s: %v; want accepted", c.name, err)
case c.want != "" && (err == nil || !strings.Contains(err.Error(), c.want)):
t.Errorf("%s: %v; want an error naming %q", c.name, err, c.want)
case c.want == "" && c.args != nil:
if d, err := masterDialTLS(f); err != nil || d == nil {
t.Errorf("%s: dial option %v %v; want mutual TLS", c.name, d, err)
}
}
}
}
@@ -76,7 +76,7 @@ func TestChunkWiringRefusesAMultiFilerGroup(t *testing.T) {
filerpb.RegisterSeaweedFilerServer(srv, &groupFiler{master: "127.0.0.1:9333." + mport})
go func() { _ = srv.Serve(lis) }()
t.Cleanup(srv.Stop)
w, err := newChunkWiring(chunkFlags{filer: filer, path: defaultChunkPath})
w, err := newChunkWiring(chunkFlags{filer: filer, path: defaultChunkPath}, nil)
if c.ok {
if err != nil {
t.Fatalf("one filer refused: %v", err)
+4 -1
View File
@@ -27,6 +27,9 @@ import (
type Config struct {
// MasterAddr is the gRPC address of the block master daemon.
MasterAddr string
// MasterTLS is the mutual-TLS dial option of the master link
// (link.ClientCredentials); nil dials plaintext (development mode).
MasterTLS grpc.DialOption
// ServerID identifies this volume process to the master.
// Used as the HeartbeatReport.ServerID field.
@@ -267,7 +270,7 @@ func New(cfg Config) (*Host, error) {
lg = log.Default()
}
conn, err := link.Dial(cfg.MasterAddr)
conn, err := link.DialMaster(cfg.MasterAddr, cfg.MasterTLS)
if err != nil {
return nil, fmt.Errorf("volume.New: dial master %q: %w", cfg.MasterAddr, err)
}