mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-16 00:38:53 +01:00
security: refuse to start when per-target gRPC server allowlists are not applied (#12662)
* security: refuse to start when per-target server allowlists are not applied A component that dials every server with one option ignored <target>_allowed_server_* keys, so a config that only set those accepted any CA-signed server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: say which components scope server allowlists per target Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * security: also catch per-target server allowlists set through WEED_ env vars Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d80e905fc7
commit
504de9b562
3 files changed
+118
-3
No files matched your search
@@ -101,9 +101,10 @@ allowed_wildcard_domain = "" # .mycompany.com
|
||||
# With a per-workload CA, list the identities each server may present so one
|
||||
# workload cannot impersonate another component. Unset accepts any CA-signed
|
||||
# server certificate, as before. Any [grpc.<component>] section may set its own
|
||||
# allowed_server_commonNames to override this list, and scope it further per
|
||||
# dialed target, e.g. the filer reads [grpc.filer]
|
||||
# volume_allowed_server_commonNames for volume server calls.
|
||||
# allowed_server_commonNames to override this list. Only the filer scopes it
|
||||
# further per dialed target: [grpc.filer] master_, volume_ and
|
||||
# s3_allowed_server_commonNames. Other components dial every server with one
|
||||
# allowlist and refuse to start when per-target keys are their only allowlist.
|
||||
allowed_server_commonNames = "" # comma-separated server identities
|
||||
allowed_server_wildcard_domain = "" # .mycompany.com
|
||||
|
||||
|
||||
@@ -266,6 +266,8 @@ func loadClientTLS(config *util.ViperProxy, component, targetComponent string) (
|
||||
}
|
||||
if len(auther.AllowedCommonNames) != 0 || auther.AllowedWildcardDomain != "" {
|
||||
options.AdditionalPeerVerification = auther.AuthenticateServer
|
||||
} else if key := unappliedTargetAllowlist(config, component, targetComponent); key != "" {
|
||||
return nil, fmt.Errorf("%s is set, but %s dials every server with one allowlist: set %s.allowed_server_commonNames", key, component, component)
|
||||
} else {
|
||||
options.AdditionalPeerVerification = func(params *advancedtls.HandshakeVerificationInfo) (*advancedtls.PostHandshakeVerificationResults, error) {
|
||||
return &advancedtls.PostHandshakeVerificationResults{}, nil
|
||||
@@ -349,6 +351,44 @@ func checkTLSFiles(certFile, keyFile, caFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// targetAllowlistClients are the components that dial some targets through
|
||||
// LoadClientTLSForTarget, so their <target>_allowed_server_* keys apply.
|
||||
var targetAllowlistClients = map[string]bool{"grpc.filer": true}
|
||||
|
||||
// unappliedTargetAllowlist returns a per-target server allowlist key set under
|
||||
// a component that only dials with one option, which would otherwise leave
|
||||
// that component accepting any CA-signed server.
|
||||
func unappliedTargetAllowlist(config *util.ViperProxy, component, targetComponent string) string {
|
||||
if targetComponent != component || targetAllowlistClients[component] {
|
||||
return ""
|
||||
}
|
||||
prefix := strings.ToLower(component) + "."
|
||||
candidates := config.AllKeys()
|
||||
envPrefix := "WEED_" + strings.ToUpper(strings.ReplaceAll(component, ".", "_")) + "_"
|
||||
for _, env := range os.Environ() {
|
||||
if name, _, ok := strings.Cut(env, "="); ok && strings.HasPrefix(name, envPrefix) {
|
||||
candidates = append(candidates, prefix+strings.ToLower(strings.TrimPrefix(name, envPrefix)))
|
||||
}
|
||||
}
|
||||
var keys []string
|
||||
for _, key := range candidates {
|
||||
name, ok := strings.CutPrefix(key, prefix)
|
||||
if !ok || strings.Contains(name, ".") || config.GetString(key) == "" {
|
||||
continue
|
||||
}
|
||||
for _, suffix := range []string{"_allowed_server_commonnames", "_allowed_server_wildcard_domain"} {
|
||||
if target, ok := strings.CutSuffix(name, suffix); ok && target != "" {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
return ""
|
||||
}
|
||||
slices.Sort(keys)
|
||||
return keys[0]
|
||||
}
|
||||
|
||||
func parseAllowedCommonNames(commonNames string) map[string]bool {
|
||||
allowed := make(map[string]bool)
|
||||
for _, s := range strings.Split(commonNames, ",") {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -567,3 +568,76 @@ func TestLoadClientTLSForTargetFallsBackToClientAllowlist(t *testing.T) {
|
||||
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 := loadClientTLS(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 := loadClientTLS(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 := loadClientTLS(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 := loadClientTLS(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 := loadClientTLS(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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user