保存 DNS 多账户数据层与实库故障夹具修复前检查点
基础模型和迁移已完成;增量竞态暴露 MySQL 审计故障注入权限问题,后续修复,不代表完整功能或验收通过。
This commit is contained in:
1 parent
26b8c3c5c0
commit
02e039984a
19 files changed
+1000
-11
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
DNSAccountActive = "active"
|
||||
DNSAccountDisabled = "disabled"
|
||||
DNSAccountUnverified = "unverified"
|
||||
DNSAccountVerified = "verified"
|
||||
DNSAccountVerificationFailed = "failed"
|
||||
)
|
||||
|
||||
// DNSAccount 只保存账户元数据和秘密引用;序列化时不暴露秘密仓库位置。
|
||||
// 归属和服务商在创建后固定,轮换必须产生新引用,版本号用于拒绝旧验证结果。
|
||||
type DNSAccount struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ProjectID string `json:"project_id"`
|
||||
OwnerUserID string `json:"owner_user_id"`
|
||||
Provider string `json:"provider"`
|
||||
CredentialRef string `json:"-"`
|
||||
Status string `json:"status"`
|
||||
VerificationState string `json:"verification_state"`
|
||||
Revision int64 `json:"revision"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (a DNSAccount) Validate() error {
|
||||
if !ValidProjectID(a.ID) || !ValidProjectID(a.ProjectID) || !ValidProjectID(a.OwnerUserID) {
|
||||
return errors.New("DNS 账户标识、项目或归属用户无效")
|
||||
}
|
||||
if !utf8.ValidString(a.Name) || strings.TrimSpace(a.Name) == "" || utf8.RuneCountInString(a.Name) > 128 || strings.ContainsFunc(a.Name, unicode.IsControl) {
|
||||
return errors.New("DNS 账户名称必须为不含控制字符的 1~128 个字符")
|
||||
}
|
||||
if !ValidProjectID(a.Provider) {
|
||||
return errors.New("DNS 服务商标识无效")
|
||||
}
|
||||
if !strings.HasPrefix(a.CredentialRef, "secret://dns/") || len(a.CredentialRef) <= len("secret://dns/") || len(a.CredentialRef) > 255 || strings.ContainsFunc(a.CredentialRef, func(r rune) bool { return unicode.IsControl(r) || unicode.IsSpace(r) }) {
|
||||
return errors.New("DNS 凭据引用无效")
|
||||
}
|
||||
if a.Status != DNSAccountActive && a.Status != DNSAccountDisabled {
|
||||
return errors.New("DNS 账户状态无效")
|
||||
}
|
||||
if a.VerificationState != DNSAccountUnverified && a.VerificationState != DNSAccountVerified && a.VerificationState != DNSAccountVerificationFailed {
|
||||
return errors.New("DNS 账户验证状态无效")
|
||||
}
|
||||
if a.Revision < 1 || a.Revision > 2147483647 {
|
||||
return errors.New("DNS 账户版本无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDNSAccountValidationAndSerialization(t *testing.T) {
|
||||
base := DNSAccount{ID: "dns-one", Name: "生产 DNS", ProjectID: "default", OwnerUserID: "primary-super-admin",
|
||||
Provider: "aliyun", CredentialRef: "secret://dns/aliyun/example", Status: DNSAccountActive,
|
||||
VerificationState: DNSAccountUnverified, Revision: 1}
|
||||
if err := base.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, change := range map[string]func(*DNSAccount){
|
||||
"empty_id": func(a *DNSAccount) { a.ID = "" },
|
||||
"bad_project": func(a *DNSAccount) { a.ProjectID = "../other" },
|
||||
"missing_owner": func(a *DNSAccount) { a.OwnerUserID = "" },
|
||||
"blank_name": func(a *DNSAccount) { a.Name = " " },
|
||||
"control_name": func(a *DNSAccount) { a.Name = "账户\n名称" },
|
||||
"long_name": func(a *DNSAccount) { a.Name = strings.Repeat("中", 129) },
|
||||
"invalid_provider": func(a *DNSAccount) { a.Provider = "other/provider" },
|
||||
"wrong_secret_type": func(a *DNSAccount) { a.CredentialRef = "secret://ssh/password" },
|
||||
"empty_secret_id": func(a *DNSAccount) { a.CredentialRef = "secret://dns/" },
|
||||
"secret_whitespace": func(a *DNSAccount) { a.CredentialRef += "\n" },
|
||||
"invalid_state": func(a *DNSAccount) { a.Status = "deleted" },
|
||||
"invalid_verification": func(a *DNSAccount) { a.VerificationState = "trusted" },
|
||||
"zero_revision": func(a *DNSAccount) { a.Revision = 0 },
|
||||
"overflow_revision": func(a *DNSAccount) { a.Revision = 2147483648 },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
item := base
|
||||
change(&item)
|
||||
if item.Validate() == nil {
|
||||
t.Fatal("无效账户未被拒绝")
|
||||
}
|
||||
})
|
||||
}
|
||||
payload, err := json.Marshal(base)
|
||||
if err != nil || strings.Contains(string(payload), "secret://") || strings.Contains(string(payload), "credential_ref") {
|
||||
t.Fatal("账户 JSON 暴露秘密引用", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user