Skip to content

Commit 12c0cfa

Browse files
authored
Merge pull request #2437 from pratikjagrut/ssh.keys
fix: generate keys using ecdsa instead of rsa
2 parents b64ce55 + 96471cd commit 12c0cfa

3 files changed

Lines changed: 41 additions & 29 deletions

File tree

pkg/devspace/services/proxycommands/commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package proxycommands
33
import (
44
"encoding/base64"
55
"fmt"
6+
"strings"
7+
68
sshpkg "github.com/gliderlabs/ssh"
79
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
810
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -13,7 +15,6 @@ import (
1315
"github.com/loft-sh/devspace/pkg/devspace/services/targetselector"
1416
"github.com/loft-sh/devspace/pkg/util/tomb"
1517
"github.com/pkg/errors"
16-
"strings"
1718
)
1819

1920
var DefaultRemotePort = 10567

pkg/devspace/services/ssh/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package ssh
22

33
import (
4-
"github.com/loft-sh/devspace/pkg/util/log"
5-
"github.com/loft-sh/devspace/pkg/util/scanner"
6-
"github.com/mitchellh/go-homedir"
7-
"github.com/pkg/errors"
84
"io"
95
"os"
106
"path/filepath"
117
"strconv"
128
"strings"
139
"sync"
10+
11+
"github.com/loft-sh/devspace/pkg/util/log"
12+
"github.com/loft-sh/devspace/pkg/util/scanner"
13+
"github.com/mitchellh/go-homedir"
14+
"github.com/pkg/errors"
1415
)
1516

1617
var configLock sync.Mutex

pkg/devspace/services/ssh/keys.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
package ssh
22

33
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
46
"crypto/rand"
5-
"crypto/rsa"
67
"crypto/x509"
78
"encoding/base64"
89
"encoding/pem"
9-
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
10-
"github.com/mitchellh/go-homedir"
11-
"github.com/pkg/errors"
12-
"golang.org/x/crypto/ssh"
1310
"os"
1411
"path/filepath"
1512
"strings"
1613
"sync"
14+
15+
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
16+
"github.com/mitchellh/go-homedir"
17+
"github.com/pkg/errors"
18+
"golang.org/x/crypto/ssh"
1719
)
1820

1921
var (
2022
DevSpaceSSHFolder = "ssh"
21-
DevSpaceSSHHostKeyFile = "id_devspace_host_rsa"
22-
DevSpaceSSHPrivateKeyFile = "id_devspace_rsa"
23-
DevSpaceSSHPublicKeyFile = "id_devspace_rsa.pub"
23+
DevSpaceSSHHostKeyFile = "id_devspace_host_ecdsa"
24+
DevSpaceSSHPrivateKeyFile = "id_devspace_ecdsa"
25+
DevSpaceSSHPublicKeyFile = "id_devspace_ecdsa.pub"
2426
)
2527

2628
func init() {
@@ -33,32 +35,40 @@ func init() {
3335

3436
var keyLock sync.Mutex
3537

36-
func MakeHostKey() (string, error) {
37-
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
38+
func generatePrivateKey() (*ecdsa.PrivateKey, string, error) {
39+
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
3840
if err != nil {
39-
return "", err
41+
return nil, "", err
4042
}
4143

4244
// generate and write private key as PEM
43-
var privKeyBuf strings.Builder
44-
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
45-
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
46-
return "", err
45+
var privateKeyBuf strings.Builder
46+
b, err := x509.MarshalPKCS8PrivateKey(privateKey)
47+
if err != nil {
48+
return nil, "", err
49+
}
50+
privateKeyPEM := &pem.Block{
51+
Type: "PRIVATE KEY",
52+
Bytes: b,
53+
}
54+
if err := pem.Encode(&privateKeyBuf, privateKeyPEM); err != nil {
55+
return nil, "", err
4756
}
4857

49-
return privKeyBuf.String(), nil
58+
return privateKey, privateKeyBuf.String(), nil
5059
}
5160

52-
func MakeSSHKeyPair() (string, string, error) {
53-
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
61+
func MakeHostKey() (string, error) {
62+
_, privKeyStr, err := generatePrivateKey()
5463
if err != nil {
55-
return "", "", err
64+
return "", err
5665
}
66+
return privKeyStr, nil
67+
}
5768

58-
// generate and write private key as PEM
59-
var privKeyBuf strings.Builder
60-
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
61-
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
69+
func MakeSSHKeyPair() (string, string, error) {
70+
privateKey, privKeyStr, err := generatePrivateKey()
71+
if err != nil {
6272
return "", "", err
6373
}
6474

@@ -70,7 +80,7 @@ func MakeSSHKeyPair() (string, string, error) {
7080

7181
var pubKeyBuf strings.Builder
7282
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub))
73-
return pubKeyBuf.String(), privKeyBuf.String(), nil
83+
return pubKeyBuf.String(), privKeyStr, nil
7484
}
7585

7686
func getHostKey() (string, error) {

0 commit comments

Comments
 (0)