feat(security): LLM api_key 端到端加密(AES-256-GCM,磁盘+线缆均密文)
新增 sundynix-shared/secrets:AES-256-GCM,密钥由 SUNDYNIX_SECRET_KEY 经 SHA-256 派生;密文带 enc:1: 版本前缀,历史明文行自动透传(下次保存升级为密文)。 - 网关 SaveModel 加密落库;ListModels/TestModel 解密后脱敏/探测; 空或脱敏占位的 api_key 视为「未改」→ 沿用库内既有密文(不二次加密)。 - 密文经 NATS 原样下发;消费方解密集中在 bus 层 decryptConfig (RequestConfig + SubscribeConfigUpdated)→ dispatcher/mcp-go 零改动。 - secrets.MustHaveKeyInProd():生产未设 SUNDYNIX_SECRET_KEY 直接 fatal, gateway/dispatcher/mcp-go 启动各调一次(三服务须配相同密钥)。 - 修复 store.SaveModel 整行 Save 把 active 清零的旧 bug:改 Select(...).Updates 只覆盖可编辑列,改 key 不再顺手取消模型激活。 - secrets 单测:往返/空串/随机 nonce/错密钥 fail-closed/历史明文透传。 - production_readiness.md 2.1 更新为「已落地」。 验证:DB 列由明文 sk-…(35) → 密文 enc:1:…(90);dispatcher 从密文广播解密后 model config set;真实任务 √256→16、12×12→144 打通 DeepSeek(非降级桩)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,8 +12,20 @@ import (
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/sundynix/sundynix-shared/contract"
|
||||
"github.com/sundynix/sundynix-shared/secrets"
|
||||
)
|
||||
|
||||
// decryptConfig 在消费侧把配置里的 api_key 从密文还原为明文(控制面以密文过线缆,见 secrets 包)。
|
||||
// 失败(密钥不匹配 / 密文损坏)时清空 api_key 并不再降级阻断——调用方据 Ready() 判定。
|
||||
func decryptConfig(cfg *contract.ModelConfig) {
|
||||
if cfg == nil || cfg.APIKey == "" {
|
||||
return
|
||||
}
|
||||
if plain, err := secrets.Decrypt(cfg.APIKey); err == nil {
|
||||
cfg.APIKey = plain
|
||||
}
|
||||
}
|
||||
|
||||
// Bus 持有 NATS 连接与 JetStream 上下文。
|
||||
type Bus struct {
|
||||
nc *nats.Conn
|
||||
@@ -281,6 +293,7 @@ func (b *Bus) RequestConfig(ctx context.Context, kind string) (*contract.ModelCo
|
||||
if !cfg.Ready() {
|
||||
return nil, nil
|
||||
}
|
||||
decryptConfig(&cfg) // 线缆上是密文,消费侧还原
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -333,6 +346,7 @@ func (b *Bus) SubscribeConfigUpdated(kind string, onUpdate func(*contract.ModelC
|
||||
sub, err := b.nc.Subscribe(contract.ConfigUpdatedSubject(kind), func(m *nats.Msg) {
|
||||
var cfg contract.ModelConfig
|
||||
if json.Unmarshal(m.Data, &cfg) == nil {
|
||||
decryptConfig(&cfg) // 线缆上是密文,消费侧还原
|
||||
onUpdate(&cfg)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user