Skip to content

Commit 91d1740

Browse files
committed
♻️ refactor: extract encrypt config into dedicated module
- Move encryption configuration files from proxy/config/ to proxy/encrypt_config/ - Rename EncryptConfig to ColumnEncryptionConfig for clarity - Create new EncryptConfig wrapper struct in manager for better encapsulation - Update module structure and imports throughout codebase
1 parent b2b6ea0 commit 91d1740

File tree

5 files changed

+61
-31
lines changed

5 files changed

+61
-31
lines changed

packages/cipherstash-proxy/src/proxy/config/mod.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/cipherstash-proxy/src/proxy/config/encrypt_config.rs renamed to packages/cipherstash-proxy/src/proxy/encrypt_config/config.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
use crate::{
22
eql,
33
error::{ConfigError, Error},
4-
log::KEYSET,
54
};
65
use cipherstash_client::schema::{
76
column::{Index, IndexType, TokenFilter, Tokenizer},
87
ColumnConfig, ColumnType,
98
};
109
use serde::{Deserialize, Serialize};
1110
use std::{collections::HashMap, str::FromStr};
12-
use tracing::debug;
11+
12+
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
13+
pub struct ColumnEncryptionConfig {
14+
#[serde(rename = "v")]
15+
pub version: u32,
16+
pub tables: Tables,
17+
}
1318

1419
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
1520
pub struct Tables(HashMap<String, Table>);
@@ -35,13 +40,6 @@ impl IntoIterator for Table {
3540
}
3641
}
3742

38-
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
39-
pub struct EncryptConfig {
40-
#[serde(rename = "v")]
41-
pub version: u32,
42-
pub tables: Tables,
43-
}
44-
4543
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
4644
pub struct Column {
4745
#[serde(default)]
@@ -133,7 +131,7 @@ impl From<CastAs> for ColumnType {
133131
}
134132
}
135133

