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

@@ -85,6 +85,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
op.Password = password
op.Su = false // TODO 需要做到前端可以配置
case "privateKey":
op.Username = username
op.PrivateKey = privateKey
}
op.Description = description
@@ -111,6 +112,7 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
op.Password = password
op.Su = false // TODO 需要做到前端可以配置
case "privateKey":
op.Username = username
op.PrivateKey = privateKey
}
op.Description = description

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

View File

@@ -214,7 +214,7 @@ func (this *NodeGrantService) TestNodeGrant(ctx context.Context, req *pb.TestNod
// 认证
methods := []ssh.AuthMethod{}
if len(grant.Password) > 0 {
if grant.Method == "user" {
{
authMethod := ssh.Password(grant.Password)
methods = append(methods, authMethod)
@@ -229,7 +229,7 @@ func (this *NodeGrantService) TestNodeGrant(ctx context.Context, req *pb.TestNod
})
methods = append(methods, authMethod)
}
} else {
} else if grant.Method == "privateKey" {
signer, err := ssh.ParsePrivateKey([]byte(grant.PrivateKey))
if err != nil {
resp.Error = "parse private key: " + err.Error()
@@ -237,9 +237,14 @@ func (this *NodeGrantService) TestNodeGrant(ctx context.Context, req *pb.TestNod
}
authMethod := ssh.PublicKeys(signer)
methods = append(methods, authMethod)
} else {
return nil, errors.New("invalid method '" + grant.Method + "'")
}
// SSH客户端
if len(grant.Username) == 0 {
grant.Username = "root"
}
config := &ssh.ClientConfig{
User: grant.Username,
Auth: methods,