Documentation
¶
Overview ¶
Package rbac 提供 Hexagon AI Agent 框架的基于角色的访问控制
支持角色定义、权限管理、资源访问控制等功能。
Index ¶
- func ContextWithUser(ctx context.Context, user *User) context.Context
- type AccessRequest
- type AccessResult
- type ConditionOperator
- type MemoryRBACStore
- func (s *MemoryRBACStore) DeletePolicy(ctx context.Context, id string) error
- func (s *MemoryRBACStore) DeleteRole(ctx context.Context, name string) error
- func (s *MemoryRBACStore) DeleteUser(ctx context.Context, id string) error
- func (s *MemoryRBACStore) GetPolicy(ctx context.Context, id string) (*Policy, error)
- func (s *MemoryRBACStore) GetRole(ctx context.Context, name string) (*Role, error)
- func (s *MemoryRBACStore) GetUser(ctx context.Context, id string) (*User, error)
- func (s *MemoryRBACStore) ListPolicies(ctx context.Context) ([]*Policy, error)
- func (s *MemoryRBACStore) ListRoles(ctx context.Context) ([]*Role, error)
- func (s *MemoryRBACStore) SavePolicy(ctx context.Context, policy *Policy) error
- func (s *MemoryRBACStore) SaveRole(ctx context.Context, role *Role) error
- func (s *MemoryRBACStore) SaveUser(ctx context.Context, user *User) error
- type Permission
- type Policy
- type PolicyCondition
- type PolicyEffect
- type RBAC
- func (r *RBAC) AddPolicy(ctx context.Context, policy *Policy) error
- func (r *RBAC) AddRole(ctx context.Context, role *Role) error
- func (r *RBAC) AddUser(ctx context.Context, user *User) error
- func (r *RBAC) AssignRole(ctx context.Context, userID, roleName string) error
- func (r *RBAC) Authorize(req AccessRequest) AccessResult
- func (r *RBAC) AuthorizeFromContext(ctx context.Context, resource, action string) AccessResult
- func (r *RBAC) DeletePolicy(ctx context.Context, id string) error
- func (r *RBAC) DeleteRole(ctx context.Context, name string) error
- func (r *RBAC) DeleteUser(ctx context.Context, id string) error
- func (r *RBAC) GetInheritedRoles(roleName string) []string
- func (r *RBAC) GetPolicy(id string) (*Policy, bool)
- func (r *RBAC) GetRole(name string) (*Role, bool)
- func (r *RBAC) GetUser(id string) (*User, bool)
- func (r *RBAC) GetUserRoles(userID string) []string
- func (r *RBAC) ListPolicies() []*Policy
- func (r *RBAC) ListRoles() []*Role
- func (r *RBAC) RevokeRole(ctx context.Context, userID, roleName string) error
- func (r *RBAC) SetRoleHierarchy(parent string, children []string)
- func (r *RBAC) SetStore(store RBACStore)
- func (r *RBAC) UpdatePolicy(ctx context.Context, policy *Policy) error
- func (r *RBAC) UpdateRole(ctx context.Context, role *Role) error
- func (r *RBAC) UpdateUser(ctx context.Context, user *User) error
- type RBACStore
- type Role
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AccessRequest ¶
type AccessRequest struct {
// Subject 主体(用户/Agent ID)
Subject string
// Resource 资源
Resource string
// Action 操作
Action string
// Context 上下文
Context map[string]any
}
AccessRequest 访问请求
type AccessResult ¶
type AccessResult struct {
// Allowed 是否允许
Allowed bool
// Reason 原因
Reason string
// MatchedPolicy 匹配的策略
MatchedPolicy string
// MatchedPermission 匹配的权限
MatchedPermission *Permission
}
AccessResult 访问结果
type ConditionOperator ¶
type ConditionOperator string
ConditionOperator 条件操作符
const ( OpEquals ConditionOperator = "equals" OpNotEquals ConditionOperator = "not_equals" OpContains ConditionOperator = "contains" OpStartsWith ConditionOperator = "starts_with" OpEndsWith ConditionOperator = "ends_with" OpMatches ConditionOperator = "matches" // 正则匹配 OpIn ConditionOperator = "in" OpNotIn ConditionOperator = "not_in" OpGreaterThan ConditionOperator = "greater_than" OpLessThan ConditionOperator = "less_than" )
type MemoryRBACStore ¶
type MemoryRBACStore struct {
// contains filtered or unexported fields
}
MemoryRBACStore 内存存储
func (*MemoryRBACStore) DeletePolicy ¶
func (s *MemoryRBACStore) DeletePolicy(ctx context.Context, id string) error
func (*MemoryRBACStore) DeleteRole ¶
func (s *MemoryRBACStore) DeleteRole(ctx context.Context, name string) error
func (*MemoryRBACStore) DeleteUser ¶
func (s *MemoryRBACStore) DeleteUser(ctx context.Context, id string) error
func (*MemoryRBACStore) ListPolicies ¶
func (s *MemoryRBACStore) ListPolicies(ctx context.Context) ([]*Policy, error)
func (*MemoryRBACStore) ListRoles ¶
func (s *MemoryRBACStore) ListRoles(ctx context.Context) ([]*Role, error)
func (*MemoryRBACStore) SavePolicy ¶
func (s *MemoryRBACStore) SavePolicy(ctx context.Context, policy *Policy) error
type Permission ¶
type Permission struct {
// Resource 资源(支持通配符 *)
Resource string `json:"resource" yaml:"resource"`
// Action 操作(支持通配符 *)
Action string `json:"action" yaml:"action"`
// Conditions 条件
Conditions map[string]any `json:"conditions,omitempty" yaml:"conditions,omitempty"`
}
Permission 权限
type Policy ¶
type Policy struct {
// ID 策略 ID
ID string `json:"id" yaml:"id"`
// Name 策略名称
Name string `json:"name" yaml:"name"`
// Description 描述
Description string `json:"description" yaml:"description"`
// Effect 效果
Effect PolicyEffect `json:"effect" yaml:"effect"`
// Subjects 主体
Subjects []string `json:"subjects" yaml:"subjects"`
// Resources 资源
Resources []string `json:"resources" yaml:"resources"`
// Actions 操作
Actions []string `json:"actions" yaml:"actions"`
// Conditions 条件
Conditions []PolicyCondition `json:"conditions,omitempty" yaml:"conditions,omitempty"`
// Priority 优先级(越大越高)
Priority int `json:"priority" yaml:"priority"`
// Enabled 是否启用
Enabled bool `json:"enabled" yaml:"enabled"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at" yaml:"created_at"`
}
Policy 策略
type PolicyCondition ¶
type PolicyCondition struct {
// Key 条件键
Key string `json:"key" yaml:"key"`
// Operator 操作符
Operator ConditionOperator `json:"operator" yaml:"operator"`
// Value 值
Value any `json:"value" yaml:"value"`
}
PolicyCondition 策略条件
type PolicyEffect ¶
type PolicyEffect string
PolicyEffect 策略效果
const ( EffectAllow PolicyEffect = "allow" EffectDeny PolicyEffect = "deny" )
type RBAC ¶
type RBAC struct {
// contains filtered or unexported fields
}
RBAC 角色权限控制系统
func (*RBAC) AddRole ¶
AddRole 添加角色 先在内存中更新,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) AssignRole ¶
AssignRole 分配角色给用户 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) Authorize ¶
func (r *RBAC) Authorize(req AccessRequest) AccessResult
Authorize 授权检查
安全说明:此方法要求 Subject 必须是已验证的用户 ID。 调用者应该通过 AuthorizeFromContext 方法进行授权检查, 该方法会从 context 中获取已验证的用户身份。
func (*RBAC) AuthorizeFromContext ¶
func (r *RBAC) AuthorizeFromContext(ctx context.Context, resource, action string) AccessResult
AuthorizeFromContext 从 context 授权检查
func (*RBAC) DeletePolicy ¶
DeletePolicy 删除策略 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) DeleteRole ¶
DeleteRole 删除角色 先在内存中删除,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) DeleteUser ¶
DeleteUser 删除用户 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) GetInheritedRoles ¶
GetInheritedRoles 获取继承的角色
线程安全:此方法会获取读锁。 注意:不要在已持有读锁的情况下调用此方法,否则可能导致死锁。 如果已持有锁,请使用 getInheritedRolesLocked。
func (*RBAC) RevokeRole ¶
RevokeRole 撤销用户角色 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) SetRoleHierarchy ¶
SetRoleHierarchy 设置角色层级
func (*RBAC) UpdatePolicy ¶
UpdatePolicy 更新策略 ctx 用于控制 store 持久化操作的超时和取消。
func (*RBAC) UpdateRole ¶
UpdateRole 更新角色 先在内存中更新,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。
type RBACStore ¶
type RBACStore interface {
SaveRole(ctx context.Context, role *Role) error
GetRole(ctx context.Context, name string) (*Role, error)
DeleteRole(ctx context.Context, name string) error
ListRoles(ctx context.Context) ([]*Role, error)
SaveUser(ctx context.Context, user *User) error
GetUser(ctx context.Context, id string) (*User, error)
DeleteUser(ctx context.Context, id string) error
SavePolicy(ctx context.Context, policy *Policy) error
GetPolicy(ctx context.Context, id string) (*Policy, error)
DeletePolicy(ctx context.Context, id string) error
ListPolicies(ctx context.Context) ([]*Policy, error)
}
RBACStore RBAC 存储接口
type Role ¶
type Role struct {
// Name 角色名称(唯一标识)
Name string `json:"name" yaml:"name"`
// DisplayName 显示名称
DisplayName string `json:"display_name" yaml:"display_name"`
// Description 描述
Description string `json:"description" yaml:"description"`
// Permissions 权限列表
Permissions []Permission `json:"permissions" yaml:"permissions"`
// Metadata 元数据
Metadata map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at" yaml:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `json:"updated_at" yaml:"updated_at"`
}
Role 角色
type User ¶
type User struct {
// ID 用户 ID
ID string `json:"id" yaml:"id"`
// Name 用户名
Name string `json:"name" yaml:"name"`
// Roles 角色列表
Roles []string `json:"roles" yaml:"roles"`
// Attributes 用户属性
Attributes map[string]any `json:"attributes,omitempty" yaml:"attributes,omitempty"`
// Enabled 是否启用
Enabled bool `json:"enabled" yaml:"enabled"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at" yaml:"created_at"`
// LastActiveAt 最后活跃时间
LastActiveAt time.Time `json:"last_active_at" yaml:"last_active_at"`
}
User 用户
func UserFromContext ¶
UserFromContext 从 context 获取用户