Set password in secret
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
@@ -18,23 +18,82 @@ package sourcesecret
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/testdata"
|
||||
)
|
||||
|
||||
func Test_loadKeyPair(t *testing.T) {
|
||||
pk, _ := ioutil.ReadFile("testdata/rsa")
|
||||
ppk, _ := ioutil.ReadFile("testdata/rsa.pub")
|
||||
func Test_passwordLoadKeyPair(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
privateKeyPath string
|
||||
publicKeyPath string
|
||||
password string
|
||||
}{
|
||||
{
|
||||
name: "private key pair with password",
|
||||
privateKeyPath: "testdata/password_rsa",
|
||||
publicKeyPath: "testdata/password_rsa.pub",
|
||||
password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pk, _ := ioutil.ReadFile(tt.privateKeyPath)
|
||||
ppk, _ := ioutil.ReadFile(tt.publicKeyPath)
|
||||
|
||||
got, err := loadKeyPair(tt.privateKeyPath, tt.password)
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PasswordlessLoadKeyPair(t *testing.T) {
|
||||
for algo, privateKey := range testdata.PEMBytes {
|
||||
t.Run(algo, func(t *testing.T) {
|
||||
f, err := ioutil.TempFile("", "test-private-key-")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create temporary file. err: %s", err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
if _, err = f.Write(privateKey); err != nil {
|
||||
t.Fatalf("unable to write private key to file. err: %s", err)
|
||||
}
|
||||
|
||||
got, err := loadKeyPair(f.Name(), "")
|
||||
if err != nil {
|
||||
t.Errorf("loadKeyPair() error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
pk, _ := ioutil.ReadFile(f.Name())
|
||||
if !reflect.DeepEqual(got.PrivateKey, pk) {
|
||||
t.Errorf("PrivateKey %s != %s", got.PrivateKey, string(privateKey))
|
||||
}
|
||||
|
||||
signer, err := ssh.ParsePrivateKey(privateKey)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got.PublicKey, ssh.MarshalAuthorizedKey(signer.PublicKey())) {
|
||||
t.Errorf("PublicKey %s != %s", got.PublicKey, ssh.MarshalAuthorizedKey(signer.PublicKey()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user