`POST /v1/audit` recorded the address of the product that sent the event. So Prüfwerk's API-key writes showed the agent's container address (172.17.0.6 on stage) instead of the caller's. - `appendAuditReq` gains `source_ip`. It is taken when `net.ParseIP` accepts it and stored in canonical form. The peer that sent the event is kept in `metadata.relayed_by`. - Anything that doesn't parse is ignored, and the peer is recorded as before. - Trust: `audit.write` is service-principal only, the same trust as the `actor_id` those callers already set. - No migration, and existing callers are unchanged. - Callers: Prüfwerk (pruefwerk #567, from the API host's proxy header); Werkmeister will reuse the field (agreed with werkmeister-18). Test: `TestAppendAudit_sourceIP` covers a valid IP (recorded, `relayed_by` set) and junk (ignored), in each store. Not in this PR: `clientIP()` takes the **leftmost** `X-Forwarded-For` entry. orca-proxy appends the peer it saw, so the rightmost entry is authoritative and the leftmost is chosen by the client. Separate issue for werkmeister-18. Not run locally (RAM); CI runs the tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Reviewed-on: #73
131 lines
3.9 KiB
Go
131 lines
3.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
type appendAuditReq struct {
|
|
TenantID string `json:"tenant_id,omitempty"`
|
|
ProjectID string `json:"project_id,omitempty"`
|
|
ActorID string `json:"actor_id,omitempty"`
|
|
ActorName string `json:"actor_name,omitempty"`
|
|
ActorType string `json:"actor_type,omitempty"`
|
|
Action string `json:"action"`
|
|
TargetID string `json:"target_id,omitempty"`
|
|
TargetType string `json:"target_type,omitempty"`
|
|
TargetName string `json:"target_name,omitempty"`
|
|
Product string `json:"product,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
// SourceIP is the address of the product's own caller, when the product
|
|
// knows it (e.g. the client of a product API). Without it the event
|
|
// records the address of the product itself.
|
|
SourceIP string `json:"source_ip,omitempty"`
|
|
}
|
|
|
|
// auditSourceIP picks the address an appended event records. Only
|
|
// audit.write service principals reach appendAudit, so a caller address they
|
|
// pass is taken when it parses as an IP; the peer that sent the event is then
|
|
// kept in metadata.relayed_by. Anything else records the peer, as before.
|
|
func auditSourceIP(in *appendAuditReq, peer string) string {
|
|
ip := net.ParseIP(in.SourceIP)
|
|
if ip == nil {
|
|
return peer
|
|
}
|
|
if in.Metadata == nil {
|
|
in.Metadata = map[string]interface{}{}
|
|
}
|
|
in.Metadata["relayed_by"] = peer
|
|
return ip.String()
|
|
}
|
|
|
|
func (s *Server) appendAudit(w http.ResponseWriter, r *http.Request) {
|
|
var in appendAuditReq
|
|
if !decodeJSON(w, r, &in) {
|
|
return
|
|
}
|
|
if in.Action == "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_action", "action is required")
|
|
return
|
|
}
|
|
|
|
sourceIP := auditSourceIP(&in, clientIP(r))
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
ev, err := s.Store.AppendAudit(ctx, store.AuditEvent{
|
|
TenantID: in.TenantID, ProjectID: in.ProjectID,
|
|
ActorID: in.ActorID, ActorName: in.ActorName, ActorType: in.ActorType,
|
|
Action: in.Action,
|
|
TargetID: in.TargetID, TargetType: in.TargetType, TargetName: in.TargetName,
|
|
Product: in.Product, Metadata: in.Metadata,
|
|
SourceIP: sourceIP, UserAgent: r.UserAgent(),
|
|
})
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, ev)
|
|
}
|
|
|
|
func (s *Server) listAudit(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
f := store.AuditFilter{
|
|
TenantID: q.Get("tenant_id"),
|
|
Product: q.Get("product"),
|
|
ActorID: q.Get("actor_id"),
|
|
Action: q.Get("action"),
|
|
}
|
|
if s := q.Get("since"); s != "" {
|
|
t, err := time.Parse(time.RFC3339, s)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_since", "must be RFC3339")
|
|
return
|
|
}
|
|
f.Since = &t
|
|
}
|
|
if s := q.Get("until"); s != "" {
|
|
t, err := time.Parse(time.RFC3339, s)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_until", "must be RFC3339")
|
|
return
|
|
}
|
|
f.Until = &t
|
|
}
|
|
if s := q.Get("limit"); s != "" {
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil || n < 1 || n > 500 {
|
|
writeError(w, http.StatusBadRequest, "invalid_limit", "must be 1..500")
|
|
return
|
|
}
|
|
f.Limit = n
|
|
}
|
|
if s := q.Get("cursor"); s != "" {
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_cursor", "must be an integer")
|
|
return
|
|
}
|
|
f.Cursor = n
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
items, next, err := s.Store.ListAudit(ctx, f)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
out := map[string]any{"items": items}
|
|
if next > 0 {
|
|
out["next_cursor"] = next
|
|
}
|
|
writeJSON(w, http.StatusOK, out)
|
|
}
|