Add initial Axum application and admin backends
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
use std::{io, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{Request, State},
|
||||
http::{HeaderMap, StatusCode, header},
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
use clusters::{Driver, ResolvedDeployment};
|
||||
use serde::Serialize;
|
||||
use subtle::ConstantTimeEq;
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
pub struct DeploymentStatus {
|
||||
deployment: String,
|
||||
config_version: Option<String>,
|
||||
storage_driver: &'static str,
|
||||
read_only: bool,
|
||||
}
|
||||
|
||||
impl DeploymentStatus {
|
||||
pub fn new(config: ResolvedDeployment, config_version: Option<String>) -> Self {
|
||||
Self {
|
||||
deployment: config.deployment,
|
||||
config_version,
|
||||
storage_driver: match config.driver {
|
||||
Driver::Sqlite => "sqlite",
|
||||
Driver::Postgres => "postgres",
|
||||
Driver::Mysql => "mysql",
|
||||
},
|
||||
read_only: config.read_only,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn router(status: DeploymentStatus, token: String) -> io::Result<Router> {
|
||||
if token.is_empty() || !token.bytes().all(|byte| byte.is_ascii_graphic()) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"BEDROCK_ADMIN_TOKEN must contain visible ASCII characters without spaces",
|
||||
));
|
||||
}
|
||||
|
||||
let admin = Router::new()
|
||||
.route("/status", get(deployment_status))
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
Arc::<str>::from(token),
|
||||
require_admin,
|
||||
));
|
||||
Ok(Router::new()
|
||||
.route("/healthz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.route("/readyz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.nest("/admin/v1", admin)
|
||||
.with_state(status))
|
||||
}
|
||||
|
||||
async fn deployment_status(State(status): State<DeploymentStatus>) -> impl IntoResponse {
|
||||
([(header::CACHE_CONTROL, "no-store")], Json(status))
|
||||
}
|
||||
|
||||
async fn require_admin(State(token): State<Arc<str>>, request: Request, next: Next) -> Response {
|
||||
if authorized(request.headers(), &token) {
|
||||
next.run(request).await
|
||||
} else {
|
||||
(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
[(header::WWW_AUTHENTICATE, "Bearer")],
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
fn authorized(headers: &HeaderMap, token: &str) -> bool {
|
||||
let mut values = headers.get_all(header::AUTHORIZATION).iter();
|
||||
let Some(value) = values.next().and_then(|value| value.to_str().ok()) else {
|
||||
return false;
|
||||
};
|
||||
if values.next().is_some() {
|
||||
return false;
|
||||
}
|
||||
let Some((scheme, supplied)) = value.split_once(' ') else {
|
||||
return false;
|
||||
};
|
||||
scheme.eq_ignore_ascii_case("Bearer") && bool::from(supplied.as_bytes().ct_eq(token.as_bytes()))
|
||||
}
|
||||
Reference in New Issue
Block a user