Correctly load private key by not decoding PEM 2x

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/1241/head
Hidde Beydals 4 years ago
parent bce0da2806
commit 5f9dd7a5a5

@ -18,7 +18,6 @@ package sourcesecret
import (
"bytes"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
@ -131,12 +130,7 @@ func loadKeyPair(path string) (*ssh.KeyPair, error) {
return nil, fmt.Errorf("failed to open private key file: %w", err)
}
block, _ := pem.Decode(b)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM block")
}
ppk, err := cryptssh.ParsePrivateKey(block.Bytes)
ppk, err := cryptssh.ParsePrivateKey(b)
if err != nil {
return nil, err
}

@ -0,0 +1,40 @@
/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sourcesecret
import (
"io/ioutil"
"reflect"
"testing"
)
func Test_loadKeyPair(t *testing.T) {
pk, _ := ioutil.ReadFile("testdata/rsa")
ppk, _ := ioutil.ReadFile("testdata/rsa.pub")
got, err := loadKeyPair("testdata/rsa")
if err != nil {
t.Errorf("loadKeyPair() error = %v", err)
return
}
if !reflect.DeepEqual(got.PrivateKey, pk) {
t.Errorf("PrivateKey %s != %s", got.PrivateKey, pk)
}
if !reflect.DeepEqual(got.PublicKey, ppk) {
t.Errorf("PublicKey %s != %s", got.PublicKey, ppk)
}
}

@ -0,0 +1,38 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEA3igXbgoTAydPOiEb4VfRPM4e26S16ZXhIEt95vka8wdcru97JKFl
3yK6i0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