Skip to content

Commit f4a57e3

Browse files
authored
Merge pull request #1202 from Scalingo/feat/1182/ed25519_support
feat(sshkeys): add support for ed25519 keys
2 parents 13d5ca3 + ecfa39d commit f4a57e3

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* refactor: autofix by `go fix` and golangci-lint
88
* fix(error): correctly parse in case of `RequestFailedError`
99
* feat(run): add a `bash` alias for us out there often forgetting the `run` in front
10+
* feat(sshkeys): add support for ed25519 keys
1011

1112
## 1.43.3
1213

crypto/sshkeys/private_key.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ func (p *PrivateKey) signer(ctx context.Context) (ssh.Signer, error) {
3838
return nil, errors.Wrapf(ctx, err, "parse encrypted private key")
3939
}
4040

41-
signer, ok := parsedPrivateKey.(ssh.Signer)
42-
if !ok {
43-
// ssh.ParseRawPrivateKeyWithPassphrase returns an empty interface for
44-
// retro-compatibility reasons, all private key types in the standard library implement
45-
// [...] interfaces such as Signer.
46-
// Hence this error should never happen.
47-
// https://pkg.go.dev/crypto@go1.20.2#PrivateKey
48-
return nil, errors.New(ctx, "not a valid signer")
41+
signer, err := ssh.NewSignerFromKey(parsedPrivateKey)
42+
if err != nil {
43+
return nil, errors.Wrap(ctx, err, "not a valid signer")
4944
}
45+
5046
return signer, nil
5147
}
5248

crypto/sshkeys/private_key_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sshkeys
22

33
import (
4+
"context"
45
"encoding/pem"
56
"testing"
67

@@ -88,3 +89,73 @@ func TestPrivateKey_IsEncrypted(t *testing.T) {
8889
})
8990
}
9091
}
92+
93+
func TestPrivateKey_signer(t *testing.T) {
94+
const encryptedFixturePassphrase = "pipomolo"
95+
const macOSYosemitePassphrase = "scalingo"
96+
const longPassphrase = "TrushorbifbixMytyubDaphnacFoldacdeirnAykEtHyacobuc"
97+
98+
tests := map[string]struct {
99+
block *pem.Block
100+
passphrase string
101+
signerType string
102+
}{
103+
"Encrypted AES RSA Key": {
104+
block: pemDecode(aesRSA),
105+
passphrase: encryptedFixturePassphrase,
106+
signerType: "ssh-rsa",
107+
},
108+
"Encrypted AES RSA Key (Mac OS Maverick)": {
109+
block: pemDecode(aesRSAMacOSMaverick),
110+
passphrase: encryptedFixturePassphrase,
111+
signerType: "ssh-rsa",
112+
},
113+
"Encrypted AES RSA Key (Mac OS Yosemite)": {
114+
block: pemDecode(aesRSAMacOSYosemite),
115+
passphrase: macOSYosemitePassphrase,
116+
signerType: "ssh-rsa",
117+
},
118+
"Encrypted DES3 RSA Key": {
119+
block: pemDecode(des3RSA),
120+
passphrase: encryptedFixturePassphrase,
121+
signerType: "ssh-rsa",
122+
},
123+
"Encrypted DES3 RSA Key with Long Passphrase": {
124+
block: pemDecode(des3RSALongPassphrase),
125+
passphrase: longPassphrase,
126+
signerType: "ssh-rsa",
127+
},
128+
"Encrypted OpenSSH RSA Key": {
129+
block: pemDecode(openSSHRSA),
130+
passphrase: encryptedFixturePassphrase,
131+
signerType: "ssh-rsa",
132+
},
133+
"Encrypted OpenSSH ecdsa Key": {
134+
block: pemDecode(openSSHecdsa),
135+
passphrase: encryptedFixturePassphrase,
136+
signerType: "ecdsa-sha2-nistp256",
137+
},
138+
"Encrypted OpenSSH ed25519 Key": {
139+
block: pemDecode(openSSHed25519),
140+
passphrase: encryptedFixturePassphrase,
141+
signerType: "ssh-ed25519",
142+
},
143+
}
144+
145+
for name, test := range tests {
146+
t.Run(name, func(t *testing.T) {
147+
privateKey := &PrivateKey{
148+
Path: "n/a",
149+
Block: test.block,
150+
PasswordMethod: func(ctx context.Context, prompt string) (string, error) {
151+
return test.passphrase, nil
152+
},
153+
}
154+
155+
signer, err := privateKey.signer(t.Context())
156+
require.NoError(t, err)
157+
require.NotNil(t, signer)
158+
require.Equal(t, test.signerType, signer.PublicKey().Type())
159+
})
160+
}
161+
}

0 commit comments

Comments
 (0)