feat(qaops): 接入独立只读问答查询与治理覆盖复检
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package dobotaiops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// aclProbeSQL 只用目录/ACL 函数判断"这个连接是不是最小只读":
|
||||
// - 角色属性:SUPER/CREATEROLE/CREATEDB/BYPASSRLS/REPLICATION
|
||||
// - 库/模式级:public schema CREATE、当前库 CREATE
|
||||
// - 继承面:角色成员关系数、可 SET ROLE 到的角色数
|
||||
// - 所有权:该角色拥有的 relation / schema 数
|
||||
// - "直接 ACL 只读但有效权限可写"的风险面:可执行的 SECURITY DEFINER 函数数
|
||||
// - 四张目标表的 SELECT 与写权限
|
||||
//
|
||||
// 它不改任何东西,也不尝试任何写语句(工单明确禁止用"写后回滚"证明只读)。
|
||||
const aclProbeSQL = `
|
||||
SELECT current_user,
|
||||
COALESCE((SELECT rolsuper FROM pg_roles WHERE rolname = current_user), FALSE),
|
||||
COALESCE((SELECT rolcreaterole FROM pg_roles WHERE rolname = current_user), FALSE),
|
||||
COALESCE((SELECT rolcreatedb FROM pg_roles WHERE rolname = current_user), FALSE),
|
||||
COALESCE((SELECT rolbypassrls FROM pg_roles WHERE rolname = current_user), FALSE),
|
||||
COALESCE((SELECT rolreplication FROM pg_roles WHERE rolname = current_user), FALSE),
|
||||
has_schema_privilege(current_user, 'public', 'CREATE'),
|
||||
has_database_privilege(current_user, current_database(), 'CREATE'),
|
||||
(SELECT count(*)::int FROM pg_auth_members WHERE member = (SELECT oid FROM pg_roles WHERE rolname = current_user)),
|
||||
(SELECT count(*)::int FROM pg_roles r WHERE r.rolname <> current_user AND pg_has_role(current_user, r.oid, 'SET')),
|
||||
(SELECT count(*)::int FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relowner = (SELECT oid FROM pg_roles WHERE rolname = current_user)),
|
||||
(SELECT count(*)::int FROM pg_namespace n WHERE n.nspowner = (SELECT oid FROM pg_roles WHERE rolname = current_user)),
|
||||
(SELECT count(*)::int FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
|
||||
WHERE p.prosecdef AND n.nspname NOT IN ('pg_catalog', 'information_schema')
|
||||
AND has_function_privilege(current_user, p.oid, 'EXECUTE')),
|
||||
has_table_privilege(current_user, 'public.turns', 'SELECT'),
|
||||
has_table_privilege(current_user, 'public.turns', 'INSERT'),
|
||||
has_table_privilege(current_user, 'public.turns', 'UPDATE'),
|
||||
has_table_privilege(current_user, 'public.turns', 'DELETE'),
|
||||
has_table_privilege(current_user, 'public.turns', 'TRUNCATE'),
|
||||
has_table_privilege(current_user, 'public.messages', 'SELECT'),
|
||||
has_table_privilege(current_user, 'public.messages', 'INSERT'),
|
||||
has_table_privilege(current_user, 'public.messages', 'UPDATE'),
|
||||
has_table_privilege(current_user, 'public.messages', 'DELETE'),
|
||||
has_table_privilege(current_user, 'public.messages', 'TRUNCATE'),
|
||||
has_table_privilege(current_user, 'public.coverage_decisions', 'SELECT'),
|
||||
has_table_privilege(current_user, 'public.coverage_decisions', 'INSERT'),
|
||||
has_table_privilege(current_user, 'public.coverage_decisions', 'UPDATE'),
|
||||
has_table_privilege(current_user, 'public.coverage_decisions', 'DELETE'),
|
||||
has_table_privilege(current_user, 'public.coverage_decisions', 'TRUNCATE'),
|
||||
has_table_privilege(current_user, 'public.channel_actors', 'SELECT'),
|
||||
has_table_privilege(current_user, 'public.channel_actors', 'INSERT'),
|
||||
has_table_privilege(current_user, 'public.channel_actors', 'UPDATE'),
|
||||
has_table_privilege(current_user, 'public.channel_actors', 'DELETE'),
|
||||
has_table_privilege(current_user, 'public.channel_actors', 'TRUNCATE')`
|
||||
|
||||
// ACLReport 是自检结果,用于启动日志与运维核对(不含任何业务数据)。
|
||||
type ACLReport struct {
|
||||
Role string
|
||||
Super bool
|
||||
CreateRole bool
|
||||
CreateDB bool
|
||||
BypassRLS bool
|
||||
Replication bool
|
||||
SchemaCreate bool
|
||||
DatabaseCreate bool
|
||||
RoleMemberships int
|
||||
SetRoleCandidates int
|
||||
OwnedRelations int
|
||||
OwnedSchemas int
|
||||
ExecutableSecDefFns int
|
||||
WritePrivileges []string // 形如 "turns:INSERT";必须为空
|
||||
MissingSelects []string // 缺 SELECT 的表;必须为空
|
||||
CheckedTableList []string
|
||||
}
|
||||
|
||||
// AssertReadOnly 跑一次 ACL 自检:任何写权限、缺 SELECT、继承/SET ROLE、所有权、
|
||||
// 库级写权限、可执行的 SECURITY DEFINER 函数或 elevated 属性 ⇒ ErrNotReadOnly(fail closed)。
|
||||
func AssertReadOnly(ctx context.Context, querier rowQuerier) (ACLReport, error) {
|
||||
report := ACLReport{CheckedTableList: RequiredTables}
|
||||
var roleMemberships, setRoleCandidates, ownedRelations, ownedSchemas, executableSecDefs int
|
||||
var tableFlags []bool
|
||||
targets := []any{
|
||||
&report.Role, &report.Super, &report.CreateRole, &report.CreateDB, &report.BypassRLS,
|
||||
&report.Replication, &report.SchemaCreate, &report.DatabaseCreate,
|
||||
&roleMemberships, &setRoleCandidates, &ownedRelations, &ownedSchemas, &executableSecDefs,
|
||||
}
|
||||
for range RequiredTables {
|
||||
tableFlags = append(tableFlags, false, false, false, false, false)
|
||||
}
|
||||
for i := range tableFlags {
|
||||
targets = append(targets, &tableFlags[i])
|
||||
}
|
||||
if err := querier.QueryRow(ctx, aclProbeSQL).Scan(targets...); err != nil {
|
||||
return ACLReport{}, fmt.Errorf("%w: ACL probe failed", ErrUnavailable)
|
||||
}
|
||||
report.RoleMemberships = roleMemberships
|
||||
report.SetRoleCandidates = setRoleCandidates
|
||||
report.OwnedRelations = ownedRelations
|
||||
report.OwnedSchemas = ownedSchemas
|
||||
report.ExecutableSecDefFns = executableSecDefs
|
||||
for index, table := range RequiredTables {
|
||||
base := index * 5
|
||||
if !tableFlags[base] {
|
||||
report.MissingSelects = append(report.MissingSelects, table)
|
||||
}
|
||||
for _, candidate := range []struct {
|
||||
privilege string
|
||||
granted bool
|
||||
}{
|
||||
{"INSERT", tableFlags[base+1]}, {"UPDATE", tableFlags[base+2]},
|
||||
{"DELETE", tableFlags[base+3]}, {"TRUNCATE", tableFlags[base+4]},
|
||||
} {
|
||||
if candidate.granted {
|
||||
report.WritePrivileges = append(report.WritePrivileges, table+":"+candidate.privilege)
|
||||
}
|
||||
}
|
||||
}
|
||||
var problems []string
|
||||
if report.Super || report.CreateRole || report.CreateDB || report.BypassRLS || report.Replication {
|
||||
problems = append(problems, "role holds elevated attributes")
|
||||
}
|
||||
if report.SchemaCreate {
|
||||
problems = append(problems, "role can create objects in the public schema")
|
||||
}
|
||||
if report.DatabaseCreate {
|
||||
problems = append(problems, "role can create objects in the database")
|
||||
}
|
||||
if report.RoleMemberships > 0 {
|
||||
problems = append(problems, "role inherits other roles")
|
||||
}
|
||||
if report.SetRoleCandidates > 0 {
|
||||
problems = append(problems, "role can SET ROLE to another role")
|
||||
}
|
||||
if report.OwnedRelations > 0 || report.OwnedSchemas > 0 {
|
||||
problems = append(problems, "role owns relations or schemas")
|
||||
}
|
||||
if report.ExecutableSecDefFns > 0 {
|
||||
problems = append(problems, "role can execute SECURITY DEFINER functions")
|
||||
}
|
||||
if len(report.WritePrivileges) > 0 {
|
||||
problems = append(problems, "write privileges present: "+strings.Join(report.WritePrivileges, ", "))
|
||||
}
|
||||
if len(report.MissingSelects) > 0 {
|
||||
problems = append(problems, "missing SELECT on: "+strings.Join(report.MissingSelects, ", "))
|
||||
}
|
||||
if len(problems) > 0 {
|
||||
return report, fmt.Errorf("%w: %s", ErrNotReadOnly, strings.Join(problems, "; "))
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// rowQuerier 是本包需要的最小查询面(*pgxpool.Pool 满足)。
|
||||
type rowQuerier interface {
|
||||
QueryRow(ctx context.Context, sql string, args ...any) rowScanner
|
||||
}
|
||||
|
||||
type rowScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
Reference in New Issue
Block a user