Skip to content

Commit eb1b39e

Browse files
authored
Merge pull request #1708 from tursodatabase/test-replication-proxy
test replication proxy
2 parents e94f7bd + 2b25748 commit eb1b39e

File tree

9 files changed

+218
-26
lines changed

9 files changed

+218
-26
lines changed

libsql-server/src/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use parsers::{parse_http_auth_header, parse_http_basic_auth_arg, parse_jwt_k
1515
pub use permission::Permission;
1616
pub use user_auth_strategies::{Disabled, HttpBasic, Jwt, UserAuthContext, UserAuthStrategy};
1717

18-
#[derive(Clone)]
18+
#[derive(Clone, Debug)]
1919
pub struct Auth {
2020
pub user_strategy: Arc<dyn UserAuthStrategy + Send + Sync>,
2121
}

libsql-server/src/auth/user_auth_strategies/disabled.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{UserAuthContext, UserAuthStrategy};
22
use crate::auth::{AuthError, Authenticated};
33

4+
#[derive(Debug)]
45
pub struct Disabled {}
56

67
impl UserAuthStrategy for Disabled {

libsql-server/src/auth/user_auth_strategies/http_basic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::auth::{
55

66
use super::{UserAuthContext, UserAuthStrategy};
77

8+
#[derive(Debug)]
89
pub struct HttpBasic {
910
credential: String,
1011
}

libsql-server/src/auth/user_auth_strategies/jwt.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{self, Debug, Formatter};
2+
13
use chrono::{DateTime, Utc};
24

35
use crate::{
@@ -15,6 +17,12 @@ pub struct Jwt {
1517
keys: Vec<jsonwebtoken::DecodingKey>,
1618
}
1719

20+
impl Debug for Jwt {
21+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22+
f.debug_struct("Jwt").finish()
23+
}
24+
}
25+
1826
impl UserAuthStrategy for Jwt {
1927
fn authenticate(&self, ctx: UserAuthContext) -> Result<Authenticated, AuthError> {
2028
tracing::trace!("executing jwt auth");

libsql-server/src/auth/user_auth_strategies/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl UserAuthContext {
5555
}
5656
}
5757

58-
pub trait UserAuthStrategy: Sync + Send {
58+
pub trait UserAuthStrategy: Sync + Send + std::fmt::Debug {
5959
/// Returns a list of fields required by the stragegy.
6060
/// Every strategy implementation should override this function if it requires input to work.
6161
/// Strategy implementations should validate the content of provided fields.

libsql-server/src/lib.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ pub(crate) static BLOCKING_RT: Lazy<Runtime> = Lazy::new(|| {
129129
type Result<T, E = Error> = std::result::Result<T, E>;
130130
type StatsSender = mpsc::Sender<(NamespaceName, MetaStoreHandle, Weak<Stats>)>;
131131
type MakeReplicationSvc = Box<
132-
dyn FnOnce(
132+
dyn Fn(
133133
NamespaceStore,
134134
Option<Auth>,
135135
Option<IdleShutdownKicker>,
136136
bool,
137+
bool,
137138
) -> BoxReplicationService
138139
+ Send
139140
+ 'static,
@@ -620,17 +621,18 @@ where
620621

621622
let replication_service = make_replication_svc(
622623
namespace_store.clone(),
623-
None,
624+
Some(user_auth_strategy.clone()),
624625
idle_shutdown_kicker.clone(),
625626
false,
627+
true,
626628
);
627629

628630
task_manager.spawn_until_shutdown(run_rpc_server(
629631
proxy_service,
630632
config.acceptor,
631633
config.tls_config,
632634
idle_shutdown_kicker.clone(),
633-
replication_service,
635+
replication_service, // internal replicaton service
634636
));
635637
}
636638

@@ -658,12 +660,12 @@ where
658660
.await?;
659661
}
660662

661-
let replication_svc = ReplicationLogService::new(
663+
let replication_svc = make_replication_svc(
662664
namespace_store.clone(),
663-
idle_shutdown_kicker.clone(),
664665
Some(user_auth_strategy.clone()),
665-
self.disable_namespaces,
666+
idle_shutdown_kicker.clone(),
666667
true,
668+
false, // external replication service
667669
);
668670

669671
let proxy_svc = ProxyService::new(
@@ -936,9 +938,9 @@ where
936938
let make_replication_svc = Box::new({
937939
let registry = registry.clone();
938940
let disable_namespaces = self.disable_namespaces;
939-
move |store, user_auth, _, _| -> BoxReplicationService {
941+
move |store, user_auth, _, _, _| -> BoxReplicationService {
940942
Box::new(LibsqlReplicationService::new(
941-
registry,
943+
registry.clone(),
942944
store,
943945
user_auth,
944946
disable_namespaces,
@@ -1023,13 +1025,19 @@ where
10231025

10241026
let make_replication_svc = Box::new({
10251027
let disable_namespaces = self.disable_namespaces;
1026-
move |store, client_auth, idle_shutdown, collect_stats| -> BoxReplicationService {
1028+
move |store,
1029+
client_auth,
1030+
idle_shutdown,
1031+
collect_stats,
1032+
is_internal|
1033+
-> BoxReplicationService {
10271034
Box::new(ReplicationLogService::new(
10281035
store,
10291036
idle_shutdown,
10301037
client_auth,
10311038
disable_namespaces,
10321039
collect_stats,
1040+
is_internal,
10331041
))
10341042
}
10351043
});
@@ -1055,13 +1063,19 @@ where
10551063

10561064
let make_replication_svc = Box::new({
10571065
let disable_namespaces = self.disable_namespaces;
1058-
move |store, client_auth, idle_shutdown, collect_stats| -> BoxReplicationService {
1066+
move |store,
1067+
client_auth,
1068+
idle_shutdown,
1069+
collect_stats,
1070+
is_internal|
1071+
-> BoxReplicationService {
10591072
Box::new(ReplicationLogService::new(
10601073
store,
10611074
idle_shutdown,
10621075
client_auth,
10631076
disable_namespaces,
10641077
collect_stats,
1078+
is_internal,
10651079
))
10661080
}
10671081
});

libsql-server/src/rpc/replication/replication_log.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub struct ReplicationLogService {
3737
disable_namespaces: bool,
3838
session_token: Bytes,
3939
collect_stats: bool,
40+
// whether this is an internal service. If it is an internal service, auth is checked for
41+
// proxied requests
42+
service_internal: bool,
4043

4144
//deprecated:
4245
generation_id: Uuid,
@@ -52,6 +55,7 @@ impl ReplicationLogService {
5255
user_auth_strategy: Option<Auth>,
5356
disable_namespaces: bool,
5457
collect_stats: bool,
58+
service_internal: bool,
5559
) -> Self {
5660
let session_token = Uuid::new_v4().to_string().into();
5761
Self {
@@ -63,6 +67,7 @@ impl ReplicationLogService {
6367
collect_stats,
6468
generation_id: Uuid::new_v4(),
6569
replicas_with_hello: Default::default(),
70+
service_internal,
6671
}
6772
}
6873

@@ -71,14 +76,20 @@ impl ReplicationLogService {
7176
req: &tonic::Request<T>,
7277
namespace: NamespaceName,
7378
) -> Result<(), Status> {
74-
super::auth::authenticate(
75-
&self.namespaces,
76-
req,
77-
namespace,
78-
&self.user_auth_strategy,
79-
true,
80-
)
81-
.await
79+
if self.service_internal && req.metadata().get("libsql-proxied").is_some()
80+
|| !self.service_internal
81+
{
82+
super::auth::authenticate(
83+
&self.namespaces,
84+
req,
85+
namespace,
86+
&self.user_auth_strategy,
87+
true,
88+
)
89+
.await
90+
} else {
91+
Ok(())
92+
}
8293
}
8394

8495
fn verify_session_token<R>(

libsql-server/src/rpc/replication/replication_log_proxy.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hyper::Uri;
2+
use tonic::metadata::AsciiMetadataValue;
23
use tonic::{transport::Channel, Status};
34

45
use super::replication_log::rpc::replication_log_client::ReplicationLogClient;
@@ -19,6 +20,12 @@ impl ReplicationLogProxyService {
1920
}
2021
}
2122

23+
fn mark_proxied<T>(mut req: tonic::Request<T>) -> tonic::Request<T> {
24+
req.metadata_mut()
25+
.insert("libsql-proxied", AsciiMetadataValue::from_static("true"));
26+
req
27+
}
28+
2229
#[tonic::async_trait]
2330
impl ReplicationLog for ReplicationLogProxyService {
2431
type LogEntriesStream = tonic::codec::Streaming<Frame>;
@@ -29,30 +36,30 @@ impl ReplicationLog for ReplicationLogProxyService {
2936
req: tonic::Request<LogOffset>,
3037
) -> Result<tonic::Response<Self::LogEntriesStream>, Status> {
3138
let mut client = self.client.clone();
32-
client.log_entries(req).await
39+
client.log_entries(mark_proxied(req)).await
3340
}
3441

3542
async fn batch_log_entries(
3643
&self,
3744
req: tonic::Request<LogOffset>,
3845
) -> Result<tonic::Response<Frames>, Status> {
3946
let mut client = self.client.clone();
40-
client.batch_log_entries(req).await
47+
client.batch_log_entries(mark_proxied(req)).await
4148
}
4249

4350
async fn hello(
4451
&self,
4552
req: tonic::Request<HelloRequest>,
4653
) -> Result<tonic::Response<HelloResponse>, Status> {
4754
let mut client = self.client.clone();
48-
client.hello(req).await
55+
client.hello(mark_proxied(req)).await
4956
}
5057

5158
async fn snapshot(
5259
&self,
5360
req: tonic::Request<LogOffset>,
5461
) -> Result<tonic::Response<Self::SnapshotStream>, Status> {
5562
let mut client = self.client.clone();
56-
client.snapshot(req).await
63+
client.snapshot(mark_proxied(req)).await
5764
}
5865
}

0 commit comments

Comments
 (0)