Skip to content

Commit 85d677b

Browse files
committed
handle auth
1 parent e38cc1e commit 85d677b

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

libsql-server/src/admin_shell.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::fmt::Display;
22
use std::pin::Pin;
3+
use std::str::FromStr;
34

45
use bytes::Bytes;
56
use dialoguer::BasicHistory;
67
use rusqlite::types::ValueRef;
78
use tokio_stream::{Stream, StreamExt as _};
8-
use tonic::metadata::BinaryMetadataValue;
9+
use tonic::metadata::{AsciiMetadataValue, BinaryMetadataValue};
910

1011
use crate::connection::Connection as _;
1112
use crate::database::Connection;
@@ -139,11 +140,12 @@ impl AdminShellService for AdminShell {
139140

140141
pub struct AdminShellClient {
141142
remote_url: String,
143+
auth: Option<String>,
142144
}
143145

144146
impl AdminShellClient {
145-
pub fn new(remote_url: String) -> Self {
146-
Self { remote_url }
147+
pub fn new(remote_url: String, auth: Option<String>) -> Self {
148+
Self { remote_url, auth }
147149
}
148150

149151
pub async fn run_namespace(&self, namespace: &str) -> anyhow::Result<()> {
@@ -160,6 +162,14 @@ impl AdminShellClient {
160162
"x-namespace-bin",
161163
BinaryMetadataValue::from_bytes(namespace.as_slice()),
162164
);
165+
166+
if let Some(ref auth) = self.auth {
167+
req.metadata_mut().insert(
168+
"authorization",
169+
AsciiMetadataValue::from_str(&format!("basic {auth}")).unwrap(),
170+
);
171+
}
172+
163173
let mut resp_stream = client.shell(req).await?.into_inner();
164174

165175
let mut history = BasicHistory::new();

libsql-server/src/http/admin/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,17 @@ where
181181
.level(tracing::Level::DEBUG)
182182
.latency_unit(tower_http::LatencyUnit::Micros),
183183
),
184-
)
185-
.layer(axum::middleware::from_fn_with_state(auth, auth_middleware));
184+
);
186185

187186
let admin_shell = crate::admin_shell::make_svc(namespaces.clone());
188187
let grpc_router = tonic::transport::Server::builder()
189188
.accept_http1(true)
190189
.add_service(tonic_web::enable(admin_shell))
191190
.into_router();
192191

193-
let router = router.merge(grpc_router);
192+
let router = router
193+
.merge(grpc_router)
194+
.layer(axum::middleware::from_fn_with_state(auth, auth_middleware));
194195

195196
hyper::server::Server::builder(acceptor)
196197
.serve(router.into_make_service())

libsql-server/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ enum UtilsSubcommands {
313313
admin_api_url: String,
314314
#[clap(long)]
315315
namespace: Option<String>,
316+
#[clap(long)]
317+
auth: Option<String>,
316318
},
317319
}
318320

@@ -719,9 +721,12 @@ async fn main() -> Result<()> {
719721
UtilsSubcommands::AdminShell {
720722
admin_api_url,
721723
namespace,
724+
auth,
722725
} => {
723-
let client =
724-
libsql_server::admin_shell::AdminShellClient::new(admin_api_url.clone());
726+
let client = libsql_server::admin_shell::AdminShellClient::new(
727+
admin_api_url.clone(),
728+
auth.clone(),
729+
);
725730
if let Some(ns) = namespace {
726731
client.run_namespace(ns).await?;
727732
}

0 commit comments

Comments
 (0)