Handle auth by key

This commit is contained in:
Manfred Touron 2017-11-01 23:42:17 +01:00
parent 729060f397
commit 35e62e562b
3 changed files with 9 additions and 5 deletions

View file

@ -29,7 +29,7 @@ func NewSSHKey(keyType string, length uint) (*SSHKey, error) {
// convert priv key to x509 format
var pemKey = &pem.Block{
Type: "PRIVATE KEY",
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
buf := bytes.NewBufferString("")

2
db.go
View file

@ -95,7 +95,7 @@ func dbDemo(db *gorm.DB) error {
func RemoteHostFromSession(s ssh.Session, db *gorm.DB) (*Host, error) {
var host Host
db.Where("name = ?", s.User()).Find(&host)
db.Preload("SSHKey").Where("name = ?", s.User()).Find(&host)
if host.Name == "" {
// FIXME: add available hosts
return nil, fmt.Errorf("No such target: %q", s.User())

View file

@ -82,12 +82,16 @@ func (host *Host) ClientConfig(_ ssh.Session) (*gossh.ClientConfig, error) {
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
Auth: []gossh.AuthMethod{},
}
if host.SSHKey != nil {
signer, err := gossh.ParsePrivateKey([]byte(host.SSHKey.PrivKey))
if err != nil {
return nil, err
}
config.Auth = append(config.Auth, gossh.PublicKeys(signer))
}
if host.Password != "" {
config.Auth = append(config.Auth, gossh.Password(host.Password))
}
if host.SSHKey != nil {
return nil, fmt.Errorf("auth by priv key is not yet implemented")
}
if len(config.Auth) == 0 {
return nil, fmt.Errorf("no valid authentication method for host %q", host.Name)
}