Skip to content

Commit 25bbcf9

Browse files
committed
fix(metrics): address CI validation failures
Signed-off-by: Gaizka Menendez Hernandez <[email protected]>
1 parent 388464a commit 25bbcf9

4 files changed

Lines changed: 44 additions & 26 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-server/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ protoc-bin-vendored = { workspace = true }
133133

134134
[dev-dependencies]
135135
tracing-core = "0.1"
136+
rustls-pki-types = "1.9"
136137
tokio = { workspace = true, features = ["test-util"] }
137138
# Tests import the example profiles from providers/ the way an operator
138139
# would; the feature is test-only and never reaches a release binary.

‎crates/openshell-server/src/cli.rs‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,6 @@ mod tests {
16451645
use clap::Parser;
16461646
use openshell_core::config::ServiceHostnameFormat;
16471647
use std::net::{IpAddr, Ipv4Addr};
1648-
use std::path::PathBuf;
16491648
use std::sync::atomic::{AtomicUsize, Ordering};
16501649

16511650
#[test]
@@ -3269,9 +3268,12 @@ require_client_auth = true
32693268
.expect("valid metrics TLS configuration")
32703269
.expect("metrics TLS is configured");
32713270

3272-
assert_eq!(tls.cert_path, PathBuf::from("/metrics/tls.crt"));
3273-
assert_eq!(tls.key_path, PathBuf::from("/metrics/tls.key"));
3274-
assert_eq!(tls.client_ca_path, Some(PathBuf::from("/metrics/ca.crt")));
3271+
assert_eq!(tls.cert_path, std::path::PathBuf::from("/metrics/tls.crt"));
3272+
assert_eq!(tls.key_path, std::path::PathBuf::from("/metrics/tls.key"));
3273+
assert_eq!(
3274+
tls.client_ca_path,
3275+
Some(std::path::PathBuf::from("/metrics/ca.crt"))
3276+
);
32753277
assert!(tls.require_client_auth);
32763278
});
32773279
}
@@ -3371,6 +3373,21 @@ require_client_auth = true
33713373
.contains("metrics TLS requires --metrics-tls-cert"),
33723374
"error: {error}"
33733375
);
3376+
3377+
let (args, _) = parse_with_args(&[
3378+
"openshell-gateway",
3379+
"--db-url",
3380+
"sqlite::memory:",
3381+
"--metrics-require-client-auth",
3382+
]);
3383+
let error = resolve_metrics_tls(&args)
3384+
.expect_err("client authentication alone must enable metrics TLS validation");
3385+
assert!(
3386+
error
3387+
.to_string()
3388+
.contains("metrics TLS requires --metrics-tls-cert"),
3389+
"error: {error}"
3390+
);
33743391
});
33753392
}
33763393

@@ -3401,12 +3418,11 @@ require_client_auth = true
34013418
]);
34023419
let registry = test_registry("podman", true);
34033420

3404-
let error =
3405-
match super::prepare_server_config_with_drivers(&mut args, &matches, &registry)
3406-
{
3407-
Err(error) => error,
3408-
Ok(_) => panic!("metrics TLS without a listener must be rejected"),
3409-
};
3421+
let Err(error) =
3422+
super::prepare_server_config_with_drivers(&mut args, &matches, &registry)
3423+
else {
3424+
panic!("metrics TLS without a listener must be rejected");
3425+
};
34103426
assert!(
34113427
error
34123428
.to_string()
@@ -3517,7 +3533,7 @@ require_client_auth = true
35173533

35183534
assert_eq!(
35193535
super::resolve_config_path(&args).unwrap(),
3520-
Some(PathBuf::from("/tmp/missing.toml"))
3536+
Some(std::path::PathBuf::from("/tmp/missing.toml"))
35213537
);
35223538
});
35233539
}

‎crates/openshell-server/src/lib.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ fn is_benign_tls_handshake_failure(error: &std::io::Error) -> bool {
440440
)
441441
}
442442

443-
const METRICS_TLS_HANDSHAKE_WARNING_INTERVAL: Duration = Duration::from_secs(60);
443+
const METRICS_TLS_HANDSHAKE_WARNING_INTERVAL: Duration = Duration::from_mins(1);
444444

