Skip to content

Commit e978106

Browse files
authored
Merge pull request #27 from G-Core/feat/secret
Secret feature
2 parents 5e07465 + 8ae6126 commit e978106

File tree

9 files changed

+90
-6
lines changed

9 files changed

+90
-6
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[build]
2-
target = "wasm32-wasi"
2+
target = "wasm32-wasip1"

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Setup Rust
2020
uses: dtolnay/rust-toolchain@stable
2121
with:
22-
target: wasm32-wasi
22+
target: wasm32-wasip1
2323
components: rustfmt, clippy
2424

2525
- name: Run cargo-audit binary crate
@@ -49,7 +49,7 @@ jobs:
4949
- name: Setup Rust
5050
uses: dtolnay/rust-toolchain@stable
5151
with:
52-
target: wasm32-wasi
52+
target: wasm32-wasip1
5353
components: rustfmt, clippy
5454

5555
- name: Run Release PR

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The table below summarizes the feature support for language SDKs.
2626
| Env Variables | Supported | Supported |
2727

2828
## Rust toolchain setup:
29-
- `rustup target add wasm32-wasi`
29+
- `rustup target add wasm32-wasip1`
3030

3131
# The FastEdge Rust SDK
3232

examples/secret/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "secret"
3+
version = {workspace = true}
4+
edition = "2021"
5+
publish = false
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
fastedge = { path = "../../" }
12+
anyhow = "1.0"

examples/secret/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
use anyhow::{Error, Result};
3+
4+
use fastedge::body::Body;
5+
use fastedge::http::{Request, Response, StatusCode};
6+
use fastedge::secret;
7+
8+
#[allow(dead_code)]
9+
#[fastedge::http]
10+
fn main(_req: Request<Body>) -> Result<Response<Body>> {
11+
let value = match secret::get("SECRET") {
12+
Ok(value) => value,
13+
Err(secret::Error::AccessDenied) => {
14+
return Response::builder()
15+
.status(StatusCode::FORBIDDEN)
16+
.body(Body::empty())
17+
.map_err(Error::msg);
18+
},
19+
Err(secret::Error::Other(msg)) => {
20+
return Response::builder()
21+
.status(StatusCode::FORBIDDEN)
22+
.body(Body::from(msg))
23+
.map_err(Error::msg);
24+
},
25+
Err(secret::Error::DecryptError) => {
26+
return Response::builder()
27+
.status(StatusCode::INTERNAL_SERVER_ERROR)
28+
.body(Body::empty())
29+
.map_err(Error::msg);
30+
}
31+
};
32+
33+
if value.is_none() {
34+
return Response::builder()
35+
.status(StatusCode::NOT_FOUND)
36+
.body(Body::empty())
37+
.map_err(Error::msg);
38+
}
39+
40+
Response::builder()
41+
.status(StatusCode::OK)
42+
.body(Body::from(value.unwrap_or_default()))
43+
.map_err(Error::msg)
44+
}

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pub use http_client::send_request;
1111
#[doc(hidden)]
1212
pub use crate::exports::gcore::fastedge::http_handler;
1313
use crate::gcore::fastedge::http::{Error as HttpError, Method, Request, Response};
14+
#[doc(hidden)]
15+
pub use crate::gcore::fastedge::secret;
16+
1417

1518
/// Implementation of Outbound HTTP component
1619
mod http_client;
@@ -24,7 +27,7 @@ pub mod wasi_nn {
2427
}
2528

2629
wit_bindgen::generate!({
27-
world: "http-reactor",
30+
world: "reactor",
2831
path: "wit",
2932
pub_export_macro: true,
3033
});

wit/secret.wit

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface secret {
2+
/// Get the secret associated with the specified `key`
3+
/// Returns `ok(none)` if the key does not exist.
4+
get: func(key: string) -> result<option<string>, error>;
5+
6+
/// The set of errors which may be raised by functions in this interface
7+
variant error {
8+
/// The requesting component does not have access to the specified key
9+
/// (which may or may not exist).
10+
access-denied,
11+
/// Decryption error.
12+
decrypt-error,
13+
/// Some implementation-specific error has occurred (e.g. I/O)
14+
other(string)
15+
}
16+
}

wit/world.wit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package gcore:fastedge;
22

3-
world http-reactor {
3+
world reactor {
44
import http;
55
import http-client;
66

77
import dictionary;
8+
import secret;
89

910
export http-handler;
1011
}

0 commit comments

Comments
 (0)