Skip to content
Merged
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
8 changes: 8 additions & 0 deletions internal/node/labels_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ var ErrProviderUnreachable = errors.New("provider unreachable")
// not who the policy says serves that name.
var ErrProviderNotGranted = errors.New("provider not granted the service")

// ErrLabelsNotSatisfied marks a provider whose credential does not attest
// every label the caller requires or the egress floor sets: it is enrolled
// and reachable, and its attested labels are not the ones asked for.
var ErrLabelsNotSatisfied = errors.New("provider labels not satisfied")

// ErrHandshakeRateLimited marks a provider that refused the credential
// handshake at its per-peer handshake rate limit: it is up, and answered
// by resetting the stream with network.StreamRateLimited. A caller that
Expand Down Expand Up @@ -337,6 +342,9 @@ func (n *AgentMeshNode) checkProvider(providerBiscuit []byte, peerID peer.ID, re
}
authorizer.AddPolicy(api.AllowIfTruePolicy)
if err := authorizer.Authorize(); err != nil {
if stopped, _ := authorizerLimit(err); !stopped {
err = fmt.Errorf("%w: %w", ErrLabelsNotSatisfied, err)
}
if len(floor) > 0 {
return fmt.Errorf("provider %s does not attest every label of the caller requirement %v and the egress floor %v: %w", peerID, required, floor, err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/node/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ func (n *AgentMeshNode) connectMCPSessionWithRetry(ctx context.Context, targetPe
// A denial is an answer, not a failure to reach anyone. Retrying it
// just delays the same reply and knocks on the remote peer three times
// for one call. That holds for the remote peer refusing the caller
// and for the caller refusing a provider not granted the service.
if errors.Is(err, ErrAuthRejected) || errors.Is(err, ErrProviderNotGranted) {
// and for the caller refusing a provider not granted the service or
// not attesting the labels the call requires.
if errors.Is(err, ErrAuthRejected) || errors.Is(err, ErrProviderNotGranted) || errors.Is(err, ErrLabelsNotSatisfied) {
logger.Warnf("[MCP] Connecting to %s denied, not retrying: %v", targetPeer, err)
return nil, nil, err
}
Expand Down
20 changes: 15 additions & 5 deletions internal/node/mcp_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,15 @@ func TestCallMCPTool_LabelEnforcement(t *testing.T) {
t.Fatalf("CallMCPTool with satisfied label requirement should succeed: %v", err)
}

// A required label that nodeB's attestation does not satisfy: fail closed.
if _, err := nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, map[string]string{"region": "na-us"}); err == nil {
t.Fatal("CallMCPTool with a non-matching label requirement must fail")
// A required label that nodeB's attestation does not satisfy: fail closed,
// once. The refusal is a verdict on nodeB's credential, and a retry
// presents the same credential again.
_, err = nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, map[string]string{"region": "na-us"})
if !errors.Is(err, ErrLabelsNotSatisfied) {
t.Fatalf("CallMCPTool with a non-matching label requirement = %v, want ErrLabelsNotSatisfied", err)
}
if strings.Contains(err.Error(), "retries") {
t.Fatalf("CallMCPTool with a non-matching label requirement = %v, want one refusal with no retry", err)
}
}

Expand Down Expand Up @@ -882,8 +888,12 @@ func TestCallMCPTool_EgressFloorEnforcement(t *testing.T) {

// Floor nodeB does not attest; the caller staying silent must not waive it.
nodeA.nodeConfig = &NodeConfigComplete{EgressRequireLabels: map[string]string{"jurisdiction": "eu"}}
if _, err := nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, nil); err == nil {
t.Fatal("CallMCPTool outside the floor with no caller requirement must fail")
_, err = nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, nil)
if !errors.Is(err, ErrLabelsNotSatisfied) {
t.Fatalf("CallMCPTool outside the floor with no caller requirement = %v, want ErrLabelsNotSatisfied", err)
}
if strings.Contains(err.Error(), "retries") {
t.Fatalf("CallMCPTool outside the floor = %v, want one refusal with no retry", err)
}
}

Expand Down
Loading