mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	SSH登录支持Passphrase
This commit is contained in:
		@@ -73,7 +73,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateGrant 创建认证信息
 | 
			
		||||
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) (grantId int64, err error) {
 | 
			
		||||
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) (grantId int64, err error) {
 | 
			
		||||
	op := NewNodeGrantOperator()
 | 
			
		||||
	op.AdminId = adminId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
@@ -87,6 +87,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		op.Username = username
 | 
			
		||||
		op.PrivateKey = privateKey
 | 
			
		||||
		op.Passphrase = passphrase
 | 
			
		||||
	}
 | 
			
		||||
	op.Description = description
 | 
			
		||||
	op.NodeId = nodeId
 | 
			
		||||
@@ -96,7 +97,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateGrant 修改认证信息
 | 
			
		||||
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) error {
 | 
			
		||||
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, passphrase string, description string, nodeId int64) error {
 | 
			
		||||
	if grantId <= 0 {
 | 
			
		||||
		return errors.New("invalid grantId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -114,6 +115,7 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		op.Username = username
 | 
			
		||||
		op.PrivateKey = privateKey
 | 
			
		||||
		op.Passphrase = passphrase
 | 
			
		||||
	}
 | 
			
		||||
	op.Description = description
 | 
			
		||||
	op.NodeId = nodeId
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,8 @@ type NodeGrant struct {
 | 
			
		||||
	Username    string `field:"username"`    // 用户名
 | 
			
		||||
	Password    string `field:"password"`    // 密码
 | 
			
		||||
	Su          uint8  `field:"su"`          // 是否需要su
 | 
			
		||||
	PrivateKey  string `field:"privateKey"`  // 密钥
 | 
			
		||||
	PrivateKey  string `field:"privateKey"`  // 私钥
 | 
			
		||||
	Passphrase  string `field:"passphrase"`  // 私钥密码
 | 
			
		||||
	Description string `field:"description"` // 备注
 | 
			
		||||
	NodeId      uint32 `field:"nodeId"`      // 专有节点
 | 
			
		||||
	Role        string `field:"role"`        // 角色
 | 
			
		||||
@@ -25,7 +26,8 @@ type NodeGrantOperator struct {
 | 
			
		||||
	Username    interface{} // 用户名
 | 
			
		||||
	Password    interface{} // 密码
 | 
			
		||||
	Su          interface{} // 是否需要su
 | 
			
		||||
	PrivateKey  interface{} // 密钥
 | 
			
		||||
	PrivateKey  interface{} // 私钥
 | 
			
		||||
	Passphrase  interface{} // 私钥密码
 | 
			
		||||
	Description interface{} // 备注
 | 
			
		||||
	NodeId      interface{} // 专有节点
 | 
			
		||||
	Role        interface{} // 角色
 | 
			
		||||
 
 | 
			
		||||
@@ -6,5 +6,6 @@ type Credentials struct {
 | 
			
		||||
	Username   string
 | 
			
		||||
	Password   string
 | 
			
		||||
	PrivateKey string
 | 
			
		||||
	Passphrase string
 | 
			
		||||
	Method     string
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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())
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,7 @@ func (this *NodeGrantService) CreateNodeGrant(ctx context.Context, req *pb.Creat
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	grantId, err := models.SharedNodeGrantDAO.CreateGrant(tx, adminId, req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Description, req.NodeId)
 | 
			
		||||
	grantId, err := models.SharedNodeGrantDAO.CreateGrant(tx, adminId, req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Passphrase, req.Description, req.NodeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -48,7 +48,7 @@ func (this *NodeGrantService) UpdateNodeGrant(ctx context.Context, req *pb.Updat
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeGrantDAO.UpdateGrant(tx, req.NodeGrantId, req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Description, req.NodeId)
 | 
			
		||||
	err = models.SharedNodeGrantDAO.UpdateGrant(tx, req.NodeGrantId, req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Passphrase, req.Description, req.NodeId)
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -162,6 +162,7 @@ func (this *NodeGrantService) FindEnabledNodeGrant(ctx context.Context, req *pb.
 | 
			
		||||
		Password:    grant.Password,
 | 
			
		||||
		Su:          grant.Su == 1,
 | 
			
		||||
		PrivateKey:  grant.PrivateKey,
 | 
			
		||||
		Passphrase:  grant.Passphrase,
 | 
			
		||||
		Description: grant.Description,
 | 
			
		||||
		NodeId:      int64(grant.NodeId),
 | 
			
		||||
	}}, nil
 | 
			
		||||
@@ -231,7 +232,12 @@ func (this *NodeGrantService) TestNodeGrant(ctx context.Context, req *pb.TestNod
 | 
			
		||||
			methods = append(methods, authMethod)
 | 
			
		||||
		}
 | 
			
		||||
	} else if grant.Method == "privateKey" {
 | 
			
		||||
		signer, err := ssh.ParsePrivateKey([]byte(grant.PrivateKey))
 | 
			
		||||
		var signer ssh.Signer
 | 
			
		||||
		if len(grant.Passphrase) != 0 {
 | 
			
		||||
			signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(grant.PrivateKey), []byte(grant.Passphrase))
 | 
			
		||||
		} else {
 | 
			
		||||
			signer, err = ssh.ParsePrivateKey([]byte(grant.PrivateKey))
 | 
			
		||||
		}
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			resp.Error = "parse private key: " + err.Error()
 | 
			
		||||
			return resp, nil
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user