rbac

package
v0.5.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package rbac 提供 Hexagon AI Agent 框架的基于角色的访问控制

支持角色定义、权限管理、资源访问控制等功能。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithUser

func ContextWithUser(ctx context.Context, user *User) context.Context

ContextWithUser 将用户添加到 context

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 NewMemoryRBACStore

func NewMemoryRBACStore() *MemoryRBACStore

NewMemoryRBACStore 创建内存存储

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) GetPolicy

func (s *MemoryRBACStore) GetPolicy(ctx context.Context, id string) (*Policy, error)

func (*MemoryRBACStore) GetRole

func (s *MemoryRBACStore) GetRole(ctx context.Context, name string) (*Role, error)

func (*MemoryRBACStore) GetUser

func (s *MemoryRBACStore) GetUser(ctx context.Context, id string) (*User, 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

func (*MemoryRBACStore) SaveRole

func (s *MemoryRBACStore) SaveRole(ctx context.Context, role *Role) error

func (*MemoryRBACStore) SaveUser

func (s *MemoryRBACStore) SaveUser(ctx context.Context, user *User) 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 NewRBAC

func NewRBAC() *RBAC

NewRBAC 创建 RBAC 系统

func (*RBAC) AddPolicy

func (r *RBAC) AddPolicy(ctx context.Context, policy *Policy) error

AddPolicy 添加策略 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) AddRole

func (r *RBAC) AddRole(ctx context.Context, role *Role) error

AddRole 添加角色 先在内存中更新,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) AddUser

func (r *RBAC) AddUser(ctx context.Context, user *User) error

AddUser 添加用户 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) AssignRole

func (r *RBAC) AssignRole(ctx context.Context, userID, roleName string) error

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

func (r *RBAC) DeletePolicy(ctx context.Context, id string) error

DeletePolicy 删除策略 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) DeleteRole

func (r *RBAC) DeleteRole(ctx context.Context, name string) error

DeleteRole 删除角色 先在内存中删除,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) DeleteUser

func (r *RBAC) DeleteUser(ctx context.Context, id string) error

DeleteUser 删除用户 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) GetInheritedRoles

func (r *RBAC) GetInheritedRoles(roleName string) []string

GetInheritedRoles 获取继承的角色

线程安全:此方法会获取读锁。 注意:不要在已持有读锁的情况下调用此方法,否则可能导致死锁。 如果已持有锁,请使用 getInheritedRolesLocked。

func (*RBAC) GetPolicy

func (r *RBAC) GetPolicy(id string) (*Policy, bool)

GetPolicy 获取策略

func (*RBAC) GetRole

func (r *RBAC) GetRole(name string) (*Role, bool)

GetRole 获取角色

func (*RBAC) GetUser

func (r *RBAC) GetUser(id string) (*User, bool)

GetUser 获取用户

func (*RBAC) GetUserRoles

func (r *RBAC) GetUserRoles(userID string) []string

GetUserRoles 获取用户的所有角色(包括继承的)

线程安全:此方法会获取读锁,并使用内部方法避免嵌套锁导致的死锁。

func (*RBAC) ListPolicies

func (r *RBAC) ListPolicies() []*Policy

ListPolicies 列出所有策略

func (*RBAC) ListRoles

func (r *RBAC) ListRoles() []*Role

ListRoles 列出所有角色

func (*RBAC) RevokeRole

func (r *RBAC) RevokeRole(ctx context.Context, userID, roleName string) error

RevokeRole 撤销用户角色 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) SetRoleHierarchy

func (r *RBAC) SetRoleHierarchy(parent string, children []string)

SetRoleHierarchy 设置角色层级

func (*RBAC) SetStore

func (r *RBAC) SetStore(store RBACStore)

SetStore 设置存储

func (*RBAC) UpdatePolicy

func (r *RBAC) UpdatePolicy(ctx context.Context, policy *Policy) error

UpdatePolicy 更新策略 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) UpdateRole

func (r *RBAC) UpdateRole(ctx context.Context, role *Role) error

UpdateRole 更新角色 先在内存中更新,然后释放锁后再持久化到 store,避免持锁调用外部函数导致死锁。 ctx 用于控制 store 持久化操作的超时和取消。

func (*RBAC) UpdateUser

func (r *RBAC) UpdateUser(ctx context.Context, user *User) error

UpdateUser 更新用户 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

func UserFromContext(ctx context.Context) *User

UserFromContext 从 context 获取用户

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL