CI / test (push) Successful in 10m29s
A tool is seen where every level above that names tools names it (store Effective.AllowsTool). The console and /admin/mcp wrote a level's list as given, so a team that saw its organization's hr.* through having no list was given jira.* and then saw neither; and revoking a level's last server left [] in the store, closed to every tool, shown nowhere. store.GrantTools and RevokeTools now do both screens' work: a level's first list starts from the nearest list above it; taking a server away leaves the others, and a level left naming nothing goes back to no list, or stays closed when a level above names tools, so it opens to nothing it was kept from. GrantTools says which levels above still keep the granted tools out of reach, and both screens say so. The page shows closed levels and member scopes by name, and offers no revoke for a * grant it would not change. The console calls the store instead of keys limits with a list built from what it had loaded, which overwrote changes made since. Found in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
4.6 KiB
Go
160 lines
4.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// Granting one MCP server's tools at a level of the tree, and taking them
|
|
// back, as the console and the admin page do. A tool is usable only where
|
|
// every level above that names tools names it too (Effective.AllowsTool),
|
|
// which makes adding a list where there was none a narrowing: a team that
|
|
// saw its organization's jira.* through having no list of its own would see
|
|
// only hr.* once given hr.*. So a level given its first list starts from
|
|
// the nearest list above it, and keeps what it had.
|
|
|
|
// ToolGrant is what granting came to: the levels above whose lists do not
|
|
// name the tools granted, so that they stay out of reach until those grant
|
|
// them too.
|
|
type ToolGrant struct {
|
|
BlockedBy []LimitOwner
|
|
}
|
|
|
|
// GrantTools makes a level's grants of server's tools these, keeping its
|
|
// grants of other servers' and the rest of its limits.
|
|
func (s *DB) GrantTools(ctx context.Context, scope Scope, id, server string, tools []string) (ToolGrant, error) {
|
|
above, err := s.limitsAbove(ctx, scope, id)
|
|
if err != nil {
|
|
return ToolGrant{}, err
|
|
}
|
|
lim, err := s.LimitsFor(ctx, scope, id)
|
|
switch {
|
|
case errors.Is(err, ErrNotFound):
|
|
lim = Limits{Scope: scope, ScopeID: id}
|
|
case err != nil:
|
|
return ToolGrant{}, err
|
|
}
|
|
if lim.AllowedTools == nil {
|
|
// Its first list: what it saw through the levels above, kept.
|
|
for _, a := range above {
|
|
if a.AllowedTools != nil {
|
|
lim.AllowedTools = slices.Clone(a.AllowedTools)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
keep := withoutServer(lim.AllowedTools, server)
|
|
for _, t := range tools {
|
|
if !slices.Contains(keep, t) {
|
|
keep = append(keep, t)
|
|
}
|
|
}
|
|
lim.AllowedTools = keep
|
|
if err := s.PutLimits(ctx, lim); err != nil {
|
|
return ToolGrant{}, err
|
|
}
|
|
var g ToolGrant
|
|
for _, a := range above {
|
|
if a.AllowedTools == nil {
|
|
continue
|
|
}
|
|
for _, t := range tools {
|
|
if !matchesTool(a.AllowedTools, sampleTool(t)) {
|
|
g.BlockedBy = append(g.BlockedBy, LimitOwner{Scope: a.Scope, ID: a.ScopeID})
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// RevokeTools takes a level's grants of server's tools away, keeping the
|
|
// rest. A level left naming no tools no longer names any — it then sees
|
|
// what the levels above let it — unless a level above names tools: then
|
|
// it is left naming none, closed to every tool, so that taking one
|
|
// server's tools away does not open it to the others above.
|
|
func (s *DB) RevokeTools(ctx context.Context, scope Scope, id, server string) error {
|
|
lim, err := s.LimitsFor(ctx, scope, id)
|
|
if errors.Is(err, ErrNotFound) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lim.AllowedTools = withoutServer(lim.AllowedTools, server)
|
|
if len(lim.AllowedTools) == 0 {
|
|
above, err := s.limitsAbove(ctx, scope, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lim.AllowedTools = nil
|
|
for _, a := range above {
|
|
if a.AllowedTools != nil {
|
|
lim.AllowedTools = []string{}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return s.PutLimits(ctx, lim)
|
|
}
|
|
|
|
func withoutServer(tools []string, server string) []string {
|
|
return slices.DeleteFunc(slices.Clone(tools), func(t string) bool { return strings.HasPrefix(t, server+".") })
|
|
}
|
|
|
|
// sampleTool is a tool a pattern grants, for asking whether a list above
|
|
// grants it too: jira.* stands for any of jira's.
|
|
func sampleTool(pattern string) string {
|
|
if before, ok := strings.CutSuffix(pattern, "*"); ok {
|
|
return before + "x"
|
|
}
|
|
return pattern
|
|
}
|
|
|
|
// limitsAbove are the limits of the levels above one, nearest first: a
|
|
// project's team and organization, a team's organization, a key's project
|
|
// and those above it. None for an organization or a person.
|
|
func (s *DB) limitsAbove(ctx context.Context, scope Scope, id string) ([]Limits, error) {
|
|
var owners []LimitOwner
|
|
if scope == ScopeKey {
|
|
k, err := s.KeyByID(ctx, id)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
scope, id = ScopeProject, k.ProjectID
|
|
owners = append(owners, LimitOwner{Scope: ScopeProject, ID: id})
|
|
}
|
|
if scope == ScopeTeam || scope == ScopeProject {
|
|
tree, err := s.Tree(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, o := range tree {
|
|
for _, t := range o.Teams {
|
|
if scope == ScopeTeam && t.ID == id {
|
|
owners = append(owners, LimitOwner{Scope: ScopeOrg, ID: o.ID})
|
|
}
|
|
for _, p := range t.Projects {
|
|
if scope == ScopeProject && p.ID == id {
|
|
owners = append(owners, LimitOwner{Scope: ScopeTeam, ID: t.ID}, LimitOwner{Scope: ScopeOrg, ID: o.ID})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var out []Limits
|
|
for _, o := range owners {
|
|
l, err := s.LimitsFor(ctx, o.Scope, o.ID)
|
|
if errors.Is(err, ErrNotFound) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
return out, nil
|
|
}
|