@@ -5,8 +5,10 @@ import (
5
5
"crypto/tls"
6
6
"crypto/x509"
7
7
"encoding/base64"
8
+ "encoding/pem"
8
9
"fmt"
9
10
vapi "github.com/hashicorp/vault/api"
11
+ keymanagerv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/server/keymanager/v1"
10
12
"net/http"
11
13
"os"
12
14
"testing"
@@ -743,6 +745,76 @@ func TestGetKeyErrorFromEndpoint(t *testing.T) {
743
745
require .Empty (t , resp )
744
746
}
745
747
748
+ func TestGetKeyEntry (t * testing.T ) {
749
+ fakeVaultServer := newFakeVaultServer ()
750
+ fakeVaultServer .CertAuthResponseCode = 200
751
+ fakeVaultServer .CertAuthResponse = []byte (testCertAuthResponse )
752
+ fakeVaultServer .GetKeyResponseCode = 200
753
+ fakeVaultServer .GetKeyResponse = []byte (testGetKeyResponseP256 )
754
+
755
+ s , addr , err := fakeVaultServer .NewTLSServer ()
756
+ require .NoError (t , err )
757
+
758
+ s .Start ()
759
+ defer s .Close ()
760
+
761
+ cp := & ClientParams {
762
+ VaultAddr : fmt .Sprintf ("https://%v/" , addr ),
763
+ CACertPath : testRootCert ,
764
+ ClientCertPath : testClientCert ,
765
+ ClientKeyPath : testClientKey ,
766
+ }
767
+
768
+ cc , err := NewClientConfig (cp , hclog .Default ())
769
+ require .NoError (t , err )
770
+
771
+ renewCh := make (chan struct {})
772
+ client , err := cc .NewAuthenticatedClient (CERT , renewCh )
773
+ require .NoError (t , err )
774
+
775
+ resp , err := client .getKeyEntry (context .Background (), "x509-CA-A" )
776
+ require .NoError (t , err )
777
+
778
+ block , _ := pem .Decode ([]byte ("-----BEGIN PUBLIC KEY-----\n MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEV57LFbIQZzyZ2YcKZfB9mGWkUhJv\n iRzIZOqV4wRHoUOZjMuhBMR2WviEsy65TYpcBjreAc6pbneiyhlTwPvgmw==\n -----END PUBLIC KEY-----\n " ))
779
+
780
+ require .Equal (t , "x509-CA-A" , resp .PublicKey .Id )
781
+ require .Equal (t , keymanagerv1 .KeyType_EC_P256 , resp .PublicKey .Type )
782
+ require .Equal (t , block .Bytes , resp .PublicKey .PkixData )
783
+ require .Equal (t , "afd4e26c151ce5c1069414bdb08fe5f7a7fdb271d40d077aa1f77a82e8ac5870" , resp .PublicKey .Fingerprint )
784
+ }
785
+
786
+ func TestGetKeyEntryErrorFromEndpoint (t * testing.T ) {
787
+ fakeVaultServer := newFakeVaultServer ()
788
+ fakeVaultServer .CertAuthResponseCode = 200
789
+ fakeVaultServer .CertAuthResponse = []byte (testCertAuthResponse )
790
+ fakeVaultServer .GetKeyResponseCode = 500
791
+ fakeVaultServer .GetKeyResponse = []byte ("some error" )
792
+
793
+ s , addr , err := fakeVaultServer .NewTLSServer ()
794
+ require .NoError (t , err )
795
+
796
+ s .Start ()
797
+ defer s .Close ()
798
+
799
+ cp := & ClientParams {
800
+ VaultAddr : fmt .Sprintf ("https://%v/" , addr ),
801
+ CACertPath : testRootCert ,
802
+ ClientCertPath : testClientCert ,
803
+ ClientKeyPath : testClientKey ,
804
+ }
805
+
806
+ cc , err := NewClientConfig (cp , hclog .Default ())
807
+ require .NoError (t , err )
808
+
809
+ renewCh := make (chan struct {})
810
+ client , err := cc .NewAuthenticatedClient (CERT , renewCh )
811
+ require .NoError (t , err )
812
+
813
+ resp , err := client .getKeyEntry (context .Background (), "x509-CA-A" )
814
+ spiretest .RequireGRPCStatusHasPrefix (t , err , codes .Internal , "failed to get transit engine key: Error making API request." )
815
+ require .Empty (t , resp )
816
+ }
817
+
746
818
func TestSignData (t * testing.T ) {
747
819
fakeVaultServer := newFakeVaultServer ()
748
820
fakeVaultServer .CertAuthResponseCode = 200
0 commit comments