// Package dobotaiops provides the governance backend's independent read-only // access to DobotAI production Q&A. It never runs source migrations or writes // source business data. Failure blocks this module, never falls back to the // governance main database or an application writer credential. // Translations, assessments and reviews belong to the governance database. package dobotaiops import ( "context" "errors" "fmt" "net" "time" ) var ( // ErrUnavailable:外部只读接入当前不可用(未配置、secret 缺失或不可读、 // 拨号/连接失败)。调用方应把它当成"这一块功能没开",治理自身功能继续工作。 ErrUnavailable = errors.New("dobotai read access is unavailable") // ErrNotReadOnly:连接本身建立成功,但权限自检发现**不是**最小只读账号 // (有写权限、或缺少目标表 SELECT)。这是配置/账号问题,必须 fail closed。 ErrNotReadOnly = errors.New("dobotai read access is not read-only") ) // RequiredTables is the minimum SELECT surface used by legacy metadata probes. // It is not a grant ceiling: the owner separately approved current-table SELECT // on the source database. All business queries still project fixed safe fields. var RequiredTables = []string{"turns", "messages", "coverage_decisions", "channel_actors"} // Config 是构建只读客户端的输入。所有时间/连接参数都有硬上限,防止误配把生产拖垮。 type Config struct { // DSNFile 是仓库外的 secret 文件路径(0600),文件内容是完整 DSN。 DSNFile string // GovernanceDatabaseURL 用于**拒绝**把只读接入指向治理主库(防误配/防回退)。 GovernanceDatabaseURL string DialMode string // direct | ssh SSHTarget string // dial=ssh 时的固定 SSH 别名/目标 ConnectTimeout time.Duration QueryTimeout time.Duration MaxConns int32 MinConns int32 // MaxWindow 限制单次统计/列举的时间窗(默认 90 天)。 MaxWindow time.Duration // MaxPageSize 限制单页条数(硬上限 100,与工单一致)。 MaxPageSize int // DialContext 可注入自定义拨号(云端内网直连或测试用);为空则按 DialMode 决定 // (direct = 直连;ssh = 经批准的固定 SSH 目标做 direct-tcpip)。 DialContext func(ctx context.Context, network, addr string) (net.Conn, error) Now func() time.Time } func (c Config) withDefaults() Config { if c.ConnectTimeout <= 0 { c.ConnectTimeout = 5 * time.Second } if c.QueryTimeout <= 0 { c.QueryTimeout = 4 * time.Second } if c.MaxConns <= 0 { c.MaxConns = 2 } if c.MinConns < 0 { c.MinConns = 0 } if c.MaxWindow <= 0 { c.MaxWindow = 90 * 24 * time.Hour } if c.MaxPageSize <= 0 || c.MaxPageSize > 100 { c.MaxPageSize = 100 } if c.Now == nil { c.Now = time.Now } return c } // Validate 只校验形状(不碰网络、不读 secret),便于单测与配置期快速失败。 func (c Config) Validate() error { if c.DSNFile == "" { return fmt.Errorf("%w: DSN file is required", ErrUnavailable) } switch c.DialMode { case "direct": case "ssh": if c.SSHTarget == "" { return fmt.Errorf("%w: ssh target is required when dial mode is ssh", ErrUnavailable) } default: return fmt.Errorf("%w: dial mode must be direct or ssh", ErrUnavailable) } if c.ConnectTimeout <= 0 || c.ConnectTimeout > 15*time.Second { return fmt.Errorf("%w: connect timeout out of range", ErrUnavailable) } if c.QueryTimeout <= 0 || c.QueryTimeout > 15*time.Second { return fmt.Errorf("%w: query timeout out of range", ErrUnavailable) } if c.MaxConns < 1 || c.MaxConns > 8 { return fmt.Errorf("%w: max conns out of range", ErrUnavailable) } if c.MinConns < 0 || c.MinConns > c.MaxConns { return fmt.Errorf("%w: min conns out of range", ErrUnavailable) } return nil }