[WIP] Add TLS/SASL authentication support for Kafka functions - #3975
[WIP] Add TLS/SASL authentication support for Kafka functions#3975aliok wants to merge 2 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: aliok The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
@aliok: The following test failed, say
DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
There was a problem hiding this comment.
Pull request overview
This PR extends the function-level Kafka configuration to support TLS and SASL authentication, and propagates the resulting settings into the various deploy/run paths (Kubernetes/Knative deployers and local runners).
Changes:
- Extend
run.kafkaschema withsecurityProtocol,tls, andsasl(including validation). - Emit additional
KAFKA_SECURITY_PROTOCOL,KAFKA_TLS_*, andKAFKA_SASL_*environment variables during deployment/run. - Support
{{ secret:name:key }}-style value references for Kafka SASL user/password in the k8s deployer env var generation.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/knative/deployer.go | Updates Knative deploy path to use the new error-returning Kafka env injection (and track referenced resources). |
| pkg/k8s/deployer.go | Extends Kafka env var generation to include TLS/SASL fields and secret/configMap key refs for SASL values. |
| pkg/k8s/deployer_test.go | Adapts existing tests to new signature and adds coverage for TLS/SASL and secret-ref cases. |
| pkg/functions/runner.go | Propagates Kafka TLS/SASL env vars for the host runner (func run). |
| pkg/functions/function.go | Adds new Kafka config types/fields and validation rules for protocol/TLS/SASL combinations. |
| pkg/functions/function_test.go | Adds validation test cases for the new Kafka TLS/SASL config combinations. |
| pkg/docker/runner.go | Propagates Kafka TLS/SASL env vars for the Docker runner. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if kafka.SASL != nil { | ||
| if kafka.SecurityProtocol != "SASL_PLAINTEXT" && kafka.SecurityProtocol != "SASL_SSL" { | ||
| errors = append(errors, "run.kafka.sasl requires securityProtocol SASL_PLAINTEXT or SASL_SSL") | ||
| } | ||
| validMechanisms := map[string]bool{"": true, "PLAIN": true, "SCRAM-SHA-256": true, "SCRAM-SHA-512": true} | ||
| if !validMechanisms[kafka.SASL.Mechanism] { | ||
| errors = append(errors, "run.kafka.sasl.mechanism must be one of: PLAIN, SCRAM-SHA-256, SCRAM-SHA-512") | ||
| } | ||
| } |
| func appendKafkaEnvValue(envVars []corev1.EnvVar, name, value string, referencedSecrets, referencedConfigMaps *sets.Set[string]) ([]corev1.EnvVar, error) { | ||
| if strings.HasPrefix(value, "{{") { | ||
| slices := strings.Split(strings.Trim(value, "{} "), ":") | ||
| if len(slices) == 3 { | ||
| valueFrom, err := createEnvVarSource(slices, referencedSecrets, referencedConfigMaps) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return append(envVars, corev1.EnvVar{Name: name, ValueFrom: valueFrom}), nil | ||
| } | ||
| return nil, fmt.Errorf("invalid reference format %q, expected {{ secret:name:key }} or {{ configMap:name:key }}", value) | ||
| } | ||
| return append(envVars, corev1.EnvVar{Name: name, Value: value}), nil | ||
| } |
| type KafkaSASL struct { | ||
| Mechanism string `yaml:"mechanism,omitempty" jsonschema:"description=SASL mechanism: PLAIN SCRAM-SHA-256 or SCRAM-SHA-512,enum=PLAIN,enum=SCRAM-SHA-256,enum=SCRAM-SHA-512"` | ||
| User string `yaml:"user,omitempty" jsonschema:"description=SASL username. Supports {{ secret:name:key }} syntax"` | ||
| Password string `yaml:"password,omitempty" jsonschema:"description=SASL password. Supports {{ secret:name:key }} syntax"` | ||
| } |
|
PR needs rebase. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Summary
KafkaConfigwithsecurityProtocol,tls, andsaslfields in func.yamlKAFKA_SECURITY_PROTOCOL,KAFKA_TLS_*,KAFKA_SASL_*env vars{{ secret:name:key }}syntax supported forsasl.userandsasl.passwordDepends on knative-extensions/func-go#186
func.yaml example (SASL_SSL)
Verification instructions (Kind + Strimzi)
Prerequisites
1. Build the func CLI
Both repos have un-merged branches. Build the CLI from the
kafka-tls-saslbranch:2. Patch the scaffolding to use the func-go fork
The func-go dependency lives in the scaffolding's
go.mod(embedded in the CLI), not the function'sgo.mod. Add areplacedirective, re-tidy, regenerate the embedded filesystem, and rebuild:3. Create a Kind cluster with Knative
4. Install Strimzi with a TLS+SASL listener
5. Create a KafkaUser and topic
6. Create the function
7. Configure func.yaml
Copy secrets to the function namespace:
Edit
func.yaml:8. Deploy and verify
FUNC_REGISTRY=ttl.sh/my-kafka-tls-test /tmp/func-local deploy --build --verbose kubectl wait pods -l serving.knative.dev/service=my-kafka-tls-func \ --for=condition=Ready --timeout=120sCheck env vars on the pod:
9. Tail logs and send a test message
Expected log output:
Cleanup
kubectl delete ksvc my-kafka-tls-func kubectl delete secret my-cluster-cluster-ca-cert my-kafka-user -n default kubectl delete kafkauser my-kafka-user -n kafka kubectl delete kafkatopic test-topic -n kafka kubectl delete kafka my-cluster -n kafka kubectl delete -f 'https://strimzi.io/install/latest?namespace=kafka' -n kafka kubectl delete namespace kafka kind delete cluster --name kafka-tls-test rm -rf /tmp/my-kafka-tls-func /tmp/func-local