Skip to content

Commit deae847

Browse files
authored
feat: kafka support custom sasl (#2259)
1 parent f73d9d5 commit deae847

22 files changed

+531
-155
lines changed

addons-cluster/kafka/templates/_helpers.tpl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,34 @@ kafka2-external-zk
9090
{{- end -}}
9191
{{- end -}}
9292

93-
{{- define "kafka-cluster.brokerCommonEnv" -}}
93+
{{- define "kafka-cluster.basicEnv" -}}
94+
- name: KB_CLUSTER_VERSION
95+
value: "{{ .Values.version }}"
96+
- name: KB_CLUSTER_WITH_ZK
97+
value: "{{- if hasPrefix "withZookeeper" .Values.mode }}true{{- else }}false{{- end }}"
98+
{{/*
99+
will deprecated:
100+
- KB_KAFKA_ENABLE_SASL
101+
- KB_KAFKA_ENABLE_SASL_SCRAM
102+
*/}}
94103
- name: KB_KAFKA_ENABLE_SASL
95104
value: "{{ .Values.saslEnable }}"
96105
- name: KB_KAFKA_ENABLE_SASL_SCRAM
97106
value: "{{ .Values.saslScramEnable }}"
107+
- name: KB_KAFKA_SASL_ENABLE
108+
value: "{{ .Values.sasl.enable }}"
109+
{{- if .Values.sasl.enable }}
110+
- name: KB_KAFKA_SASL_USE_KB_BUILTIN
111+
value: "{{ .Values.sasl.useKBBuildInSasl }}"
112+
- name: KB_KAFKA_SASL_MECHANISMS
113+
value: "{{ .Values.sasl.mechanisms | join "," }}"
114+
- name: KB_KAFKA_SASL_INTER_BROKER_PROTOCOL
115+
value: "{{ .Values.sasl.interBrokerProtocol }}"
116+
{{- end }}
117+
{{- end -}}
118+
119+
{{- define "kafka-cluster.brokerCommonEnv" -}}
120+
{{- include "kafka-cluster.basicEnv" . }}
98121
- name: KB_KAFKA_BROKER_HEAP
99122
value: "{{ .Values.brokerHeap }}"
100123
- name: KB_KAFKA_CONTROLLER_HEAP
@@ -143,3 +166,23 @@ volumeClaimTemplates:
143166
{{- end }}
144167
{{- end -}}
145168

169+
{{- define "kafka-broker-component" -}}
170+
{{- if eq "combined" .Values.mode -}}
171+
kafka-combine
172+
{{- else -}}
173+
kafka-broker
174+
{{- end -}}
175+
{{- end -}}
176+
177+
{{- define "kafka-broker-accounts-secret-name" -}}
178+
{{ include "kblib.clusterName" . }}-{{ include "kafka-broker-component" . }}-accounts
179+
{{- end -}}
180+
181+
{{- define "kafka-broker-volumes" -}}
182+
{{- if .Values.sasl.enable }}
183+
volumes:
184+
- name: accounts
185+
secret:
186+
secretName: {{ include "kafka-broker-accounts-secret-name" . }}
187+
{{- end }}
188+
{{- end -}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{- if and (.Values.sasl.enable) .Values.sasl.useKBBuildInSasl }}
2+
apiVersion: v1
3+
kind: Secret
4+
metadata:
5+
name: {{ include "kafka-broker-accounts-secret-name" . }}
6+
namespace: {{ .Release.Namespace }}
7+
labels:
8+
app.kubernetes.io/name: {{ include "kblib.clusterName" . }}
9+
app.kubernetes.io/instance: {{ .Release.Name }}
10+
app.kubernetes.io/component: {{ include "kafka-broker-component" . }}
11+
type: Opaque
12+
stringData:
13+
# accounts: |
14+
# [mechanism=PLAIN] format:
15+
# {accountName}:{md5Password}
16+
accounts: ""
17+
{{- end }}

addons-cluster/kafka/templates/cluster.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ spec:
1111
componentSpecs:
1212
{{- if eq "combined" .Values.mode }}
1313
- name: kafka-combine
14+
serviceVersion: {{ .Values.version }}
1415
tls: {{ .Values.tlsEnable }}
1516
{{- if .Values.tlsEnable }}
1617
issuer:
@@ -29,8 +30,10 @@ spec:
2930
env:
3031
{{- include "kafka-cluster.brokerCommonEnv" . | nindent 8 }}
3132
{{- include "kafka-cluster.brokerVCT" . | indent 6 }}
33+
{{- include "kafka-broker-volumes" . | indent 6 }}
3234
{{- else }}
3335
- name: kafka-broker
36+
serviceVersion: {{ .Values.version }}
3437
tls: {{ .Values.tlsEnable }}
3538
{{- if .Values.tlsEnable }}
3639
issuer:
@@ -49,6 +52,7 @@ spec:
4952
env:
5053
{{- include "kafka-cluster.brokerCommonEnv" . | nindent 8 }}
5154
{{- include "kafka-cluster.brokerVCT" . | indent 6 }}
55+
{{- include "kafka-broker-volumes" . | indent 6 }}
5256
{{- if eq "withZookeeper-10" $.Values.mode }}
5357
serviceRefs:
5458
- name: kafkaZookeeper
@@ -66,6 +70,7 @@ spec:
6670
{{- end }}
6771
{{- if eq "separated" $.Values.mode }}
6872
- name: kafka-controller
73+
serviceVersion: {{ .Values.version }}
6974
tls: {{ .Values.tlsEnable }}
7075
{{- if .Values.tlsEnable }}
7176
issuer:
@@ -78,9 +83,9 @@ spec:
7883
{{- end }}
7984
{{- if .Values.monitorEnable }}
8085
- name: kafka-exporter
86+
serviceVersion: {{ .Values.exporterVersion }}
8187
replicas: {{ .Values.monitor.replicas }}
8288
env:
83-
- name: KB_KAFKA_ENABLE_SASL_SCRAM
84-
value: "{{ .Values.saslScramEnable }}"
89+
{{- include "kafka-cluster.basicEnv" . | nindent 8 }}
8590
{{- include "kafka-exporter.resources" . | nindent 6 }}
8691
{{- end }}

addons-cluster/kafka/templates/validate.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ Validate kafka version and mode
1515
{{- end }}
1616
{{- end }}
1717
{{- end }}
18+
19+
{{- if or (eq .Values.mode "combined") (eq .Values.mode "separated") }}
20+
{{- if .Values.sasl.mechanisms }}
21+
{{- range $mech := .Values.sasl.mechanisms }}
22+
{{- if ne $mech "PLAIN" }}
23+
{{ fail (printf "When mode is 'combined' or 'separated', only 'PLAIN' is supported in sasl.mechanisms (got: %s)" $mech) }}
24+
{{- end }}
25+
{{- end }}
26+
{{- end }}
27+
{{- end }}

addons-cluster/kafka/values.schema.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,53 @@
198198
"title": "fixedPodIPEnabled",
199199
"description":"Whether to enable fixed Pod IP mode in Kafka's `advertised.listeners`"
200200
}
201+
,
202+
"sasl": {
203+
"title": "Sasl",
204+
"description": "Advanced SASL authentication options.",
205+
"type": "object",
206+
"properties": {
207+
"enable": {
208+
"title": "Enable",
209+
"description": "Enable SASL authentication.",
210+
"type": "boolean",
211+
"default": false
212+
},
213+
"useKBBuildInSasl": {
214+
"title": "UseKBBuildInSasl",
215+
"description": "Whether to use the built-in SASL server callback handler.",
216+
"type": "boolean",
217+
"default": true
218+
},
219+
"mechanisms": {
220+
"title": "Mechanisms",
221+
"description": "List of SASL mechanisms to enable. When useKBBuildInSasl is true, only [PLAIN] is supported currently.",
222+
"type": "array",
223+
"items": {
224+
"type": "string",
225+
"enum": [
226+
"PLAIN",
227+
"SCRAM-SHA-256",
228+
"SCRAM-SHA-512"
229+
]
230+
},
231+
"default": [
232+
"PLAIN"
233+
],
234+
"minItems": 1
235+
},
236+
"interBrokerProtocol": {
237+
"title": "Inter Broker Protocol",
238+
"description": "SASL mechanism used for inter-broker communication. Should be one of sasl.mechanisms.",
239+
"type": "string",
240+
"enum": [
241+
"PLAIN",
242+
"SCRAM-SHA-256",
243+
"SCRAM-SHA-512"
244+
],
245+
"default": "PLAIN"
246+
}
247+
}
248+
}
201249
}
202250
}

