diff --git a/weed/command/scaffold/security.toml b/weed/command/scaffold/security.toml index 2bebdc45f4..29b40e35e2 100644 --- a/weed/command/scaffold/security.toml +++ b/weed/command/scaffold/security.toml @@ -78,7 +78,10 @@ expires_after_seconds = 10 # seconds # gRPC mTLS configuration # All gRPC TLS authentications are mutual (mTLS) # The values for ca, cert, and key are paths to the certificate/key files -# The host name is not checked, so the certificate files can be shared +# The host name is not checked, so the certificate files can be shared. +# Set verify_server_host = true under [grpc] or a [grpc.] section to +# require that a server certificate is valid for the host or IP that component +# dials, as the Rust volume server always does. # Each [grpc.] section also accepts optional client_cert/client_key, # presented when that component dials other servers. Set them when your CA # issues separate serverAuth-only and clientAuth-only certificates; when unset, @@ -89,6 +92,7 @@ expires_after_seconds = 10 # seconds # fatal: the component never falls back to plaintext. [grpc] ca = "" +verify_server_host = false # Set wildcard domain for enable TLS authentication by common names allowed_wildcard_domain = "" # .mycompany.com # Optional allowlist a client checks the dialed server's certificate against: diff --git a/weed/security/tls.go b/weed/security/tls.go index 88417db533..5ef7d6c5e5 100644 --- a/weed/security/tls.go +++ b/weed/security/tls.go @@ -245,6 +245,9 @@ func loadClientTLS(config *util.ViperProxy, component, targetComponent string) ( }, VerificationType: advancedtls.CertVerification, } + if config.GetBool(component+".verify_server_host") || config.GetBool("grpc.verify_server_host") { + options.VerificationType = advancedtls.CertAndHostVerification + } targetPrefix := "" if targetComponent != component { targetPrefix = strings.TrimPrefix(targetComponent, "grpc.") + "_" diff --git a/weed/security/tls_client_cert_test.go b/weed/security/tls_client_cert_test.go index 82cd59b83e..a6f231fcba 100644 --- a/weed/security/tls_client_cert_test.go +++ b/weed/security/tls_client_cert_test.go @@ -178,6 +178,42 @@ func TestLoadClientTLSFallsBackToServingCert(t *testing.T) { // 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)