Skip to content

Commit 9458a03

Browse files
committed
feat: configure TLS with environment variables.
Updates the opentelemetry-otlp crate to allow users to configure TLS using environment variables. Removing the need to crating the TLS config object and defining it with the `with_tls_config` method. In the same way other OTLP libraries does (e.g. go lang). Signed-off-by: José Guilherme Vanz <[email protected]>
1 parent 6e1032f commit 9458a03

File tree

10 files changed

+497
-42
lines changed

10 files changed

+497
-42
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ tracing = { version = ">=0.1.40", default-features = false }
5151
tracing-core = { version = ">=0.1.33", default-features = false }
5252
tracing-subscriber = { version = "0.3", default-features = false }
5353
url = { version = "2.5", default-features = false }
54+
rcgen = { version = "0.13", features = ["crypto"] }
55+
tempfile = "3.14"

opentelemetry-otlp/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## vNext
44

55
- Bump msrv to 1.75.0.
6+
- TLS configuration via environment variables for GRPc exporters.
67

78

89
## 0.27.0

opentelemetry-otlp/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ opentelemetry_sdk = { features = ["trace", "rt-tokio", "testing"], path = "../op
5151
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
5252
futures-util = { workspace = true }
5353
temp-env = { workspace = true }
54+
rcgen = { workspace = true }
55+
tempfile = { workspace = true }
5456

5557
[features]
5658
# telemetry pillars and functions

opentelemetry-otlp/src/exporter/mod.rs

+61
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL";
2828
/// Compression algorithm to use, defaults to none.
2929
pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION";
3030

31+
/// Certificate file to validate the OTLP server connection
32+
#[cfg(feature = "tls")]
33+
pub const OTEL_EXPORTER_OTLP_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CERTIFICATE";
34+
/// Path to the certificate file to use for client authentication (mTLS).
35+
#[cfg(feature = "tls")]
36+
pub const OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE";
37+
/// Path to the key file to use for client authentication (mTLS).
38+
#[cfg(feature = "tls")]
39+
pub const OTEL_EXPORTER_OTLP_CLIENT_KEY: &str = "OTEL_EXPORTER_OTLP_CLIENT_KEY";
40+
/// Use insecure connection. Disable TLS
41+
#[cfg(feature = "tls")]
42+
pub const OTEL_EXPORTER_OTLP_INSECURE: &str = "OTEL_EXPORTER_OTLP_INSECURE";
43+
3144
#[cfg(feature = "http-json")]
3245
/// Default protocol, using http-json.
3346
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
@@ -76,6 +89,18 @@ pub struct ExportConfig {
7689

7790
/// The timeout to the collector.
7891
pub timeout: Duration,
92+
93+
/// Disable TLS
94+
pub insecure: Option<bool>,
95+
96+
/// The certificate file to validate the OTLP server connection
97+
pub certificate: Option<String>,
98+
99+
/// The path to the certificate file to use for client authentication (mTLS).
100+
pub client_certificate: Option<String>,
101+
102+
/// The path to the key file to use for client authentication (mTLS).
103+
pub client_key: Option<String>,
79104
}
80105

81106
impl Default for ExportConfig {
@@ -88,6 +113,10 @@ impl Default for ExportConfig {
88113
// won't know if user provided a value
89114
protocol,
90115
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
116+
insecure: None,
117+
certificate: None,
118+
client_certificate: None,
119+
client_key: None,
91120
}
92121
}
93122
}
@@ -195,6 +224,17 @@ pub trait WithExportConfig {
195224
fn with_timeout(self, timeout: Duration) -> Self;
196225
/// Set export config. This will override all previous configuration.
197226
fn with_export_config(self, export_config: ExportConfig) -> Self;
227+
/// Set insecure connection. Disable TLS
228+
fn with_insecure(self) -> Self;
229+
/// Set the certificate file to validate the OTLP server connection
230+
/// This is only available when the `tls` feature is enabled.
231+
fn with_certificate<T: Into<String>>(self, certificate: T) -> Self;
232+
/// Set the path to the certificate file to use for client authentication (mTLS).
233+
/// This is only available when the `tls` feature is enabled.
234+
fn with_client_certificate<T: Into<String>>(self, client_certificate: T) -> Self;
235+
/// Set the path to the key file to use for client authentication (mTLS).
236+
/// This is only available when the `tls` feature is enabled.
237+
fn with_client_key<T: Into<String>>(self, client_key: T) -> Self;
198238
}
199239

200240
impl<B: HasExportConfig> WithExportConfig for B {
@@ -217,6 +257,27 @@ impl<B: HasExportConfig> WithExportConfig for B {
217257
self.export_config().endpoint = exporter_config.endpoint;
218258
self.export_config().protocol = exporter_config.protocol;
219259
self.export_config().timeout = exporter_config.timeout;
260+
self.export_config().insecure = Some(true);
261+
self
262+
}
263+
264+
fn with_insecure(mut self) -> Self {
265+
self.export_config().insecure = Some(true);
266+
self
267+
}
268+
269+
fn with_certificate<T: Into<String>>(mut self, certificate: T) -> Self {
270+
self.export_config().certificate = Some(certificate.into());
271+
self
272+
}
273+
274+
fn with_client_certificate<T: Into<String>>(mut self, client_certificate: T) -> Self {
275+
self.export_config().client_certificate = Some(client_certificate.into());
276+
self
277+
}
278+
279+
fn with_client_key<T: Into<String>>(mut self, client_key: T) -> Self {
280+
self.export_config().client_key = Some(client_key.into());
220281
self
221282
}
222283
}

0 commit comments

Comments
 (0)