Skip to content

Agent Mesh

storage: persist a role's agent and label grants

Antonio Ojea7aa4f8707eee
diff --git a/api/sam.pb.go b/api/sam.pb.go
index ea636943..0a35c060 100644
--- a/api/sam.pb.go
+++ b/api/sam.pb.go
@@ -1305,6 +1305,10 @@ type PolicyRole struct {
// control plane attested about that node. Distinct from allowed_targets:
// being allowed to call an agent is not being allowed to impersonate it.
AllowedAgents []string `protobuf:"bytes,5,rep,name=allowed_agents,json=allowedAgents,proto3" json:"allowed_agents,omitempty"`
+ // Labels a node with this role may declare at enrollment, as "*", "key=*"
+ // or "key=value". A node declares its own labels, so this is what turns a
+ // declaration into something the control plane is willing to sign.
+ AllowedLabels []string `protobuf:"bytes,6,rep,name=allowed_labels,json=allowedLabels,proto3" json:"allowed_labels,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -1374,6 +1378,13 @@ func (x *PolicyRole) GetAllowedAgents() []string {
return nil
}
+func (x *PolicyRole) GetAllowedLabels() []string {
+ if x != nil {
+ return x.AllowedLabels
+ }
+ return nil
+}
+
type PolicyBinding struct {
state protoimpl.MessageState `protogen:"open.v1"`
Role string `protobuf:"bytes,1,opt,name=role,proto3" json:"role,omitempty"`
@@ -2761,14 +2772,15 @@ const file_api_sam_proto_rawDesc = "" +
"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" +
"\x05error\x18\x02 \x01(\tR\x05error\x12\x1d\n" +
"\n" +
- "expires_at\x18\x03 \x01(\x03R\texpiresAt\"\xc2\x01\n" +
+ "expires_at\x18\x03 \x01(\x03R\texpiresAt\"\xe9\x01\n" +
"\n" +
"PolicyRole\x12\x12\n" +
"\x04name\x18\x01 \x01(\tR\x04name\x12'\n" +
"\x0fallowed_targets\x18\x02 \x03(\tR\x0eallowedTargets\x12)\n" +
"\x10allowed_services\x18\x03 \x03(\tR\x0fallowedServices\x12%\n" +
"\x0ecustom_datalog\x18\x04 \x03(\tR\rcustomDatalog\x12%\n" +
- "\x0eallowed_agents\x18\x05 \x03(\tR\rallowedAgents\"=\n" +
+ "\x0eallowed_agents\x18\x05 \x03(\tR\rallowedAgents\x12%\n" +
+ "\x0eallowed_labels\x18\x06 \x03(\tR\rallowedLabels\"=\n" +
"\rPolicyBinding\x12\x12\n" +
"\x04role\x18\x01 \x01(\tR\x04role\x12\x18\n" +
"\amembers\x18\x02 \x03(\tR\amembers\"\x18\n" +
diff --git a/api/sam.proto b/api/sam.proto
index 4e586aad..d88e9185 100644
--- a/api/sam.proto
+++ b/api/sam.proto
@@ -182,6 +182,10 @@ message PolicyRole {
// control plane attested about that node. Distinct from allowed_targets:
// being allowed to call an agent is not being allowed to impersonate it.
repeated string allowed_agents = 5;
+ // Labels a node with this role may declare at enrollment, as "*", "key=*"
+ // or "key=value". A node declares its own labels, so this is what turns a
+ // declaration into something the control plane is willing to sign.
+ repeated string allowed_labels = 6;
}
message PolicyBinding {
diff --git a/internal/storage/mesh_policy_test.go b/internal/storage/mesh_policy_test.go
new file mode 100644
index 00000000..23808961
--- /dev/null
+++ b/internal/storage/mesh_policy_test.go
@@ -0,0 +1,79 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package storage
+
+import (
+ "context"
+ "path/filepath"
+ "reflect"
+ "testing"
+
+ "github.com/google/sam/api"
+)
+
+// TestMeshPolicyRoundTripsEveryRoleField stores a role with every repeated
+// field populated and reads it back.
+//
+// role_permissions rows are discriminated by a resource_type string, so a field
+// added to PolicyRole without a matching case here is written nowhere and read
+// back empty. Nothing fails: the policy saves, the API returns it, and the
+// grant silently does not exist. This walks the message with reflection so a
+// future field is caught here rather than in production.
+func TestMeshPolicyRoundTripsEveryRoleField(t *testing.T) {
+ store, err := NewSQLStore("sqlite", filepath.Join(t.TempDir(), "policy.db"))
+ if err != nil {
+ t.Fatalf("NewSQLStore: %v", err)
+ }
+ defer func() { _ = store.Close() }()
+
+ ctx := context.Background()
+ want := &api.PolicyRole{
+ Name: "everything",
+ AllowedTargets: []string{"group:backend"},
+ AllowedServices: []string{"mcp://tool"},
+ CustomDatalog: []string{`region("emea")`},
+ AllowedAgents: []string{"*.prod.acme.example"},
+ AllowedLabels: []string{"region=*"},
+ }
+
+ if err := store.SaveMeshPolicy(ctx, []*api.PolicyRole{want}, nil); err != nil {
+ t.Fatalf("SaveMeshPolicy: %v", err)
+ }
+
+ roles, _, err := store.GetMeshPolicy(ctx)
+ if err != nil {
+ t.Fatalf("GetMeshPolicy: %v", err)
+ }
+ if len(roles) != 1 {
+ t.Fatalf("got %d roles, want 1", len(roles))
+ }
+ got := roles[0]
+
+ // Reflection rather than a field list: a new repeated field left out of
+ // SaveMeshPolicy comes back empty and is reported here by name.
+ wantVal := reflect.ValueOf(want).Elem()
+ gotVal := reflect.ValueOf(got).Elem()
+ for i := 0; i < wantVal.NumField(); i++ {
+ field := wantVal.Type().Field(i)
+ if !field.IsExported() || field.Type.Kind() != reflect.Slice {
+ continue
+ }
+ w := wantVal.Field(i).Interface()
+ g := gotVal.Field(i).Interface()
+ if !reflect.DeepEqual(w, g) {
+ t.Errorf("field %s did not round trip: saved %v, loaded %v", field.Name, w, g)
+ }
+ }
+}
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 1eb94957..ed12b367 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -801,6 +801,16 @@ func (s *SQLStore) SaveMeshPolicy(ctx context.Context, roles []*api.PolicyRole,
return err
}
}
+ for _, agent := range r.AllowedAgents {
+ if _, err := tx.ExecContext(ctx, s.rebind("INSERT INTO role_permissions (role_name, resource_type, resource_value) VALUES (?, 'agent', ?)"), r.Name, agent); err != nil {
+ return err
+ }
+ }
+ for _, label := range r.AllowedLabels {
+ if _, err := tx.ExecContext(ctx, s.rebind("INSERT INTO role_permissions (role_name, resource_type, resource_value) VALUES (?, 'label', ?)"), r.Name, label); err != nil {
+ return err
+ }
+ }
}
for _, b := range bindings {
@@ -860,6 +870,10 @@ func (s *SQLStore) GetMeshPolicy(ctx context.Context) ([]*api.PolicyRole, []*api
r.AllowedServices = append(r.AllowedServices, resValue)
case "custom_datalog":
r.CustomDatalog = append(r.CustomDatalog, resValue)
+ case "agent":
+ r.AllowedAgents = append(r.AllowedAgents, resValue)
+ case "label":
+ r.AllowedLabels = append(r.AllowedLabels, resValue)
}
}
}