Move ssh package from internal to pkg

pull/32/head
Hidde Beydals 5 years ago
parent a332e12338
commit 2dfe88b82d

@ -19,7 +19,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/fluxcd/toolkit/internal/ssh" "github.com/fluxcd/toolkit/pkg/ssh"
) )
var createSourceGitCmd = &cobra.Command{ var createSourceGitCmd = &cobra.Command{

@ -9,6 +9,10 @@ import (
"golang.org/x/crypto/ssh/knownhosts" "golang.org/x/crypto/ssh/knownhosts"
) )
// ScanHostKey collects the given host's preferred public key for the
// algorithm of the given key pair. Any errors (e.g. authentication
// failures) are ignored, except if no key could be collected from the
// host.
func ScanHostKey(host string, user string, pair *KeyPair) ([]byte, error) { func ScanHostKey(host string, user string, pair *KeyPair) ([]byte, error) {
signer, err := ssh.ParsePrivateKey(pair.PrivateKey) signer, err := ssh.ParsePrivateKey(pair.PrivateKey)
if err != nil { if err != nil {

@ -11,6 +11,7 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
// KeyPair holds the public and private key PEM block bytes.
type KeyPair struct { type KeyPair struct {
PublicKey []byte PublicKey []byte
PrivateKey []byte PrivateKey []byte
@ -41,9 +42,13 @@ func (g *RSAGenerator) Generate() (*KeyPair, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
priv, err := encodePrivateKeyToPEM(pk)
if err != nil {
return nil, err
}
return &KeyPair{ return &KeyPair{
PublicKey: pub, PublicKey: pub,
PrivateKey: encodePrivateKeyToPEM(pk), PrivateKey: priv,
}, nil }, nil
} }
@ -64,9 +69,13 @@ func (g *ECDSAGenerator) Generate() (*KeyPair, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
priv, err := encodePrivateKeyToPEM(pk)
if err != nil {
return nil, err
}
return &KeyPair{ return &KeyPair{
PublicKey: pub, PublicKey: pub,
PrivateKey: encodePrivateKeyToPEM(pk), PrivateKey: priv,
}, nil }, nil
} }
@ -79,11 +88,17 @@ func generatePublicKey(pk interface{}) ([]byte, error) {
return k, nil return k, nil
} }
func encodePrivateKeyToPEM(pk interface{}) []byte { // encodePrivateKeyToPEM encodes the given private key to a PEM block.
b, _ := x509.MarshalPKCS8PrivateKey(pk) // The encoded format is PKCS#8 for universal support of the most
// common key types (rsa, ecdsa, ed25519).
func encodePrivateKeyToPEM(pk interface{}) ([]byte, error) {
b, err := x509.MarshalPKCS8PrivateKey(pk)
if err != nil {
return nil, err
}
block := pem.Block{ block := pem.Block{
Type: "PRIVATE KEY", Type: "PRIVATE KEY",
Bytes: b, Bytes: b,
} }
return pem.EncodeToMemory(&block) return pem.EncodeToMemory(&block), nil
} }
Loading…
Cancel
Save