kubemq 1.0.0

KubeMQ Rust SDK - Message broker client for events, commands, queries, and queues
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/// TLS configuration for connecting to a KubeMQ server.
#[derive(Clone, Default)]
pub struct TlsConfig {
    /// Path to CA certificate file for server verification.
    pub ca_cert_file: Option<String>,
    /// PEM-encoded CA certificate bytes for server verification.
    pub ca_cert_pem: Option<Vec<u8>>,
    /// Path to client certificate file (mTLS).
    pub cert_file: Option<String>,
    /// Path to client private key file (mTLS).
    pub key_file: Option<String>,
    /// PEM-encoded client certificate bytes (mTLS).
    pub cert_pem: Option<Vec<u8>>,
    /// PEM-encoded client private key bytes (mTLS).
    pub key_pem: Option<Vec<u8>>,
    /// Override the server name for TLS verification.
    pub server_name: Option<String>,
}

impl std::fmt::Debug for TlsConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TlsConfig")
            .field("ca_cert_file", &self.ca_cert_file)
            .field(
                "ca_cert_pem",
                &self
                    .ca_cert_pem
                    .as_ref()
                    .map(|v| format!("[{} bytes]", v.len())),
            )
            .field("cert_file", &self.cert_file)
            .field("key_file", &"[REDACTED]")
            .field(
                "cert_pem",
                &self
                    .cert_pem
                    .as_ref()
                    .map(|v| format!("[{} bytes]", v.len())),
            )
            .field("key_pem", &self.key_pem.as_ref().map(|_| "[REDACTED]"))
            .field("server_name", &self.server_name)
            .finish()
    }
}

impl TlsConfig {
    /// Build a tonic TLS config from this configuration.
    ///
    /// REQ-M34: Uses async `tokio::fs::read` instead of blocking `std::fs::read`.
    pub(crate) async fn to_tonic_tls_config(
        &self,
    ) -> crate::Result<tonic::transport::ClientTlsConfig> {
        let mut tls_config = tonic::transport::ClientTlsConfig::new();

        // Server name override
        if let Some(ref server_name) = self.server_name {
            tls_config = tls_config.domain_name(server_name.clone());
        }

        // CA certificate -- REQ-M34: async file I/O
        if let Some(ref ca_pem) = self.ca_cert_pem {
            let cert = tonic::transport::Certificate::from_pem(ca_pem.clone());
            tls_config = tls_config.ca_certificate(cert);
        } else if let Some(ref ca_file) = self.ca_cert_file {
            let pem = tokio::fs::read(ca_file)
                .await
                .map_err(|e| crate::KubemqError::Fatal {
                    code: crate::ErrorCode::Fatal,
                    message: format!("failed to read CA cert file '{}': {}", ca_file, e),
                    operation: "tls_config".to_string(),
                    source: Some(Box::new(e)),
                    suggestion: "Check that the CA certificate file exists and is readable.",
                })?;
            let cert = tonic::transport::Certificate::from_pem(pem);
            tls_config = tls_config.ca_certificate(cert);
        }

        // Client identity (mTLS) -- REQ-M34: async file I/O
        let client_cert = if let Some(ref pem) = self.cert_pem {
            Some(pem.clone())
        } else if let Some(ref path) = self.cert_file {
            Some(
                tokio::fs::read(path)
                    .await
                    .map_err(|e| crate::KubemqError::Fatal {
                        code: crate::ErrorCode::Fatal,
                        message: format!("failed to read client cert file '{}': {}", path, e),
                        operation: "tls_config".to_string(),
                        source: Some(Box::new(e)),
                        suggestion:
                            "Check that the client certificate file exists and is readable.",
                    })?,
            )
        } else {
            None
        };
        let client_key = if let Some(ref pem) = self.key_pem {
            Some(pem.clone())
        } else if let Some(ref path) = self.key_file {
            Some(
                tokio::fs::read(path)
                    .await
                    .map_err(|e| crate::KubemqError::Fatal {
                        code: crate::ErrorCode::Fatal,
                        message: format!("failed to read client key file '{}': {}", path, e),
                        operation: "tls_config".to_string(),
                        source: Some(Box::new(e)),
                        suggestion: "Check that the client key file exists and is readable.",
                    })?,
            )
        } else {
            None
        };

        // REQ-M5: mTLS half-configuration validation
        match (client_cert.is_some(), client_key.is_some()) {
            (true, true) => {
                let identity =
                    tonic::transport::Identity::from_pem(client_cert.unwrap(), client_key.unwrap());
                tls_config = tls_config.identity(identity);
            }
            (false, false) => { /* No mTLS -- OK */ }
            _ => {
                return Err(crate::KubemqError::Validation {
                    code: crate::ErrorCode::Validation,
                    message: "mTLS requires both client certificate and key".to_string(),
                    operation: "tls_config".to_string(),
                    channel: String::new(),
                    suggestion: "Provide both cert_file/cert_pem and key_file/key_pem, or neither.",
                });
            }
        }

        Ok(tls_config)
    }

