Skip to content

Commit 8972c41

Browse files
authored
Merge pull request #315 from cipherstash/refactor-rename-encrypt-to-proxy
refactor: rename Encrypt module to Proxy for better clarity
2 parents 94addf4 + a908238 commit 8972c41

File tree

16 files changed

+97
-115
lines changed

16 files changed

+97
-115
lines changed

packages/cipherstash-proxy/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
pub mod cli;
44
pub mod config;
55
pub mod connect;
6-
pub mod encrypt;
76
pub mod eql;
87
pub mod error;
98
pub mod log;
109
pub mod postgresql;
1110
pub mod prometheus;
11+
pub mod proxy;
1212
pub mod tls;
1313

1414
pub use crate::cli::Args;
1515
pub use crate::cli::Migrate;
1616
pub use crate::config::{DatabaseConfig, ServerConfig, TandemConfig, TlsConfig};
17-
pub use crate::encrypt::Encrypt;
1817
pub use crate::eql::{EqlEncrypted, ForQuery, Identifier, Plaintext};
1918
pub use crate::log::init;
19+
pub use crate::proxy::Proxy;
2020

2121
use std::mem;
2222

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub const AUTHENTICATION: &str = "authentication";
1818
pub const CONFIG: &str = "config";
1919
pub const CONTEXT: &str = "context";
2020
pub const ENCRYPT: &str = "encrypt";
21+
pub const PROXY: &str = "proxy";
2122
pub const DECRYPT: &str = "decrypt";
2223
pub const ENCODING: &str = "encoding";
2324
pub const ENCRYPT_CONFIG: &str = "encrypt_config";

