refactor(ws/config): accept agent_key or UUID for permission handlers

All 3 handlers (handleList, handleGrant, handleRevoke) now accept either
a UUID or an agent_key for params.agentId via resolveAgentUUIDCached.

Adds ConfigPermissionsMethods.SetAgentRouter setter wired from
cmd/gateway_methods.go (already staged in previous commit).

Phase 3 of agent identity hardening.
This commit is contained in:
viettranx committed 2026-04-11 21:22:23 +07:00
1 parent 84babc1c3c
commit 21a9076298
1 file changed
+12 -6
+12 -6
View File
@@ -5,7 +5,7 @@ import (
"encoding/json"
"log/slog"
"github.com/google/uuid"
"github.com/nextlevelbuilder/goclaw/internal/agent"
"github.com/nextlevelbuilder/goclaw/internal/gateway"
"github.com/nextlevelbuilder/goclaw/internal/i18n"
"github.com/nextlevelbuilder/goclaw/internal/store"
@@ -14,14 +14,20 @@ import (
// ConfigPermissionsMethods handles config.permissions.* RPC methods.
type ConfigPermissionsMethods struct {
permStore store.ConfigPermissionStore
agentStore store.AgentStore
permStore store.ConfigPermissionStore
agentStore store.AgentStore
agentRouter *agent.Router // cache-aware agent resolver; nil = DB-only fallback
}
func NewConfigPermissionsMethods(ps store.ConfigPermissionStore, as store.AgentStore) *ConfigPermissionsMethods {
return &ConfigPermissionsMethods{permStore: ps, agentStore: as}
}
// SetAgentRouter wires the agent router for cache-aware agent_key resolution.
func (m *ConfigPermissionsMethods) SetAgentRouter(r *agent.Router) {
m.agentRouter = r
}
func (m *ConfigPermissionsMethods) Register(router *gateway.MethodRouter) {
router.Register(protocol.MethodConfigPermissionsList, m.handleList)
router.Register(protocol.MethodConfigPermissionsGrant, m.handleGrant)
@@ -42,7 +48,7 @@ func (m *ConfigPermissionsMethods) handleList(ctx context.Context, client *gatew
return
}
agentUUID, err := uuid.Parse(params.AgentID)
agentUUID, err := resolveAgentUUIDCached(ctx, m.agentRouter, m.agentStore, params.AgentID)
if err != nil {
client.SendResponse(protocol.NewErrorResponse(req.ID, protocol.ErrInvalidRequest, "invalid agentId"))
return
@@ -90,7 +96,7 @@ func (m *ConfigPermissionsMethods) handleGrant(ctx context.Context, client *gate
return
}
agentUUID, err := uuid.Parse(params.AgentID)
agentUUID, err := resolveAgentUUIDCached(ctx, m.agentRouter, m.agentStore, params.AgentID)
if err != nil {
client.SendResponse(protocol.NewErrorResponse(req.ID, protocol.ErrInvalidRequest, "invalid agentId"))
return
@@ -149,7 +155,7 @@ func (m *ConfigPermissionsMethods) handleRevoke(ctx context.Context, client *gat
return
}
agentUUID, err := uuid.Parse(params.AgentID)
agentUUID, err := resolveAgentUUIDCached(ctx, m.agentRouter, m.agentStore, params.AgentID)
if err != nil {
client.SendResponse(protocol.NewErrorResponse(req.ID, protocol.ErrInvalidRequest, "invalid agentId"))
return