Use fixed ssh host key

This commit is contained in:
Manfred Touron 2017-11-03 22:54:16 +01:00
parent f85c062c0d
commit 47d2d63f0d
2 changed files with 36 additions and 8 deletions

16
db.go
View file

@ -75,6 +75,22 @@ func dbInit(db *gorm.DB) error {
return err
}
}
// create host ssh key
if err := db.Table("ssh_keys").Where("name = ?", "host").Count(&count).Error; err != nil {
return err
}
if count == 0 {
key, err := NewSSHKey("rsa", 2048)
if err != nil {
return err
}
key.Name = "host"
key.Comment = "created by sshportal"
if err := db.Create(&key).Error; err != nil {
return err
}
}
return nil
}

28
main.go
View file

@ -1,7 +1,6 @@
package main
import (
"errors"
"fmt"
"log"
"os"
@ -12,6 +11,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/urfave/cli"
gossh "golang.org/x/crypto/ssh"
)
var version = "0.0.1"
@ -75,11 +75,6 @@ func server(c *cli.Context) error {
if err := dbInit(db); err != nil {
return err
}
if c.Bool("demo") {
if err := dbDemo(db); err != nil {
return err
}
}
ssh.Handle(func(s ssh.Session) {
currentUser := s.Context().Value(userContextKey).(User)
@ -113,8 +108,12 @@ func server(c *cli.Context) error {
})
opts := []ssh.Option{}
if !c.Bool("demo") {
return errors.New("use `--demo` for now")
if c.Bool("demo") {
if c.Bool("demo") {
if err := dbDemo(db); err != nil {
return err
}
}
}
opts = append(opts, ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
@ -157,6 +156,19 @@ func server(c *cli.Context) error {
return true
}))
opts = append(opts, func(srv *ssh.Server) error {
key, err := FindKeyByIdOrName(db, "host")
if err != nil {
return err
}
signer, err := gossh.ParsePrivateKey([]byte(key.PrivKey))
if err != nil {
return err
}
srv.AddHostKey(signer)
return nil
})
log.Printf("SSH Server accepting connections on %s", c.String("bind-address"))
return ssh.ListenAndServe(c.String("bind-address"), nil, opts...)
}