From 11848df2b98a20f2f1956f9f9ab6f249362c4ebd Mon Sep 17 00:00:00 2001 From: itowlson Date: Fri, 27 Sep 2024 15:06:23 +1200 Subject: [PATCH] Implement wasi-config Signed-off-by: itowlson --- Cargo.lock | 1 + crates/expressions/Cargo.toml | 1 + crates/expressions/src/lib.rs | 15 +++++ crates/factor-variables/src/host.rs | 55 +++++++++++++++++++ crates/factor-variables/src/lib.rs | 1 + crates/world/src/lib.rs | 1 + tests/runtime-tests/tests/variables/spin.toml | 17 ++++++ .../runtime-tests/tests/wasi-config/spin.toml | 17 ++++++ tests/test-components/components/Cargo.lock | 8 +++ .../components/wasi-config/Cargo.toml | 11 ++++ .../components/wasi-config/README.md | 8 +++ .../components/wasi-config/src/lib.rs | 23 ++++++++ .../wasi-runtime-config-2024-09-27/store.wit | 30 ++++++++++ .../wasi-runtime-config-2024-09-27/world.wit | 6 ++ wit/world.wit | 2 + 15 files changed, 196 insertions(+) create mode 100644 tests/runtime-tests/tests/variables/spin.toml create mode 100644 tests/runtime-tests/tests/wasi-config/spin.toml create mode 100644 tests/test-components/components/wasi-config/Cargo.toml create mode 100644 tests/test-components/components/wasi-config/README.md create mode 100644 tests/test-components/components/wasi-config/src/lib.rs create mode 100644 wit/deps/wasi-runtime-config-2024-09-27/store.wit create mode 100644 wit/deps/wasi-runtime-config-2024-09-27/world.wit diff --git a/Cargo.lock b/Cargo.lock index bb84e5a0d9..a2c11acfbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7103,6 +7103,7 @@ version = "2.8.0-pre0" dependencies = [ "anyhow", "async-trait", + "futures", "serde 1.0.210", "spin-locked-app", "thiserror", diff --git a/crates/expressions/Cargo.toml b/crates/expressions/Cargo.toml index 15bb79a68a..8b62451849 100644 --- a/crates/expressions/Cargo.toml +++ b/crates/expressions/Cargo.toml @@ -7,6 +7,7 @@ edition = { workspace = true } [dependencies] anyhow = { workspace = true } async-trait = { workspace = true } +futures = { workspace = true } serde = { workspace = true } spin-locked-app = { path = "../locked-app" } thiserror = { workspace = true } diff --git a/crates/expressions/src/lib.rs b/crates/expressions/src/lib.rs index 350914b558..e5a1ff5cdf 100644 --- a/crates/expressions/src/lib.rs +++ b/crates/expressions/src/lib.rs @@ -52,6 +52,21 @@ impl ProviderResolver { self.resolve_template(template).await } + pub async fn resolve_all(&self, component_id: &str) -> Result> { + use futures::FutureExt; + + let Some(keys2templates) = self.internal.component_configs.get(component_id) else { + return Ok(vec![]); + }; + + let resolve_futs = keys2templates.iter().map(|(key, template)| { + self.resolve_template(template) + .map(|r| r.map(|value| (key.to_string(), value))) + }); + + futures::future::try_join_all(resolve_futs).await + } + /// Resolves the given template. pub async fn resolve_template(&self, template: &Template) -> Result { let mut resolved_parts: Vec> = Vec::with_capacity(template.parts().len()); diff --git a/crates/factor-variables/src/host.rs b/crates/factor-variables/src/host.rs index bc30fe0a79..1f0f00d69f 100644 --- a/crates/factor-variables/src/host.rs +++ b/crates/factor-variables/src/host.rs @@ -37,6 +37,61 @@ impl v1::config::Host for InstanceState { } } +#[async_trait] +impl spin_world::wasi::config::store::Host for InstanceState { + async fn get( + &mut self, + key: String, + ) -> Result, spin_world::wasi::config::store::Error> { + match ::get(self, key).await { + Ok(value) => Ok(Some(value)), + // Err(e) if matches!(e, variables::Error::Undefined(_)) => Ok(Ok(None)), + Err(e) => { + match e { + variables::Error::Undefined(_) => Ok(None), + variables::Error::InvalidName(msg) => { + Err(spin_world::wasi::config::store::Error::Io(msg)) + } // TODO: this doesn't feel ideal, but wasi-config only allows two error cases + variables::Error::Provider(msg) => { + Err(spin_world::wasi::config::store::Error::Upstream(msg)) + } + variables::Error::Other(msg) => { + Err(spin_world::wasi::config::store::Error::Io(msg)) + } // TODO: again, hard to know how to map this (and the original expressions::Error is not more helpful) + } + } + } + } + + async fn get_all( + &mut self, + ) -> Result, spin_world::wasi::config::store::Error> { + let all = self + .expression_resolver + .resolve_all(&self.component_id) + .await; + all.map_err(|e| { + match expressions_to_variables_err(e) { + variables::Error::Undefined(msg) => spin_world::wasi::config::store::Error::Io(msg), // TODO: this should presumably not occur here + variables::Error::InvalidName(msg) => { + spin_world::wasi::config::store::Error::Io(msg) + } // TODO: this doesn't feel ideal, but wasi-config only allows two error cases + variables::Error::Provider(msg) => { + spin_world::wasi::config::store::Error::Upstream(msg) + } + variables::Error::Other(msg) => spin_world::wasi::config::store::Error::Io(msg), // TODO: again, hard to know how to map this (and the original expressions::Error is not more helpful) + } + }) + } + + fn convert_error( + &mut self, + err: spin_world::wasi::config::store::Error, + ) -> anyhow::Result { + Ok(err) + } +} + fn expressions_to_variables_err(err: spin_expressions::Error) -> variables::Error { use spin_expressions::Error; match err { diff --git a/crates/factor-variables/src/lib.rs b/crates/factor-variables/src/lib.rs index ac5c7e626b..0983696094 100644 --- a/crates/factor-variables/src/lib.rs +++ b/crates/factor-variables/src/lib.rs @@ -31,6 +31,7 @@ impl Factor for VariablesFactor { fn init(&mut self, mut ctx: InitContext) -> anyhow::Result<()> { ctx.link_bindings(spin_world::v1::config::add_to_linker)?; ctx.link_bindings(spin_world::v2::variables::add_to_linker)?; + ctx.link_bindings(spin_world::wasi::config::store::add_to_linker)?; Ok(()) } diff --git a/crates/world/src/lib.rs b/crates/world/src/lib.rs index d7adeb19f0..61aab0b589 100644 --- a/crates/world/src/lib.rs +++ b/crates/world/src/lib.rs @@ -27,6 +27,7 @@ wasmtime::component::bindgen!({ "fermyon:spin/sqlite@2.0.0/error" => v2::sqlite::Error, "fermyon:spin/sqlite/error" => v1::sqlite::Error, "fermyon:spin/variables@2.0.0/error" => v2::variables::Error, + "wasi:config/store/error" => wasi::config::store::Error, }, trappable_imports: true, }); diff --git a/tests/runtime-tests/tests/variables/spin.toml b/tests/runtime-tests/tests/variables/spin.toml new file mode 100644 index 0000000000..b63cd61ddd --- /dev/null +++ b/tests/runtime-tests/tests/variables/spin.toml @@ -0,0 +1,17 @@ +spin_manifest_version = "1" +authors = [""] +description = "" +name = "variables" +trigger = { type = "http" } +version = "0.1.0" + +[variables] +variable = { default = "value" } + +[[component]] +id = "variables" +source = "%{source=variables}" +[component.trigger] +route = "/..." +[component.config] +variable = "{{ variable }}" diff --git a/tests/runtime-tests/tests/wasi-config/spin.toml b/tests/runtime-tests/tests/wasi-config/spin.toml new file mode 100644 index 0000000000..2eee4f45e9 --- /dev/null +++ b/tests/runtime-tests/tests/wasi-config/spin.toml @@ -0,0 +1,17 @@ +spin_manifest_version = "1" +authors = [""] +description = "" +name = "wasi-config" +trigger = { type = "http" } +version = "0.1.0" + +[variables] +variable = { default = "value" } + +[[component]] +id = "wasi-config" +source = "%{source=wasi-config}" +[component.trigger] +route = "/..." +[component.config] +variable = "{{ variable }}" diff --git a/tests/test-components/components/Cargo.lock b/tests/test-components/components/Cargo.lock index c8a2224121..ac72d7f75b 100644 --- a/tests/test-components/components/Cargo.lock +++ b/tests/test-components/components/Cargo.lock @@ -913,6 +913,14 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wasi-config" +version = "0.1.0" +dependencies = [ + "helper", + "wit-bindgen 0.16.0", +] + [[package]] name = "wasi-http-rc-2023-11-10" version = "0.1.0" diff --git a/tests/test-components/components/wasi-config/Cargo.toml b/tests/test-components/components/wasi-config/Cargo.toml new file mode 100644 index 0000000000..558e708682 --- /dev/null +++ b/tests/test-components/components/wasi-config/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "wasi-config" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +helper = { path = "../../helper" } +wit-bindgen = "0.16.0" diff --git a/tests/test-components/components/wasi-config/README.md b/tests/test-components/components/wasi-config/README.md new file mode 100644 index 0000000000..7492706f45 --- /dev/null +++ b/tests/test-components/components/wasi-config/README.md @@ -0,0 +1,8 @@ +# Variables + +Tests the wasi:config interface. + +## Expectations + +This test component expects the following to be true: +* Only the variable named "variable" is defined with value "value" diff --git a/tests/test-components/components/wasi-config/src/lib.rs b/tests/test-components/components/wasi-config/src/lib.rs new file mode 100644 index 0000000000..19db446133 --- /dev/null +++ b/tests/test-components/components/wasi-config/src/lib.rs @@ -0,0 +1,23 @@ +use helper::ensure_matches; + +use bindings::wasi::config::store::{get, get_all, Error}; + +helper::define_component!(Component); + +impl Component { + fn main() -> Result<(), String> { + ensure_matches!(get("variable"), Ok(Some(val)) if val == "value"); + ensure_matches!(get("non_existent"), Ok(None)); + + let expected_all = vec![ + ("variable".to_owned(), "value".to_owned()), + ]; + ensure_matches!(get_all(), Ok(val) if val == expected_all); + + ensure_matches!(get("invalid-name"), Err(Error::Io(_))); + ensure_matches!(get("invalid!name"), Err(Error::Io(_))); + ensure_matches!(get("4invalidname"), Err(Error::Io(_))); + + Ok(()) + } +} diff --git a/wit/deps/wasi-runtime-config-2024-09-27/store.wit b/wit/deps/wasi-runtime-config-2024-09-27/store.wit new file mode 100644 index 0000000000..794379a754 --- /dev/null +++ b/wit/deps/wasi-runtime-config-2024-09-27/store.wit @@ -0,0 +1,30 @@ +interface store { + /// An error type that encapsulates the different errors that can occur fetching configuration values. + variant error { + /// This indicates an error from an "upstream" config source. + /// As this could be almost _anything_ (such as Vault, Kubernetes ConfigMaps, KeyValue buckets, etc), + /// the error message is a string. + upstream(string), + /// This indicates an error from an I/O operation. + /// As this could be almost _anything_ (such as a file read, network connection, etc), + /// the error message is a string. + /// Depending on how this ends up being consumed, + /// we may consider moving this to use the `wasi:io/error` type instead. + /// For simplicity right now in supporting multiple implementations, it is being left as a string. + io(string), + } + + /// Gets a configuration value of type `string` associated with the `key`. + /// + /// The value is returned as an `option`. If the key is not found, + /// `Ok(none)` is returned. If an error occurs, an `Err(error)` is returned. + get: func( + /// A string key to fetch + key: string + ) -> result, error>; + + /// Gets a list of configuration key-value pairs of type `string`. + /// + /// If an error occurs, an `Err(error)` is returned. + get-all: func() -> result>, error>; +} diff --git a/wit/deps/wasi-runtime-config-2024-09-27/world.wit b/wit/deps/wasi-runtime-config-2024-09-27/world.wit new file mode 100644 index 0000000000..f92f080a57 --- /dev/null +++ b/wit/deps/wasi-runtime-config-2024-09-27/world.wit @@ -0,0 +1,6 @@ +package wasi:config@0.2.0-draft; + +world imports { + /// The interface for wasi:config/store + import store; +} \ No newline at end of file diff --git a/wit/world.wit b/wit/world.wit index b16e429054..9838cfb1a9 100644 --- a/wit/world.wit +++ b/wit/world.wit @@ -16,6 +16,7 @@ world http-trigger-rc20231018 { world platform { include wasi:cli/imports@0.2.0; import wasi:http/outgoing-handler@0.2.0; + import wasi:config/store@0.2.0-draft; import llm; import redis; import mqtt; @@ -30,6 +31,7 @@ world platform { world platform-rc20231018 { include wasi:cli/reactor@0.2.0-rc-2023-10-18; import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18; + import wasi:config/store@0.2.0-draft; import llm; import redis; import mqtt;