packages/cipherstash-proxy/src/main.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use cipherstash_proxy::config::TandemConfig;
22
use cipherstash_proxy::connect::{self, AsyncStream};
3-
use cipherstash_proxy::encrypt::Encrypt;
43
use cipherstash_proxy::error::Error;
54
use cipherstash_proxy::prometheus::CLIENTS_ACTIVE_CONNECTIONS;
5+
use cipherstash_proxy::proxy::Proxy;
66
use cipherstash_proxy::{cli, log, postgresql as pg, prometheus, tls, Args};
77
use clap::Parser;
88
use metrics::gauge;
@@ -53,16 +53,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5353
runtime.block_on(async move {
5454
let shutdown_timeout = &config.server.shutdown_timeout();
5555

56-
let mut encrypt = init(config).await;
56+
let mut proxy = init(config).await;
5757

58-
let mut listener = connect::bind_with_retry(&encrypt.config.server).await;
58+
let mut listener = connect::bind_with_retry(&proxy.config.server).await;
5959
let tracker = TaskTracker::new();
6060

6161
let mut client_id = 0;
6262

63-
if encrypt.config.prometheus_enabled() {
64-
let host = encrypt.config.server.host.to_owned();
65-
match prometheus::start(host, encrypt.config.prometheus.port) {
63+
if proxy.config.prometheus_enabled() {
64+
let host = proxy.config.server.host.to_owned();
65+
match prometheus::start(host, proxy.config.prometheus.port) {
6666
Ok(_) => {}
6767
Err(err) => {
6868
error!(
@@ -82,7 +82,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8282
},
8383
_ = sighup() => {
8484
info!(msg = "Received SIGHUP. Reloading configuration");
85-
(listener, encrypt) = reload_config(listener, &args, encrypt).await;
85+
(listener, proxy) = reload_config(listener, &args, proxy).await;
8686
info!(msg = "Reloaded configuration");
8787
},
8888
_ = sigterm() => {
@@ -91,16 +91,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9191
},
9292
Ok(client_stream) = AsyncStream::accept(&listener) => {
9393

94-
let encrypt = encrypt.clone();
94+
let proxy = proxy.clone();
9595

9696
client_id += 1;
9797

9898
tracker.spawn(async move {
99-
let encrypt = encrypt.clone();
99+
let proxy = proxy.clone();
100100

101101
gauge!(CLIENTS_ACTIVE_CONNECTIONS).increment(1);
102102

103-
match pg::handler(client_stream, encrypt, client_id).await {
103+
match pg::handler(client_stream, proxy, client_id).await {
104104
Ok(_) => (),
105105
Err(err) => {
106106

@@ -145,9 +145,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
145145

146146
///
147147
/// Validate various configuration options and
148-
/// Init the Encrypt service
148+
/// Init the Proxy service
149149
///
150-
async fn init(mut config: TandemConfig) -> Encrypt {
150+
async fn init(mut config: TandemConfig) -> Proxy {
151151
if config.encrypt.default_keyset_id.is_none() {
152152
warn!(msg = "Default Keyset Id has not been configured");
153153
warn!(msg = "A Keyset Identifier must be set using the `SET CIPHERSTASH.KEYSET_ID` or `SET CIPHERSTASH.KEYSET_NAME` commands");
@@ -216,25 +216,25 @@ async fn init(mut config: TandemConfig) -> Encrypt {
216216
}
217217
}
218218

219-
match Encrypt::init(config).await {
220-
Ok(encrypt) => {
221-
info!(msg = "Connected to CipherStash Encrypt");
219+
match Proxy::init(config).await {
220+
Ok(proxy) => {
221+
info!(msg = "Connected to CipherStash Proxy");
222222
info!(
223223
msg = "Connected to Database",
224-
database = encrypt.config.database.name,
225-
host = encrypt.config.database.host,
226-
port = encrypt.config.database.port,
227-
username = encrypt.config.database.username,
228-
eql_version = encrypt.eql_version,
224+
database = proxy.config.database.name,
225+
host = proxy.config.database.host,
226+
port = proxy.config.database.port,
227+
username = proxy.config.database.username,
228+
eql_version = proxy.eql_version,
229229
);
230-
if encrypt.eql_version.as_deref() != EQL_VERSION_AT_BUILD_TIME {
230+
if proxy.eql_version.as_deref() != EQL_VERSION_AT_BUILD_TIME {
231231
warn!(
232232
msg = "installed version of EQL is different to the version that Proxy was built with",
233233
eql_build_version = EQL_VERSION_AT_BUILD_TIME,
234-
eql_installed_version = encrypt.eql_version,
234+
eql_installed_version = proxy.eql_version,
235235
);
236236
}
237-
encrypt
237+
proxy
238238
}
239239
Err(err) => {
240240
error!(
@@ -261,29 +261,25 @@ async fn sighup() -> std::io::Result<()> {
261261
Ok(())
262262
}
263263

264-
async fn reload_config(
265-
listener: TcpListener,
266-
args: &Args,
267-
encrypt: Encrypt,
268-
) -> (TcpListener, Encrypt) {
264+
async fn reload_config(listener: TcpListener, args: &Args, proxy: Proxy) -> (TcpListener, Proxy) {
269265
let new_config = match TandemConfig::load(args) {
270266
Ok(config) => config,
271267
Err(err) => {
272268
warn!(
273269
msg = "Configuration could not be reloaded: {}",
274270
error = err.to_string()
275271
);
276-
return (listener, encrypt);
272+
return (listener, proxy);
277273
}
278274
};
279275

280-
let new_encrypt = init(new_config).await;
276+
let new_proxy = init(new_config).await;
281277

282278
// Explicit drop needed here to free the network resources before binding if using the same address & port
283279
std::mem::drop(listener);
284280

285281
(
286-
connect::bind_with_retry(&new_encrypt.config.server).await,
287-
new_encrypt,
282+
connect::bind_with_retry(&new_proxy.config.server).await,
283+
new_proxy,
288284
)
289285
}

packages/cipherstash-proxy/src/postgresql/backend.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::messages::row_description::RowDescription;
77
use super::messages::BackendCode;
88
use super::Column;
99
use crate::connect::Sender;
10-
use crate::encrypt::Encrypt;
1110
use crate::eql::EqlEncrypted;
1211
use crate::error::{EncryptError, Error};
1312
use crate::log::{CONTEXT, DEVELOPMENT, MAPPER, PROTOCOL};
@@ -20,6 +19,7 @@ use crate::prometheus::{
2019
DECRYPTION_ERROR_TOTAL, DECRYPTION_REQUESTS_TOTAL, ROWS_ENCRYPTED_TOTAL,
2120
ROWS_PASSTHROUGH_TOTAL, ROWS_TOTAL, SERVER_BYTES_RECEIVED_TOTAL,
2221
};
22+
use crate::proxy::Proxy;
2323
use bytes::BytesMut;
2424
use metrics::{counter, histogram};
2525
use std::time::Instant;
@@ -79,7 +79,7 @@ where
7979
/// Reader for incoming messages from server
8080
server_reader: R,
8181
/// Encryption service for column decryption
82-
encrypt: Encrypt,
82+
proxy: Proxy,
8383
/// Session context with portal and statement metadata
8484
context: Context,
8585
/// Buffer for batching DataRow messages before decryption
@@ -98,17 +98,12 @@ where
9898
/// * `server_reader` - Stream for reading messages from the PostgreSQL server
9999
/// * `encrypt` - Encryption service for handling column decryption
100100
/// * `context` - Session context shared with the frontend
101-
pub fn new(
102-
client_sender: Sender,
103-
server_reader: R,
104-
encrypt: Encrypt,
105-
context: Context,
106-
) -> Self {
101+
pub fn new(client_sender: Sender, server_reader: R, proxy: Proxy, context: Context) -> Self {
107102
let buffer = MessageBuffer::new();
108103
Backend {
109104
client_sender,
110105
server_reader,
111-
encrypt,
106+
proxy,
112107
context,
113108
buffer,
114109
}
@@ -155,7 +150,7 @@ where
155150
/// Returns `Ok(())` on successful message processing, or an `Error` if a fatal
156151
/// error occurs that should terminate the connection.
157152
pub async fn rewrite(&mut self) -> Result<(), Error> {
158-
let connection_timeout = self.encrypt.config.database.connection_timeout();
153+
let connection_timeout = self.proxy.config.database.connection_timeout();
159154

160155
let (code, mut bytes) = protocol::read_message(
161156
&mut self.server_reader,
@@ -167,7 +162,7 @@ where
167162
let sent: u64 = bytes.len() as u64;
168163
counter!(SERVER_BYTES_RECEIVED_TOTAL).increment(sent);
169164

170-
if self.encrypt.is_passthrough() {
165+
if self.proxy.is_passthrough() {
171166
debug!(target: DEVELOPMENT,
172167
client_id = self.context.client_id,
173168
msg = "Passthrough enabled"
@@ -255,7 +250,7 @@ where
255250
msg = "ReadyForQuery"
256251
);
257252
if self.context.schema_changed() {
258-
self.encrypt.reload_schema().await;
253+
self.proxy.reload_schema().await;
259254
}
260255
}
261256

@@ -456,15 +451,15 @@ where
456451

457452
// Decrypt CipherText -> Plaintext
458453
let plaintexts = self
459-
.encrypt
454+
.proxy
460455
.decrypt(keyset_id, ciphertexts)
461456
.await
462457
.inspect_err(|_| {
463458
counter!(DECRYPTION_ERROR_TOTAL).increment(1);
464459
})?;
465460

466461
// Avoid the iter calculation if we can
467-
if self.encrypt.config.prometheus_enabled() {
462+
if self.proxy.config.prometheus_enabled() {
468463
let decrypted_count =
469464
plaintexts
470465
.iter()

0 commit comments

Comments
 (0)