|
@@ -1,20 +1,27 @@
|
|
-use std::collections::HashMap;
|
|
|
|
|
|
+use crate::schema;
|
|
use ring::signature::Ed25519KeyPair;
|
|
use ring::signature::Ed25519KeyPair;
|
|
use sha2::Digest;
|
|
use sha2::Digest;
|
|
-use crate::schema;
|
|
|
|
|
|
+use std::collections::HashMap;
|
|
|
|
|
|
pub struct CertStore<'a> {
|
|
pub struct CertStore<'a> {
|
|
- db: &'a crate::DB,
|
|
|
|
|
|
+ db: &'a microrm::DB,
|
|
|
|
+ qi: microrm::QueryInterface<'a>,
|
|
keys: HashMap<String, Ed25519KeyPair>,
|
|
keys: HashMap<String, Ed25519KeyPair>,
|
|
}
|
|
}
|
|
|
|
|
|
impl<'a> CertStore<'a> {
|
|
impl<'a> CertStore<'a> {
|
|
- pub fn new(db: &'a crate::DB) -> Self {
|
|
|
|
- Self { db, keys: HashMap::new() }
|
|
|
|
|
|
+ pub fn new(db: &'a microrm::DB) -> Self {
|
|
|
|
+ Self {
|
|
|
|
+ db,
|
|
|
|
+ qi: db.query_interface(),
|
|
|
|
+ keys: HashMap::new(),
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
fn realm_id(&self, realm_name: &str) -> Option<schema::RealmID> {
|
|
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())
|
|
|
|
|
|
+ self.qi
|
|
|
|
+ .get_one_by(schema::RealmColumns::Shortname, realm_name)
|
|
|
|
+ .map(|x| x.id())
|
|
}
|
|
}
|
|
|
|
|
|
pub fn generate_in(&self, realm_name: &str) -> Result<String, &'static str> {
|
|
pub fn generate_in(&self, realm_name: &str) -> Result<String, &'static str> {
|
|
@@ -24,7 +31,7 @@ impl<'a> CertStore<'a> {
|
|
let realm_id = self.realm_id(realm_name).ok_or("Failed to find realm")?;
|
|
let realm_id = self.realm_id(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");
|
|
}
|
|
}
|
|
let sign_generated = sign_generated.unwrap();
|
|
let sign_generated = sign_generated.unwrap();
|
|
|
|
|
|
@@ -35,30 +42,40 @@ 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");
|
|
|
|
|
|
+ self.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) {
|
|
|
|
|
|
+pub fn inspect(db: µrm::DB, realm_name: &str) {
|
|
|
|
+ let qi = db.query_interface();
|
|
let cs = CertStore::new(db);
|
|
let cs = CertStore::new(db);
|
|
println!("Certstore loaded.");
|
|
println!("Certstore loaded.");
|
|
- let realm = microrm::query::get_one_by(db, schema::RealmColumns::Shortname, realm_name);
|
|
|
|
|
|
+ let realm = qi.get_one_by(schema::RealmColumns::Shortname, realm_name);
|
|
if realm.is_none() {
|
|
if realm.is_none() {
|
|
println!("No such realm {}", realm_name);
|
|
println!("No such realm {}", realm_name);
|
|
- return
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
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_all_by(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) {
|
|
|
|
|
|
+pub fn generate(db: µrm::DB, realm_name: &str) {
|
|
|
|
+ let qi = db.query_interface();
|
|
let cs = CertStore::new(db);
|
|
let cs = CertStore::new(db);
|
|
if let Err(e) = cs.generate_in(realm_name) {
|
|
if let Err(e) = cs.generate_in(realm_name) {
|
|
println!("Failed to generate key: {}", e);
|
|
println!("Failed to generate key: {}", e);
|