前端: - src/srv/api → src/api,按域分文件夹(auth/cluster/host/storage/deployment/collector) - 新增 http.ts 引擎(http.get/post/put/delete),域模块改用语义化调用 - index.html 增加 favicon(匹配应用 logo 的 emerald + Activity 脉冲) 后端分层增强(保留包名): - 新增 repository/:从 models 拆出 JsonDB 持久化层(InitDB/GetDB/Shutdown/RequestSave) - 新增 dto/:从 controllers 抽出各域请求契约(带 binding 校验) - 新增 pkg/crypto、pkg/jwt:从 utils 拆出安全原语(外部 jwt 别名 jwtv5 避免冲突) - models 只留领域实体 + sanitizer;utils 只留 gin/应用相关(response/errors) v2 优化移植与缺陷修复: - 移植密钥分离/SSH TOFU/并发写安全/Loki 生产级配置/Docker 加固 - 修复 deployment.go 并发丢更新、Helm values 重复键、EncryptField 哨兵、loki-gateway 端点等审计缺陷 - 实现真正的优雅关停(main.go http.Server + signal + repository.Shutdown) - 修复登录契约:后端返回 accessToken,前端 auth.ts/Login.tsx 对齐 - 补 crypto/jwt/repository 单元测试(含并发写回归) 验证: go build/vet/test 全绿;npm run build 通过;登录→鉴权→优雅关停冒烟通过 Co-Authored-By: Claude <noreply@anthropic.com>
122 lines
3.1 KiB
Go
122 lines
3.1 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"lokideploy/pkg/crypto"
|
|
)
|
|
|
|
// SSHAuthConfig SSH认证配置(定义在models包避免循环导入)
|
|
type SSHAuthConfig struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
PrivateKey string
|
|
KeyPhrase string
|
|
}
|
|
|
|
// Host 主机模型,敏感字段(Password/PrivateKey/KeyPhrase)加密存储
|
|
// 加密格式: ENC:<base64(AES-GCM(ciphertext))>
|
|
type Host struct {
|
|
ID uint `json:"id"`
|
|
Name string `json:"name"`
|
|
HostIP string `json:"hostIP"`
|
|
Port int `json:"port"`
|
|
User string `json:"user"`
|
|
AuthType string `json:"authType"`
|
|
Password string `json:"password"` // 加密存储(ENC:前缀)
|
|
PrivateKey string `json:"privateKey"` // 加密存储(ENC:前缀)
|
|
KeyPhrase string `json:"keyPhrase"` // 加密存储(ENC:前缀)
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
Status string `json:"status"`
|
|
Labels string `json:"labels"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
func (h *Host) Sanitized() map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"id": h.ID,
|
|
"name": h.Name,
|
|
"hostIP": h.HostIP,
|
|
"port": h.Port,
|
|
"user": h.User,
|
|
"authType": h.AuthType,
|
|
"os": h.OS,
|
|
"arch": h.Arch,
|
|
"status": h.Status,
|
|
"labels": h.Labels,
|
|
"createdAt": h.CreatedAt,
|
|
"updatedAt": h.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
// DecryptPassword 解密主机密码
|
|
func (h *Host) DecryptPassword() string {
|
|
return crypto.DecryptField(h.Password)
|
|
}
|
|
|
|
// DecryptPrivateKey 解密SSH私钥
|
|
func (h *Host) DecryptPrivateKey() string {
|
|
return crypto.DecryptField(h.PrivateKey)
|
|
}
|
|
|
|
// DecryptKeyPhrase 解密私钥密码
|
|
func (h *Host) DecryptKeyPhrase() string {
|
|
return crypto.DecryptField(h.KeyPhrase)
|
|
}
|
|
|
|
// IsPasswordEncrypted 检查密码是否已加密
|
|
func (h *Host) IsPasswordEncrypted() bool {
|
|
return crypto.IsEncryptedField(h.Password)
|
|
}
|
|
|
|
// IsPrivateKeyEncrypted 检查私钥是否已加密
|
|
func (h *Host) IsPrivateKeyEncrypted() bool {
|
|
return crypto.IsEncryptedField(h.PrivateKey)
|
|
}
|
|
|
|
// EncryptAndSetPassword 加密并设置密码
|
|
func (h *Host) EncryptAndSetPassword(password string) error {
|
|
enc, err := crypto.EncryptField(password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
h.Password = enc
|
|
return nil
|
|
}
|
|
|
|
// EncryptAndSetPrivateKey 加密并设置私钥
|
|
func (h *Host) EncryptAndSetPrivateKey(privateKey string) error {
|
|
enc, err := crypto.EncryptField(privateKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
h.PrivateKey = enc
|
|
return nil
|
|
}
|
|
|
|
// EncryptAndSetKeyPhrase 加密并设置私钥密码
|
|
func (h *Host) EncryptAndSetKeyPhrase(keyPhrase string) error {
|
|
enc, err := crypto.EncryptField(keyPhrase)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
h.KeyPhrase = enc
|
|
return nil
|
|
}
|
|
|
|
// GetSSHAuthConfig 获取SSH认证配置(自动解密敏感字段)
|
|
func (h *Host) GetSSHAuthConfig() *SSHAuthConfig {
|
|
return &SSHAuthConfig{
|
|
Host: h.HostIP,
|
|
Port: h.Port,
|
|
User: h.User,
|
|
Password: h.DecryptPassword(),
|
|
PrivateKey: h.DecryptPrivateKey(),
|
|
KeyPhrase: h.DecryptKeyPhrase(),
|
|
}
|
|
}
|