forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkafka.rs
More file actions
49 lines (44 loc) · 1.45 KB
/
Copy pathkafka.rs
File metadata and controls
49 lines (44 loc) · 1.45 KB
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
use crate::tls::TlsOptions;
use rdkafka::ClientConfig;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::path::PathBuf;
#[derive(Debug, Snafu)]
enum KafkaError {
#[snafu(display("invalid path: {:?}", path))]
InvalidPath { path: PathBuf },
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct KafkaTlsConfig {
pub enabled: Option<bool>,
#[serde(flatten)]
pub options: TlsOptions,
}
impl KafkaTlsConfig {
pub(crate) fn apply(&self, client: &mut ClientConfig) -> crate::Result<()> {
client.set(
"security.protocol",
if self.enabled() { "ssl" } else { "plaintext" },
);
if let Some(ref path) = self.options.ca_path {
client.set("ssl.ca.location", pathbuf_to_string(&path)?);
}
if let Some(ref path) = self.options.crt_path {
client.set("ssl.certificate.location", pathbuf_to_string(&path)?);
}
if let Some(ref path) = self.options.key_path {
client.set("ssl.keystore.location", pathbuf_to_string(&path)?);
}
if let Some(ref pass) = self.options.key_pass {
client.set("ssl.keystore.password", pass);
}
Ok(())
}
pub(crate) fn enabled(&self) -> bool {
self.enabled.unwrap_or(false)
}
}
fn pathbuf_to_string(path: &PathBuf) -> crate::Result<&str> {
path.to_str()
.ok_or_else(|| KafkaError::InvalidPath { path: path.into() }.into())
}