feat(security): add AES-256-GCM encryption for API keys at rest
- Add crypto utility (internal/utils/crypto.go) with AES-256-GCM encrypt/decrypt using SYSTEM_AES_KEY env var, with "enc:v1:" prefix for versioned ciphertext - Encrypt tenant API key via GORM BeforeSave/AfterFind hooks and manual encryption in CreateTenant/UpdateAPIKey (db.Updates bypasses hooks) - Encrypt model API key in ModelParameters Value/Scan (driver.Valuer) - Widen api_key column from varchar(64) to varchar(256) across all DB dialects (MySQL, ParadeDB, SQLite) and add versioned migration 000018 - Propagate SYSTEM_AES_KEY through docker-compose, Helm secrets and values - Fix migration 000017 PL/pgSQL dollar-quoting syntax ($ -> $$)
This commit is contained in:
+19
-3
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -94,12 +95,19 @@ type Model struct {
|
||||
DeletedAt gorm.DeletedAt `yaml:"deleted_at" json:"deleted_at" gorm:"index"`
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert ModelParameters to database value
|
||||
// Value implements the driver.Valuer interface, used to convert ModelParameters to database value.
|
||||
// Encrypts APIKey before persisting to database (value receiver = no memory pollution).
|
||||
func (c ModelParameters) Value() (driver.Value, error) {
|
||||
if key := utils.GetAESKey(); key != nil && c.APIKey != "" {
|
||||
if encrypted, err := utils.EncryptAESGCM(c.APIKey, key); err == nil {
|
||||
c.APIKey = encrypted
|
||||
}
|
||||
}
|
||||
return json.Marshal(c)
|
||||
}
|
||||
|
||||
// Scan implements the sql.Scanner interface, used to convert database value to ModelParameters
|
||||
// Scan implements the sql.Scanner interface, used to convert database value to ModelParameters.
|
||||
// Decrypts APIKey after loading from database; legacy plaintext is returned as-is.
|
||||
func (c *ModelParameters) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
@@ -108,7 +116,15 @@ func (c *ModelParameters) Scan(value interface{}) error {
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(b, c)
|
||||
if err := json.Unmarshal(b, c); err != nil {
|
||||
return err
|
||||
}
|
||||
if key := utils.GetAESKey(); key != nil && c.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {
|
||||
c.APIKey = decrypted
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeCreate is a GORM hook that runs before creating a new model record
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -127,6 +128,28 @@ func (t *Tenant) BeforeCreate(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeSave encrypts APIKey before persisting to database.
|
||||
// Uses tx.Statement.SetColumn to avoid polluting the in-memory struct.
|
||||
func (t *Tenant) BeforeSave(tx *gorm.DB) error {
|
||||
if key := utils.GetAESKey(); key != nil && t.APIKey != "" {
|
||||
if encrypted, err := utils.EncryptAESGCM(t.APIKey, key); err == nil {
|
||||
tx.Statement.SetColumn("api_key", encrypted)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterFind decrypts APIKey after loading from database.
|
||||
// Legacy plaintext (without enc:v1: prefix) is returned as-is.
|
||||
func (t *Tenant) AfterFind(tx *gorm.DB) error {
|
||||
if key := utils.GetAESKey(); key != nil && t.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(t.APIKey, key); err == nil {
|
||||
t.APIKey = decrypted
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert RetrieverEngines to database value
|
||||
func (c RetrieverEngines) Value() (driver.Value, error) {
|
||||
return json.Marshal(c)
|
||||
|
||||
Reference in New Issue
Block a user