mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-15 15:16:34 +08:00
SSH认证--私钥认证方式增加用户名选项
This commit is contained in:
@@ -6,4 +6,5 @@ type Credentials struct {
|
||||
Username string
|
||||
Password string
|
||||
PrivateKey string
|
||||
Method string
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
||||
|
||||
// 认证
|
||||
methods := []ssh.AuthMethod{}
|
||||
if len(credentials.Password) > 0 {
|
||||
if credentials.Method == "user" {
|
||||
{
|
||||
authMethod := ssh.Password(credentials.Password)
|
||||
methods = append(methods, authMethod)
|
||||
@@ -56,16 +56,21 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
||||
})
|
||||
methods = append(methods, authMethod)
|
||||
}
|
||||
} else {
|
||||
} else if credentials.Method == "privateKey" {
|
||||
signer, err := ssh.ParsePrivateKey([]byte(credentials.PrivateKey))
|
||||
if err != nil {
|
||||
return errors.New("parse private key: " + err.Error())
|
||||
}
|
||||
authMethod := ssh.PublicKeys(signer)
|
||||
methods = append(methods, authMethod)
|
||||
} else {
|
||||
return errors.New("invalid method '" + credentials.Method + "'")
|
||||
}
|
||||
|
||||
// SSH客户端
|
||||
if len(credentials.Username) == 0 {
|
||||
credentials.Username = "root"
|
||||
}
|
||||
config := &ssh.ClientConfig{
|
||||
User: credentials.Username,
|
||||
Auth: methods,
|
||||
|
||||
@@ -184,6 +184,7 @@ func (this *Queue) InstallNode(nodeId int64, installStatus *models.NodeInstallSt
|
||||
Username: grant.Username,
|
||||
Password: grant.Password,
|
||||
PrivateKey: grant.PrivateKey,
|
||||
Method: grant.Method,
|
||||
})
|
||||
if err != nil {
|
||||
installStatus.ErrorCode = "SSH_LOGIN_FAILED"
|
||||
@@ -272,6 +273,7 @@ func (this *Queue) StartNode(nodeId int64) error {
|
||||
Username: grant.Username,
|
||||
Password: grant.Password,
|
||||
PrivateKey: grant.PrivateKey,
|
||||
Method: grant.Method,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -376,6 +378,7 @@ func (this *Queue) StopNode(nodeId int64) error {
|
||||
Username: grant.Username,
|
||||
Password: grant.Password,
|
||||
PrivateKey: grant.PrivateKey,
|
||||
Method: grant.Method,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user