-
Notifications
You must be signed in to change notification settings - Fork 289
Expand wildcard service chaining during spin up
#3266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
itowlson
merged 1 commit into
spinframework:main
from
itowlson:expand-service-chaining-wildcard
Sep 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -465,12 +465,14 @@ impl AllowedHostsConfig { | |
| pub fn parse<S: AsRef<str>>( | ||
| hosts: &[S], | ||
| resolver: &spin_expressions::PreparedResolver, | ||
| component_ids: &[String], | ||
| ) -> anyhow::Result<AllowedHostsConfig> { | ||
| let partial = Self::parse_partial(hosts)?; | ||
| let allowed = partial | ||
| .into_iter() | ||
| .map(|p| p.resolve(resolver)) | ||
| .collect::<anyhow::Result<Vec<_>>>()?; | ||
| let allowed = Self::expand_wildcard_service_chaining(allowed, component_ids); | ||
| Ok(Self::SpecificHosts(allowed)) | ||
| } | ||
|
|
||
|
|
@@ -502,6 +504,28 @@ impl AllowedHostsConfig { | |
| Ok(allowed) | ||
| } | ||
|
|
||
| fn expand_wildcard_service_chaining( | ||
| hosts: Vec<AllowedHostConfig>, | ||
| component_ids: &[String], | ||
| ) -> Vec<AllowedHostConfig> { | ||
| let expand_one = |host: AllowedHostConfig| match host.host() { | ||
| HostConfig::AnySubdomain(domain) if domain == SERVICE_CHAINING_DOMAIN_SUFFIX => { | ||
| let expanded_domains = component_ids | ||
| .iter() | ||
| .map(|c| format!("{c}{SERVICE_CHAINING_DOMAIN_SUFFIX}")); | ||
| let expanded_hosts = expanded_domains.map(|d| { | ||
| let mut hh = host.clone(); | ||
| hh.host = HostConfig::Literal(url::Host::Domain(d)); | ||
| hh | ||
| }); | ||
| expanded_hosts.collect() | ||
| } | ||
| _ => vec![host], | ||
| }; | ||
|
|
||
| hosts.into_iter().flat_map(expand_one).collect() | ||
| } | ||
|
|
||
| /// Returns true if the given url is allowed. | ||
| pub fn allows(&self, url: &OutboundUrl) -> bool { | ||
| match self { | ||
|
|
@@ -901,10 +925,13 @@ mod test { | |
|
|
||
| #[test] | ||
| fn test_allowed_hosts_respects_allow_all() { | ||
| assert!(AllowedHostsConfig::parse(&["insecure:allow-all"], &dummy_resolver()).is_err()); | ||
| assert!( | ||
| AllowedHostsConfig::parse(&["insecure:allow-all"], &dummy_resolver(), &[]).is_err() | ||
| ); | ||
| assert!(AllowedHostsConfig::parse( | ||
| &["spin.fermyon.dev", "insecure:allow-all"], | ||
| &dummy_resolver() | ||
| &dummy_resolver(), | ||
| &[] | ||
| ) | ||
| .is_err()); | ||
| } | ||
|
|
@@ -927,6 +954,7 @@ mod test { | |
| let allowed = AllowedHostsConfig::parse( | ||
| &["*://spin.fermyon.dev:443", "http://example.com:8383"], | ||
| &dummy_resolver(), | ||
| &[], | ||
| ) | ||
| .unwrap(); | ||
| assert!( | ||
|
|
@@ -944,7 +972,7 @@ mod test { | |
| #[test] | ||
| fn test_allowed_hosts_with_trailing_slash() { | ||
| let allowed = | ||
| AllowedHostsConfig::parse(&["https://my.api.com/"], &dummy_resolver()).unwrap(); | ||
| AllowedHostsConfig::parse(&["https://my.api.com/"], &dummy_resolver(), &[]).unwrap(); | ||
| assert!(allowed.allows(&OutboundUrl::parse("https://my.api.com", "https").unwrap())); | ||
| assert!(allowed.allows(&OutboundUrl::parse("https://my.api.com/", "https").unwrap())); | ||
| } | ||
|
|
@@ -954,6 +982,7 @@ mod test { | |
| let allowed = AllowedHostsConfig::parse( | ||
| &["http://*.example.com", "http://*.example2.com:8383"], | ||
| &dummy_resolver(), | ||
| &[], | ||
| ) | ||
| .unwrap(); | ||
| assert!( | ||
|
|
@@ -976,7 +1005,8 @@ mod test { | |
|
|
||
| #[test] | ||
| fn test_hash_char_in_db_password() { | ||
| let allowed = AllowedHostsConfig::parse(&["mysql://xyz.com"], &dummy_resolver()).unwrap(); | ||
| let allowed = | ||
| AllowedHostsConfig::parse(&["mysql://xyz.com"], &dummy_resolver(), &[]).unwrap(); | ||
| assert!( | ||
| allowed.allows(&OutboundUrl::parse("mysql://user:pass#[email protected]", "mysql").unwrap()) | ||
| ); | ||
|
|
@@ -988,7 +1018,66 @@ mod test { | |
| #[test] | ||
| fn test_cidr() { | ||
| let allowed = | ||
| AllowedHostsConfig::parse(&["*://127.0.0.1/24:63551"], &dummy_resolver()).unwrap(); | ||
| AllowedHostsConfig::parse(&["*://127.0.0.1/24:63551"], &dummy_resolver(), &[]).unwrap(); | ||
| assert!(allowed.allows(&OutboundUrl::parse("tcp://127.0.0.1:63551", "tcp").unwrap())); | ||
| } | ||
|
|
||
| fn exact_host(ahc: &AllowedHostConfig) -> String { | ||
| match ahc.host() { | ||
| HostConfig::Literal(host) => host.to_string(), | ||
| _ => panic!("expected host {:?} to be a literal", ahc.host()), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn expand_wildcard_service_chaining_lists_all_components() { | ||
| let component_ids = ["first", "second", "third"] | ||
| .iter() | ||
| .map(|s| s.to_string()) | ||
| .collect::<Vec<_>>(); | ||
| let allowed = AllowedHostsConfig::parse( | ||
| &["http://*.spin.internal"], | ||
| &dummy_resolver(), | ||
| &component_ids, | ||
| ) | ||
| .unwrap(); | ||
| let AllowedHostsConfig::SpecificHosts(allowed) = allowed else { | ||
| panic!("expanded AllowedHostsConfig should be specific hosts"); | ||
| }; | ||
|
|
||
| assert_eq!(3, allowed.len()); | ||
|
|
||
| assert_eq!("first.spin.internal", exact_host(&allowed[0])); | ||
| assert_eq!("second.spin.internal", exact_host(&allowed[1])); | ||
| assert_eq!("third.spin.internal", exact_host(&allowed[2])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn expand_wildcard_service_chaining_leaves_others_untouched() { | ||
| let component_ids = ["first", "second", "third"] | ||
| .iter() | ||
| .map(|s| s.to_string()) | ||
| .collect::<Vec<_>>(); | ||
| let allowed = AllowedHostsConfig::parse( | ||
| &[ | ||
| "pg://localhost:5656", | ||
| "http://*.spin.internal", | ||
| "https://spinframework.dev", | ||
| ], | ||
| &dummy_resolver(), | ||
| &component_ids, | ||
| ) | ||
| .unwrap(); | ||
| let AllowedHostsConfig::SpecificHosts(allowed) = allowed else { | ||
| panic!("expanded AllowedHostsConfig should be specific hosts"); | ||
| }; | ||
|
|
||
| assert_eq!(5, allowed.len()); | ||
|
|
||
| assert_eq!("localhost", exact_host(&allowed[0])); | ||
| assert_eq!("first.spin.internal", exact_host(&allowed[1])); | ||
| assert_eq!("second.spin.internal", exact_host(&allowed[2])); | ||
| assert_eq!("third.spin.internal", exact_host(&allowed[3])); | ||
| assert_eq!("spinframework.dev", exact_host(&allowed[4])); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can the copy be avoided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't yet found a way to do so. This was what I originally tried. Unfortunately the compiler doesn't like it because it reckons the
PrepareContextthat the IDs are borrowed from doesn't live long enough (although this may be a spurious report from its other error which is that it results in the PrepareContext having mutable and immutable borrows at the same time). I tried it again and still no joy.I don't trust my expertise enough to say the copy can't be avoided. It sure seems like it should be possible. But I haven't managed to find a way. I'll merge as is, but if you do spot a way, I'm ready to learn!