@@ -23,12 +23,14 @@ use policy_evaluator::{
23
23
admission_response:: AdmissionResponseStatus ,
24
24
policy_fetcher:: verify:: config:: VerificationConfigV1 ,
25
25
} ;
26
+ use policy_server:: config:: OtlpTlsConfig ;
26
27
use policy_server:: {
27
28
api:: admission_review:: AdmissionReviewResponse ,
28
29
config:: { PolicyMode , PolicyOrPolicyGroup } ,
29
30
metrics:: setup_metrics,
30
31
tracing:: setup_tracing,
31
32
} ;
33
+ use rcgen:: { BasicConstraints , CertificateParams , DnType , IsCa , KeyPair } ;
32
34
use regex:: Regex ;
33
35
use rstest:: * ;
34
36
use tempfile:: NamedTempFile ;
@@ -37,6 +39,7 @@ use testcontainers::{
37
39
runners:: AsyncRunner ,
38
40
GenericImage , ImageExt ,
39
41
} ;
42
+ use tokio:: fs;
40
43
use tower:: ServiceExt ;
41
44
42
45
use crate :: common:: default_test_config;
@@ -746,18 +749,51 @@ async fn test_detect_certificate_rotation() {
746
749
async fn test_otel ( ) {
747
750
setup ( ) ;
748
751
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" ) ;
751
754
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
+ }
754
778
779
+ let metrics_output_file = NamedTempFile :: new ( ) . unwrap ( ) ;
755
780
let traces_output_file = NamedTempFile :: new ( ) . unwrap ( ) ;
756
- let traces_output_file_path = traces_output_file. path ( ) ;
757
781
758
782
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
+ }
761
797
762
798
let otelc = GenericImage :: new ( "otel/opentelemetry-collector" , "0.98.0" )
763
799
. with_wait_for ( WaitFor :: message_on_stderr ( "Everything is ready" ) )
@@ -766,13 +802,29 @@ async fn test_otel() {
766
802
"/etc/otel-collector-config.yaml" ,
767
803
) )
768
804
. with_mount ( Mount :: bind_mount (
769
- metrics_output_file_path . to_str ( ) . unwrap ( ) ,
805
+ metrics_output_file . path ( ) . to_str ( ) . unwrap ( ) ,
770
806
"/tmp/metrics.json" ,
771
807
) )
772
808
. with_mount ( Mount :: bind_mount (
773
- traces_output_file_path . to_str ( ) . unwrap ( ) ,
809
+ traces_output_file . path ( ) . to_str ( ) . unwrap ( ) ,
774
810
"/tmp/traces.json" ,
775
811
) )
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
+ ) )
776
828
. with_mapped_port ( 1337 , 4317 . into ( ) )
777
829
. with_cmd ( vec ! [ "--config=/etc/otel-collector-config.yaml" ] )
778
830
. with_startup_timeout ( Duration :: from_secs ( 30 ) )
@@ -783,13 +835,24 @@ async fn test_otel() {
783
835
let mut config = default_test_config ( ) ;
784
836
config. metrics_enabled = true ;
785
837
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 ( ) ;
788
850
setup_tracing (
789
851
& config. log_level ,
790
852
& config. log_fmt ,
791
853
config. log_no_color ,
792
854
config. otlp_endpoint . as_deref ( ) ,
855
+ config. otlp_tls_config . clone ( ) ,
793
856
)
794
857
. unwrap ( ) ;
795
858
@@ -862,3 +925,25 @@ async fn parse_exporter_output(
862
925
863
926
serde_json:: from_str ( & exporter_output)
864
927
}
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