136-
impl FromStr for EncryptConfig {
134+
impl FromStr for ColumnEncryptionConfig {
137135
type Err = Error;
138136

139137
fn from_str(data: &str) -> Result<Self, Self::Err> {
@@ -142,7 +140,7 @@ impl FromStr for EncryptConfig {
142140
}
143141
}
144142

145-
impl EncryptConfig {
143+
impl ColumnEncryptionConfig {
146144
pub fn is_empty(&self) -> bool {
147145
self.tables.0.is_empty()
148146
}
@@ -151,7 +149,6 @@ impl EncryptConfig {
151149
let mut map = HashMap::new();
152150
for (table_name, columns) in self.tables.into_iter() {
153151
for (column_name, column) in columns.into_iter() {
154-
debug!(target: KEYSET, msg = "Configured column", table = table_name, column = column_name);
155152
let column_config = column.into_column_config(&column_name);
156153
let key = eql::Identifier::new(&table_name, &column_name);
157154
map.insert(key, column_config);
@@ -201,7 +198,7 @@ mod tests {
201198
use super::*;
202199

203200
fn parse(json: serde_json::Value) -> HashMap<eql::Identifier, ColumnConfig> {
204-
serde_json::from_value::<EncryptConfig>(json)
201+
serde_json::from_value::<ColumnEncryptionConfig>(json)
205202
.map(|config| config.into_config_map())
206203
.expect("Error ok")
207204
}

packages/cipherstash-proxy/src/proxy/config/manager.rs renamed to packages/cipherstash-proxy/src/proxy/encrypt_config/manager.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,49 @@ use std::{collections::HashMap, sync::Arc, time::Duration};
1212
use tokio::{task::JoinHandle, time};
1313
use tracing::{debug, error, info, warn};
1414

15-
use super::encrypt_config::EncryptConfig;
15+
use super::config::ColumnEncryptionConfig;
1616

1717
///
1818
/// Column configuration keyed by table name and column name
1919
/// - key: `{table_name}.{column_name}`
2020
///
2121
type EncryptConfigMap = HashMap<eql::Identifier, ColumnConfig>;
2222

23+
#[derive(Clone, Debug)]
24+
pub struct EncryptConfig {
25+
config: EncryptConfigMap,
26+
}
27+
28+
impl EncryptConfig {
29+
pub fn new_from_config(config: EncryptConfigMap) -> Self {
30+
Self { config }
31+
}
32+
33+
pub fn new() -> Self {
34+
Self {
35+
config: HashMap::new(),
36+
}
37+
}
38+
39+
pub fn is_empty(&self) -> bool {
40+
self.config.is_empty()
41+
}
42+
43+
pub fn get_column_config(&self, identifier: &eql::Identifier) -> Option<ColumnConfig> {
44+
self.config.get(identifier).cloned()
45+
}
46+
}
47+
48+
impl Default for EncryptConfig {
49+
fn default() -> Self {
50+
Self::new()
51+
}
52+
}
53+
2354
#[derive(Clone, Debug)]
2455
pub struct EncryptConfigManager {
2556
config: DatabaseConfig,
26-
encrypt_config: Arc<ArcSwap<EncryptConfigMap>>,
57+
encrypt_config: Arc<ArcSwap<EncryptConfig>>,
2758
_reload_handle: Arc<JoinHandle<()>>,
2859
}
2960

@@ -33,7 +64,7 @@ impl EncryptConfigManager {
3364
init_reloader(config).await
3465
}
3566

36-
pub fn load(&self) -> Arc<EncryptConfigMap> {
67+
pub fn load(&self) -> Arc<EncryptConfig> {
3768
self.encrypt_config.load().clone()
3869
}
3970

@@ -79,7 +110,7 @@ async fn init_reloader(config: DatabaseConfig) -> Result<EncryptConfigManager, E
79110
return Err(err);
80111
}
81112
}
82-
HashMap::new()
113+
EncryptConfig::new()
83114
}
84115
};
85116

@@ -136,9 +167,7 @@ async fn init_reloader(config: DatabaseConfig) -> Result<EncryptConfigManager, E
136167
/// When databases and the proxy start up at the same time they might not be ready to accept connections before the
137168
/// proxy tries to query the schema. To give the proxy the best chance of initialising correctly this method will
138169
/// retry the query a few times before passing on the error.
139-
async fn load_encrypt_config_with_retry(
140-
config: &DatabaseConfig,
141-
) -> Result<EncryptConfigMap, Error> {
170+
async fn load_encrypt_config_with_retry(config: &DatabaseConfig) -> Result<EncryptConfig, Error> {
142171
let mut retry_count = 0;
143172
let max_retry_count = 10;
144173
let max_backoff = Duration::from_secs(2);
@@ -170,21 +199,23 @@ async fn load_encrypt_config_with_retry(
170199
}
171200
}
172201

173-
pub async fn load_encrypt_config(config: &DatabaseConfig) -> Result<EncryptConfigMap, Error> {
202+
pub async fn load_encrypt_config(config: &DatabaseConfig) -> Result<EncryptConfig, Error> {
174203
let client = connect::database(config).await?;
175204

176205
match client.query(ENCRYPT_CONFIG_QUERY, &[]).await {
177206
Ok(rows) => {
178207
if rows.is_empty() {
179-
return Ok(EncryptConfigMap::new());
208+
return Ok(EncryptConfig::new());
180209
};
181210

182211
// We know there is at least one row
183212
let row = rows.first().unwrap();
184213

185214
let json_value: Value = row.get("data");
186-
let encrypt_config: EncryptConfig = serde_json::from_value(json_value)?;
187-
Ok(encrypt_config.into_config_map())
215+
let encrypt_config: ColumnEncryptionConfig = serde_json::from_value(json_value)?;
216+
let encrypt_config = EncryptConfig::new_from_config(encrypt_config.into_config_map());
217+
218+
Ok(encrypt_config)
188219
}
189220
Err(err) => {
190221
if configuration_table_not_found(&err) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod config;
2+
mod manager;
3+
4+
pub use manager::{EncryptConfig, EncryptConfigManager};

packages/cipherstash-proxy/src/proxy/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ use crate::{
44
error::Error,
55
log::PROXY,
66
postgresql::{Column, KeysetIdentifier},
7-
proxy::{config::EncryptConfigManager, schema::SchemaManager, zerokms::ZeroKms},
7+
proxy::{encrypt_config::EncryptConfigManager, schema::SchemaManager, zerokms::ZeroKms},
88
};
99
use cipherstash_client::{encryption::Plaintext, schema::ColumnConfig};
1010
use tracing::{debug, warn};
1111

12-
mod config;
12+
mod encrypt_config;
1313
mod schema;
1414
mod zerokms;
1515

16+
pub use encrypt_config::EncryptConfig;
17+
1618
/// SQL Statement for loading encrypt configuration from database
1719
const ENCRYPT_CONFIG_QUERY: &str = include_str!("./sql/select_config.sql");
1820

@@ -118,7 +120,7 @@ impl Proxy {
118120

119121
pub fn get_column_config(&self, identifier: &eql::Identifier) -> Option<ColumnConfig> {
120122
let encrypt_config = self.encrypt_config.load();
121-
encrypt_config.get(identifier).cloned()
123+
encrypt_config.get_column_config(identifier)
122124
}
123125

124126
pub async fn reload_schema(&self) {

0 commit comments

Comments
 (0)