t_rust_less_lib/block_store/mod.rs
1pub use self::model::*;
2use data_encoding::HEXLOWER;
3use sha2::{Digest, Sha256};
4use std::sync::Arc;
5use url::Url;
6
7#[cfg(feature = "dropbox")]
8pub mod dropbox;
9mod error;
10mod local_dir;
11mod local_wal;
12mod memory;
13mod model;
14#[cfg(feature = "sled")]
15mod sled;
16pub mod sync;
17
18#[cfg(test)]
19mod tests;
20
21pub use self::error::{StoreError, StoreResult};
22use crate::memguard::weak::ZeroingWords;
23
24type RingId = (String, u64);
25type RingContent = (u64, ZeroingWords);
26
27/// Common interface of all block stores
28///
29/// In terms of persistence t-rust-less thinks in collections of blocks. Whereas a
30/// block is just a chunk of bytes (i.e. Vec<u8>) stored on some sort of medium that
31/// may or may not be available to the public (!!!).
32///
33/// To put it even more bluntly: All data inside a block has to be protected in a way
34/// that the underlying medium might as well be twitter or facebook (not that I actually
35/// suggest or plan to write an implementation of that sort). The blcok store in itself
36/// is not responsible providing this sort of protection ... is just stores and organizes
37/// blocks.
38///
39/// As a rule a block store is supposed to be distributed among multiple clients, each able
40/// to asynchronously create additions to it.
41///
42/// All implementation are supposed to be thread-safe. Any kind of internal state has to be
43/// protected accordingly.
44///
45pub trait BlockStore: std::fmt::Debug + Send + Sync {
46 /// Get the current node id.
47 ///
48 /// Each accessor to a distributed store should have a unique id.
49 ///
50 fn node_id(&self) -> &str;
51
52 /// Get list of ring block identifiers.
53 ///
54 /// A store may contain any number of secrets rings. Usually associated with identities/users
55 /// that may access the store.
56 fn list_ring_ids(&self) -> StoreResult<Vec<RingId>>;
57
58 /// Get/read the ring block by its id.
59 ///
60 /// Every identities/user should have a ring block containing all the relevant key material to
61 /// encrypt/descript all the other blocks.
62 ///
63 /// Theses block should be protected by some sort of passphrase/key-derivation
64 ///
65 fn get_ring(&self, ring_id: &str) -> StoreResult<RingContent>;
66
67 /// Set/write a ring block.
68 ///
69 /// Implementors should ensure a sort of backup in case this operation fails, since
70 /// loosing the (private) ring will render the entire store useless to a user
71 ///
72 fn store_ring(&self, ring_id: &str, version: u64, raw: &[u8]) -> StoreResult<()>;
73
74 /// Get all the change logs of the store.
75 ///
76 /// The store has to keep track of all commits (see below).
77 ///
78 fn change_logs(&self) -> StoreResult<Vec<ChangeLog>>;
79
80 /// Get the index block of a specific client/user.
81 ///
82 /// An index block contains any sort of searchable index data referencing the
83 /// underlying data blocks. As the index might contain sensible data this block
84 /// has to be protected similar to a regular data block.
85 ///
86 /// Index blocks should not be shared among clients or user. I.e. every client/user
87 /// should have its own set index blocks.
88 ///
89 fn get_index(&self, index_id: &str) -> StoreResult<Option<ZeroingWords>>;
90
91 /// Store the index block of a specific client/user.
92 ///
93 fn store_index(&self, index_id: &str, raw: &[u8]) -> StoreResult<()>;
94
95 /// Add a new data block to the store.
96 ///
97 /// Data blocks contain the secret data shared between clients and should be
98 /// protected by the keys inside the ring block.
99 ///
100 /// The result of an add operation is a unique key of the data block.
101 ///
102 fn add_block(&self, raw: &[u8]) -> StoreResult<String>;
103 /// Get a block by its id.
104 ///
105 fn get_block(&self, block: &str) -> StoreResult<ZeroingWords>;
106
107 /// Commit a set of changes to the store.
108 ///
109 /// After adding one or more blocks to the store every client has to
110 /// commit its changes. This will create an entry in the `change_log` so that
111 /// other clients will notice the new data blocks.
112 ///
113 fn commit(&self, changes: &[Change]) -> StoreResult<()>;
114
115 /// Update changelog of other nodes.
116 ///
117 /// This is intended for store synchronization only.
118 fn update_change_log(&self, change_log: ChangeLog) -> StoreResult<()>;
119}
120
121pub fn open_block_store(url: &str, node_id: &str) -> StoreResult<Arc<dyn BlockStore>> {
122 let store_url = Url::parse(url)?;
123
124 match store_url.scheme() {
125 "file" => Ok(Arc::new(local_dir::LocalDirBlockStore::new(
126 store_url.to_file_path().unwrap(),
127 node_id,
128 )?)),
129 "wal" => Ok(Arc::new(local_wal::LocalWalBlockStore::new(
130 store_url.to_file_path().unwrap(),
131 node_id,
132 )?)),
133 "memory" => Ok(Arc::new(memory::MemoryBlockStore::new(node_id))),
134 #[cfg(feature = "sled")]
135 "sled" => Ok(Arc::new(sled::SledBlockStore::new(
136 store_url.to_file_path().unwrap(),
137 node_id,
138 )?)),
139 #[cfg(feature = "dropbox")]
140 "dropbox" => Ok(Arc::new(dropbox::DropboxBlockStore::new(
141 store_url.username(),
142 store_url.host_str().unwrap(),
143 node_id,
144 )?)),
145 _ => Err(StoreError::InvalidStoreUrl(url.to_string())),
146 }
147}
148
149pub fn generate_block_id(data: &[u8]) -> String {
150 let mut hasher = Sha256::new();
151
152 hasher.update(data);
153
154 HEXLOWER.encode(&hasher.finalize())
155}