Coalesce private key type switch cases per review feedback.

This commit is contained in:
Stephen Fox 2019-02-28 09:56:11 -05:00
parent 672b22bd13
commit 856810e82e
1 changed files with 6 additions and 21 deletions

View File

@ -2,6 +2,7 @@ package ssh
import ( import (
"bytes" "bytes"
"crypto"
"crypto/dsa" "crypto/dsa"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
@ -12,7 +13,6 @@ import (
"fmt" "fmt"
"strings" "strings"
"golang.org/x/crypto/ed25519"
gossh "golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
) )
@ -66,17 +66,11 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
} }
switch pk := privateKey.(type) { switch pk := privateKey.(type) {
case *rsa.PrivateKey: case crypto.Signer:
publicKey, err := gossh.NewPublicKey(&pk.PublicKey) // crypto.Signer is implemented by ecdsa.PrivateKey,
if err != nil { // ed25519.PrivateKey, and rsa.PrivateKey - separate cases
return KeyPair{}, err // for each PrivateKey type would be redundant.
} publicKey, err := gossh.NewPublicKey(pk.Public())
return KeyPair{
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name),
}, nil
case *ecdsa.PrivateKey:
publicKey, err := gossh.NewPublicKey(&pk.PublicKey)
if err != nil { if err != nil {
return KeyPair{}, err return KeyPair{}, err
} }
@ -93,15 +87,6 @@ func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock, PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name), PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name),
}, nil }, nil
case *ed25519.PrivateKey:
publicKey, err := gossh.NewPublicKey(pk.Public())
if err != nil {
return KeyPair{}, err
}
return KeyPair{
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Name),
}, nil
} }
return KeyPair{}, fmt.Errorf("Cannot parse existing SSH key pair - unknown key pair type") return KeyPair{}, fmt.Errorf("Cannot parse existing SSH key pair - unknown key pair type")