    /// Check if TLS is enabled (any TLS option is set).
    #[allow(dead_code)]
    pub(crate) fn is_enabled(&self) -> bool {
        self.ca_cert_file.is_some()
            || self.ca_cert_pem.is_some()
            || self.cert_file.is_some()
            || self.key_file.is_some()
            || self.cert_pem.is_some()
            || self.key_pem.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // -- is_enabled tests --

    #[test]
    fn test_is_enabled_default_false() {
        let config = TlsConfig::default();
        assert!(!config.is_enabled());
    }

    #[test]
    fn test_is_enabled_ca_cert_file() {
        let config = TlsConfig {
            ca_cert_file: Some("/path/to/ca.pem".to_string()),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    #[test]
    fn test_is_enabled_ca_cert_pem() {
        let config = TlsConfig {
            ca_cert_pem: Some(vec![1, 2, 3]),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    #[test]
    fn test_is_enabled_cert_file() {
        let config = TlsConfig {
            cert_file: Some("/path/to/cert.pem".to_string()),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    #[test]
    fn test_is_enabled_key_file() {
        let config = TlsConfig {
            key_file: Some("/path/to/key.pem".to_string()),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    #[test]
    fn test_is_enabled_cert_pem() {
        let config = TlsConfig {
            cert_pem: Some(vec![4, 5, 6]),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    #[test]
    fn test_is_enabled_key_pem() {
        let config = TlsConfig {
            key_pem: Some(vec![7, 8, 9]),
            ..Default::default()
        };
        assert!(config.is_enabled());
    }

    // -- to_tonic_tls_config tests --

    #[tokio::test]
    async fn test_to_tonic_tls_config_minimal() {
        // No CA, no mTLS, no insecure -- should produce a valid config
        let config = TlsConfig::default();
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_with_server_name() {
        let config = TlsConfig {
            server_name: Some("example.com".to_string()),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_with_ca_pem() {
        // Use a syntactically valid (but not real) PEM -- tonic accepts the bytes
        let config = TlsConfig {
            ca_cert_pem: Some(
                b"-----BEGIN CERTIFICATE-----\nMIIBfake\n-----END CERTIFICATE-----\n".to_vec(),
            ),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        // tonic's Certificate::from_pem just stores bytes; validation happens at connect time
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_ca_cert_file_not_found() {
        let config = TlsConfig {
            ca_cert_file: Some("/nonexistent/ca-cert.pem".to_string()),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), crate::ErrorCode::Fatal);
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_ca_cert_from_file() {
        // Write a temp CA cert file and read it back
        let dir = tempfile::tempdir().unwrap();
        let ca_path = dir.path().join("ca.pem");
        tokio::fs::write(
            &ca_path,
            b"-----BEGIN CERTIFICATE-----\nMIIBfake\n-----END CERTIFICATE-----\n",
        )
        .await
        .unwrap();

        let config = TlsConfig {
            ca_cert_file: Some(ca_path.to_str().unwrap().to_string()),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_mtls_half_config_cert_only() {
        let config = TlsConfig {
            cert_pem: Some(
                b"-----BEGIN CERTIFICATE-----\nfake\n-----END CERTIFICATE-----".to_vec(),
            ),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), crate::ErrorCode::Validation);
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_mtls_half_config_key_only() {
        let config = TlsConfig {
            key_pem: Some(
                b"-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----".to_vec(),
            ),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), crate::ErrorCode::Validation);
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_mtls_full_pem() {
        let config = TlsConfig {
            cert_pem: Some(
                b"-----BEGIN CERTIFICATE-----\nfake\n-----END CERTIFICATE-----".to_vec(),
            ),
            key_pem: Some(
                b"-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----".to_vec(),
            ),
            ..Default::default()
        };
        // Both provided, so mTLS validation passes (connect-time will fail with bad certs)
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_cert_file_not_found() {
        let config = TlsConfig {
            cert_file: Some("/nonexistent/client.pem".to_string()),
            key_pem: Some(
                b"-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----".to_vec(),
            ),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), crate::ErrorCode::Fatal);
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_key_file_not_found() {
        let config = TlsConfig {
            cert_pem: Some(
                b"-----BEGIN CERTIFICATE-----\nfake\n-----END CERTIFICATE-----".to_vec(),
            ),
            key_file: Some("/nonexistent/key.pem".to_string()),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code(), crate::ErrorCode::Fatal);
    }

    #[tokio::test]
    async fn test_to_tonic_tls_config_mtls_from_files() {
        let dir = tempfile::tempdir().unwrap();
        let cert_path = dir.path().join("client.pem");
        let key_path = dir.path().join("client-key.pem");
        tokio::fs::write(
            &cert_path,
            b"-----BEGIN CERTIFICATE-----\nfake\n-----END CERTIFICATE-----\n",
        )
        .await
        .unwrap();
        tokio::fs::write(
            &key_path,
            b"-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----\n",
        )
        .await
        .unwrap();

        let config = TlsConfig {
            cert_file: Some(cert_path.to_str().unwrap().to_string()),
            key_file: Some(key_path.to_str().unwrap().to_string()),
            ..Default::default()
        };
        let result = config.to_tonic_tls_config().await;
        assert!(result.is_ok());
    }

    // -- Debug redaction tests --

    #[test]
    fn test_debug_redacts_key_file() {
        let config = TlsConfig {
            key_file: Some("/secret/key.pem".to_string()),
            ..Default::default()
        };
        let debug = format!("{:?}", config);
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains("/secret/key.pem"));
    }

    #[test]
    fn test_debug_redacts_key_pem() {
        let config = TlsConfig {
            key_pem: Some(vec![1, 2, 3, 4, 5]),
            ..Default::default()
        };
        let debug = format!("{:?}", config);
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains("[1, 2, 3, 4, 5]"));
    }

    #[test]
    fn test_debug_shows_pem_lengths() {
        let config = TlsConfig {
            ca_cert_pem: Some(vec![0; 42]),
            cert_pem: Some(vec![0; 100]),
            ..Default::default()
        };
        let debug = format!("{:?}", config);
        assert!(debug.contains("[42 bytes]"));
        assert!(debug.contains("[100 bytes]"));
    }
}