fix(sled): Put the StoreCipher behind the Arc, not the Option

This commit is contained in:
Damir Jelić
2022-04-26 18:06:39 +02:00
parent e1baa25713
commit 742c1944eb
2 changed files with 17 additions and 17 deletions

View File

@@ -171,7 +171,7 @@ struct TrackedUser {
#[derive(Clone)]
pub struct SledStore {
account_info: Arc<RwLock<Option<AccountInfo>>>,
store_cipher: Arc<Option<StoreCipher>>,
store_cipher: Option<Arc<StoreCipher>>,
path: Option<PathBuf>,
inner: Db,
@@ -222,11 +222,10 @@ impl SledStore {
.map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
let store_cipher = if let Some(passphrase) = passphrase {
Some(Self::get_or_create_store_cipher(passphrase, &db)?)
Some(Self::get_or_create_store_cipher(passphrase, &db)?.into())
} else {
None
}
.into();
};
SledStore::open_helper(db, Some(path), store_cipher)
}
@@ -235,7 +234,7 @@ impl SledStore {
/// The given passphrase will be used to encrypt private data.
pub fn open_with_database(
db: Db,
store_cipher: Arc<Option<StoreCipher>>,
store_cipher: Option<Arc<StoreCipher>>,
) -> Result<Self, OpenStoreError> {
SledStore::open_helper(db, None, store_cipher)
}
@@ -245,7 +244,7 @@ impl SledStore {
}
fn serialize_value(&self, event: &impl Serialize) -> Result<Vec<u8>, CryptoStoreError> {
if let Some(key) = &*self.store_cipher {
if let Some(key) = &self.store_cipher {
key.encrypt_value(event).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))
} else {
Ok(serde_json::to_vec(event)?)
@@ -256,7 +255,7 @@ impl SledStore {
&self,
event: &[u8],
) -> Result<T, CryptoStoreError> {
if let Some(key) = &*self.store_cipher {
if let Some(key) = &self.store_cipher {
key.decrypt_value(event).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))
} else {
Ok(serde_json::from_slice(event)?)
@@ -264,7 +263,7 @@ impl SledStore {
}
fn encode_key<T: EncodeKey>(&self, table_name: &str, key: T) -> Vec<u8> {
if let Some(store_cipher) = &*self.store_cipher {
if let Some(store_cipher) = &self.store_cipher {
key.encode_secure(table_name, store_cipher).to_vec()
} else {
key.encode()
@@ -357,7 +356,7 @@ impl SledStore {
fn open_helper(
db: Db,
path: Option<PathBuf>,
store_cipher: Arc<Option<StoreCipher>>,
store_cipher: Option<Arc<StoreCipher>>,
) -> Result<Self, OpenStoreError> {
let account = db.open_tree("account")?;
let private_identity = db.open_tree("private_identity")?;

View File

@@ -143,7 +143,7 @@ type Result<A, E = SledStoreError> = std::result::Result<A, E>;
pub struct SledStore {
path: Option<PathBuf>,
pub(crate) inner: Db,
store_cipher: Arc<Option<StoreCipher>>,
store_cipher: Option<Arc<StoreCipher>>,
session: Tree,
account_data: Tree,
members: Tree,
@@ -181,7 +181,7 @@ impl SledStore {
fn open_helper(
db: Db,
path: Option<PathBuf>,
store_cipher: Option<StoreCipher>,
store_cipher: Option<Arc<StoreCipher>>,
) -> Result<Self> {
let session = db.open_tree(SESSION)?;
let account_data = db.open_tree(ACCOUNT_DATA)?;
@@ -215,7 +215,7 @@ impl SledStore {
Ok(Self {
path,
inner: db,
store_cipher: store_cipher.into(),
store_cipher,
session,
account_data,
members,
@@ -256,7 +256,7 @@ impl SledStore {
SledStore::open_helper(
db,
None,
Some(StoreCipher::new().expect("can't create store cipher")),
Some(StoreCipher::new().expect("can't create store cipher").into()),
)
.map_err(|e| e.into())
}
@@ -275,7 +275,8 @@ impl SledStore {
let cipher = StoreCipher::new()?;
db.insert("store_cipher".encode(), cipher.export(passphrase)?)?;
cipher
};
}
.into();
SledStore::open_helper(db, Some(path), Some(store_cipher))
}
@@ -300,7 +301,7 @@ impl SledStore {
}
fn serialize_event(&self, event: &impl Serialize) -> Result<Vec<u8>, SledStoreError> {
if let Some(key) = &*self.store_cipher {
if let Some(key) = &self.store_cipher {
Ok(key.encrypt_value(event)?)
} else {
Ok(serde_json::to_vec(event)?)
@@ -311,7 +312,7 @@ impl SledStore {
&self,
event: &[u8],
) -> Result<T, SledStoreError> {
if let Some(key) = &*self.store_cipher {
if let Some(key) = &self.store_cipher {
Ok(key.decrypt_value(event)?)
} else {
Ok(serde_json::from_slice(event)?)
@@ -319,7 +320,7 @@ impl SledStore {
}
fn encode_key<T: EncodeKey>(&self, table_name: &str, key: T) -> Vec<u8> {
if let Some(store_cipher) = &*self.store_cipher {
if let Some(store_cipher) = &self.store_cipher {
key.encode_secure(table_name, store_cipher).to_vec()
} else {
key.encode()