From a8755032f48d346eb1b85dd9029fe01d4853e041 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Tue, 31 Oct 2023 11:24:04 +0100 Subject: [PATCH] Don't require a port Signed-off-by: Ryan Levick --- crates/outbound-networking/src/lib.rs | 40 ++++++++++++++----- examples/http-rust-outbound-http/spin.toml | 2 +- examples/rust-outbound-mysql/spin.toml | 2 +- examples/rust-outbound-pg/spin.toml | 2 +- examples/rust-outbound-redis/spin.toml | 2 +- examples/spin-timer/Cargo.lock | 1 + sdk/go/http/testdata/spin-roundtrip/spin.toml | 2 +- sdk/rust/readme.md | 2 +- .../http-rust-outbound-mysql/spin.toml | 2 +- .../testcases/http-rust-outbound-pg/spin.toml | 2 +- 10 files changed, 38 insertions(+), 19 deletions(-) diff --git a/crates/outbound-networking/src/lib.rs b/crates/outbound-networking/src/lib.rs index a277a12be7..2b5052af98 100644 --- a/crates/outbound-networking/src/lib.rs +++ b/crates/outbound-networking/src/lib.rs @@ -64,9 +64,13 @@ impl AddressConfig { let host_and_port = &url[(scheme_end + 3)..]; let host_end = host_and_port .find(':') - .with_context(|| format!("{url:?} does not contain port"))?; + .or_else(|| host_and_port.find('/')) + .unwrap_or(host_and_port.len()); let host = &host_and_port[..host_end]; - let port_and_path = &host_and_port[host_end + 1..]; + let port_and_path = match host_and_port.find(':') { + Some(colon) => &host_and_port[colon + 1..], + None => &host_and_port[host.len()..], + }; let port = match port_and_path.find('/') { Some(s) => { if &port_and_path[s..] != "/" { @@ -80,7 +84,7 @@ impl AddressConfig { Ok(Self { scheme: SchemeConfig::parse(scheme)?, host: HostConfig::parse(host)?, - port: PortConfig::parse(port)?, + port: PortConfig::parse(port, scheme)?, original, }) } @@ -171,7 +175,12 @@ enum PortConfig { } impl PortConfig { - fn parse(port: &str) -> anyhow::Result { + fn parse(port: &str, scheme: &str) -> anyhow::Result { + if port.is_empty() { + return well_known_port(scheme) + .map(|p| PortConfig::List(vec![IndividualPortConfig::Port(p)])) + .with_context(|| format!("no port was provided and the scheme {scheme:?} does not have a known default port number")); + } if port == "*" { return Ok(PortConfig::Any); } @@ -389,11 +398,6 @@ mod test { use super::*; - #[test] - fn test_allowed_hosts_rejects_url_without_port() { - assert!(AllowedHostConfig::parse("http://spin.fermyon.dev").is_err()); - } - #[test] fn test_allowed_hosts_accepts_url_with_port() { assert_eq!( @@ -467,7 +471,14 @@ mod test { #[test] fn test_allowed_hosts_accepts_localhost_addresses() { assert!(AllowedHostConfig::parse("localhost").is_err()); - assert!(AllowedHostConfig::parse("http://localhost").is_err()); + assert_eq!( + AllowedHostConfig::new( + SchemeConfig::new("http"), + HostConfig::new("localhost"), + PortConfig::new(80) + ), + AllowedHostConfig::parse("http://localhost").unwrap() + ); assert!(AllowedHostConfig::parse("localhost:3001").is_err()); assert_eq!( AllowedHostConfig::new( @@ -481,7 +492,14 @@ mod test { #[test] fn test_allowed_hosts_accepts_ip_addresses() { - assert!(AllowedHostConfig::parse("http://192.168.1.1").is_err()); + assert_eq!( + AllowedHostConfig::new( + SchemeConfig::new("http"), + HostConfig::new("192.168.1.1"), + PortConfig::new(80) + ), + AllowedHostConfig::parse("http://192.168.1.1").unwrap() + ); assert_eq!( AllowedHostConfig::new( SchemeConfig::new("http"), diff --git a/examples/http-rust-outbound-http/spin.toml b/examples/http-rust-outbound-http/spin.toml index 26245d76b3..0c2728d9c1 100644 --- a/examples/http-rust-outbound-http/spin.toml +++ b/examples/http-rust-outbound-http/spin.toml @@ -25,7 +25,7 @@ component = "hello-component" [component.outbound-http] source = "outbound-http/target/wasm32-wasi/release/http_rust_outbound_http.wasm" allowed_http_hosts = ["foo.com"] -allowed_outbound_hosts = ["https://random-data-api.fermyon.app:443"] +allowed_outbound_hosts = ["https://random-data-api.fermyon.app"] [component.outbound-http.build] workdir = "outbound-http" command = "cargo build --target wasm32-wasi --release" diff --git a/examples/rust-outbound-mysql/spin.toml b/examples/rust-outbound-mysql/spin.toml index 16218c415d..ac1855931f 100644 --- a/examples/rust-outbound-mysql/spin.toml +++ b/examples/rust-outbound-mysql/spin.toml @@ -13,6 +13,6 @@ component = "rust-outbound-mysql" [component.rust-outbound-mysql] environment = { DB_URL = "mysql://spin:spin@127.0.0.1/spin_dev" } source = "target/wasm32-wasi/release/rust_outbound_mysql.wasm" -allowed_outbound_hosts = ["mysql://127.0.0.1:3306"] +allowed_outbound_hosts = ["mysql://127.0.0.1"] [component.rust-outbound-mysql.build] command = "cargo build --target wasm32-wasi --release" diff --git a/examples/rust-outbound-pg/spin.toml b/examples/rust-outbound-pg/spin.toml index a288a4e8df..4dc305653d 100644 --- a/examples/rust-outbound-pg/spin.toml +++ b/examples/rust-outbound-pg/spin.toml @@ -12,6 +12,6 @@ component = "outbound-pg" [component.outbound-pg] environment = { DB_URL = "host=localhost user=postgres dbname=spin_dev" } source = "target/wasm32-wasi/release/rust_outbound_pg.wasm" -allowed_outbound_hosts = ["postgres://localhost:5432"] +allowed_outbound_hosts = ["postgres://localhost"] [component.outbound-pg.build] command = "cargo build --target wasm32-wasi --release" diff --git a/examples/rust-outbound-redis/spin.toml b/examples/rust-outbound-redis/spin.toml index f5c64714a7..658ed595eb 100644 --- a/examples/rust-outbound-redis/spin.toml +++ b/examples/rust-outbound-redis/spin.toml @@ -12,6 +12,6 @@ component = "outbound-redis" [component.outbound-redis] environment = { REDIS_ADDRESS = "redis://127.0.0.1:6379", REDIS_CHANNEL = "messages" } source = "target/wasm32-wasi/release/rust_outbound_redis.wasm" -allowed_outbound_hosts = ["redis://127.0.0.1:6379"] +allowed_outbound_hosts = ["redis://127.0.0.1"] [component.outbound-redis.build] command = "cargo build --target wasm32-wasi --release" diff --git a/examples/spin-timer/Cargo.lock b/examples/spin-timer/Cargo.lock index fb08601cb2..7640961096 100644 --- a/examples/spin-timer/Cargo.lock +++ b/examples/spin-timer/Cargo.lock @@ -3785,6 +3785,7 @@ dependencies = [ "indexmap 1.9.3", "serde", "spin-serde", + "terminal", "thiserror", "toml 0.8.2", "url", diff --git a/sdk/go/http/testdata/spin-roundtrip/spin.toml b/sdk/go/http/testdata/spin-roundtrip/spin.toml index 6dffb2b70f..4613d21ace 100644 --- a/sdk/go/http/testdata/spin-roundtrip/spin.toml +++ b/sdk/go/http/testdata/spin-roundtrip/spin.toml @@ -8,7 +8,7 @@ version = "1.0.0" [[component]] id = "http-roundtrip-test" source = "main.wasm" -allowed_outbound_hosts = ["https://example.com:443"] +allowed_outbound_hosts = ["https://example.com"] [component.trigger] route = "/hello/..." [component.build] diff --git a/sdk/rust/readme.md b/sdk/rust/readme.md index 007e14ad00..b505d676ae 100644 --- a/sdk/rust/readme.md +++ b/sdk/rust/readme.md @@ -64,7 +64,7 @@ configuration: [[component]] id = "hello" source = "target/wasm32-wasi/release/spinhelloworld.wasm" -allowed_outbound_hosts = [ "https://fermyon.com:443" ] +allowed_outbound_hosts = [ "https://fermyon.com" ] [component.trigger] route = "/hello" ``` diff --git a/tests/testcases/http-rust-outbound-mysql/spin.toml b/tests/testcases/http-rust-outbound-mysql/spin.toml index 46f95eb444..69ee2de047 100644 --- a/tests/testcases/http-rust-outbound-mysql/spin.toml +++ b/tests/testcases/http-rust-outbound-mysql/spin.toml @@ -8,7 +8,7 @@ version = "0.1.0" environment = { DB_URL = "mysql://spin:spin@mysql/spin_dev" } id = "outbound-mysql" source = "target/wasm32-wasi/release/http_rust_outbound_mysql_test.wasm" -allowed_outbound_hosts = ["mysql://mysql:3306"] +allowed_outbound_hosts = ["mysql://mysql"] [component.trigger] route = "/..." [component.build] diff --git a/tests/testcases/http-rust-outbound-pg/spin.toml b/tests/testcases/http-rust-outbound-pg/spin.toml index 076bdfe082..9d98a08d9f 100644 --- a/tests/testcases/http-rust-outbound-pg/spin.toml +++ b/tests/testcases/http-rust-outbound-pg/spin.toml @@ -8,7 +8,7 @@ version = "0.1.0" environment = { DB_URL = "host=postgres user=postgres password=postgres dbname=spin_dev" } id = "outbound-pg" source = "target/wasm32-wasi/release/http_rust_outbound_pg.wasm" -allowed_outbound_hosts = ["postgres://postgres:5432"] +allowed_outbound_hosts = ["postgres://postgres"] [component.trigger] route = "/..." [component.build]