Skip to content

Commit 0b1f3de

Browse files
committed
Rename to 'allowed_outbound_hosts'
Signed-off-by: Ryan Levick <[email protected]>
1 parent cd45525 commit 0b1f3de

File tree

8 files changed

+20
-18
lines changed

8 files changed

+20
-18
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/loader/src/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl LocalLoader {
114114
let metadata = ValuesMapBuilder::new()
115115
.string("description", component.description)
116116
.string_array("allowed_http_hosts", component.allowed_http_hosts)
117-
.string_array_option("allowed_redis_hosts", component.allowed_redis_hosts)
117+
.string_array_option("allowed_outbound_hosts", component.allowed_outbound_hosts)
118118
.string_array("key_value_stores", component.key_value_stores)
119119
.string_array("databases", component.sqlite_databases)
120120
.string_array("ai_models", component.ai_models)

crates/manifest/src/compat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn v1_to_v2_app(manifest: v1::AppManifestV1) -> Result<v2::AppManifest, Erro
6969
sqlite_databases,
7070
ai_models,
7171
build: component.build,
72-
allowed_redis_hosts: component.allowed_redis_hosts,
72+
allowed_outbound_hosts: component.allowed_outbound_hosts,
7373
},
7474
);
7575
triggers

crates/manifest/src/schema/v1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub struct ComponentV1 {
7878
/// `allowed_http_hosts = ["example.com"]`
7979
#[serde(default)]
8080
pub allowed_http_hosts: Vec<String>,
81-
/// `allowed_redis_hosts` = ["redis://redis.com:6379"]`
81+
/// `allowed_outbound_hosts` = ["redis://redis.com:6379"]`
8282
#[serde(default)]
83-
pub allowed_redis_hosts: Option<Vec<String>>,
83+
pub allowed_outbound_hosts: Option<Vec<String>>,
8484
/// `key_value_stores = ["default"]`
8585
#[serde(default)]
8686
pub key_value_stores: Vec<String>,

crates/manifest/src/schema/v2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ pub struct Component {
102102
/// `allowed_http_hosts = ["example.com"]`
103103
#[serde(default, skip_serializing_if = "Vec::is_empty")]
104104
pub allowed_http_hosts: Vec<String>,
105-
/// `allowed_redis_hosts = ["myredishost.com"]`
105+
/// `allowed_outbound_hosts = ["myredishost.com"]`
106106
#[serde(default, skip_serializing_if = "is_none_or_empty")]
107-
pub allowed_redis_hosts: Option<Vec<String>>,
107+
pub allowed_outbound_hosts: Option<Vec<String>>,
108108
/// `key_value_stores = ["default"]`
109109
#[serde(default, skip_serializing_if = "Vec::is_empty")]
110110
pub key_value_stores: Vec<SnakeId>,

crates/outbound-redis/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ table = { path = "../table" }
1818
tokio = { version = "1", features = ["sync"] }
1919
tracing = { workspace = true }
2020
terminal = { path = "../terminal" }
21+
url = "2.4.1"

crates/outbound-redis/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use spin_world::v1::redis::{self as v1, RedisParameter, RedisResult};
1010
use spin_world::v2::redis::{self as v2, Connection as RedisConnection, Error};
1111

1212
pub const ALLOWED_REDIS_HOSTS_KEY: MetadataKey<Option<HashSet<String>>> =
13-
MetadataKey::new("allowed_redis_hosts");
13+
MetadataKey::new("allowed_outbound_hosts");
1414

1515
pub use host_component::OutboundRedisComponent;
1616

@@ -50,21 +50,21 @@ impl Default for OutboundRedis {
5050

5151
impl OutboundRedis {
5252
fn is_address_allowed(&self, address: &str, default: bool) -> bool {
53-
fn do_check(allowed_hosts: Option<&HashSet<String>>, address: &str, default: bool) -> bool {
54-
let Some(allowed_hosts) = allowed_hosts else {
55-
return default;
56-
};
57-
allowed_hosts.contains(address)
58-
}
53+
let url = url::Url::parse(address).ok();
54+
let host = url.as_ref().and_then(|u| u.host_str()).unwrap_or(address);
55+
let is_allowed = if let Some(allowed_hosts) = self.allowed_hosts.as_ref() {
56+
allowed_hosts.contains(host)
57+
} else {
58+
default
59+
};
5960

60-
let response = do_check(self.allowed_hosts.as_ref(), address, default);
61-
if !response {
61+
if !is_allowed {
6262
terminal::warn!(
6363
"A component tried to make a HTTP request to non-allowed address {address:?}."
6464
);
65-
eprintln!("To allow requests, add 'allowed_redis_hosts = [{address:?}]' to the manifest component section.");
65+
eprintln!("To allow requests, add 'allowed_outbound_hosts = [{host:?}]' to the manifest component section.");
6666
}
67-
response
67+
is_allowed
6868
}
6969

7070
async fn establish_connection(

examples/rust-outbound-redis/spin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ version = "0.1.0"
88
environment = { REDIS_ADDRESS = "redis://127.0.0.1:6379", REDIS_CHANNEL = "messages" }
99
id = "outbound-redis"
1010
source = "target/wasm32-wasi/release/rust_outbound_redis.wasm"
11-
allowed_redis_hosts = ["redis://127.0.0.1:6379"]
11+
allowed_outbound_hosts = ["127.0.0.1:6379"]
1212
[component.trigger]
1313
route = "/publish"
1414
[component.build]

0 commit comments

Comments
 (0)