update
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -275,6 +276,7 @@ func (app *App) serverRoute(writer http.ResponseWriter, request *http.Request, u
|
||||
"listeners": "/v1/network/listeners", "connections": "/v1/network/connections",
|
||||
"firewall": "/v1/firewall/rules", "fail2ban": "/v1/fail2ban/status",
|
||||
"wireguard": "/v1/wireguard/status", "services": "/v1/services",
|
||||
"rate-limits": "/v1/rate-limits",
|
||||
}
|
||||
if len(parts) == 2 && request.Method == http.MethodGet {
|
||||
if parts[1] == "metrics" {
|
||||
@@ -295,6 +297,25 @@ func (app *App) serverRoute(writer http.ResponseWriter, request *http.Request, u
|
||||
writeJSON(writer, 200, entries)
|
||||
return
|
||||
}
|
||||
if parts[1] == "connection-history" {
|
||||
hours := queryHours(request, 24, 24*30)
|
||||
history, historyErr := app.store.ConnectionHistory(request.Context(), server.ID, time.Now().Add(-time.Duration(hours)*time.Hour))
|
||||
if historyErr != nil {
|
||||
writeInternal(writer, historyErr)
|
||||
return
|
||||
}
|
||||
writeJSON(writer, 200, history)
|
||||
return
|
||||
}
|
||||
if parts[1] == "fail2ban-history" {
|
||||
events, eventErr := app.store.Fail2BanEvents(request.Context(), server.ID, queryLimit(request, 200, 1000))
|
||||
if eventErr != nil {
|
||||
writeInternal(writer, eventErr)
|
||||
return
|
||||
}
|
||||
writeJSON(writer, 200, events)
|
||||
return
|
||||
}
|
||||
if agentPath, found := readRoutes[parts[1]]; found {
|
||||
app.proxyRead(writer, request, server, agentPath)
|
||||
return
|
||||
@@ -311,11 +332,51 @@ func (app *App) serverRoute(writer http.ResponseWriter, request *http.Request, u
|
||||
app.fail2banMutation(writer, request, user, server, parts[2])
|
||||
return
|
||||
}
|
||||
if len(parts) == 4 && request.Method == http.MethodPost && parts[1] == "services" && parts[3] == "restart" && validServiceName(parts[2]) {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleOperator) {
|
||||
if len(parts) == 3 && request.Method == http.MethodPost && parts[1] == "fail2ban" && parts[2] == "configure" {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleAdministrator) {
|
||||
return
|
||||
}
|
||||
app.serviceMutation(writer, request, user, server, parts[2])
|
||||
app.fail2banConfigure(writer, request, user, server)
|
||||
return
|
||||
}
|
||||
if len(parts) == 4 && request.Method == http.MethodPost && parts[1] == "services" && validServiceName(parts[2]) {
|
||||
operation := parts[3]
|
||||
requiredRole := RoleOperator
|
||||
if operation == "install" {
|
||||
requiredRole = RoleAdministrator
|
||||
}
|
||||
if !app.authorize(writer, request, user, csrfHash, requiredRole) {
|
||||
return
|
||||
}
|
||||
app.serviceMutation(writer, request, user, server, parts[2], operation)
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[1] == "firewall" && parts[2] == "rules" && request.Method == http.MethodPost {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleAdministrator) {
|
||||
return
|
||||
}
|
||||
app.firewallAdd(writer, request, user, server)
|
||||
return
|
||||
}
|
||||
if len(parts) == 4 && parts[1] == "firewall" && parts[2] == "rules" && request.Method == http.MethodDelete {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleAdministrator) {
|
||||
return
|
||||
}
|
||||
app.managedDelete(writer, request, user, server, "firewall", parts[3])
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "rate-limits" && request.Method == http.MethodPost {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleAdministrator) {
|
||||
return
|
||||
}
|
||||
app.rateLimitAdd(writer, request, user, server)
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[1] == "rate-limits" && request.Method == http.MethodDelete {
|
||||
if !app.authorize(writer, request, user, csrfHash, RoleAdministrator) {
|
||||
return
|
||||
}
|
||||
app.managedDelete(writer, request, user, server, "rate-limits", parts[2])
|
||||
return
|
||||
}
|
||||
writeError(writer, 404, "not found", "not_found")
|
||||
@@ -352,18 +413,25 @@ func (app *App) fail2banMutation(writer http.ResponseWriter, request *http.Reque
|
||||
writeError(writer, 502, "agent unavailable", "agent_unavailable")
|
||||
return
|
||||
}
|
||||
if status < 300 {
|
||||
app.store.AddFail2BanEvent(request.Context(), server.ID, input.Jail, input.IP, action, "operator")
|
||||
}
|
||||
writeAgentResponse(writer, status, response)
|
||||
}
|
||||
|
||||
func (app *App) serviceMutation(writer http.ResponseWriter, request *http.Request, user User, server Server, name string) {
|
||||
func (app *App) serviceMutation(writer http.ResponseWriter, request *http.Request, user User, server Server, name, operation string) {
|
||||
if !contains([]string{"start", "stop", "restart", "enable", "disable", "install"}, operation) {
|
||||
writeError(writer, 400, "invalid service operation", "invalid_request")
|
||||
return
|
||||
}
|
||||
status, response, err := app.agent.Do(request.Context(), server, http.MethodPost,
|
||||
"/v1/services/"+url.PathEscape(name)+"/restart", []byte("{}"))
|
||||
"/v1/services/"+url.PathEscape(name)+"/"+operation, []byte("{}"))
|
||||
result := "success"
|
||||
if err != nil || status >= 300 {
|
||||
result = "failure"
|
||||
}
|
||||
app.store.Audit(request.Context(), &user, server.ID, "service.restart", name, nil,
|
||||
map[string]string{"requested": "restart"}, result, app.clientIP(request))
|
||||
app.store.Audit(request.Context(), &user, server.ID, "service."+operation, name, nil,
|
||||
map[string]string{"requested": operation}, result, app.clientIP(request))
|
||||
if err != nil {
|
||||
writeError(writer, 502, "agent unavailable", "agent_unavailable")
|
||||
return
|
||||
@@ -371,6 +439,141 @@ func (app *App) serviceMutation(writer http.ResponseWriter, request *http.Reques
|
||||
writeAgentResponse(writer, status, response)
|
||||
}
|
||||
|
||||
func (app *App) firewallAdd(writer http.ResponseWriter, request *http.Request, user User, server Server) {
|
||||
var input struct {
|
||||
Interface string `json:"interface"`
|
||||
Source string `json:"source"`
|
||||
Protocol string `json:"protocol"`
|
||||
Port int `json:"port"`
|
||||
Target string `json:"target"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
if decodeJSON(writer, request, &input) != nil || !validInterface(input.Interface) || !validIPv4CIDR(input.Source) || !contains([]string{"tcp", "udp"}, input.Protocol) || input.Port < 1 || input.Port > 65535 || !contains([]string{"ACCEPT", "DROP", "REJECT"}, input.Target) || !validComment(input.Comment) {
|
||||
writeError(writer, 400, "invalid firewall rule", "invalid_request")
|
||||
return
|
||||
}
|
||||
app.forwardMutation(writer, request, user, server, "firewall.add", fmt.Sprintf("%s/%s:%d", input.Source, input.Protocol, input.Port), "/v1/firewall/rules", http.MethodPost, input)
|
||||
}
|
||||
|
||||
func (app *App) rateLimitAdd(writer http.ResponseWriter, request *http.Request, user User, server Server) {
|
||||
var input struct {
|
||||
Interface string `json:"interface"`
|
||||
Source string `json:"source"`
|
||||
Protocol string `json:"protocol"`
|
||||
Port int `json:"port"`
|
||||
Rate int `json:"rate_per_second"`
|
||||
Burst int `json:"burst"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
if decodeJSON(writer, request, &input) != nil || !validInterface(input.Interface) || !validIPv4CIDR(input.Source) || !contains([]string{"tcp", "udp"}, input.Protocol) || input.Port < 1 || input.Port > 65535 || input.Rate < 1 || input.Rate > 100000 || input.Burst < 1 || input.Burst > 10000 || !validComment(input.Comment) {
|
||||
writeError(writer, 400, "invalid rate-limit policy", "invalid_request")
|
||||
return
|
||||
}
|
||||
app.forwardMutation(writer, request, user, server, "rate_limit.add", fmt.Sprintf("%s/%s:%d", input.Source, input.Protocol, input.Port), "/v1/rate-limits", http.MethodPost, input)
|
||||
}
|
||||
|
||||
func (app *App) managedDelete(writer http.ResponseWriter, request *http.Request, user User, server Server, kind, id string) {
|
||||
if len(id) != 16 {
|
||||
writeError(writer, 400, "invalid managed object id", "invalid_request")
|
||||
return
|
||||
}
|
||||
for _, character := range id {
|
||||
if !((character >= '0' && character <= '9') || (character >= 'a' && character <= 'f') || (character >= 'A' && character <= 'F')) {
|
||||
writeError(writer, 400, "invalid managed object id", "invalid_request")
|
||||
return
|
||||
}
|
||||
}
|
||||
path := "/v1/firewall/rules/" + id
|
||||
action := "firewall.delete"
|
||||
if kind == "rate-limits" {
|
||||
path = "/v1/rate-limits/" + id
|
||||
action = "rate_limit.delete"
|
||||
}
|
||||
app.forwardMutation(writer, request, user, server, action, id, path, http.MethodDelete, map[string]string{"id": id})
|
||||
}
|
||||
|
||||
func (app *App) fail2banConfigure(writer http.ResponseWriter, request *http.Request, user User, server Server) {
|
||||
var input struct {
|
||||
Jail string `json:"jail"`
|
||||
Enabled bool `json:"enabled"`
|
||||
BanTime int `json:"bantime_seconds"`
|
||||
FindTime int `json:"findtime_seconds"`
|
||||
MaxRetry int `json:"maxretry"`
|
||||
}
|
||||
if decodeJSON(writer, request, &input) != nil || !validSimpleName(input.Jail) || input.BanTime < 10 || input.BanTime > 2592000 || input.FindTime < 10 || input.FindTime > 86400 || input.MaxRetry < 1 || input.MaxRetry > 100 {
|
||||
writeError(writer, 400, "invalid Fail2Ban settings", "invalid_request")
|
||||
return
|
||||
}
|
||||
app.forwardMutation(writer, request, user, server, "fail2ban.configure", input.Jail, "/v1/fail2ban/configure", http.MethodPost, input)
|
||||
}
|
||||
|
||||
func (app *App) forwardMutation(writer http.ResponseWriter, request *http.Request, user User, server Server, action, target, path, method string, input any) {
|
||||
body, _ := json.Marshal(input)
|
||||
status, response, err := app.agent.Do(request.Context(), server, method, path, body)
|
||||
if err == nil && status < 300 && (strings.HasPrefix(action, "firewall.") || strings.HasPrefix(action, "rate_limit.")) {
|
||||
var pending struct {
|
||||
TransactionID string `json:"transaction_id"`
|
||||
}
|
||||
if json.Unmarshal(response, &pending) != nil || len(pending.TransactionID) != 16 {
|
||||
err = errors.New("agent returned invalid firewall transaction")
|
||||
} else {
|
||||
confirmPath := "/v1/firewall/transactions/" + pending.TransactionID + "/confirm"
|
||||
confirmStatus, confirmResponse, confirmErr := app.agent.Do(request.Context(), server, http.MethodPost, confirmPath, []byte("{}"))
|
||||
if confirmErr != nil || confirmStatus >= 300 {
|
||||
err = errors.New("firewall confirmation failed; safety rollback remains armed")
|
||||
} else {
|
||||
status = confirmStatus
|
||||
response = confirmResponse
|
||||
}
|
||||
}
|
||||
}
|
||||
result := "success"
|
||||
if err != nil || status >= 300 {
|
||||
result = "failure"
|
||||
}
|
||||
app.store.Audit(request.Context(), &user, server.ID, action, target, nil, input, result, app.clientIP(request))
|
||||
if err != nil {
|
||||
writeError(writer, 502, "agent unavailable", "agent_unavailable")
|
||||
return
|
||||
}
|
||||
writeAgentResponse(writer, status, response)
|
||||
}
|
||||
|
||||
func contains(values []string, wanted string) bool {
|
||||
for _, value := range values {
|
||||
if value == wanted {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func validInterface(value string) bool {
|
||||
if len(value) < 1 || len(value) > 15 {
|
||||
return false
|
||||
}
|
||||
for _, c := range value {
|
||||
if !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && !strings.ContainsRune("_.-", c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func validIPv4CIDR(value string) bool {
|
||||
ip, _, err := net.ParseCIDR(value)
|
||||
return err == nil && ip.To4() != nil
|
||||
}
|
||||
func validComment(value string) bool {
|
||||
if len(value) > 80 {
|
||||
return false
|
||||
}
|
||||
for _, c := range value {
|
||||
if !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && !strings.ContainsRune(" ._-", c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (app *App) clientIP(request *http.Request) string {
|
||||
if app.config.TrustProxy {
|
||||
if value := request.Header.Get("X-Real-IP"); net.ParseIP(value) != nil {
|
||||
@@ -434,6 +637,17 @@ func queryLimit(request *http.Request, fallback, maximum int) int {
|
||||
return value
|
||||
}
|
||||
|
||||
func queryHours(request *http.Request, fallback, maximum int) int {
|
||||
value, err := strconv.Atoi(request.URL.Query().Get("hours"))
|
||||
if err != nil || value < 1 {
|
||||
return fallback
|
||||
}
|
||||
if value > maximum {
|
||||
return maximum
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func writeAgentResponse(writer http.ResponseWriter, status int, payload []byte) {
|
||||
if status < 100 || status > 599 {
|
||||
status = http.StatusBadGateway
|
||||
|
||||
Reference in New Issue
Block a user