|
| 1 | +// Copyright 2026 The OpenAgent Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package object |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/the-open-agent/openagent/guard" |
| 22 | + "github.com/the-open-agent/openagent/util" |
| 23 | + "xorm.io/core" |
| 24 | +) |
| 25 | + |
| 26 | +// ToolPolicy is a single tool-permission rule. It is the DB-backed source of |
| 27 | +// the loosely-coupled guard engine: rows are translated into guard.Rule at |
| 28 | +// enforcement time. Rules are scoped to a store (agent); an empty or "*" Store |
| 29 | +// applies the rule to every store owned by the same owner. |
| 30 | +type ToolPolicy struct { |
| 31 | + Owner string `xorm:"varchar(100) notnull pk" json:"owner"` |
| 32 | + Name string `xorm:"varchar(100) notnull pk" json:"name"` |
| 33 | + CreatedTime string `xorm:"varchar(100)" json:"createdTime"` |
| 34 | + |
| 35 | + DisplayName string `xorm:"varchar(100)" json:"displayName"` |
| 36 | + Store string `xorm:"varchar(100) index" json:"store"` |
| 37 | + |
| 38 | + // Match patterns; "" or "*" means "any". Tool/Resource accept globs (*, ?). |
| 39 | + Subject string `xorm:"varchar(100)" json:"subject"` |
| 40 | + Tool string `xorm:"varchar(200)" json:"tool"` |
| 41 | + Category string `xorm:"varchar(100)" json:"category"` |
| 42 | + Resource string `xorm:"varchar(500)" json:"resource"` |
| 43 | + |
| 44 | + // Effect is one of "allow" / "ask" / "deny". |
| 45 | + Effect string `xorm:"varchar(100)" json:"effect"` |
| 46 | + Priority int `json:"priority"` |
| 47 | + |
| 48 | + State string `xorm:"varchar(100)" json:"state"` |
| 49 | +} |
| 50 | + |
| 51 | +// normalize canonicalizes and validates a policy before it is persisted, so the |
| 52 | +// DB never holds an ambiguous effect/state. Effect is lowercased and MUST be one |
| 53 | +// of allow/ask/deny (empty or unknown is rejected — the engine treats unknowns |
| 54 | +// as deny, so a mislabeled rule must not slip in silently). An empty state |
| 55 | +// defaults to "Active". |
| 56 | +func (p *ToolPolicy) normalize() error { |
| 57 | + p.Effect = strings.ToLower(strings.TrimSpace(p.Effect)) |
| 58 | + switch guard.Effect(p.Effect) { |
| 59 | + case guard.EffectAllow, guard.EffectAsk, guard.EffectDeny: |
| 60 | + default: |
| 61 | + return fmt.Errorf("invalid effect %q: must be allow, ask or deny", p.Effect) |
| 62 | + } |
| 63 | + if p.State == "" { |
| 64 | + p.State = "Active" |
| 65 | + } |
| 66 | + if p.State != "Active" && p.State != "Disabled" { |
| 67 | + return fmt.Errorf("invalid state %q: must be Active or Disabled", p.State) |
| 68 | + } |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func GetToolPolicies(owner string) ([]*ToolPolicy, error) { |
| 73 | + policies := []*ToolPolicy{} |
| 74 | + err := adapter.engine.Desc("priority").Desc("created_time").Find(&policies, &ToolPolicy{Owner: owner}) |
| 75 | + return policies, err |
| 76 | +} |
| 77 | + |
| 78 | +func getToolPolicy(owner string, name string) (*ToolPolicy, error) { |
| 79 | + p := ToolPolicy{Owner: owner, Name: name} |
| 80 | + existed, err := adapter.engine.Get(&p) |
| 81 | + if err != nil { |
| 82 | + return &p, err |
| 83 | + } |
| 84 | + if existed { |
| 85 | + return &p, nil |
| 86 | + } |
| 87 | + return nil, nil |
| 88 | +} |
| 89 | + |
| 90 | +func GetToolPolicy(id string) (*ToolPolicy, error) { |
| 91 | + owner, name, err := util.GetOwnerAndNameFromIdWithError(id) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return getToolPolicy(owner, name) |
| 96 | +} |
| 97 | + |
| 98 | +func GetToolPolicyCount(owner, field, value string) (int64, error) { |
| 99 | + session := GetDbSession(owner, -1, -1, field, value, "", "") |
| 100 | + return session.Count(&ToolPolicy{}) |
| 101 | +} |
| 102 | + |
| 103 | +func GetPaginationToolPolicies(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*ToolPolicy, error) { |
| 104 | + policies := []*ToolPolicy{} |
| 105 | + session := GetDbSession(owner, offset, limit, field, value, sortField, sortOrder) |
| 106 | + err := session.Find(&policies) |
| 107 | + return policies, err |
| 108 | +} |
| 109 | + |
| 110 | +func UpdateToolPolicy(id string, p *ToolPolicy) (bool, error) { |
| 111 | + owner, name, err := util.GetOwnerAndNameFromIdWithError(id) |
| 112 | + if err != nil { |
| 113 | + return false, err |
| 114 | + } |
| 115 | + policyDb, err := getToolPolicy(owner, name) |
| 116 | + if err != nil { |
| 117 | + return false, err |
| 118 | + } |
| 119 | + if p == nil || policyDb == nil { |
| 120 | + return false, nil |
| 121 | + } |
| 122 | + if err := p.normalize(); err != nil { |
| 123 | + return false, err |
| 124 | + } |
| 125 | + |
| 126 | + _, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(p) |
| 127 | + if err != nil { |
| 128 | + return false, err |
| 129 | + } |
| 130 | + return true, nil |
| 131 | +} |
| 132 | + |
| 133 | +func AddToolPolicy(p *ToolPolicy) (bool, error) { |
| 134 | + if err := p.normalize(); err != nil { |
| 135 | + return false, err |
| 136 | + } |
| 137 | + affected, err := adapter.engine.Insert(p) |
| 138 | + if err != nil { |
| 139 | + return false, err |
| 140 | + } |
| 141 | + return affected != 0, nil |
| 142 | +} |
| 143 | + |
| 144 | +func DeleteToolPolicy(p *ToolPolicy) (bool, error) { |
| 145 | + affected, err := adapter.engine.ID(core.PK{p.Owner, p.Name}).Delete(&ToolPolicy{}) |
| 146 | + if err != nil { |
| 147 | + return false, err |
| 148 | + } |
| 149 | + return affected != 0, nil |
| 150 | +} |
0 commit comments