2021-01-18 03:57:10 +03:00
|
|
|
// store list of trusts
|
|
|
|
// check if trust is already in list before adding
|
2021-01-20 00:17:24 +03:00
|
|
|
// if there is an older trust - don't add received trust
|
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
use crate::storage_impl::SqliteStorageError::{
|
|
|
|
ConvertionError, DecodeError, EncodeError, RevokeError, SqliteError, Unexpected,
|
|
|
|
};
|
2021-01-21 20:35:00 +03:00
|
|
|
use core::convert::TryFrom;
|
2021-01-25 15:33:40 +03:00
|
|
|
use fce_sqlite_connector;
|
|
|
|
use fce_sqlite_connector::Connection;
|
2021-02-09 14:31:41 +03:00
|
|
|
use fce_sqlite_connector::Error as InternalSqliteError;
|
2021-02-09 15:50:21 +03:00
|
|
|
use fce_sqlite_connector::Value;
|
|
|
|
use fluence_identity::public_key::PublicKey;
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use rmp_serde::decode::Error as RmpDecodeError;
|
|
|
|
use rmp_serde::encode::Error as RmpEncodeError;
|
|
|
|
use std::convert::From;
|
2021-01-21 20:26:17 +03:00
|
|
|
use std::str::FromStr;
|
2021-01-20 00:17:24 +03:00
|
|
|
use std::time::Duration;
|
2021-02-09 14:31:41 +03:00
|
|
|
use thiserror::Error as ThisError;
|
2021-02-09 15:50:21 +03:00
|
|
|
use trust_graph::{
|
|
|
|
Auth, PublicKeyHashable, Revoke, Storage, StorageError, TrustGraph, TrustNode, Weight,
|
|
|
|
};
|
2021-01-20 00:17:24 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
static INSTANCE: OnceCell<Mutex<TrustGraph<SqliteStorage>>> = OnceCell::new();
|
2021-01-20 00:17:24 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
pub fn get_data() -> &'static Mutex<TrustGraph<SqliteStorage>> {
|
2021-01-20 00:17:24 +03:00
|
|
|
INSTANCE.get_or_init(|| {
|
2021-01-27 12:57:19 +03:00
|
|
|
let db_path = "/tmp/users123123.sqlite";
|
2021-01-25 15:33:40 +03:00
|
|
|
let connection = fce_sqlite_connector::open(db_path).unwrap();
|
2021-01-20 00:17:24 +03:00
|
|
|
|
2021-01-21 20:26:17 +03:00
|
|
|
let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(
|
|
|
|
public_key TEXT PRIMARY KEY,
|
|
|
|
trustnode BLOB NOT NULL
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS roots(
|
|
|
|
public_key TEXT,
|
|
|
|
weight INTEGER
|
2021-01-20 00:17:24 +03:00
|
|
|
);";
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-01-20 17:21:02 +03:00
|
|
|
connection.execute(init_sql).expect("cannot connect to db");
|
|
|
|
|
2021-01-25 15:32:43 +03:00
|
|
|
Mutex::new(TrustGraph::new(Box::new(SqliteStorage::new(connection))))
|
2021-01-20 17:21:02 +03:00
|
|
|
})
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:32:43 +03:00
|
|
|
pub struct SqliteStorage {
|
2021-01-20 17:21:02 +03:00
|
|
|
connection: Connection,
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:32:43 +03:00
|
|
|
impl SqliteStorage {
|
|
|
|
pub fn new(connection: Connection) -> SqliteStorage {
|
|
|
|
SqliteStorage { connection }
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 17:21:02 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
#[derive(ThisError, Debug)]
|
|
|
|
pub enum SqliteStorageError {
|
|
|
|
#[error("Unexpected: {0}")]
|
|
|
|
Unexpected(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
SqliteError(InternalSqliteError),
|
|
|
|
#[error("{0}")]
|
|
|
|
EncodeError(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
DecodeError(String),
|
|
|
|
#[error("There is no entry for {0}")]
|
|
|
|
ConvertionError(String),
|
|
|
|
#[error("Cannot revoke a trust: {0}")]
|
2021-02-09 15:50:21 +03:00
|
|
|
RevokeError(String),
|
2021-02-09 14:31:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<InternalSqliteError> for SqliteStorageError {
|
|
|
|
fn from(err: InternalSqliteError) -> Self {
|
|
|
|
SqliteError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RmpEncodeError> for SqliteStorageError {
|
|
|
|
fn from(err: RmpEncodeError) -> Self {
|
|
|
|
EncodeError(format!("{}", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RmpDecodeError> for SqliteStorageError {
|
|
|
|
fn from(err: RmpDecodeError) -> Self {
|
|
|
|
DecodeError(format!("{}", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SqliteStorageError> for String {
|
|
|
|
fn from(err: SqliteStorageError) -> Self {
|
|
|
|
err.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageError for SqliteStorageError {}
|
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
impl Storage for SqliteStorage {
|
2021-02-09 14:31:41 +03:00
|
|
|
type Error = SqliteStorageError;
|
|
|
|
|
|
|
|
fn get(&self, pk: &PublicKeyHashable) -> Result<Option<TrustNode>, Self::Error> {
|
2021-01-20 17:21:02 +03:00
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
2021-02-09 14:31:41 +03:00
|
|
|
.prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")?
|
2021-01-20 17:21:02 +03:00
|
|
|
.cursor();
|
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
cursor.bind(&[Value::String(format!("{}", pk))])?;
|
2021-01-20 17:21:02 +03:00
|
|
|
|
|
|
|
match cursor.next().unwrap() {
|
|
|
|
Some(r) => {
|
2021-01-27 12:57:19 +03:00
|
|
|
log::info!("row: {:?}", r);
|
2021-02-09 15:50:21 +03:00
|
|
|
let tn_bin: &[u8] = r[0].as_binary().ok_or(ConvertionError(
|
|
|
|
"cannot get trustnode as binary".to_string(),
|
|
|
|
))?;
|
2021-01-21 20:26:17 +03:00
|
|
|
|
2021-01-27 12:57:19 +03:00
|
|
|
log::info!("binary: {:?}", tn_bin);
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
let trust_node: TrustNode = rmp_serde::from_read_ref(tn_bin)?;
|
2021-01-21 20:26:17 +03:00
|
|
|
|
|
|
|
log::info!("trustnode: {:?}", trust_node);
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
Ok(Some(trust_node))
|
2021-01-20 17:21:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
None => Ok(None),
|
2021-01-20 17:21:02 +03:00
|
|
|
}
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) -> Result<(), Self::Error> {
|
2021-01-20 16:14:01 +03:00
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
2021-02-09 14:31:41 +03:00
|
|
|
.prepare("INSERT OR REPLACE INTO trustnodes VALUES (?, ?)")?
|
2021-01-20 16:14:01 +03:00
|
|
|
.cursor();
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
let tn_vec = rmp_serde::to_vec(&node)?;
|
2021-01-27 12:57:19 +03:00
|
|
|
|
2021-01-27 14:24:34 +03:00
|
|
|
log::info!("insert: {:?}", tn_vec);
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
cursor.bind(&[Value::String(format!("{}", pk)), Value::Binary(tn_vec)])?;
|
2021-01-20 00:17:24 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
cursor.next()?;
|
|
|
|
Ok({})
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Result<Option<Weight>, Self::Error> {
|
2021-01-21 20:35:00 +03:00
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
2021-02-09 14:31:41 +03:00
|
|
|
.prepare("SELECT public_key,weight FROM roots WHERE public_key = ?")?
|
2021-01-21 20:35:00 +03:00
|
|
|
.cursor();
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
cursor.bind(&[Value::String(format!("{}", pk))])?;
|
2021-01-21 20:35:00 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
if let Some(row) = cursor.next()? {
|
2021-01-21 20:35:00 +03:00
|
|
|
log::info!("row: {:?}", row);
|
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
let w = u32::try_from(
|
|
|
|
row[1]
|
|
|
|
.as_integer()
|
|
|
|
.ok_or(ConvertionError("cannot get weight as integer".to_string()))?,
|
|
|
|
)
|
|
|
|
.map_err(|e| Unexpected(format!("Unexpected. Cannot convert weight to u32: {}", e)))?;
|
2021-01-21 20:35:00 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
Ok(Some(w))
|
2021-01-21 20:35:00 +03:00
|
|
|
} else {
|
2021-02-09 14:31:41 +03:00
|
|
|
Ok(None)
|
2021-01-21 20:35:00 +03:00
|
|
|
}
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
fn add_root_weight(
|
|
|
|
&mut self,
|
|
|
|
pk: PublicKeyHashable,
|
|
|
|
weight: Weight,
|
|
|
|
) -> Result<(), Self::Error> {
|
2021-01-21 20:26:17 +03:00
|
|
|
log::info!("add root: {} weight: {}", pk, weight);
|
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
2021-02-09 14:31:41 +03:00
|
|
|
.prepare("INSERT OR REPLACE INTO roots VALUES (?, ?)")?
|
2021-01-21 20:26:17 +03:00
|
|
|
.cursor();
|
|
|
|
|
2021-02-09 15:50:21 +03:00
|
|
|
cursor.bind(&[
|
|
|
|
Value::String(format!("{}", pk)),
|
|
|
|
Value::Integer(i64::from(weight)),
|
|
|
|
])?;
|
2021-01-21 20:26:17 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
cursor.next()?;
|
|
|
|
Ok({})
|
2021-01-21 20:26:17 +03:00
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
fn root_keys(&self) -> Result<Vec<PublicKeyHashable>, Self::Error> {
|
2021-01-21 20:26:17 +03:00
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
2021-02-09 14:31:41 +03:00
|
|
|
.prepare("SELECT public_key,weight FROM roots")?
|
2021-01-21 20:26:17 +03:00
|
|
|
.cursor();
|
|
|
|
|
|
|
|
let mut roots = vec![];
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
while let Some(row) = cursor.next()? {
|
2021-01-21 20:26:17 +03:00
|
|
|
log::info!("row: {:?}", row);
|
2021-02-09 15:50:21 +03:00
|
|
|
let pk = row[0].as_string().ok_or(ConvertionError(
|
|
|
|
"cannot get public key as binary".to_string(),
|
|
|
|
))?;
|
2021-01-21 20:26:17 +03:00
|
|
|
let pk: PublicKeyHashable =
|
2021-02-09 14:31:41 +03:00
|
|
|
PublicKeyHashable::from_str(pk).map_err(|e| DecodeError(e.to_string()))?;
|
2021-01-21 20:26:17 +03:00
|
|
|
|
|
|
|
roots.push(pk)
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
Ok(roots)
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-02-09 14:31:41 +03:00
|
|
|
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), Self::Error> {
|
|
|
|
match self.get(&pk)? {
|
2021-01-25 15:32:43 +03:00
|
|
|
Some(mut trust_node) => {
|
|
|
|
trust_node.update_revoke(revoke);
|
2021-02-09 14:31:41 +03:00
|
|
|
self.insert(pk.clone(), trust_node)?;
|
2021-01-25 15:32:43 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-09 15:50:21 +03:00
|
|
|
None => Err(RevokeError(
|
|
|
|
"There is no trust with such PublicKey".to_string(),
|
|
|
|
)),
|
2021-01-25 15:32:43 +03:00
|
|
|
}
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
fn update_auth(
|
|
|
|
&mut self,
|
|
|
|
pk: &PublicKeyHashable,
|
|
|
|
auth: Auth,
|
|
|
|
issued_for: &PublicKey,
|
|
|
|
cur_time: Duration,
|
2021-02-09 14:31:41 +03:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match self.get(&pk)? {
|
2021-01-21 20:26:17 +03:00
|
|
|
Some(mut trust_node) => {
|
|
|
|
trust_node.update_auth(auth);
|
|
|
|
self.insert(pk.clone(), trust_node)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
|
|
|
|
trust_node.update_auth(auth);
|
2021-02-09 14:31:41 +03:00
|
|
|
self.insert(pk.clone(), trust_node)
|
2021-01-21 20:26:17 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
}
|