feat(audit): Products may pass their caller's address as source_ip (#73)
`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
This commit was merged in pull request #73.
This commit is contained in:
1 parent
fdebac1735
commit
358c4abcb7
2 files changed
+66
-1
No files matched your search
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -21,6 +22,26 @@ type appendAuditReq struct {
|
||||
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) {
|
||||
@@ -33,6 +54,8 @@ func (s *Server) appendAudit(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sourceIP := auditSourceIP(&in, clientIP(r))
|
||||
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -42,7 +65,7 @@ func (s *Server) appendAudit(w http.ResponseWriter, r *http.Request) {
|
||||
Action: in.Action,
|
||||
TargetID: in.TargetID, TargetType: in.TargetType, TargetName: in.TargetName,
|
||||
Product: in.Product, Metadata: in.Metadata,
|
||||
SourceIP: clientIP(r), UserAgent: r.UserAgent(),
|
||||
SourceIP: sourceIP, UserAgent: r.UserAgent(),
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
|
||||
@@ -63,6 +63,48 @@ func TestAppendAndListAudit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppendAudit_sourceIP(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||||
for _, ip := range []string{"203.0.113.7", "not-an-ip"} {
|
||||
resp, body := h.do("POST", "/v1/audit", map[string]any{
|
||||
"tenant_id": h.tenant.ID, "action": "test.ip." + ip,
|
||||
"actor_id": "u1", "source_ip": ip,
|
||||
})
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("append %s status = %d, body=%s", ip, resp.StatusCode, body)
|
||||
}
|
||||
}
|
||||
|
||||
events := func(action string) []store.AuditEvent {
|
||||
resp, body := h.do("GET", "/v1/audit?action="+action, nil)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("list status = %d", resp.StatusCode)
|
||||
}
|
||||
return decode[struct {
|
||||
Items []store.AuditEvent `json:"items"`
|
||||
}](t, body).Items
|
||||
}
|
||||
|
||||
// A caller address that is an IP is recorded, the peer kept beside it.
|
||||
got := events("test.ip.203.0.113.7")
|
||||
if len(got) != 1 || got[0].SourceIP != "203.0.113.7" {
|
||||
t.Fatalf("source_ip not recorded: %+v", got)
|
||||
}
|
||||
if relayed, _ := got[0].Metadata["relayed_by"].(string); relayed == "" || relayed == "203.0.113.7" {
|
||||
t.Errorf("relayed_by = %q, want the peer", relayed)
|
||||
}
|
||||
|
||||
// Anything else is ignored: the peer is recorded, as without it.
|
||||
got = events("test.ip.not-an-ip")
|
||||
if len(got) != 1 || got[0].SourceIP == "not-an-ip" {
|
||||
t.Fatalf("invalid source_ip recorded: %+v", got)
|
||||
}
|
||||
if _, ok := got[0].Metadata["relayed_by"]; ok {
|
||||
t.Errorf("relayed_by set for an ignored source_ip")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppendAudit_actionRequired(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||||
resp, _ := h.do("POST", "/v1/audit", map[string]any{
|
||||
|
||||
Reference in new issue
Block a user