add basic test to see if generated files are parseable
This commit is contained in:
parent
ca23dab943
commit
48199c5aa8
@ -47,17 +47,34 @@ func NewPair(public, private interface{}) (*Pair, error) {
|
||||
}
|
||||
|
||||
privBlk := &pem.Block{
|
||||
Type: "OPENSSH PRIVATE KEY",
|
||||
Type: "PRIVATE KEY",
|
||||
Headers: nil,
|
||||
Bytes: kb,
|
||||
}
|
||||
|
||||
switch private.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
privBlk.Type = "RSA PRIVATE KEY"
|
||||
publicKey, err := ssh.NewPublicKey(public)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Pair{
|
||||
Private: pem.EncodeToMemory(privBlk),
|
||||
Public: ssh.MarshalAuthorizedKey(publicKey),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func PairFromEC(key *ecdsa.PrivateKey) (*Pair, error) {
|
||||
kb, err := x509.MarshalECPrivateKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
publicKey, err := ssh.NewPublicKey(public)
|
||||
privBlk := &pem.Block{
|
||||
Type: "EC PRIVATE KEY",
|
||||
Headers: nil,
|
||||
Bytes: kb,
|
||||
}
|
||||
|
||||
publicKey, err := ssh.NewPublicKey(&key.PublicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -120,7 +137,7 @@ func GeneratePair(t Algorithm, rand io.Reader, bits int) (*Pair, error) {
|
||||
switch t {
|
||||
case DSA:
|
||||
if bits == 0 {
|
||||
bits = 3072
|
||||
bits = 1024
|
||||
}
|
||||
var sizes dsa.ParameterSizes
|
||||
switch bits {
|
||||
@ -167,7 +184,7 @@ func GeneratePair(t Algorithm, rand io.Reader, bits int) (*Pair, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewPair(&ecdsakey.PublicKey, ecdsakey)
|
||||
return PairFromEC(ecdsakey)
|
||||
case ED25519:
|
||||
publicKey, privateKey, err := ed25519.GenerateKey(rand)
|
||||
if err != nil {
|
||||
|
43
helper/communicator/sshkey/generate_test.go
Normal file
43
helper/communicator/sshkey/generate_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package sshkey
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func TestGeneratePair_parseable(t *testing.T) {
|
||||
type args struct {
|
||||
t Algorithm
|
||||
}
|
||||
tests := []struct {
|
||||
t Algorithm
|
||||
}{
|
||||
{DSA},
|
||||
{RSA},
|
||||
{ECDSA},
|
||||
{ED25519},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.t.String(), func(t *testing.T) {
|
||||
got, err := GeneratePair(tt.t, nil, 0)
|
||||
if err != nil {
|
||||
t.Errorf("GeneratePair() error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
privateKey, err := ssh.ParsePrivateKey(got.Private)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(got.Public)
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %s", err, got.Public)
|
||||
}
|
||||
if diff := cmp.Diff(privateKey.PublicKey().Marshal(), publicKey.Marshal()); diff != "" {
|
||||
t.Fatalf("wrong public key: %s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user