Skip to content

Agent Mesh

fix(ci,sts,node): restore e2e runtime image and address review comments

Antonio Ojea649a46612a25
diff --git a/Makefile b/Makefile
index a8611c84..024ccb31 100644
--- a/Makefile
+++ b/Makefile
@@ -271,9 +271,12 @@ docker-build-node:
docker-build-mock-oidc:
docker build --load -t sam-mock-oidc:local -f tests/e2e/docker/Dockerfile.mock-oidc .
+docker-build-e2e-runtime:
+ docker build --load -t sam-e2e-runtime:local -f tests/e2e/docker/Dockerfile.sam-runtime .
+
docker-build-sam-console:
docker build --load -t sam-console:local -f Dockerfile.sam-console .
-docker-build: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-sam-console
+docker-build: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console
-.PHONY: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-sam-console docker-build
+.PHONY: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-build-sam-console docker-build
diff --git a/internal/controlplane/sts.go b/internal/controlplane/sts.go
index a712a7f8..62cf91a1 100644
--- a/internal/controlplane/sts.go
+++ b/internal/controlplane/sts.go
@@ -803,16 +803,21 @@ func (s *Server) authorizeBiscuitForEgress(ctx context.Context, rawBiscuit []byt
return nil, http.StatusForbidden, err
}
- for _, peerID := range []string{claims.NodePeerID, claims.ActorNodePeerID, claims.ClientPeerID} {
- if peerID == "" {
+ for _, rawPeerID := range []string{claims.NodePeerID, claims.ActorNodePeerID, claims.ClientPeerID} {
+ if rawPeerID == "" {
continue
}
- banned, err := s.store.IsNodeBanned(ctx, peerID)
+ pID, err := peer.Decode(rawPeerID)
+ if err != nil {
+ return nil, http.StatusForbidden, fmt.Errorf("invalid peer ID %q: %w", rawPeerID, err)
+ }
+ canonical := pID.String()
+ banned, err := s.store.IsNodeBanned(ctx, canonical)
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to check node ban: %w", err)
}
if banned {
- return nil, http.StatusForbidden, fmt.Errorf("peer %s is banned", peerID)
+ return nil, http.StatusForbidden, fmt.Errorf("peer %s is banned", canonical)
}
}
@@ -829,7 +834,7 @@ func (s *Server) authorizeBiscuitForEgress(ctx context.Context, rawBiscuit []byt
authorizer.AddFact(biscuit.Fact{
Predicate: biscuit.Predicate{
Name: api.FactConnectionPeerID,
- IDs: []biscuit.Term{biscuit.String(claims.ClientPeerID)},
+ IDs: []biscuit.Term{biscuit.String(clientPeer.String())},
},
})
authorizer.AddCheck(api.BaselineReplayCheck)
diff --git a/internal/controlplane/sts_test.go b/internal/controlplane/sts_test.go
index 858c2147..c895a7c6 100644
--- a/internal/controlplane/sts_test.go
+++ b/internal/controlplane/sts_test.go
@@ -32,6 +32,7 @@ import (
"testing"
"time"
+ "github.com/biscuit-auth/biscuit-go/v2"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/google/sam/api"
"github.com/google/sam/internal/identity"
@@ -664,4 +665,46 @@ func TestOAuth21AuthorizationCodePKCEAndTokenExchange(t *testing.T) {
}, cpPubKey); err != nil {
t.Fatalf("expected sealed 2-hop OAuth Biscuit to authorize get_forecast, got: %v", err)
}
+
+ // Verify that a non-canonical (CIDv1 base32) peer ID in Biscuit claims is
+ // canonicalized before checking IsNodeBanned in authorizeBiscuitForEgress.
+ if err := store.SetNodeBanned(ctx, nodePeerID.String(), true); err != nil {
+ t.Fatalf("SetNodeBanned: %v", err)
+ }
+ cidV1Str := peer.ToCid(nodePeerID).String()
+ if cidV1Str == nodePeerID.String() {
+ t.Fatalf("expected CIDv1 string %q to differ from canonical base58 %q", cidV1Str, nodePeerID.String())
+ }
+ cpPriv, _, err := store.GetCurrentKey(ctx)
+ if err != nil {
+ t.Fatalf("GetCurrentKey: %v", err)
+ }
+ bBuilder := biscuit.NewBuilder(cpPriv)
+ _ = bBuilder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactExpiration,
+ IDs: []biscuit.Term{biscuit.Date(time.Now().Add(time.Hour))},
+ }})
+ _ = bBuilder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactActorNode,
+ IDs: []biscuit.Term{biscuit.String(cidV1Str)},
+ }})
+ _ = bBuilder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactClientPeerID,
+ IDs: []biscuit.Term{biscuit.String(cidV1Str)},
+ }})
+ _ = bBuilder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactUser,
+ IDs: []biscuit.Term{biscuit.String("alice-sub")},
+ }})
+ nonCanonicalB, err := bBuilder.Build()
+ if err != nil {
+ t.Fatalf("bBuilder.Build: %v", err)
+ }
+ nonCanonicalBiscuit, err := nonCanonicalB.Serialize()
+ if err != nil {
+ t.Fatalf("nonCanonicalB.Serialize: %v", err)
+ }
+ if _, status, err := srv.authorizeBiscuitForEgress(ctx, nonCanonicalBiscuit, "bigquery.googleapis.com"); err == nil || status != http.StatusForbidden {
+ t.Fatalf("expected authorizeBiscuitForEgress to reject banned CIDv1 peer ID with 403, got status=%d err=%v", status, err)
+ }
}
diff --git a/internal/node/egress_inspect.go b/internal/node/egress_inspect.go
index ba207e1b..a08ba5cc 100644
--- a/internal/node/egress_inspect.go
+++ b/internal/node/egress_inspect.go
@@ -687,7 +687,11 @@ func (s *EgressService) inspectModelArmorRequest(ctx context.Context, cfg *api.M
return body, blocked, err
}
if replacement != "" && replacement != promptText {
- body = bytes.ReplaceAll(body, []byte(promptText), []byte(replacement))
+ updated, err := replaceInspectableText(body, promptText, replacement)
+ if err != nil {
+ return body, false, err
+ }
+ body = updated
}
return body, false, nil
}
@@ -705,7 +709,11 @@ func (s *EgressService) inspectModelArmorResponse(ctx context.Context, cfg *api.
return body, blocked, err
}
if replacement != "" && replacement != respText {
- body = bytes.ReplaceAll(body, []byte(respText), []byte(replacement))
+ updated, err := replaceInspectableText(body, respText, replacement)
+ if err != nil {
+ return body, false, err
+ }
+ body = updated
}
return body, false, nil
}
@@ -899,3 +907,77 @@ func extractInspectableText(body []byte) string {
}
return strings.TrimSpace(string(body))
}
+
+func replaceInspectableText(body []byte, original, replacement string) ([]byte, error) {
+ var doc map[string]any
+ if err := json.Unmarshal(body, &doc); err != nil {
+ return bytes.ReplaceAll(body, []byte(original), []byte(replacement)), nil
+ }
+
+ // If messages[].content were joined by '\n' in extractInspectableText, split back
+ // when the replacement has the same number of lines; otherwise replace in-place.
+ if msgs, ok := doc["messages"].([]any); ok {
+ var contentMaps []map[string]any
+ for _, m := range msgs {
+ if mm, ok := m.(map[string]any); ok {
+ if c, ok := mm["content"].(string); ok && c != "" {
+ contentMaps = append(contentMaps, mm)
+ }
+ }
+ }
+ if len(contentMaps) == 1 {
+ contentMaps[0]["content"] = replacement
+ return json.Marshal(doc)
+ }
+ if len(contentMaps) > 1 {
+ parts := strings.Split(replacement, "\n")
+ if len(parts) == len(contentMaps) {
+ for i, mm := range contentMaps {
+ mm["content"] = parts[i]
+ }
+ return json.Marshal(doc)
+ }
+ }
+ }
+
+ updated, modified := replaceStringInJSONValue(doc, original, replacement)
+ if modified {
+ return json.Marshal(updated)
+ }
+ return bytes.ReplaceAll(body, []byte(original), []byte(replacement)), nil
+}
+
+func replaceStringInJSONValue(v any, original, replacement string) (any, bool) {
+ switch val := v.(type) {
+ case string:
+ if val == original {
+ return replacement, true
+ }
+ if strings.Contains(val, original) {
+ return strings.ReplaceAll(val, original, replacement), true
+ }
+ return val, false
+ case []any:
+ anyMod := false
+ for i, elem := range val {
+ next, mod := replaceStringInJSONValue(elem, original, replacement)
+ if mod {
+ val[i] = next
+ anyMod = true
+ }
+ }
+ return val, anyMod
+ case map[string]any:
+ anyMod := false
+ for k, elem := range val {
+ next, mod := replaceStringInJSONValue(elem, original, replacement)
+ if mod {
+ val[k] = next
+ anyMod = true
+ }
+ }
+ return val, anyMod
+ default:
+ return v, false
+ }
+}
diff --git a/internal/node/egress_inspect_test.go b/internal/node/egress_inspect_test.go
index 8b56983a..7d61da90 100644
--- a/internal/node/egress_inspect_test.go
+++ b/internal/node/egress_inspect_test.go
@@ -135,15 +135,16 @@ func TestModelArmorInspection(t *testing.T) {
t.Fatalf("blocked prompt must not invoke exchanger (%d) or upstream (%d)", exCalls.Load(), upstreamCalls.Load())
}
- // 2. SDP de-identification rewrites prompt before forwarding to upstream with brokered token.
- req2 := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(`{"messages":[{"role":"user","content":"my ssn is 123-45-6789"}]}`))
+ // 2. SDP de-identification rewrites prompt before forwarding to upstream with brokered token,
+ // including when the prompt contains JSON-escaped newlines and quotes.
+ req2 := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(`{"messages":[{"role":"user","content":"line 1\nmy ssn is \"123-45-6789\""}]}`))
rec2 := httptest.NewRecorder()
svc.Handler().ServeHTTP(rec2, req2)
if rec2.Code != http.StatusOK {
t.Fatalf("redacted prompt status = %d, want 200 (%s)", rec2.Code, rec2.Body.String())
}
if !strings.Contains(lastUpstreamBody, "my ssn is [REDACTED]") || strings.Contains(lastUpstreamBody, "123-45-6789") {
- t.Fatalf("expected redacted upstream body, got %q", lastUpstreamBody)
+ t.Fatalf("expected redacted upstream body even with JSON escapes, got %q", lastUpstreamBody)
}
if lastUpstreamAuth != "Bearer brokered-cloud-token" {
t.Fatalf("expected brokered Authorization header, got %q", lastUpstreamAuth)
diff --git a/tests/e2e/docker/Dockerfile.sam-runtime b/tests/e2e/docker/Dockerfile.sam-runtime
new file mode 100644
index 00000000..1a23f369
--- /dev/null
+++ b/tests/e2e/docker/Dockerfile.sam-runtime
@@ -0,0 +1,35 @@
+# syntax=docker/dockerfile:1.7
+
+# Build container: compiles the Go binaries via `make build`.
+FROM golang:1.27-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 AS build
+
+WORKDIR /src
+RUN apk add --no-cache git make
+
+COPY go.mod go.sum ./
+RUN --mount=type=cache,target=/go/pkg/mod \
+ go mod download
+
+COPY . .
+
+RUN --mount=type=cache,target=/go/pkg/mod \
+ --mount=type=cache,target=/root/.cache/go-build \
+ make build
+
+# Final container: minimal alpine runtime hosting the built binaries for E2E tests.
+FROM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6
+
+RUN apk add --no-cache busybox socat curl iproute2
+
+RUN addgroup -S sam && adduser -S -G sam sam
+
+COPY --from=build /src/bin/ /usr/local/bin/
+
+USER root
+RUN mv /usr/local/bin/mcp-client /usr/local/bin/mcp-client-real && \
+ echo '#!/bin/sh' > /usr/local/bin/mcp-client && \
+ echo 'exec /usr/local/bin/mcp-client-real -token "secret-token" "$@"' >> /usr/local/bin/mcp-client && \
+ chmod +x /usr/local/bin/mcp-client
+
+USER sam
+WORKDIR /home/sam