584 lines
18 KiB
Rust
584 lines
18 KiB
Rust
use crate::{
|
|
config::Config,
|
|
fail2ban::{self, BanRequest},
|
|
firewall, logs, network, services, system, wireguard,
|
|
};
|
|
use anyhow::{Context, Result};
|
|
use axum::{
|
|
body::Body,
|
|
extract::{DefaultBodyLimit, Path, Query, State},
|
|
http::{header::AUTHORIZATION, Method, Request, StatusCode},
|
|
middleware::{self, Next},
|
|
response::{IntoResponse, Response},
|
|
routing::{delete, get, post},
|
|
Json, Router,
|
|
};
|
|
use serde_json::{json, Value};
|
|
use std::{
|
|
fs,
|
|
path::Path as FilePath,
|
|
sync::{Arc, Mutex},
|
|
time::{Duration, Instant},
|
|
};
|
|
use subtle::ConstantTimeEq;
|
|
use tokio::sync::Semaphore;
|
|
|
|
pub struct AppState {
|
|
pub config: Config,
|
|
token: Vec<u8>,
|
|
read_limiter: Mutex<TokenBucket>,
|
|
write_limiter: Mutex<TokenBucket>,
|
|
last_auth_warning: Mutex<Instant>,
|
|
concurrency: Arc<Semaphore>,
|
|
cpu: Mutex<Option<system::CpuTimes>>,
|
|
}
|
|
|
|
impl AppState {
|
|
pub fn load(config: Config) -> Result<Arc<Self>> {
|
|
let token_path = FilePath::new(&config.auth.token_file);
|
|
let token = fs::read(token_path)
|
|
.with_context(|| format!("read token file {}", token_path.display()))?;
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let mode = fs::metadata(token_path)?.permissions().mode();
|
|
if mode & 0o077 != 0 {
|
|
anyhow::bail!("token file must not be accessible by group or others")
|
|
}
|
|
}
|
|
let token = trim_ascii(&token).to_vec();
|
|
if token.len() < 32 {
|
|
anyhow::bail!("agent token must contain at least 32 bytes")
|
|
}
|
|
Ok(Self::new(config, token))
|
|
}
|
|
|
|
fn new(config: Config, token: Vec<u8>) -> Arc<Self> {
|
|
Arc::new(Self {
|
|
read_limiter: Mutex::new(TokenBucket::new(
|
|
config.limits.read_requests_per_second,
|
|
config.limits.burst,
|
|
)),
|
|
write_limiter: Mutex::new(TokenBucket::new(
|
|
config.limits.write_requests_per_second,
|
|
config.limits.burst,
|
|
)),
|
|
last_auth_warning: Mutex::new(Instant::now() - Duration::from_secs(60)),
|
|
concurrency: Arc::new(Semaphore::new(config.limits.max_concurrent_requests)),
|
|
config,
|
|
token,
|
|
cpu: Mutex::new(None),
|
|
})
|
|
}
|
|
}
|
|
|
|
struct TokenBucket {
|
|
rate: f64,
|
|
capacity: f64,
|
|
tokens: f64,
|
|
updated_at: Instant,
|
|
}
|
|
|
|
impl TokenBucket {
|
|
fn new(rate: f64, capacity: f64) -> Self {
|
|
Self {
|
|
rate,
|
|
capacity,
|
|
tokens: capacity,
|
|
updated_at: Instant::now(),
|
|
}
|
|
}
|
|
|
|
fn allow(&mut self, now: Instant) -> bool {
|
|
let elapsed = now.duration_since(self.updated_at).as_secs_f64();
|
|
self.tokens = (self.tokens + elapsed * self.rate).min(self.capacity);
|
|
self.updated_at = now;
|
|
if self.tokens >= 1.0 {
|
|
self.tokens -= 1.0;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn router(state: Arc<AppState>) -> Router {
|
|
let max_body = state.config.limits.max_body_bytes;
|
|
Router::new()
|
|
.route("/healthz", get(health))
|
|
.route("/v1/version", get(version))
|
|
.route("/v1/system/status", get(system_status))
|
|
.route("/v1/network/interfaces", get(network_interfaces))
|
|
.route("/v1/network/routes", get(network_routes))
|
|
.route("/v1/network/listeners", get(network_listeners))
|
|
.route("/v1/network/connections", get(network_connections))
|
|
.route("/v1/firewall/rules", get(firewall_rules))
|
|
.route("/v1/firewall/rules", post(firewall_add))
|
|
.route("/v1/firewall/rules/:id", delete(firewall_delete))
|
|
.route(
|
|
"/v1/firewall/transactions/:id/confirm",
|
|
post(firewall_confirm),
|
|
)
|
|
.route("/v1/rate-limits", get(rate_limits).post(rate_limit_add))
|
|
.route("/v1/rate-limits/:id", delete(rate_limit_delete))
|
|
.route("/v1/fail2ban/status", get(fail2ban_status))
|
|
.route("/v1/fail2ban/jails/:jail", get(fail2ban_jail))
|
|
.route("/v1/fail2ban/ban", post(fail2ban_ban))
|
|
.route("/v1/fail2ban/unban", post(fail2ban_unban))
|
|
.route("/v1/fail2ban/configure", post(fail2ban_configure))
|
|
.route("/v1/wireguard/status", get(wireguard_status))
|
|
.route("/v1/services", get(service_list))
|
|
.route("/v1/services/:name/:operation", post(service_mutation))
|
|
.route("/v1/logs", get(log_entries))
|
|
.layer(DefaultBodyLimit::max(max_body))
|
|
.layer(middleware::from_fn_with_state(
|
|
state.clone(),
|
|
security_middleware,
|
|
))
|
|
.with_state(state)
|
|
}
|
|
|
|
async fn security_middleware(
|
|
State(state): State<Arc<AppState>>,
|
|
request: Request<Body>,
|
|
next: Next,
|
|
) -> Response {
|
|
let is_health = request.uri().path() == "/healthz";
|
|
|
|
let header_bytes: usize = request
|
|
.headers()
|
|
.iter()
|
|
.map(|(name, value)| name.as_str().len().saturating_add(value.as_bytes().len()))
|
|
.sum();
|
|
if header_bytes > 16 * 1024 {
|
|
return error_response(
|
|
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
|
|
"request headers are too large",
|
|
"headers_too_large",
|
|
);
|
|
}
|
|
|
|
let is_read = matches!(*request.method(), Method::GET | Method::HEAD);
|
|
let allowed = if is_read {
|
|
state
|
|
.read_limiter
|
|
.lock()
|
|
.expect("rate mutex poisoned")
|
|
.allow(Instant::now())
|
|
} else {
|
|
state
|
|
.write_limiter
|
|
.lock()
|
|
.expect("rate mutex poisoned")
|
|
.allow(Instant::now())
|
|
};
|
|
if !allowed {
|
|
return error_response(
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
"request rate exceeded",
|
|
"rate_limited",
|
|
);
|
|
}
|
|
if is_health {
|
|
return next.run(request).await;
|
|
}
|
|
|
|
let authenticated = request
|
|
.headers()
|
|
.get(AUTHORIZATION)
|
|
.and_then(|value| value.to_str().ok())
|
|
.and_then(|value| value.strip_prefix("Bearer "))
|
|
.is_some_and(|provided| constant_time_eq(provided.as_bytes(), &state.token));
|
|
if !authenticated {
|
|
let mut last_warning = state
|
|
.last_auth_warning
|
|
.lock()
|
|
.expect("authentication warning mutex poisoned");
|
|
if last_warning.elapsed() >= Duration::from_secs(10) {
|
|
tracing::warn!("agent authentication failed (repeated failures are coalesced)");
|
|
*last_warning = Instant::now();
|
|
}
|
|
return error_response(
|
|
StatusCode::UNAUTHORIZED,
|
|
"authentication required",
|
|
"unauthorized",
|
|
);
|
|
}
|
|
|
|
let Ok(_permit) = state.concurrency.clone().try_acquire_owned() else {
|
|
return error_response(
|
|
StatusCode::SERVICE_UNAVAILABLE,
|
|
"request concurrency exceeded",
|
|
"busy",
|
|
);
|
|
};
|
|
match tokio::time::timeout(
|
|
Duration::from_secs(state.config.limits.request_timeout_seconds),
|
|
next.run(request),
|
|
)
|
|
.await
|
|
{
|
|
Ok(response) => response,
|
|
Err(_) => error_response(StatusCode::GATEWAY_TIMEOUT, "request timed out", "timeout"),
|
|
}
|
|
}
|
|
|
|
fn constant_time_eq(provided: &[u8], expected: &[u8]) -> bool {
|
|
provided.len() == expected.len() && bool::from(provided.ct_eq(expected))
|
|
}
|
|
|
|
fn trim_ascii(mut value: &[u8]) -> &[u8] {
|
|
while value.first().is_some_and(u8::is_ascii_whitespace) {
|
|
value = &value[1..];
|
|
}
|
|
while value.last().is_some_and(u8::is_ascii_whitespace) {
|
|
value = &value[..value.len() - 1];
|
|
}
|
|
value
|
|
}
|
|
|
|
async fn health() -> Json<Value> {
|
|
Json(json!({"status": "ok"}))
|
|
}
|
|
|
|
async fn version() -> Json<Value> {
|
|
Json(json!({"version": env!("CARGO_PKG_VERSION"), "api_version": "v1"}))
|
|
}
|
|
|
|
async fn system_status(
|
|
State(state): State<Arc<AppState>>,
|
|
) -> Result<Json<system::Status>, ApiError> {
|
|
let state_for_work = state.clone();
|
|
let status = tokio::task::spawn_blocking(move || system::collect(&state_for_work.cpu))
|
|
.await
|
|
.map_err(|_| ApiError::internal())??;
|
|
Ok(Json(status))
|
|
}
|
|
|
|
async fn network_interfaces(State(state): State<Arc<AppState>>) -> Json<Vec<network::Interface>> {
|
|
Json(network::interfaces(command_timeout(&state)).await)
|
|
}
|
|
|
|
async fn network_routes(State(state): State<Arc<AppState>>) -> Result<Json<Value>, ApiError> {
|
|
Ok(Json(network::routes(command_timeout(&state)).await?))
|
|
}
|
|
|
|
async fn network_listeners(
|
|
State(state): State<Arc<AppState>>,
|
|
) -> Result<Json<Vec<network::SocketInfo>>, ApiError> {
|
|
let include_process = state.config.telemetry.process_info_enabled;
|
|
let rows = tokio::task::spawn_blocking(move || network::sockets(include_process))
|
|
.await
|
|
.map_err(|_| ApiError::internal())??;
|
|
Ok(Json(network::listeners(rows)))
|
|
}
|
|
|
|
async fn network_connections(
|
|
State(state): State<Arc<AppState>>,
|
|
) -> Result<Json<Vec<network::SocketInfo>>, ApiError> {
|
|
if !state.config.telemetry.connections_enabled {
|
|
return Err(ApiError::new(
|
|
StatusCode::FORBIDDEN,
|
|
"connection telemetry disabled",
|
|
"disabled",
|
|
));
|
|
}
|
|
let include_process = state.config.telemetry.process_info_enabled;
|
|
let rows = tokio::task::spawn_blocking(move || network::sockets(include_process))
|
|
.await
|
|
.map_err(|_| ApiError::internal())??;
|
|
Ok(Json(
|
|
rows.into_iter()
|
|
.filter(|row| row.state != "LISTEN")
|
|
.collect(),
|
|
))
|
|
}
|
|
|
|
async fn firewall_rules(State(state): State<Arc<AppState>>) -> Json<firewall::FirewallStatus> {
|
|
Json(firewall::status(&state.config, command_timeout(&state)).await)
|
|
}
|
|
|
|
async fn firewall_add(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(request): Json<firewall::AddRuleRequest>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let transaction_id = firewall::add(&state.config, request, command_timeout(&state)).await?;
|
|
tracing::info!("managed firewall rule added");
|
|
Ok(Json(
|
|
json!({"status":"pending_confirmation","transaction_id":transaction_id}),
|
|
))
|
|
}
|
|
|
|
async fn firewall_delete(
|
|
State(state): State<Arc<AppState>>,
|
|
Path(id): Path<String>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let transaction_id = firewall::delete(&state.config, &id, command_timeout(&state)).await?;
|
|
tracing::info!(rule_id = id, "managed firewall rule deleted");
|
|
Ok(Json(
|
|
json!({"status":"pending_confirmation","transaction_id":transaction_id}),
|
|
))
|
|
}
|
|
|
|
async fn rate_limits(State(state): State<Arc<AppState>>) -> Json<Vec<firewall::FirewallRule>> {
|
|
let mut rules = firewall::status(&state.config, command_timeout(&state))
|
|
.await
|
|
.rules;
|
|
rules.retain(|rule| rule.rate_limit && rule.managed);
|
|
Json(rules)
|
|
}
|
|
|
|
async fn rate_limit_add(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(request): Json<firewall::RateLimitRequest>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let transaction_id =
|
|
firewall::add_rate_limit(&state.config, request, command_timeout(&state)).await?;
|
|
tracing::info!("managed L4 rate-limit policy added");
|
|
Ok(Json(
|
|
json!({"status":"pending_confirmation","transaction_id":transaction_id}),
|
|
))
|
|
}
|
|
|
|
async fn rate_limit_delete(
|
|
State(state): State<Arc<AppState>>,
|
|
Path(id): Path<String>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
let transaction_id =
|
|
firewall::delete_rate_limit(&state.config, &id, command_timeout(&state)).await?;
|
|
tracing::info!(policy_id = id, "managed L4 rate-limit policy deleted");
|
|
Ok(Json(
|
|
json!({"status":"pending_confirmation","transaction_id":transaction_id}),
|
|
))
|
|
}
|
|
|
|
async fn firewall_confirm(
|
|
State(state): State<Arc<AppState>>,
|
|
Path(id): Path<String>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
firewall::confirm(&id, command_timeout(&state)).await?;
|
|
tracing::info!(transaction_id = id, "firewall transaction confirmed");
|
|
Ok(Json(json!({"status":"ok"})))
|
|
}
|
|
|
|
async fn fail2ban_status(State(state): State<Arc<AppState>>) -> Json<fail2ban::Status> {
|
|
Json(fail2ban::status(&state.config, command_timeout(&state)).await)
|
|
}
|
|
|
|
async fn fail2ban_jail(
|
|
State(state): State<Arc<AppState>>,
|
|
Path(jail): Path<String>,
|
|
) -> Result<Json<fail2ban::JailStatus>, ApiError> {
|
|
fail2ban::validate_jail(&jail).map_err(|_| ApiError::bad_request("invalid jail name"))?;
|
|
Ok(Json(fail2ban::jail(&jail, command_timeout(&state)).await?))
|
|
}
|
|
|
|
async fn fail2ban_ban(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(request): Json<BanRequest>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
mutate_ban(&state, request, false).await
|
|
}
|
|
|
|
async fn fail2ban_unban(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(request): Json<BanRequest>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
mutate_ban(&state, request, true).await
|
|
}
|
|
|
|
async fn mutate_ban(
|
|
state: &Arc<AppState>,
|
|
request: BanRequest,
|
|
unban: bool,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
fail2ban::validate_jail(&request.jail)
|
|
.map_err(|_| ApiError::bad_request("invalid jail name"))?;
|
|
request
|
|
.ip
|
|
.parse::<std::net::IpAddr>()
|
|
.map_err(|_| ApiError::bad_request("invalid IP address"))?;
|
|
fail2ban::ban(&state.config, &request, unban, command_timeout(state)).await?;
|
|
let action = if unban { "unban" } else { "ban" };
|
|
tracing::info!(
|
|
action,
|
|
jail = request.jail,
|
|
ip = request.ip,
|
|
"privileged Fail2Ban action completed"
|
|
);
|
|
Ok(Json(json!({"status": "ok"})))
|
|
}
|
|
|
|
async fn fail2ban_configure(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(request): Json<fail2ban::ConfigureRequest>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
fail2ban::configure(&state.config, request, command_timeout(&state)).await?;
|
|
tracing::info!("managed Fail2Ban configuration updated");
|
|
Ok(Json(json!({"status":"ok"})))
|
|
}
|
|
|
|
async fn wireguard_status(
|
|
State(state): State<Arc<AppState>>,
|
|
) -> Result<Json<Vec<wireguard::Interface>>, ApiError> {
|
|
Ok(Json(wireguard::status(command_timeout(&state)).await?))
|
|
}
|
|
|
|
async fn service_list(State(state): State<Arc<AppState>>) -> Json<Vec<services::ServiceStatus>> {
|
|
Json(services::list(&state.config.services, command_timeout(&state)).await)
|
|
}
|
|
|
|
async fn service_mutation(
|
|
State(state): State<Arc<AppState>>,
|
|
Path((name, operation)): Path<(String, String)>,
|
|
) -> Result<Json<Value>, ApiError> {
|
|
if operation == "install" {
|
|
services::install(
|
|
&name,
|
|
&state.config.services,
|
|
Duration::from_secs(state.config.services.install_timeout_seconds),
|
|
)
|
|
.await?;
|
|
} else {
|
|
services::action(
|
|
&name,
|
|
&operation,
|
|
&state.config.services,
|
|
command_timeout(&state),
|
|
)
|
|
.await?;
|
|
}
|
|
tracing::info!(
|
|
service = name,
|
|
operation,
|
|
"allow-listed service mutation completed"
|
|
);
|
|
Ok(Json(json!({"status": "ok"})))
|
|
}
|
|
|
|
async fn log_entries(
|
|
State(state): State<Arc<AppState>>,
|
|
Query(query): Query<logs::LogsQuery>,
|
|
) -> Result<Json<logs::LogsResponse>, ApiError> {
|
|
if !state.config.logs.enabled {
|
|
return Err(ApiError::new(
|
|
StatusCode::FORBIDDEN,
|
|
"log collection is disabled",
|
|
"disabled",
|
|
));
|
|
}
|
|
Ok(Json(
|
|
logs::collect(&state.config.logs, query, command_timeout(&state)).await?,
|
|
))
|
|
}
|
|
|
|
fn command_timeout(state: &AppState) -> Duration {
|
|
system::command_timeout(state.config.limits.command_timeout_seconds)
|
|
}
|
|
|
|
struct ApiError {
|
|
status: StatusCode,
|
|
message: &'static str,
|
|
code: &'static str,
|
|
}
|
|
|
|
impl ApiError {
|
|
fn new(status: StatusCode, message: &'static str, code: &'static str) -> Self {
|
|
Self {
|
|
status,
|
|
message,
|
|
code,
|
|
}
|
|
}
|
|
fn bad_request(message: &'static str) -> Self {
|
|
Self::new(StatusCode::BAD_REQUEST, message, "invalid_request")
|
|
}
|
|
fn internal() -> Self {
|
|
Self::new(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"internal error",
|
|
"internal_error",
|
|
)
|
|
}
|
|
}
|
|
|
|
impl From<anyhow::Error> for ApiError {
|
|
fn from(error: anyhow::Error) -> Self {
|
|
tracing::error!(error = %error, "agent operation failed");
|
|
Self::new(
|
|
StatusCode::BAD_GATEWAY,
|
|
"host operation failed",
|
|
"host_error",
|
|
)
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
error_response(self.status, self.message, self.code)
|
|
}
|
|
}
|
|
|
|
fn error_response(status: StatusCode, message: &str, code: &str) -> Response {
|
|
(status, Json(json!({"error": message, "code": code}))).into_response()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use axum::http::Request;
|
|
use tower::ServiceExt;
|
|
|
|
#[test]
|
|
fn token_comparison_is_exact() {
|
|
assert!(constant_time_eq(
|
|
b"abcdefghijklmnopqrstuvwxyz123456",
|
|
b"abcdefghijklmnopqrstuvwxyz123456"
|
|
));
|
|
assert!(!constant_time_eq(
|
|
b"wrong",
|
|
b"abcdefghijklmnopqrstuvwxyz123456"
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn rate_limiter_refills_and_bounds() {
|
|
let start = Instant::now();
|
|
let mut limiter = TokenBucket::new(1.0, 2.0);
|
|
assert!(limiter.allow(start));
|
|
assert!(limiter.allow(start));
|
|
assert!(!limiter.allow(start));
|
|
assert!(limiter.allow(start + Duration::from_secs(1)));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn health_is_public_but_api_is_not() {
|
|
let state = AppState::new(
|
|
Config::default(),
|
|
b"abcdefghijklmnopqrstuvwxyz123456".to_vec(),
|
|
);
|
|
let app = router(state);
|
|
let health = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri("/healthz")
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(health.status(), StatusCode::OK);
|
|
let denied = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri("/v1/version")
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(denied.status(), StatusCode::UNAUTHORIZED);
|
|
}
|
|
}
|