Skip to content

Commit 8296cf9

Browse files
committed
fix(a2a): forward effective user to managed agents
Signed-off-by: Evan Rauner <[email protected]> (cherry picked from commit 600f946) (cherry picked from commit 696c45b)
1 parent 5d6b867 commit 8296cf9

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎go/core/internal/a2a/client_interceptors.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ func (u *upstreamAuthInterceptor) Before(ctx context.Context, req *a2aclient.Req
6464
if err := u.authProvider.UpstreamAuth(httpReq, session, upstreamPrincipal); err != nil {
6565
return ctx, nil, err
6666
}
67+
effectiveUserID := session.Principal().User.ID
68+
if shareContext, ok := auth.ShareContextFrom(ctx); ok {
69+
effectiveUserID = shareContext.UserID
70+
}
71+
if effectiveUserID != "" {
72+
httpReq.Header.Set("X-User-Id", effectiveUserID)
73+
delete(req.ServiceParams, "x-user-id")
74+
}
6775
}
6876
propagation.TraceContext{}.Inject(ctx, propagation.HeaderCarrier(httpReq.Header))
6977
for k, values := range httpReq.Header {

‎go/core/internal/a2a/client_interceptors_test.go‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,78 @@ package a2a
22

33
import (
44
"context"
5+
"net/http"
56
"testing"
67

78
a2aclient "github.com/a2aproject/a2a-go/v2/a2aclient"
9+
"github.com/kagent-dev/kagent/go/core/pkg/auth"
810
"go.opentelemetry.io/otel/propagation"
911
"go.opentelemetry.io/otel/trace"
1012
"k8s.io/apimachinery/pkg/types"
1113
)
1214

15+
type interceptorTestSession struct {
16+
principal auth.Principal
17+
}
18+
19+
func (s interceptorTestSession) Principal() auth.Principal {
20+
return s.principal
21+
}
22+
23+
type interceptorTestAuthProvider struct {
24+
auth.AuthProvider
25+
userID string
26+
}
27+
28+
func (p interceptorTestAuthProvider) UpstreamAuth(r *http.Request, _ auth.Session, _ auth.Principal) error {
29+
if p.userID != "" {
30+
r.Header.Set("X-User-Id", p.userID)
31+
}
32+
return nil
33+
}
34+
35+
func TestUpstreamAuthInterceptor_ForwardsAuthenticatedUser(t *testing.T) {
36+
ctx := auth.AuthSessionTo(context.Background(), interceptorTestSession{
37+
principal: auth.Principal{User: auth.User{ID: "caller-user"}},
38+
})
39+
req := &a2aclient.Request{
40+
BaseURL: "http://agent.default:8080",
41+
ServiceParams: a2aclient.ServiceParams{},
42+
}
43+
interceptor := NewUpstreamAuthInterceptor(interceptorTestAuthProvider{}, types.NamespacedName{})
44+
45+
if _, _, err := interceptor.Before(ctx, req); err != nil {
46+
t.Fatalf("unexpected error: %v", err)
47+
}
48+
if got := req.ServiceParams.Get("X-User-Id"); len(got) != 1 || got[0] != "caller-user" {
49+
t.Errorf("X-User-Id: want %q, got %q", "caller-user", got)
50+
}
51+
}
52+
53+
func TestUpstreamAuthInterceptor_ForwardsShareOwner(t *testing.T) {
54+
ctx := auth.AuthSessionTo(context.Background(), interceptorTestSession{
55+
principal: auth.Principal{User: auth.User{ID: "visiting-user"}},
56+
})
57+
ctx = auth.ShareContextTo(ctx, &auth.ShareContext{UserID: "owner-user"})
58+
req := &a2aclient.Request{
59+
BaseURL: "http://agent.default:8080",
60+
ServiceParams: a2aclient.ServiceParams{
61+
"x-user-id": {"preexisting-user"},
62+
},
63+
}
64+
interceptor := NewUpstreamAuthInterceptor(
65+
interceptorTestAuthProvider{userID: "visiting-user"},
66+
types.NamespacedName{},
67+
)
68+
69+
if _, _, err := interceptor.Before(ctx, req); err != nil {
70+
t.Fatalf("unexpected error: %v", err)
71+
}
72+
if got := req.ServiceParams.Get("X-User-Id"); len(got) != 1 || got[0] != "owner-user" {
73+
t.Errorf("X-User-Id: want %q, got %q", "owner-user", got)
74+
}
75+
}
76+
1377
func TestUpstreamAuthInterceptor_InjectsTraceContext(t *testing.T) {
1478
const rawTraceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
1579

0 commit comments

Comments
 (0)