Skip to content

Agent Mesh

fix(node): address PR review comments for audit and telemetry

Antonio Ojea90adbc78cd1b
diff --git a/internal/node/middleware.go b/internal/node/middleware.go
index db29951f..0cfa72e4 100644
--- a/internal/node/middleware.go
+++ b/internal/node/middleware.go
@@ -17,6 +17,7 @@ package node
import (
"crypto/ed25519"
"fmt"
+ "sync/atomic"
"github.com/biscuit-auth/biscuit-go/v2"
"github.com/biscuit-auth/biscuit-go/v2/datalog"
@@ -45,13 +46,13 @@ type trackingStream struct {
func (t *trackingStream) Read(p []byte) (n int, err error) {
n, err = t.Stream.Read(p)
- t.bytesRead += int64(n)
+ atomic.AddInt64(&t.bytesRead, int64(n))
return n, err
}
func (t *trackingStream) Write(p []byte) (n int, err error) {
n, err = t.Stream.Write(p)
- t.bytesWritten += int64(n)
+ atomic.AddInt64(&t.bytesWritten, int64(n))
return n, err
}
@@ -63,15 +64,17 @@ func (n *SamNode) WithBiscuitAuth(next func(network.Stream, RequestContext)) net
var reqCtx RequestContext
defer func() {
- if reqCtx.Target != "" { // Only log if we successfully got a request context
- logger.Infow("Stream Accounting",
- "peer_id", remotePeer.String(),
- "target", reqCtx.Target,
- "protocol", reqCtx.Protocol,
- "bytes_read", ts.bytesRead,
- "bytes_written", ts.bytesWritten,
- )
+ target := reqCtx.Target
+ if target == "" {
+ target = "unauthenticated_or_failed"
}
+ logger.Infow("Stream Accounting",
+ "peer_id", remotePeer.String(),
+ "target", target,
+ "protocol", reqCtx.Protocol,
+ "bytes_read", atomic.LoadInt64(&ts.bytesRead),
+ "bytes_written", atomic.LoadInt64(&ts.bytesWritten),
+ )
if err := ts.Close(); err != nil {
logger.Debugf("[Auth] Failed to close stream: %v", err)
}
@@ -259,8 +262,8 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.
if facts, _ := authorizer.Query(biscuit.Rule{
Head: biscuit.Predicate{Name: "get_user", IDs: []biscuit.Term{biscuit.Variable("u")}},
Body: []biscuit.Predicate{{Name: api.FactUser, IDs: []biscuit.Term{biscuit.Variable("u")}}},
- }); len(facts) > 0 {
- if s, ok := facts[0].Predicate.IDs[0].(biscuit.String); ok {
+ }); len(facts) > 0 && len(facts[0].IDs) > 0 {
+ if s, ok := facts[0].IDs[0].(biscuit.String); ok {
userStr = string(s)
}
}
@@ -268,8 +271,8 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.
if facts, _ := authorizer.Query(biscuit.Rule{
Head: biscuit.Predicate{Name: "get_email", IDs: []biscuit.Term{biscuit.Variable("e")}},
Body: []biscuit.Predicate{{Name: api.FactEmail, IDs: []biscuit.Term{biscuit.Variable("e")}}},
- }); len(facts) > 0 {
- if s, ok := facts[0].Predicate.IDs[0].(biscuit.String); ok {
+ }); len(facts) > 0 && len(facts[0].IDs) > 0 {
+ if s, ok := facts[0].IDs[0].(biscuit.String); ok {
emailStr = string(s)
}
}
@@ -277,8 +280,8 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.
if facts, _ := authorizer.Query(biscuit.Rule{
Head: biscuit.Predicate{Name: "get_role", IDs: []biscuit.Term{biscuit.Variable("r")}},
Body: []biscuit.Predicate{{Name: api.FactRole, IDs: []biscuit.Term{biscuit.Variable("r")}}},
- }); len(facts) > 0 {
- if s, ok := facts[0].Predicate.IDs[0].(biscuit.String); ok {
+ }); len(facts) > 0 && len(facts[0].IDs) > 0 {
+ if s, ok := facts[0].IDs[0].(biscuit.String); ok {
roleStr = string(s)
}
}
diff --git a/internal/node/node.go b/internal/node/node.go
index e20841db..cb03d06d 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -1719,7 +1719,7 @@ type readCloserWithCount struct {
func (rc *readCloserWithCount) Read(p []byte) (int, error) {
n, err := rc.ReadCloser.Read(p)
- rc.bytesRead += int64(n)
+ atomic.AddInt64(&rc.bytesRead, int64(n))
return n, err
}
@@ -1731,7 +1731,7 @@ type responseWriterWithCount struct {
func (w *responseWriterWithCount) Write(p []byte) (int, error) {
n, err := w.ResponseWriter.Write(p)
- w.bytesWritten += int64(n)
+ atomic.AddInt64(&w.bytesWritten, int64(n))
return n, err
}
@@ -1740,6 +1740,12 @@ func (w *responseWriterWithCount) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
+func (w *responseWriterWithCount) Flush() {
+ if f, ok := w.ResponseWriter.(http.Flusher); ok {
+ f.Flush()
+ }
+}
+
func (n *SamNode) StartIngressServer(ctx context.Context) error {
listener, err := gostream.Listen(n.Host, "/libp2p-http")
if err != nil {
@@ -1748,6 +1754,32 @@ func (n *SamNode) StartIngressServer(ctx context.Context) error {
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ rc := &readCloserWithCount{ReadCloser: r.Body}
+ r.Body = rc
+ wc := &responseWriterWithCount{ResponseWriter: w, statusCode: http.StatusOK}
+ w = wc
+
+ var remotePeer peer.ID
+ var target string
+
+ defer func() {
+ peerIDStr := ""
+ if remotePeer != "" {
+ peerIDStr = remotePeer.String()
+ }
+ finalTarget := target
+ if finalTarget == "" {
+ finalTarget = "unauthenticated_or_failed"
+ }
+ logger.Infow("Stream Accounting",
+ "peer_id", peerIDStr,
+ "target", finalTarget,
+ "protocol", "/libp2p-http",
+ "bytes_read", atomic.LoadInt64(&rc.bytesRead),
+ "bytes_written", atomic.LoadInt64(&wc.bytesWritten),
+ )
+ }()
+
logger.Infof("[Ingress] Received request: %s %s", r.Method, r.URL.Path)
path := r.URL.Path
parts := strings.SplitN(strings.TrimPrefix(path, "/"), "/", 3)
@@ -1781,17 +1813,18 @@ func (n *SamNode) StartIngressServer(ctx context.Context) error {
}
// Parse remote peer from RemoteAddr
- remotePeer, err := peer.Decode(r.RemoteAddr)
+ remotePeer, err = peer.Decode(r.RemoteAddr)
if err != nil {
http.Error(w, "Invalid remote peer", http.StatusBadRequest)
return
}
+ target = strings.ToLower(serviceTypeStr) + "://" + serviceName
reqCtx := RequestContext{
PeerID: remotePeer,
User: "", // Extracted implicitly if needed, or left empty
Protocol: "/libp2p-http",
- Target: strings.ToLower(serviceTypeStr) + "://" + serviceName,
+ Target: target,
}
// Verify authorization
@@ -1827,22 +1860,9 @@ func (n *SamNode) StartIngressServer(ctx context.Context) error {
}
r.URL.RawPath = ""
- // Wrap for accounting
- rc := &readCloserWithCount{ReadCloser: r.Body}
- r.Body = rc
- wc := &responseWriterWithCount{ResponseWriter: w, statusCode: http.StatusOK}
-
- defer func() {
- logger.Infow("Stream Accounting",
- "peer_id", remotePeer.String(),
- "target", reqCtx.Target,
- "protocol", "/libp2p-http",
- "bytes_read", rc.bytesRead,
- "bytes_written", wc.bytesWritten,
- )
- }()
+ r.URL.RawPath = ""
- svc.Handler().ServeHTTP(wc, r)
+ svc.Handler().ServeHTTP(w, r)
}),
}
diff --git a/tests/integration/datapath_test.go b/tests/integration/datapath_test.go
index 995dd7a5..1e8de91e 100644
--- a/tests/integration/datapath_test.go
+++ b/tests/integration/datapath_test.go
@@ -290,5 +290,9 @@ func assertLogInFile(t *testing.T, logFile, expectedMsg string) {
time.Sleep(100 * time.Millisecond)
}
b, _ := os.ReadFile(logFile)
- t.Errorf("Expected log file %s to contain %q, but it didn't.\nLog end:\n%s", logFile, expectedMsg, string(b[len(b)-1000:])) // print last 1000 bytes
+ logEnd := string(b)
+ if len(b) > 1000 {
+ logEnd = string(b[len(b)-1000:])
+ }
+ t.Errorf("Expected log file %s to contain %q, but it didn't.\nLog end:\n%s", logFile, expectedMsg, logEnd)
}