SSH认证--私钥认证方式增加用户名选项

This commit is contained in:
刘祥超
2021-06-30 14:56:36 +08:00
parent e544e088be
commit 98a2d61fd1
7 changed files with 25 additions and 5 deletions

View File

@@ -6,4 +6,5 @@ type Credentials struct {
Username string
Password string
PrivateKey string
Method string
}

View File

@@ -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,

View File

@@ -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