继续实现按服务器管理SSH密钥

补充服务器SSH密钥状态模型、密钥生成及公钥部署接口,为自动部署流程继续开发做检查点。
This commit is contained in:
xuans committed 2026-09-27 14:47:41 +08:00
1 parent 5edf0fb092
commit 38c3e2a389
6 files changed
+87 -3

No files matched your search

+1
View File
@@ -17,6 +17,7 @@ type Client interface {
Rename(ctx context.Context, source, target string) error
Remove(ctx context.Context, path string) error
Run(ctx context.Context, command []string) (CommandResult, error)
InstallAuthorizedKey(ctx context.Context, publicKey string) error
Close() error
}
+26
View File
@@ -19,6 +19,16 @@ const (
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"
@@ -44,6 +54,11 @@ type Server struct {
PrivateKeyRef string `json:"private_key_ref"`
PasswordRef string `json:"-"`
PasswordConfigured bool `json:"password_configured"`
SSHKeyStatus SSHKeyStatus `json:"ssh_key_status"`
SSHKeyFingerprint string `json:"ssh_key_fingerprint,omitempty"`
SSHKeyDeployedAt string `json:"ssh_key_deployed_at,omitempty"`
SSHKeyTestedAt string `json:"ssh_key_tested_at,omitempty"`
SSHKeyError string `json:"ssh_key_error,omitempty"`
HostKeyFingerprint string `json:"host_key_fingerprint"`
CertificatePath string `json:"certificate_path"`
PrivateKeyPath string `json:"private_key_path"`
@@ -64,6 +79,11 @@ func (s Server) Normalize() Server {
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 == "" {
if s.PasswordRef != "" {
s.AuthMethod = SSHAuthPassword
@@ -71,6 +91,12 @@ func (s Server) Normalize() Server {
s.AuthMethod = SSHAuthPrivateKey
}
}
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)
@@ -120,6 +120,8 @@ func (c *memoryClient) Run(_ context.Context, command []string) (CommandResult,
return result, err
}
func (c *memoryClient) InstallAuthorizedKey(context.Context, string) error { return nil }
func (c *memoryClient) Close() error { return nil }
type memoryFactory struct {
+26
View File
@@ -2,6 +2,7 @@ package deployment
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
@@ -332,6 +333,31 @@ func (c *sshClientAdapter) Remove(ctx context.Context, path string) error {
})
}
func (c *sshClientAdapter) InstallAuthorizedKey(ctx context.Context, publicKey string) error {
publicKey = strings.TrimSpace(publicKey)
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
if err != nil {
return fmt.Errorf("SSH 公钥格式无效:%w", err)
}
encoded := base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(key))
script := fmt.Sprintf(`set -eu
umask 077
install -d -m 700 "$HOME/.ssh"
touch "$HOME/.ssh/authorized_keys"
chmod 600 "$HOME/.ssh/authorized_keys"
decoded="$(printf '%s' '%s' | base64 -d)"
if ! grep -Fqx -- "$decoded" "$HOME/.ssh/authorized_keys"; then
printf '%%s\\n' "$decoded" >> "$HOME/.ssh/authorized_keys"
fi`, "%s", encoded)
result, err := c.Run(ctx, []string{"sh", "-c", script})
if err != nil {
return fmt.Errorf("部署 SSH 公钥失败:%w", err)
}
if result.ExitCode != 0 {
return fmt.Errorf("部署 SSH 公钥失败,远程退出码:%d", result.ExitCode)
}
return nil
}
func (c *sshClientAdapter) Run(ctx context.Context, command []string) (CommandResult, error) {
if len(command) == 0 {
return CommandResult{}, errors.New("远程命令不能为空")