addons-cluster/kafka/values.yaml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## @param version Kafka cluster version
22
##
3-
version: 3.3.2
3+
version: 3.9.0
4+
# @param version Kafka exporter component version
5+
##
6+
exporterVersion: 1.6.0
47

58
## @param mode for Kafka cluster mode, 'combined' is combined Kafka controller (KRaft) and broker,
69
## 'separated' is a Kafka KRaft and Kafka broker cluster.
@@ -11,12 +14,46 @@ mode: combined
1114
# Todo: Monitoring is not supported when tls is enabled
1215
tlsEnable: false
1316

17+
18+
# Deprecated: use sasl.enable + sasl.mechanisms instead
19+
# equals with:
20+
# sasl:
21+
# enable: true
22+
# useKBBuildInSasl: false
23+
# mechanism:
24+
# - PLAIN
25+
# interBrokerProtocol: PLAIN
1426
# Enable SASL plain auth
1527
saslEnable: false
1628

29+
# Will deprecate when 'sasl.mechanisms' support SCRAM-SHA-256, SCRAM-SHA-512
30+
# equals with:
31+
# sasl:
32+
# enable: true
33+
# useKBBuildInSasl: false
34+
# mechanism:
35+
# - SCRAM-SHA-256
36+
# - SCRAM-SHA-512
37+
# interBrokerProtocol: SCRAM-SHA-512
1738
# Enable SASL Scram auth
1839
saslScramEnable: false
1940

