Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configure TLS with environment variables. #2465

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ tracing = { version = ">=0.1.40", default-features = false }
tracing-core = { version = ">=0.1.33", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false }
url = { version = "2.5", default-features = false }
rcgen = { version = "0.13", features = ["crypto"] }
tempfile = "3.14"
1 change: 1 addition & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"reqwest-blocking-client" features as default, to align with the
specification.
[2516](https://github.com/open-telemetry/opentelemetry-rust/pull/2516)
- TLS configuration via environment variables for GRPC exporters.

## 0.27.0

Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ opentelemetry_sdk = { features = ["trace", "rt-tokio", "testing"], path = "../op
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
futures-util = { workspace = true }
temp-env = { workspace = true }
rcgen = { workspace = true }
tempfile = { workspace = true }

[features]
# telemetry pillars and functions
Expand Down
80 changes: 80 additions & 0 deletions opentelemetry-otlp/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
/// Compression algorithm to use, defaults to none.
pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION";

/// Certificate file to validate the OTLP server connection
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CERTIFICATE";
/// Path to the certificate file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE";
/// Path to the key file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CLIENT_KEY: &str = "OTEL_EXPORTER_OTLP_CLIENT_KEY";
/// Use insecure connection. Disable TLS
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_INSECURE: &str = "OTEL_EXPORTER_OTLP_INSECURE";

#[cfg(feature = "http-json")]
/// Default protocol, using http-json.
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
Expand Down Expand Up @@ -76,6 +89,22 @@

/// The timeout to the collector.
pub timeout: Duration,

/// Disable TLS
#[cfg(feature = "tls")]
pub insecure: Option<bool>,

/// The certificate file to validate the OTLP server connection
#[cfg(feature = "tls")]
pub certificate: Option<String>,

/// The path to the certificate file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub client_certificate: Option<String>,

/// The path to the key file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub client_key: Option<String>,
}

impl Default for ExportConfig {
Expand All @@ -88,6 +117,14 @@
// won't know if user provided a value
protocol,
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
#[cfg(feature = "tls")]
insecure: None,
#[cfg(feature = "tls")]
certificate: None,
#[cfg(feature = "tls")]
client_certificate: None,
#[cfg(feature = "tls")]
client_key: None,
}
}
}
Expand Down Expand Up @@ -195,6 +232,21 @@
fn with_timeout(self, timeout: Duration) -> Self;
/// Set export config. This will override all previous configuration.
fn with_export_config(self, export_config: ExportConfig) -> Self;
/// Set insecure connection. Disable TLS
#[cfg(feature = "tls")]
fn with_insecure(self) -> Self;
/// Set the certificate file to validate the OTLP server connection
/// This is only available when the `tls` feature is enabled.
#[cfg(feature = "tls")]
fn with_certificate<T: Into<String>>(self, certificate: T) -> Self;
jvanz marked this conversation as resolved.
Show resolved Hide resolved
/// Set the path to the certificate file to use for client authentication (mTLS).
/// This is only available when the `tls` feature is enabled.
#[cfg(feature = "tls")]
fn with_client_certificate<T: Into<String>>(self, client_certificate: T) -> Self;
/// Set the path to the key file to use for client authentication (mTLS).
/// This is only available when the `tls` feature is enabled.
#[cfg(feature = "tls")]
fn with_client_key<T: Into<String>>(self, client_key: T) -> Self;
}

impl<B: HasExportConfig> WithExportConfig for B {
Expand All @@ -217,6 +269,34 @@
self.export_config().endpoint = exporter_config.endpoint;
self.export_config().protocol = exporter_config.protocol;
self.export_config().timeout = exporter_config.timeout;
#[cfg(feature = "tls")]
{
self.export_config().insecure = Some(true);
}
self
}

Check warning on line 277 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L272-L277

Added lines #L272 - L277 were not covered by tests

#[cfg(feature = "tls")]
fn with_insecure(mut self) -> Self {
self.export_config().insecure = Some(true);
self
}

Check warning on line 283 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L280-L283

Added lines #L280 - L283 were not covered by tests

#[cfg(feature = "tls")]
fn with_certificate<T: Into<String>>(mut self, certificate: T) -> Self {
self.export_config().certificate = Some(certificate.into());
self
}

Check warning on line 289 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L286-L289

Added lines #L286 - L289 were not covered by tests

#[cfg(feature = "tls")]
fn with_client_certificate<T: Into<String>>(mut self, client_certificate: T) -> Self {
self.export_config().client_certificate = Some(client_certificate.into());
self
}

Check warning on line 295 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L292-L295

Added lines #L292 - L295 were not covered by tests

#[cfg(feature = "tls")]
fn with_client_key<T: Into<String>>(mut self, client_key: T) -> Self {
self.export_config().client_key = Some(client_key.into());

Check warning on line 299 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L298-L299

Added lines #L298 - L299 were not covered by tests
self
}
}
Expand Down
Loading
Loading