Skip to content

Commit

Permalink
Enable auth for admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 5, 2023
1 parent 413f38f commit a8b66db
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"json",
"ansi",
] }
tower-http = { version = "0.4.4", features = ["trace"] }
tower-http = { version = "0.4.4", features = [ "trace", "auth" ] }
uuid = { version = "0.8", features = ["v4"] }
futures = "0.3"
chrono = "0.4"
Expand Down
4 changes: 0 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ impl App {
&self,
api_token: &ApiKey,
) -> eyre::Result<bool> {
if self.config.server.disable_auth {
return Ok(true);
}

self.db
.is_api_key_valid(&api_token.relayer_id, api_token.api_key_hash())
.await
Expand Down
21 changes: 15 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ pub struct TxSitterConfig {
pub struct ServerConfig {
pub host: SocketAddr,

#[serde(default)]
pub disable_auth: bool,
pub username: Option<String>,
pub password: Option<String>,
}

impl ServerConfig {
pub fn credentials(&self) -> Option<(&str, &str)> {
let username = self.username.as_deref()?;
let password = self.password.as_deref()?;

Some((username, password))
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -102,7 +111,6 @@ mod tests {
[server]
host = "127.0.0.1:3000"
disable_auth = false
[database]
kind = "connection_string"
Expand All @@ -118,7 +126,6 @@ mod tests {
[server]
host = "127.0.0.1:3000"
disable_auth = false
[database]
kind = "parts"
Expand All @@ -140,7 +147,8 @@ mod tests {
},
server: ServerConfig {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
disable_auth: false,
username: None,
password: None,
},
database: DatabaseConfig::connection_string(
"postgres://postgres:postgres@127.0.0.1:52804/database"
Expand All @@ -162,7 +170,8 @@ mod tests {
},
server: ServerConfig {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
disable_auth: false,
username: None,
password: None,
},
database: DatabaseConfig::Parts(DbParts {
host: "host".to_string(),
Expand Down
8 changes: 7 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use axum::routing::{get, post, IntoMakeService};
use axum::Router;
use hyper::server::conn::AddrIncoming;
use thiserror::Error;
use tower_http::validate_request::ValidateRequestHeaderLayer;

use self::routes::relayer::{
create_relayer, create_relayer_api_key, get_relayer, relayer_rpc,
Expand Down Expand Up @@ -73,7 +74,7 @@ pub async fn spawn_server(
.route("/:api_token/rpc", post(relayer_rpc))
.with_state(app.clone());

let admin_routes = Router::new()
let mut admin_routes = Router::new()
.route("/relayer", post(create_relayer))
.route(
"/relayer/:relayer_id",
Expand All @@ -83,6 +84,11 @@ pub async fn spawn_server(
.route("/network/:chain_id", post(routes::network::create_network))
.with_state(app.clone());

if let Some((username, password)) = app.config.server.credentials() {
admin_routes = admin_routes
.layer(ValidateRequestHeaderLayer::basic(username, password));
}

let v1_routes = Router::new()
.nest("/api", api_routes)
.nest("/admin", admin_routes);
Expand Down
3 changes: 2 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ pub async fn setup_service(
Ipv4Addr::new(127, 0, 0, 1),
0,
)),
disable_auth: true,
username: None,
password: None,
},
database: DatabaseConfig::connection_string(db_connection_url),
keys: KeysConfig::Local(LocalKeysConfig {}),
Expand Down

0 comments on commit a8b66db

Please sign in to comment.