Skip to content

Commit 346ac4d

Browse files
authored
Merge pull request #31 from G-Core/secret-provide-the-possibility-to-get-key-with-timestamp
adding secret get_effective_at method
2 parents ce8109d + 66d6a95 commit 346ac4d

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

examples/secret/src/lib.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
use std::time::SystemTime;
22
use anyhow::{Error, Result};
33

44
use fastedge::body::Body;
@@ -37,8 +37,38 @@ fn main(_req: Request<Body>) -> Result<Response<Body>> {
3737
.map_err(Error::msg);
3838
}
3939

40+
let ts = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("Time went backwards").as_secs();
41+
let effective_at_value = match secret::get_effective_at("SECRET", ts as u32) {
42+
Ok(value) => value,
43+
Err(secret::Error::AccessDenied) => {
44+
return Response::builder()
45+
.status(StatusCode::FORBIDDEN)
46+
.body(Body::empty())
47+
.map_err(Error::msg);
48+
},
49+
Err(secret::Error::Other(msg)) => {
50+
return Response::builder()
51+
.status(StatusCode::FORBIDDEN)
52+
.body(Body::from(msg))
53+
.map_err(Error::msg);
54+
},
55+
Err(secret::Error::DecryptError) => {
56+
return Response::builder()
57+
.status(StatusCode::INTERNAL_SERVER_ERROR)
58+
.body(Body::empty())
59+
.map_err(Error::msg);
60+
}
61+
};
62+
63+
if effective_at_value.is_none() {
64+
return Response::builder()
65+
.status(StatusCode::NOT_FOUND)
66+
.body(Body::empty())
67+
.map_err(Error::msg);
68+
}
69+
4070
Response::builder()
4171
.status(StatusCode::OK)
42-
.body(Body::from(value.unwrap_or_default()))
72+
.body(Body::from(format!("get={:?}\nget_efective_at={:?}\n", value, effective_at_value)))
4373
.map_err(Error::msg)
4474
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub mod dictionary {
4343
pub mod secret {
4444
#[doc(inline)]
4545
pub use crate::gcore::fastedge::secret::get;
46+
#[doc(inline)]
47+
pub use crate::gcore::fastedge::secret::get_effective_at;
48+
pub use crate::gcore::fastedge::secret::Error;
4649
}
4750

4851
/// Error type returned by [`send_request`]

src/proxywasm.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ extern "C" {
55
return_value_data: *mut *mut u8,
66
return_value_size: *mut usize,
77
) -> u32;
8+
9+
fn proxy_get_effective_at_secret(
10+
key_data: *const u8,
11+
key_size: usize,
12+
at: u32,
13+
return_value_data: *mut *mut u8,
14+
return_value_size: *mut usize,
15+
) -> u32;
816
}
917

1018
/// ProxyWasm secret interface
1119
pub mod secret {
12-
use crate::proxywasm::proxy_get_secret;
20+
use crate::proxywasm::{proxy_get_secret, proxy_get_effective_at_secret};
1321
use std::ptr::null_mut;
1422

15-
/// Get secret method.
16-
/// return None if secret not found for given key
23+
/// Returns a secret value to the corresponding key effective now.
24+
/// If the value does not exist returns `None`.
1725
pub fn get(key: &str) -> Result<Option<Vec<u8>>, u32> {
1826
let mut return_data: *mut u8 = null_mut();
1927
let mut return_size: usize = 0;
@@ -40,4 +48,34 @@ pub mod secret {
4048
}
4149
}
4250
}
51+
52+
/// Returns a secret value to the corresponding key effective at given timestamp (in sec).
53+
/// If the value does not exist returns `None`.
54+
pub fn get_effective_at(key: &str, at: u32) -> Result<Option<Vec<u8>>, u32> {
55+
let mut return_data: *mut u8 = null_mut();
56+
let mut return_size: usize = 0;
57+
unsafe {
58+
match proxy_get_effective_at_secret(
59+
key.as_ptr(),
60+
key.len(),
61+
at,
62+
&mut return_data,
63+
&mut return_size,
64+
) {
65+
0 => {
66+
if !return_data.is_null() {
67+
Ok(Some(Vec::from_raw_parts(
68+
return_data,
69+
return_size,
70+
return_size,
71+
)))
72+
} else {
73+
Ok(None)
74+
}
75+
}
76+
1 => Ok(None),
77+
status => panic!("unexpected status: {}", status),
78+
}
79+
}
80+
}
4381
}

wit/secret.wit

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
interface secret {
2-
/// Get the secret associated with the specified `key`
2+
/// Get the secret associated with the specified `key` efective at current timestamp.
33
/// Returns `ok(none)` if the key does not exist.
44
get: func(key: string) -> result<option<string>, error>;
55

6+
/// Get the secret associated with the specified `key` effective `at` given timestamp in seconds.
7+
/// Returns `ok(none)` if the key does not exist.
8+
get-effective-at: func(key: string, at: u32) -> result<option<string>, error>;
9+
610
/// The set of errors which may be raised by functions in this interface
711
variant error {
812
/// The requesting component does not have access to the specified key

0 commit comments

Comments
 (0)