diff --git a/k8s/charts/seaweedfs/README.md b/k8s/charts/seaweedfs/README.md index 796abd12b6..ae15dfdb7b 100644 --- a/k8s/charts/seaweedfs/README.md +++ b/k8s/charts/seaweedfs/README.md @@ -267,6 +267,18 @@ filer: enabled: true ``` +### Message queue broker + +An S3 gateway can run a message queue broker beside it, on gRPC port 17777 by default. `s3.mqBroker`, `filer.s3.mqBroker` and `allInOne.s3.mqBroker` choose it: + +- `auto` (the default) runs it only with `global.seaweedfs.enableSecurity`, where the chart gives it mutual TLS through `[grpc.msg_broker]` in security.toml. +- `true` runs it either way. Without `enableSecurity` it then accepts any client that can reach the port. +- `false` never runs it. + +`s3.mqBrokerPort`, `filer.s3.mqBrokerPort` and `allInOne.s3.mqBrokerPort` set the port, and 0 also turns the broker off. The `allInOne.s3` values inherit the `s3` ones when null. The container port and NetworkPolicy entry appear only when the broker runs. + +Brokers register with the master under the pod IP, and clients and other brokers find them there and dial that address, so the port is deliberately left off the Services; a LoadBalancer or NodePort Service would only publish it outside the cluster. With `networkPolicy.enabled` the chart admits the port from the release's own pods; publishers and subscribers elsewhere need an `extraIngress` rule for it. + ### Enabling Authentication to S3 To enable authentication for S3, you have two options: diff --git a/k8s/charts/seaweedfs/templates/all-in-one/all-in-one-deployment.yaml b/k8s/charts/seaweedfs/templates/all-in-one/all-in-one-deployment.yaml index c81d7aafee..937b89c17a 100644 --- a/k8s/charts/seaweedfs/templates/all-in-one/all-in-one-deployment.yaml +++ b/k8s/charts/seaweedfs/templates/all-in-one/all-in-one-deployment.yaml @@ -274,6 +274,9 @@ spec: {{- if gt $s3PreStopSeconds 0 }} -s3.preStopSeconds={{ $s3PreStopSeconds }} \ {{- end }} + {{- with (include "seaweedfs.mqBroker" (list . "allInOne") | fromJson).args }} + {{ . }} \ + {{- end }} {{- end }} {{- if .Values.allInOne.sftp.enabled }} -sftp \ @@ -406,6 +409,10 @@ spec: - containerPort: {{ $httpsPort }} name: swfs-s3-tls {{- end }} + {{- with (include "seaweedfs.mqBroker" (list . "allInOne") | fromJson).port }} + - containerPort: {{ int . }} + name: swfs-mq-broker + {{- end }} {{- end }} {{- if .Values.allInOne.sftp.enabled }} - containerPort: {{ .Values.allInOne.sftp.port | default .Values.sftp.port }} diff --git a/k8s/charts/seaweedfs/templates/filer/filer-statefulset.yaml b/k8s/charts/seaweedfs/templates/filer/filer-statefulset.yaml index a8d427683c..d1dab53538 100644 --- a/k8s/charts/seaweedfs/templates/filer/filer-statefulset.yaml +++ b/k8s/charts/seaweedfs/templates/filer/filer-statefulset.yaml @@ -296,6 +296,9 @@ spec: {{- if gt $s3PreStopSeconds 0 }} -s3.preStopSeconds={{ $s3PreStopSeconds }} \ {{- end }} + {{- with (include "seaweedfs.mqBroker" (list . "filer") | fromJson).args }} + {{ . }} \ + {{- end }} {{- end }} -master={{ include "seaweedfs.masterServerArg" . }} \ {{- range .Values.filer.extraArgs }} @@ -363,6 +366,10 @@ spec: - containerPort: {{ .Values.filer.s3.httpsPort }} name: swfs-s3-tls {{- end }} + {{- with (include "seaweedfs.mqBroker" (list . "filer") | fromJson).port }} + - containerPort: {{ int . }} + name: swfs-mq-broker + {{- end }} {{- end }} {{- $jwt := (.Values.global.seaweedfs.securityConfig).jwtSigning | default dict }} {{- $isJwtEnabled := or $jwt.filerWrite $jwt.filerRead }} diff --git a/k8s/charts/seaweedfs/templates/s3/s3-deployment.yaml b/k8s/charts/seaweedfs/templates/s3/s3-deployment.yaml index 5f627e9e2d..244bc31e17 100644 --- a/k8s/charts/seaweedfs/templates/s3/s3-deployment.yaml +++ b/k8s/charts/seaweedfs/templates/s3/s3-deployment.yaml @@ -179,6 +179,9 @@ spec: {{- end }} {{- /* rendered even when 0: an if would drop the flag and weed would serve its default */}} -port.lance={{ .Values.s3.lancePort | default 0 }} \ + {{- with (include "seaweedfs.mqBroker" (list . "s3") | fromJson).args }} + {{ . }} \ + {{- end }} {{- range .Values.s3.extraArgs }} {{ . }} \ {{- end }} @@ -233,6 +236,10 @@ spec: - containerPort: {{ .Values.s3.lancePort }} name: swfs-lance {{- end }} + {{- with (include "seaweedfs.mqBroker" (list . "s3") | fromJson).port }} + - containerPort: {{ int . }} + name: swfs-mq-broker + {{- end }} {{- if .Values.s3.metricsPort }} - containerPort: {{ .Values.s3.metricsPort }} name: metrics diff --git a/k8s/charts/seaweedfs/templates/shared/_helpers.tpl b/k8s/charts/seaweedfs/templates/shared/_helpers.tpl index 899f613bc4..81849e6962 100644 --- a/k8s/charts/seaweedfs/templates/shared/_helpers.tpl +++ b/k8s/charts/seaweedfs/templates/shared/_helpers.tpl @@ -283,6 +283,55 @@ true {{- end -}} {{- end -}} +{{/* +The message queue broker beside an S3 gateway, for (list $ "s3"|"filer"|"allInOne"), +as JSON: "args" are its flags and "port" the port it serves, 0 when it cannot run. +Unflagged, weed starts it only with grpc.msg_broker TLS, which enableSecurity +writes, and on 17777; defaults are left out so images without these flags start. +allInOne.s3 inherits s3's mqBroker and mqBrokerPort when null. +*/}} +{{- define "seaweedfs.mqBroker" -}} +{{- $root := index . 0 -}} +{{- $component := index . 1 -}} +{{- $prefix := "" -}} +{{- $name := "s3.mqBroker" -}} +{{- $mode := $root.Values.s3.mqBroker -}} +{{- $port := $root.Values.s3.mqBrokerPort -}} +{{- if eq $component "filer" -}} +{{- $prefix = "s3." -}} +{{- $name = "filer.s3.mqBroker" -}} +{{- $mode = $root.Values.filer.s3.mqBroker -}} +{{- $port = $root.Values.filer.s3.mqBrokerPort -}} +{{- else if eq $component "allInOne" -}} +{{- if not (kindIs "invalid" $root.Values.allInOne.s3.mqBroker) -}} +{{- $name = "allInOne.s3.mqBroker" -}} +{{- $mode = $root.Values.allInOne.s3.mqBroker -}} +{{- end -}} +{{- if not (kindIs "invalid" $root.Values.allInOne.s3.mqBrokerPort) -}} +{{- $port = $root.Values.allInOne.s3.mqBrokerPort -}} +{{- end -}} +{{- end -}} +{{- $mode = ternary "auto" (toString $mode | lower) (kindIs "invalid" $mode) -}} +{{- if not (has $mode (list "auto" "true" "false")) -}} +{{- fail (printf "%s must be auto, true or false, not %q" $name $mode) -}} +{{- end -}} +{{- $port = int ($port | default 0) -}} +{{- $runs := false -}} +{{- if and (gt $port 0) (or (eq $mode "true") (and (eq $mode "auto") $root.Values.global.seaweedfs.enableSecurity)) -}} +{{- $runs = true -}} +{{- end -}} +{{- $args := list -}} +{{- if or (eq $mode "false") (eq $port 0) -}} +{{- $args = append $args (printf "-%smq.broker=false" $prefix) -}} +{{- else if eq $mode "true" -}} +{{- $args = append $args (printf "-%smq.broker=true" $prefix) -}} +{{- end -}} +{{- if and $runs (ne $port 17777) -}} +{{- $args = append $args (printf "-%smq.broker.port=%d" $prefix $port) -}} +{{- end -}} +{{- dict "args" (join " " $args) "port" (ternary $port 0 $runs) | toJson -}} +{{- end -}} + {{/* Return the proper volume image */}} {{- define "seaweedfs.volume.image" -}} {{- if .Values.volume.imageOverride -}} diff --git a/k8s/charts/seaweedfs/templates/shared/networkpolicy.yaml b/k8s/charts/seaweedfs/templates/shared/networkpolicy.yaml index 80f46ea55d..8d84e62507 100644 --- a/k8s/charts/seaweedfs/templates/shared/networkpolicy.yaml +++ b/k8s/charts/seaweedfs/templates/shared/networkpolicy.yaml @@ -26,6 +26,9 @@ {{- if and $https (gt (int $https) 0) }} {{- $ports = append $ports $https }} {{- end }} +{{- with (include "seaweedfs.mqBroker" (list $ "allInOne") | fromJson).port }} +{{- $ports = append $ports (int .) }} +{{- end }} {{- end }} {{- if .Values.allInOne.sftp.enabled }} {{- $ports = append $ports (.Values.allInOne.sftp.port | default .Values.sftp.port) }} @@ -62,6 +65,9 @@ {{- if and .Values.filer.s3.httpsPort (gt (int .Values.filer.s3.httpsPort) 0) }} {{- $ports = append $ports .Values.filer.s3.httpsPort }} {{- end }} +{{- with (include "seaweedfs.mqBroker" (list $ "filer") | fromJson).port }} +{{- $ports = append $ports (int .) }} +{{- end }} {{- end }} {{- $targets = append $targets (dict "component" "filer" "ports" $ports) }} {{- end }} @@ -76,6 +82,9 @@ {{- if .Values.s3.lancePort }} {{- $ports = append $ports .Values.s3.lancePort }} {{- end }} +{{- with (include "seaweedfs.mqBroker" (list $ "s3") | fromJson).port }} +{{- $ports = append $ports (int .) }} +{{- end }} {{- if .Values.s3.metricsPort }} {{- $ports = append $ports .Values.s3.metricsPort }} {{- end }} diff --git a/k8s/charts/seaweedfs/templates/shared/security-configmap.yaml b/k8s/charts/seaweedfs/templates/shared/security-configmap.yaml index 8dc58d693e..e6131c41a6 100644 --- a/k8s/charts/seaweedfs/templates/shared/security-configmap.yaml +++ b/k8s/charts/seaweedfs/templates/shared/security-configmap.yaml @@ -120,6 +120,14 @@ stringData: cert = "/usr/local/share/ca-certificates/client/tls.crt" key = "/usr/local/share/ca-certificates/client/tls.key" + {{- /* The broker beside each S3 gateway serves with this identity and + dials other brokers with it; weed starts that broker only when this + section has a cert and key. Same internal client cert as grpc.s3, which + every pod running a gateway already mounts. */}} + [grpc.msg_broker] + cert = "/usr/local/share/ca-certificates/client/tls.crt" + key = "/usr/local/share/ca-certificates/client/tls.key" + # use this for any place needs a grpc client # i.e., "weed backup|benchmark|filer.copy|filer.replicate|mount|s3|upload" [grpc.client] diff --git a/k8s/charts/seaweedfs/tests/mq_broker_test.go b/k8s/charts/seaweedfs/tests/mq_broker_test.go new file mode 100644 index 0000000000..fc52710cc7 --- /dev/null +++ b/k8s/charts/seaweedfs/tests/mq_broker_test.go @@ -0,0 +1,199 @@ +package tests + +import ( + "fmt" + "os/exec" + "strings" + "testing" +) + +const security = "global.seaweedfs.enableSecurity=true" + +type brokerGateway struct { + name string + settings []string + kind string + object string + prefix string + broker string + port string +} + +var brokerGateways = []brokerGateway{ + {"s3", []string{"s3.enabled=true"}, "Deployment", "vector-test-seaweedfs-s3", "", "s3.mqBroker", "s3.mqBrokerPort"}, + {"filer.s3", []string{"filer.s3.enabled=true"}, "StatefulSet", "vector-test-seaweedfs-filer", "s3.", "filer.s3.mqBroker", "filer.s3.mqBrokerPort"}, + {"allInOne", []string{"allInOne.enabled=true", "allInOne.s3.enabled=true"}, "Deployment", "vector-test-seaweedfs-all-in-one", "", "allInOne.s3.mqBroker", "allInOne.s3.mqBrokerPort"}, +} + +func firstContainer(workload map[string]interface{}) map[string]interface{} { + return workload["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"].([]interface{})[0].(map[string]interface{}) +} + +// brokerFlags returns the broker flags of the workload's first container. +func brokerFlags(workload map[string]interface{}) []string { + container := firstContainer(workload) + var words []string + for _, key := range []string{"command", "args"} { + if list, ok := container[key].([]interface{}); ok { + for _, word := range list { + words = append(words, strings.Fields(word.(string))...) + } + } + } + var flags []string + for _, word := range words { + if strings.Contains(word, "mq.broker") { + flags = append(flags, word) + } + } + return flags +} + +func namedPort(ports []interface{}, name, field string) int { + for _, port := range ports { + if p := port.(map[string]interface{}); p["name"] == name { + return p[field].(int) + } + } + return 0 +} + +func policyAllows(policy map[string]interface{}, want int) bool { + rules, _ := policy["spec"].(map[string]interface{})["ingress"].([]interface{}) + for _, rule := range rules { + ports, _ := rule.(map[string]interface{})["ports"].([]interface{}) + for _, port := range ports { + if port.(map[string]interface{})["port"] == want { + return true + } + } + } + return false +} + +// checkBroker asserts the flags, that the broker port is declared and +// admitted exactly when the broker runs, so a broker that runs with its port +// blocked fails, and that no Service ever carries it. +func checkBroker(t *testing.T, gateway brokerGateway, label string, settings []string, wantFlags []string, wantPort int) { + t.Helper() + _, documents := render(t, append(append([]string{"networkPolicy.enabled=true"}, gateway.settings...), settings...)...) + label = fmt.Sprintf("%s %s", gateway.name, label) + + workload := findDocument(documents, gateway.kind, gateway.object) + if workload == nil { + t.Fatalf("%s: no %s %s", label, gateway.kind, gateway.object) + } + if got := brokerFlags(workload); strings.Join(got, " ") != strings.Join(wantFlags, " ") { + t.Errorf("%s: broker flags %q, want %q", label, got, wantFlags) + } + + containerPorts, _ := firstContainer(workload)["ports"].([]interface{}) + if got := namedPort(containerPorts, "swfs-mq-broker", "containerPort"); got != wantPort { + t.Errorf("%s: containerPort %d, want %d", label, got, wantPort) + } + + // Clients find brokers through the master and dial the pod, so a Service + // port would only publish the broker through a LoadBalancer or NodePort. + for _, document := range documents { + if document["kind"] != "Service" { + continue + } + for _, port := range document["spec"].(map[string]interface{})["ports"].([]interface{}) { + p := port.(map[string]interface{}) + if p["name"] == "swfs-mq-broker" || p["port"] == 17777 || p["targetPort"] == 17777 || (wantPort != 0 && (p["port"] == wantPort || p["targetPort"] == wantPort)) { + t.Errorf("%s: Service %v carries the broker port: %v", label, document["metadata"].(map[string]interface{})["name"], p) + } + } + } + + policy := findDocument(documents, "NetworkPolicy", gateway.object) + if policy == nil { + t.Fatalf("%s: no NetworkPolicy %s", label, gateway.object) + } + if wantPort != 0 && !policyAllows(policy, wantPort) { + t.Errorf("%s: NetworkPolicy does not admit broker port %d", label, wantPort) + } + for _, port := range []int{17777, 18887} { + if port != wantPort && policyAllows(policy, port) { + t.Errorf("%s: NetworkPolicy admits %d with no broker on it", label, port) + } + } +} + +func TestMqBrokerModes(t *testing.T) { + for _, gateway := range brokerGateways { + on := "-" + gateway.prefix + "mq.broker=true" + off := "-" + gateway.prefix + "mq.broker=false" + port := "-" + gateway.prefix + "mq.broker.port=18887" + cases := []struct { + label string + settings []string + wantFlags []string + wantPort int + }{ + {"auto without security", nil, nil, 0}, + {"auto with security", []string{security}, nil, 17777}, + {"auto with security, custom port", []string{security, gateway.port + "=18887"}, []string{port}, 18887}, + {"auto without security, custom port", []string{gateway.port + "=18887"}, nil, 0}, + {"true", []string{gateway.broker + "=true"}, []string{on}, 17777}, + {"true, custom port", []string{gateway.broker + "=true", gateway.port + "=18887"}, []string{on, port}, 18887}, + {"false with security", []string{security, gateway.broker + "=false"}, []string{off}, 0}, + {"true, port 0", []string{gateway.broker + "=true", gateway.port + "=0"}, []string{off}, 0}, + } + for _, c := range cases { + checkBroker(t, gateway, c.label, c.settings, c.wantFlags, c.wantPort) + } + } +} + +func TestMqBrokerAllInOneInherits(t *testing.T) { + allInOne := brokerGateways[2] + cases := []struct { + label string + settings []string + wantFlags []string + wantPort int + }{ + {"null inherits s3.mqBroker=true", []string{"s3.mqBroker=true"}, []string{"-mq.broker=true"}, 17777}, + {"null inherits s3.mqBroker=false", []string{security, "s3.mqBroker=false"}, []string{"-mq.broker=false"}, 0}, + {"explicit auto overrides s3.mqBroker=false", []string{security, "s3.mqBroker=false", "allInOne.s3.mqBroker=auto"}, nil, 17777}, + {"null inherits s3.mqBrokerPort", []string{security, "s3.mqBrokerPort=18887"}, []string{"-mq.broker.port=18887"}, 18887}, + {"explicit port overrides s3.mqBrokerPort=0", []string{security, "s3.mqBrokerPort=0", "allInOne.s3.mqBrokerPort=18887"}, []string{"-mq.broker.port=18887"}, 18887}, + } + for _, c := range cases { + checkBroker(t, allInOne, c.label, c.settings, c.wantFlags, c.wantPort) + } +} + +func TestMqBrokerTLSOnlyWithSecurity(t *testing.T) { + for _, secured := range []bool{false, true} { + // filer JWT signing writes security.toml without enableSecurity + settings := []string{"s3.enabled=true", "global.seaweedfs.securityConfig.jwtSigning.filerWrite=true"} + if secured { + settings = append(settings, security) + } + _, documents := render(t, settings...) + secret := findDocument(documents, "Secret", "vector-test-seaweedfs-security-config") + if secret == nil { + t.Fatalf("security %v: no security config Secret", secured) + } + toml := secret["stringData"].(map[string]interface{})["security.toml"].(string) + section := "[grpc.msg_broker]\ncert = \"/usr/local/share/ca-certificates/client/tls.crt\"\nkey = \"/usr/local/share/ca-certificates/client/tls.key\"" + if got := strings.Contains(toml, section); got != secured { + t.Errorf("security %v: [grpc.msg_broker] with the client cert present = %v\n%s", secured, got, toml) + } + if !secured && strings.Contains(toml, "[grpc.msg_broker]") { + t.Errorf("[grpc.msg_broker] written without enableSecurity") + } + } +} + +func TestMqBrokerRejectsUnknownValue(t *testing.T) { + if _, err := exec.LookPath("helm"); err != nil { + t.Skip("helm is not installed") + } + output, err := exec.Command("helm", "template", "test", "..", "--set", "s3.enabled=true", "--set", "s3.mqBroker=yes").CombinedOutput() + if err == nil || !strings.Contains(string(output), `s3.mqBroker must be auto, true or false, not "yes"`) { + t.Fatalf("s3.mqBroker=yes accepted: %v %s", err, output) + } +} diff --git a/k8s/charts/seaweedfs/values.yaml b/k8s/charts/seaweedfs/values.yaml index 0166b428d7..1669704850 100644 --- a/k8s/charts/seaweedfs/values.yaml +++ b/k8s/charts/seaweedfs/values.yaml @@ -1096,6 +1096,9 @@ filer: port: 8333 # add additional https port httpsPort: 0 + # Message queue broker beside the gateway, see s3.mqBroker + mqBroker: auto + mqBrokerPort: 17777 # Suffix of the host name, {bucket}.{domainName} domainName: "" # Seconds the gateway keeps serving after SIGTERM while its health checks @@ -1186,6 +1189,13 @@ s3: # Lance Namespace port; weed serves 9101 by default, 0 disables it # (and the worker's Lance container) lancePort: 9101 + # Message queue broker beside the gateway: auto runs it only with + # global.seaweedfs.enableSecurity, which gives it mTLS; true runs it either + # way, without TLS when security is off; false never does. + mqBroker: auto + # Its gRPC port, 0 turns it off. Clients and other brokers find the broker + # through the master and dial the pod IP, so no Service carries this port. + mqBrokerPort: 17777 loggingOverrideLevel: null # enable user & permission to s3 (need to inject to all services) enableAuth: false @@ -2060,6 +2070,8 @@ allInOne: enabled: false # Whether to enable S3 gateway port: null # S3 gateway port (null inherits from s3.port) httpsPort: null # S3 gateway HTTPS port (null inherits from s3.httpsPort) + mqBroker: null # Message queue broker: auto, true or false (null inherits from s3.mqBroker) + mqBrokerPort: null # Message queue broker gRPC port (null inherits from s3.mqBrokerPort, 0 turns the broker off) domainName: null # Suffix of the host name (null inherits from s3.domainName) enableAuth: false # Enable user & permission to S3 # Set to the name of an existing kubernetes Secret with the s3 json config file