From 365edd5870f408e09e63a1ad12610bc7b0e706dc Mon Sep 17 00:00:00 2001 From: Jason Parraga Date: Sun, 6 Oct 2024 20:59:22 -0700 Subject: [PATCH] Add basic SASL and TLS support for Kafka cloud events Signed-off-by: Jason Parraga --- charts/flyte-core/README.md | 14 ++++ .../flyte-core/templates/admin/configmap.yaml | 2 +- .../templates/admin/deployment.yaml | 4 ++ charts/flyte-core/values.yaml | 30 ++++++++ .../manifests/complete-agent.yaml | 4 +- .../sandbox-bundled/manifests/complete.yaml | 4 +- docker/sandbox-bundled/manifests/dev.yaml | 4 +- flyteadmin/.golangci.yml | 2 + flyteadmin/pkg/async/cloudevent/factory.go | 7 +- .../interfaces/application_configuration.go | 68 +++++++++++++++++++ 10 files changed, 126 insertions(+), 13 deletions(-) diff --git a/charts/flyte-core/README.md b/charts/flyte-core/README.md index 3fa8d2dc2ad..316ee7a36a8 100644 --- a/charts/flyte-core/README.md +++ b/charts/flyte-core/README.md @@ -59,6 +59,20 @@ helm install gateway bitnami/contour -n flyte | cloud_events.enable | bool | `false` | | | cloud_events.eventsPublisher.eventTypes[0] | string | `"all"` | | | cloud_events.eventsPublisher.topicName | string | `"arn:aws:sns:us-east-2:123456:123-my-topic"` | | +| cloud_events.kafka | object | `{"brokers":["mybroker:443"],"saslConfig":{"enabled":false,"handshake":true,"mechanism":"PLAIN","password":"","user":"kafka"},"tlsConfig":{"certPath":"/etc/ssl/certs/kafka-client.crt","enabled":false,"keyPath":"/etc/ssl/certs/kafka-client.key"},"version":"3.7.0"}` | Configuration for sending cloud events to Kafka | +| cloud_events.kafka.brokers | list | `["mybroker:443"]` | The kafka brokers to talk to | +| cloud_events.kafka.saslConfig | object | `{"enabled":false,"handshake":true,"mechanism":"PLAIN","password":"","user":"kafka"}` | SASL based authentication | +| cloud_events.kafka.saslConfig.enabled | bool | `false` | Whether to use SASL authentication | +| cloud_events.kafka.saslConfig.handshake | bool | `true` | Whether the send the SASL handsahke first | +| cloud_events.kafka.saslConfig.mechanism | string | `"PLAIN"` | Which SASL mechanism to use. Defaults to PLAIN | +| cloud_events.kafka.saslConfig.password | string | `""` | The password for the kafka user | +| cloud_events.kafka.saslConfig.user | string | `"kafka"` | The kafka user | +| cloud_events.kafka.tlsConfig | object | `{"certPath":"/etc/ssl/certs/kafka-client.crt","enabled":false,"keyPath":"/etc/ssl/certs/kafka-client.key"}` | Certificate based authentication | +| cloud_events.kafka.tlsConfig.certPath | string | `"/etc/ssl/certs/kafka-client.crt"` | Path to the client certificate | +| cloud_events.kafka.tlsConfig.enabled | bool | `false` | Whether to use certificate based authentication or TLS | +| cloud_events.kafka.tlsConfig.keyPath | string | `"/etc/ssl/certs/kafka-client.key"` | Path to the client private key | +| cloud_events.kafka.version | string | `"3.7.0"` | The version of Kafka | +| cloud_events.secretName | string | `""` | The name of the secret to use to alternatively load in cloud events configuration via a secret. Useful when the configuration contains secrets. | | cloud_events.type | string | `"aws"` | | | cluster_resource_manager | object | `{"config":{"cluster_resources":{"customData":[{"production":[{"projectQuotaCpu":{"value":"5"}},{"projectQuotaMemory":{"value":"4000Mi"}}]},{"staging":[{"projectQuotaCpu":{"value":"2"}},{"projectQuotaMemory":{"value":"3000Mi"}}]},{"development":[{"projectQuotaCpu":{"value":"4"}},{"projectQuotaMemory":{"value":"3000Mi"}}]}],"refreshInterval":"5m","standaloneDeployment":false,"templatePath":"/etc/flyte/clusterresource/templates"}},"enabled":true,"nodeSelector":{},"podAnnotations":{},"podEnv":{},"podLabels":{},"prometheus":{"enabled":false,"path":"/metrics","port":10254},"resources":{},"service_account_name":"flyteadmin","standaloneDeployment":false,"templates":[{"key":"aa_namespace","value":"apiVersion: v1\nkind: Namespace\nmetadata:\n name: {{ namespace }}\nspec:\n finalizers:\n - kubernetes\n"},{"key":"ab_project_resource_quota","value":"apiVersion: v1\nkind: ResourceQuota\nmetadata:\n name: project-quota\n namespace: {{ namespace }}\nspec:\n hard:\n limits.cpu: {{ projectQuotaCpu }}\n limits.memory: {{ projectQuotaMemory }}\n"}]}` | Configuration for the Cluster resource manager component. This is an optional component, that enables automatic cluster configuration. This is useful to set default quotas, manage namespaces etc that map to a project/domain | | cluster_resource_manager.config | object | `{"cluster_resources":{"customData":[{"production":[{"projectQuotaCpu":{"value":"5"}},{"projectQuotaMemory":{"value":"4000Mi"}}]},{"staging":[{"projectQuotaCpu":{"value":"2"}},{"projectQuotaMemory":{"value":"3000Mi"}}]},{"development":[{"projectQuotaCpu":{"value":"4"}},{"projectQuotaMemory":{"value":"3000Mi"}}]}],"refreshInterval":"5m","standaloneDeployment":false,"templatePath":"/etc/flyte/clusterresource/templates"}}` | Configmap for ClusterResource parameters | diff --git a/charts/flyte-core/templates/admin/configmap.yaml b/charts/flyte-core/templates/admin/configmap.yaml index 04e5cac6b38..9d1f9a40c82 100644 --- a/charts/flyte-core/templates/admin/configmap.yaml +++ b/charts/flyte-core/templates/admin/configmap.yaml @@ -79,7 +79,7 @@ data: externalEvents: {{ tpl (toYaml .) $ | nindent 6 }} {{- end }} {{- end }} -{{- if .Values.cloud_events.enable }} +{{- if and .Values.cloud_events.enable (not .Values.cloud_events.secretName) }} {{- with .Values.cloud_events }} cloud_events.yaml: | cloudEvents: {{ tpl (toYaml .) $ | nindent 6 }} diff --git a/charts/flyte-core/templates/admin/deployment.yaml b/charts/flyte-core/templates/admin/deployment.yaml index 23ea9966dfc..d185e490538 100755 --- a/charts/flyte-core/templates/admin/deployment.yaml +++ b/charts/flyte-core/templates/admin/deployment.yaml @@ -196,6 +196,10 @@ spec: name: flyte-admin-base-config - configMap: name: flyte-admin-clusters-config + {{- if .Values.cloud_events.secretName }} + - secret: + name: {{ .Values.cloud_events.secretName }} + {{- end }} name: clusters-config-volume {{- if .Values.cluster_resource_manager.enabled }} - configMap: diff --git a/charts/flyte-core/values.yaml b/charts/flyte-core/values.yaml index 33ef5746908..c5e5713668e 100755 --- a/charts/flyte-core/values.yaml +++ b/charts/flyte-core/values.yaml @@ -943,6 +943,9 @@ external_events: # Cloud events are used to send events (unprocessed, as Admin see them) in cloud event format to # an SNS topic (or gcp equivalent) cloud_events: + # -- The name of the secret to use to alternatively load in cloud events configuration via a secret. Useful when the + # configuration contains secrets. + secretName: "" enable: false type: aws aws: @@ -953,6 +956,33 @@ cloud_events: topicName: "arn:aws:sns:us-east-2:123456:123-my-topic" eventTypes: - all # Or workflow, node, task. Or "*" + # -- Configuration for sending cloud events to Kafka + kafka: + # -- The version of Kafka + version: "3.7.0" + # -- The kafka brokers to talk to + brokers: + - mybroker:443 + # -- SASL based authentication + saslConfig: + # -- Whether to use SASL authentication + enabled: false + # -- The kafka user + user: kafka + # -- The password for the kafka user + password: "" + # -- Whether the send the SASL handsahke first + handshake: true + # -- Which SASL mechanism to use. Defaults to PLAIN + mechanism: PLAIN + # -- Certificate based authentication + tlsConfig: + # -- Whether to use certificate based authentication or TLS + enabled: false + # -- Path to the client certificate + certPath: /etc/ssl/certs/kafka-client.crt + # -- Path to the client private key + keyPath: /etc/ssl/certs/kafka-client.key # -- Configuration for the Cluster resource manager component. This is an optional component, that enables automatic # cluster configuration. This is useful to set default quotas, manage namespaces etc that map to a project/domain diff --git a/docker/sandbox-bundled/manifests/complete-agent.yaml b/docker/sandbox-bundled/manifests/complete-agent.yaml index 4b0ef1cd982..cbc1e34a1ee 100644 --- a/docker/sandbox-bundled/manifests/complete-agent.yaml +++ b/docker/sandbox-bundled/manifests/complete-agent.yaml @@ -816,7 +816,7 @@ type: Opaque --- apiVersion: v1 data: - haSharedSecret: cmRzbzQ4N3RQaWhuMk00OA== + haSharedSecret: RFBFbkg4ZHQ4WEx3YlF5cQ== proxyPassword: "" proxyUsername: "" kind: Secret @@ -1413,7 +1413,7 @@ spec: metadata: annotations: checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81 - checksum/secret: 51528951e92c2bf712bbde990941593aae1fcf72144a1fe944c312ddad86e161 + checksum/secret: 298357119cf08686c6a9cdaef9c94114dd0f6500b379a247b434573ce51b0ffe labels: app: docker-registry release: flyte-sandbox diff --git a/docker/sandbox-bundled/manifests/complete.yaml b/docker/sandbox-bundled/manifests/complete.yaml index a44f3dad5dd..05ec0f0966f 100644 --- a/docker/sandbox-bundled/manifests/complete.yaml +++ b/docker/sandbox-bundled/manifests/complete.yaml @@ -798,7 +798,7 @@ type: Opaque --- apiVersion: v1 data: - haSharedSecret: T1I2Q2tTcmREVG15MldGUQ== + haSharedSecret: dWRjaWtia3YyRDgzSlN3RA== proxyPassword: "" proxyUsername: "" kind: Secret @@ -1362,7 +1362,7 @@ spec: metadata: annotations: checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81 - checksum/secret: d723e395edc0fd2f221b9088efffe0d1f4dfabdef9892065fdabe12233362cf5 + checksum/secret: 3720b7dbfd4a13228a83cec5a65ef7a90c6a4e8b315f02133fcb6c21c6ee4e7f labels: app: docker-registry release: flyte-sandbox diff --git a/docker/sandbox-bundled/manifests/dev.yaml b/docker/sandbox-bundled/manifests/dev.yaml index d87f3a1642c..54e15573f16 100644 --- a/docker/sandbox-bundled/manifests/dev.yaml +++ b/docker/sandbox-bundled/manifests/dev.yaml @@ -499,7 +499,7 @@ metadata: --- apiVersion: v1 data: - haSharedSecret: ZnltNHNiZ01NRFNkb1RlMA== + haSharedSecret: ZXRkMjhyTGV4M1FPbXJNTA== proxyPassword: "" proxyUsername: "" kind: Secret @@ -934,7 +934,7 @@ spec: metadata: annotations: checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81 - checksum/secret: eeab364c20a0e8ad5a1526ccd7ddbd1d5a442087e7267c4d761279102b81be21 + checksum/secret: 25478efd95bcb669b5320d1b479d1d794f60d8579748f9fea8a5b0fc2a80eb04 labels: app: docker-registry release: flyte-sandbox diff --git a/flyteadmin/.golangci.yml b/flyteadmin/.golangci.yml index 4dbb031812b..cd180b89d1c 100644 --- a/flyteadmin/.golangci.yml +++ b/flyteadmin/.golangci.yml @@ -39,3 +39,5 @@ issues: exclude-rules: - path: pkg/workflowengine/impl/prepare_execution.go text: "copies lock" + - path: pkg/runtime/interfaces/application_configuration.go + text: "G402: TLS InsecureSkipVerify may be true." diff --git a/flyteadmin/pkg/async/cloudevent/factory.go b/flyteadmin/pkg/async/cloudevent/factory.go index 65cd48de938..51c38ffea44 100644 --- a/flyteadmin/pkg/async/cloudevent/factory.go +++ b/flyteadmin/pkg/async/cloudevent/factory.go @@ -73,12 +73,7 @@ func NewCloudEventsPublisher(ctx context.Context, db repositoryInterfaces.Reposi case cloudEventImplementations.Kafka: saramaConfig := sarama.NewConfig() - var err error - saramaConfig.Version, err = sarama.ParseKafkaVersion(cloudEventsConfig.KafkaConfig.Version) - if err != nil { - logger.Fatalf(ctx, "failed to parse kafka version, %v", err) - panic(err) - } + cloudEventsConfig.KafkaConfig.UpdateSaramaConfig(ctx, saramaConfig) kafkaSender, err := kafka_sarama.NewSender(cloudEventsConfig.KafkaConfig.Brokers, saramaConfig, cloudEventsConfig.EventsPublisherConfig.TopicName) if err != nil { panic(err) diff --git a/flyteadmin/pkg/runtime/interfaces/application_configuration.go b/flyteadmin/pkg/runtime/interfaces/application_configuration.go index 15ed271412a..243b38b343d 100644 --- a/flyteadmin/pkg/runtime/interfaces/application_configuration.go +++ b/flyteadmin/pkg/runtime/interfaces/application_configuration.go @@ -1,6 +1,10 @@ package interfaces import ( + "context" + "crypto/tls" + + "github.com/Shopify/sarama" "github.com/golang/protobuf/ptypes/wrappers" "golang.org/x/time/rate" @@ -231,11 +235,75 @@ type GCPConfig struct { ProjectID string `json:"projectId"` } +// This section holds SASL config for Kafka +type SASLConfig struct { + // Whether to use SASL + Enabled bool `json:"enabled"` + // The username + User string `json:"user"` + // The password + Password string `json:"password"` + Handshake bool `json:"handshake"` + // Which SASL Mechanism to use. Defaults to PLAIN + Mechanism sarama.SASLMechanism `json:"mechanism"` +} + +// This section holds TLS config for Kafka clients +type TLSConfig struct { + // Whether to use TLS + Enabled bool `json:"enabled"` + // Whether to skip certificate verification + InsecureSkipVerify bool `json:"insecureSkipVerify"` + // The location of the client certificate + CertPath string `json:"certPath"` + // The location of the client private key + KeyPath string `json:"keyPath"` +} + +// This section holds configs for Kafka clients type KafkaConfig struct { // The version of Kafka, e.g. 2.1.0, 0.8.2.0 Version string `json:"version"` // kafka broker addresses Brokers []string `json:"brokers"` + // sasl config + SASLConfig SASLConfig `json:"saslConfig"` + // tls config + TLSConfig TLSConfig `json:"tlsConfig"` +} + +func (k KafkaConfig) UpdateSaramaConfig(ctx context.Context, s *sarama.Config) { + var err error + s.Version, err = sarama.ParseKafkaVersion(k.Version) + if err != nil { + panic(err) + } + + if k.SASLConfig.Enabled { + s.Net.SASL.Enable = true + s.Net.SASL.User = k.SASLConfig.User + s.Net.SASL.Password = k.SASLConfig.Password + s.Net.SASL.Handshake = k.SASLConfig.Handshake + + if k.SASLConfig.Mechanism == "" { + k.SASLConfig.Mechanism = sarama.SASLTypePlaintext + } + s.Net.SASL.Mechanism = k.SASLConfig.Mechanism + } + + if k.TLSConfig.Enabled { + s.Net.TLS.Enable = true + s.Net.TLS.Config = &tls.Config{ + InsecureSkipVerify: k.TLSConfig.InsecureSkipVerify, + } + if k.TLSConfig.KeyPath != "" && k.TLSConfig.CertPath != "" { + cert, err := tls.LoadX509KeyPair(k.TLSConfig.CertPath, k.TLSConfig.KeyPath) + if err != nil { + panic(err) + } + s.Net.TLS.Config.Certificates = []tls.Certificate{cert} + } + } } // This section holds configuration for the event scheduler used to schedule workflow executions.