@@ -2,20 +2,15 @@ package main
22
33import (
44 "crypto"
5- "crypto/ecdsa"
6- "crypto/elliptic"
7- "crypto/rand"
8- "crypto/rsa"
95 "fmt"
106 "io"
117 "net"
12-
138 log "github.com/Sirupsen/logrus"
149 "github.com/axsh/openvdc/hypervisor"
1510 "github.com/axsh/openvdc/model"
1611 "github.com/pkg/errors"
17- "golang.org/x/crypto/ed25519"
1812 "golang.org/x/crypto/ssh"
13+ "io/ioutil"
1914 "golang.org/x/net/context"
2015)
2116
@@ -40,31 +35,29 @@ func NewSSHServer(provider hypervisor.HypervisorProvider, ctx context.Context) *
4035
4136type HostKeyGen func (rand io.Reader ) (crypto.Signer , error )
4237
43- var KeyGenList = []HostKeyGen {
44- func (rand io.Reader ) (crypto.Signer , error ) {
45- _ , priv , err := ed25519 .GenerateKey (rand )
46- return priv , err
47- },
48- func (rand io.Reader ) (crypto.Signer , error ) {
49- return ecdsa .GenerateKey (elliptic .P521 (), rand )
50- },
51- func (rand io.Reader ) (crypto.Signer , error ) {
52- return rsa .GenerateKey (rand , 2048 )
53- },
54- }
38+ var HostRsaKeyPath string
39+ var HostEcdsaKeyPath string
40+ var HostEd25519KeyPath string
5541
42+ var KeyGenPathList = []string {
43+ HostRsaKeyPath ,
44+ HostEcdsaKeyPath ,
45+ HostEd25519KeyPath ,
46+ }
5647func (sshd * SSHServer ) Setup () error {
5748 if model .GetBackendCtx (sshd .ctx ) == nil {
5849 return errors .New ("Context does not have model connection" )
5950 }
60- for _ , gen := range KeyGenList {
61- priv , err := gen (rand .Reader )
51+ for _ , path := range KeyGenPathList {
52+ // Reading key file
53+ buf , err := ioutil .ReadFile (path )
6254 if err != nil {
63- return errors .Wrap (err , "Failed to generate host key " )
55+ return errors .Wrap (err , path + " doesn't exist " )
6456 }
65- sshSigner , err := ssh .NewSignerFromSigner (priv )
57+ // Check integrity of pem file
58+ sshSigner , err := ssh .ParsePrivateKey (buf )
6659 if err != nil {
67- return errors .Wrap (err , "Failed to convert to ssh.Signer " )
60+ return errors .Wrap (err , path + " is not a valid pem file " )
6861 }
6962 sshd .config .AddHostKey (sshSigner )
7063 }
0 commit comments