You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.2 KiB
Go
159 lines
5.2 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"git.luoli.live/root/virt/agent/auth"
|
|
"git.luoli.live/root/virt/agent/hypervisor"
|
|
"git.luoli.live/root/virt/agent/networkobserve"
|
|
)
|
|
|
|
var networkObservationGenerationPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
|
|
|
// NetworkObservationReader is the bounded, read-only surface exposed by the
|
|
// in-memory collector. It deliberately offers no firewall, lifecycle, billing,
|
|
// or other mutation method.
|
|
type NetworkObservationReader interface {
|
|
Generation() string
|
|
Capability() networkobserve.Capability
|
|
Batch(after uint64, limit int) (networkobserve.Batch, error)
|
|
}
|
|
|
|
// SetNetworkObserver installs the process-local collector before the Agent
|
|
// begins admitting requests. Observation is optional and never gates ordinary
|
|
// VM scheduling or mutation endpoints.
|
|
func (s *Server) SetNetworkObserver(observer NetworkObservationReader) error {
|
|
if observer == nil || !networkObservationGenerationPattern.MatchString(observer.Generation()) {
|
|
return errors.New("valid network observation reader is required")
|
|
}
|
|
if err := observer.Capability().Validate(); err != nil {
|
|
return err
|
|
}
|
|
s.networkObserverMu.Lock()
|
|
defer s.networkObserverMu.Unlock()
|
|
if s.networkObserver != nil {
|
|
return errors.New("network observation reader is already configured")
|
|
}
|
|
s.networkObserver = observer
|
|
return nil
|
|
}
|
|
|
|
type capabilitiesResponse struct {
|
|
hypervisor.Capabilities
|
|
NetworkAbuseObservation *networkobserve.Capability `json:"network_abuse_observation,omitempty"`
|
|
}
|
|
|
|
func (s *Server) capabilitiesResponse(base hypervisor.Capabilities) capabilitiesResponse {
|
|
result := capabilitiesResponse{Capabilities: base}
|
|
s.networkObserverMu.RLock()
|
|
observer := s.networkObserver
|
|
s.networkObserverMu.RUnlock()
|
|
if observer != nil {
|
|
capability := observer.Capability()
|
|
result.NetworkAbuseObservation = &capability
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (s *Server) routeNetworkObservations(w http.ResponseWriter, r *http.Request, segments []string, actor auth.Actor) {
|
|
if len(segments) != 2 {
|
|
writePreDispatchError(w, http.StatusNotFound, "not_found", "endpoint not found")
|
|
return
|
|
}
|
|
if r.Method != http.MethodGet {
|
|
methodOrNotFound(w, r, true, http.MethodGet)
|
|
return
|
|
}
|
|
if !requireSuperAdminObservation(w, actor) {
|
|
return
|
|
}
|
|
query, err := parseNetworkObservationQuery(r.URL.RawQuery)
|
|
if err != nil {
|
|
writePreDispatchError(w, http.StatusUnprocessableEntity, "invalid_network_observation_query", err.Error())
|
|
return
|
|
}
|
|
s.networkObserverMu.RLock()
|
|
observer := s.networkObserver
|
|
s.networkObserverMu.RUnlock()
|
|
if observer == nil {
|
|
writePreDispatchError(w, http.StatusServiceUnavailable, "network_observation_unavailable", "network observation is not configured")
|
|
return
|
|
}
|
|
after := query.after
|
|
if query.generation != "" && query.generation != observer.Generation() {
|
|
// A process restart creates a new generation. Old cursors must never be
|
|
// interpreted in the new ring; the panel re-establishes its baseline.
|
|
after = 0
|
|
}
|
|
batch, err := observer.Batch(after, query.limit)
|
|
if err != nil || batch.Validate() != nil {
|
|
writePreDispatchError(w, http.StatusServiceUnavailable, "network_observation_unavailable", "network observations could not be read safely")
|
|
return
|
|
}
|
|
if batch.Windows == nil {
|
|
batch.Windows = make([]networkobserve.Window, 0)
|
|
}
|
|
writeJSON(w, http.StatusOK, batch)
|
|
}
|
|
|
|
type networkObservationQuery struct {
|
|
generation string
|
|
after uint64
|
|
limit int
|
|
}
|
|
|
|
func parseNetworkObservationQuery(raw string) (networkObservationQuery, error) {
|
|
values, err := url.ParseQuery(raw)
|
|
if err != nil {
|
|
return networkObservationQuery{}, errors.New("query string is invalid")
|
|
}
|
|
for key, entries := range values {
|
|
if key != "generation" && key != "after_cursor" && key != "limit" {
|
|
return networkObservationQuery{}, errors.New("query contains an unknown field")
|
|
}
|
|
if len(entries) != 1 {
|
|
return networkObservationQuery{}, errors.New("query field must occur exactly once")
|
|
}
|
|
}
|
|
result := networkObservationQuery{limit: networkobserve.MaxBatchWindows}
|
|
if entries, exists := values["generation"]; exists {
|
|
generation := entries[0]
|
|
if !networkObservationGenerationPattern.MatchString(generation) {
|
|
return networkObservationQuery{}, errors.New("generation is invalid")
|
|
}
|
|
result.generation = generation
|
|
}
|
|
if entries, exists := values["after_cursor"]; exists {
|
|
rawAfter := entries[0]
|
|
after, parseErr := strconv.ParseUint(rawAfter, 10, 64)
|
|
if parseErr != nil {
|
|
return networkObservationQuery{}, errors.New("after_cursor is invalid")
|
|
}
|
|
result.after = after
|
|
}
|
|
if result.after != 0 && result.generation == "" {
|
|
return networkObservationQuery{}, errors.New("generation is required with after_cursor")
|
|
}
|
|
if entries, exists := values["limit"]; exists {
|
|
rawLimit := entries[0]
|
|
limit, parseErr := strconv.Atoi(rawLimit)
|
|
if parseErr != nil || limit <= 0 || limit > networkobserve.MaxBatchWindows {
|
|
return networkObservationQuery{}, errors.New("limit is invalid")
|
|
}
|
|
result.limit = limit
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func requireSuperAdminObservation(w http.ResponseWriter, actor auth.Actor) bool {
|
|
if actor.UserID < 1 || !actor.IsSuperAdmin || !actor.CanManage {
|
|
writePreDispatchError(w, http.StatusForbidden, "forbidden", "actor is not authorized to read host network observations")
|
|
return false
|
|
}
|
|
return true
|
|
}
|