Skip to content

Commit d7e156e

Browse files
test: update otel integration tests
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
1 parent a4699ac commit d7e156e

File tree

3 files changed

+102
-11
lines changed

3 files changed

+102
-11
lines changed

tests/common/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub(crate) fn default_test_config() -> Config {
121121
log_fmt: "json".to_owned(),
122122
log_no_color: false,
123123
otlp_endpoint: None,
124+
otlp_tls_config: Default::default(),
124125
daemon: false,
125126
daemon_pid_file: "policy_server.pid".to_owned(),
126127
daemon_stdout_file: None,

tests/data/otel-collector-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ receivers:
55
otlp:
66
protocols:
77
grpc:
8+
tls:
9+
ca_file: "certs/server-ca.pem"
10+
cert_file: "certs/server-cert.pem"
11+
key_file: "certs/server-key.pem"
12+
client_ca_file: "certs/client-ca.pem"
813

914
exporters:
1015
file/metrics:

tests/integration_test.rs

+96-11
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ use policy_evaluator::{
2323
admission_response::AdmissionResponseStatus,
2424
policy_fetcher::verify::config::VerificationConfigV1,
2525
};
26+
use policy_server::config::OtlpTlsConfig;
2627
use policy_server::{
2728
api::admission_review::AdmissionReviewResponse,
2829
config::{PolicyMode, PolicyOrPolicyGroup},
2930
metrics::setup_metrics,
3031
tracing::setup_tracing,
3132
};
33+
use rcgen::{BasicConstraints, CertificateParams, DnType, IsCa, KeyPair};
3234
use regex::Regex;
3335
use rstest::*;
3436
use tempfile::NamedTempFile;
@@ -37,6 +39,7 @@ use testcontainers::{
3739
runners::AsyncRunner,
3840
GenericImage, ImageExt,
3941
};
42+
use tokio::fs;
4043
use tower::ServiceExt;
4144

4245
use crate::common::default_test_config;
@@ -746,18 +749,51 @@ async fn test_detect_certificate_rotation() {
746749
async fn test_otel() {
747750
setup();
748751

749-
let mut otelc_config_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
750-
otelc_config_path.push("tests/data/otel-collector-config.yaml");
752+
let otelc_config_path =
753+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/otel-collector-config.yaml");
751754

752-
let metrics_output_file = NamedTempFile::new().unwrap();
753-
let metrics_output_file_path = metrics_output_file.path();
755+
let (server_ca, server_cert, server_key) = generate_tls_certs();
756+
let (client_ca, client_cert, client_key) = generate_tls_certs();
757+
758+
let server_ca_file = NamedTempFile::new().unwrap();
759+
let server_cert_file = NamedTempFile::new().unwrap();
760+
let server_key_file = NamedTempFile::new().unwrap();
761+
762+
let client_ca_file = NamedTempFile::new().unwrap();
763+
let client_cert_file = NamedTempFile::new().unwrap();
764+
let client_key_file = NamedTempFile::new().unwrap();
765+
766+
let files_and_contents = [
767+
(server_ca_file.path(), &server_ca),
768+
(server_cert_file.path(), &server_cert),
769+
(server_key_file.path(), &server_key),
770+
(client_ca_file.path(), &client_ca),
771+
(client_cert_file.path(), &client_cert),
772+
(client_key_file.path(), &client_key),
773+
];
774+
775+
for (file_path, content) in &files_and_contents {
776+
fs::write(file_path, content).await.unwrap();
777+
}
754778

779+
let metrics_output_file = NamedTempFile::new().unwrap();
755780
let traces_output_file = NamedTempFile::new().unwrap();
756-
let traces_output_file_path = traces_output_file.path();
757781

758782
let permissions = Permissions::from_mode(0o666);
759-
set_permissions(metrics_output_file_path, permissions.clone()).unwrap();
760-
set_permissions(traces_output_file_path, permissions).unwrap();
783+
let files_to_set_permissions = [
784+
metrics_output_file.path(),
785+
traces_output_file.path(),
786+
server_ca_file.path(),
787+
server_cert_file.path(),
788+
server_key_file.path(),
789+
client_ca_file.path(),
790+
client_cert_file.path(),
791+
client_key_file.path(),
792+
];
793+
794+
for file_path in &files_to_set_permissions {
795+
set_permissions(file_path, permissions.clone()).unwrap();
796+
}
761797

762798
let otelc = GenericImage::new("otel/opentelemetry-collector", "0.98.0")
763799
.with_wait_for(WaitFor::message_on_stderr("Everything is ready"))
@@ -766,13 +802,29 @@ async fn test_otel() {
766802
"/etc/otel-collector-config.yaml",
767803
))
768804
.with_mount(Mount::bind_mount(
769-
metrics_output_file_path.to_str().unwrap(),
805+
metrics_output_file.path().to_str().unwrap(),
770806
"/tmp/metrics.json",
771807
))
772808
.with_mount(Mount::bind_mount(
773-
traces_output_file_path.to_str().unwrap(),
809+
traces_output_file.path().to_str().unwrap(),
774810
"/tmp/traces.json",
775811
))
812+
.with_mount(Mount::bind_mount(
813+
server_ca_file.path().to_str().unwrap(),
814+
"/certs/server-ca.pem",
815+
))
816+
.with_mount(Mount::bind_mount(
817+
server_cert_file.path().to_str().unwrap(),
818+
"/certs/server-cert.pem",
819+
))
820+
.with_mount(Mount::bind_mount(
821+
server_key_file.path().to_str().unwrap(),
822+
"/certs/server-key.pem",
823+
))
824+
.with_mount(Mount::bind_mount(
825+
client_ca_file.path().to_str().unwrap(),
826+
"/certs/client-ca.pem",
827+
))
776828
.with_mapped_port(1337, 4317.into())
777829
.with_cmd(vec!["--config=/etc/otel-collector-config.yaml"])
778830
.with_startup_timeout(Duration::from_secs(30))
@@ -783,13 +835,24 @@ async fn test_otel() {
783835
let mut config = default_test_config();
784836
config.metrics_enabled = true;
785837
config.log_fmt = "otlp".to_string();
786-
config.otlp_endpoint = Some("http://localhost:1337".to_string());
787-
setup_metrics(config.otlp_endpoint.as_deref()).unwrap();
838+
config.otlp_endpoint = Some("https://localhost:1337".to_string());
839+
config.otlp_tls_config = OtlpTlsConfig {
840+
ca_file: Some(server_ca_file.path().to_owned()),
841+
cert_file: Some(client_cert_file.path().to_owned()),
842+
key_file: Some(client_key_file.path().to_owned()),
843+
};
844+
845+
setup_metrics(
846+
config.otlp_endpoint.as_deref(),
847+
config.otlp_tls_config.clone(),
848+
)
849+
.unwrap();
788850
setup_tracing(
789851
&config.log_level,
790852
&config.log_fmt,
791853
config.log_no_color,
792854
config.otlp_endpoint.as_deref(),
855+
config.otlp_tls_config.clone(),
793856
)
794857
.unwrap();
795858

@@ -862,3 +925,25 @@ async fn parse_exporter_output(
862925

863926
serde_json::from_str(&exporter_output)
864927
}
928+
929+
fn generate_tls_certs() -> (String, String, String) {
930+
let ca_key = KeyPair::generate().unwrap();
931+
let mut params = CertificateParams::new(vec!["My Test CA".to_string()]).unwrap();
932+
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
933+
let ca_cert = params.self_signed(&ca_key).unwrap();
934+
let ca_cert_pem = ca_cert.pem();
935+
936+
let mut params = CertificateParams::new(vec!["localhost".to_string()]).unwrap();
937+
params
938+
.distinguished_name
939+
.push(DnType::OrganizationName, "Kubewarden");
940+
params
941+
.distinguished_name
942+
.push(DnType::CommonName, "kubewarden.io");
943+
944+
let cert_key = KeyPair::generate().unwrap();
945+
let cert = params.signed_by(&cert_key, &ca_cert, &ca_key).unwrap();
946+
let key = cert_key.serialize_pem();
947+
948+
(ca_cert_pem, cert.pem(), key)
949+
}

0 commit comments

Comments
 (0)