update
This commit is contained in:
136
agent/src/api.rs
136
agent/src/api.rs
@@ -10,7 +10,7 @@ use axum::{
|
||||
http::{header::AUTHORIZATION, Method, Request, StatusCode},
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
routing::{delete, get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde_json::{json, Value};
|
||||
@@ -113,13 +113,22 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||
.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/restart", post(service_restart))
|
||||
.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(
|
||||
@@ -286,11 +295,74 @@ async fn network_connections(
|
||||
}
|
||||
|
||||
async fn firewall_rules(State(state): State<Arc<AppState>>) -> Json<firewall::FirewallStatus> {
|
||||
Json(firewall::status(command_timeout(&state)).await)
|
||||
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(command_timeout(&state)).await)
|
||||
Json(fail2ban::status(&state.config, command_timeout(&state)).await)
|
||||
}
|
||||
|
||||
async fn fail2ban_jail(
|
||||
@@ -326,7 +398,7 @@ async fn mutate_ban(
|
||||
.ip
|
||||
.parse::<std::net::IpAddr>()
|
||||
.map_err(|_| ApiError::bad_request("invalid IP address"))?;
|
||||
fail2ban::ban(&request, unban, command_timeout(state)).await?;
|
||||
fail2ban::ban(&state.config, &request, unban, command_timeout(state)).await?;
|
||||
let action = if unban { "unban" } else { "ban" };
|
||||
tracing::info!(
|
||||
action,
|
||||
@@ -337,6 +409,15 @@ async fn mutate_ban(
|
||||
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> {
|
||||
@@ -344,33 +425,34 @@ async fn wireguard_status(
|
||||
}
|
||||
|
||||
async fn service_list(State(state): State<Arc<AppState>>) -> Json<Vec<services::ServiceStatus>> {
|
||||
Json(services::list(&state.config.services.allowed, command_timeout(&state)).await)
|
||||
Json(services::list(&state.config.services, command_timeout(&state)).await)
|
||||
}
|
||||
|
||||
async fn service_restart(
|
||||
async fn service_mutation(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(name): Path<String>,
|
||||
Path((name, operation)): Path<(String, 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",
|
||||
));
|
||||
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?;
|
||||
}
|
||||
services::restart(
|
||||
&name,
|
||||
&state.config.services.allowed,
|
||||
command_timeout(&state),
|
||||
)
|
||||
.await?;
|
||||
tracing::info!(service = name, "allow-listed service restarted");
|
||||
tracing::info!(
|
||||
service = name,
|
||||
operation,
|
||||
"allow-listed service mutation completed"
|
||||
);
|
||||
Ok(Json(json!({"status": "ok"})))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user