41+
# SASL settings
42+
sasl:
43+
enable: false
44+
# if true, the built-in SASL server callback handler in the knowledge base is used, and users are managed by modifying the key.
45+
# if false, use kafka native SASL callback handler
46+
# server.properties:
47+
# useKBBuildInSasl=true -> sasl.server.callback.handler.class=${KBClassName}
48+
# useKBBuildInSasl=false -> sasl.server.callback.handler.class=''
49+
useKBBuildInSasl: true
50+
# [PLAIN,SCRAM-SHA-256,SCRAM-SHA-512]
51+
# when useKBBuildInSasl=true, only supported [PLAIN] yet. SCRAM-SHA-256, SCRAM-SHA-512 will be comming soon
52+
mechanisms:
53+
- PLAIN
54+
# should be one of sasl.mechanisms setting
55+
interBrokerProtocol: PLAIN
56+
2057
# Enable Monitor
2158
monitorEnable: true
2259

addons/kafka/configs/2.7/kafka-27-server.prop.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ offsets.retention.check.interval.ms=600000
110110
offsets.retention.minutes=10080
111111
offsets.topic.compression.codec=0
112112
offsets.topic.num.partitions=50
113-
offsets.topic.replication.factor=3
113+
offsets.topic.replication.factor=1
114114
offsets.topic.segment.bytes=104857600
115115
password.encoder.cipher.algorithm=AES/CBC/PKCS5Padding
116116
password.encoder.iterations=4096
@@ -190,9 +190,9 @@ transaction.abort.timed.out.transaction.cleanup.interval.ms=10000
190190
transaction.max.timeout.ms=900000
191191
transaction.remove.expired.transaction.cleanup.interval.ms=3600000
192192
transaction.state.log.load.buffer.size=5242880
193-
transaction.state.log.min.isr=2
193+
transaction.state.log.min.isr=1
194194
transaction.state.log.num.partitions=50
195-
transaction.state.log.replication.factor=3
195+
transaction.state.log.replication.factor=1
196196
transaction.state.log.segment.bytes=104857600
197197
transactional.id.expiration.ms=604800000
198198
unclean.leader.election.enable=false

addons/kafka/scripts-ut-spec/kafka_server_setup_spec.sh

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ Describe "Kafka Server Setup Script Tests"
5757
unset CONTROLLER_POD_NAME_LIST
5858
unset KB_HOST_IP
5959
unset BROKER_MIN_NODE_ID
60-
unset KB_KAFKA_ENABLE_SASL
61-
unset KB_KAFKA_SASL_CONFIG_PATH
6260
unset KAFKA_KRAFT_CLUSTER_ID
6361
unset KB_KAFKA_BROKER_HEAP
6462
unset KB_KAFKA_CONTROLLER_HEAP
@@ -147,24 +145,6 @@ Describe "Kafka Server Setup Script Tests"
147145
End
148146
End
149147

150-
Describe "override_sasl_configuration()"
151-
It "sets SASL configuration when KB_KAFKA_ENABLE_SASL is true"
152-
KB_KAFKA_ENABLE_SASL="true"
153-
KB_KAFKA_SASL_CONFIG_PATH="$kafka_config_path/kafka_jaas.conf"
154-
touch "$KB_KAFKA_SASL_CONFIG_PATH"
155-
When run override_sasl_configuration
156-
The output should include "[sasl]KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,INTERNAL:SASL_PLAINTEXT,CLIENT:SASL_PLAINTEXT"
157-
The status should be success
158-
End
159-
160-
It "does not set SASL configuration when KB_KAFKA_ENABLE_SASL is false"
161-
KB_KAFKA_ENABLE_SASL="false"
162-
When run override_sasl_configuration
163-
The output should not include "KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP"
164-
The status should be success
165-
End
166-
End
167-
168148
Describe "generate_kraft_cluster_id()"
169149
It "sets KAFKA_KRAFT_CLUSTER_ID if provided"
170150
KAFKA_KRAFT_CLUSTER_ID="my-cluster-id"

0 commit comments

Comments
 (0)