feat(server): authorized requests

repo-db
Jef Roosens 2023-08-03 09:34:33 +02:00
parent bc19158747
commit a7e0c03b58
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 29 additions and 7 deletions

13
Cargo.lock generated
View File

@ -243,6 +243,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.21.2" version = "0.21.2"
@ -1762,7 +1768,7 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2"
dependencies = [ dependencies = [
"base64", "base64 0.21.2",
] ]
[[package]] [[package]]
@ -2255,7 +2261,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ca69bf415b93b60b80dc8fda3cb4ef52b2336614d8da2de5456cc942a110482" checksum = "8ca69bf415b93b60b80dc8fda3cb4ef52b2336614d8da2de5456cc942a110482"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64", "base64 0.21.2",
"bigdecimal", "bigdecimal",
"bitflags 2.3.3", "bitflags 2.3.3",
"byteorder", "byteorder",
@ -2302,7 +2308,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0db2df1b8731c3651e204629dd55e52adbae0462fa1bdcbed56a2302c18181e" checksum = "a0db2df1b8731c3651e204629dd55e52adbae0462fa1bdcbed56a2302c18181e"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64", "base64 0.21.2",
"bigdecimal", "bigdecimal",
"bitflags 2.3.3", "bitflags 2.3.3",
"byteorder", "byteorder",
@ -2616,6 +2622,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8bd22a874a2d0b70452d5597b12c537331d49060824a95f49f108994f94aa4c" checksum = "a8bd22a874a2d0b70452d5597b12c537331d49060824a95f49f108994f94aa4c"
dependencies = [ dependencies = [
"base64 0.20.0",
"bitflags 2.3.3", "bitflags 2.3.3",
"bytes", "bytes",
"futures-core", "futures-core",

View File

@ -18,7 +18,7 @@ sha256 = "1.1.4"
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }
tokio-util = { version = "0.7.8", features = ["io"] } tokio-util = { version = "0.7.8", features = ["io"] }
tower = { version = "0.4.13", features = ["make"] } tower = { version = "0.4.13", features = ["make"] }
tower-http = { version = "0.4.1", features = ["fs", "trace"] } tower-http = { version = "0.4.1", features = ["fs", "trace", "auth"] }
tracing = "0.1.37" tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
uuid = { version = "1.4.0", features = ["v4"] } uuid = { version = "1.4.0", features = ["v4"] }

View File

@ -17,15 +17,28 @@ use std::sync::Arc;
use tokio::{fs, io::AsyncWriteExt}; use tokio::{fs, io::AsyncWriteExt};
use tower::util::ServiceExt; use tower::util::ServiceExt;
use tower_http::services::{ServeDir, ServeFile}; use tower_http::services::{ServeDir, ServeFile};
use tower_http::validate_request::ValidateRequestHeaderLayer;
use uuid::Uuid; use uuid::Uuid;
pub fn router() -> Router<crate::Global> { pub fn router() -> Router<crate::Global> {
Router::new() Router::new()
.route("/:repo", post(post_package_archive).delete(delete_repo)) .route(
.route("/:repo/:arch", delete(delete_arch_repo)) "/:repo",
post(post_package_archive)
.delete(delete_repo)
.layer(ValidateRequestHeaderLayer::bearer("test")),
)
.route(
"/:repo/:arch",
delete(delete_arch_repo).layer(ValidateRequestHeaderLayer::bearer("test")),
)
// Routes added after the layer do not get that layer applied, so the GET requests will not
// be authorized
.route( .route(
"/:repo/:arch/:filename", "/:repo/:arch/:filename",
delete(delete_package).get(get_file), delete(delete_package)
.layer(ValidateRequestHeaderLayer::bearer("test"))
.get(get_file),
) )
} }
@ -57,6 +70,8 @@ async fn post_package_archive(
match res { match res {
// Insert the newly added package into the database // Insert the newly added package into the database
Ok(pkg) => { Ok(pkg) => {
tracing::info!("Added '{}' to repository '{}'", pkg.file_name(), repo);
// Query the repo for its ID, or create it if it does not already exist // Query the repo for its ID, or create it if it does not already exist
let repo_entity = db_repo::Entity::find() let repo_entity = db_repo::Entity::find()
.filter(db_repo::Column::Name.eq(&repo)) .filter(db_repo::Column::Name.eq(&repo))