Skip to content

Agent Mesh

node: enforce the operator's egress label floor

HosniBelfekif983da566560
diff --git a/internal/node/config.go b/internal/node/config.go
index 8d34ce87..ee60b0e5 100644
--- a/internal/node/config.go
+++ b/internal/node/config.go
@@ -31,6 +31,11 @@ type NodeConfigComplete struct {
Rules []biscuit.Rule
Services []api.ServiceConfig
Labels map[string]string
+
+ // EgressRequireLabels is the operator's egress floor (api.Egress). Nil
+ // means no floor, so a caller's requirement — or the absence of one —
+ // stands on its own, as it always has.
+ EgressRequireLabels map[string]string
}
// LoadNodeConfig loads the node configuration from the specified path.
@@ -75,6 +80,19 @@ func CompleteNodeConfig(config api.NodeConfig) (*NodeConfigComplete, error) {
Labels: config.Labels,
}
+ // Rejected at load, not at first use: a floor that cannot compile would
+ // otherwise fail open on the request that needed it, and an operator who
+ // wrote one is entitled to find out at startup instead.
+ if len(config.Egress.RequireLabels) > 0 {
+ if err := api.ValidateLabels(config.Egress.RequireLabels); err != nil {
+ return nil, fmt.Errorf("invalid egress.require_labels: %w", err)
+ }
+ if _, err := api.LabelFloorCheck(config.Egress.RequireLabels); err != nil {
+ return nil, fmt.Errorf("invalid egress.require_labels: %w", err)
+ }
+ complete.EgressRequireLabels = config.Egress.RequireLabels
+ }
+
for i, svc := range config.Services {
if err := api.ValidateServiceFormat(svc.Type + "://" + svc.Name); err != nil {
return nil, fmt.Errorf("invalid service config at index %d: %w", i, err)
diff --git a/internal/node/config_test.go b/internal/node/config_test.go
index a1cf687b..3b9190fb 100644
--- a/internal/node/config_test.go
+++ b/internal/node/config_test.go
@@ -336,3 +336,64 @@ func TestCompleteNodeConfig(t *testing.T) {
t.Fatal("CompleteNodeConfig() with invalid Datalog: want error, got nil")
}
}
+
+func TestLoadNodeConfigEgressFloor(t *testing.T) {
+ write := func(t *testing.T, body string) string {
+ t.Helper()
+ path := filepath.Join(t.TempDir(), "sam-node.yaml")
+ if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ return path
+ }
+
+ t.Run("floor is loaded", func(t *testing.T) {
+ cfg, err := LoadNodeConfig(write(t, `
+version: v1alpha1
+egress:
+ require_labels:
+ jurisdiction: eu
+ compliance: gdpr
+`))
+ if err != nil {
+ t.Fatalf("LoadNodeConfig: %v", err)
+ }
+ if len(cfg.EgressRequireLabels) != 2 ||
+ cfg.EgressRequireLabels["jurisdiction"] != "eu" ||
+ cfg.EgressRequireLabels["compliance"] != "gdpr" {
+ t.Errorf("EgressRequireLabels = %v", cfg.EgressRequireLabels)
+ }
+ })
+
+ t.Run("absent block means no floor", func(t *testing.T) {
+ cfg, err := LoadNodeConfig(write(t, "version: v1alpha1\n"))
+ if err != nil {
+ t.Fatalf("LoadNodeConfig: %v", err)
+ }
+ if cfg.EgressRequireLabels != nil {
+ t.Errorf("no egress block must leave the floor nil, got %v", cfg.EgressRequireLabels)
+ }
+ })
+
+ // Rejected at load: a floor that cannot compile would otherwise fail open
+ // on the first request that needed it.
+ t.Run("a malformed floor fails startup", func(t *testing.T) {
+ for _, body := range []string{
+ "version: v1alpha1\negress:\n require_labels:\n \"bad key!\": eu\n",
+ "version: v1alpha1\negress:\n require_labels:\n jurisdiction: \"has,comma\"\n",
+ "version: v1alpha1\negress:\n require_labels:\n jurisdiction: \"\"\n",
+ } {
+ if _, err := LoadNodeConfig(write(t, body)); err == nil {
+ t.Errorf("expected a load error for %q", body)
+ }
+ }
+ })
+
+ // The schema is strict, so a typo in the block name is refused rather than
+ // silently leaving the node with no floor.
+ t.Run("a misspelled key is refused, not ignored", func(t *testing.T) {
+ if _, err := LoadNodeConfig(write(t, "version: v1alpha1\negress:\n required_labels:\n jurisdiction: eu\n")); err == nil {
+ t.Error("require_labels misspelled as required_labels must fail the strict schema")
+ }
+ })
+}
diff --git a/internal/node/labels_gate.go b/internal/node/labels_gate.go
index 3c19efd0..f509aaea 100644
--- a/internal/node/labels_gate.go
+++ b/internal/node/labels_gate.go
@@ -89,29 +89,65 @@ const (
labelGateDialTimeout = 10 * time.Second
)
-// labelGateKey builds a deterministic cache key from a required label set.
-func labelGateKey(peerID peer.ID, required map[string]string) string {
- keys := make([]string, 0, len(required))
- for k := range required {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- key := peerID.String()
- for _, k := range keys {
- key += "|" + k + "=" + required[k]
- }
- return key
+// labelGateKey builds a deterministic cache key from a required label set and
+// the floor in force. The floor is part of the key because a verdict reached
+// under one floor says nothing about another: keying on the caller's
+// requirement alone would let a cached pass survive a configuration change.
+//
+// The encoding is unambiguous, and it is worth saying why, because the
+// separators are ordinary printable characters that a label value is allowed
+// to contain. What rules a collision out is that "=" is not: every entry is
+// "|<key>=<value>", ValidateLabelKey admits only [a-zA-Z0-9_.-] so a key
+// cannot hold a separator, and ValidateLabelValue rejects "=" so a value
+// cannot forge the "|<key>=" that starts an entry. A value may therefore
+// contain "|" or "#floor" freely without shifting where any boundary falls,
+// and no pair of distinct (required, floor) inputs can render the same string.
+func labelGateKey(peerID peer.ID, required, floor map[string]string) string {
+ render := func(sb *strings.Builder, m map[string]string) {
+ keys := make([]string, 0, len(m))
+ for k := range m {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ for _, k := range keys {
+ sb.WriteByte('|')
+ sb.WriteString(k)
+ sb.WriteByte('=')
+ sb.WriteString(m[k])
+ }
+ }
+ var sb strings.Builder
+ sb.WriteString(peerID.String())
+ render(&sb, required)
+ sb.WriteString("#floor")
+ render(&sb, floor)
+ return sb.String()
+}
+
+// egressFloor returns the operator's egress floor, or nil when none is
+// configured (api.Egress.RequireLabels).
+func (n *SamNode) egressFloor() map[string]string {
+ // Nil-safe for the same reason labels() is: NewSamNode always leaves
+ // nodeConfig set, but tests build SamNode directly.
+ if n.nodeConfig == nil {
+ return nil
+ }
+ return n.nodeConfig.EgressRequireLabels
}
// VerifyPeerLabels ensures the peer holds control-plane-attested labels
-// satisfying any of the required key=value pairs (canonical, pre-validated).
-// A nil/empty requirement passes without network traffic. Positive verdicts
-// are cached.
+// satisfying the caller's requirement (any one pair) and the operator's egress
+// floor (every pair). Positive verdicts are cached.
+//
+// The gate runs whenever either exists. A caller that requires nothing is
+// still held to the floor — the whole point of a floor is that the party being
+// constrained does not get to opt out of it by saying nothing.
func (n *SamNode) VerifyPeerLabels(ctx context.Context, peerID peer.ID, required map[string]string) error {
- if len(required) == 0 {
+ floor := n.egressFloor()
+ if len(required) == 0 && len(floor) == 0 {
return nil
}
- key := labelGateKey(peerID, required)
+ key := labelGateKey(peerID, required, floor)
if until, ok := n.peerLabelGate.Get(key); ok && time.Now().Before(until) {
return nil
}
@@ -129,11 +165,12 @@ func (n *SamNode) VerifyPeerLabels(ctx context.Context, peerID peer.ID, required
}
// checkPeerLabels verifies the provider's biscuit (control-plane signature,
-// expiry, binding to peerID) and evaluates the compiled label requirement
-// against its attested facts.
+// expiry, binding to peerID) and evaluates the caller's requirement and the
+// operator's egress floor against its attested facts.
func (n *SamNode) checkPeerLabels(providerBiscuit []byte, peerID peer.ID, required map[string]string) error {
+ floor := n.egressFloor()
if len(providerBiscuit) == 0 {
- return fmt.Errorf("provider %s returned no identity biscuit; cannot attest required labels %v", peerID, required)
+ return fmt.Errorf("provider %s returned no identity biscuit; cannot attest required labels %v (egress floor %v)", peerID, required, floor)
}
n.keysMu.RLock()
@@ -155,13 +192,29 @@ func (n *SamNode) checkPeerLabels(providerBiscuit []byte, peerID peer.ID, requir
if err != nil {
return fmt.Errorf("provider %s authorizer instantiation failed: %w", peerID, err)
}
- check, err := api.LabelCheck(required)
- if err != nil {
- return err
+ // Two checks, not one merged set. The authorizer ANDs them, so the peer
+ // has to satisfy both independently; merging them into a single
+ // disjunction would let the caller's pairs stand in for the floor's and
+ // widen exactly what the floor exists to bound.
+ if len(required) > 0 {
+ check, err := api.LabelCheck(required)
+ if err != nil {
+ return err
+ }
+ authorizer.AddCheck(check)
+ }
+ if len(floor) > 0 {
+ check, err := api.LabelFloorCheck(floor)
+ if err != nil {
+ return err
+ }
+ authorizer.AddCheck(check)
}
- authorizer.AddCheck(check)
authorizer.AddPolicy(api.AllowIfTruePolicy)
if err := authorizer.Authorize(); err != nil {
+ if len(floor) > 0 {
+ return fmt.Errorf("provider %s does not attest the caller requirement %v and the egress floor %v: %w", peerID, required, floor, err)
+ }
return fmt.Errorf("provider %s has no attested label matching %v: %w", peerID, required, err)
}
return nil
diff --git a/internal/node/labels_gate_test.go b/internal/node/labels_gate_test.go
index 972283e6..31c0c5f5 100644
--- a/internal/node/labels_gate_test.go
+++ b/internal/node/labels_gate_test.go
@@ -15,12 +15,14 @@
package node
import (
+ "context"
"crypto/ed25519"
"testing"
"time"
"github.com/google/sam/api"
"github.com/google/sam/internal/identity"
+ lru "github.com/hashicorp/golang-lru/v2"
"github.com/libp2p/go-libp2p/core/peer"
)
@@ -86,3 +88,177 @@ func TestCheckPeerLabels(t *testing.T) {
}
})
}
+
+// The egress floor is the operator's, and a caller cannot waive it by asking
+// for nothing or by asking for something else. It is ANDed with the caller's
+// requirement, so both hold independently: the caller may narrow the choice of
+// provider, never widen it past the floor.
+func TestCheckPeerLabelsEnforcesEgressFloor(t *testing.T) {
+ cpPub, cpPriv, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ providerPeer := peer.ID("provider-peer-id")
+ expiry := time.Now().Add(time.Hour)
+
+ mint := func(labels map[string]string) []byte {
+ t.Helper()
+ b, err := identity.MintBootstrapBiscuitToken(cpPriv, providerPeer, api.RoleNode, expiry, nil, labels)
+ if err != nil {
+ t.Fatalf("mint: %v", err)
+ }
+ return b
+ }
+
+ nodeWithFloor := func(floor map[string]string) *SamNode {
+ return &SamNode{
+ trustedKeys: []TrustedKey{{Key: cpPub, ReceivedAt: time.Now()}},
+ BiscuitTimeout: 500 * time.Millisecond,
+ nodeConfig: &NodeConfigComplete{EgressRequireLabels: floor},
+ }
+ }
+
+ euGdpr := map[string]string{"jurisdiction": "eu", "compliance": "gdpr"}
+
+ tests := []struct {
+ name string
+ floor map[string]string
+ required map[string]string
+ attested map[string]string
+ expectErr bool
+ }{{
+ name: "caller requires nothing but the floor still applies",
+ floor: map[string]string{"jurisdiction": "eu"},
+ required: nil,
+ attested: map[string]string{"jurisdiction": "eu"},
+ }, {
+ name: "caller requires nothing and the provider is outside the floor",
+ floor: map[string]string{"jurisdiction": "eu"},
+ required: nil,
+ attested: map[string]string{"jurisdiction": "us"},
+ expectErr: true,
+ }, {
+ name: "the floor is a conjunction: one pair short is not enough",
+ floor: euGdpr,
+ required: nil,
+ attested: map[string]string{"jurisdiction": "eu"},
+ expectErr: true,
+ }, {
+ name: "the floor is satisfied when every pair is attested",
+ floor: euGdpr,
+ required: nil,
+ attested: map[string]string{"jurisdiction": "eu", "compliance": "gdpr", "region": "de"},
+ }, {
+ // The whole reason the two are separate checks. Merged into one
+ // disjunction, region=us-east-1 alone would satisfy it.
+ name: "a caller cannot widen the floor by naming another label",
+ floor: map[string]string{"jurisdiction": "eu"},
+ required: map[string]string{"region": "us-east-1"},
+ attested: map[string]string{"jurisdiction": "us", "region": "us-east-1"},
+ expectErr: true,
+ }, {
+ name: "caller and floor both satisfied",
+ floor: map[string]string{"jurisdiction": "eu"},
+ required: map[string]string{"region": "de-txl"},
+ attested: map[string]string{"jurisdiction": "eu", "region": "de-txl"},
+ }, {
+ name: "caller narrows within the floor and the provider misses the narrowing",
+ floor: map[string]string{"jurisdiction": "eu"},
+ required: map[string]string{"region": "de-txl"},
+ attested: map[string]string{"jurisdiction": "eu", "region": "fr-par"},
+ expectErr: true,
+ }, {
+ name: "no floor configured leaves the caller's requirement alone",
+ floor: nil,
+ required: map[string]string{"region": "eu"},
+ attested: map[string]string{"region": "eu"},
+ expectErr: false,
+ }}
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := nodeWithFloor(tt.floor).checkPeerLabels(mint(tt.attested), providerPeer, tt.required)
+ if tt.expectErr && err == nil {
+ t.Errorf("expected rejection, got nil")
+ }
+ if !tt.expectErr && err != nil {
+ t.Errorf("expected acceptance, got %v", err)
+ }
+ })
+ }
+}
+
+// VerifyPeerLabels short-circuits when there is nothing to check. With a floor
+// configured there always is, so the short-circuit must not swallow it: the
+// gate has to run even for a caller that asked for nothing, which is the
+// difference between a floor and a suggestion.
+func TestVerifyPeerLabelsDoesNotShortCircuitPastTheFloor(t *testing.T) {
+ node := &SamNode{
+ BiscuitTimeout: 500 * time.Millisecond,
+ nodeConfig: &NodeConfigComplete{EgressRequireLabels: map[string]string{"jurisdiction": "eu"}},
+ }
+ cache, err := lru.New[string, time.Time](8)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node.peerLabelGate = cache
+
+ // No identity, so the biscuit fetch cannot succeed. Reaching that failure
+ // is the proof the gate ran at all; returning nil would mean it skipped.
+ err = node.VerifyPeerLabels(context.Background(), peer.ID("some-peer"), nil)
+ if err == nil {
+ t.Fatal("a caller requiring nothing must still be gated when a floor is configured")
+ }
+
+ // Without a floor, the same call is the documented no-op.
+ node.nodeConfig = &NodeConfigComplete{}
+ if err := node.VerifyPeerLabels(context.Background(), peer.ID("some-peer"), nil); err != nil {
+ t.Errorf("with no floor and no requirement the gate must not run: %v", err)
+ }
+}
+
+// The cache key has to separate the caller's requirement from the floor, or a
+// pass recorded under one floor could be replayed under another.
+func TestLabelGateKeySeparatesFloorFromRequirement(t *testing.T) {
+ p := peer.ID("peer")
+ a := labelGateKey(p, map[string]string{"jurisdiction": "eu"}, nil)
+ b := labelGateKey(p, nil, map[string]string{"jurisdiction": "eu"})
+ if a == b {
+ t.Errorf("a requirement and a floor with the same pair must not share a cache key (%q)", a)
+ }
+}
+
+// The key's separators are printable characters a label value is allowed to
+// contain, so the encoding's unambiguity rests on ValidateLabelValue rejecting
+// "=": a value can hold "|" or "#floor" but cannot forge the "|<key>=" that
+// begins an entry. These are the shapes that would collide if it could.
+func TestLabelGateKeyIsUnambiguousWithSeparatorsInValues(t *testing.T) {
+ p := peer.ID("peer")
+ // Every value here is valid input: the validator rejects only a comma, an
+ // equals sign, and the three whitespace control characters.
+ for _, v := range []string{"eu|x", "eu#floor", "#floor|b", "a|b#floor|c"} {
+ if err := api.ValidateLabelValue(v); err != nil {
+ t.Fatalf("test premise wrong, %q is not a valid label value: %v", v, err)
+ }
+ }
+
+ seen := map[string][]string{}
+ add := func(desc string, required, floor map[string]string) {
+ k := labelGateKey(p, required, floor)
+ seen[k] = append(seen[k], desc)
+ }
+ add("value carrying the entry separator", map[string]string{"a": "eu|x"}, nil)
+ add("two entries", map[string]string{"a": "eu", "x": "1"}, nil)
+ add("value carrying the floor marker", map[string]string{"a": "eu#floor"}, nil)
+ add("floor holding the same pair", nil, map[string]string{"a": "eu"})
+ add("requirement holding the same pair", map[string]string{"a": "eu"}, nil)
+ add("value that looks like a floor section", map[string]string{"a": "#floor|b"}, nil)
+ add("split across both sets", map[string]string{"a": "eu"}, map[string]string{"b": "1"})
+ add("both sets, values with separators", map[string]string{"a": "a|b#floor|c"}, map[string]string{"b": "1"})
+
+ for key, descs := range seen {
+ if len(descs) > 1 {
+ t.Errorf("these distinct inputs share cache key %q: %v", key, descs)
+ }
+ }
+}
diff --git a/internal/node/openai_facade.go b/internal/node/openai_facade.go
index 481533ad..a00eb112 100644
--- a/internal/node/openai_facade.go
+++ b/internal/node/openai_facade.go
@@ -81,6 +81,11 @@ type openAIFacade struct {
// peerLabels resolves a peer's gossip-observed labels for providers
// discovered via the registry probe, which carries no labels.
peerLabels func(peerID string) map[string]string
+ // egressFloor (may be nil) is the operator's egress floor. Remotes are
+ // held to it by the label gate, on attested facts; a local service has no
+ // biscuit and no gate, so ranking is the only place its floor can be
+ // applied at all.
+ egressFloor func() map[string]string
// Label gate seam (may be nil = enforcement unavailable, fail closed
// when a requirement exists): verifies the provider's
// control-plane-attested labels before any request data is sent
@@ -141,6 +146,7 @@ func newOpenAIFacade(node *SamNode, egress http.Handler) *openAIFacade {
return node.revokedPeers != nil && node.revokedPeers.Contains(peerID)
},
localLabels: node.labels,
+ egressFloor: node.egressFloor,
peerLabels: func(peerID string) map[string]string {
if node.Discovery == nil {
return nil
diff --git a/internal/node/openai_facade_test.go b/internal/node/openai_facade_test.go
index 748b2047..956c4caf 100644
--- a/internal/node/openai_facade_test.go
+++ b/internal/node/openai_facade_test.go
@@ -823,3 +823,76 @@ func TestAttemptWriter(t *testing.T) {
}
})
}
+
+// A local service has no biscuit, so the label gate can never speak for it and
+// ranking is the only place an egress floor can apply. A remote is filtered
+// here only on claims that already contradict the floor; an unlabelled one is
+// left for the gate, which decides on attested facts.
+func TestRankProvidersEnforcesEgressFloor(t *testing.T) {
+ floor := map[string]string{"jurisdiction": "eu", "compliance": "gdpr"}
+
+ newFacadeWithFloor := func(local map[string]string, peer map[string]string) *openAIFacade {
+ f := newTestFacade()
+ f.egressFloor = func() map[string]string { return floor }
+ f.localLabels = func() map[string]string { return local }
+ f.peerLabels = func(string) map[string]string { return peer }
+ return f
+ }
+
+ t.Run("a local outside the floor is dropped", func(t *testing.T) {
+ f := newFacadeWithFloor(map[string]string{"jurisdiction": "us"}, nil)
+ got := f.rankProviders([]modelProvider{{service: "local"}}, nil)
+ if len(got) != 0 {
+ t.Errorf("a local that does not satisfy the floor must not be used: got %+v", got)
+ }
+ })
+
+ t.Run("a local one pair short is dropped", func(t *testing.T) {
+ f := newFacadeWithFloor(map[string]string{"jurisdiction": "eu"}, nil)
+ if got := f.rankProviders([]modelProvider{{service: "local"}}, nil); len(got) != 0 {
+ t.Errorf("the floor is a conjunction: got %+v", got)
+ }
+ })
+
+ t.Run("a local satisfying every pair survives", func(t *testing.T) {
+ f := newFacadeWithFloor(map[string]string{"jurisdiction": "eu", "compliance": "gdpr"}, nil)
+ if got := f.rankProviders([]modelProvider{{service: "local"}}, nil); len(got) != 1 {
+ t.Errorf("a local inside the floor must be usable: got %+v", got)
+ }
+ })
+
+ t.Run("a remote whose claims contradict the floor is dropped early", func(t *testing.T) {
+ f := newFacadeWithFloor(nil, nil)
+ p := modelProvider{peerID: "peerUS", service: "srv", labels: map[string]string{"jurisdiction": "us"}}
+ if got := f.rankProviders([]modelProvider{p}, nil); len(got) != 0 {
+ t.Errorf("a remote claiming outside the floor need not be dialled: got %+v", got)
+ }
+ })
+
+ // Gossip carries only part of what a peer attests, so a remote silent on
+ // one pair of the floor may still satisfy all of it in its Biscuit.
+ // Dropping it here would exclude a provider that is inside the boundary.
+ t.Run("a remote gossiping only part of the floor is left to the gate", func(t *testing.T) {
+ f := newFacadeWithFloor(nil, nil)
+ p := modelProvider{peerID: "peerEU", service: "srv", labels: map[string]string{"jurisdiction": "eu"}}
+ if got := f.rankProviders([]modelProvider{p}, nil); len(got) != 1 {
+ t.Errorf("silence on a pair is not a contradiction; the gate decides: got %+v", got)
+ }
+ })
+
+ t.Run("an unlabelled remote is left to the gate", func(t *testing.T) {
+ f := newFacadeWithFloor(nil, nil)
+ p := modelProvider{peerID: "peerUnknown", service: "srv"}
+ if got := f.rankProviders([]modelProvider{p}, nil); len(got) != 1 {
+ t.Errorf("ranking must not reject on absent claims; the gate decides: got %+v", got)
+ }
+ })
+
+ t.Run("no floor configured leaves ranking unchanged", func(t *testing.T) {
+ f := newTestFacade()
+ f.localLabels = func() map[string]string { return map[string]string{"jurisdiction": "us"} }
+ if got := f.rankProviders([]modelProvider{{service: "local"}}, nil); len(got) != 1 {
+ t.Errorf("without a floor a local is unconstrained: got %+v", got)
+ }
+ })
+}
diff --git a/internal/node/openai_scorer.go b/internal/node/openai_scorer.go
index b7a34561..055cee7d 100644
--- a/internal/node/openai_scorer.go
+++ b/internal/node/openai_scorer.go
@@ -18,6 +18,8 @@ import (
"net/http"
"sort"
"time"
+
+ "github.com/google/sam/api"
)
// providerBackoff is how long a provider is skipped after a retryable failure.
@@ -25,14 +27,27 @@ const providerBackoff = 15 * time.Second
// Rejection reasons for facade provider filtering (metric label values).
const (
- reasonPeerRevoked = "peer_revoked"
- reasonProviderBackoff = "provider_backoff"
- reasonLabelMismatch = "label_mismatch"
- reasonLabelUnattested = "label_unattested"
- reasonNoEligible = "no_eligible_provider"
- reasonAttemptsExceeded = "attempts_exceeded"
+ reasonPeerRevoked = "peer_revoked"
+ reasonProviderBackoff = "provider_backoff"
+ reasonLabelMismatch = "label_mismatch"
+ // reasonEgressFloorMismatch is kept distinct from reasonLabelMismatch so
+ // an operator can tell their own floor apart from a caller's requirement
+ // when a request finds no provider.
+ reasonEgressFloorMismatch = "egress_floor_mismatch"
+ reasonLabelUnattested = "label_unattested"
+ reasonNoEligible = "no_eligible_provider"
+ reasonAttemptsExceeded = "attempts_exceeded"
)
+// floor returns the operator's egress floor, or nil when the seam is unset
+// (tests) or no floor is configured.
+func (f *openAIFacade) floor() map[string]string {
+ if f.egressFloor == nil {
+ return nil
+ }
+ return f.egressFloor()
+}
+
// labelsAllowed reports whether a provider's claimed labels satisfy any
// required key=value pair (exact match).
func labelsAllowed(required, claimed map[string]string) bool {
@@ -71,6 +86,30 @@ func (f *openAIFacade) rankProviders(providers []modelProvider, requiredLabels m
continue
}
}
+ // The operator's egress floor, which the caller cannot waive. Every
+ // pair must hold, so this is not labelsAllowed. A remote is dropped
+ // here only on a claim that already contradicts the floor; the gate
+ // still decides on attested facts. A local is decided here for good,
+ // because it has no biscuit to attest anything.
+ if floor := f.floor(); len(floor) > 0 {
+ // A local is settled here: its labels are its own configuration,
+ // it has no Biscuit, and it never reaches the gate, so the floor
+ // must hold in full.
+ //
+ // A remote is only skipped on a claim that conflicts. Gossip may
+ // carry part of what a peer attests, so silence on a pair of the
+ // floor is not failure — the gate resolves it on attested facts.
+ var outside bool
+ if p.peerID == "" {
+ outside = !api.LabelsSatisfyFloor(floor, labels)
+ } else {
+ outside = api.LabelsContradictFloor(floor, labels)
+ }
+ if outside {
+ recordFacadeRejection(reasonEgressFloorMismatch)
+ continue
+ }
+ }
if p.peerID == "" {
locals = append(locals, p)
continue