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

😯 Add support for the new secret from_bytes extension. #283

Merged
merged 1 commit into from
Jul 12, 2023
Merged
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
6 changes: 6 additions & 0 deletions lib/compute-at-edge-abi/compute-at-edge.witx
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@
(param $nwritten_out (@witx pointer (@witx usize)))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "from_bytes")
(param $buf (@witx pointer (@witx char8)))
(param $buf_len (@witx usize))
(result $err (expected $secret_handle (error $fastly_status)))
)
)

(module $fastly_backend
Expand Down
13 changes: 9 additions & 4 deletions lib/src/secret_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ impl Secret {
}
}

#[derive(Clone, Debug, Default)]
pub struct SecretLookup {
pub store_name: String,
pub secret_name: String,
#[derive(Clone, Debug)]
pub enum SecretLookup {
Standard {
store_name: String,
secret_name: String,
},
Injected {
plaintext: Vec<u8>,
},
}
7 changes: 6 additions & 1 deletion lib/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ impl Session {
self.secret_stores
.get_store(store_name)?
.get_secret(secret_name)?;
Some(self.secrets_by_name.push(SecretLookup {
Some(self.secrets_by_name.push(SecretLookup::Standard {
store_name: store_name.to_string(),
secret_name: secret_name.to_string(),
}))
Expand All @@ -707,6 +707,11 @@ impl Session {
self.secrets_by_name.get(handle).cloned()
}

pub fn add_secret(&mut self, plaintext: Vec<u8>) -> SecretHandle {
self.secrets_by_name
.push(SecretLookup::Injected { plaintext })
}

pub fn secret_stores(&self) -> &Arc<SecretStores> {
&self.secret_stores
}
Expand Down
44 changes: 33 additions & 11 deletions lib/src/wiggle_abi/secret_store_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
crate::{
error::Error,
secret_store::SecretLookup,
session::Session,
wiggle_abi::{
fastly_secret_store::FastlySecretStore,
Expand Down Expand Up @@ -81,17 +82,25 @@ impl FastlySecretStore for Session {
.ok_or(Error::SecretStoreError(
SecretStoreError::InvalidSecretHandle(secret_handle),
))?;
let plaintext = self
.secret_stores()
.get_store(lookup.store_name.as_str())
.ok_or(Error::SecretStoreError(
SecretStoreError::InvalidSecretHandle(secret_handle),
))?
.get_secret(lookup.secret_name.as_str())
.ok_or(Error::SecretStoreError(
SecretStoreError::InvalidSecretHandle(secret_handle),
))?
.plaintext();

let plaintext = match &lookup {
SecretLookup::Standard {
store_name,
secret_name,
} => self
.secret_stores()
.get_store(store_name)
.ok_or(Error::SecretStoreError(
SecretStoreError::InvalidSecretHandle(secret_handle),
))?
.get_secret(secret_name)
.ok_or(Error::SecretStoreError(
SecretStoreError::InvalidSecretHandle(secret_handle),
))?
.plaintext(),

SecretLookup::Injected { plaintext } => plaintext,
};

if plaintext.len() > plaintext_max_len as usize {
// Write out the number of bytes necessary to fit the
Expand All @@ -115,4 +124,17 @@ impl FastlySecretStore for Session {

Ok(())
}

fn from_bytes(
&mut self,
plaintext_buf: &GuestPtr<'_, u8>,
plaintext_len: u32,
) -> Result<SecretHandle, Error> {
let plaintext = plaintext_buf
.as_array(plaintext_len)
.as_slice()?
.ok_or(Error::SharedMemory)?
.to_vec();
Ok(self.add_secret(plaintext))
}
}
6 changes: 6 additions & 0 deletions test-fixtures/src/bin/secret-store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A guest program to test that secret store works properly.

use fastly::SecretStore;
use fastly::secret_store::Secret;

fn main() {
// Check we can't get a store that does not exist
Expand All @@ -20,4 +21,9 @@ fn main() {
None => {}
_ => panic!(),
}

// FIXME: uncomment this when we release this API more fully
//let hello_bytes = "hello, wasm_world!".as_bytes().to_vec();
//let secret = Secret::from_bytes(hello_bytes).unwrap();
//assert_eq!("hello, wasm_world!", secret.plaintext());
Comment on lines +24 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time to FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as the 0.9.5 release goes out, I'll update it 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we have ourselves a circular dependency here, don't we

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can either merge this as is, with a commented-out test case (I can say, I ran it manually and it worked), or we can merge a broken test case and have the SDK release fix it. I think I'd lean towards the former, and then update Viceroy afterwards.

}