Skip to content

Agent Mesh

fix(node): enforce TAR service match in NarrowOIDCScopes and cache ext_proc HTTP clients

Antonio Ojeaa45e761b029a
diff --git a/internal/node/egress.go b/internal/node/egress.go
index c4dbb1d3..f6592d12 100644
--- a/internal/node/egress.go
+++ b/internal/node/egress.go
@@ -25,6 +25,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "sync"
"time"
"github.com/google/sam/api"
@@ -52,6 +53,11 @@ func refuse(w http.ResponseWriter, status int, text, errorType string) {
http.Error(w, text, status)
}
+type extProcClientEntry struct {
+ client *http.Client
+ endpoint string
+}
+
// EgressService serves egress://<name>: this node is the HTTP origin for one
// destination outside the mesh. The destination, where to forward, and which
// credential to present are the control plane's decision (an
@@ -68,6 +74,8 @@ type EgressService struct {
isStaticSecret bool
modelArmorBaseURL string
modelArmorClient *http.Client
+ extProcMu sync.Mutex
+ extProcClients map[string]extProcClientEntry
handler http.Handler
}
@@ -111,10 +119,12 @@ func newEgressServiceForNode(node *SamNode, d *api.EgressDestination, secretsDir
Name: d.GetName(),
Description: "egress to " + target.Scheme + "://" + target.Host,
},
- target: target,
- secretsDir: secretsDir,
+ target: target,
+ secretsDir: secretsDir,
+ extProcClients: make(map[string]extProcClientEntry),
}
s.initExchanger()
+ s.initExtProcClients()
return s, nil
}
diff --git a/internal/node/egress_broker.go b/internal/node/egress_broker.go
index 5a2f5831..dbec4f54 100644
--- a/internal/node/egress_broker.go
+++ b/internal/node/egress_broker.go
@@ -555,10 +555,6 @@ func (e *PlatformIdentityExchanger) Exchange(ctx context.Context, _ string, rule
// - If a TAR specifies scope constraints that have an empty intersection with
// policyScopes, NarrowOIDCScopes returns an error so the exchange fails closed.
func NarrowOIDCScopes(policyScopes []string, destName string, rules []*api.TaskAuthorizationRule) ([]string, error) {
- if len(policyScopes) == 0 {
- // Policy grants no OAuth scopes; a TAR can never select or add scopes.
- return nil, nil
- }
current := slices.Clone(policyScopes)
targetSvc := api.EgressServicePrefix + api.NormalizeMeshHost(strings.TrimPrefix(destName, api.EgressServicePrefix))
@@ -567,10 +563,12 @@ func NarrowOIDCScopes(policyScopes []string, destName string, rules []*api.TaskA
continue
}
var tarPerms []string
+ matchedRule := false
for _, r := range tar.GetRules() {
if !taskRuleMatchesService(r, targetSvc) {
continue
}
+ matchedRule = true
for _, p := range r.GetOperation().GetAllowedPermissions() {
p = strings.TrimSpace(p)
if p != "" {
@@ -578,7 +576,10 @@ func NarrowOIDCScopes(policyScopes []string, destName string, rules []*api.TaskA
}
}
}
- if len(tarPerms) == 0 {
+ if !matchedRule {
+ return nil, fmt.Errorf("task authorization rule %q does not allow service %s", tar.GetName(), targetSvc)
+ }
+ if len(policyScopes) == 0 || len(tarPerms) == 0 {
continue
}
// Distinguish fine-grained cloud IAM permissions (e.g.
@@ -608,6 +609,10 @@ func NarrowOIDCScopes(policyScopes []string, destName string, rules []*api.TaskA
}
current = next
}
+ if len(policyScopes) == 0 {
+ // Policy grants no OAuth scopes; a TAR can never select or add scopes.
+ return nil, nil
+ }
return current, nil
}
diff --git a/internal/node/egress_broker_test.go b/internal/node/egress_broker_test.go
index 756b1038..88ac26f7 100644
--- a/internal/node/egress_broker_test.go
+++ b/internal/node/egress_broker_test.go
@@ -149,6 +149,24 @@ func TestTARNarrowsNeverSelects_OIDCScopes(t *testing.T) {
if !slices.Equal(resources, []string{"//bigquery.googleapis.com/projects/my-proj/datasets/sales_2026/tables/q1"}) {
t.Fatalf("expected intersected resources [../tables/q1], got %v", resources)
}
+
+ // 7. TAR with empty rules (fail-closed) or targeting a different service -> rejected by NarrowOIDCScopes.
+ emptyTAR := &api.TaskAuthorizationRule{Name: "tasks/empty-fail-closed"}
+ if _, err := NarrowOIDCScopes(policyScopes, "bigquery.googleapis.com", []*api.TaskAuthorizationRule{emptyTAR}); err == nil {
+ t.Fatal("expected NarrowOIDCScopes to reject TAR with empty rules list")
+ }
+ if _, err := NarrowOIDCScopes(nil, "bigquery.googleapis.com", []*api.TaskAuthorizationRule{emptyTAR}); err == nil {
+ t.Fatal("expected NarrowOIDCScopes(nil scopes) to reject TAR with empty rules list")
+ }
+ otherSvcTAR := &api.TaskAuthorizationRule{
+ Name: "tasks/storage-only",
+ Rules: []*api.TaskRule{{
+ AllowedServices: []string{"egress://storage.googleapis.com"},
+ }},
+ }
+ if _, err := NarrowOIDCScopes(policyScopes, "bigquery.googleapis.com", []*api.TaskAuthorizationRule{otherSvcTAR}); err == nil {
+ t.Fatal("expected NarrowOIDCScopes to reject TAR targeting a different service")
+ }
}
func TestTARNarrowsNeverSelects_AWSSessionPolicy(t *testing.T) {
diff --git a/internal/node/egress_inspect.go b/internal/node/egress_inspect.go
index a08ba5cc..6e6dafb7 100644
--- a/internal/node/egress_inspect.go
+++ b/internal/node/egress_inspect.go
@@ -473,12 +473,45 @@ func initialExtProcMode(cfg *api.ExtProc) *extprocv3http.ProcessingMode {
return mode
}
+func extProcClientCacheKey(cfg *api.ExtProc) string {
+ return strings.TrimSpace(cfg.GetTarget()) + "|" + strings.TrimSpace(cfg.GetCa()) + "|" + strings.TrimSpace(cfg.GetClientCertificate())
+}
+
+func (s *EgressService) initExtProcClients() {
+ for _, ins := range s.destination.GetInspection().GetInspectors() {
+ if ep := ins.GetExtProc(); ep != nil {
+ _, _, _ = s.getExtProcHTTPClient(ep)
+ }
+ }
+}
+
+func (s *EgressService) getExtProcHTTPClient(cfg *api.ExtProc) (*http.Client, string, error) {
+ key := extProcClientCacheKey(cfg)
+ s.extProcMu.Lock()
+ defer s.extProcMu.Unlock()
+ if s.extProcClients == nil {
+ s.extProcClients = make(map[string]extProcClientEntry)
+ }
+ if entry, ok := s.extProcClients[key]; ok {
+ return entry.client, entry.endpoint, nil
+ }
+ client, endpoint, err := buildExtProcHTTPClient(cfg, s.secretsDir)
+ if err != nil {
+ return nil, "", err
+ }
+ s.extProcClients[key] = extProcClientEntry{
+ client: client,
+ endpoint: endpoint,
+ }
+ return client, endpoint, nil
+}
+
func (s *EgressService) runExtProcRequestPhase(r *http.Request, cfg *api.ExtProc, reqBody []byte, callerCtx egressCallerContext) (*extProcClientStream, *extprocv3http.ProcessingMode, *extprocv3.ImmediateResponse, []byte, error) {
msgTimeout := defaultExtProcMessageTimeout
if cfg.GetMessageTimeout().IsValid() && cfg.GetMessageTimeout().AsDuration() > 0 {
msgTimeout = cfg.GetMessageTimeout().AsDuration()
}
- client, endpoint, err := buildExtProcHTTPClient(cfg, s.secretsDir)
+ client, endpoint, err := s.getExtProcHTTPClient(cfg)
if err != nil {
return nil, nil, nil, reqBody, err
}
diff --git a/internal/node/egress_inspect_test.go b/internal/node/egress_inspect_test.go
index 7d61da90..69ecdec6 100644
--- a/internal/node/egress_inspect_test.go
+++ b/internal/node/egress_inspect_test.go
@@ -450,9 +450,14 @@ func TestGatewayExtProcServer(t *testing.T) {
})
addr := startH2CServer(t, mux)
- client, endpoint, err := buildExtProcHTTPClient(&api.ExtProc{Target: addr}, t.TempDir())
+ extProcCfg := &api.ExtProc{Target: addr}
+ client, endpoint, err := egressSvc.getExtProcHTTPClient(extProcCfg)
if err != nil {
- t.Fatalf("buildExtProcHTTPClient: %v", err)
+ t.Fatalf("getExtProcHTTPClient: %v", err)
+ }
+ client2, _, err := egressSvc.getExtProcHTTPClient(extProcCfg)
+ if err != nil || client2 != client {
+ t.Fatalf("expected getExtProcHTTPClient to return cached *http.Client, got err=%v same=%v", err, client2 == client)
}
// 1. MCP tools/call with allowed tool "get_pr":