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 base64 decode/encode to rhai #2394

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ Description! And a link to a [reference](http://url)
By [@USERNAME](https://github.com/USERNAME) in https://github.com/apollographql/router/pull/PULL_NUMBER
-->

## 🚀 Features

### Add support for `base64encode()` / `base64decode()` in Rhai ([Issue #2025](https://github.com/apollographql/router/issues/2025))

Two new functions, `base64encode()` and `base64decode()` may now be used to Base64-encode or Base64-decode strings, respectively.

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/2394

26 changes: 26 additions & 0 deletions apollo-router/src/plugins/rhai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,14 @@ impl Rhai {
TraceId::maybe_new().ok_or_else(|| "trace unavailable".into())
})
.register_fn("to_string", |id: &mut TraceId| -> String { id.to_string() })
// Register basic "crypto" functions
// Base64 decode/encode
.register_fn("base64decode", |input: &mut ImmutableString| -> Result<String, Box<EvalAltResult>> {
garypen marked this conversation as resolved.
Show resolved Hide resolved
String::from_utf8(base64::decode(input.as_bytes()).map_err(|e| e.to_string())?).map_err(|e| e.to_string().into())
})
.register_fn("base64encode", |input: &mut ImmutableString| -> String {
base64::encode(input.as_bytes())
})
// Register a series of logging functions
.register_fn("log_trace", |out: Dynamic| {
tracing::trace!(%out, "rhai_trace");
Expand Down Expand Up @@ -2096,6 +2104,24 @@ mod tests {
assert_eq!(decoded, "This has an ümlaut in it.");
}

#[test]
fn it_can_base64encode_string() {
let engine = Rhai::new_rhai_engine(None);
let encoded: String = engine
.eval(r#"base64encode("This has an ümlaut in it.")"#)
.expect("can encode string");
assert_eq!(encoded, "VGhpcyBoYXMgYW4gw7xtbGF1dCBpbiBpdC4=");
}

#[test]
fn it_can_base64decode_string() {
let engine = Rhai::new_rhai_engine(None);
let decoded: String = engine
.eval(r#"base64decode("VGhpcyBoYXMgYW4gw7xtbGF1dCBpbiBpdC4=")"#)
.expect("can decode string");
assert_eq!(decoded, "This has an ümlaut in it.");
}

async fn base_process_function(fn_name: &str) -> Result<(), Box<rhai::EvalAltResult>> {
let dyn_plugin: Box<dyn DynPlugin> = crate::plugin::plugins()
.find(|factory| factory.name == "apollo.rhai")
Expand Down
21 changes: 21 additions & 0 deletions docs/source/customizations/rhai-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ fn supergraph_service(service) {
}
```

## base64 encode/decode strings

Your Rhai customization can use the functions `base64encode()` and `base64decode()` to encode/decode strings. `encode()` does not fail, but `decode()` can fail, so always handle exceptions when using the `decode()` function.

```rhai
fn supergraph_service(service) {
let original = "alice and bob";
let encoded = base64encode(original);
// encoded will be "YWxpY2UgYW5kIGJvYgo="
try {
let and_back = base64decode(encoded);
// and_back will be "alice and bob"
}
catch(err)
{
// log any errors
log_error(`base64decode error: ${err}`);
}
}
```

### Headers with multiple values

The simple get/set api for dealing with single value headers is sufficient for most use cases. If you wish to set multiple values on a key then you should do this by supplying an array of values.
Expand Down