Skip to content

Commit

Permalink
v2, domain model redesign, redefine:
Browse files Browse the repository at this point in the history
 - DeviceInfo -> DeviceId/DeviceData
 - UserSignature -> UserData/UserMemebership
 - BaseEncodedText -> Base64Text
  • Loading branch information
cypherkitty committed Oct 24, 2023
1 parent 020b999 commit f3b4a0e
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 383 deletions.
58 changes: 16 additions & 42 deletions core/src/crypto/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use rand::{distributions::Alphanumeric, Rng};
use sha2::{Digest, Sha256};
use uuid::Uuid;

use crate::crypto::encoding::base64::Base64Text;
use crate::node::db::events::object_descriptor::{ObjectDescriptorFqdn, ObjectDescriptorId};

const SEED_LENGTH: usize = 64;

Expand Down Expand Up @@ -36,52 +38,24 @@ pub fn generate_uuid_b64_url_enc(value: String) -> String {
Base64Text::from(uuid.as_bytes().as_slice()).base64_text
}

pub struct IdStrGenerator {

pub trait NextId {
fn next_id(&self) -> ObjectDescriptorId;
}

impl IdStrGenerator {
/// Convert a string to a base64 url encoded uuid
pub fn next_id_str(curr_id_or_name: &str) -> String {
//let hash = Sha256::digest(str.as_bytes());
//let uuid = Uuid::from_slice(&hash.as_slice()[..16]).unwrap();
//Base64Text::from(uuid.as_bytes().as_slice())
//Base64Text::from(uuid.as_bytes().as_slice())
let next = if curr_id_or_name.contains("::") {
let parts: Vec<&str> = curr_id_or_name.split("::").collect();
let next_counter: usize = parts[1].parse().unwrap();
format!("{}::{:?}", parts[0], next_counter + 1)
} else {
format!("{}::{}", curr_id_or_name, 0)
};

//Base64Text::from(next)
next
impl NextId for ObjectDescriptorFqdn {
fn next_id(&self) -> ObjectDescriptorId {
ObjectDescriptorId {
fqdn: self.clone(),
counter: 0,
}
}
}

#[cfg(test)]
mod test {

#[test]
fn to_id_test() {
//let id = to_id("yay");
//let expected_uuid = uuid!("f6078ebe-0c2f-08c2-25c0-349aef2fe062").as_ref().as_bytes();
//let expected_uuid = String::from_utf8(expected_uuid.to_vec()).unwrap();
//let expected_uuid = Base64Text::from(expected_uuid.as_ref());
//assert_eq!(expected_uuid, id)

let id_0 = next_id("qwe:qwe");
println!("{}", id_0);
let id_0 = next_id(id_0.as_str());
println!("{}", id_0);
let id_0 = next_id(id_0.as_str());
println!("{}", id_0);
let id_0 = next_id(id_0.as_str());
println!("{}", id_0);
let id_0 = next_id(id_0.as_str());
println!("{}", id_0);
let id_0 = next_id(id_0.as_str());
println!("{}", id_0);
impl NextId for ObjectDescriptorId {
fn next_id(self) -> ObjectDescriptorId {
ObjectDescriptorId {
counter: self.counter + 1,
..self
}
}
}
32 changes: 21 additions & 11 deletions core/src/node/app/sync_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use std::time::Duration;

use tracing::{debug, error, info, instrument, Instrument};

use crate::node::app::device_creds_manager::DeviceCredentialsManager;
use crate::node::common::model::device::DeviceCredentials;
use crate::node::db::events::common::{LogEventKeyBasedRecord, ObjectCreator, SharedSecretObject};
use crate::node::db::events::common::SharedSecretObject;
use crate::node::db::events::db_tail::{DbTail, ObjectIdDbEvent};
use crate::node::db::events::generic_log_event::GenericKvLogEvent;
use crate::node::db::events::global_index::GlobalIndexObject;
use crate::node::db::events::kv_log_event::{KvKey, KvLogEvent};
use crate::node::db::events::local::DbTailObject;
use crate::node::db::events::object_descriptor::ObjectDescriptor;
use crate::node::db::events::object_id::{IdGen, ObjectId};
use crate::node::db::events::object_descriptor::{ObjectDescriptor};
use crate::node::db::events::object_descriptor::global_index::GlobalIndexDescriptor;
use crate::node::db::events::object_id::{Next, ObjectId, UnitId};
use crate::node::db::generic_db::KvLogEventRepo;
use crate::node::db::read_db::read_db_service::ReadDbServiceProxy;
use crate::node::db::read_db::store::vault_store::VaultStore;
Expand Down Expand Up @@ -77,12 +77,12 @@ impl<Repo: KvLogEventRepo> SyncGateway<Repo> {

let sync_request = {
let vault_id_request = match &new_db_tail.vault_id {
ObjectIdDbEvent::Empty { unit_id } => unit_id.clone(),
ObjectIdDbEvent::Empty { obj_desc } => ObjectId::unit(obj_desc),
ObjectIdDbEvent::Id { tail_id } => tail_id.next(),
};

let meta_pass_id_request = match &new_db_tail.meta_pass_id {
ObjectIdDbEvent::Empty { unit_id } => unit_id.clone(),
ObjectIdDbEvent::Empty { obj_desc } => ObjectId::unit(obj_desc),
ObjectIdDbEvent::Id { tail_id } => tail_id.next(),
};

Expand Down Expand Up @@ -119,11 +119,21 @@ impl<Repo: KvLogEventRepo> SyncGateway<Repo> {

match new_event {
GenericKvLogEvent::GlobalIndex(gi_obj) => {

if let GlobalIndexObject::Update { event } = gi_obj {
let gi_record = event.value;
let gi_obj_id = ObjectId::Unit { id: gi_record.vault_id };
let vault_unit_id = event.value;
let idx_desc = ObjectDescriptor::GlobalIndex(GlobalIndexDescriptor::VaultIndex {
vault_id: vault_unit_id.clone()
});

let vault_idx_evt = GenericKvLogEvent::GlobalIndex(GlobalIndexObject::VaultIndex {
event: KvLogEvent::new_global_index_event(&gi_obj_id, gi_obj_id., GlobalIndexDescriptor::VaultIndex),
event: KvLogEvent {
key: KvKey {
obj_id: UnitId::unit(&idx_desc),
obj_desc: idx_desc,
},
value: vault_unit_id,
}
});
self.persistent_object.repo.save_event(vault_idx_evt)

Expand All @@ -138,7 +148,7 @@ impl<Repo: KvLogEventRepo> SyncGateway<Repo> {
latest_meta_pass_id = ObjectIdDbEvent::Id { tail_id: key.clone() }
},
GenericKvLogEvent::SharedSecret(SharedSecretObject::Audit { event }) => {
latest_audit_tail = Some(event.value)
latest_audit_tail = Some(ObjectId::from(event.value))
}
_ => {
//ignore any non global event
Expand Down Expand Up @@ -249,7 +259,7 @@ impl<Repo: KvLogEventRepo> SyncGateway<Repo> {

async fn get_new_tail_for_an_obj(&self, db_tail_obj: &ObjectIdDbEvent) -> ObjectIdDbEvent {
match db_tail_obj {
ObjectIdDbEvent::Empty { unit_id } => self
ObjectIdDbEvent::Empty { obj_desc } => self
.persistent_object
.find_tail_id(unit_id.clone())
.await
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/db/actions/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod test {
panic!("Invalid event");
};

assert_eq!(recovery_request_desc.to_id(), event.value.id_str());
assert_eq!(recovery_request_desc.to_fqdn(), event.value.id_str());
}

{
Expand Down
62 changes: 27 additions & 35 deletions core/src/node/db/events/common.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
use crate::crypto::encoding::base64::Base64Text;
use crate::models::password_recovery_request::PasswordRecoveryRequest;
use crate::models::{Base64EncodedText, MetaPasswordDoc, SecretDistributionDocData, UserSignature, VaultDoc};
use crate::node::db::events::kv_log_event::{KvKey, KvLogEvent};
use crate::node::db::events::object_descriptor::ObjectDescriptor;
use crate::node::db::events::object_id::ObjectId;
use crate::models::{MetaPasswordDoc, SecretDistributionDocData};
use crate::node::common::model::user::UserDataCandidate;
use crate::node::db::events::generic_log_event::ObjIdExtractor;
use crate::node::db::events::kv_log_event::KvLogEvent;
use crate::node::db::events::object_id::{ArtifactId, GenesisId, ObjectId, UnitId};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MemPoolObject {
JoinRequest { event: KvLogEvent<UserSignature> },
JoinRequest { event: KvLogEvent<ArtifactId, UserDataCandidate> },
}

impl MemPoolObject {
pub fn key(&self) -> &KvKey {
impl ObjIdExtractor for MemPoolObject {
fn obj_id(&self) -> ObjectId {
match self {
MemPoolObject::JoinRequest { event } => &event.key,
MemPoolObject::JoinRequest { event } => ObjectId::from(event.key.obj_id.clone())
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MetaPassObject {
Unit { event: KvLogEvent<()> },
Genesis { event: KvLogEvent<PublicKeyRecord> },
Update { event: KvLogEvent<MetaPasswordDoc> },
Unit { event: KvLogEvent<UnitId, ()> },
Genesis { event: KvLogEvent<GenesisId, PublicKeyRecord> },
Update { event: KvLogEvent<ArtifactId, MetaPasswordDoc> },
}

impl MetaPassObject {
pub fn key(&self) -> &KvKey {
impl ObjIdExtractor for MetaPassObject {
fn obj_id(&self) -> ObjectId {
match self {
MetaPassObject::Unit { event } => &event.key,
MetaPassObject::Genesis { event } => &event.key,
MetaPassObject::Update { event } => &event.key,
MetaPassObject::Unit { event } => ObjectId::from(event.key.obj_id.clone()),
MetaPassObject::Genesis { event } => ObjectId::from(event.key.obj_id.clone()),
MetaPassObject::Update { event } => ObjectId::from(event.key.obj_id.clone())
}
}
}
Expand All @@ -41,34 +42,30 @@ impl MetaPassObject {
#[serde(rename_all = "camelCase")]
pub enum SharedSecretObject {
Split {
event: KvLogEvent<SecretDistributionDocData>,
event: KvLogEvent<UnitId, SecretDistributionDocData>,
},
Recover {
event: KvLogEvent<SecretDistributionDocData>,
event: KvLogEvent<UnitId, SecretDistributionDocData>,
},
RecoveryRequest {
event: KvLogEvent<PasswordRecoveryRequest>,
event: KvLogEvent<UnitId, PasswordRecoveryRequest>,
},
Audit {
event: KvLogEvent<ObjectId>,
event: KvLogEvent<ArtifactId, ArtifactId>,
},
}

impl SharedSecretObject {
pub fn key(&self) -> &KvKey {
impl ObjIdExtractor for SharedSecretObject {
fn obj_id(&self) -> ObjectId {
match self {
SharedSecretObject::Split { event } => &event.key,
SharedSecretObject::Recover { event } => &event.key,
SharedSecretObject::RecoveryRequest { event } => &event.key,
SharedSecretObject::Audit { event } => &event.key,
SharedSecretObject::Split { event } => ObjectId::from(event.key.obj_id.clone()),
SharedSecretObject::Recover { event } => ObjectId::from(event.key.obj_id.clone()),
SharedSecretObject::RecoveryRequest { event } => ObjectId::from(event.key.obj_id.clone()),
SharedSecretObject::Audit { event } => ObjectId::from(event.key.obj_id.clone())
}
}
}

pub trait LogEventKeyBasedRecord {
fn key(&self) -> &KvKey;
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyRecord {
Expand All @@ -81,8 +78,3 @@ impl From<Base64Text> for PublicKeyRecord {
}
}

pub trait ObjectCreator<T> {
fn unit(value: T) -> Self;
fn genesis(obj_desc: &ObjectDescriptor) -> Self;
}

5 changes: 3 additions & 2 deletions core/src/node/db/events/db_tail.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::node::db::events::object_id::ObjectId;
use crate::node::db::events::object_descriptor::ObjectDescriptor;
use crate::node::db::events::object_id::{ArtifactId, ObjectId, UnitId};

#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -15,6 +16,6 @@ pub struct DbTail {
#[serde(rename_all = "camelCase")]
#[serde(tag = "__db_tail_obj")]
pub enum ObjectIdDbEvent {
Empty { unit_id: ObjectId },
Empty { obj_desc: ObjectDescriptor },
Id { tail_id: ObjectId },
}
35 changes: 22 additions & 13 deletions core/src/node/db/events/generic_log_event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::node::db::events::common::{LogEventKeyBasedRecord, MemPoolObject, MetaPassObject, SharedSecretObject};
use crate::node::db::events::common::{MemPoolObject, MetaPassObject, SharedSecretObject};
use crate::node::db::events::error::ErrorMessage;
use crate::node::db::events::global_index::GlobalIndexObject;
use crate::node::db::events::kv_log_event::{KvKey, KvLogEvent};
use crate::node::db::events::kv_log_event::{GenericKvKey, KvLogEvent};
use crate::node::db::events::local::{DbTailObject, DeviceCredentialsObject};
use crate::node::db::events::object_id::{ArtifactId, ObjectId};
use crate::node::db::events::vault_event::VaultObject;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand All @@ -19,20 +20,28 @@ pub enum GenericKvLogEvent {
DeviceCredentials(DeviceCredentialsObject),
DbTail(DbTailObject),

Error { event: KvLogEvent<ErrorMessage> },
Error { event: KvLogEvent<ArtifactId, ErrorMessage> },
}

impl LogEventKeyBasedRecord for GenericKvLogEvent {
fn key(&self) -> &KvKey {
pub trait ObjIdExtractor {
fn obj_id(&self) -> ObjectId;
}

pub trait KeyExtractor {
fn key(&self) -> GenericKvKey;
}

impl ObjIdExtractor for GenericKvLogEvent {
fn obj_id(&self) -> ObjectId {
match self {
GenericKvLogEvent::GlobalIndex(gi_obj) => gi_obj.key(),
GenericKvLogEvent::Vault(vault_obj) => vault_obj.key(),
GenericKvLogEvent::MetaPass(pass_obj) => pass_obj.key(),
GenericKvLogEvent::SharedSecret(obj) => obj.key(),
GenericKvLogEvent::MemPool(mem_pool_obj) => mem_pool_obj.key(),
GenericKvLogEvent::Error { event } => &event.key,
GenericKvLogEvent::DeviceCredentials(obj) => &obj.event.key,
GenericKvLogEvent::DbTail(obj) => &obj.event.key
GenericKvLogEvent::GlobalIndex(obj) => obj.obj_id(),
GenericKvLogEvent::Vault(obj) => obj.obj_id(),
GenericKvLogEvent::MetaPass(obj) => obj.obj_id(),
GenericKvLogEvent::SharedSecret(obj) => obj.obj_id(),
GenericKvLogEvent::MemPool(obj) => obj.obj_id(),
GenericKvLogEvent::DeviceCredentials(obj) => obj.obj_id(),
GenericKvLogEvent::DbTail(obj) => obj.obj_id(),
GenericKvLogEvent::Error { event } => event.key.obj_id.clone(),
}
}
}
Expand Down
Loading

0 comments on commit f3b4a0e

Please sign in to comment.