Skip to content

Commit ed1077f

Browse files
committed
parse component-id as spin_serde::KebabId to catch invalid ids early
Signed-off-by: Rajat Jindal <[email protected]>
1 parent 3cad9dd commit ed1077f

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

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/trigger-http/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ struct ChainedRequestHandler {
586586
pub struct HttpRuntimeData {
587587
origin: Option<String>,
588588
chained_handler: Option<ChainedRequestHandler>,
589-
/// If provided, these options used for client cert auth
589+
// Optional mapping of authority and TLS options for the current component
590590
client_tls_opts: Option<HashMap<Authority, ParsedClientTlsOpts>>,
591591
/// The hosts this app is allowed to make outbound requests to
592592
allowed_hosts: AllowedHostsConfig,

crates/trigger/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ rustls-pemfile = "2.1.2"
3232
rustls-pki-types = "1.7.0"
3333
spin-common = { path = "../common" }
3434
spin-expressions = { path = "../expressions" }
35+
spin-serde = { path = "../serde" }
3536
spin-key-value = { path = "../key-value" }
3637
spin-key-value-azure = { path = "../key-value-azure" }
3738
spin-key-value-redis = { path = "../key-value-redis" }

crates/trigger/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<Executor: TriggerExecutor> TriggerAppEngine<Executor> {
442442
&self,
443443
component_id: &str,
444444
) -> Option<HashMap<Authority, ParsedClientTlsOpts>> {
445-
self.client_tls_opts.get(component_id).cloned()
445+
self.client_tls_opts.get(&component_id.to_string()).cloned()
446446
}
447447

448448
pub fn resolve_template(

crates/trigger/src/runtime_config.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl RuntimeConfig {
202202
.into_iter()
203203
.map(|host| (host, parsed.clone()))
204204
.collect::<HashMap<Authority, ParsedClientTlsOpts>>();
205-
components_map.insert(component_id.to_owned(), hostmap);
205+
components_map.insert(component_id.to_string().to_owned(), hostmap);
206206
}
207207
}
208208
}
@@ -532,10 +532,14 @@ mod tests {
532532
Ok(())
533533
}
534534

535+
fn to_component_id(inp: &str) -> spin_serde::KebabId {
536+
spin_serde::KebabId::try_from(inp.to_string()).expect("parse component id into kebab id")
537+
}
538+
535539
#[test]
536540
fn test_parsing_valid_hosts_in_client_tls_opts() {
537541
let input = ClientTlsOpts {
538-
component_ids: vec!["component-id-foo".to_string()],
542+
component_ids: vec![to_component_id("component-id-foo")],
539543
hosts: vec!["fermyon.com".to_string(), "fermyon.com:5443".to_string()],
540544
ca_roots_file: None,
541545
cert_chain_file: None,
@@ -551,7 +555,7 @@ mod tests {
551555
#[test]
552556
fn test_parsing_empty_hosts_in_client_tls_opts() {
553557
let input = ClientTlsOpts {
554-
component_ids: vec!["component-id-foo".to_string()],
558+
component_ids: vec![to_component_id("component-id-foo")],
555559
hosts: vec!["".to_string(), "fermyon.com:5443".to_string()],
556560
ca_roots_file: None,
557561
cert_chain_file: None,
@@ -570,7 +574,7 @@ mod tests {
570574
#[test]
571575
fn test_parsing_invalid_hosts_in_client_tls_opts() {
572576
let input = ClientTlsOpts {
573-
component_ids: vec!["component-id-foo".to_string()],
577+
component_ids: vec![to_component_id("component-id-foo")],
574578
hosts: vec!["perc%ent:443".to_string(), "fermyon.com:5443".to_string()],
575579
ca_roots_file: None,
576580
cert_chain_file: None,
@@ -628,10 +632,12 @@ ca_roots_file = "{}"
628632
let client_tls_opts_ok = client_tls_opts.as_ref().unwrap();
629633

630634
// assert for component-no1
631-
assert!(client_tls_opts_ok.get("component-no1").is_some());
635+
assert!(client_tls_opts_ok
636+
.get(&"component-no1".to_string())
637+
.is_some());
632638

633639
let component_no1_client_tls_opts = client_tls_opts_ok
634-
.get("component-no1")
640+
.get(&"component-no1".to_string())
635641
.expect("get opts for component-no1");
636642
assert!(component_no1_client_tls_opts
637643
.get(&"localhost:6551".parse::<Authority>().unwrap())
@@ -643,10 +649,12 @@ ca_roots_file = "{}"
643649
assert!(component_no1_host_client_tls_opts.custom_root_ca.is_none());
644650

645651
// assert for component-no2
646-
assert!(client_tls_opts_ok.get("component-no2").is_some());
652+
assert!(client_tls_opts_ok
653+
.get(&"component-no2".to_string())
654+
.is_some());
647655

648656
let component_no2_client_tls_opts = client_tls_opts_ok
649-
.get("component-no2")
657+
.get(&"component-no2".to_string())
650658
.expect("get opts for component-no2");
651659
assert!(component_no2_client_tls_opts
652660
.get(&"localhost:6551".parse::<Authority>().unwrap())
@@ -700,10 +708,12 @@ ca_roots_file = "{}"
700708
let client_tls_opts_ok = client_tls_opts.as_ref().unwrap();
701709

702710
// assert for component-no1
703-
assert!(client_tls_opts_ok.get("component-no1").is_some());
711+
assert!(client_tls_opts_ok
712+
.get(&"component-no1".to_string())
713+
.is_some());
704714

705715
let component_no1_client_tls_opts = client_tls_opts_ok
706-
.get("component-no1")
716+
.get(&"component-no1".to_string())
707717
.expect("get opts for component-no1");
708718
assert!(component_no1_client_tls_opts
709719
.get(&"localhost:6551".parse::<Authority>().unwrap())
@@ -783,9 +793,16 @@ fn parse_client_tls_opts(inp: &ClientTlsOpts) -> Result<ParsedClientTlsOpts, any
783793
inp.ca_webpki_roots
784794
.unwrap_or(if custom_root_ca_provided { false } else { true });
785795

796+
let parsed_component_ids: Vec<String> = inp
797+
.component_ids
798+
.clone()
799+
.into_iter()
800+
.map(|s| s.to_string())
801+
.collect();
802+
786803
Ok(ParsedClientTlsOpts {
787804
hosts: parsed_hosts,
788-
components: inp.component_ids.clone(),
805+
components: parsed_component_ids,
789806
custom_root_ca,
790807
cert_chain,
791808
private_key,

crates/trigger/src/runtime_config/client_tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
#[derive(Debug, serde::Deserialize)]
1111
#[serde(rename_all = "snake_case", tag = "type")]
1212
pub struct ClientTlsOpts {
13-
pub component_ids: Vec<String>,
13+
pub component_ids: Vec<spin_serde::KebabId>,
1414
pub hosts: Vec<String>,
1515
pub ca_roots_file: Option<PathBuf>,
1616
pub cert_chain_file: Option<PathBuf>,

0 commit comments

Comments
 (0)