Browse Source

Follow microrm changes.

Kestrel 1 year ago
parent
commit
da132ef25f
5 changed files with 70 additions and 106 deletions
  1. 36 75
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 20 19
      src/cert.rs
  4. 10 8
      src/cli.rs
  5. 3 3
      src/schema.rs

+ 36 - 75
Cargo.lock

@@ -56,17 +56,6 @@ dependencies = [
  "opaque-debug",
  "opaque-debug",
 ]
 ]
 
 
-[[package]]
-name = "ahash"
-version = "0.7.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
-dependencies = [
- "getrandom 0.2.6",
- "once_cell",
- "version_check",
-]
-
 [[package]]
 [[package]]
 name = "anyhow"
 name = "anyhow"
 version = "1.0.57"
 version = "1.0.57"
@@ -649,18 +638,6 @@ version = "2.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"
 checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"
 
 
-[[package]]
-name = "fallible-iterator"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
-
-[[package]]
-name = "fallible-streaming-iterator"
-version = "0.1.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
-
 [[package]]
 [[package]]
 name = "fastrand"
 name = "fastrand"
 version = "1.7.0"
 version = "1.7.0"
@@ -822,18 +799,6 @@ name = "hashbrown"
 version = "0.11.2"
 version = "0.11.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
 checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
-dependencies = [
- "ahash",
-]
-
-[[package]]
-name = "hashlink"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf"
-dependencies = [
- "hashbrown",
-]
 
 
 [[package]]
 [[package]]
 name = "heck"
 name = "heck"
@@ -993,16 +958,6 @@ version = "0.2.125"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
 checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
 
 
-[[package]]
-name = "libsqlite3-sys"
-version = "0.24.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "898745e570c7d0453cc1fbc4a701eb6c662ed54e8fec8b7d14be137ebeeb9d14"
-dependencies = [
- "pkg-config",
- "vcpkg",
-]
-
 [[package]]
 [[package]]
 name = "log"
 name = "log"
 version = "0.4.17"
 version = "0.4.17"
@@ -1028,19 +983,22 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
 
 
 [[package]]
 [[package]]
 name = "microrm"
 name = "microrm"
-version = "0.1.0"
+version = "0.3.7"
 dependencies = [
 dependencies = [
  "base64 0.13.0",
  "base64 0.13.0",
+ "lazy_static",
+ "log",
  "microrm-macros",
  "microrm-macros",
- "rusqlite",
  "serde",
  "serde",
  "serde_bytes",
  "serde_bytes",
+ "serde_json",
  "sha2 0.10.2",
  "sha2 0.10.2",
+ "sqlite",
 ]
 ]
 
 
 [[package]]
 [[package]]
 name = "microrm-macros"
 name = "microrm-macros"
-version = "0.1.0"
+version = "0.2.4"
 dependencies = [
 dependencies = [
  "convert_case",
  "convert_case",
  "proc-macro2",
  "proc-macro2",
@@ -1321,21 +1279,6 @@ version = "0.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "56770675ebc04927ded3e60633437841581c285dc6236109ea25fbf3beb7b59e"
 checksum = "56770675ebc04927ded3e60633437841581c285dc6236109ea25fbf3beb7b59e"
 
 
-[[package]]
-name = "rusqlite"
-version = "0.27.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "85127183a999f7db96d1a976a309eebbfb6ea3b0b400ddd8340190129de6eb7a"
-dependencies = [
- "bitflags",
- "fallible-iterator",
- "fallible-streaming-iterator",
- "hashlink",
- "libsqlite3-sys",
- "memchr",
- "smallvec",
-]
-
 [[package]]
 [[package]]
 name = "rustc_version"
 name = "rustc_version"
 version = "0.2.3"
 version = "0.2.3"
@@ -1511,12 +1454,6 @@ version = "0.4.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32"
 checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32"
 
 
-[[package]]
-name = "smallvec"
-version = "1.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
-
 [[package]]
 [[package]]
 name = "socket2"
 name = "socket2"
 version = "0.4.4"
 version = "0.4.4"
@@ -1533,6 +1470,36 @@ version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
 checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
 
 
+[[package]]
+name = "sqlite"
+version = "0.26.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fb1a534c07ec276fbbe0e55a1c00814d8563da3a2f4d9d9d4c802bd1278db6a"
+dependencies = [
+ "libc",
+ "sqlite3-sys",
+]
+
+[[package]]
+name = "sqlite3-src"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a260b07ce75a0644c6f5891f34f46db9869e731838e95293469ab17999abcfa3"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
+
+[[package]]
+name = "sqlite3-sys"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04d2f028faeb14352df7934b4771806f60d61ce61be1928ec92396d7492e2e54"
+dependencies = [
+ "libc",
+ "sqlite3-src",
+]
+
 [[package]]
 [[package]]
 name = "standback"
 name = "standback"
 version = "0.2.17"
 version = "0.2.17"
@@ -1815,12 +1782,6 @@ dependencies = [
  "version_check",
  "version_check",
 ]
 ]
 
 
-[[package]]
-name = "vcpkg"
-version = "0.2.15"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
-
 [[package]]
 [[package]]
 name = "version_check"
 name = "version_check"
 version = "0.9.4"
 version = "0.9.4"

+ 1 - 1
Cargo.toml

@@ -16,7 +16,7 @@ sha2 = { version = "0.10.2" }
 base64 = { version = "0.13.0" }
 base64 = { version = "0.13.0" }
 
 
 # Data storage dependencies
 # Data storage dependencies
-microrm = { path = "../microrm/microrm" }
+microrm = { version = "0.3", path = "../microrm/microrm" }
 
 
 # Public API dependencies
 # Public API dependencies
 tide = { version = "0.16.0" }
 tide = { version = "0.16.0" }

+ 20 - 19
src/cert.rs

@@ -2,26 +2,26 @@ use std::collections::HashMap;
 use ring::signature::Ed25519KeyPair;
 use ring::signature::Ed25519KeyPair;
 use sha2::Digest;
 use sha2::Digest;
 use crate::schema;
 use crate::schema;
+use microrm::prelude::*;
 
 
-pub struct CertStore<'a> {
-    db: &'a crate::DB,
+pub struct CertStore {
     keys: HashMap<String, Ed25519KeyPair>,
     keys: HashMap<String, Ed25519KeyPair>,
 }
 }
 
 
