Skip to content

Agent Mesh

bridge: scaffold sam-a2a-bridge module with the sidecar gate transport

Tomas Tormo83571fbb8a60
diff --git a/cmd/sam-a2a-bridge/go.mod b/cmd/sam-a2a-bridge/go.mod
new file mode 100644
index 00000000..897a0ca7
--- /dev/null
+++ b/cmd/sam-a2a-bridge/go.mod
@@ -0,0 +1,8 @@
+module github.com/google/sam/cmd/sam-a2a-bridge
+
+go 1.25.7
+
+require (
+ github.com/a2aproject/a2a-go/v2 v2.5.0 // indirect
+ github.com/modelcontextprotocol/go-sdk v1.7.0 // indirect
+)
diff --git a/cmd/sam-a2a-bridge/go.sum b/cmd/sam-a2a-bridge/go.sum
new file mode 100644
index 00000000..5058ce6f
--- /dev/null
+++ b/cmd/sam-a2a-bridge/go.sum
@@ -0,0 +1,4 @@
+github.com/a2aproject/a2a-go/v2 v2.5.0 h1:ZdcFoxv+nZTUV0i2ue5hES76YCANFPG9vjqd7vK8yWM=
+github.com/a2aproject/a2a-go/v2 v2.5.0/go.mod h1:NcRp/ZHxgMzDj12/BteIC2gOjljuEBKaGRfEdJ2lNSI=
+github.com/modelcontextprotocol/go-sdk v1.7.0 h1:yqjY2dsbKAC0LSuWZVBMrHgiG8ukXv6NRo0JiALay44=
+github.com/modelcontextprotocol/go-sdk v1.7.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts=
diff --git a/cmd/sam-a2a-bridge/transport.go b/cmd/sam-a2a-bridge/transport.go
new file mode 100644
index 00000000..66d52e4e
--- /dev/null
+++ b/cmd/sam-a2a-bridge/transport.go
@@ -0,0 +1,69 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+)
+
+// Mirrors api.HeaderSamAuthentication / api.HeaderSamRequiredLabels
+// (api/network.go:114,133); literals because this module must not
+// depend on the root module.
+const (
+ headerSamAuthentication = "X-Sam-Authentication"
+ headerSamRequiredLabels = "X-Sam-Required-Labels"
+)
+
+// sidecarError carries a non-2xx sidecar reply so tool handlers can show
+// the refusal (e.g. the labels-gate 403) to the harness verbatim.
+type sidecarError struct {
+ Status int
+ Body string
+}
+
+func (e *sidecarError) Error() string {
+ return fmt.Sprintf("%d: %s", e.Status, strings.TrimSpace(e.Body))
+}
+
+// samTransport injects the local sidecar gate headers on every outbound
+// request and converts non-2xx replies into sidecarError.
+type samTransport struct {
+ base http.RoundTripper
+ token string
+ requiredLabels string
+}
+
+func (t *samTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ req = req.Clone(req.Context())
+ if t.token != "" {
+ req.Header.Set(headerSamAuthentication, "Bearer "+t.token)
+ }
+ if t.requiredLabels != "" {
+ req.Header.Set(headerSamRequiredLabels, t.requiredLabels)
+ }
+ resp, err := t.base.RoundTrip(req)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode >= 400 {
+ body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
+ _ = resp.Body.Close()
+ return nil, &sidecarError{Status: resp.StatusCode, Body: string(body)}
+ }
+ return resp, nil
+}
diff --git a/cmd/sam-a2a-bridge/transport_test.go b/cmd/sam-a2a-bridge/transport_test.go
new file mode 100644
index 00000000..3fe801cf
--- /dev/null
+++ b/cmd/sam-a2a-bridge/transport_test.go
@@ -0,0 +1,96 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "errors"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestSamTransportInjectsHeaders(t *testing.T) {
+ var gotAuth, gotLabels string
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ gotAuth = r.Header.Get("X-Sam-Authentication")
+ gotLabels = r.Header.Get("X-Sam-Required-Labels")
+ _, _ = w.Write([]byte("ok"))
+ }))
+ defer backend.Close()
+
+ client := &http.Client{Transport: &samTransport{base: http.DefaultTransport, token: "tok", requiredLabels: "region=eu"}}
+ resp, err := client.Get(backend.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if string(body) != "ok" {
+ t.Fatalf("body = %q", body)
+ }
+ if gotAuth != "Bearer tok" {
+ t.Errorf("X-Sam-Authentication = %q, want %q", gotAuth, "Bearer tok")
+ }
+ if gotLabels != "region=eu" {
+ t.Errorf("X-Sam-Required-Labels = %q, want %q", gotLabels, "region=eu")
+ }
+}
+
+func TestSamTransportOmitsEmptyHeaders(t *testing.T) {
+ var sawAuth, sawLabels bool
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, sawAuth = r.Header["X-Sam-Authentication"]
+ _, sawLabels = r.Header["X-Sam-Required-Labels"]
+ }))
+ defer backend.Close()
+
+ client := &http.Client{Transport: &samTransport{base: http.DefaultTransport}}
+ resp, err := client.Get(backend.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = resp.Body.Close()
+ if sawAuth || sawLabels {
+ t.Fatalf("empty config must not send gate headers (auth=%v labels=%v)", sawAuth, sawLabels)
+ }
+}
+
+func TestSamTransportMapsRefusalToSidecarError(t *testing.T) {
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "Required labels not attested by provider", http.StatusForbidden)
+ }))
+ defer backend.Close()
+
+ client := &http.Client{Transport: &samTransport{base: http.DefaultTransport, token: "tok"}}
+ _, err := client.Get(backend.URL)
+ if err == nil {
+ t.Fatal("403 must surface as an error")
+ }
+ var se *sidecarError
+ if !errors.As(err, &se) {
+ t.Fatalf("error %T does not unwrap to *sidecarError: %v", err, err)
+ }
+ if se.Status != http.StatusForbidden {
+ t.Errorf("status = %d, want 403", se.Status)
+ }
+ if !strings.Contains(se.Body, "Required labels not attested by provider") {
+ t.Errorf("body not verbatim: %q", se.Body)
+ }
+ if want := "403: Required labels not attested by provider"; se.Error() != want {
+ t.Errorf("Error() = %q, want %q", se.Error(), want)
+ }
+}