Skip to content

Commit

Permalink
chore: add Data struct
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jun 11, 2024
1 parent dab84db commit 2f4d537
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setup-db:
reset-deps: clean-deps start-deps setup-db

run-server:
cargo run --bin cala-server -- --config ./bats/cala.yml
cargo run --bin cala-server -- --config ./bats/cala.yml

rust-example:
cargo run --bin cala-ledger-example-rust
Expand Down
3 changes: 2 additions & 1 deletion cala-server/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Config {
Config::default()
};

let _ = config.apply_env_override(env_override);
let _ = config.apply_env_override(env_override)?;
Ok(config)
}

Expand All @@ -60,6 +60,7 @@ impl Config {
}

self.app.encryption.key = EncryptionKey::clone_from_slice(key_bytes.as_ref());

Ok(())
}
}
33 changes: 21 additions & 12 deletions cala-server/src/integration/encryption_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chacha20poly1305::{
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use super::{error::IntegrationError, Integration};
use super::{error::IntegrationError, Data};

pub type EncryptionKey = chacha20poly1305::Key;
#[derive(Clone)]
Expand All @@ -19,15 +19,15 @@ pub struct EncryptionConfig {
pub key: EncryptionKey,
}

impl Integration {
impl Data {
pub(super) fn encrypt(
&self,
key: &EncryptionKey,
) -> Result<(ConfigCipher, Nonce), IntegrationError> {
let cipher = ChaCha20Poly1305::new(key);
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
let encrypted_config = cipher
.encrypt(&nonce, serde_json::to_vec(&self.data)?.as_slice())
.encrypt(&nonce, serde_json::to_vec(&self.0)?.as_slice())
.expect("should always encrypt");
Ok((ConfigCipher(encrypted_config), Nonce(nonce.to_vec())))
}
Expand Down Expand Up @@ -81,19 +81,19 @@ impl std::fmt::Debug for EncryptionConfig {

#[cfg(test)]
mod tests {
pub use super::*;
use super::*;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct Data {
struct Dummy {
pub name: String,
pub secret: String,
}

impl Default for Data {
impl Default for Dummy {
fn default() -> Self {
Self {
name: "Alice".to_string(),
secret: "secret".to_string(),
secret: "Secret".to_string(),
}
}
}
Expand All @@ -104,12 +104,21 @@ mod tests {

#[test]
fn encrypt_decrypt() {
let integration = Integration::new("name".to_string(), Data::default());
let key = gen_encryption_key();
let (encrypted, nonce) = integration.encrypt(&key).expect("Failed to encrypt");
let decrypted: Data =
Integration::decrypt(&encrypted, &nonce, &key).expect("Failed to decrypt");
let data = Data::new(Dummy::default());
let (encrypted, nonce) = data.encrypt(&key).expect("Failed to encrypt");
let decrypted: Dummy = Data::decrypt(&encrypted, &nonce, &key).expect("Failed to decrypt");

assert_eq!(integration.data, serde_json::to_value(&decrypted).unwrap());
assert_eq!(data.0, serde_json::to_value(&decrypted).unwrap());
}

#[test]
fn serialize_deserialize() {
let key = gen_encryption_key();
let encryption_config = EncryptionConfig { key };
let serialized = serde_json::to_string(&encryption_config).unwrap();
let deserialized: EncryptionConfig = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.key, key);
assert_eq!(encryption_config, deserialized)
}
}
27 changes: 22 additions & 5 deletions cala-server/src/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::{Deserialize, Serialize};

mod encryption_config;
pub mod error;

Expand All @@ -13,19 +15,34 @@ cala_types::entity_id! { IntegrationId }
pub struct Integration {
pub id: IntegrationId,
pub name: String,
data: serde_json::Value,
data: Data,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct Data(serde_json::Value);

impl Data {
fn new(data: impl serde::Serialize) -> Self {
Self(serde_json::to_value(data).unwrap())
}
}

impl AsRef<serde_json::Value> for Data {
fn as_ref(&self) -> &serde_json::Value {
&self.0
}
}

impl Integration {
fn new(id: IntegrationId, name: String, data: impl serde::Serialize) -> Self {
Self {
id,
name,
data: serde_json::to_value(data).expect("Could not serialize data"),
data: Data::new(data),
}
}
pub fn data<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
serde_json::from_value(self.data.clone())
serde_json::from_value(self.data.as_ref().clone())
}
}

Expand All @@ -50,7 +67,7 @@ impl Integrations {
data: impl serde::Serialize,
) -> Result<Integration, IntegrationError> {
let integration = Integration::new(name, data);
let (cipher, nonce) = integration.encrypt(&self.encryption_config.key)?;
let (cipher, nonce) = integration.data.encrypt(&self.encryption_config.key)?;
sqlx::query!(
r#"INSERT INTO integrations (id, name, cipher, nonce)
VALUES ($1, $2, $3, $4)"#,
Expand Down Expand Up @@ -78,7 +95,7 @@ impl Integrations {
.fetch_one(&self.pool)
.await?;

let data = Integration::decrypt(
let data = Data::decrypt(
&ConfigCipher(row.cipher),
&Nonce(row.nonce),
&self.encryption_config.key,
Expand Down

0 comments on commit 2f4d537

Please sign in to comment.