SSH登录支持Passphrase

This commit is contained in:
GoEdgeLab
2021-11-06 15:31:01 +08:00
parent 9fbe20c18a
commit 5a7e3c265f
8 changed files with 32 additions and 9 deletions

View File

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

View File

@@ -59,7 +59,13 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
methods = append(methods, authMethod)
}
} else if credentials.Method == "privateKey" {
signer, err := ssh.ParsePrivateKey([]byte(credentials.PrivateKey))
var signer ssh.Signer
var err error
if len(credentials.Passphrase) > 0 {
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(credentials.PrivateKey), []byte(credentials.Passphrase))
} else {
signer, err = ssh.ParsePrivateKey([]byte(credentials.PrivateKey))
}
if err != nil {
return errors.New("parse private key: " + err.Error())
}

View File

@@ -6,7 +6,7 @@ import (
)
func TestDNSNodeInstaller_Install(t *testing.T) {
var installer InstallerInterface = &DNSNodeInstaller{}
var installer InstallerInterface = &NSNodeInstaller{}
err := installer.Login(&Credentials{
Host: "192.168.2.30",
Port: 22,

View File

@@ -185,6 +185,7 @@ func (this *NodeQueue) InstallNode(nodeId int64, installStatus *models.NodeInsta
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {
@@ -274,6 +275,7 @@ func (this *NodeQueue) StartNode(nodeId int64) error {
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {
@@ -379,6 +381,7 @@ func (this *NodeQueue) StopNode(nodeId int64) error {
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {

View File

@@ -185,6 +185,7 @@ func (this *NSNodeQueue) InstallNode(nodeId int64, installStatus *models.NodeIns
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {
@@ -274,6 +275,7 @@ func (this *NSNodeQueue) StartNode(nodeId int64) error {
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {
@@ -379,6 +381,7 @@ func (this *NSNodeQueue) StopNode(nodeId int64) error {
Username: grant.Username,
Password: grant.Password,
PrivateKey: grant.PrivateKey,
Passphrase: grant.Passphrase,
Method: grant.Method,
})
if err != nil {