From 26b2888a09d6d9d2ecb8fe4fec9df64a49758215 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Fri, 26 Nov 2021 21:01:43 +0100 Subject: [PATCH] DB Refactor --- Cargo.lock | 106 ++++++++++++++++++++ Cargo.toml | 5 + api/schema | 2 +- bffhd/db.rs | 18 +--- bffhd/lib.rs | 4 + bffhd/resource/mod.rs | 5 +- bffhd/{db/state.rs => resource/state/db.rs} | 2 +- bffhd/resource/state/mod.rs | 1 + bffhd/server/mod.rs | 2 + bffhd/server/tls.rs | 0 bffhd/{db/user.rs => users/db.rs} | 3 +- bffhd/users/mod.rs | 2 + bffhd/utils/l10nstring.rs | 61 +++++++++++ bffhd/utils/mod.rs | 7 +- bffhd/utils/uuid.rs | 20 ++++ 15 files changed, 216 insertions(+), 22 deletions(-) rename bffhd/{db/state.rs => resource/state/db.rs} (99%) create mode 100644 bffhd/server/mod.rs create mode 100644 bffhd/server/tls.rs rename bffhd/{db/user.rs => users/db.rs} (96%) create mode 100644 bffhd/users/mod.rs create mode 100644 bffhd/utils/l10nstring.rs create mode 100644 bffhd/utils/uuid.rs diff --git a/Cargo.lock b/Cargo.lock index e17a3fd..d94a644 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -241,6 +241,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "async-rustls" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c86f33abd5a4f3e2d6d9251a9e0c6a7e52eb1113caf893dae8429bf4a53f378" +dependencies = [ + "futures-lite", + "rustls 0.19.1", + "webpki 0.21.4", +] + [[package]] name = "async-std" version = "1.10.0" @@ -828,6 +839,7 @@ dependencies = [ "async-channel", "async-native-tls", "async-oneshot", + "async-rustls", "async-trait", "bincode", "capnp", @@ -843,6 +855,7 @@ dependencies = [ "lazy_static", "libc", "lmdb-rkv", + "once_cell", "pin-utils", "ptr_meta", "rand", @@ -851,6 +864,7 @@ dependencies = [ "rkyv_typename", "rsasl", "rust-argon2", + "rustls 0.20.2", "serde", "serde_dhall", "serde_json", @@ -2019,6 +2033,21 @@ dependencies = [ "bytecheck", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi", +] + [[package]] name = "rkyv" version = "0.7.20" @@ -2127,6 +2156,31 @@ dependencies = [ "semver", ] +[[package]] +name = "rustls" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7" +dependencies = [ + "base64", + "log", + "ring", + "sct 0.6.1", + "webpki 0.21.4", +] + +[[package]] +name = "rustls" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d37e5e2290f3e040b594b1a9e04377c2c671f1a1cfd9bfdef82106ac1c113f84" +dependencies = [ + "log", + "ring", + "sct 0.7.0", + "webpki 0.22.0", +] + [[package]] name = "ryu" version = "1.0.5" @@ -2158,6 +2212,26 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sct" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "sdk" version = "0.1.0" @@ -2356,6 +2430,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -2615,6 +2695,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "url" version = "2.2.2" @@ -2764,6 +2850,26 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "wepoll-ffi" version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index bfb2c49..5b46990 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,6 +78,11 @@ erased-serde = "0.3" serde_dhall = { version = "0.10.1", default-features = false } serde_json = "1.0" +once_cell = "1.8" + +rustls = "0.20" +async-rustls = "0.2" + [dev-dependencies] futures-test = "0.3.16" tempfile = "3.2" diff --git a/api/schema b/api/schema index f713df2..a13478a 160000 --- a/api/schema +++ b/api/schema @@ -1 +1 @@ -Subproject commit f713df2221727f2f9535be4c4c9ae36de77c1a63 +Subproject commit a13478a3f00d6f00580dc344d3a697d90bc50377 diff --git a/bffhd/db.rs b/bffhd/db.rs index 7d13a5f..7a4659c 100644 --- a/bffhd/db.rs +++ b/bffhd/db.rs @@ -17,7 +17,7 @@ pub use lmdb::{ use rkyv::{Fallible, Serialize, ser::serializers::AllocSerializer, AlignedVec, Archived}; mod raw; -use raw::RawDB; +pub use raw::RawDB; mod typed; // re-exports @@ -38,11 +38,6 @@ pub use hash::{ mod fix; pub use fix::LMDBorrow; -pub mod state; -pub use state::{ - StateDB, -}; - mod resources; pub use resources::{ ResourceDB, @@ -53,19 +48,14 @@ pub use pass::{ PassDB, }; -mod user; -pub use user::{ - UserDB, -}; - use lmdb::Error; use rkyv::Deserialize; use rkyv::ser::serializers::AlignedSerializer; use std::sync::Arc; use std::path::Path; -use crate::db::user::User; +use crate::users::db::{User, UserDB}; use std::collections::HashMap; -use crate::resource::state::{OwnedEntry, State}; +use crate::resource::state::{OwnedEntry, State, db::StateDB}; use std::iter::FromIterator; use std::ops::Deref; use crate::utils::oid::{ArchivedObjectIdentifier, ObjectIdentifier}; @@ -87,7 +77,7 @@ impl From for DBError { type Ser = AllocSerializer<1024>; #[derive(Clone)] -struct AllocAdapter { +pub struct AllocAdapter { phantom: PhantomData, } diff --git a/bffhd/lib.rs b/bffhd/lib.rs index e5ec24e..d81bb73 100644 --- a/bffhd/lib.rs +++ b/bffhd/lib.rs @@ -18,8 +18,12 @@ pub mod error; /// Policy decision engine pub mod permissions; +pub mod users; + /// Resources pub mod resource; pub mod resources; +pub mod server; + pub mod utils; \ No newline at end of file diff --git a/bffhd/resource/mod.rs b/bffhd/resource/mod.rs index 3ea7a74..4eb6965 100644 --- a/bffhd/resource/mod.rs +++ b/bffhd/resource/mod.rs @@ -6,13 +6,12 @@ use async_oneshot::Sender; use async_channel::Receiver; use state::State; -use crate::db::{ - state::StateAccessor, -}; +use state::db::StateAccessor; pub mod state; pub mod claim; + /// A resource in BFFH has to contain several different parts; /// - Currently set state /// - Execution state of attached actors (⇒ BFFH's job) diff --git a/bffhd/db/state.rs b/bffhd/resource/state/db.rs similarity index 99% rename from bffhd/db/state.rs rename to bffhd/resource/state/db.rs index 8db7570..b4a4279 100644 --- a/bffhd/db/state.rs +++ b/bffhd/resource/state/db.rs @@ -5,7 +5,7 @@ use std::{ use rkyv::{Archived}; -use super::{ +use crate::db::{ DB, Environment, diff --git a/bffhd/resource/state/mod.rs b/bffhd/resource/state/mod.rs index 58fe0c6..308e336 100644 --- a/bffhd/resource/state/mod.rs +++ b/bffhd/resource/state/mod.rs @@ -26,6 +26,7 @@ use crate::utils::oid::ObjectIdentifier; use crate::resource::state::value::{DynOwnedVal, DynVal, TypeOid, }; pub mod value; +pub mod db; #[derive(serde::Serialize, serde::Deserialize)] #[derive(Archive, Serialize, Deserialize)] diff --git a/bffhd/server/mod.rs b/bffhd/server/mod.rs new file mode 100644 index 0000000..8288d91 --- /dev/null +++ b/bffhd/server/mod.rs @@ -0,0 +1,2 @@ + +mod tls; \ No newline at end of file diff --git a/bffhd/server/tls.rs b/bffhd/server/tls.rs new file mode 100644 index 0000000..e69de29 diff --git a/bffhd/db/user.rs b/bffhd/users/db.rs similarity index 96% rename from bffhd/db/user.rs rename to bffhd/users/db.rs index 0434a1c..e26e366 100644 --- a/bffhd/db/user.rs +++ b/bffhd/users/db.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use super::{DB, AllocAdapter, Environment, Result}; -use crate::db::raw::RawDB; +use crate::db::{RawDB, DB, AllocAdapter, Environment, Result}; use crate::db::{DatabaseFlags, LMDBorrow, RoTransaction, WriteFlags, }; use rkyv::{Archive, Serialize, Deserialize, Archived}; diff --git a/bffhd/users/mod.rs b/bffhd/users/mod.rs new file mode 100644 index 0000000..ddbe7e8 --- /dev/null +++ b/bffhd/users/mod.rs @@ -0,0 +1,2 @@ + +pub mod db; \ No newline at end of file diff --git a/bffhd/utils/l10nstring.rs b/bffhd/utils/l10nstring.rs new file mode 100644 index 0000000..2c84259 --- /dev/null +++ b/bffhd/utils/l10nstring.rs @@ -0,0 +1,61 @@ +use std::collections::HashMap; +use capnp::capability::Promise; +use capnp::Error; +use capnp_rpc::pry; +use once_cell::sync::Lazy; +use api::utils::l10n_string as l10n; + +struct Locales { + map: HashMap<&'static str, HashMap<&'static str, &'static str>> +} + +impl Locales { + pub fn get(&self, lang: &str, msg: &str) + -> Option<(&'static str, &'static str)> + { + self.map.get(msg).and_then(|map| { + map.get_key_value(lang).map(|(k,v)| (*k, *v)) + }) + } + + pub fn available(&self, _msg: &str) -> &[&'static str] { + &[] + } +} + +static LANG: Lazy = Lazy::new(|| { + Locales { map: HashMap::new() } +}); + +struct L10NString { + msg: &'static str, +} + +impl l10n::Server for L10NString { + fn get(&mut self, params: l10n::GetParams, mut results: l10n::GetResults) + -> Promise<(), Error> + { + let lang = pry!(pry!(params.get()).get_lang()); + + if let Some((lang, content)) = LANG.get(lang, &self.msg) { + let mut builder = results.get(); + builder.set_lang(lang); + builder.set_content(content); + } + + Promise::ok(()) + } + + fn available(&mut self, _: l10n::AvailableParams, mut results: l10n::AvailableResults) + -> Promise<(), Error> + { + let langs = LANG.available(self.msg); + let builder = results.get(); + let mut lb = builder.init_langs(langs.len() as u32); + for (n, lang) in langs.into_iter().enumerate() { + lb.reborrow().set(n as u32, *lang); + } + + Promise::ok(()) + } +} \ No newline at end of file diff --git a/bffhd/utils/mod.rs b/bffhd/utils/mod.rs index 2a8538c..8dd6dcc 100644 --- a/bffhd/utils/mod.rs +++ b/bffhd/utils/mod.rs @@ -2,4 +2,9 @@ pub mod oid; /// Variable sized integer types -pub mod varint; \ No newline at end of file +pub mod varint; + +/// Localization strings +pub mod l10nstring; + +pub mod uuid; \ No newline at end of file diff --git a/bffhd/utils/uuid.rs b/bffhd/utils/uuid.rs new file mode 100644 index 0000000..19c2f06 --- /dev/null +++ b/bffhd/utils/uuid.rs @@ -0,0 +1,20 @@ +use uuid::Uuid; +use api::utils::uuid::{Builder, Reader}; + +pub fn uuid_to_api(uuid: Uuid, mut builder: Builder) { + let [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] + = uuid.as_u128().to_ne_bytes(); + let lower = u64::from_ne_bytes([a,b,c,d,e,f,g,h]); + let upper = u64::from_ne_bytes([i,j,k,l,m,n,o,p]); + builder.set_lower(lower); + builder.set_upper(upper); +} + +pub fn api_to_uuid(reader: Reader) -> Uuid { + let lower: u64 = reader.reborrow().get_lower(); + let upper: u64 = reader.get_upper(); + let [a,b,c,d,e,f,g,h] = lower.to_ne_bytes(); + let [i,j,k,l,m,n,o,p] = upper.to_ne_bytes(); + let num = u128::from_ne_bytes([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p]); + Uuid::from_u128(num) +} \ No newline at end of file