Skip to content

Commit

Permalink
Merge pull request #361 from lsst-sqre/tickets/DM-48214/disable-kafka…
Browse files Browse the repository at this point in the history
…-x509-strict-verification

DM-48214: Unset `ssl.VERIFY_X509_STRICT` flag on Kafka SSl contexts
  • Loading branch information
fajpunk authored Dec 18, 2024
2 parents 20e62a9 + 80038a8 commit 97ce7c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bug fixes

- Unset the `ssl.VERIFY_X509_STRICT` flag in SSL contexts used for Kafka connections. Python 3.13 enables this flag by default, and the current certs that Strimzi generates for these Kafka endpoints are missing an Authority Key Identifier, which prevents connections when the flag is set.
22 changes: 20 additions & 2 deletions safir/src/safir/kafka/_kafka_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ class SslSettings(BaseModel):

def to_ssl_context(self) -> ssl.SSLContext:
"""Create an SSL context for connecting to Kafka."""
return helpers.create_ssl_context(
context = helpers.create_ssl_context(
cafile=str(self.cluster_ca_path),
certfile=str(self.client_cert_path),
keyfile=str(self.client_key_path),
)

# Starting with Python 3.13, the VERIFY_X509_STRICT flag is set in
# contexts created with ssl.create_default_context(), which this
# context is. The certs generated by Strimzi on the Kafka bootstrap
# endpoints do not have an Authority Key Identifier set, which makes
# connections fail. Until these get fixed and regenerated in Strimzi,
# unset VERIFY_X590_STRICT.
context.verify_flags &= ~ssl.VERIFY_X509_STRICT
return context

def to_faststream_params(self) -> FastStreamBrokerParams:
return {
"bootstrap_servers": self.bootstrap_servers,
Expand Down Expand Up @@ -189,10 +198,19 @@ def to_ssl_context(self) -> ssl.SSLContext:

if self.cluster_ca_path:
cafile = str(self.cluster_ca_path)
return helpers.create_ssl_context(
context = helpers.create_ssl_context(
cafile=cafile,
)

# Starting with Python 3.13, the VERIFY_X509_STRICT flag is set in
# contexts created with ssl.create_default_context(), which this
# context is. The certs generated by Strimzi on the Kafka bootstrap
# endpoints do not have an Authority Key Identifier set, which makes
# connections fail. Until these get fixed and regenerated in Strimzi,
# unset VERIFY_X590_STRICT.
context.verify_flags &= ~ssl.VERIFY_X509_STRICT
return context

def to_faststream_params(self) -> FastStreamBrokerParams:
cls: type[SASLScram512 | SASLScram256 | SASLPlaintext]
match self.sasl_mechanism:
Expand Down

0 comments on commit 97ce7c7

Please sign in to comment.