保存部署服务器密码认证支持与页面改造前检查点
This commit is contained in:
1 parent
24b9e7abdc
commit
be6bc63102
10 files changed
+186
-49
No files matched your search
@@ -12,6 +12,13 @@ import (
|
||||
|
||||
type WebServer string
|
||||
|
||||
type SSHAuthMethod string
|
||||
|
||||
const (
|
||||
SSHAuthPrivateKey SSHAuthMethod = "private_key"
|
||||
SSHAuthPassword SSHAuthMethod = "password"
|
||||
)
|
||||
|
||||
const (
|
||||
WebServerNginx WebServer = "nginx"
|
||||
WebServerCaddy WebServer = "caddy"
|
||||
@@ -28,21 +35,24 @@ const (
|
||||
var serverPathPlaceholderPattern = regexp.MustCompile(`\{[^{}]+\}`)
|
||||
|
||||
type Server struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
PrivateKeyRef string `json:"private_key_ref"`
|
||||
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"`
|
||||
PrivateKeyRef string `json:"private_key_ref"`
|
||||
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 {
|
||||
@@ -50,7 +60,17 @@ func (s Server) Normalize() Server {
|
||||
s.Name = strings.TrimSpace(s.Name)
|
||||
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 != ""
|
||||
if s.AuthMethod == "" {
|
||||
if s.PasswordRef != "" {
|
||||
s.AuthMethod = SSHAuthPassword
|
||||
} else {
|
||||
s.AuthMethod = SSHAuthPrivateKey
|
||||
}
|
||||
}
|
||||
s.HostKeyFingerprint = strings.TrimSpace(s.HostKeyFingerprint)
|
||||
s.CertificatePath = strings.TrimSpace(s.CertificatePath)
|
||||
s.PrivateKeyPath = strings.TrimSpace(s.PrivateKeyPath)
|
||||
@@ -164,8 +184,17 @@ func (s Server) Validate() error {
|
||||
if s.Username == "" {
|
||||
return errors.New("SSH 用户不能为空")
|
||||
}
|
||||
if s.PrivateKeyRef == "" {
|
||||
return errors.New("SSH 私钥引用不能为空")
|
||||
switch s.AuthMethod {
|
||||
case SSHAuthPrivateKey:
|
||||
if s.PrivateKeyRef == "" {
|
||||
return errors.New("SSH \u79c1\u94a5\u5f15\u7528\u4e0d\u80fd\u4e3a\u7a7a")
|
||||
}
|
||||
case SSHAuthPassword:
|
||||
if s.PasswordRef == "" {
|
||||
return errors.New("SSH \u5bc6\u7801\u5f15\u7528\u4e0d\u80fd\u4e3a\u7a7a")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("SSH \u8ba4\u8bc1\u65b9\u5f0f\u65e0\u6548\uff1a%s", s.AuthMethod)
|
||||
}
|
||||
if s.HostKeyFingerprint == "" {
|
||||
return errors.New("SSH 主机指纹不能为空")
|
||||
|
||||
@@ -33,23 +33,37 @@ func (f SSHFactory) Connect(ctx context.Context, server Server) (Client, error)
|
||||
if f.Secrets == nil {
|
||||
return nil, errors.New("SSH 敏感信息存储未初始化")
|
||||
}
|
||||
secrets, err := f.Secrets.Get(ctx, server.PrivateKeyRef)
|
||||
secretReference := server.PrivateKeyRef
|
||||
if server.AuthMethod == SSHAuthPassword {
|
||||
secretReference = server.PasswordRef
|
||||
}
|
||||
secrets, err := f.Secrets.Get(ctx, secretReference)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 SSH 私钥失败:%w", err)
|
||||
return nil, fmt.Errorf("读取 SSH 认证信息失败:%w", err)
|
||||
}
|
||||
privateKey := strings.TrimSpace(secrets["private_key"])
|
||||
if privateKey == "" {
|
||||
return nil, errors.New("SSH 私钥内容不能为空")
|
||||
}
|
||||
var signer ssh.Signer
|
||||
passphrase := secrets["passphrase"]
|
||||
if passphrase != "" {
|
||||
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(passphrase))
|
||||
var authMethods []ssh.AuthMethod
|
||||
if server.AuthMethod == SSHAuthPassword {
|
||||
password := secrets["password"]
|
||||
if password == "" {
|
||||
return nil, errors.New("SSH 密码不能为空")
|
||||
}
|
||||
authMethods = []ssh.AuthMethod{ssh.Password(password)}
|
||||
} else {
|
||||
signer, err = ssh.ParsePrivateKey([]byte(privateKey))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("解析 SSH 私钥失败:私钥格式或口令无效")
|
||||
privateKey := strings.TrimSpace(secrets["private_key"])
|
||||
if privateKey == "" {
|
||||
return nil, errors.New("SSH 私钥内容不能为空")
|
||||
}
|
||||
var signer ssh.Signer
|
||||
passphrase := secrets["passphrase"]
|
||||
if passphrase != "" {
|
||||
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(passphrase))
|
||||
} else {
|
||||
signer, err = ssh.ParsePrivateKey([]byte(privateKey))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("解析 SSH 私钥失败:私钥格式或口令无效")
|
||||
}
|
||||
authMethods = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
||||
}
|
||||
timeout := f.Timeout
|
||||
if timeout <= 0 {
|
||||
@@ -61,7 +75,7 @@ func (f SSHFactory) Connect(ctx context.Context, server Server) (Client, error)
|
||||
}
|
||||
timeoutContext, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
sshClient, err := connectSSHClient(timeoutContext, server, signer, algorithms)
|
||||
sshClient, err := connectSSHClient(timeoutContext, server, authMethods, algorithms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -108,7 +122,7 @@ func (f SSHFactory) secureHostKeyAlgorithms() ([]string, error) {
|
||||
func connectSSHClient(
|
||||
ctx context.Context,
|
||||
server Server,
|
||||
signer ssh.Signer,
|
||||
authMethods []ssh.AuthMethod,
|
||||
algorithms []string,
|
||||
) (*ssh.Client, error) {
|
||||
address := net.JoinHostPort(server.Address, fmt.Sprintf("%d", server.Port))
|
||||
@@ -131,7 +145,7 @@ func connectSSHClient(
|
||||
hostKeyMismatch := ""
|
||||
config := &ssh.ClientConfig{
|
||||
User: server.Username,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||
Auth: authMethods,
|
||||
HostKeyAlgorithms: []string{algorithm},
|
||||
HostKeyCallback: func(_ string, _ net.Addr, key ssh.PublicKey) error {
|
||||
fingerprint := ssh.FingerprintSHA256(key)
|
||||
|
||||
Reference in new issue
Block a user