Files
seaweedfs/weed/security/tls_client_cert_test.go
T
Chris LuandClaude Opus 5.5 45458336e1 s3: run a message queue broker beside every S3 gateway (#13235)
* mq broker: let an embedding command own the broker's start failure and shutdown

startQueueServer now returns an error when the broker cannot start instead
of exiting, binds its port before creating the broker so one that cannot
serve never registers with the master, and stops when an optional
shutdownCtx is cancelled. Profiling setup moves to weed mq.broker, since an
embedded broker read the standalone command's profile flags. weed mq.broker
and weed server -mq.broker still exit on a start failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: run a message queue broker beside the gateway by default

weed s3 now starts a broker on -mq.broker.port (17777) once it has reached
its filer, using the masters and filer group the filer reports. The broker
is its own gRPC server: it binds the gateway's -ip.bind, advertises the
address the gateway registers with the master, and joins the cluster like
any other broker. It stops with the gateway. A broker that cannot start,
say because the port is taken, is logged and the gateway keeps serving.
-mq.broker=false turns it off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: start the message queue broker with the S3 gateway

weed server -s3 now runs a broker beside its gateway, started by the
gateway once it reaches the filer. -mq.broker=false leaves it out. An
explicit -mq.broker or -mq.agent still starts the broker on its own, and
then the gateway does not start a second one. Only that explicit broker
exits the server when it cannot start; the one that comes with -s3 is
logged, so a host already running a broker on the port keeps its gateway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mini: run the message queue broker beside the S3 gateway

weed mini now starts a broker with its S3 gateway on -mq.broker.port
(17777, below the Linux ephemeral range), binding and advertising mini's
-ip.bind and -ip like the other services. The port joins mini's port
layout, so a taken port moves to the next free one unless it was given
explicitly. The broker stops with the gateway, before the filer it
flushes to. -mq.broker=false or -s3=false leaves it out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: start the message queue broker with the S3 gateway

weed filer -s3 now runs a broker beside its gateway on -s3.mq.broker.port
(17777), the same way weed server -s3 does: it binds and advertises where
the gateway does, takes the filer's -dataCenter and -rack, stops with the
gateway, and a broker that cannot start is logged while the gateway keeps
serving. -s3.mq.broker=false leaves it out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mini: list the message queue broker when it is running

The S3 gateway now binds its broker's port before it starts serving, as it
does for the Iceberg and Lance listeners, and records whether the bind
worked. Once mini sees the S3 port answer, the result is final: the
progress board shows the broker ready or failed, and the welcome banner
lists its address only when it bound. With -mq.broker=false or -s3=false
neither shows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: keep the default broker off the gateway's own ports

The broker binds before the gateway binds its ports, so a gateway started
with -port=17777, -port.grpc=17777 or -port=7777 (gRPC port 17777) lost
its port to the default broker and exited. weed s3, weed server -s3 and
weed filer -s3 now compare the broker port with the gateway's HTTP,
HTTPS, gRPC, Iceberg, Lance and metrics ports first. A default broker
port that clashes skips the broker with a warning; a broker port given
on the command line that clashes is an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mini: reserve explicitly set gRPC ports before placing other ports

Mini only kept derived gRPC ports free while it placed the other
services, so the broker's default port could take an explicit
-s3.port.grpc=17777 and push the S3 gRPC server to another port. An
explicit gRPC port is now reserved too: the default broker moves off it,
and an explicit broker port on it is rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: keep the default broker off the ports of the servers it starts

With -volume.port=7777 the volume gRPC port is 17777, the default broker
port. The volume server binds its gRPC port only after a master answers,
so the broker could take it first and the whole process exited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mini: keep an explicit gRPC reservation when a derived one moves

When the filer's HTTP port was taken and moved, dropping its old derived
gRPC port also dropped an explicit -s3.port.grpc on the same port, so an
explicit -mq.broker.port could share it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: bind the broker beside S3 where the gateway binds

With -ip.bind=0.0.0.0 -s3.ip.bind=127.0.0.1 the broker that comes with
the gateway listened on every interface. It now takes the gateway's bind
and advertised address, as under weed s3 and weed filer -s3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: keep the default broker off the volume public port

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: report a bad TLS config instead of exiting

The embedded broker loaded grpc.msg_broker TLS through LoadClientTLS and
LoadServerTLS, which exit on a bad config, so a cert without a key or
without grpc.ca took the whole S3 gateway down. The broker now loads TLS
before binding through the error-returning loaders, exported from the
security package as TryLoadClientTLS and TryLoadServerTLS, and returns
the error like any other start failure: the gateway logs it and keeps
serving, while weed mq.broker and weed server -mq.broker still exit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: start the gateway's broker by default only with broker TLS

Without grpc.msg_broker TLS the broker accepts any peer, so the broker
beside weed s3, weed server -s3 and weed filer -s3 now starts on its own
only when grpc.msg_broker has a cert and key in security.toml. Otherwise
it stays off and the gateway logs one line saying so and that
-mq.broker=true (-s3.mq.broker=true for the filer) runs it without TLS.
An explicit true starts it either way and an explicit false keeps it
off. weed mini, a single-process dev setup, keeps its broker on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep the default broker off the ports of the servers it starts

weed filer -s3 checked the broker port only against the gateway's ports,
so with -webdav -webdav.port=17777 the broker could bind 17777 first and
WebDAV then took the whole filer down. The check now also covers the
filer's own HTTP, gRPC and read-only ports, its metrics port, and the
WebDAV, IAM and SFTP ports when those run, as weed server does. A
default broker port that clashes skips the broker with a warning; a
clashing -s3.mq.broker.port is an error before the servers start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: keep the default broker off the filer read-only and debug ports

The port check beside -s3 left out -filer.port.public and, with -debug,
-debug.port, so a broker on 17777 could still take a port the filer or
the debug server binds. Both are now in the list. WebDAV, SFTP and IAM
were already covered when enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 14:53:36 -08:00

644 lines
24 KiB
Go

package security
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/spf13/viper"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
type testCA struct {
cert *x509.Certificate
key *ecdsa.PrivateKey
file string
}
func newTestCA(t *testing.T, dir string) *testCA {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test-ca"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatal(err)
}
file := filepath.Join(dir, "ca.pem")
writePem(t, file, "CERTIFICATE", der)
return &testCA{cert: cert, key: key, file: file}
}
// issue creates a leaf certificate restricted to the given extended key usages.
func (ca *testCA) issue(t *testing.T, dir, name string, ekus []x509.ExtKeyUsage) (certFile, keyFile string) {
return ca.issueWithSANs(t, dir, name, ekus, []string{"localhost"}, nil)
}
func (ca *testCA) issueWithSANs(t *testing.T, dir, name string, ekus []x509.ExtKeyUsage, dnsNames []string, uris []string) (certFile, keyFile string) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
template := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano()),
Subject: pkix.Name{CommonName: name},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: ekus,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
DNSNames: dnsNames,
}
for _, u := range uris {
parsed, err := url.Parse(u)
if err != nil {
t.Fatal(err)
}
template.URIs = append(template.URIs, parsed)
}
der, err := x509.CreateCertificate(rand.Reader, template, ca.cert, &key.PublicKey, ca.key)
if err != nil {
t.Fatal(err)
}
keyDer, err := x509.MarshalECPrivateKey(key)
if err != nil {
t.Fatal(err)
}
certFile = filepath.Join(dir, name+".pem")
keyFile = filepath.Join(dir, name+".key")
writePem(t, certFile, "CERTIFICATE", der)
writePem(t, keyFile, "EC PRIVATE KEY", keyDer)
return certFile, keyFile
}
func writePem(t *testing.T, file, blockType string, der []byte) {
t.Helper()
if err := os.WriteFile(file, pem.EncodeToMemory(&pem.Block{Type: blockType, Bytes: der}), 0600); err != nil {
t.Fatal(err)
}
}
func startTestGrpcServer(t *testing.T, config *util.ViperProxy, component string) string {
t.Helper()
creds, _ := LoadServerTLS(config, component)
if creds == nil {
t.Fatal("LoadServerTLS returned nil")
}
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
server := grpc.NewServer(creds)
healthpb.RegisterHealthServer(server, health.NewServer())
go server.Serve(lis)
t.Cleanup(server.Stop)
return lis.Addr().String()
}
func healthCheck(t *testing.T, addr string, dialOption grpc.DialOption) error {
t.Helper()
conn, err := grpc.NewClient(addr, dialOption)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = healthpb.NewHealthClient(conn).Check(ctx, &healthpb.HealthCheckRequest{})
return err
}
// A component configured with a serverAuth-only serving cert plus a
// clientAuth-only client_cert/client_key pair must dial with the client pair.
func TestLoadClientTLSPrefersClientCert(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "server", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with split client cert failed: %v", err)
}
}
// Without client_cert, the component keeps presenting its serving cert.
func TestLoadClientTLSFallsBackToServingCert(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
dualCert, dualKey := ca.issue(t, dir, "dual", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", dualCert)
v.Set("grpc.master.key", dualKey)
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with dual-EKU cert failed: %v", err)
}
}
// A serverAuth-only cert presented as the client identity fails the peer's
// clientAuth EKU verification — the failure mode client_cert exists to fix.
func TestLoadClientTLSVerifyServerHost(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issueWithSANs(t, dir, "seaweed-master", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, []string{"master.invalid"}, nil)
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.client.cert", clientCert)
v.Set("grpc.client.key", clientKey)
v.Set("grpc.client.allowed_server_commonNames", "seaweed-master")
addr := startTestGrpcServer(t, v, "grpc.master")
_, port, _ := net.SplitHostPort(addr)
byName := net.JoinHostPort("localhost", port)
if err := healthCheck(t, byName, LoadClientTLS(v, "grpc.client")); err != nil {
t.Fatalf("the dialed host is not checked by default: %v", err)
}
v.Set("grpc.verify_server_host", true)
if err := healthCheck(t, byName, LoadClientTLS(v, "grpc.client")); err == nil {
t.Fatal("expected a certificate for master.invalid to be rejected on a connection to localhost")
}
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.client")); err != nil {
t.Fatalf("the certificate's IP SAN matches the dialed address: %v", err)
}
v.Set("grpc.verify_server_host", false)
v.Set("grpc.client.verify_server_host", true)
if err := healthCheck(t, byName, LoadClientTLSForTarget(v, "grpc.client", "grpc.master")); err == nil {
t.Fatal("expected the component setting to check the dialed host")
}
}
func TestLoadClientTLSServerOnlyEkuRejected(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "server", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err == nil {
t.Fatal("expected handshake failure when presenting a serverAuth-only cert as client identity")
}
}
func TestLoadServerTLSUnconfiguredIsPlaintext(t *testing.T) {
v := util.NewViperProxy(viper.New())
creds, _, err := TryLoadServerTLS(v, "grpc.master")
if err != nil || creds != nil {
t.Fatalf("expected no TLS and no error, got creds=%v err=%v", creds, err)
}
}
func TestLoadServerTLSPartialCertKeyFails(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, _ := ca.issue(t, dir, "server", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", certFile)
if _, _, err := TryLoadServerTLS(v, "grpc.master"); err == nil {
t.Fatal("expected error when only cert is set")
}
}
func TestLoadServerTLSBadVersionNameFails(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "server", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", certFile)
v.Set("grpc.master.key", keyFile)
v.Set("tls.min_version", "bogus")
if _, _, err := TryLoadServerTLS(v, "grpc.master"); err == nil {
t.Fatal("expected error for unparseable tls.min_version")
}
}
func TestLoadClientTLSUnconfiguredIsPlaintext(t *testing.T) {
v := util.NewViperProxy(viper.New())
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err != nil {
t.Fatalf("expected insecure dial option, got err=%v", err)
}
}
// A dedicated client pair missing one half is a misconfiguration: the dialer
// must fail rather than silently dial with the fallback serving identity.
func TestLoadClientTLSPartialClientPairFails(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "dual", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth})
clientCert, _ := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.client.cert", certFile)
v.Set("grpc.client.key", keyFile)
v.Set("grpc.client.client_cert", clientCert)
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err == nil {
t.Fatal("expected error for a partial client_cert/client_key pair with a valid fallback")
}
}
func TestLoadClientTLSMissingCaFails(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.client.cert", certFile)
v.Set("grpc.client.key", keyFile)
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err == nil {
t.Fatal("expected error when cert and key are set without grpc.ca")
}
}
func TestLoadClientTLSBadCaPathFails(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", filepath.Join(dir, "missing-ca.pem"))
v.Set("grpc.client.cert", certFile)
v.Set("grpc.client.key", keyFile)
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err == nil {
t.Fatal("expected error for unreadable grpc.ca")
}
}
func TestLoadClientTLSAllowsListedServerName(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-filer", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.master.allowed_server_commonNames", "seaweed-filer,seaweed-volume")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with listed server name failed: %v", err)
}
}
// Any CA-signed cert must not be able to stand in for another component:
// a filer's identity dialing as "the master" has to fail the allowlist.
func TestLoadClientTLSRejectsUnlistedServerName(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-filer", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.master.allowed_server_commonNames", "seaweed-master")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err == nil {
t.Fatal("expected handshake failure for a server identity outside the allowlist")
}
}
func TestLoadClientTLSMatchesServerURISAN(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issueWithSANs(t, dir, "server",
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, nil, []string{"spiffe://cluster.local/ns/seaweedfs/master"})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.master.allowed_server_commonNames", "spiffe://cluster.local/ns/seaweedfs/master")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with URI SAN allowlist failed: %v", err)
}
}
func TestLoadClientTLSMatchesServerWildcardDomain(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issueWithSANs(t, dir, "server",
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, []string{"master.example.com"}, nil)
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.allowed_server_wildcard_domain", ".example.com")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with wildcard domain failed: %v", err)
}
}
func TestMatchesWildcardDomain(t *testing.T) {
cases := []struct {
name string
domain string
want bool
}{
{"node.prod.example.com", ".example.com", true},
{"node.example.com.", ".example.com", true},
{"node.example.com.", ".example.com.", true},
{"node.example.com..", ".example.com", false},
{"node..example.com.", ".example.com", false},
{"example.com", ".example.com", false},
{".example.com", ".example.com", false},
{"node..example.com", ".example.com", false},
{"node-.example.com", ".example.com", false},
{"-node.example.com", ".example.com", false},
{"node.example.com", "", false},
{"node.example.com", ".other.net", false},
{"badexample.com", "example.com", false},
{"good.example.com", "example.com", true},
{"*.example.com", ".example.com", true},
{"1.2.3.4", ".3.4", false},
}
for _, c := range cases {
if got := matchesWildcardDomain(c.name, c.domain); got != c.want {
t.Errorf("matchesWildcardDomain(%q, %q) = %v, want %v", c.name, c.domain, got, c.want)
}
}
}
// A CA-signed name that is not a valid hostname must not satisfy the wildcard
// suffix check: ".example.com" equals the wildcard and has no hostname label.
func TestLoadClientTLSRejectsInvalidWildcardName(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issueWithSANs(t, dir, ".example.com",
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, []string{".example.com"}, nil)
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.allowed_server_wildcard_domain", ".example.com")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err == nil {
t.Fatal("expected handshake failure: a non-hostname identity must not match the wildcard domain")
}
}
// A URI SAN whose path happens to end in the wildcard domain must not pass:
// URI identities are exact-match only.
func TestLoadClientTLSURIPathDoesNotMatchWildcardDomain(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issueWithSANs(t, dir, "server",
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, nil, []string{"spiffe://other.invalid/workload.example.com"})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.allowed_server_wildcard_domain", ".example.com")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err == nil {
t.Fatal("expected handshake failure: URI SAN path must not match the wildcard domain")
}
}
func TestLoadClientTLSGlobalServerNamesFallback(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-filer", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.master.cert", serverCert)
v.Set("grpc.master.key", serverKey)
v.Set("grpc.master.client_cert", clientCert)
v.Set("grpc.master.client_key", clientKey)
v.Set("grpc.allowed_server_commonNames", "seaweed-filer")
addr := startTestGrpcServer(t, v, "grpc.master")
if err := healthCheck(t, addr, LoadClientTLS(v, "grpc.master")); err != nil {
t.Fatalf("health check with global allowlist failed: %v", err)
}
}
// A per-target allowlist lives under the dialing component: the filer reads
// grpc.filer.volume_allowed_server_commonNames for volume calls, never
// grpc.volume.allowed_server_commonNames, which is the volume's own outbound
// allowlist (e.g. naming only the master).
func TestLoadClientTLSForTargetUsesClientScopedAllowlist(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-volume", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.volume.cert", serverCert)
v.Set("grpc.volume.key", serverKey)
v.Set("grpc.volume.allowed_server_commonNames", "seaweed-master")
v.Set("grpc.filer.client_cert", clientCert)
v.Set("grpc.filer.client_key", clientKey)
v.Set("grpc.filer.volume_allowed_server_commonNames", "seaweed-volume")
addr := startTestGrpcServer(t, v, "grpc.volume")
if err := healthCheck(t, addr, LoadClientTLSForTarget(v, "grpc.filer", "grpc.volume")); err != nil {
t.Fatalf("health check with client-scoped target allowlist failed: %v", err)
}
}
func TestLoadClientTLSForTargetIgnoresTargetOutboundAllowlist(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-volume", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.volume.cert", serverCert)
v.Set("grpc.volume.key", serverKey)
v.Set("grpc.volume.allowed_server_commonNames", "seaweed-master")
v.Set("grpc.filer.client_cert", clientCert)
v.Set("grpc.filer.client_key", clientKey)
addr := startTestGrpcServer(t, v, "grpc.volume")
if err := healthCheck(t, addr, LoadClientTLSForTarget(v, "grpc.filer", "grpc.volume")); err != nil {
t.Fatalf("health check must not apply the target's outbound allowlist: %v", err)
}
}
func TestLoadClientTLSForTargetFallsBackToClientAllowlist(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-volume", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.volume.cert", serverCert)
v.Set("grpc.volume.key", serverKey)
v.Set("grpc.filer.client_cert", clientCert)
v.Set("grpc.filer.client_key", clientKey)
v.Set("grpc.filer.allowed_server_commonNames", "seaweed-volume")
addr := startTestGrpcServer(t, v, "grpc.volume")
if err := healthCheck(t, addr, LoadClientTLSForTarget(v, "grpc.filer", "grpc.volume")); err != nil {
t.Fatalf("health check with client component allowlist failed: %v", err)
}
}
func TestLoadClientTLSRejectsUnappliedTargetAllowlist(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.client.cert", certFile)
v.Set("grpc.client.key", keyFile)
v.Set("grpc.client.filer_allowed_server_commonNames", "seaweed-filer")
v.Set("grpc.client.master_allowed_server_wildcard_domain", ".master.example.com")
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err == nil {
t.Fatal("expected error when per-target allowlists are the only allowlist of a single-option client")
}
v.Set("grpc.client.allowed_server_commonNames", "seaweed-filer,seaweed-master")
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err != nil {
t.Fatalf("component allowlist must be enough: %v", err)
}
}
func TestLoadClientTLSRejectsUnappliedTargetAllowlistFromEnv(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
certFile, keyFile := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
raw := viper.New()
raw.AutomaticEnv()
raw.SetEnvPrefix("weed")
raw.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v := util.NewViperProxy(raw)
v.Set("grpc.ca", ca.file)
v.Set("grpc.client.cert", certFile)
v.Set("grpc.client.key", keyFile)
t.Setenv("WEED_GRPC_CLIENT_FILER_ALLOWED_SERVER_COMMONNAMES", "seaweed-filer")
if _, err := TryLoadClientTLS(v, "grpc.client", "grpc.client"); err == nil {
t.Fatal("expected error when a per-target allowlist comes only from the environment")
}
fileOnly := util.NewViperProxy(viper.New())
fileOnly.Set("grpc.ca", ca.file)
fileOnly.Set("grpc.client.cert", certFile)
fileOnly.Set("grpc.client.key", keyFile)
if _, err := TryLoadClientTLS(fileOnly, "grpc.client", "grpc.client"); err != nil {
t.Fatalf("a config that does not read the environment must ignore it: %v", err)
}
}
func TestLoadClientTLSFilerKeepsTargetAllowlists(t *testing.T) {
dir := t.TempDir()
ca := newTestCA(t, dir)
serverCert, serverKey := ca.issue(t, dir, "seaweed-volume", []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth})
clientCert, clientKey := ca.issue(t, dir, "client", []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth})
v := util.NewViperProxy(viper.New())
v.Set("grpc.ca", ca.file)
v.Set("grpc.volume.cert", serverCert)
v.Set("grpc.volume.key", serverKey)
v.Set("grpc.filer.client_cert", clientCert)
v.Set("grpc.filer.client_key", clientKey)
v.Set("grpc.filer.volume_allowed_server_commonNames", "seaweed-volume")
v.Set("grpc.filer.master_allowed_server_commonNames", "seaweed-master")
if _, err := TryLoadClientTLS(v, "grpc.filer", "grpc.filer"); err != nil {
t.Fatalf("the filer applies its per-target keys through LoadClientTLSForTarget: %v", err)
}
addr := startTestGrpcServer(t, v, "grpc.volume")
if err := healthCheck(t, addr, LoadClientTLSForTarget(v, "grpc.filer", "grpc.master")); err == nil {
t.Fatal("expected the master allowlist to reject a volume certificate")
}
}