Skip to content

Commit 3ef8673

Browse files
authored
Merge pull request #2837 from fermyon/convert-from-old-http
Convert from old allowed_http_hosts locked app value.
2 parents 49694dd + 7301062 commit 3ef8673

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
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/componentize/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ mod tests {
466466
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
467467
let mut cmd = process::Command::new("cargo");
468468
cmd.arg("build")
469-
.current_dir(&format!("tests/{name}"))
469+
.current_dir(format!("tests/{name}"))
470470
.arg("--release")
471471
.arg("--target=wasm32-wasi")
472472
.env("CARGO_TARGET_DIR", out_dir);

crates/factor-outbound-networking/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ spin-factor-variables = { path = "../factor-variables" }
1818
spin-factor-wasi = { path = "../factor-wasi" }
1919
spin-factors = { path = "../factors" }
2020
spin-locked-app = { path = "../locked-app" }
21+
spin-manifest = { path = "../manifest" }
2122
spin-serde = { path = "../serde" }
2223
terminal = { path = "../terminal" }
2324
tracing = { workspace = true }

crates/factor-outbound-networking/src/config.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
use std::ops::Range;
22

33
use anyhow::{bail, ensure, Context};
4+
use spin_factors::AppComponent;
45
use spin_locked_app::MetadataKey;
56

6-
pub const ALLOWED_HOSTS_KEY: MetadataKey<Vec<String>> = MetadataKey::new("allowed_outbound_hosts");
7+
const ALLOWED_HOSTS_KEY: MetadataKey<Vec<String>> = MetadataKey::new("allowed_outbound_hosts");
8+
const ALLOWED_HTTP_KEY: MetadataKey<Vec<String>> = MetadataKey::new("allowed_http_hosts");
79

810
pub const SERVICE_CHAINING_DOMAIN: &str = "spin.internal";
911
pub const SERVICE_CHAINING_DOMAIN_SUFFIX: &str = ".spin.internal";
1012

13+
/// Get the raw values of the `allowed_outbound_hosts` locked app metadata key.
14+
///
15+
/// This has support for converting the old `allowed_http_hosts` key to the new `allowed_outbound_hosts` key.
16+
pub fn allowed_outbound_hosts(component: &AppComponent) -> anyhow::Result<Vec<String>> {
17+
let mut allowed_hosts = component
18+
.get_metadata(ALLOWED_HOSTS_KEY)
19+
.with_context(|| {
20+
format!(
21+
"locked app metadata was malformed for key {}",
22+
ALLOWED_HOSTS_KEY.as_ref()
23+
)
24+
})?
25+
.unwrap_or_default();
26+
let allowed_http = component
27+
.get_metadata(ALLOWED_HTTP_KEY)
28+
.map(|h| h.unwrap_or_default())
29+
.unwrap_or_default();
30+
let converted =
31+
spin_manifest::compat::convert_allowed_http_to_allowed_hosts(&allowed_http, false)
32+
.unwrap_or_default();
33+
allowed_hosts.extend(converted);
34+
Ok(allowed_hosts)
35+
}
36+
1137
/// An address is a url-like string that contains a host, a port, and an optional scheme
1238
#[derive(Eq, Debug, Clone)]
1339
pub struct AllowedHostConfig {
@@ -718,6 +744,11 @@ mod test {
718744
);
719745
}
720746

747+
#[test]
748+
fn test_missing_scheme() {
749+
assert!(AllowedHostConfig::parse("example.com").is_err());
750+
}
751+
721752
#[test]
722753
fn test_allowed_hosts_can_be_specific() {
723754
let allowed = AllowedHostsConfig::parse(

crates/factor-outbound-networking/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub mod runtime_config;
33

44
use std::{collections::HashMap, sync::Arc};
55

6-
use config::ALLOWED_HOSTS_KEY;
76
use futures_util::{
87
future::{BoxFuture, Shared},
98
FutureExt,
@@ -17,8 +16,8 @@ use spin_factors::{
1716
};
1817

1918
pub use config::{
20-
is_service_chaining_host, parse_service_chaining_target, AllowedHostConfig, AllowedHostsConfig,
21-
HostConfig, OutboundUrl, SERVICE_CHAINING_DOMAIN_SUFFIX,
19+
allowed_outbound_hosts, is_service_chaining_host, parse_service_chaining_target,
20+
AllowedHostConfig, AllowedHostsConfig, HostConfig, OutboundUrl, SERVICE_CHAINING_DOMAIN_SUFFIX,
2221
};
2322

2423
pub use runtime_config::ComponentTlsConfigs;
@@ -58,9 +57,7 @@ impl Factor for OutboundNetworkingFactor {
5857
.map(|component| {
5958
Ok((
6059
component.id().to_string(),
61-
component
62-
.get_metadata(ALLOWED_HOSTS_KEY)?
63-
.unwrap_or_default()
60+
allowed_outbound_hosts(&component)?
6461
.into_boxed_slice()
6562
.into(),
6663
))

crates/manifest/src/compat.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ pub fn v1_to_v2_app(manifest: v1::AppManifestV1) -> Result<v2::AppManifest, Erro
9797
})
9898
}
9999

100-
pub(crate) fn convert_allowed_http_to_allowed_hosts(
100+
/// Converts the old `allowed_http_hosts` field to the new `allowed_outbound_hosts` field.
101+
///
102+
/// If `allow_database_access` is `true`, the function will also allow access to all redis,
103+
/// mysql, and postgres databases as this was the default before `allowed_outbound_hosts` was introduced.
104+
pub fn convert_allowed_http_to_allowed_hosts(
101105
allowed_http_hosts: &[impl AsRef<str>],
102106
allow_database_access: bool,
103107
) -> anyhow::Result<Vec<String>> {

0 commit comments

Comments
 (0)