fix(agent): last-good 双失败必须告警 + 降级日志分级 + derr 落日志

loadACL 终点此前有两个问题:(1) 读磁盘 last-good 的 derr 从不落日志;(2) ALERT
只在 err != nil 时触发,但 acl.json 单纯缺失(LoadACLConfig 按设计返回 nil,nil,
不是 error)时 err 恰好是 nil——于是 acl.json 缺失 + 磁盘 last-good 同时损坏这种
"gate 实质消失"的最糟场景,反而完全没有日志。

修复:
- derr 非 nil 时打 ERROR。
- ALERT 判断改用文件是否曾经存在(os.Stat)而非 err 是否非 nil:acl.json 与
  last-good 均 not-exist → 判定"从未配置过该功能",安静返回;否则(其一存在但
  读取/解析失败)→ ALERT。
- 两条降级 fallback 日志补 WARN 级别标签,配合 ERROR/ALERT 可用
  journalctl | grep -E 'ERROR|WARN|ALERT' 一并抓到。

TDD:acl_test.go 新增三条——双失败必须 ALERT(RED)、从未配置不误报(基线即绿,
防止告警刷屏回归)、降级日志缺 WARN 标签(RED)。全部现绿。

复现细节:双失败场景没有采用"把 acl.json 和 last-good 都整个删除"来复现——那种
状态在文件系统层面与"这台节点从没配置过 ACL"完全无法区分(两次 os.Stat 皆
not-exist),任何仅凭当前文件状态判断的实现都做不出区分,要区分需要额外的持久
标记,超出本 finding 范围。改用"acl.json 缺失 + 磁盘 last-good 存在但损坏"复现,
这是一个可被 os.Stat 命中的信号,也更贴近 §5 描述的真实故障。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-23 07:39:30 +08:00
parent dbc787ac2e
commit 8e24a157e1
2 changed files with 110 additions and 3 deletions
+15 -3
View File
@@ -371,20 +371,32 @@ func (s *SingBox) loadACL() *ACLConfig {
lg := s.lastGoodACL
s.mu.Unlock()
if lg != nil {
logf("[acl] falling back to in-memory last-good ACL")
logf("[acl] WARN falling back to in-memory last-good ACL")
return lg
}
disk, derr := LoadACLConfig(s.cfg.ACLLastGoodPath())
if derr != nil {
logf("[acl] ERROR reading last-good %s: %v", s.cfg.ACLLastGoodPath(), derr)
}
if derr == nil && disk != nil {
logf("[acl] falling back to on-disk last-good %s", s.cfg.ACLLastGoodPath())
logf("[acl] WARN falling back to on-disk last-good %s", s.cfg.ACLLastGoodPath())
s.mu.Lock()
s.lastGoodACL = disk
s.mu.Unlock()
return disk
}
if err != nil {
// 两级 last-good 都没有可用配置。区分两种终态:
// - 这台节点从未配置过 ACL(acl.json 与 last-good 均从未存在过)→ 安静返回,
// 不刷屏告警。
// - 除此之外的任何情况(acl.json 存在但损坏/last-good 存在但损坏等)→ 私有
// 服务的访问闸实质已消失,必须大声告警(§5 "两者都失败才不产出 ACL 规则,
// 同时打 ERROR 并告警")。
_, aclStatErr := os.Stat(s.cfg.ACLConfigPath)
_, lgStatErr := os.Stat(s.cfg.ACLLastGoodPath())
neverConfigured := os.IsNotExist(aclStatErr) && os.IsNotExist(lgStatErr)
if !neverConfigured {
logf("[acl] ALERT acl.json is broken and no last-good snapshot exists — "+
"private destinations are UNPROTECTED (path=%s)", s.cfg.ACLConfigPath)
}