445445
#[derive(Default)]
446446
struct MetricsTlsHandshakeLogState {
@@ -2486,8 +2486,9 @@ mod tests {
24862486
proto::{HealthRequest, open_shell_client::OpenShellClient},
24872487
};
24882488
use rcgen::{CertificateParams, KeyUsagePurpose};
2489-
use std::fs::{self, File};
2490-
use std::io::{BufReader, Error, ErrorKind};
2489+
use rustls_pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject};
2490+
use std::fs;
2491+
use std::io::{Error, ErrorKind};
24912492
use std::net::SocketAddr;
24922493
use std::path::{Path, PathBuf};
24932494
use std::sync::{
@@ -2520,22 +2521,21 @@ mod tests {
25202521

25212522
fn test_tls_connector(ca_path: &Path, client_auth: Option<(&Path, &Path)>) -> TlsConnector {
25222523
let mut roots = rustls::RootCertStore::empty();
2523-
let file = File::open(ca_path).expect("failed to open test CA certificate");
2524-
for certificate in rustls_pemfile::certs(&mut BufReader::new(file)) {
2524+
let certificates =
2525+
CertificateDer::pem_file_iter(ca_path).expect("failed to open test CA certificate");
2526+
for certificate in certificates {
25252527
roots
25262528
.add(certificate.expect("failed to parse test CA certificate"))
25272529
.expect("failed to add test CA certificate");
25282530
}
25292531

25302532
let config = if let Some((cert_path, key_path)) = client_auth {
2531-
let cert_file = File::open(cert_path).expect("failed to open test client certificate");
2532-
let cert_chain = rustls_pemfile::certs(&mut BufReader::new(cert_file))
2533+
let cert_chain = CertificateDer::pem_file_iter(cert_path)
2534+
.expect("failed to open test client certificate")
25332535
.collect::<Result<Vec<_>, _>>()
25342536
.expect("failed to parse test client certificate");
2535-
let key_file = File::open(key_path).expect("failed to open test client key");
2536-
let private_key = rustls_pemfile::private_key(&mut BufReader::new(key_file))
2537-
.expect("failed to parse test client key")
2538-
.expect("test client key is missing");
2537+
let private_key =
2538+
PrivateKeyDer::from_pem_file(key_path).expect("failed to parse test client key");
25392539

25402540
Arc::new(
25412541
rustls::ClientConfig::builder()
@@ -3118,7 +3118,7 @@ mod tests {
31183118
.with_filter(tracing_subscriber::filter::LevelFilter::DEBUG),
31193119
);
31203120
let _traced = crate::otel_tracing::test_exporter::install_scoped(subscriber);
3121-
let limiter = MetricsTlsHandshakeLogLimiter::new(Duration::from_secs(60));
3121+
let limiter = MetricsTlsHandshakeLogLimiter::new(Duration::from_mins(1));
31223122

31233123
log_metrics_tls_handshake_rejection(
31243124
&limiter,
@@ -3146,14 +3146,14 @@ mod tests {
31463146

31473147
#[test]
31483148
fn metrics_tls_handshake_warnings_are_rate_limited() {
3149-
let limiter = MetricsTlsHandshakeLogLimiter::new(Duration::from_secs(60));
3149+
let limiter = MetricsTlsHandshakeLogLimiter::new(Duration::from_mins(1));
31503150
let start = Instant::now();
31513151

31523152
assert_eq!(limiter.record(start), Some(0));
31533153
assert_eq!(limiter.record(start + Duration::from_secs(1)), None);
31543154
assert_eq!(limiter.record(start + Duration::from_secs(2)), None);
31553155
assert_eq!(
3156-
limiter.record(start + Duration::from_secs(60)),
3156+
limiter.record(start + Duration::from_mins(1)),
31573157
Some(2),
31583158
"the next warning reports the rejected handshakes suppressed during the interval"
31593159
);
@@ -3172,7 +3172,7 @@ mod tests {
31723172
.with_filter(tracing_subscriber::filter::LevelFilter::DEBUG),
31733173
);
31743174
let _traced = crate::otel_tracing::test_exporter::install_scoped(subscriber);
3175-
let limiters = MetricsTlsHandshakeLogLimiters::new(Duration::from_secs(60));
3175+
let limiters = MetricsTlsHandshakeLogLimiters::new(Duration::from_mins(1));
31763176
let client = "192.0.2.10:443".parse().expect("valid test client address");
31773177

31783178
// A rejected handshake warns once and suppresses further rejections

0 commit comments

Comments
 (0)