Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plumb through reading the FASTLY_KEY env variable to compute modules #377

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion lib/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,6 +57,7 @@ pub fn link_host_functions(linker: &mut component::Linker<ComponentCtx>) -> 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(())
}
Expand All @@ -78,3 +79,4 @@ pub mod purge;
pub mod secret_store;
pub mod types;
pub mod uap;
pub mod viceroy_env;
8 changes: 8 additions & 0 deletions lib/src/component/viceroy_env.rs
Original file line number Diff line number Diff line change
@@ -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<String, ()> {
self.fastly_key_read().ok_or(())
}
}
6 changes: 6 additions & 0 deletions lib/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub struct ExecuteCtx {
object_store: ObjectStores,
/// The secret stores for this execution.
secret_stores: Arc<SecretStores>,
/// The FASTLY_KEY read in from the environment
fastly_key: Arc<Option<String>>,
// `Arc` for the two fields below because this struct must be `Clone`.
epoch_increment_thread: Option<Arc<JoinHandle<()>>>,
epoch_increment_stop: Arc<AtomicBool>,
Expand Down Expand Up @@ -189,6 +191,7 @@ impl ExecuteCtx {
engine_clone.increment_epoch();
}
})));
let fastly_key = std::env::var("FASTLY_KEY").ok();

Ok(Self {
engine,
Expand All @@ -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),
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub struct Session {
config_path: Arc<Option<PathBuf>>,
/// The ID for the client request being processed.
req_id: u64,
/// FASTLY_KEY read in from the environment
fastly_key: Arc<Option<String>>,
}

impl Session {
Expand All @@ -144,6 +146,7 @@ impl Session {
config_path: Arc<Option<PathBuf>>,
object_store: ObjectStores,
secret_stores: Arc<SecretStores>,
fastly_key: Arc<Option<String>>,
) -> Session {
let (parts, body) = req.into_parts();
let downstream_req_original_headers = parts.headers.clone();
Expand Down Expand Up @@ -179,6 +182,7 @@ impl Session {
secrets_by_name: PrimaryMap::new(),
config_path,
req_id,
fastly_key,
}
}

Expand Down Expand Up @@ -839,6 +843,12 @@ impl Session {
&self.secret_stores
}

// ----- FASTLY_KEY (env var) -----

pub fn fastly_key_read(&self) -> Option<String> {
self.fastly_key.as_ref().as_ref().cloned()
}

// ----- Pending Requests API -----

/// Insert a [`PendingRequest`] into the session.
Expand Down
4 changes: 4 additions & 0 deletions lib/wit/deps/fastly/compute.wit
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ interface reactor {
serve: func(req: request-handle, body: body-handle) -> result;
}

interface viceroy-env {
fastly-key: func() -> result<string>;
}

world compute {
import wasi:clocks/[email protected];
import wasi:clocks/[email protected];
Expand Down
1 change: 1 addition & 0 deletions lib/wit/viceroy.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package fastly:viceroy;

world compute {
include fastly:api/compute;
import fastly:api/viceroy-env;
}
Loading