Skip to main content

confluent_kafka/common/config/
ssl_configs.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! SSL/TLS configuration for Kafka connections.
16//!
17//! Translated from `org.apache.kafka.common.config.SslConfigs`.
18//!
19//! In Java this is a class of static string constants used as config keys with
20//! `AbstractConfig`. In Rust we define a typed struct holding the actual config
21//! values the client needs, plus the config key constants for compatibility.
22//!
23//! Java-specific settings that rustls does not use are defined, parsed and
24//! validated as in Java (`SslConfigs.addClientSslSupport`), but are not
25//! honoured:
26//! - `ssl.provider` — Java security provider
27//! - `ssl.secure.random.implementation` — Java SecureRandom PRNG
28//! - `ssl.keymanager.algorithm` — Java JSSE key manager
29//! - `ssl.trustmanager.algorithm` — Java JSSE trust manager
30//!
31//! `ssl.protocol` and `ssl.cipher.suites` are honoured by the
32//! [`SslFactory`](crate::common::security::ssl::SslFactory) as JSSE honours
33//! them: `ssl.protocol` selects Java's `SSLContext.getInstance(protocol)`,
34//! whose default protocols and default cipher suites apply when
35//! `ssl.enabled.protocols` or `ssl.cipher.suites` is empty (so `TLSv1.2` keeps
36//! TLS 1.3 out only with the default suites), and `ssl.cipher.suites`
37//! restricts and orders the TLS provider's cipher suites.
38//!
39//! Four of Java's keys are not defined:
40//! - `ssl.engine.factory.class`, the Java SSLEngine factory: a configuration
41//!   never takes a class (CLAUDE.md §2).
42//! - `ssl.keystore.password` and `ssl.truststore.password`: PEM is the only
43//!   store format here, and Java rejects a store password with PEM stores
44//!   (`org.apache.kafka.common.security.ssl.DefaultSslEngineFactory#createKeystore`, `org.apache.kafka.common.security.ssl.DefaultSslEngineFactory#createTruststore`), so the keys stay
45//!   undefined until another store format is implemented.
46//! - `ssl.key.password`: encrypted PEM private keys are not decrypted (Java
47//!   decrypts them with it, `org.apache.kafka.common.security.ssl.DefaultSslEngineFactory.PemStore#privateKey`), so the
48//!   key stays undefined until that is implemented.
49//!
50//! A value set for an undefined key is reported as unused.
51
52use crate::common::Error;
53use crate::common::config::abstract_config::ValuesReader;
54use crate::common::config::config_def::{ConfigDef, DefaultValue, Importance, Type, ValidList};
55use crate::common::config::types::Password;
56
57// ---------------------------------------------------------------------------
58// Config key constants (matching Java SslConfigs constant values)
59// ---------------------------------------------------------------------------
60
61impl SslConfigs {
62    /// Config key: `ssl.protocol`.
63    pub const SSL_PROTOCOL_CONFIG: &str = "ssl.protocol";
64
65    /// Default SSL protocol.
66    pub const DEFAULT_SSL_PROTOCOL: &str = "TLSv1.3";
67
68    /// Config key: `ssl.provider`.
69    pub const SSL_PROVIDER_CONFIG: &str = "ssl.provider";
70
71    /// Config key: `ssl.cipher.suites`.
72    pub const SSL_CIPHER_SUITES_CONFIG: &str = "ssl.cipher.suites";
73
74    /// Config key: `ssl.enabled.protocols`.
75    pub const SSL_ENABLED_PROTOCOLS_CONFIG: &str = "ssl.enabled.protocols";
76
77    /// Default enabled SSL protocols.
78    pub const DEFAULT_SSL_ENABLED_PROTOCOLS: &str = "TLSv1.2,TLSv1.3";
79
80    /// Config key: `ssl.keystore.type`.
81    pub const SSL_KEYSTORE_TYPE_CONFIG: &str = "ssl.keystore.type";
82
83    /// Default for `ssl.keystore.type`: `PEM`.
84    ///
85    /// This deviates from Java, whose `DEFAULT_SSL_KEYSTORE_TYPE` is `JKS`
86    /// (`org.apache.kafka.common.config.SslConfigs#DEFAULT_SSL_KEYSTORE_TYPE`): the rustls-based SSL factory reads only PEM and
87    /// rejects JKS and PKCS12, so Java's default would make every TLS client
88    /// fail to construct.
89    pub const DEFAULT_SSL_KEYSTORE_TYPE: &str = "PEM";
90
91    /// Config key: `ssl.keystore.key`.
92    pub const SSL_KEYSTORE_KEY_CONFIG: &str = "ssl.keystore.key";
93
94    /// Config key: `ssl.keystore.certificate.chain`.
95    pub const SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG: &str = "ssl.keystore.certificate.chain";
96
97    /// Config key: `ssl.truststore.certificates`.
98    pub const SSL_TRUSTSTORE_CERTIFICATES_CONFIG: &str = "ssl.truststore.certificates";
99
100    /// Config key: `ssl.keystore.location`.
101    pub const SSL_KEYSTORE_LOCATION_CONFIG: &str = "ssl.keystore.location";
102
103    /// Config key: `ssl.truststore.type`.
104    pub const SSL_TRUSTSTORE_TYPE_CONFIG: &str = "ssl.truststore.type";
105
106    /// Default for `ssl.truststore.type`: `PEM`, deviating from Java's `JKS`
107    /// (`org.apache.kafka.common.config.SslConfigs#DEFAULT_SSL_TRUSTSTORE_TYPE`) for the reason given at
108    /// [`DEFAULT_SSL_KEYSTORE_TYPE`](Self::DEFAULT_SSL_KEYSTORE_TYPE).
109    pub const DEFAULT_SSL_TRUSTSTORE_TYPE: &str = "PEM";
110
111    /// Config key: `ssl.truststore.location`.
112    pub const SSL_TRUSTSTORE_LOCATION_CONFIG: &str = "ssl.truststore.location";
113
114    /// Config key: `ssl.keymanager.algorithm`.
115    pub const SSL_KEYMANAGER_ALGORITHM_CONFIG: &str = "ssl.keymanager.algorithm";
116
117    /// Config key: `ssl.trustmanager.algorithm`.
118    pub const SSL_TRUSTMANAGER_ALGORITHM_CONFIG: &str = "ssl.trustmanager.algorithm";
119
120    /// Config key: `ssl.endpoint.identification.algorithm`.
121    pub const SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG: &str = "ssl.endpoint.identification.algorithm";
122
123    /// Default endpoint identification algorithm (enables hostname verification).
124    pub const DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM: &str = "https";
125
126    /// Config key: `ssl.secure.random.implementation`.
127    pub const SSL_SECURE_RANDOM_IMPLEMENTATION_CONFIG: &str = "ssl.secure.random.implementation";
128}
129
130// ---------------------------------------------------------------------------
131// SslConfigs value fields
132// ---------------------------------------------------------------------------
133
134/// SSL/TLS configuration for Kafka connections.
135///
136/// The Java class
137/// hosts only the `ssl.*` config keys and defaults; the values themselves
138/// travel in the `Map<String, ?>` handed to `SslFactory.configure`. The Rust
139/// struct keeps Java's name and constants, and additionally holds the
140/// client-relevant subset of those values, parsed once from the client config
141/// properties. The fields are crate-private: callers set them through the
142/// `ssl.*` properties of a client config, as a Java caller does.
143///
144/// `truststore_type` and `keystore_type` default to `"PEM"`
145/// ([`DEFAULT_SSL_KEYSTORE_TYPE`](Self::DEFAULT_SSL_KEYSTORE_TYPE)), where
146/// Java defaults to JKS.
147///
148/// Every field whose key Java defines as `ConfigDef.Type.PASSWORD` is a
149/// `Password`, so the derived `Debug` renders each of them as `[hidden]`.
150#[derive(Debug)]
151#[non_exhaustive]
152#[doc(alias = "org.apache.kafka.common.config.SslConfigs")]
153#[cfg_attr(
154    all(not(feature = "rustls"), not(test)),
155    expect(
156        dead_code,
157        reason = "the values are parsed and kept, but only the TLS transport reads them"
158    )
159)]
160pub struct SslConfigs {
161    /// Path to the trust store file (CA certificates).
162    /// Corresponds to `ssl.truststore.location`.
163    pub(crate) truststore_location: Option<String>,
164
165    /// Trusted certificates in PEM format (alternative to `truststore_location`).
166    /// Corresponds to `ssl.truststore.certificates`. The certificates are
167    /// public, but Java defines the key as `Type.PASSWORD`
168    /// (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`) and hides them, so this client does too.
169    pub(crate) truststore_certificates: Option<Password>,
170
171    /// Trust store format: `"JKS"`, `"PKCS12"`, or `"PEM"`.
172    /// Corresponds to `ssl.truststore.type`. Default: `"PEM"`.
173    pub(crate) truststore_type: String,
174
175    /// Path to the key store file (client certificate for mTLS).
176    /// Corresponds to `ssl.keystore.location`.
177    pub(crate) keystore_location: Option<String>,
178
179    /// Private key in PEM format (alternative to `keystore_location`).
180    /// Corresponds to `ssl.keystore.key`, which Java defines as
181    /// `Type.PASSWORD` (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`).
182    pub(crate) keystore_key: Option<Password>,
183
184    /// Certificate chain in PEM format (alternative to `keystore_location`).
185    /// Corresponds to `ssl.keystore.certificate.chain`. The certificates are
186    /// public, but Java defines the key as `Type.PASSWORD`
187    /// (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`) and hides them, so this client does too.
188    pub(crate) keystore_certificate_chain: Option<Password>,
189
190    /// Key store format: `"JKS"`, `"PKCS12"`, or `"PEM"`.
191    /// Corresponds to `ssl.keystore.type`. Default: `"PEM"`.
192    pub(crate) keystore_type: String,
193
194    /// Endpoint identification algorithm for hostname verification.
195    /// `"https"` enables hostname verification (default). Empty string disables it.
196    /// Corresponds to `ssl.endpoint.identification.algorithm`.
197    pub(crate) endpoint_identification_algorithm: String,
198
199    /// Enabled TLS protocol versions.
200    /// Corresponds to `ssl.enabled.protocols`. Default: `["TLSv1.2", "TLSv1.3"]`.
201    pub(crate) enabled_protocols: Vec<String>,
202
203    /// The protocol of Java's `SSLContext`, which gives the default protocols
204    /// and cipher suites. Corresponds to `ssl.protocol`. Default: `"TLSv1.3"`.
205    pub(crate) protocol: String,
206
207    /// The enabled cipher suites, in preference order; empty for the TLS
208    /// provider's defaults. Corresponds to `ssl.cipher.suites`. Default: empty.
209    pub(crate) cipher_suites: Vec<String>,
210}
211
212impl Default for SslConfigs {
213    /// Returns an `SslConfigs` with sensible defaults for Rust/rustls:
214    ///
215    /// - PEM format for both truststore and keystore
216    /// - TLSv1.2 and TLSv1.3 enabled, `TLSv1.3` as the protocol
217    /// - the TLS provider's default cipher suites
218    /// - `"https"` endpoint identification (hostname verification enabled)
219    /// - All optional locations and PEM values are `None`
220    fn default() -> Self {
221        SslConfigs {
222            truststore_location: None,
223            truststore_certificates: None,
224            truststore_type: SslConfigs::DEFAULT_SSL_TRUSTSTORE_TYPE.to_owned(),
225            keystore_location: None,
226            keystore_key: None,
227            keystore_certificate_chain: None,
228            keystore_type: SslConfigs::DEFAULT_SSL_KEYSTORE_TYPE.to_owned(),
229            endpoint_identification_algorithm: SslConfigs::DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM.to_owned(),
230            enabled_protocols: vec!["TLSv1.2".to_owned(), "TLSv1.3".to_owned()],
231            protocol: SslConfigs::DEFAULT_SSL_PROTOCOL.to_owned(),
232            cipher_suites: Vec::new(),
233        }
234    }
235}
236
237impl SslConfigs {
238    /// The `ssl.*` settings of a configuration parsed with
239    /// [`Self::add_client_ssl_support`]'s definitions, the keys Java's
240    /// `SslFactory` reads through `DefaultSslEngineFactory.configure`.
241    ///
242    /// # Errors
243    ///
244    /// [`Error::LocalIllegalState`] if a key's definition is missing or of
245    /// another type, a defect of the definition rather than of the input.
246    pub(crate) fn from_values(values: &mut ValuesReader<'_>) -> Result<SslConfigs, Error> {
247        Ok(SslConfigs {
248            truststore_location: values
249                .string_or_null(SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG)?
250                .map(str::to_string),
251            truststore_certificates: values
252                .password_or_null(SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG)?
253                .cloned(),
254            truststore_type: values.string(SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG)?.to_string(),
255            keystore_location: values
256                .string_or_null(SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG)?
257                .map(str::to_string),
258            keystore_key: values.password_or_null(SslConfigs::SSL_KEYSTORE_KEY_CONFIG)?.cloned(),
259            keystore_certificate_chain: values
260                .password_or_null(SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG)?
261                .cloned(),
262            keystore_type: values.string(SslConfigs::SSL_KEYSTORE_TYPE_CONFIG)?.to_string(),
263            endpoint_identification_algorithm: values
264                .string(SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG)?
265                .to_string(),
266            enabled_protocols: values.list(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG)?.to_vec(),
267            protocol: values.string(SslConfigs::SSL_PROTOCOL_CONFIG)?.to_string(),
268            cipher_suites: values.list(SslConfigs::SSL_CIPHER_SUITES_CONFIG)?.to_vec(),
269        })
270    }
271
272    /// The `ssl.*` settings parsed from `props` with
273    /// [`Self::add_client_ssl_support`]'s definitions, for the crate's tests.
274    #[cfg(test)]
275    pub(crate) fn parse_for_test(props: &[(&str, &str)]) -> Result<SslConfigs, Error> {
276        use std::collections::HashMap;
277        use std::sync::LazyLock;
278
279        use crate::common::config::abstract_config::{AbstractConfig, string_originals};
280
281        static DEFINITION: LazyLock<ConfigDef> = LazyLock::new(|| ConfigDef::new().with_client_ssl_support());
282        let props: HashMap<String, String> = props.iter().map(|&(k, v)| (k.to_string(), v.to_string())).collect();
283        let config = AbstractConfig::new(&DEFINITION, string_originals(&props), false)?;
284        SslConfigs::from_values(&mut ValuesReader::new(&config))
285    }
286}
287
288impl SslConfigs {
289    /// Java's `SslConfigs.DEFAULT_SSL_KEYMANGER_ALGORITHM`, which is
290    /// `KeyManagerFactory.getDefaultAlgorithm()`, the JVM's default: `SunX509`
291    /// unless the `ssl.KeyManagerFactory.algorithm` security property says
292    /// otherwise. Without a JVM it is the literal default.
293    const DEFAULT_SSL_KEYMANGER_ALGORITHM: &str = "SunX509";
294
295    /// Java's `SslConfigs.DEFAULT_SSL_TRUSTMANAGER_ALGORITHM`, which is
296    /// `TrustManagerFactory.getDefaultAlgorithm()`: `PKIX` on a stock JVM.
297    const DEFAULT_SSL_TRUSTMANAGER_ALGORITHM: &str = "PKIX";
298
299    /// Defines the client `ssl.*` keys on `config`: Java's
300    /// `SslConfigs.addClientSslSupport(ConfigDef)`, in Java's order, except
301    /// `ssl.engine.factory.class`, a class-valued key (CLAUDE.md §2), and
302    /// `ssl.keystore.password`, `ssl.key.password` and
303    /// `ssl.truststore.password`, which cannot be honoured (module docs).
304    pub(crate) fn add_client_ssl_support(config: ConfigDef) -> ConfigDef {
305        config
306            .define(
307                SslConfigs::SSL_PROTOCOL_CONFIG,
308                Type::String,
309                SslConfigs::DEFAULT_SSL_PROTOCOL,
310                Importance::Medium,
311            )
312            .define(
313                SslConfigs::SSL_PROVIDER_CONFIG,
314                Type::String,
315                DefaultValue::Null,
316                Importance::Medium,
317            )
318            .define_with_validator(
319                SslConfigs::SSL_CIPHER_SUITES_CONFIG,
320                Type::List,
321                Vec::<String>::new(),
322                ValidList::any_non_duplicate_values(true, false),
323                Importance::Low,
324            )
325            .define_with_validator(
326                SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG,
327                Type::List,
328                SslConfigs::DEFAULT_SSL_ENABLED_PROTOCOLS,
329                ValidList::any_non_duplicate_values(true, false),
330                Importance::Medium,
331            )
332            .define(
333                SslConfigs::SSL_KEYSTORE_TYPE_CONFIG,
334                Type::String,
335                SslConfigs::DEFAULT_SSL_KEYSTORE_TYPE,
336                Importance::Medium,
337            )
338            .define(
339                SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG,
340                Type::String,
341                DefaultValue::Null,
342                Importance::High,
343            )
344            // Java defines `ssl.keystore.password` here (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`);
345            // it stays undefined because Java rejects a key store password with
346            // PEM stores (`org.apache.kafka.common.security.ssl.DefaultSslEngineFactory#createKeystore`) and PEM is the
347            // only format here, until another format is implemented, so a value
348            // set for it is reported as unused.
349            //
350            // Java defines `ssl.key.password` here (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`); it
351            // stays undefined because encrypted PEM private keys are not
352            // decrypted (`org.apache.kafka.common.security.ssl.DefaultSslEngineFactory.PemStore#privateKey`), until that is
353            // implemented, so a value set for it is reported as unused.
354            .define(
355                SslConfigs::SSL_KEYSTORE_KEY_CONFIG,
356                Type::Password,
357                DefaultValue::Null,
358                Importance::High,
359            )
360            .define(
361                SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG,
362                Type::Password,
363                DefaultValue::Null,
364                Importance::High,
365            )
366            .define(
367                SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG,
368                Type::Password,
369                DefaultValue::Null,
370                Importance::High,
371            )
372            .define(
373                SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG,
374                Type::String,
375                SslConfigs::DEFAULT_SSL_TRUSTSTORE_TYPE,
376                Importance::Medium,
377            )
378            .define(
379                SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG,
380                Type::String,
381                DefaultValue::Null,
382                Importance::High,
383            )
384            // Java defines `ssl.truststore.password` here (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`);
385            // it stays undefined because Java rejects a trust store password
386            // with PEM stores (`org.apache.kafka.common.security.ssl.DefaultSslEngineFactory#createTruststore`) and PEM is
387            // the only format here, until another format is implemented, so a
388            // value set for it is reported as unused.
389            .define(
390                SslConfigs::SSL_KEYMANAGER_ALGORITHM_CONFIG,
391                Type::String,
392                SslConfigs::DEFAULT_SSL_KEYMANGER_ALGORITHM,
393                Importance::Low,
394            )
395            .define(
396                SslConfigs::SSL_TRUSTMANAGER_ALGORITHM_CONFIG,
397                Type::String,
398                SslConfigs::DEFAULT_SSL_TRUSTMANAGER_ALGORITHM,
399                Importance::Low,
400            )
401            .define(
402                SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG,
403                Type::String,
404                SslConfigs::DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM,
405                Importance::Low,
406            )
407            .define(
408                SslConfigs::SSL_SECURE_RANDOM_IMPLEMENTATION_CONFIG,
409                Type::String,
410                DefaultValue::Null,
411                Importance::Low,
412            )
413    }
414}
415
416#[cfg(test)]
417mod tests {
418    use super::*;
419
420    #[test]
421    fn test_default_truststore_type() {
422        let config = SslConfigs::default();
423        assert_eq!(config.truststore_type, "PEM");
424    }
425
426    #[test]
427    fn test_default_keystore_type() {
428        let config = SslConfigs::default();
429        assert_eq!(config.keystore_type, "PEM");
430    }
431
432    #[test]
433    fn test_default_endpoint_identification() {
434        let config = SslConfigs::default();
435        assert_eq!(config.endpoint_identification_algorithm, "https");
436    }
437
438    #[test]
439    fn test_default_enabled_protocols() {
440        let config = SslConfigs::default();
441        assert_eq!(config.enabled_protocols, vec!["TLSv1.2", "TLSv1.3"]);
442    }
443
444    #[test]
445    fn test_default_optional_fields_are_none() {
446        let config = SslConfigs::default();
447        assert!(config.truststore_location.is_none());
448        assert!(config.truststore_certificates.is_none());
449        assert!(config.keystore_location.is_none());
450        assert!(config.keystore_key.is_none());
451        assert!(config.keystore_certificate_chain.is_none());
452    }
453
454    #[test]
455    fn test_custom_config() {
456        let config = SslConfigs {
457            truststore_location: Some("/path/to/truststore.pem".to_owned()),
458            keystore_location: Some("/path/to/keystore.pem".to_owned()),
459            keystore_key: Some(Password::new("-----BEGIN PRIVATE KEY-----\n...")),
460            keystore_certificate_chain: Some(Password::new("-----BEGIN CERTIFICATE-----\n...")),
461            keystore_type: "PKCS12".to_owned(),
462            endpoint_identification_algorithm: String::new(),
463            ..SslConfigs::default()
464        };
465        assert_eq!(config.truststore_location.as_deref(), Some("/path/to/truststore.pem"));
466        assert_eq!(config.keystore_location.as_deref(), Some("/path/to/keystore.pem"));
467        assert_eq!(config.keystore_type, "PKCS12");
468        assert_eq!(config.endpoint_identification_algorithm, "");
469        // Inherited from default
470        assert_eq!(config.truststore_type, "PEM");
471    }
472
473    #[test]
474    fn test_config_key_constants() {
475        assert_eq!(SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, "ssl.truststore.location");
476        assert_eq!(SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG, "ssl.truststore.certificates");
477        assert_eq!(SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, "ssl.truststore.type");
478        assert_eq!(SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, "ssl.keystore.location");
479        assert_eq!(SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "ssl.keystore.key");
480        assert_eq!(
481            SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG,
482            "ssl.keystore.certificate.chain"
483        );
484        assert_eq!(SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "ssl.keystore.type");
485        assert_eq!(
486            SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG,
487            "ssl.endpoint.identification.algorithm"
488        );
489        assert_eq!(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, "ssl.enabled.protocols");
490        assert_eq!(SslConfigs::SSL_PROTOCOL_CONFIG, "ssl.protocol");
491        assert_eq!(SslConfigs::SSL_PROVIDER_CONFIG, "ssl.provider");
492        assert_eq!(SslConfigs::SSL_CIPHER_SUITES_CONFIG, "ssl.cipher.suites");
493        assert_eq!(
494            SslConfigs::SSL_SECURE_RANDOM_IMPLEMENTATION_CONFIG,
495            "ssl.secure.random.implementation"
496        );
497        assert_eq!(SslConfigs::SSL_KEYMANAGER_ALGORITHM_CONFIG, "ssl.keymanager.algorithm");
498        assert_eq!(SslConfigs::SSL_TRUSTMANAGER_ALGORITHM_CONFIG, "ssl.trustmanager.algorithm");
499    }
500
501    /// Java's `ssl.keystore.password`, `ssl.key.password` and
502    /// `ssl.truststore.password` (`org.apache.kafka.common.config.SslConfigs#SSL_KEYSTORE_PASSWORD_CONFIG`, `SSL_KEY_PASSWORD_CONFIG`, `SSL_TRUSTSTORE_PASSWORD_CONFIG`) are not defined,
503    /// so a value set for one is reported as unused.
504    #[test]
505    fn test_password_keys_not_defined() {
506        let names = SslConfigs::add_client_ssl_support(ConfigDef::new()).names();
507        for key in ["ssl.keystore.password", "ssl.key.password", "ssl.truststore.password"] {
508            assert!(!names.contains(key), "{key}");
509        }
510        assert!(names.contains(SslConfigs::SSL_KEYSTORE_KEY_CONFIG));
511    }
512
513    #[test]
514    fn test_default_constants() {
515        assert_eq!(SslConfigs::DEFAULT_SSL_PROTOCOL, "TLSv1.3");
516        assert_eq!(SslConfigs::DEFAULT_SSL_ENABLED_PROTOCOLS, "TLSv1.2,TLSv1.3");
517        assert_eq!(SslConfigs::DEFAULT_SSL_KEYSTORE_TYPE, "PEM");
518        assert_eq!(SslConfigs::DEFAULT_SSL_TRUSTSTORE_TYPE, "PEM");
519        assert_eq!(SslConfigs::DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM, "https");
520    }
521
522    #[test]
523    fn test_from_values_sets_each_field() {
524        let ssl = SslConfigs::parse_for_test(&[
525            (SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, "/ts.pem"),
526            (SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG, "-----BEGIN CERTIFICATE-----"),
527            (SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, "PKCS12"),
528            (SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, "/ks.pem"),
529            (SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "-----BEGIN PRIVATE KEY-----"),
530            (SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG, "-----BEGIN CERTIFICATE-----"),
531            (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "JKS"),
532            (SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, ""),
533            (SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, "TLSv1.2, TLSv1.3"),
534            (SslConfigs::SSL_PROTOCOL_CONFIG, "TLSv1.2"),
535            (
536                SslConfigs::SSL_CIPHER_SUITES_CONFIG,
537                "TLS_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
538            ),
539        ])
540        .unwrap();
541
542        assert_eq!(ssl.truststore_location.as_deref(), Some("/ts.pem"));
543        assert_eq!(
544            ssl.truststore_certificates.as_ref().map(Password::value),
545            Some("-----BEGIN CERTIFICATE-----")
546        );
547        assert_eq!(ssl.truststore_type, "PKCS12");
548        assert_eq!(ssl.keystore_location.as_deref(), Some("/ks.pem"));
549        assert_eq!(
550            ssl.keystore_key.as_ref().map(Password::value),
551            Some("-----BEGIN PRIVATE KEY-----")
552        );
553        assert_eq!(
554            ssl.keystore_certificate_chain.as_ref().map(Password::value),
555            Some("-----BEGIN CERTIFICATE-----")
556        );
557        assert_eq!(ssl.keystore_type, "JKS");
558        assert_eq!(ssl.endpoint_identification_algorithm, "");
559        assert_eq!(ssl.enabled_protocols, vec!["TLSv1.2", "TLSv1.3"]);
560        assert_eq!(ssl.protocol, "TLSv1.2");
561        assert_eq!(
562            ssl.cipher_suites,
563            vec!["TLS_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"]
564        );
565    }
566
567    #[test]
568    fn test_default_protocol_and_cipher_suites() {
569        let ssl = SslConfigs::parse_for_test(&[]).unwrap();
570        assert_eq!(ssl.protocol, "TLSv1.3");
571        assert!(ssl.cipher_suites.is_empty());
572        // `SslConfigs` has no `PartialEq` (Java's has no `equals`), so the whole
573        // struct is compared through its `Debug` rendering; the defaults hold no
574        // password, so `Password`'s `[hidden]` hides nothing here.
575        assert_eq!(format!("{ssl:?}"), format!("{:?}", SslConfigs::default()));
576    }
577
578    /// `ssl.cipher.suites` is validated with Java's
579    /// `ValidList.anyNonDuplicateValues(true, false)` (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`):
580    /// an empty suite is rejected with `ConfigDef`'s exact message, duplicates
581    /// are removed, and an empty list is allowed.
582    #[test]
583    fn test_cipher_suites_valid_list() {
584        for value in ["A,,B", ",,"] {
585            match SslConfigs::parse_for_test(&[(SslConfigs::SSL_CIPHER_SUITES_CONFIG, value)]) {
586                Err(Error::Config(e)) => assert_eq!(
587                    e.message(),
588                    "Configuration 'ssl.cipher.suites' values must not be empty.",
589                    "{value:?}"
590                ),
591                other => panic!("expected a ConfigError for {value:?}, got {other:?}"),
592            }
593        }
594        let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_CIPHER_SUITES_CONFIG, "A, B")]).unwrap();
595        assert_eq!(ssl.cipher_suites, vec!["A", "B"]);
596        let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_CIPHER_SUITES_CONFIG, "A,A")]).unwrap();
597        assert_eq!(ssl.cipher_suites, vec!["A"]);
598        let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_CIPHER_SUITES_CONFIG, "")]).unwrap();
599        assert!(ssl.cipher_suites.is_empty());
600    }
601
602    #[test]
603    fn test_from_values_ignores_an_unknown_key() {
604        // An `ssl.*` key the definitions do not have is accepted, as Java's
605        // `AbstractConfig` accepts any key, and leaves the defaults untouched.
606        let ssl = SslConfigs::parse_for_test(&[("ssl.unknown.key", "value")]).unwrap();
607        assert_eq!(ssl.truststore_type, "PEM");
608        assert!(ssl.truststore_location.is_none());
609    }
610
611    /// `ssl.enabled.protocols` is a `Type.LIST`, parsed like every list by
612    /// `ConfigDef::parse_list`: whitespace around commas and at the ends is
613    /// dropped, and a blank value is the empty list.
614    #[test]
615    fn test_enabled_protocols_list_parsing() {
616        for value in ["TLSv1.2,TLSv1.3", "TLSv1.2, TLSv1.3", " TLSv1.2 ,TLSv1.3 "] {
617            let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, value)]).unwrap();
618            assert_eq!(ssl.enabled_protocols, vec!["TLSv1.2", "TLSv1.3"], "{value:?}");
619        }
620        let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, " ")]).unwrap();
621        assert!(ssl.enabled_protocols.is_empty());
622    }
623
624    /// `ssl.enabled.protocols` is validated with Java's
625    /// `ValidList.anyNonDuplicateValues(true, false)` (`org.apache.kafka.common.config.SslConfigs#addClientSslSupport`):
626    /// an empty protocol is rejected with `ConfigDef`'s exact message,
627    /// duplicates are removed, and an empty list is allowed.
628    #[test]
629    fn test_enabled_protocols_valid_list() {
630        for (value, expected) in [
631            (
632                "TLSv1.2,,TLSv1.3",
633                "Configuration 'ssl.enabled.protocols' values must not be empty.",
634            ),
635            (",,", "Configuration 'ssl.enabled.protocols' values must not be empty."),
636        ] {
637            match SslConfigs::parse_for_test(&[(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, value)]) {
638                Err(Error::Config(e)) => assert_eq!(e.message(), expected, "{value:?}"),
639                other => panic!("expected a ConfigError for {value:?}, got {other:?}"),
640            }
641        }
642        let ssl = SslConfigs::parse_for_test(&[(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, "")]).unwrap();
643        assert!(ssl.enabled_protocols.is_empty());
644        // Duplicates are removed by `ConfigDef.parseValue`, not rejected.
645        let ssl =
646            SslConfigs::parse_for_test(&[(SslConfigs::SSL_ENABLED_PROTOCOLS_CONFIG, "TLSv1.2, TLSv1.2")]).unwrap();
647        assert_eq!(ssl.enabled_protocols, vec!["TLSv1.2".to_string()]);
648    }
649
650    /// `{:?}` hides the three `Type.PASSWORD` fields and keeps the others.
651    #[test]
652    fn test_debug_redacts_password_fields() {
653        let config = SslConfigs {
654            truststore_location: Some("/etc/kafka/visible-truststore.pem".to_owned()),
655            truststore_certificates: Some(Password::new("TRUSTSTORE-CERTIFICATES-PEM")),
656            keystore_location: Some("/etc/kafka/visible-keystore.pem".to_owned()),
657            keystore_key: Some(Password::new("-----BEGIN PRIVATE KEY-----KEYSTORE-KEY-PEM")),
658            keystore_certificate_chain: Some(Password::new("KEYSTORE-CERTIFICATE-CHAIN-PEM")),
659            keystore_type: "PKCS12".to_owned(),
660            ..SslConfigs::default()
661        };
662
663        for rendered in [format!("{config:?}"), format!("{config:#?}")] {
664            for secret in [
665                "TRUSTSTORE-CERTIFICATES-PEM",
666                "PRIVATE KEY",
667                "KEYSTORE-KEY-PEM",
668                "KEYSTORE-CERTIFICATE-CHAIN-PEM",
669            ] {
670                assert!(!rendered.contains(secret), "{secret:?} leaked: {rendered}");
671            }
672            assert_eq!(rendered.matches(Password::HIDDEN).count(), 3, "{rendered}");
673            assert!(rendered.contains("/etc/kafka/visible-truststore.pem"), "{rendered}");
674            assert!(rendered.contains("/etc/kafka/visible-keystore.pem"), "{rendered}");
675            assert!(rendered.contains("\"PKCS12\""), "{rendered}");
676        }
677        let rendered = format!("{config:?}");
678        for field in ["truststore_certificates", "keystore_key", "keystore_certificate_chain"] {
679            let hidden = format!("{field}: Some([hidden])");
680            assert!(rendered.contains(&hidden), "{hidden} missing: {rendered}");
681        }
682        assert!(
683            rendered.contains("truststore_location: Some(\"/etc/kafka/visible-truststore.pem\")"),
684            "{rendered}"
685        );
686        assert!(rendered.contains("keystore_type: \"PKCS12\""), "{rendered}");
687    }
688}