Skip to content

Commit b67950b

Browse files
committed
enable remote encryption support for remote and synced databases
1 parent 84961b8 commit b67950b

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

libsql/examples/encryption_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn main() {
2424
None
2525
};
2626

27-
let db_builder = Builder::new_synced_database(db_path, sync_url, auth_token, encryption);
27+
let db_builder = Builder::new_synced_database(db_path, sync_url, auth_token);
2828

2929
let db = match db_builder.build().await {
3030
Ok(db) => db,

libsql/src/database.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ enum DbType {
109109
connector: crate::util::ConnectorService,
110110
version: Option<String>,
111111
namespace: Option<String>,
112+
remote_encryption: Option<crate::sync::EncryptionContext>,
112113
},
113114
}
114115

@@ -545,6 +546,7 @@ cfg_remote! {
545546
connector: crate::util::ConnectorService::new(svc),
546547
version,
547548
namespace: None,
549+
remote_encryption: None
548550
},
549551
max_write_replication_index: Default::default(),
550552
})
@@ -733,6 +735,7 @@ impl Database {
733735
connector,
734736
version,
735737
namespace,
738+
remote_encryption,
736739
} => {
737740
let conn = std::sync::Arc::new(
738741
crate::hrana::connection::HttpConnection::new_with_connector(
@@ -741,7 +744,7 @@ impl Database {
741744
connector.clone(),
742745
version.as_ref().map(|s| s.as_str()),
743746
namespace.as_ref().map(|s| s.as_str()),
744-
None,
747+
remote_encryption.clone(),
745748
),
746749
);
747750

libsql/src/database/builder.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ impl Builder<()> {
5252
path: impl AsRef<std::path::Path>,
5353
url: String,
5454
auth_token: String,
55-
#[cfg(feature = "sync")]
56-
remote_encryption: Option<crate::sync::EncryptionContext>,
5755
) -> Builder<RemoteReplica> {
5856
Builder {
5957
inner: RemoteReplica {
@@ -64,6 +62,7 @@ impl Builder<()> {
6462
connector: None,
6563
version: None,
6664
namespace: None,
65+
remote_encryption: None
6766
},
6867
encryption_config: None,
6968
read_your_writes: true,
@@ -73,7 +72,7 @@ impl Builder<()> {
7372
#[cfg(feature = "sync")]
7473
sync_protocol: Default::default(),
7574
#[cfg(feature = "sync")]
76-
remote_encryption,
75+
remote_encryption: None
7776
},
7877
}
7978
}
@@ -98,7 +97,6 @@ impl Builder<()> {
9897
path: impl AsRef<std::path::Path>,
9998
url: String,
10099
auth_token: String,
101-
remote_encryption: Option<EncryptionContext>,
102100
) -> Builder<SyncedDatabase> {
103101
Builder {
104102
inner: SyncedDatabase {
@@ -110,13 +108,14 @@ impl Builder<()> {
110108
connector: None,
111109
version: None,
112110
namespace: None,
111+
remote_encryption: None,
113112
},
114113
connector: None,
115114
read_your_writes: true,
116115
remote_writes: false,
117116
push_batch_size: 0,
118117
sync_interval: None,
119-
remote_encryption,
118+
remote_encryption: None,
120119
},
121120
}
122121
}
@@ -132,6 +131,7 @@ impl Builder<()> {
132131
connector: None,
133132
version: None,
134133
namespace: None,
134+
remote_encryption: None,
135135
},
136136
}
137137
}
@@ -146,6 +146,7 @@ cfg_replication_or_remote_or_sync! {
146146
connector: Option<crate::util::ConnectorService>,
147147
version: Option<String>,
148148
namespace: Option<String>,
149+
remote_encryption: Option<EncryptionContext>,
149150
}
150151
}
151152

@@ -238,7 +239,7 @@ cfg_replication! {
238239
#[cfg(feature = "sync")]
239240
sync_protocol: super::SyncProtocol,
240241
#[cfg(feature = "sync")]
241-
remote_encryption: Option<crate::sync::EncryptionContext>,
242+
remote_encryption: Option<EncryptionContext>,
242243
}
243244

