Skip to content

Commit dacfd84

Browse files
tobyhedeauxesis
authored andcommitted
improve config query handling
1 parent 6c4753a commit dacfd84

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

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

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use crate::{
66
};
77
use arc_swap::ArcSwap;
88
use cipherstash_config::ColumnConfig;
9-
use std::str::FromStr;
9+
use serde_json::Value;
1010
use std::{collections::HashMap, sync::Arc, time::Duration};
1111
use tokio::{task::JoinHandle, time};
12-
use tokio_postgres::{SimpleQueryMessage, SimpleQueryRow};
1312
use tracing::{error, info, warn};
1413

1514
///
@@ -109,45 +108,31 @@ async fn load_dataset_with_retry(config: &DatabaseConfig) -> Result<EncryptConfi
109108
pub async fn load_dataset(config: &DatabaseConfig) -> Result<EncryptConfigMap, Error> {
110109
let client = connect::database(config).await?;
111110

112-
let result = client.simple_query(ENCRYPT_DATASET_CONFIG_QUERY).await;
113-
114-
let rows = match result {
115-
Ok(rows) => rows
116-
.into_iter()
117-
.filter_map(|row| match row {
118-
SimpleQueryMessage::Row(row) => Some(row),
119-
_ => None,
120-
})
121-
.collect::<Vec<SimpleQueryRow>>(),
122-
Err(e) => {
123-
if configuration_table_not_found(&e) {
111+
match client.query(ENCRYPT_DATASET_CONFIG_QUERY, &[]).await {
112+
Ok(rows) => {
113+
if rows.is_empty() {
114+
warn!("No active Encrypt configuration");
115+
return Ok(EncryptConfigMap::new());
116+
};
117+
// We know there is at least one row
118+
let row = rows.get(0).unwrap();
119+
120+
let json_value: Value = row.get("data");
121+
let encrypt_config: EncryptConfig = serde_json::from_value(json_value)?;
122+
Ok(encrypt_config.to_config_map())
123+
}
124+
Err(err) => {
125+
if configuration_table_not_found(&err) {
124126
error!("No Encrypt configuration table in database.");
125127
warn!("Encrypt requires the Encrypt Query Language (EQL) to be installed in the target database");
126128
warn!("See https://github.com/cipherstash/encrypt-query-language");
127129

128130
return Err(ConfigError::MissingEncryptConfigTable.into());
129131
}
130132
error!("Error loading Encrypt configuration");
131-
return Err(ConfigError::Database(e).into());
133+
Err(ConfigError::Database(err).into())
132134
}
133-
};
134-
135-
if rows.is_empty() {
136-
warn!("No active Encrypt configuration");
137-
};
138-
139-
let data = rows
140-
.first()
141-
.ok_or_else(|| ConfigError::MissingActiveEncryptConfig)
142-
.and_then(|row| row.try_get(0).map_err(ConfigError::Database))
143-
.and_then(|opt_str: Option<&str>| {
144-
opt_str.ok_or_else(|| ConfigError::MissingActiveEncryptConfig)
145-
})?;
146-
147-
let encrypt = EncryptConfig::from_str(data)?;
148-
let map = encrypt.to_config_map();
149-
150-
Ok(map)
135+
}
151136
}
152137

153138
fn configuration_table_not_found(e: &tokio_postgres::Error) -> bool {

0 commit comments

Comments
 (0)