|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2026-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build darwin |
| 7 | + |
| 8 | +package modules |
| 9 | + |
| 10 | +import ( |
| 11 | + "encoding/json" |
| 12 | + "errors" |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | + "net/http" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "github.com/DataDog/datadog-agent/pkg/notableevents" |
| 19 | + "github.com/DataDog/datadog-agent/pkg/system-probe/api/module" |
| 20 | + "github.com/DataDog/datadog-agent/pkg/system-probe/config" |
| 21 | + sysconfigtypes "github.com/DataDog/datadog-agent/pkg/system-probe/config/types" |
| 22 | + "github.com/DataDog/datadog-agent/pkg/system-probe/utils" |
| 23 | + "github.com/DataDog/datadog-agent/pkg/util/log" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + maxNotableEventsAckBodyBytes = 32 << 10 |
| 28 | + maxNotableEventsAckIDs = 100 |
| 29 | + maxNotableEventIDLength = 256 |
| 30 | +) |
| 31 | + |
| 32 | +type notableEventsCollector interface { |
| 33 | + Start() error |
| 34 | + Close() error |
| 35 | + Pending() []notableevents.Event |
| 36 | + Ack([]string) error |
| 37 | +} |
| 38 | + |
| 39 | +// newNotableEventsCollector provides the platform collector constructor and a test seam. |
| 40 | +var newNotableEventsCollector = func() (notableEventsCollector, error) { |
| 41 | + return notableevents.NewCollector() |
| 42 | +} |
| 43 | + |
| 44 | +// init registers the macOS notable-events module during system-probe startup. |
| 45 | +func init() { registerModule(NotableEvents) } |
| 46 | + |
| 47 | +// NotableEvents is the Darwin notable-events module factory. |
| 48 | +var NotableEvents = &module.Factory{ |
| 49 | + Name: config.NotableEventsModule, |
| 50 | + Fn: createNotableEventsModule, |
| 51 | +} |
| 52 | + |
| 53 | +var _ module.Module = (*notableEventsModule)(nil) |
| 54 | + |
| 55 | +type notableEventsModule struct { |
| 56 | + collector notableEventsCollector |
| 57 | +} |
| 58 | + |
| 59 | +type notableEventsAckRequest struct { |
| 60 | + IDs []string `json:"ids"` |
| 61 | +} |
| 62 | + |
| 63 | +type notableEventsErrorResponse struct { |
| 64 | + Error string `json:"error"` |
| 65 | +} |
| 66 | + |
| 67 | +// createNotableEventsModule constructs and starts the collector backing the module. |
| 68 | +func createNotableEventsModule(_ *sysconfigtypes.Config, _ module.FactoryDependencies) (module.Module, error) { |
| 69 | + collector, err := newNotableEventsCollector() |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("create notable events collector: %w", err) |
| 72 | + } |
| 73 | + if err := collector.Start(); err != nil { |
| 74 | + if closeErr := collector.Close(); closeErr != nil { |
| 75 | + log.Warnf("Failed to close notable events collector after start failure: %v", closeErr) |
| 76 | + } |
| 77 | + return nil, fmt.Errorf("start notable events collector: %w", err) |
| 78 | + } |
| 79 | + return ¬ableEventsModule{collector: collector}, nil |
| 80 | +} |
| 81 | + |
| 82 | +// Register exposes the pending-event and acknowledgement endpoints. |
| 83 | +func (m *notableEventsModule) Register(router *module.Router) error { |
| 84 | + router.HandleFunc("GET /check", m.handleCheck) |
| 85 | + router.HandleFunc("POST /ack", m.handleAck) |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// handleCheck returns a snapshot of events awaiting forwarding-pipeline acceptance. |
| 90 | +func (m *notableEventsModule) handleCheck(w http.ResponseWriter, req *http.Request) { |
| 91 | + writeNotableEventsJSON(req, w, http.StatusOK, m.collector.Pending()) |
| 92 | +} |
| 93 | + |
| 94 | +// handleAck validates and persists acknowledgement of accepted events. |
| 95 | +func (m *notableEventsModule) handleAck(w http.ResponseWriter, req *http.Request) { |
| 96 | + body, status, err := decodeNotableEventsAckRequest(w, req) |
| 97 | + if err != nil { |
| 98 | + writeNotableEventsJSON(req, w, status, notableEventsErrorResponse{Error: err.Error()}) |
| 99 | + return |
| 100 | + } |
| 101 | + if err := m.collector.Ack(body.IDs); err != nil { |
| 102 | + log.Errorf("Failed to acknowledge notable events: %v", err) |
| 103 | + writeNotableEventsJSON(req, w, http.StatusInternalServerError, notableEventsErrorResponse{Error: "failed to acknowledge notable events"}) |
| 104 | + return |
| 105 | + } |
| 106 | + writeNotableEventsJSON(req, w, http.StatusOK, struct{}{}) |
| 107 | +} |
| 108 | + |
| 109 | +// decodeNotableEventsAckRequest decodes a bounded acknowledgement request and validates its IDs. |
| 110 | +func decodeNotableEventsAckRequest(w http.ResponseWriter, req *http.Request) (notableEventsAckRequest, int, error) { |
| 111 | + var body notableEventsAckRequest |
| 112 | + req.Body = http.MaxBytesReader(w, req.Body, maxNotableEventsAckBodyBytes) |
| 113 | + decoder := json.NewDecoder(req.Body) |
| 114 | + decoder.DisallowUnknownFields() |
| 115 | + |
| 116 | + if err := decoder.Decode(&body); err != nil { |
| 117 | + status, decodeErr := notableEventsDecodeError(err) |
| 118 | + return body, status, decodeErr |
| 119 | + } |
| 120 | + if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) { |
| 121 | + if err == nil { |
| 122 | + err = errors.New("multiple JSON values") |
| 123 | + } |
| 124 | + status, decodeErr := notableEventsDecodeError(err) |
| 125 | + return body, status, decodeErr |
| 126 | + } |
| 127 | + if body.IDs == nil { |
| 128 | + return body, http.StatusBadRequest, errors.New("ids is required") |
| 129 | + } |
| 130 | + if len(body.IDs) > maxNotableEventsAckIDs { |
| 131 | + return body, http.StatusBadRequest, fmt.Errorf("ids must contain at most %d entries", maxNotableEventsAckIDs) |
| 132 | + } |
| 133 | + for _, id := range body.IDs { |
| 134 | + if strings.TrimSpace(id) == "" { |
| 135 | + return body, http.StatusBadRequest, errors.New("ids must not contain empty values") |
| 136 | + } |
| 137 | + if len(id) > maxNotableEventIDLength { |
| 138 | + return body, http.StatusBadRequest, fmt.Errorf("ids must not exceed %d bytes", maxNotableEventIDLength) |
| 139 | + } |
| 140 | + } |
| 141 | + return body, http.StatusOK, nil |
| 142 | +} |
| 143 | + |
| 144 | +// notableEventsDecodeError maps JSON decoding failures to safe HTTP responses. |
| 145 | +func notableEventsDecodeError(err error) (int, error) { |
| 146 | + var maxBytesErr *http.MaxBytesError |
| 147 | + if errors.As(err, &maxBytesErr) { |
| 148 | + return http.StatusRequestEntityTooLarge, fmt.Errorf("request body exceeds %d bytes", maxNotableEventsAckBodyBytes) |
| 149 | + } |
| 150 | + return http.StatusBadRequest, fmt.Errorf("invalid JSON request: %w", err) |
| 151 | +} |
| 152 | + |
| 153 | +// writeNotableEventsJSON writes a compact JSON response with the requested status. |
| 154 | +func writeNotableEventsJSON(req *http.Request, w http.ResponseWriter, status int, body interface{}) { |
| 155 | + w.Header().Set("Content-Type", "application/json") |
| 156 | + w.WriteHeader(status) |
| 157 | + utils.WriteAsJSON(req, w, body, utils.CompactOutput) |
| 158 | +} |
| 159 | + |
| 160 | +// GetStats returns the module's currently empty runtime statistics. |
| 161 | +func (m *notableEventsModule) GetStats() map[string]interface{} { |
| 162 | + return map[string]interface{}{} |
| 163 | +} |
| 164 | + |
| 165 | +// Close releases the collector and its filesystem monitoring resources. |
| 166 | +func (m *notableEventsModule) Close() { |
| 167 | + if err := m.collector.Close(); err != nil { |
| 168 | + log.Warnf("Failed to close notable events collector: %v", err) |
| 169 | + } |
| 170 | +} |
0 commit comments