Skip to content

Commit

Permalink
fix: don't trim key path
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 11, 2023
1 parent 6ed0378 commit ba5b374
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 0 additions & 4 deletions keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ func WithEllipticCurve(curve elliptic.Curve) Option {
// If no key type is specified, Ed25519 will be used.
func New(path string, opts ...Option) (*SSHKeyPair, error) {
var err error
for _, kt := range []KeyType{RSA, Ed25519, ECDSA} {
path = strings.TrimSuffix(path, "_"+kt.String())
}

s := &SSHKeyPair{
path: path,
rsaBitSize: rsaDefaultBits,
Expand Down
21 changes: 20 additions & 1 deletion keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestNilSSHKeyPairWithPassphrase(t *testing.T) {
func TestNilSSHKeyPairTestdata(t *testing.T) {
for _, kt := range []KeyType{RSA, Ed25519, ECDSA} {
t.Run(fmt.Sprintf("test nil key pair for %s", kt), func(t *testing.T) {
kp, err := New(filepath.Join("testdata", "test_"+kt.String()), WithKeyType(kt))
kp, err := New(filepath.Join("testdata", "test_"+kt.String()), WithPassphrase("test"), WithKeyType(kt))
if err != nil {
t.Errorf("error creating SSH key pair: %v", err)
}
Expand Down Expand Up @@ -317,3 +317,22 @@ func TestReadingKeyWithPassphrase(t *testing.T) {
}
}
}

func TestKeynameSuffix(t *testing.T) {
for _, keyType := range []KeyType{RSA, ECDSA, Ed25519} {
t.Run("test keyname suffix", func(t *testing.T) {
fp := filepath.Join(t.TempDir(), "testkey_"+keyType.String())
_, err := New(fp, WithKeyType(keyType), WithWrite())
if err != nil {
t.Fatalf("error creating SSH key pair: %v", err)
}
if _, err := os.Stat(fp); os.IsNotExist(err) {
t.Errorf("private key file %s does not exist", fp)
}
t.Cleanup(func() {
os.Remove(fp)
os.Remove(fp + ".pub")
})
})
}
}

0 comments on commit ba5b374

Please sign in to comment.