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

wip: datalog foreign function interface prototype #228

Draft
wants to merge 7 commits into
base: v5
Choose a base branch
from
Draft
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
69 changes: 68 additions & 1 deletion biscuit-auth/examples/testcases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
use biscuit::Authorizer;
use biscuit::{builder::*, builder_ext::*, Biscuit};
use biscuit::{KeyPair, PrivateKey, PublicKey};
use biscuit_auth::builder;
use biscuit_auth::datalog::ExternFunc;
use biscuit_auth::datalog::RunLimits;
use prost::Message;
use rand::prelude::*;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;
use std::{
collections::{BTreeMap, BTreeSet},
fs::File,
Expand Down Expand Up @@ -157,6 +162,9 @@
add_test_result(&mut results, type_of(&target, &root, test));

add_test_result(&mut results, array_map(&target, &root, test));

add_test_result(&mut results, ffi(&target, &root, test));

if json {
let s = serde_json::to_string_pretty(&TestCases {
root_private_key: hex::encode(root.private().to_bytes()),
Expand Down Expand Up @@ -297,6 +305,15 @@
}

fn validate_token(root: &KeyPair, data: &[u8], authorizer_code: &str) -> Validation {
validate_token_with_limits(root, data, authorizer_code, RunLimits::default())
}

fn validate_token_with_limits(
root: &KeyPair,
data: &[u8],
authorizer_code: &str,
run_limits: RunLimits,
) -> Validation {
let token = match Biscuit::from(&data[..], &root.public()) {
Ok(t) => t,
Err(e) => {
Expand Down Expand Up @@ -331,7 +348,7 @@
}
};

let res = authorizer.authorize();
let res = authorizer.authorize_with_limits(run_limits);
//println!("authorizer world:\n{}", authorizer.print_world());
let (_, _, _, policies) = authorizer.dump();
let snapshot = authorizer.snapshot().unwrap();
Expand Down Expand Up @@ -2269,6 +2286,56 @@
}
}

fn ffi(target: &str, root: &KeyPair, test: bool) -> TestResult {
let mut rng: StdRng = SeedableRng::seed_from_u64(1234);
let title = "test ffi calls (v6 blocks)".to_string();
let filename = "test035_ffi".to_string();
let token;

let biscuit =
biscuit!(r#"check if true.extern::test(), "a".extern::test("a") == "equal strings""#)
.build_with_rng(&root, SymbolTable::default(), &mut rng)
.unwrap();
token = print_blocks(&biscuit);

let data = write_or_load_testcase(target, &filename, root, &biscuit, test);

let mut validations = BTreeMap::new();
validations.insert(
"".to_string(),
validate_token_with_limits(
root,
&data[..],
"allow if true",
RunLimits {
extern_funcs: HashMap::from([(
"test".to_string(),
ExternFunc::new(Arc::new(|left, right| match (left, right) {
(t, None) => Ok(t),
(builder::Term::Str(left), Some(builder::Term::Str(right)))
if left == right =>
{
Ok(builder::Term::Str("equal strings".to_string()))
}
(builder::Term::Str(_), Some(builder::Term::Str(_))) => {
Ok(builder::Term::Str("different strings".to_string()))

Check warning on line 2321 in biscuit-auth/examples/testcases.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/examples/testcases.rs#L2321

Added line #L2321 was not covered by tests
}
_ => Err("unsupported operands".to_string()),

Check warning on line 2323 in biscuit-auth/examples/testcases.rs

View check run for this annotation

Codecov / codecov/patch

biscuit-auth/examples/testcases.rs#L2323

Added line #L2323 was not covered by tests
})),
)]),
..Default::default()
},
),
);

TestResult {
title,
filename,
token,
validations,
}
}

fn print_blocks(token: &Biscuit) -> Vec<BlockContent> {
let mut v = Vec::new();

Expand Down
48 changes: 48 additions & 0 deletions biscuit-auth/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3139,3 +3139,51 @@ World {

result: `Ok(0)`


------------------------------

## test ffi calls (v6 blocks): test035_ffi.bc
### token

authority:
symbols: ["test", "a", "equal strings"]

public keys: []

```
check if true.extern::test(), "a".extern::test("a") == "equal strings";
```

### validation

authorizer code:
```
allow if true;
```

revocation ids:
- `faf26fe6f5dfa08c114a0a29321405b6fb7be79b0d80694d27925f7deb01effe5707600e42fd74f9a1d2920466446d51949155f4548f0fd68f3e9326c7e12404`

authorizer world:
```
World {
facts: []
rules: []
checks: [
Checks {
origin: Some(
0,
),
checks: [
"check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\"",
],
},
]
policies: [
"allow if true",
]
}
```

result: `Ok(0)`

42 changes: 42 additions & 0 deletions biscuit-auth/samples/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,48 @@
]
}
}
},
{
"title": "test ffi calls (v6 blocks)",
"filename": "test035_ffi.bc",
"token": [
{
"symbols": [
"test",
"a",
"equal strings"
],
"public_keys": [],
"external_key": null,
"code": "check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\";\n"
}
],
"validations": {
"": {
"world": {
"facts": [],
"rules": [],
"checks": [
{
"origin": 0,
"checks": [
"check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\""
]
}
],
"policies": [
"allow if true"
]
},
"result": {
"Ok": 0
},
"authorizer_code": "allow if true;\n",
"revocation_ids": [
"faf26fe6f5dfa08c114a0a29321405b6fb7be79b0d80694d27925f7deb01effe5707600e42fd74f9a1d2920466446d51949155f4548f0fd68f3e9326c7e12404"
]
}
}
}
]
}
Binary file added biscuit-auth/samples/test035_ffi.bc
Binary file not shown.
Loading
Loading