Skip to content

Commit 00c676d

Browse files
authored
Merge pull request #16 from ivoire/limits
Add Authorizer (set_)limits bindings
2 parents 69a7bee + 1c0a5ea commit 00c676d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

biscuit_test.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import os
3-
from datetime import datetime, timezone
3+
from datetime import datetime, timedelta, timezone
44

55
import pytest
66

@@ -205,6 +205,16 @@ def test_authorizer_builder():
205205
allow if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
206206
"""
207207

208+
209+
def test_authorizer_limits():
210+
auth = Authorizer("")
211+
limits = auth.limits()
212+
limits.max_time = timedelta(microseconds=2000)
213+
auth.set_limits(limits)
214+
limits = auth.limits()
215+
assert limits.max_time.microseconds == 2000
216+
217+
208218
def test_key_selection():
209219
private_key = PrivateKey.from_hex("473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97")
210220
root = KeyPair.from_private_key(private_key)
@@ -431,4 +441,4 @@ def test_keypair_from_private_key_pem():
431441
private_key_pem = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIASZaU0NoF3KxABSZj5x1QwVOUZfiSbf6SAzz3qq1T1l\n-----END PRIVATE KEY-----"
432442
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
433443
kp = KeyPair.from_private_key_pem(pem=private_key_pem)
434-
assert kp.private_key.to_hex() == private_key_hex
444+
assert kp.private_key.to_hex() == private_key_hex

src/lib.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
use ::biscuit_auth::RootKeyProvider;
44
use ::biscuit_auth::UnverifiedBiscuit;
55
use chrono::DateTime;
6+
use chrono::Duration;
67
use chrono::TimeZone;
78
use chrono::Utc;
89
use std::collections::BTreeSet;
910
use std::collections::HashMap;
1011

11-
use ::biscuit_auth::{builder, error, Authorizer, Biscuit, KeyPair, PrivateKey, PublicKey};
12+
use ::biscuit_auth::{
13+
builder, error, Authorizer, AuthorizerLimits, Biscuit, KeyPair, PrivateKey, PublicKey,
14+
};
1215

1316
use pyo3::exceptions::PyValueError;
1417
use pyo3::prelude::*;
@@ -341,6 +344,17 @@ impl PyBiscuit {
341344
#[pyclass(name = "Authorizer")]
342345
pub struct PyAuthorizer(Authorizer);
343346

347+
#[pyclass(name = "AuthorizerLimits")]
348+
#[derive(Clone)]
349+
pub struct PyAuthorizerLimits {
350+
#[pyo3(get, set)]
351+
pub max_facts: u64,
352+
#[pyo3(get, set)]
353+
pub max_iterations: u64,
354+
#[pyo3(get, set)]
355+
pub max_time: Duration,
356+
}
357+
344358
#[pymethods]
345359
impl PyAuthorizer {
346360
/// Create a new authorizer from a datalog snippet and optional parameter values
@@ -446,6 +460,29 @@ impl PyAuthorizer {
446460
.map_err(|e| DataLogError::new_err(e.to_string()))
447461
}
448462

463+
/// Returns the runtime limits of the authorizer
464+
///
465+
/// Those limits cover all the executions under the `authorize`, `query` and `query_all` methods
466+
pub fn limits(&self) -> PyAuthorizerLimits {
467+
let limits = self.0.limits();
468+
PyAuthorizerLimits {
469+
max_facts: limits.max_facts,
470+
max_iterations: limits.max_iterations,
471+
max_time: Duration::from_std(limits.max_time).expect("Duration out of range"),
472+
}
473+
}
474+
475+
/// Sets the runtime limits of the authorizer
476+
///
477+
/// Those limits cover all the executions under the `authorize`, `query` and `query_all` methods
478+
pub fn set_limits(&mut self, limits: &PyAuthorizerLimits) {
479+
self.0.set_limits(AuthorizerLimits {
480+
max_facts: limits.max_facts,
481+
max_iterations: limits.max_iterations,
482+
max_time: Duration::to_std(&limits.max_time).expect("Duration out of range"),
483+
})
484+
}
485+
449486
/// Merge another `Authorizer` in this `Authorizer`. The `Authorizer` argument will not be modified
450487
///
451488
/// :param builder: an Authorizer

0 commit comments

Comments
 (0)