diff --git a/lib/src/component/mod.rs b/lib/src/component/mod.rs index c1d1d2e6..45afc00a 100644 --- a/lib/src/component/mod.rs +++ b/lib/src/component/mod.rs @@ -2,7 +2,7 @@ use {crate::linking::ComponentCtx, wasmtime::component}; component::bindgen!({ path: "wit", - world: "fastly:api/compute", + world: "fastly:viceroy/compute", async: true, with: { "fastly:api/uap/user-agent": uap::UserAgent, @@ -57,6 +57,7 @@ pub fn link_host_functions(linker: &mut component::Linker) -> anyh fastly::api::secret_store::add_to_linker(linker, |x| x.session())?; fastly::api::types::add_to_linker(linker, |x| x.session())?; fastly::api::uap::add_to_linker(linker, |x| x.session())?; + fastly::api::viceroy_env::add_to_linker(linker, |x| x.session())?; Ok(()) } @@ -78,3 +79,4 @@ pub mod purge; pub mod secret_store; pub mod types; pub mod uap; +pub mod viceroy_env; diff --git a/lib/src/component/viceroy_env.rs b/lib/src/component/viceroy_env.rs new file mode 100644 index 00000000..9172a84c --- /dev/null +++ b/lib/src/component/viceroy_env.rs @@ -0,0 +1,8 @@ +use {super::fastly::api::viceroy_env, crate::session::Session}; + +#[async_trait::async_trait] +impl viceroy_env::Host for Session { + async fn fastly_key(&mut self) -> Result { + self.fastly_key_read().ok_or(()) + } +} diff --git a/lib/src/execute.rs b/lib/src/execute.rs index c3b1fe19..da0e52ea 100644 --- a/lib/src/execute.rs +++ b/lib/src/execute.rs @@ -89,6 +89,8 @@ pub struct ExecuteCtx { object_store: ObjectStores, /// The secret stores for this execution. secret_stores: Arc, + /// The FASTLY_KEY read in from the environment + fastly_key: Arc>, // `Arc` for the two fields below because this struct must be `Clone`. epoch_increment_thread: Option>>, epoch_increment_stop: Arc, @@ -189,6 +191,7 @@ impl ExecuteCtx { engine_clone.increment_epoch(); } }))); + let fastly_key = std::env::var("FASTLY_KEY").ok(); Ok(Self { engine, @@ -204,6 +207,7 @@ impl ExecuteCtx { next_req_id: Arc::new(AtomicU64::new(0)), object_store: ObjectStores::new(), secret_stores: Arc::new(SecretStores::new()), + fastly_key: Arc::new(fastly_key), epoch_increment_thread, epoch_increment_stop, guest_profile_path: Arc::new(guest_profile_path), @@ -416,6 +420,7 @@ impl ExecuteCtx { self.config_path.clone(), self.object_store.clone(), self.secret_stores.clone(), + self.fastly_key.clone(), ); let guest_profile_path = self.guest_profile_path.as_deref().map(|path| { @@ -560,6 +565,7 @@ impl ExecuteCtx { self.config_path.clone(), self.object_store.clone(), self.secret_stores.clone(), + self.fastly_key.clone(), ); if let Instance::Component(_) = self.instance_pre.as_ref() { diff --git a/lib/src/session.rs b/lib/src/session.rs index e70b4a93..6623b9da 100644 --- a/lib/src/session.rs +++ b/lib/src/session.rs @@ -126,6 +126,8 @@ pub struct Session { config_path: Arc>, /// The ID for the client request being processed. req_id: u64, + /// FASTLY_KEY read in from the environment + fastly_key: Arc>, } impl Session { @@ -144,6 +146,7 @@ impl Session { config_path: Arc>, object_store: ObjectStores, secret_stores: Arc, + fastly_key: Arc>, ) -> Session { let (parts, body) = req.into_parts(); let downstream_req_original_headers = parts.headers.clone(); @@ -179,6 +182,7 @@ impl Session { secrets_by_name: PrimaryMap::new(), config_path, req_id, + fastly_key, } } @@ -839,6 +843,12 @@ impl Session { &self.secret_stores } + // ----- FASTLY_KEY (env var) ----- + + pub fn fastly_key_read(&self) -> Option { + self.fastly_key.as_ref().as_ref().cloned() + } + // ----- Pending Requests API ----- /// Insert a [`PendingRequest`] into the session. diff --git a/lib/wit/deps/fastly/compute.wit b/lib/wit/deps/fastly/compute.wit index 3367d589..b94c2593 100644 --- a/lib/wit/deps/fastly/compute.wit +++ b/lib/wit/deps/fastly/compute.wit @@ -1065,6 +1065,10 @@ interface reactor { serve: func(req: request-handle, body: body-handle) -> result; } +interface viceroy-env { + fastly-key: func() -> result; +} + world compute { import wasi:clocks/wall-clock@0.2.0; import wasi:clocks/monotonic-clock@0.2.0; diff --git a/lib/wit/viceroy.wit b/lib/wit/viceroy.wit index 646dd328..19fd9a5a 100644 --- a/lib/wit/viceroy.wit +++ b/lib/wit/viceroy.wit @@ -2,4 +2,5 @@ package fastly:viceroy; world compute { include fastly:api/compute; + import fastly:api/viceroy-env; } \ No newline at end of file