Add support for encrypted ssh keys

This commit is contained in:
Dan Sosedoff
2019-11-02 12:44:04 -05:00
parent 9e64bb5eec
commit 7475f398b1
30 changed files with 1748 additions and 237 deletions

View File

@@ -115,11 +115,12 @@ func parseIntFormValue(c *gin.Context, name string, defValue int) (int, error) {
func parseSshInfo(c *gin.Context) *shared.SSHInfo {
info := shared.SSHInfo{
Host: c.Request.FormValue("ssh_host"),
Port: c.Request.FormValue("ssh_port"),
User: c.Request.FormValue("ssh_user"),
Password: c.Request.FormValue("ssh_password"),
Key: c.Request.FormValue("ssh_key"),
Host: c.Request.FormValue("ssh_host"),
Port: c.Request.FormValue("ssh_port"),
User: c.Request.FormValue("ssh_user"),
Password: c.Request.FormValue("ssh_password"),
Key: c.Request.FormValue("ssh_key"),
KeyPassword: c.Request.FormValue("ssh_key_password"),
}
if info.Port == "" {

View File

@@ -71,6 +71,7 @@ func readServerConfig(path string) (Bookmark, error) {
bookmark.Ssl = "disable"
}
// Set default SSH port if it's not provided by user
if bookmark.Ssh != nil && bookmark.Ssh.Port == "" {
bookmark.Ssh.Port = "22"
}

View File

@@ -13,6 +13,7 @@ import (
"sync"
"time"
"github.com/ScaleFT/sshkeys"
"golang.org/x/crypto/ssh"
"github.com/sosedoff/pgweb/pkg/connection"
@@ -52,13 +53,22 @@ func fileExists(path string) bool {
return err == nil
}
func parsePrivateKey(keyPath string) (ssh.Signer, error) {
func parsePrivateKey(keyPath string, keyPass string) (ssh.Signer, error) {
buff, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
return ssh.ParsePrivateKey(buff)
signer, err := ssh.ParsePrivateKey(buff)
if err != nil {
if strings.Contains(err.Error(), "cannot decode encrypted private keys") {
if keyPass == "" {
return nil, errors.New("SSH key password is not provided")
}
return sshkeys.ParseEncryptedPrivateKey(buff, []byte(keyPass))
}
}
return signer, err
}
func makeConfig(info *shared.SSHInfo) (*ssh.ClientConfig, error) {
@@ -77,7 +87,7 @@ func makeConfig(info *shared.SSHInfo) (*ssh.ClientConfig, error) {
}
// Appen public key authentication method
key, err := parsePrivateKey(keyPath)
key, err := parsePrivateKey(keyPath, info.KeyPassword)
if err != nil {
return nil, err
}

File diff suppressed because one or more lines are too long

View File

@@ -6,11 +6,12 @@ import (
// SSHInfo contains ssh server configuration
type SSHInfo struct {
Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Key string `json:"key,omitempty"`
Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Key string `json:"key,omitempty"`
KeyPassword string `json:"keypassword,omitempty"`
}
func (info SSHInfo) String() string {