Skip to content

Commit 9abc061

Browse files
committed
Encryption at rest support
1 parent 7579dff commit 9abc061

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ crate-type = ["cdylib"]
99

1010
[dependencies]
1111
pyo3 = "0.19.0"
12-
libsql = { version = "0.3.0" }
12+
libsql = { version = "0.3.0", features = ["encryption"] }
1313
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
1414
tracing-subscriber = "0.3"

src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn is_remote_path(path: &str) -> bool {
1919
}
2020

2121
#[pyfunction]
22-
#[pyo3(signature = (database, isolation_level="DEFERRED".to_string(), check_same_thread=true, uri=false, sync_url=None, auth_token=""))]
22+
#[pyo3(signature = (database, isolation_level="DEFERRED".to_string(), check_same_thread=true, uri=false, sync_url=None, auth_token="", encryption_key=None))]
2323
fn connect(
2424
py: Python<'_>,
2525
database: String,
@@ -28,10 +28,19 @@ fn connect(
2828
uri: bool,
2929
sync_url: Option<String>,
3030
auth_token: &str,
31+
encryption_key: Option<String>,
3132
) -> PyResult<Connection> {
3233
let ver = env!("CARGO_PKG_VERSION");
3334
let ver = format!("libsql-python-rpc-{ver}");
3435
let rt = tokio::runtime::Runtime::new().unwrap();
36+
let encryption_config = match encryption_key {
37+
Some(key) => {
38+
let cipher = libsql::Cipher::default();
39+
let encryption_config = libsql::EncryptionConfig::new(cipher, key.into());
40+
Some(encryption_config)
41+
}
42+
None => None,
43+
};
3544
let db = if is_remote_path(&database) {
3645
let result = libsql::Database::open_remote_internal(database.clone(), auth_token, ver);
3746
result.map_err(to_py_err)?
@@ -44,14 +53,23 @@ fn connect(
4453
auth_token,
4554
Some(ver),
4655
true,
47-
None,
56+
encryption_config,
4857
None,
4958
);
5059
tokio::pin!(fut);
5160
let result = rt.block_on(check_signals(py, fut));
5261
result.map_err(to_py_err)?
5362
}
54-
None => libsql_core::Database::open(database).map_err(to_py_err)?,
63+
None => {
64+
let mut builder = libsql::Builder::new_local(database);
65+
if let Some(config) = encryption_config {
66+
builder = builder.encryption_config(config);
67+
}
68+
let fut = builder.build();
69+
tokio::pin!(fut);
70+
let result = rt.block_on(check_signals(py, fut));
71+
result.map_err(to_py_err)?
72+
}
5573
}
5674
};
5775
let autocommit = isolation_level.is_none();

0 commit comments

Comments
 (0)