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 sha256 support to rhai #4940

Merged
merged 8 commits into from
Apr 16, 2024
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
17 changes: 17 additions & 0 deletions .changesets/feat_feat_sha256_in_rhai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Add support for SHA256 hashing in Rhai ([Issue #4939](https://github.com/apollographql/router/issues/4939))

This adds a new `sha256` module to create SHA256 hashes within Rhai scripts. An example looks like:

```rs
fn supergraph_service(service){
service.map_request(|request|{
log_info("hello world");
let sha = sha256::digest("hello world");
log_info(sha);
});
}
```

The only function currently is `digest`.

By [@lleadbet](https://github.com/lleadbet) in https://github.com/apollographql/router/pull/4940
14 changes: 14 additions & 0 deletions apollo-router/src/plugins/rhai/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ mod router_json {
}
}

#[export_module]
mod router_sha256 {
use sha2::Digest;

#[rhai_fn(pure)]
pub(crate) fn digest(input: &mut ImmutableString) -> String {
let hash = sha2::Sha256::digest(input.as_bytes());
hex::encode(hash)
}
}

#[export_module]
mod router_expansion {
pub(crate) type Expansion = expansion::Expansion;
Expand Down Expand Up @@ -1625,6 +1636,7 @@ impl Rhai {

let base64_module = exported_module!(router_base64);
let json_module = exported_module!(router_json);
let sha256_module = exported_module!(router_sha256);

let expansion_module = exported_module!(router_expansion);

Expand All @@ -1651,6 +1663,8 @@ impl Rhai {
.register_static_module("base64", base64_module.into())
// Register our json module (not global)
.register_static_module("json", json_module.into())
// Register our SHA256 module (not global)
.register_static_module("sha256", sha256_module.into())
// Register our expansion module (not global)
// Hide the fact that it is an expansion module by calling it "env"
.register_static_module("env", expansion_module.into())
Expand Down
11 changes: 11 additions & 0 deletions apollo-router/src/plugins/rhai/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use http::StatusCode;
use rhai::Engine;
use rhai::EvalAltResult;
use serde_json::Value;
use sha2::Digest;
use tower::util::BoxService;
use tower::BoxError;
use tower::Service;
Expand Down Expand Up @@ -584,6 +585,16 @@ fn it_can_generate_uuid() {
assert_eq!(uuid_v4_rhai, uuid_parsed.to_string());
}

#[test]
fn it_can_sha256_string() {
let engine = new_rhai_test_engine();
let hash = sha2::Sha256::digest("hello world".as_bytes());
let hash_rhai: String = engine
.eval(r#"sha256::digest("hello world")"#)
.expect("can decode string");
assert_eq!(hash_rhai, hex::encode(hash));
}

async fn base_globals_function(fn_name: &str) -> Result<bool, Box<rhai::EvalAltResult>> {
let dyn_plugin: Box<dyn DynPlugin> = crate::plugin::plugins()
.find(|factory| factory.name == "apollo.rhai")
Expand Down
18 changes: 18 additions & 0 deletions docs/source/customizations/rhai-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ You don't need to import the "base64" module. It is imported in the router.

lleadbet marked this conversation as resolved.
Show resolved Hide resolved
</Note>

## sha256 hash strings

Your Rhai customization can use the function `sha256::digest()` to hash strings using the SHA256 hashing algorithm.

```rhai
fn supergraph_service(service){
service.map_request(|request|{
let sha = sha256::digest("hello world");
log_info(sha);
});
}
```
<Note>

You don't need to import the "sha256" module. It is imported in the router.

</Note>

### Different alphabets

Base64 supports multiple alphabets to encode data, depending on the supported characters where it is used. The router supports the following alphabets:
Expand Down
Loading