Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions api/agentmesh.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/agentmesh.proto
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ message ServiceInfo {
string description = 3;
// Optional external ingress exposure for this service.
ServiceIngress ingress = 4;
// Forward X-Mesh-Principal, X-Mesh-Roles, X-Mesh-Claims, X-Mesh-Labels,
// X-Mesh-Actor-Node, and X-Mesh-Task to target_url for non-loopback destinations.
bool forward_context = 5;
}

message CommandBackend {
Expand Down
1 change: 1 addition & 0 deletions api/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ServiceConfig struct {
Command []string `yaml:"command,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Ingress *IngressConfig `yaml:"ingress,omitempty"`
ForwardContext bool `yaml:"forward_context,omitempty"`
}

// NodeConfig defines the optional attenuation rules and static services for a specific Agent Mesh Node.
Expand Down
16 changes: 15 additions & 1 deletion api/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,28 @@ func ValidateControlPlaneTransport(rawURL string, allowInsecure bool) error {
}
}

func isLoopbackHost(host string) bool {
// IsLoopbackHost reports whether host is "localhost" or an IPv4/IPv6 loopback address.
func IsLoopbackHost(host string) bool {
if strings.EqualFold(host, "localhost") {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}

// IsLoopbackURL reports whether rawURL parses and has a loopback host.
func IsLoopbackURL(rawURL string) bool {
u, err := url.Parse(rawURL)
if err != nil || u.Hostname() == "" {
return false
}
return IsLoopbackHost(u.Hostname())
}

func isLoopbackHost(host string) bool {
return IsLoopbackHost(host)
}

// KeysResponseFreshness bounds how far a signed /keys response's timestamp
// may be from the receiver's clock: a captured response must not be able to
// keep a retired key trusted after its grace period.
Expand Down
28 changes: 28 additions & 0 deletions api/trust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ func TestValidateControlPlaneTransport(t *testing.T) {
}
}

func TestIsLoopbackHostAndURL(t *testing.T) {
for _, tc := range []struct {
host string
url string
want bool
}{
{host: "localhost", url: "http://localhost:8080/mcp", want: true},
{host: "127.0.0.1", url: "http://127.0.0.1:9090", want: true},
{host: "::1", url: "http://[::1]:9090/mcp", want: true},
{host: "127.0.0.2", url: "http://127.0.0.2:8000", want: true},
{host: "example.com", url: "http://example.com/mcp", want: false},
{host: "192.168.1.1", url: "https://192.168.1.1:8443", want: false},
{host: "10.0.0.1", url: "http://10.0.0.1:8080", want: false},
{host: "", url: "not-a-valid-url", want: false},
} {
t.Run(tc.url, func(t *testing.T) {
if tc.host != "" {
if got := IsLoopbackHost(tc.host); got != tc.want {
t.Errorf("IsLoopbackHost(%q) = %v, want %v", tc.host, got, tc.want)
}
}
if got := IsLoopbackURL(tc.url); got != tc.want {
t.Errorf("IsLoopbackURL(%q) = %v, want %v", tc.url, got, tc.want)
}
})
}
}

func genKey(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey) {
t.Helper()
pub, priv, err := ed25519.GenerateKey(rand.Reader)
Expand Down
3 changes: 2 additions & 1 deletion internal/node/a2a_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type A2AService struct{ baseService }
func (s *A2AService) Init(ctx context.Context) error {
switch x := s.backend.(type) {
case *api.RegisterServiceRequest_TargetUrl:
h, err := newReverseProxy(x.TargetUrl)
fwd := s.info != nil && s.info.GetForwardContext()
h, err := newReverseProxy(x.TargetUrl, fwd)
if err != nil {
return err
}
Expand Down
37 changes: 29 additions & 8 deletions internal/node/backend_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,40 @@ func (t backendTarget) apply(h http.Header) {
// client returns an HTTP client that presents the backend credential on
// every request, for backends dialled through an SDK rather than a proxy.
func (t backendTarget) client() *http.Client {
if t.auth == "" {
return t.clientWithHeaders(nil)
}

// clientWithHeaders returns an HTTP client that presents extra headers (e.g. verified identity headers)
// and the backend credential on every outbound request.
func (t backendTarget) clientWithHeaders(extraHeaders http.Header) *http.Client {
if len(extraHeaders) == 0 && t.auth == "" {
return http.DefaultClient
}
return &http.Client{Transport: backendAuthTransport{target: t, base: http.DefaultTransport}}
return &http.Client{
Transport: mcpHeaderTransport{
target: t,
headers: extraHeaders.Clone(),
base: http.DefaultTransport,
},
}
}

type backendAuthTransport struct {
target backendTarget
base http.RoundTripper
type mcpHeaderTransport struct {
target backendTarget
headers http.Header
base http.RoundTripper
}

func (b backendAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
func (m mcpHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req = req.Clone(req.Context())
b.target.apply(req.Header)
return b.base.RoundTrip(req)
stripMeshIdentityHeaders(req.Header)
for k, vv := range m.headers {
req.Header[k] = vv
}
m.target.apply(req.Header)
base := m.base
if base == nil {
base = http.DefaultTransport
}
return base.RoundTrip(req)
}
Loading
Loading