-
-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rust): move the lmdb storage
to be able to compile rust doc examples to build a node
- Loading branch information
1 parent
1f80fff
commit 7f21117
Showing
15 changed files
with
152 additions
and
101 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
implementations/rust/ockam/ockam_abac/src/storage/lmdb_storage.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::tokio::task::{spawn_blocking, JoinError}; | ||
use crate::{Action, Expr, PolicyStorage, Resource}; | ||
use core::str; | ||
use lmdb::{Cursor, Transaction}; | ||
use minicbor::{Decode, Encode}; | ||
use ockam_core::async_trait; | ||
use ockam_core::compat::boxed::Box; | ||
use ockam_core::compat::fmt::Vec; | ||
use ockam_core::errcode::{Kind, Origin}; | ||
use ockam_core::{Error, Result}; | ||
use ockam_identity::LmdbStorage; | ||
use std::borrow::Cow; | ||
use tracing as log; | ||
|
||
/// Policy storage entry. | ||
/// | ||
/// Used instead of storing plain `Expr` values to allow for additional | ||
/// metadata, versioning, etc. | ||
#[derive(Debug, Encode, Decode)] | ||
#[rustfmt::skip] | ||
struct PolicyEntry<'a> { | ||
#[b(0)] expr: Cow<'a, Expr>, | ||
} | ||
|
||
#[async_trait] | ||
impl PolicyStorage for LmdbStorage { | ||
async fn get_policy(&self, r: &Resource, a: &Action) -> Result<Option<Expr>> { | ||
let d = self.clone(); | ||
let k = format!("{r}:{a}"); | ||
let t = move || { | ||
let r = d.env.begin_ro_txn().map_err(map_lmdb_err)?; | ||
match r.get(d.map, &k) { | ||
Ok(value) => { | ||
let e: PolicyEntry = minicbor::decode(value)?; | ||
Ok(Some(e.expr.into_owned())) | ||
} | ||
Err(lmdb::Error::NotFound) => Ok(None), | ||
Err(e) => Err(map_lmdb_err(e)), | ||
} | ||
}; | ||
spawn_blocking(t).await.map_err(map_join_err)? | ||
} | ||
|
||
async fn set_policy(&self, r: &Resource, a: &Action, c: &Expr) -> Result<()> { | ||
let v = minicbor::to_vec(PolicyEntry { | ||
expr: Cow::Borrowed(c), | ||
})?; | ||
self.write(format!("{r}:{a}"), v).await | ||
} | ||
|
||
async fn del_policy(&self, r: &Resource, a: &Action) -> Result<()> { | ||
self.delete(format!("{r}:{a}")).await | ||
} | ||
|
||
async fn policies(&self, r: &Resource) -> Result<Vec<(Action, Expr)>> { | ||
let d = self.clone(); | ||
let r = r.clone(); | ||
let t = move || { | ||
let tx = d.env.begin_ro_txn().map_err(map_lmdb_err)?; | ||
let mut c = tx.open_ro_cursor(d.map).map_err(map_lmdb_err)?; | ||
let mut xs = Vec::new(); | ||
for entry in c.iter_from(r.as_str()) { | ||
let (k, v) = entry.map_err(map_lmdb_err)?; | ||
let ks = str::from_utf8(k).map_err(from_utf8_err)?; | ||
if let Some((prefix, a)) = ks.split_once(':') { | ||
if prefix != r.as_str() { | ||
break; | ||
} | ||
let x: PolicyEntry = minicbor::decode(v)?; | ||
xs.push((Action::new(a), x.expr.into_owned())) | ||
} else { | ||
log::warn!(key = %ks, "malformed key in policy database") | ||
} | ||
} | ||
Ok(xs) | ||
}; | ||
spawn_blocking(t).await.map_err(map_join_err)? | ||
} | ||
} | ||
|
||
fn map_join_err(err: JoinError) -> Error { | ||
Error::new(Origin::Application, Kind::Io, err) | ||
} | ||
|
||
fn map_lmdb_err(err: lmdb::Error) -> Error { | ||
Error::new(Origin::Application, Kind::Io, err) | ||
} | ||
|
||
fn from_utf8_err(err: str::Utf8Error) -> Error { | ||
Error::new(Origin::Other, Kind::Invalid, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[cfg(feature = "std")] | ||
pub mod lmdb_storage; | ||
|
||
#[cfg(feature = "std")] | ||
pub use lmdb_storage::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.