transparent, PublicKeyHashable -> PK

This commit is contained in:
DieMyst 2021-02-15 12:44:41 +03:00
parent 953e0b6f0a
commit 5ff8d2f8d0
5 changed files with 25 additions and 34 deletions

View File

@ -21,7 +21,7 @@ use thiserror::Error as ThisError;
#[derive(ThisError, Debug)] #[derive(ThisError, Debug)]
pub enum SignatureError { pub enum SignatureError {
#[error("{0}")] #[error(transparent)]
Error(#[from] SigError), Error(#[from] SigError),
} }

View File

@ -67,7 +67,7 @@ pub enum TrustError {
Expired(Duration, Duration), Expired(Duration, Duration),
/// Errors occured on signature verification /// Errors occured on signature verification
#[error("{0}")] #[error(transparent)]
SignatureError(#[from] ed25519_dalek::SignatureError), SignatureError(#[from] ed25519_dalek::SignatureError),
/// Errors occured on trust decoding from differrent formats /// Errors occured on trust decoding from differrent formats
@ -83,7 +83,7 @@ pub enum TrustError {
#[error("Cannot decode a signature from bytes: {0}")] #[error("Cannot decode a signature from bytes: {0}")]
SignatureFromBytesError(#[from] SigError), SignatureFromBytesError(#[from] SigError),
#[error("{0}")] #[error(transparent)]
PublicKeyError(#[from] PKError), PublicKeyError(#[from] PKError),
#[error( #[error(

View File

@ -16,7 +16,7 @@
use crate::certificate::CertificateError::{CertificateLengthError, Unexpected}; use crate::certificate::CertificateError::{CertificateLengthError, Unexpected};
use crate::certificate::{Certificate, CertificateError}; use crate::certificate::{Certificate, CertificateError};
use crate::public_key_hashable::PublicKeyHashable; use crate::public_key_hashable::PublicKeyHashable as PK;
use crate::revoke::Revoke; use crate::revoke::Revoke;
use crate::revoke::RevokeError; use crate::revoke::RevokeError;
use crate::trust::Trust; use crate::trust::Trust;
@ -96,11 +96,7 @@ where
} }
/// Insert new root weight /// Insert new root weight
pub fn add_root_weight( pub fn add_root_weight(&mut self, pk: PK, weight: Weight) -> Result<(), TrustGraphError> {
&mut self,
pk: PublicKeyHashable,
weight: Weight,
) -> Result<(), TrustGraphError> {
Ok(self.storage.add_root_weight(pk, weight)?) Ok(self.storage.add_root_weight(pk, weight)?)
} }
@ -127,7 +123,7 @@ where
let mut chain = cert.borrow().chain.iter(); let mut chain = cert.borrow().chain.iter();
let root_trust = chain.next().ok_or(EmptyChain)?; let root_trust = chain.next().ok_or(EmptyChain)?;
let root_pk: PublicKeyHashable = root_trust.issued_for.clone().into(); let root_pk: PK = root_trust.issued_for.clone().into();
// Insert new TrustNode for this root_pk if there wasn't one // Insert new TrustNode for this root_pk if there wasn't one
if self.storage.get(&root_pk)?.is_none() { if self.storage.get(&root_pk)?.is_none() {
@ -225,7 +221,7 @@ where
fn bf_search_paths( fn bf_search_paths(
&self, &self,
node: &TrustNode, node: &TrustNode,
roots: HashSet<&PublicKeyHashable>, roots: HashSet<&PK>,
) -> Result<Vec<Vec<Auth>>, TrustGraphError> { ) -> Result<Vec<Vec<Auth>>, TrustGraphError> {
// queue to collect all chains in the trust graph (each chain is a path in the trust graph) // queue to collect all chains in the trust graph (each chain is a path in the trust graph)
let mut chains_queue: VecDeque<Vec<Auth>> = VecDeque::new(); let mut chains_queue: VecDeque<Vec<Auth>> = VecDeque::new();
@ -273,7 +269,7 @@ where
// - that trust must converge to one of the root weights // - that trust must converge to one of the root weights
// - there should be more than 1 trust in the chain // - there should be more than 1 trust in the chain
let self_signed = last.issued_by == last.trust.issued_for; let self_signed = last.issued_by == last.trust.issued_for;
let issued_by: &PublicKeyHashable = last.issued_by.as_ref(); let issued_by: &PK = last.issued_by.as_ref();
let converges_to_root = roots.contains(issued_by); let converges_to_root = roots.contains(issued_by);
if self_signed && converges_to_root && cur_chain.len() > 1 { if self_signed && converges_to_root && cur_chain.len() > 1 {
@ -330,7 +326,7 @@ where
pub fn revoke(&mut self, revoke: Revoke) -> Result<(), TrustGraphError> { pub fn revoke(&mut self, revoke: Revoke) -> Result<(), TrustGraphError> {
Revoke::verify(&revoke)?; Revoke::verify(&revoke)?;
let pk: PublicKeyHashable = revoke.pk.clone().into(); let pk: PK = revoke.pk.clone().into();
Ok(self.storage.revoke(&pk, revoke)?) Ok(self.storage.revoke(&pk, revoke)?)
} }

View File

@ -10,13 +10,13 @@ use trust_graph::{CertificateError, TrustGraphError};
#[derive(ThisError, Debug)] #[derive(ThisError, Debug)]
pub enum ServiceError { pub enum ServiceError {
#[error("{0}")] #[error(transparent)]
PublicKeyDecodeError(#[from] PKError), PublicKeyDecodeError(#[from] PKError),
#[error("{0}")] #[error(transparent)]
TGError(#[from] TrustGraphError), TGError(#[from] TrustGraphError),
#[error("{0}")] #[error(transparent)]
CertError(#[from] CertificateError), CertError(#[from] CertificateError),
#[error("{0}")] #[error(transparent)]
DtoError(#[from] DtoConversionError), DtoError(#[from] DtoConversionError),
} }

View File

@ -21,7 +21,7 @@ use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use trust_graph::{ use trust_graph::{
Auth, PublicKeyHashable, Revoke, Storage, StorageError, TrustGraph, TrustNode, Weight, Auth, PublicKeyHashable as PK, Revoke, Storage, StorageError, TrustGraph, TrustNode, Weight,
}; };
static INSTANCE: OnceCell<Mutex<TrustGraph<SQLiteStorage>>> = OnceCell::new(); static INSTANCE: OnceCell<Mutex<TrustGraph<SQLiteStorage>>> = OnceCell::new();
@ -58,13 +58,13 @@ impl SQLiteStorage {
#[derive(ThisError, Debug)] #[derive(ThisError, Debug)]
pub enum SQLiteStorageError { pub enum SQLiteStorageError {
#[error("{0}")] #[error(transparent)]
SQLiteError(InternalSqliteError), SQLiteError(InternalSqliteError),
#[error("{0}")] #[error("{0}")]
PublicKeyFromStr(String), PublicKeyFromStr(String),
#[error("{0}")] #[error(transparent)]
EncodeError(RmpEncodeError), EncodeError(RmpEncodeError),
#[error("{0}")] #[error(transparent)]
DecodeError(RmpDecodeError), DecodeError(RmpDecodeError),
#[error("Cannot convert weight as integer from DB")] #[error("Cannot convert weight as integer from DB")]
WeightConversionDB, WeightConversionDB,
@ -105,7 +105,7 @@ impl StorageError for SQLiteStorageError {}
impl Storage for SQLiteStorage { impl Storage for SQLiteStorage {
type Error = SQLiteStorageError; type Error = SQLiteStorageError;
fn get(&self, pk: &PublicKeyHashable) -> Result<Option<TrustNode>, Self::Error> { fn get(&self, pk: &PK) -> Result<Option<TrustNode>, Self::Error> {
let mut cursor = self let mut cursor = self
.connection .connection
.prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")? .prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")?
@ -131,7 +131,7 @@ impl Storage for SQLiteStorage {
} }
} }
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) -> Result<(), Self::Error> { fn insert(&mut self, pk: PK, node: TrustNode) -> Result<(), Self::Error> {
let mut cursor = self let mut cursor = self
.connection .connection
.prepare("INSERT OR REPLACE INTO trustnodes VALUES (?, ?)")? .prepare("INSERT OR REPLACE INTO trustnodes VALUES (?, ?)")?
@ -147,7 +147,7 @@ impl Storage for SQLiteStorage {
Ok({}) Ok({})
} }
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Result<Option<Weight>, Self::Error> { fn get_root_weight(&self, pk: &PK) -> Result<Option<Weight>, Self::Error> {
let mut cursor = self let mut cursor = self
.connection .connection
.prepare("SELECT public_key,weight FROM roots WHERE public_key = ?")? .prepare("SELECT public_key,weight FROM roots WHERE public_key = ?")?
@ -167,11 +167,7 @@ impl Storage for SQLiteStorage {
} }
} }
fn add_root_weight( fn add_root_weight(&mut self, pk: PK, weight: Weight) -> Result<(), Self::Error> {
&mut self,
pk: PublicKeyHashable,
weight: Weight,
) -> Result<(), Self::Error> {
log::info!("add root: {} weight: {}", pk, weight); log::info!("add root: {} weight: {}", pk, weight);
let mut cursor = self let mut cursor = self
.connection .connection
@ -187,7 +183,7 @@ impl Storage for SQLiteStorage {
Ok({}) Ok({})
} }
fn root_keys(&self) -> Result<Vec<PublicKeyHashable>, Self::Error> { fn root_keys(&self) -> Result<Vec<PK>, Self::Error> {
let mut cursor = self let mut cursor = self
.connection .connection
.prepare("SELECT public_key,weight FROM roots")? .prepare("SELECT public_key,weight FROM roots")?
@ -198,8 +194,7 @@ impl Storage for SQLiteStorage {
while let Some(row) = cursor.next()? { while let Some(row) = cursor.next()? {
log::info!("row: {:?}", row); log::info!("row: {:?}", row);
let pk = row[0].as_string().ok_or(PublicKeyConversion)?; let pk = row[0].as_string().ok_or(PublicKeyConversion)?;
let pk: PublicKeyHashable = let pk: PK = PK::from_str(pk).map_err(|e| PublicKeyFromStr(e.to_string()))?;
PublicKeyHashable::from_str(pk).map_err(|e| PublicKeyFromStr(e.to_string()))?;
roots.push(pk) roots.push(pk)
} }
@ -207,7 +202,7 @@ impl Storage for SQLiteStorage {
Ok(roots) Ok(roots)
} }
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), Self::Error> { fn revoke(&mut self, pk: &PK, revoke: Revoke) -> Result<(), Self::Error> {
match self.get(&pk)? { match self.get(&pk)? {
Some(mut trust_node) => { Some(mut trust_node) => {
trust_node.update_revoke(revoke); trust_node.update_revoke(revoke);
@ -220,7 +215,7 @@ impl Storage for SQLiteStorage {
fn update_auth( fn update_auth(
&mut self, &mut self,
pk: &PublicKeyHashable, pk: &PK,
auth: Auth, auth: Auth,
issued_for: &PublicKey, issued_for: &PublicKey,
cur_time: Duration, cur_time: Duration,