Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::num::NonZeroUsize;
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
use std::{fmt, io};

Expand Down Expand Up @@ -139,6 +140,7 @@ pub struct ConfigFile {
pub logs: LogConfig,
pub wasm: WasmConfig,
pub v8: V8Config,
pub http_egress_policy: HttpEgressPolicy,
}

#[derive(serde::Deserialize, Default)]
Expand All @@ -154,6 +156,8 @@ struct ConfigFileToml {
v8: V8ConfigToml,
#[serde(default)]
v8_heap_policy: V8HeapPolicyConfig,
#[serde(default)]
http_egress_policy: HttpEgressPolicy,
}

impl<'de> serde::Deserialize<'de> for ConfigFile {
Expand All @@ -172,6 +176,7 @@ impl<'de> serde::Deserialize<'de> for ConfigFile {
procedure_instance_pool_size: config.v8.procedure_instance_pool_size,
heap_policy: config.v8_heap_policy,
},
http_egress_policy: config.http_egress_policy,
})
}
}
Expand Down Expand Up @@ -202,6 +207,37 @@ impl CertificateAuthority {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HttpEgressPolicy {
PublicInternet,
AllowLoopback,
LoopbackOnly,
}

impl Default for HttpEgressPolicy {
fn default() -> Self {
if cfg!(feature = "allow_loopback_http_for_tests") {
Self::AllowLoopback
} else {
Self::PublicInternet
}
}
}

impl FromStr for HttpEgressPolicy {
type Err = String;

fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"public-internet" => Ok(Self::PublicInternet),
"allow-loopback" => Ok(Self::AllowLoopback),
"loopback-only" => Ok(Self::LoopbackOnly),
_ => Err(format!("invalid HTTP egress policy `{value}`")),
}
}
}

#[serde_with::serde_as]
#[derive(Clone, serde::Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -562,6 +598,7 @@ mod tests {
fn v8_heap_policy_defaults_when_omitted() {
let config: ConfigFile = toml::from_str("").unwrap();

assert_eq!(config.http_egress_policy, HttpEgressPolicy::default());
assert_eq!(
config.wasm.procedure_instance_pool_size,
default_wasm_procedure_instance_pool_size()
Expand All @@ -583,6 +620,8 @@ mod tests {
#[test]
fn v8_heap_policy_parses_from_toml() {
let toml = r#"
http-egress-policy = "loopback-only"

[wasm]
procedure-instance-pool-size = 4

Expand All @@ -599,6 +638,7 @@ mod tests {

let config: ConfigFile = toml::from_str(toml).unwrap();

assert_eq!(config.http_egress_policy, HttpEgressPolicy::LoopbackOnly);
assert_eq!(config.wasm.procedure_instance_pool_size.get(), 4);
assert_eq!(config.v8.procedure_instance_pool_size.get(), 3);
assert_eq!(config.v8.heap_policy.heap_check_request_interval, None);
Expand Down
19 changes: 15 additions & 4 deletions crates/core/src/host/host_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::v8::V8HeapMetrics;
use super::wasmtime::{WasmMemoryBytesMetric, WasmtimeRuntime};
use super::{Scheduler, UpdateDatabaseResult};
use crate::client::{ClientActorId, ClientName};
use crate::config::{V8Config, WasmConfig};
use crate::config::{HttpEgressPolicy, V8Config, WasmConfig};
use crate::database_logger::DatabaseLogger;
use crate::db::persistence::PersistenceProvider;
use crate::db::relational_db::{self, spawn_view_cleanup_loop, DiskSizeFn, RelationalDB, Txdata};
Expand Down Expand Up @@ -203,25 +203,35 @@ pub struct HostController {
pub(crate) struct HostRuntimes {
wasmtime: WasmtimeRuntime,
v8: V8Runtime,
http_egress_policy: HttpEgressPolicy,
}

#[derive(Clone, Copy, Debug, Default)]
pub struct HostRuntimeConfig {
pub wasm: WasmConfig,
pub v8: V8Config,
pub http_egress_policy: HttpEgressPolicy,
}

impl HostRuntimeConfig {
pub fn new(wasm: WasmConfig, v8: V8Config) -> Self {
Self { wasm, v8 }
pub fn new(wasm: WasmConfig, v8: V8Config, http_egress_policy: HttpEgressPolicy) -> Self {
Self {
wasm,
v8,
http_egress_policy,
}
}
}

impl HostRuntimes {
fn new(data_dir: Option<&ServerDataDir>, config: HostRuntimeConfig) -> Arc<Self> {
let wasmtime = WasmtimeRuntime::new(data_dir, config.wasm);
let v8 = V8Runtime::new(config.v8);
Arc::new(Self { wasmtime, v8 })
Arc::new(Self {
wasmtime,
v8,
http_egress_policy: config.http_egress_policy,
})
}
}

Expand Down Expand Up @@ -838,6 +848,7 @@ async fn make_module_host(
scheduler,
program_hash: program.hash,
energy_monitor,
http_egress_policy: runtimes.http_egress_policy,
};

match HostType::from(program.kind) {
Expand Down
Loading
Loading