sudo

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package sudo 提供特权操作执行模块 通过白名单机制安全地执行需要 sudo 权限的操作

Package sudo 提供特权操作执行模块

Index

Constants

View Source
const (
	ModuleID      = "sudo"
	ModuleName    = "特权执行"
	ModuleVersion = "1.0.0"
)

Variables

This section is empty.

Functions

func NewModule

func NewModule() module.Module

NewModule 创建模块实例

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 审计日志

func (AuditLog) TableName

func (AuditLog) TableName() string

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 NewExecutor

func NewExecutor(logger *zap.Logger, db *gorm.DB) *Executor

NewExecutor 创建执行器

func (*Executor) BuildCommand

func (e *Executor) BuildCommand(action *Action, args []string) (string, error)

BuildCommand 构建完整命令

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, req *ExecuteRequest, userID, username, clientIP string) (*ExecuteResponse, error)

Execute 执行 sudo 操作

func (*Executor) GetAction

func (e *Executor) GetAction(id string) *Action

GetAction 获取操作定义

func (*Executor) GetAllActions

func (e *Executor) GetAllActions() []*Action

GetAllActions 获取所有操作

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler HTTP 处理器

func NewHandler

func NewHandler(executor *Executor, logger *zap.Logger) *Handler

NewHandler 创建处理器

func (*Handler) Execute

func (h *Handler) Execute(c *gin.Context)

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

func (h *Handler) GetLogs(c *gin.Context)

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

func (h *Handler) ListActions(c *gin.Context)

ListActions 列出所有可用操作 @Summary 列出所有可用的 sudo 操作 @Tags sudo @Produce json @Success 200 {array} ActionInfo @Router /api/v1/sudo/actions [get]

func (*Handler) Preview

func (h *Handler) Preview(c *gin.Context)

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

func (m *Module) Dependencies() []string

Dependencies 返回依赖模块

func (*Module) GetExecutor

func (m *Module) GetExecutor() *Executor

GetExecutor 获取执行器(供其他模块调用)

func (*Module) ID

func (m *Module) ID() string

ID 返回模块 ID

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init 初始化模块

func (*Module) Name

func (m *Module) Name() string

Name 返回模块名称

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(group *gin.RouterGroup)

RegisterRoutes 注册路由

func (*Module) Start

func (m *Module) Start() error

Start 启动模块

func (*Module) Stop

func (m *Module) Stop() error

Stop 停止模块

func (*Module) Version

func (m *Module) Version() string

Version 返回模块版本

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 预览响应

Jump to

Keyboard shortcuts

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