-impl<'a> CertStore<'a> {
-    pub fn new(db: &'a crate::DB) -> Self {
-        Self { db, keys: HashMap::new() }
+impl CertStore {
+    pub fn new() -> Self {
+        Self { keys: HashMap::new() }
     }
     }
 
 
-    fn realm_id(&self, realm_name: &str) -> Option<schema::RealmID> {
-        microrm::query::get_one_by(self.db, schema::RealmColumns::Shortname, realm_name).map(|x| x.id())
+    fn realm_id(&self, qi: &microrm::QueryInterface, realm_name: &str) -> Option<schema::RealmID> {
+        qi.get().by(schema::Realm::Shortname, realm_name).one().ok()?.map(|v| v.id())
     }
     }
 
 
-    pub fn generate_in(&self, realm_name: &str) -> Result<String, &'static str> {
+    pub fn generate_in(&self, qi: &microrm::QueryInterface, realm_name: &str) -> Result<String, &'static str> {
         let mut rng = ring::rand::SystemRandom::new();
         let mut rng = ring::rand::SystemRandom::new();
         let sign_generated = Ed25519KeyPair::generate_pkcs8(&mut rng);
         let sign_generated = Ed25519KeyPair::generate_pkcs8(&mut rng);
 
 
-        let realm_id = self.realm_id(realm_name).ok_or("Failed to find realm")?;
+        let realm_id = self.realm_id(qi, realm_name).ok_or("Failed to find realm")?;
 
 
         if let Err(_) = sign_generated {
         if let Err(_) = sign_generated {
             return Err("Failed to generate key")
             return Err("Failed to generate key")
@@ -35,16 +35,16 @@ impl<'a> CertStore<'a> {
         let mut key_id = base64::encode(key_hasher.finalize());
         let mut key_id = base64::encode(key_hasher.finalize());
         key_id.truncate(16);
         key_id.truncate(16);
 
 
-        microrm::query::add(self.db, &schema::Key { realm: realm_id, key_id: key_id.clone(), keydata }).expect("Couldn't add key");
+        qi.add(&schema::Key { realm: realm_id, key_id: key_id.clone(), keydata }).expect("Couldn't add key");
 
 
         Ok(key_id)
         Ok(key_id)
     }
     }
 }
 }
 
 
-pub fn inspect(db: &crate::DB, realm_name: &str) {
-    let cs = CertStore::new(db);
-    println!("Certstore loaded.");
-    let realm = microrm::query::get_one_by(db, schema::RealmColumns::Shortname, realm_name);
+pub fn inspect(db: &microrm::DB, realm_name: &str) {
+    let qi = db.query_interface();
+
+    let realm = qi.get().by(schema::Realm::Shortname, realm_name).one().expect("couldn't load realm");
     if realm.is_none() {
     if realm.is_none() {
         println!("No such realm {}", realm_name);
         println!("No such realm {}", realm_name);
         return
         return
@@ -52,15 +52,16 @@ pub fn inspect(db: &crate::DB, realm_name: &str) {
     let realm = realm.unwrap();
     let realm = realm.unwrap();
 
 
     println!("Retrieving keys for {} realm...", realm_name);
     println!("Retrieving keys for {} realm...", realm_name);
-    let keys = microrm::query::get_all_by(db, schema::KeyColumns::Realm, realm.id()).expect("Can get keys");
+    let keys = qi.get().by(schema::Key::Realm, &realm.id()).all().expect("couldn't load keys"); // microrm::query::get_all_by(db, schema::KeyColumns::Realm, realm.id()).expect("Can get keys");
     for key in keys {
     for key in keys {
-        println!("[{:20}]", key.key_id);
+        println!("- [{:20}]", key.key_id);
     }
     }
 }
 }
 
 
-pub fn generate(db: &crate::DB, realm_name: &str) {
-    let cs = CertStore::new(db);
-    if let Err(e) = cs.generate_in(realm_name) {
+pub fn generate(db: &microrm::DB, realm_name: &str) {
+    let cs = CertStore::new();
+    let qi = db.query_interface();
+    if let Err(e) = cs.generate_in(&qi, realm_name) {
         println!("Failed to generate key: {}", e);
         println!("Failed to generate key: {}", e);
     }
     }
 }
 }

+ 10 - 8
src/cli.rs

@@ -1,4 +1,5 @@
 use clap::{Parser,Subcommand};
 use clap::{Parser,Subcommand};
+use crate::{schema,cert};
 
 
 #[derive(Debug, Parser)]
 #[derive(Debug, Parser)]
 #[clap(author, version, about, long_about = None)]
 #[clap(author, version, about, long_about = None)]
@@ -27,7 +28,7 @@ impl RootArgs {
             return self.init().await;
             return self.init().await;
         }
         }
 
 
-        let storage = crate::DB::new(crate::schema::schema(), &self.db, false);
+        let storage = microrm::DB::new(schema::schema(), &self.db, microrm::CreateMode::MustExist);
 
 
         if let Err(e) = storage {
         if let Err(e) = storage {
             println!("Error occured while loading database: {}", e);
             println!("Error occured while loading database: {}", e);
@@ -44,7 +45,7 @@ impl RootArgs {
 
 
     async fn init(&self) {
     async fn init(&self) {
         // first check to see if the database is already vaguely set up
         // first check to see if the database is already vaguely set up
-        let maybedb = crate::DB::new(crate::schema::schema(), &self.db, false);
+        let maybedb = microrm::DB::new(schema::schema(), &self.db, microrm::CreateMode::MustExist);
 
 
         if maybedb.is_ok() {
         if maybedb.is_ok() {
             println!("Database already initialized, not overwriting!");
             println!("Database already initialized, not overwriting!");
@@ -53,10 +54,11 @@ impl RootArgs {
 
 
         println!("Initializing!");
         println!("Initializing!");
 
 
-        let db = crate::DB::new(crate::schema::schema(), &self.db, true).expect("Unable to initialize database!");
+        let db = microrm::DB::new(schema::schema(), &self.db, microrm::CreateMode::AllowNewDatabase).expect("Unable to initialize database!");
 
 
         // create primary realm
         // create primary realm
-        microrm::query::add(&db, &crate::schema::Realm { shortname: "primary".to_string() });
+        let qi = db.query_interface();
+        qi.add(&schema::Realm { shortname: "primary".to_string() }).expect("couldn't add primary realm");
     }
     }
 }
 }
 
 
@@ -73,13 +75,13 @@ struct CertArgs {
 }
 }
 
 
 impl CertArgs {
 impl CertArgs {
-    async fn run(&self, root: &RootArgs, si: &crate::DB) {
+    async fn run(&self, root: &RootArgs, si: &microrm::DB) {
         match &self.command {
         match &self.command {
             CertCommand::Inspect => {
             CertCommand::Inspect => {
-                crate::cert::inspect(si, &root.realm);
+                cert::inspect(si, &root.realm);
             }
             }
             CertCommand::Generate => {
             CertCommand::Generate => {
-                crate::cert::generate(si, &root.realm);
+                cert::generate(si, &root.realm);
             }
             }
         }
         }
     }
     }
@@ -91,7 +93,7 @@ struct ServerArgs {
 }
 }
 
 
 impl ServerArgs {
 impl ServerArgs {
-    async fn run(&self, root: &RootArgs, _si: &crate::DB) {
+    async fn run(&self, root: &RootArgs, _si: &microrm::DB) {
         
         
     }
     }
 }
 }

+ 3 - 3
src/schema.rs

@@ -1,5 +1,5 @@
 use serde::{Serialize,Deserialize};
 use serde::{Serialize,Deserialize};
-pub use microrm::{Entity, model::SchemaModel};
+pub use microrm::{Entity, Schema};
 
 
 #[derive(Entity,Serialize,Deserialize)]
 #[derive(Entity,Serialize,Deserialize)]
 pub struct Realm {
 pub struct Realm {
@@ -53,8 +53,8 @@ pub struct Role {
     pub shortname: String,
     pub shortname: String,
 }
 }
 
 
-pub fn schema() -> SchemaModel {
-    SchemaModel::new()
+pub fn schema() -> Schema {
+    Schema::new()
         .add::<Realm>()
         .add::<Realm>()
         .add::<Key>()
         .add::<Key>()
         .add::<User>()
         .add::<User>()