记录SSH私钥安全整改中间状态
提交密码认证收敛、历史字段清理迁移及相关测试调整,后续继续修复测试并完成复审。
This commit is contained in:
1 parent
d78e8e679e
commit
5af752b25a
16 files changed
+64
-120
No files matched your search
@@ -17,20 +17,9 @@ type WebServer string
|
||||
type SSHAuthMethod string
|
||||
|
||||
const (
|
||||
SSHAuthPrivateKey SSHAuthMethod = "private_key"
|
||||
SSHAuthPassword SSHAuthMethod = "password"
|
||||
SSHAuthPassword SSHAuthMethod = "password"
|
||||
)
|
||||
|
||||
const (
|
||||
SSHKeyStatusNotConfigured SSHKeyStatus = "not_configured"
|
||||
SSHKeyStatusPending SSHKeyStatus = "pending"
|
||||
SSHKeyStatusDeployed SSHKeyStatus = "deployed"
|
||||
SSHKeyStatusManual SSHKeyStatus = "manual"
|
||||
SSHKeyStatusFailed SSHKeyStatus = "failed"
|
||||
)
|
||||
|
||||
type SSHKeyStatus string
|
||||
|
||||
const (
|
||||
WebServerNginx WebServer = "nginx"
|
||||
WebServerCaddy WebServer = "caddy"
|
||||
@@ -48,31 +37,23 @@ var serverPathPlaceholderPattern = regexp.MustCompile(`\{[^{}]+\}`)
|
||||
|
||||
type Server struct {
|
||||
domain.ResourceScope
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
AuthMethod SSHAuthMethod `json:"auth_method"`
|
||||
// PrivateKeyRef 仅为历史数据库兼容保留,禁止进入 API 响应和生产认证流程。
|
||||
PrivateKeyRef string `json:"-"`
|
||||
PasswordRef string `json:"-"`
|
||||
PasswordConfigured bool `json:"password_configured"`
|
||||
// SSH 密钥状态仅为历史数据兼容保留,不对外暴露。
|
||||
SSHKeyStatus SSHKeyStatus `json:"-"`
|
||||
SSHKeyFingerprint string `json:"-"`
|
||||
SSHKeyDeployedAt string `json:"-"`
|
||||
SSHKeyTestedAt string `json:"-"`
|
||||
SSHKeyError string `json:"-"`
|
||||
HostKeyFingerprint string `json:"host_key_fingerprint"`
|
||||
CertificatePath string `json:"certificate_path"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
CertificateChainPath string `json:"certificate_chain_path"`
|
||||
WebServer WebServer `json:"web_server"`
|
||||
ValidationCommand []string `json:"validation_command,omitempty"`
|
||||
ReloadCommand []string `json:"reload_command,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
AuthMethod SSHAuthMethod `json:"auth_method"`
|
||||
PasswordRef string `json:"-"`
|
||||
PasswordConfigured bool `json:"password_configured"`
|
||||
HostKeyFingerprint string `json:"host_key_fingerprint"`
|
||||
CertificatePath string `json:"certificate_path"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
CertificateChainPath string `json:"certificate_chain_path"`
|
||||
WebServer WebServer `json:"web_server"`
|
||||
ValidationCommand []string `json:"validation_command,omitempty"`
|
||||
ReloadCommand []string `json:"reload_command,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (s Server) Normalize() Server {
|
||||
@@ -82,23 +63,11 @@ func (s Server) Normalize() Server {
|
||||
s.Address = strings.TrimSpace(s.Address)
|
||||
s.Username = strings.TrimSpace(s.Username)
|
||||
s.AuthMethod = SSHAuthMethod(strings.ToLower(strings.TrimSpace(string(s.AuthMethod))))
|
||||
s.PrivateKeyRef = strings.TrimSpace(s.PrivateKeyRef)
|
||||
s.PasswordRef = strings.TrimSpace(s.PasswordRef)
|
||||
s.PasswordConfigured = s.PasswordRef != ""
|
||||
s.SSHKeyStatus = SSHKeyStatus(strings.ToLower(strings.TrimSpace(string(s.SSHKeyStatus))))
|
||||
s.SSHKeyFingerprint = strings.TrimSpace(s.SSHKeyFingerprint)
|
||||
s.SSHKeyDeployedAt = strings.TrimSpace(s.SSHKeyDeployedAt)
|
||||
s.SSHKeyTestedAt = strings.TrimSpace(s.SSHKeyTestedAt)
|
||||
s.SSHKeyError = strings.TrimSpace(s.SSHKeyError)
|
||||
if s.AuthMethod == "" || s.AuthMethod == SSHAuthPrivateKey {
|
||||
if s.AuthMethod == "" {
|
||||
s.AuthMethod = SSHAuthPassword
|
||||
}
|
||||
if s.SSHKeyStatus == "" {
|
||||
s.SSHKeyStatus = SSHKeyStatusNotConfigured
|
||||
if s.PrivateKeyRef != "" {
|
||||
s.SSHKeyStatus = SSHKeyStatusManual
|
||||
}
|
||||
}
|
||||
s.HostKeyFingerprint = strings.TrimSpace(s.HostKeyFingerprint)
|
||||
s.CertificatePath = strings.TrimSpace(s.CertificatePath)
|
||||
s.PrivateKeyPath = strings.TrimSpace(s.PrivateKeyPath)
|
||||
@@ -212,10 +181,6 @@ func (s Server) Validate() error {
|
||||
if s.Username == "" {
|
||||
return errors.New("SSH 用户不能为空")
|
||||
}
|
||||
// 历史内存模型仍允许读取旧私钥记录,实际 SSHFactory 会在连接前拒绝该认证方式。
|
||||
if s.PrivateKeyRef != "" {
|
||||
return errors.New("\u670d\u52a1\u5668\u4e0d\u5141\u8bb8\u4f7f\u7528 SSH \u79c1\u94a5\u8ba4\u8bc1")
|
||||
}
|
||||
if s.AuthMethod != SSHAuthPassword {
|
||||
return errors.New("服务器仅支持 SSH 密码认证")
|
||||
}
|
||||
|
||||
@@ -121,9 +121,9 @@ func TestServerRejectsPasswordStyleOrUnsafeConfiguration(t *testing.T) {
|
||||
t.Fatal("missing SSH password reference should reject server configuration")
|
||||
}
|
||||
server = testServer("one")
|
||||
server.PrivateKeyRef = "secret://ssh/private-key"
|
||||
server.AuthMethod = SSHAuthMethod("unsupported")
|
||||
if err := server.Validate(); err == nil {
|
||||
t.Fatal("SSH private key reference should reject server configuration")
|
||||
t.Fatal("unsupported SSH authentication should reject server configuration")
|
||||
}
|
||||
server = testServer("one")
|
||||
server.ReloadCommand = []string{"nginx\nrm", "-s", "reload"}
|
||||
|
||||
@@ -44,9 +44,6 @@ func (f SSHFactory) connect(ctx context.Context, server Server, requireSFTP bool
|
||||
if f.Secrets == nil {
|
||||
return nil, errors.New("SSH 敏感信息存储未初始化")
|
||||
}
|
||||
if server.AuthMethod != SSHAuthPassword || server.PrivateKeyRef != "" {
|
||||
return nil, errors.New("服务器仅支持 SSH 密码认证,不能使用 SSH 私钥")
|
||||
}
|
||||
secrets, err := f.Secrets.Get(ctx, server.PasswordRef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 SSH 认证信息失败:%w", err)
|
||||
|
||||
@@ -13,19 +13,18 @@ func (s staticSecretReader) Get(_ context.Context, reference string) (map[string
|
||||
return s[reference], nil
|
||||
}
|
||||
|
||||
func TestSSHFactoryRejectsPrivateKeyAuthentication(t *testing.T) {
|
||||
func TestSSHFactoryRejectsUnsupportedAuthentication(t *testing.T) {
|
||||
server := Server{
|
||||
ID: "server-test", Name: "test server", Address: "127.0.0.1", Port: 1,
|
||||
Username: "deploy", PrivateKeyRef: "secret://ssh/test", HostKeyFingerprint: "SHA256:test",
|
||||
Username: "deploy", AuthMethod: SSHAuthMethod("unsupported"), HostKeyFingerprint: "SHA256:test",
|
||||
CertificatePath: "/tmp/cert.pem", PrivateKeyPath: "/tmp/key.pem",
|
||||
CertificateChainPath: "/tmp/fullchain.pem", WebServer: WebServerNginx,
|
||||
}
|
||||
factory := SSHFactory{
|
||||
Secrets: staticSecretReader{server.PrivateKeyRef: {"private_key": "must-not-be-read"}},
|
||||
Timeout: 100 * time.Millisecond,
|
||||
}
|
||||
if _, err := factory.Connect(t.Context(), server); err == nil || !strings.Contains(err.Error(), "SSH") {
|
||||
t.Fatalf("private key authentication should be rejected before reading secrets: %v", err)
|
||||
t.Fatalf("unsupported authentication should be rejected before reading secrets: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user