244245
/// Local replica configuration type in [`Builder`].
@@ -300,6 +301,13 @@ cfg_replication! {
300301
self
301302
}
302303

304+
/// Set the encryption context if the database is encrypted in remote server.
305+
#[cfg(feature = "sync")]
306+
pub fn remote_encryption(mut self, encryption_context: EncryptionContext) -> Builder<RemoteReplica> {
307+
self.inner.remote_encryption = Some(encryption_context);
308+
self
309+
}
310+
303311
pub fn http_request_callback<F>(mut self, f: F) -> Builder<RemoteReplica>
304312
where
305313
F: Fn(&mut http::Request<()>) + Send + Sync + 'static
@@ -347,6 +355,7 @@ cfg_replication! {
347355
connector,
348356
version,
349357
namespace,
358+
..
350359
},
351360
encryption_config,
352361
read_your_writes,
@@ -415,10 +424,11 @@ cfg_replication! {
415424

416425
if res.status().is_success() {
417426
tracing::trace!("Using sync protocol v2 for {}", url);
418-
let builder = Builder::new_synced_database(path, url, auth_token, remote_encryption)
427+
let builder = Builder::new_synced_database(path, url, auth_token)
419428
.connector(connector)
420429
.remote_writes(true)
421-
.read_your_writes(read_your_writes);
430+
.read_your_writes(read_your_writes)
431+
.remote_encryption(remote_encryption);
422432

423433
let builder = if let Some(sync_interval) = sync_interval {
424434
builder.sync_interval(sync_interval)
@@ -475,7 +485,10 @@ cfg_replication! {
475485

476486

477487
Ok(Database {
478-
db_type: DbType::Sync { db, encryption_config },
488+
db_type: DbType::Sync {
489+
db,
490+
encryption_config,
491+
},
479492
max_write_replication_index: Default::default(),
480493
})
481494
}
@@ -515,6 +528,7 @@ cfg_replication! {
515528
connector,
516529
version,
517530
namespace,
531+
..
518532
}) = remote
519533
{
520534
let connector = if let Some(connector) = connector {
@@ -598,6 +612,12 @@ cfg_sync! {
598612
self
599613
}
600614

615+
/// Set the encryption context if the database is encrypted in remote server.
616+
pub fn remote_encryption(mut self, encryption_context: Option<EncryptionContext>) -> Builder<SyncedDatabase> {
617+
self.inner.remote_encryption = encryption_context;
618+
self
619+
}
620+
601621
/// Provide a custom http connector that will be used to create http connections.
602622
pub fn connector<C>(mut self, connector: C) -> Builder<SyncedDatabase>
603623
where
@@ -624,6 +644,7 @@ cfg_sync! {
624644
connector: _,
625645
version: _,
626646
namespace: _,
647+
..
627648
},
628649
connector,
629650
remote_writes,
@@ -759,6 +780,12 @@ cfg_remote! {
759780
self
760781
}
761782

783+
/// Set the encryption context if the database is encrypted in remote server.
784+
pub fn remote_encryption(mut self, encryption_context: EncryptionContext) -> Builder<Remote> {
785+
self.inner.remote_encryption = Some(encryption_context);
786+
self
787+
}
788+
762789
/// Build the remote database client.
763790
pub async fn build(self) -> Result<Database> {
764791
let Remote {
@@ -767,6 +794,7 @@ cfg_remote! {
767794
connector,
768795
version,
769796
namespace,
797+
remote_encryption,
770798
} = self.inner;
771799

772800
let connector = if let Some(connector) = connector {
@@ -789,6 +817,7 @@ cfg_remote! {
789817
connector,
790818
version,
791819
namespace,
820+
remote_encryption
792821
},
793822
max_write_replication_index: Default::default(),
794823
})

0 commit comments

Comments
 (0)