git init
This commit is contained in:
501
agent/src/api.rs
Normal file
501
agent/src/api.rs
Normal file
@@ -0,0 +1,501 @@
|
||||
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::{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/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/wireguard/status", get(wireguard_status))
|
||||
.route("/v1/services", get(service_list))
|
||||
.route("/v1/services/:name/restart", post(service_restart))
|
||||
.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(command_timeout(&state)).await)
|
||||
}
|
||||
|
||||
async fn fail2ban_status(State(state): State<Arc<AppState>>) -> Json<fail2ban::Status> {
|
||||
Json(fail2ban::status(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(&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 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.allowed, command_timeout(&state)).await)
|
||||
}
|
||||
|
||||
async fn service_restart(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
if !state
|
||||
.config
|
||||
.services
|
||||
.allowed
|
||||
.iter()
|
||||
.any(|item| item == &name)
|
||||
{
|
||||
return Err(ApiError::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
"service is not allow-listed",
|
||||
"not_allowed",
|
||||
));
|
||||
}
|
||||
services::restart(
|
||||
&name,
|
||||
&state.config.services.allowed,
|
||||
command_timeout(&state),
|
||||
)
|
||||
.await?;
|
||||
tracing::info!(service = name, "allow-listed service restarted");
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user