diff --git a/internal/node/labels_gate.go b/internal/node/labels_gate.go index cafc1a4a..1cfd8145 100644 --- a/internal/node/labels_gate.go +++ b/internal/node/labels_gate.go @@ -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 @@ -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) } diff --git a/internal/node/mcp.go b/internal/node/mcp.go index b8e0ea5f..44250fc2 100644 --- a/internal/node/mcp.go +++ b/internal/node/mcp.go @@ -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 } diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.go index 015ea9ca..d0dd718d 100644 --- a/internal/node/mcp_handlers_test.go +++ b/internal/node/mcp_handlers_test.go @@ -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) } } @@ -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) } }