Skip to content

Commit 58a3595

Browse files
committed
add testcases for client tls options in runtime config
Signed-off-by: Rajat Jindal <[email protected]>
1 parent e4094d6 commit 58a3595

File tree

1 file changed

+134
-3
lines changed

1 file changed

+134
-3
lines changed

crates/trigger/src/runtime_config.rs

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ mod tests {
533533
}
534534

535535
#[test]
536-
fn test_parsing_valid_hosts_in_client_opts() {
536+
fn test_parsing_valid_hosts_in_client_tls_opts() {
537537
let input = ClientTlsOpts {
538538
component_ids: vec!["component-id-foo".to_string()],
539539
hosts: vec!["fermyon.com".to_string(), "fermyon.com:5443".to_string()],
@@ -549,7 +549,7 @@ mod tests {
549549
}
550550

551551
#[test]
552-
fn test_parsing_empty_hosts_in_client_opts() {
552+
fn test_parsing_empty_hosts_in_client_tls_opts() {
553553
let input = ClientTlsOpts {
554554
component_ids: vec!["component-id-foo".to_string()],
555555
hosts: vec!["".to_string(), "fermyon.com:5443".to_string()],
@@ -568,7 +568,7 @@ mod tests {
568568
}
569569

570570
#[test]
571-
fn test_parsing_invalid_hosts_in_client_opts() {
571+
fn test_parsing_invalid_hosts_in_client_tls_opts() {
572572
let input = ClientTlsOpts {
573573
component_ids: vec!["component-id-foo".to_string()],
574574
hosts: vec!["perc%ent:443".to_string(), "fermyon.com:5443".to_string()],
@@ -586,6 +586,137 @@ mod tests {
586586
)
587587
}
588588

589+
#[test]
590+
fn test_parsing_multiple_client_tls_opts() {
591+
let custom_root_ca = r#"
592+
-----BEGIN CERTIFICATE-----
593+
MIIBeDCCAR2gAwIBAgIBADAKBggqhkjOPQQDAjAjMSEwHwYDVQQDDBhrM3Mtc2Vy
594+
dmVyLWNhQDE3MTc3ODA1MjAwHhcNMjQwNjA3MTcxNTIwWhcNMzQwNjA1MTcxNTIw
595+
WjAjMSEwHwYDVQQDDBhrM3Mtc2VydmVyLWNhQDE3MTc3ODA1MjAwWTATBgcqhkjO
596+
PQIBBggqhkjOPQMBBwNCAAQnhGmz/r5E+ZBgkg/kpeSliS4LjMFaeFNM3C0SUksV
597+
cVDbymRZt+D2loVpSIn9PnBHUIiR9kz+cmWJaJDhcY6Ho0IwQDAOBgNVHQ8BAf8E
598+
BAMCAqQwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUzXLACkzCDPAXXERIxQim
599+
NdG07zEwCgYIKoZIzj0EAwIDSQAwRgIhALwsHX2R7a7GXfgmn7h8rNRRvlQwyRaG
600+
9hyv0a1cyJr2AiEA8+2vF0CZ/S0MG6rT0Y6xZ+iqi/vhcDnmBhJCxx2rwAI=
601+
-----END CERTIFICATE-----
602+
"#;
603+
let mut custom_root_ca_file = NamedTempFile::new().expect("temp file for custom root ca");
604+
custom_root_ca_file
605+
.write_all(custom_root_ca.as_bytes())
606+
.expect("write custom root ca file");
607+
608+
let runtimeconfig_data = format!(
609+
r#"
610+
[[client_tls]]
611+
hosts = ["localhost:6551"]
612+
component_ids = ["component-no1"]
613+
[[client_tls]]
614+
hosts = ["localhost:6551"]
615+
component_ids = ["component-no2"]
616+
ca_roots_file = "{}"
617+
"#,
618+
custom_root_ca_file.path().to_str().unwrap()
619+
);
620+
621+
let mut config = RuntimeConfig::new(None);
622+
merge_config_toml(&mut config, toml::from_str(&runtimeconfig_data).unwrap());
623+
624+
let client_tls_opts = config.client_tls_opts();
625+
assert!(client_tls_opts.is_ok());
626+
627+
//assert that component level mapping works as expected
628+
let client_tls_opts_ok = client_tls_opts.as_ref().unwrap();
629+
630+
// assert for component-no1
631+
assert!(client_tls_opts_ok.get("component-no1").is_some());
632+
633+
let component_no1_client_tls_opts = client_tls_opts_ok
634+
.get("component-no1")
635+
.expect("get opts for component-no1");
636+
assert!(component_no1_client_tls_opts
637+
.get(&"localhost:6551".parse::<Authority>().unwrap())
638+
.is_some());
639+
640+
let component_no1_host_client_tls_opts = component_no1_client_tls_opts
641+
.get(&"localhost:6551".parse::<Authority>().unwrap())
642+
.unwrap();
643+
assert!(component_no1_host_client_tls_opts.custom_root_ca.is_none());
644+
645+
// assert for component-no2
646+
assert!(client_tls_opts_ok.get("component-no2").is_some());
647+
648+
let component_no2_client_tls_opts = client_tls_opts_ok
649+
.get("component-no2")
650+
.expect("get opts for component-no2");
651+
assert!(component_no2_client_tls_opts
652+
.get(&"localhost:6551".parse::<Authority>().unwrap())
653+
.is_some());
654+
655+
let component_no2_host_client_tls_opts = component_no2_client_tls_opts
656+
.get(&"localhost:6551".parse::<Authority>().unwrap())
657+
.unwrap();
658+
assert!(component_no2_host_client_tls_opts.custom_root_ca.is_some())
659+
}
660+
661+
#[test]
662+
fn test_parsing_multiple_overlapping_client_tls_opts() {
663+
let custom_root_ca = r#"
664+
-----BEGIN CERTIFICATE-----
665+
MIIBeDCCAR2gAwIBAgIBADAKBggqhkjOPQQDAjAjMSEwHwYDVQQDDBhrM3Mtc2Vy
666+
dmVyLWNhQDE3MTc3ODA1MjAwHhcNMjQwNjA3MTcxNTIwWhcNMzQwNjA1MTcxNTIw
667+
WjAjMSEwHwYDVQQDDBhrM3Mtc2VydmVyLWNhQDE3MTc3ODA1MjAwWTATBgcqhkjO
668+
PQIBBggqhkjOPQMBBwNCAAQnhGmz/r5E+ZBgkg/kpeSliS4LjMFaeFNM3C0SUksV
669+
cVDbymRZt+D2loVpSIn9PnBHUIiR9kz+cmWJaJDhcY6Ho0IwQDAOBgNVHQ8BAf8E
670+
BAMCAqQwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUzXLACkzCDPAXXERIxQim
671+
NdG07zEwCgYIKoZIzj0EAwIDSQAwRgIhALwsHX2R7a7GXfgmn7h8rNRRvlQwyRaG
672+
9hyv0a1cyJr2AiEA8+2vF0CZ/S0MG6rT0Y6xZ+iqi/vhcDnmBhJCxx2rwAI=
673+
-----END CERTIFICATE-----
674+
"#;
675+
let mut custom_root_ca_file = NamedTempFile::new().expect("temp file for custom root ca");
676+
custom_root_ca_file
677+
.write_all(custom_root_ca.as_bytes())
678+
.expect("write custom root ca file");
679+
680+
let runtimeconfig_data = format!(
681+
r#"
682+
[[client_tls]]
683+
hosts = ["localhost:6551"]
684+
component_ids = ["component-no1"]
685+
[[client_tls]]
686+
hosts = ["localhost:6551"]
687+
component_ids = ["component-no1"]
688+
ca_roots_file = "{}"
689+
"#,
690+
custom_root_ca_file.path().to_str().unwrap()
691+
);
692+
693+
let mut config = RuntimeConfig::new(None);
694+
merge_config_toml(&mut config, toml::from_str(&runtimeconfig_data).unwrap());
695+
696+
let client_tls_opts = config.client_tls_opts();
697+
assert!(client_tls_opts.is_ok());
698+
699+
//assert that component level mapping works as expected
700+
let client_tls_opts_ok = client_tls_opts.as_ref().unwrap();
701+
702+
// assert for component-no1
703+
assert!(client_tls_opts_ok.get("component-no1").is_some());
704+
705+
let component_no1_client_tls_opts = client_tls_opts_ok
706+
.get("component-no1")
707+
.expect("get opts for component-no1");
708+
assert!(component_no1_client_tls_opts
709+
.get(&"localhost:6551".parse::<Authority>().unwrap())
710+
.is_some());
711+
712+
let component_no1_host_client_tls_opts = component_no1_client_tls_opts
713+
.get(&"localhost:6551".parse::<Authority>().unwrap())
714+
.unwrap();
715+
716+
// verify that the last client_tls block wins for same component-id and host combination
717+
assert!(component_no1_host_client_tls_opts.custom_root_ca.is_some());
718+
}
719+
589720
fn merge_config_toml(config: &mut RuntimeConfig, value: toml::Value) {
590721
let data = toml::to_vec(&value).expect("encode toml");
591722
let mut file = NamedTempFile::new().expect("temp file");

0 commit comments

Comments
 (0)