Skip to content

Commit 099b178

Browse files
committed
Add support for using the system's default CA cert store
1 parent bfdc774 commit 099b178

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,21 @@ kafka = Kafka.new(
888888

889889
Without passing the CA certificate to the client it would be impossible to protect against [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack).
890890

891+
##### Using your system's CA cert store
892+
893+
If you want to use the CA certs from your system's default certificate store, you
894+
can use:
895+
896+
```ruby
897+
kafka = Kafka.new(
898+
use_ssl_ca_default_store: true
899+
# ...
900+
)
901+
```
902+
903+
This configures the store to look up CA certificates from the system default certificate store on an as needed basis. The location of the store can usually be determined by:
904+
`OpenSSL::X509::DEFAULT_CERT_FILE`
905+
891906
##### Client Authentication
892907

893908
In order to authenticate the client to the cluster, you need to pass in a certificate and key created for the client and trusted by the brokers.

lib/kafka/client.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def initialize(seed_brokers:, client_id: "ruby-kafka", logger: nil, connect_time
6060
ssl_ca_cert_file_path: nil, ssl_ca_cert: nil, ssl_client_cert: nil, ssl_client_cert_key: nil,
6161
sasl_gssapi_principal: nil, sasl_gssapi_keytab: nil,
6262
sasl_plain_authzid: '', sasl_plain_username: nil, sasl_plain_password: nil,
63-
sasl_scram_username: nil, sasl_scram_password: nil, sasl_scram_mechanism: nil)
63+
sasl_scram_username: nil, sasl_scram_password: nil, sasl_scram_mechanism: nil, use_ssl_ca_default_store: false)
6464
@logger = logger || Logger.new(nil)
6565
@instrumenter = Instrumenter.new(client_id: client_id)
6666
@seed_brokers = normalize_seed_brokers(seed_brokers)
6767

68-
ssl_context = build_ssl_context(ssl_ca_cert_file_path, ssl_ca_cert, ssl_client_cert, ssl_client_cert_key)
68+
ssl_context = build_ssl_context(ssl_ca_cert_file_path, ssl_ca_cert, ssl_client_cert, ssl_client_cert_key, use_ssl_ca_default_store)
6969

7070
sasl_authenticator = SaslAuthenticator.new(
7171
sasl_gssapi_principal: sasl_gssapi_principal,
@@ -542,8 +542,8 @@ def initialize_cluster
542542
)
543543
end
544544

545-
def build_ssl_context(ca_cert_file_path, ca_cert, client_cert, client_cert_key)
546-
return nil unless ca_cert_file_path || ca_cert || client_cert || client_cert_key
545+
def build_ssl_context(ca_cert_file_path, ca_cert, client_cert, client_cert_key, use_ssl_ca_default_store)
546+
return nil unless ca_cert_file_path || ca_cert || client_cert || client_cert_key || use_ssl_ca_default_store
547547

548548
ssl_context = OpenSSL::SSL::SSLContext.new
549549

@@ -558,14 +558,17 @@ def build_ssl_context(ca_cert_file_path, ca_cert, client_cert, client_cert_key)
558558
raise ArgumentError, "Kafka client initialized with `ssl_client_cert_key`, but no `ssl_client_cert`. Please provide both."
559559
end
560560

561-
if ca_cert || ca_cert_file_path
561+
if ca_cert || ca_cert_file_path || use_ssl_ca_default_store
562562
store = OpenSSL::X509::Store.new
563563
Array(ca_cert).each do |cert|
564564
store.add_cert(OpenSSL::X509::Certificate.new(cert))
565565
end
566566
if ca_cert_file_path
567567
store.add_file(ca_cert_file_path)
568568
end
569+
if use_ssl_ca_default_store
570+
store.set_default_paths
571+
end
569572
ssl_context.cert_store = store
570573
end
571574

0 commit comments

Comments
 (0)