Skip to content

Agent Mesh

api: reject an allowed_target naming a fact nothing derives

Antonio Ojea250f853aa4b1
diff --git a/api/validation.go b/api/validation.go
index 5306b7d2..01e6f117 100644
--- a/api/validation.go
+++ b/api/validation.go
@@ -16,6 +16,7 @@ package api
import (
"fmt"
+ "slices"
"strings"
)
@@ -133,5 +134,12 @@ func ValidateTargetFormat(target string) error {
if val == "" {
return fmt.Errorf("invalid target format %q: value cannot be empty", target)
}
+ // "*" as the fact matches every target_fact (granted_target_all_facts).
+ if fact != "*" && !slices.Contains(TargetFactNames(), fact) {
+ if fact == FactAgent {
+ return fmt.Errorf("invalid target %q: an agent cannot be a target, because a node's identity does not say which agents it hosts. Use allowed_agents to grant the agent namespaces a node may act for", target)
+ }
+ return fmt.Errorf("invalid target %q: %q is not a target fact, so nothing would ever match it (want %s or \"*\")", target, fact, strings.Join(TargetFactNames(), ", "))
+ }
return nil
}
diff --git a/api/validation_test.go b/api/validation_test.go
index acf1bf72..01dcb74a 100644
--- a/api/validation_test.go
+++ b/api/validation_test.go
@@ -61,9 +61,15 @@ func TestValidateTargetFormat(t *testing.T) {
{"valid target", "group:backend", false},
{"valid email", "email:[email protected]", false},
{"valid wildcard", "*", false},
+ {"wildcard fact and value", "*:*", false},
+ {"wildcard value for one fact", "group:*", false},
{"invalid no colon", "group-backend", true},
{"invalid empty fact", ":backend", true},
{"invalid empty value", "group:", true},
+ // A fact nothing derives would mint a grant that never matches, so it
+ // is refused at config time rather than denying silently at runtime.
+ {"unknown fact", "banana:yellow", true},
+ {"agent is not a target", "agent:reviewer-7.prod.acme.example", true},
}
for _, tt := range tests {
diff --git a/tests/integration/controlplane_pubsub_integration_test.go b/tests/integration/controlplane_pubsub_integration_test.go
index adb0fc8d..04ac9e98 100644
--- a/tests/integration/controlplane_pubsub_integration_test.go
+++ b/tests/integration/controlplane_pubsub_integration_test.go
@@ -121,7 +121,7 @@ func TestControlPlanePubSubEventIntegration(t *testing.T) {
{
Name: "developer",
AllowedServices: []string{"mcp://api"},
- AllowedTargets: []string{"tcp://db:5432"},
+ AllowedTargets: []string{"group:db-nodes"},
},
},
Bindings: []*api.PolicyBinding{