Documentation
¶
Overview ¶
Package sudo 提供特权操作执行模块 通过白名单机制安全地执行需要 sudo 权限的操作
Package sudo 提供特权操作执行模块
Index ¶
- Constants
- func NewModule() module.Module
- type Action
- type ActionInfo
- type AuditLog
- type ExecuteRequest
- type ExecuteResponse
- type Executor
- func (e *Executor) BuildCommand(action *Action, args []string) (string, error)
- func (e *Executor) Execute(ctx context.Context, req *ExecuteRequest, userID, username, clientIP string) (*ExecuteResponse, error)
- func (e *Executor) GetAction(id string) *Action
- func (e *Executor) GetAllActions() []*Action
- type Handler
- type Module
- func (m *Module) Dependencies() []string
- func (m *Module) GetExecutor() *Executor
- func (m *Module) ID() string
- func (m *Module) Init(ctx *module.Context) error
- func (m *Module) Name() string
- func (m *Module) RegisterRoutes(group *gin.RouterGroup)
- func (m *Module) Start() error
- func (m *Module) Stop() error
- func (m *Module) Version() string
- type PreviewRequest
- type PreviewResponse
Constants ¶
const ( ModuleID = "sudo" ModuleName = "特权执行" ModuleVersion = "1.0.0" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Action ¶
type Action struct {
ID string // 操作标识
Name string // 显示名称
Description string // 描述
Command string // 命令模板 (使用 %s 作为参数占位符)
ArgCount int // 期望的参数数量
Dangerous bool // 是否危险操作(需要额外确认)
AllowedArgs []string // 允许的参数值(为空表示不限制)
}
Action 定义允许的 sudo 操作
type ActionInfo ¶
type ActionInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
ArgCount int `json:"arg_count"`
Dangerous bool `json:"dangerous"`
}
ActionInfo 操作信息(用于前端显示)
type AuditLog ¶
type AuditLog struct {
ID uint `gorm:"primaryKey" json:"id"`
Timestamp time.Time `gorm:"autoCreateTime" json:"timestamp"`
UserID string `json:"user_id"`
Username string `json:"username"`
ActionID string `json:"action_id"`
Args string `json:"args"` // JSON 序列化的参数
Command string `json:"command"`
Success bool `json:"success"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
ExitCode int `json:"exit_code"`
Duration int64 `json:"duration_ms"`
ClientIP string `json:"client_ip"`
}
AuditLog 审计日志
type ExecuteRequest ¶
type ExecuteRequest struct {
ActionID string `json:"action_id" binding:"required"` // 操作 ID
Args []string `json:"args"` // 参数列表
Confirmed bool `json:"confirmed"` // 是否已确认(危险操作需要)
}
ExecuteRequest 执行请求
type ExecuteResponse ¶
type ExecuteResponse struct {
Success bool `json:"success"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
ExitCode int `json:"exit_code"`
Duration int64 `json:"duration_ms"` // 执行时长(毫秒)
}
ExecuteResponse 执行响应
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor sudo 执行器
func (*Executor) BuildCommand ¶
BuildCommand 构建完整命令
func (*Executor) Execute ¶
func (e *Executor) Execute(ctx context.Context, req *ExecuteRequest, userID, username, clientIP string) (*ExecuteResponse, error)
Execute 执行 sudo 操作
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler HTTP 处理器
func (*Handler) Execute ¶
Execute 执行 sudo 命令 @Summary 执行 sudo 命令 @Tags sudo @Accept json @Produce json @Param request body ExecuteRequest true "执行请求" @Success 200 {object} ExecuteResponse @Router /api/v1/sudo/execute [post]
func (*Handler) GetLogs ¶
GetLogs 获取审计日志 @Summary 获取 sudo 审计日志 @Tags sudo @Produce json @Param page query int false "页码" default(1) @Param size query int false "每页数量" default(20) @Success 200 {array} AuditLog @Router /api/v1/sudo/logs [get]
func (*Handler) ListActions ¶
ListActions 列出所有可用操作 @Summary 列出所有可用的 sudo 操作 @Tags sudo @Produce json @Success 200 {array} ActionInfo @Router /api/v1/sudo/actions [get]
func (*Handler) Preview ¶
Preview 预览将要执行的命令 @Summary 预览 sudo 命令 @Tags sudo @Accept json @Produce json @Param request body PreviewRequest true "预览请求" @Success 200 {object} PreviewResponse @Router /api/v1/sudo/preview [post]
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(group *gin.RouterGroup)
RegisterRoutes 注册路由
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module sudo 模块
func (*Module) RegisterRoutes ¶
func (m *Module) RegisterRoutes(group *gin.RouterGroup)
RegisterRoutes 注册路由
type PreviewRequest ¶
type PreviewRequest struct {
ActionID string `json:"action_id" binding:"required"`
Args []string `json:"args"`
}
PreviewRequest 预览请求(返回将要执行的命令)
type PreviewResponse ¶
type PreviewResponse struct {
Action ActionInfo `json:"action"`
Command string `json:"command"` // 将要执行的完整命令
}
PreviewResponse 预览响应