Skip to content

Commit

Permalink
Don't require a port
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Oct 31, 2023
1 parent 641555a commit a875503
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 19 deletions.
40 changes: 29 additions & 11 deletions crates/outbound-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..] != "/" {
Expand All @@ -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,
})
}
Expand Down Expand Up @@ -171,7 +175,12 @@ enum PortConfig {
}

impl PortConfig {
fn parse(port: &str) -> anyhow::Result<PortConfig> {
fn parse(port: &str, scheme: &str) -> anyhow::Result<PortConfig> {
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);
}
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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(
Expand All @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion examples/http-rust-outbound-http/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-outbound-mysql/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/rust-outbound-pg/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/rust-outbound-redis/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/go/http/testdata/spin-roundtrip/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion sdk/rust/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/http-rust-outbound-mysql/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/testcases/http-rust-outbound-pg/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a875503

Please # to comment.