@@ -2,14 +2,78 @@ package a2a
22
33import (
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+
1377func TestUpstreamAuthInterceptor_InjectsTraceContext (t * testing.T ) {
1478 const rawTraceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
1579
0 commit comments