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
167 lines (140 loc) · 5.12 KB
/
kafka.rs
File metadata and controls
167 lines (140 loc) · 5.12 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
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
#![allow(missing_docs)]
use std::path::{Path, PathBuf};
use rdkafka::{consumer::ConsumerContext, ClientConfig, ClientContext, Statistics};
use snafu::Snafu;
use vector_common::sensitive_string::SensitiveString;
use vector_config::configurable_component;
use crate::{
internal_events::KafkaStatisticsReceived, tls::TlsEnableableConfig, tls::PEM_START_MARKER,
};
#[derive(Debug, Snafu)]
enum KafkaError {
#[snafu(display("invalid path: {:?}", path))]
InvalidPath { path: PathBuf },
}
/// Supported compression types for Kafka.
#[configurable_component]
#[derive(Clone, Copy, Debug, Derivative)]
#[derivative(Default)]
#[serde(rename_all = "lowercase")]
pub enum KafkaCompression {
/// No compression.
#[derivative(Default)]
None,
/// Gzip.
Gzip,
/// Snappy.
Snappy,
/// LZ4.
Lz4,
/// Zstandard.
Zstd,
}
/// Kafka authentication configuration.
#[configurable_component]
#[derive(Clone, Debug, Default)]
pub struct KafkaAuthConfig {
#[configurable(derived)]
pub(crate) sasl: Option<KafkaSaslConfig>,
#[configurable(derived)]
#[configurable(metadata(docs::advanced))]
pub(crate) tls: Option<TlsEnableableConfig>,
}
/// Configuration for SASL authentication when interacting with Kafka.
#[configurable_component]
#[derive(Clone, Debug, Default)]
pub struct KafkaSaslConfig {
/// Enables SASL authentication.
///
/// Only `PLAIN` and `SCRAM`-based mechanisms are supported when configuring SASL authentication via `sasl.*`. For
/// other mechanisms, `librdkafka_options.*` must be used directly to configure other `librdkafka`-specific values
/// i.e. `sasl.kerberos.*` and so on.
///
/// See the [librdkafka documentation](https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md) for details.
///
/// SASL authentication is not supported on Windows.
pub(crate) enabled: Option<bool>,
/// The SASL username.
#[configurable(metadata(docs::examples = "username"))]
pub(crate) username: Option<String>,
/// The SASL password.
#[configurable(metadata(docs::examples = "password"))]
pub(crate) password: Option<SensitiveString>,
/// The SASL mechanism to use.
#[configurable(metadata(docs::examples = "SCRAM-SHA-256"))]
#[configurable(metadata(docs::examples = "SCRAM-SHA-512"))]
pub(crate) mechanism: Option<String>,
}
impl KafkaAuthConfig {
pub(crate) fn apply(&self, client: &mut ClientConfig) -> crate::Result<()> {
let sasl_enabled = self.sasl.as_ref().and_then(|s| s.enabled).unwrap_or(false);
let tls_enabled = self.tls.as_ref().and_then(|s| s.enabled).unwrap_or(false);
let protocol = match (sasl_enabled, tls_enabled) {
(false, false) => "plaintext",
(false, true) => "ssl",
(true, false) => "sasl_plaintext",
(true, true) => "sasl_ssl",
};
client.set("security.protocol", protocol);
if sasl_enabled {
let sasl = self.sasl.as_ref().unwrap();
if let Some(username) = &sasl.username {
client.set("sasl.username", username.as_str());
}
if let Some(password) = &sasl.password {
client.set("sasl.password", password.inner());
}
if let Some(mechanism) = &sasl.mechanism {
client.set("sasl.mechanism", mechanism);
}
}
if tls_enabled {
let tls = self.tls.as_ref().unwrap();
if let Some(path) = &tls.options.ca_file {
let text = pathbuf_to_string(path)?;
if text.contains(PEM_START_MARKER) {
client.set("ssl.ca.pem", text);
} else {
client.set("ssl.ca.location", text);
}
}
if let Some(path) = &tls.options.crt_file {
let text = pathbuf_to_string(path)?;
if text.contains(PEM_START_MARKER) {
client.set("ssl.certificate.pem", text);
} else {
client.set("ssl.certificate.location", text);
}
}
if let Some(path) = &tls.options.key_file {
let text = pathbuf_to_string(path)?;
if text.contains(PEM_START_MARKER) {
client.set("ssl.key.pem", text);
} else {
client.set("ssl.key.location", text);
}
}
if let Some(pass) = &tls.options.key_pass {
client.set("ssl.key.password", pass);
}
}
Ok(())
}
}
fn pathbuf_to_string(path: &Path) -> crate::Result<&str> {
path.to_str()
.ok_or_else(|| KafkaError::InvalidPath { path: path.into() }.into())
}
#[derive(Default)]
pub(crate) struct KafkaStatisticsContext {
pub(crate) expose_lag_metrics: bool,
}
impl ClientContext for KafkaStatisticsContext {
fn stats(&self, statistics: Statistics) {
emit!(KafkaStatisticsReceived {
statistics: &statistics,
expose_lag_metrics: self.expose_lag_metrics,
});
}
}
impl ConsumerContext for KafkaStatisticsContext {}