mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-06 01:07:35 +00:00
refactor(security): Store 复用 Codec 加密能力 + 删 globalCodec 单例
## 用户裁定
> store也应该调用security的加密能力!
## 改动前的事实(实测)
两套**完全重复**的 AES-256-GCM:
Store.seal/open store.go:372,388 裸 base64
Codec.Seal/Open codec.go:113,139 enc:v1: 前缀 + 域隔离 + 幂等
算法、nonce 布局(前置 + io.ReadFull(rand))、密钥环轮换逻辑全都一样,
只有 Codec 多了前缀与域隔离。
危害不是"代码重复"这么轻:
· 两套实现各自演化,任何一侧的改动不会被另一侧的判据发现
· Store 缺域隔离 ⇒ 变量库与配置共用密钥(安全倒退)
· Store 缺前缀 ⇒ 无法区分"旧密文"与"明文"(惰性迁移的障碍)
## 本次做的
### Store 委托 Codec
codecOf() 按当前 keys 环惰性构建(轮换后自动跟上)
sealWithRing() 新格式(enc:v1: 前缀)
openWithStoreRing() 兼容新旧两种格式,返回 needsMigrate
删除 Store 自带的 seal/open/openWithRing —— 现在**只有一处** AES-GCM。
### 域隔离(安全判据)
storeCodecDomain = "security/variables"
configCodecDomain = "config-at-rest"
两者必须不同:同域时一处明文泄漏可推导另一处密文。
### 存量密文惰性迁移
旧格式是裸 base64 无前缀,而 Codec.Open 对无前缀输入**原样返回明文**
(那是给 config 的存量明文兼容)。直接交给 Open 会把密文当明文返回,
症状是"凭据回填出一个 base64 串"。
所以 Store 走 openWithStoreRing:
enc:v1:... → Codec.Open
裸 base64 → Codec.OpenLegacy(解开后立刻回写新格式)
不做启动时全量迁移(21G 数据目录的教训:迁移窗口越长越危险),
也不让两种格式永久共存(判据永不能要求"落盘都是新格式")。
### 删 globalCodec 单例 → 归 Runtime
security 模块 → rt.SetCodec(c) → ks.Security
config 模块 → cfgReg.SetCodec(ks.Security.Codec())
ConfigRegistry 加 codec 字段(注入而非读全局),6 处 ConfigCodec() 改为
r.codecOf();PluginSettings 经 p.registry 转调。
sdk.SecretsAPI 也加 codec 参数(插件加密能力来自它的内核实例)。
### 顺带
· InitConfigCodec* / ClearConfigCodec 删除,改 NewConfigCodec 返回实例
· homed-key-rotate 改为 构造 + reg.SetCodec(不再装全局)
· bootstrap_order_test 的锚点从 InitConfigCodecWithOldKeys 改为
NewConfigCodec(守的不变量不变,只是生效点写法变了)
## 判据(均经变异验证)
TestNoPackageLevelSingletons 禁表补入 globalCodec
TestStoreWritesPrefixedCiphertext 新密文必须带 enc:v1:(旧实现裸 base64 ⇒ 红)
TestStoreAndConfigUseDifferentDomains 域隔离;改成同域 ⇒ 红(实测确认)
TestLegacyCiphertextStillReadableAndMigrates 旧格式可读 + 落盘已升级
TestRotationKeepsOldCiphertextReadable 轮换后旧条目仍可回填
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l 干净
go test ./... -count=1 63 包全过,0 FAIL
干净检出(git worktree) build + security/config 测试全绿
## 数据风险
加密实现切换有丢数据风险,因此:
· 先写往返判据,再改实现
· 惰性迁移(读到才升级),不做启动全量重写
· 旧格式与旧密钥环**都保留**读取路径
This commit is contained in:
@ -17,6 +17,7 @@ package proc
|
||||
// 内核侧修好了接口」这类错位(本次实际踩到过一次)。
|
||||
|
||||
import (
|
||||
"github.com/JianFeeeee/HologramAgent/internal/security"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -74,8 +75,8 @@ func TestE2E_RealTemplateSecretRoundTrip(t *testing.T) {
|
||||
|
||||
// 用**真实** ConfigRegistry(真加密)+ 真主密钥
|
||||
dir := t.TempDir()
|
||||
setTestCodec(t)
|
||||
reg := internalConfig.NewConfigRegistry(filepath.Join(dir, "config.db"))
|
||||
reg.SetCodec(setTestCodec(t))
|
||||
defer reg.Close()
|
||||
|
||||
host, err := NewHost()
|
||||
@ -131,8 +132,10 @@ func TestE2E_RealTemplateSecretRoundTrip(t *testing.T) {
|
||||
_ = pubsdk.ConfigDef{}
|
||||
}
|
||||
|
||||
// setTestCodec 装配一把测试主密钥。
|
||||
func setTestCodec(t *testing.T) {
|
||||
// setTestCodec 造一把测试主密钥的 codec(由调用方注入 Registry)。
|
||||
//
|
||||
// ★ 2026-11-04:包级单例已删,不能"装一次全局就到处生效"。
|
||||
func setTestCodec(t *testing.T) *security.Codec {
|
||||
t.Helper()
|
||||
internalConfig.ResetTestCodec(t)
|
||||
return internalConfig.NewTestCodec(t)
|
||||
}
|
||||
|
||||
@ -80,7 +80,6 @@ func TestE2E_RealTemplateSecretsRoundTrip(t *testing.T) {
|
||||
bin := buildPluginWithRealTemplate(t, secretsE2EPlugin)
|
||||
|
||||
dir := t.TempDir()
|
||||
setTestCodec(t)
|
||||
// 变量库(2026-11-04:不再有进程级单例,改为显式持有并注入)
|
||||
store, err := internalSecurity.MustStore(internalSecurity.Options{
|
||||
Path: filepath.Join(dir, "variables.json"),
|
||||
@ -99,7 +98,7 @@ func TestE2E_RealTemplateSecretsRoundTrip(t *testing.T) {
|
||||
defer host.Close()
|
||||
|
||||
core := newFakeCore()
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(store)
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(store, nil)
|
||||
p := New("e2e", bin, dir, nil, host, nil)
|
||||
if err := p.Start(core); err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
@ -140,7 +139,6 @@ func TestE2E_PluginAndKernelShareOneVault(t *testing.T) {
|
||||
bin := buildPluginWithRealTemplate(t, secretsE2EPlugin)
|
||||
|
||||
dir := t.TempDir()
|
||||
setTestCodec(t)
|
||||
st, err := internalSecurity.MustStore(internalSecurity.Options{
|
||||
Path: filepath.Join(dir, "variables.json"),
|
||||
KeyFn: func() ([]byte, error) { return []byte("0123456789abcdef0123456789abcdef"), nil },
|
||||
@ -153,7 +151,7 @@ func TestE2E_PluginAndKernelShareOneVault(t *testing.T) {
|
||||
host, _ := NewHost()
|
||||
defer host.Close()
|
||||
core := newFakeCore()
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(st)
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(st, nil)
|
||||
p := New("e2e", bin, dir, nil, host, nil)
|
||||
if err := p.Start(core); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -193,7 +191,7 @@ func TestE2E_SecretsUnavailableWhenDisabled(t *testing.T) {
|
||||
defer host.Close()
|
||||
core := newFakeCore()
|
||||
// 未启用安全模块 ⇒ 传 nil(Available() 必须为 false,且不得 panic)
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(nil)
|
||||
core.secretsAPI = internalSDK.NewSecretsAPI(nil, nil)
|
||||
p := New("e2e", bin, dir, nil, host, nil)
|
||||
if err := p.Start(core); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@ -73,7 +73,6 @@ func TestE2E_RealTemplateSecurityDeclare(t *testing.T) {
|
||||
bin := buildPluginWithRealTemplate(t, securityE2EPlugin)
|
||||
|
||||
dir := t.TempDir()
|
||||
setTestCodec(t)
|
||||
|
||||
// ★ 2026-11-04:包级单例已删。Runtime 显式构造并**注入**给插件 SDK
|
||||
// (生产路径同款:kstate.State.Security → plugin.Registry.SetSecurityRuntime)。
|
||||
|
||||
@ -346,6 +346,14 @@ func (r restartOnly) RequestKernelShutdown(reason string) error {
|
||||
//
|
||||
// (那是反向依赖,且 cmd 包不可被 import)。用接口 + setter 打破方向。
|
||||
//
|
||||
// codecOf 从 Runtime 取配置加密器(Runtime 可 nil)。
|
||||
func codecOf(rt *security.Runtime) *security.Codec {
|
||||
if rt == nil {
|
||||
return nil
|
||||
}
|
||||
return rt.Codec()
|
||||
}
|
||||
|
||||
// SetSecurityRuntime 注入安全运行时(plugin-host 模块装配时调用)。
|
||||
func (r *Registry) SetSecurityRuntime(rt *security.Runtime) {
|
||||
r.mu.Lock()
|
||||
@ -793,7 +801,7 @@ func (r *Registry) buildSDK(name string) *sdk.PluginSDK {
|
||||
Settings: sett,
|
||||
// ★ 变量库:桥接到**注入的** Store(不另建存储)。
|
||||
// 内核未启用安全模块时为 nil ⇒ Available()==false(不是 nil API)。
|
||||
Secrets: sdk.NewSecretsAPI(deps.secretStore),
|
||||
Secrets: sdk.NewSecretsAPI(deps.secretStore, codecOf(deps.securityRT)),
|
||||
SecurityRuntime: deps.securityRT,
|
||||
// ★ 策略声明面:插件**声明**档位(qq.owner / qq.guest…),
|
||||
// 判定仍在内核 security.Runtime。Owner 由内核用真实插件名填,
|
||||
|
||||
Reference in New Issue
Block a user