package remote import ( "context" "errors" "fmt" "io/ioutil" "os" "strconv" "strings" "testing" "github.com/golang/mock/gomock" validatorpb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/mock" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/urfave/cli/v2" ) var validClientCert = `-----BEGIN CERTIFICATE----- MIIEITCCAgmgAwIBAgIQXUJWQZgVO4IX+zlWGI1/mTANBgkqhkiG9w0BAQsFADAU MRIwEAYDVQQDEwlBdHRlc3RhbnQwHhcNMjAwMzE3MDgwNjU3WhcNMjEwOTE3MDc1 OTUyWjASMRAwDgYDVQQDEwdjbGllbnQxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A MIIBCgKCAQEAsc977g16Tan2j7YuA+zQOlDntb4Bkfs4sDOznOEvnozHwRZOgfcP jVcA9AS5eZOGIRrsTssptrgVNDPoIHWoKk7LAKyyLM3dGp5PWeyMBoQA5cq+yPAT 4JkJpDnBFfwxXB99osJH0z3jSTRa62CSVvPRBisK4B9AlLQfcleEQlKJugy9tOAj G7zodwEi+J4AYQHmOiwL38ZsKq9We5y4HMQ0E7de0FoU5QHrtuPNrTuwVwrq825l cEAAFey6Btngx+sziysPHWHYOq4xOZ1UPBApeaAFLguzusc/4VwM7kzRNr4VOD8a eC3CtKLhBBVVxHI5ZlaHS+YylNGYD4+FxQIDAQABo3EwbzAOBgNVHQ8BAf8EBAMC A7gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB0GA1UdDgQWBBQDGCE0 3k4rHzB+Ycf3pt1MzeDPgzAfBgNVHSMEGDAWgBScIYZa4dQBIW/gVwR0ctGCuHhe 9jANBgkqhkiG9w0BAQsFAAOCAgEAHG/EfvqIwbhYfci+zRCYC7aQPuvhivJblB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gqkTHOn4j4eKS1O7/H8QQSVe5M0bocmAIbgJ4At3GnI E1JcIY2SZtSwAWs6aQPGE42gwsNCCsQWdJNtViO23JbCwlcPToC4aDfc0JJNaYqp oVV7C5jmJh9VRd2tXIXIZMMNOfThfNf2qDQuJ1S2t5KugozFiRsHUg== -----END RSA PRIVATE KEY-----` func TestNewRemoteKeymanager(t *testing.T) { tests := []struct { name string opts *Config clientCert string clientKey string caCert string err string }{ { name: "NoCertificates", opts: &Config{ RemoteCertificate: nil, }, err: "certificates are required", }, { name: "NoClientCertificate", opts: &Config{ RemoteCertificate: &CertificateConfig{}, }, err: "client certificate is required", }, { name: "NoClientKey", opts: &Config{ RemoteCertificate: &CertificateConfig{ ClientCertPath: "/foo/client.crt", ClientKeyPath: "", }, }, err: "client key is required", }, { name: "MissingClientKey", opts: &Config{ RemoteCertificate: &CertificateConfig{ ClientCertPath: "/foo/client.crt", ClientKeyPath: "/foo/client.key", CACertPath: "", }, }, err: "failed to obtain client's certificate and/or key", }, { name: "BadClientCert", clientCert: `bad`, clientKey: validClientKey, opts: &Config{ RemoteCertificate: &CertificateConfig{}, }, err: "failed to obtain client's certificate and/or key: tls: failed to find any PEM data in certificate input", }, { name: "BadClientKey", clientCert: validClientCert, clientKey: `bad`, opts: &Config{ RemoteCertificate: &CertificateConfig{}, }, err: "failed to obtain client's certificate and/or key: tls: failed to find any PEM data in key input", }, { name: "MissingCACert", clientCert: validClientCert, clientKey: validClientKey, opts: &Config{ RemoteCertificate: &CertificateConfig{ CACertPath: `bad`, }, }, err: "failed to obtain server's CA certificate: open bad: no such file or directory", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if test.caCert != "" || test.clientCert != "" || test.clientKey != "" { dir := fmt.Sprintf("%s/%s", testutil.TempDir(), test.name) require.NoError(t, os.MkdirAll(dir, 0777)) if test.caCert != "" { caCertPath := fmt.Sprintf("%s/ca.crt", dir) err := ioutil.WriteFile(caCertPath, []byte(test.caCert), params.BeaconIoConfig().ReadWritePermissions) require.NoError(t, err, "Failed to write CA certificate") test.opts.RemoteCertificate.CACertPath = caCertPath } if test.clientCert != "" { clientCertPath := fmt.Sprintf("%s/client.crt", dir) err := ioutil.WriteFile(clientCertPath, []byte(test.clientCert), params.BeaconIoConfig().ReadWritePermissions) require.NoError(t, err, "Failed to write client certificate") test.opts.RemoteCertificate.ClientCertPath = clientCertPath } if test.clientKey != "" { clientKeyPath := fmt.Sprintf("%s/client.key", dir) err := ioutil.WriteFile(clientKeyPath, []byte(test.clientKey), params.BeaconIoConfig().ReadWritePermissions) require.NoError(t, err, "Failed to write client key") test.opts.RemoteCertificate.ClientKeyPath = clientKeyPath } } _, err := NewKeymanager(&cli.Context{}, 1, test.opts) if test.err == "" { require.NoError(t, err) } else { require.ErrorContains(t, test.err, err) } }) } } func TestRemoteKeymanager_Sign(t *testing.T) { ctrl := gomock.NewController(t) m := mock.NewMockRemoteSignerClient(ctrl) k := &Keymanager{ client: m, } // Expect error handling to work. m.EXPECT().Sign( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(nil, errors.New("could not sign")) _, err := k.Sign(context.Background(), nil) if err == nil { t.Fatal("Expected error, received nil") } if !strings.Contains(err.Error(), "could not sign") { t.Errorf("Unexpected error %v", err) } // Expected proper error handling for signing response statuses. m.EXPECT().Sign( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(&validatorpb.SignResponse{ Status: validatorpb.SignResponse_FAILED, }, nil /*err*/) _, err = k.Sign(context.Background(), nil) if err == nil { t.Fatal(err) } if err != ErrSigningFailed { t.Errorf("Expected %v, received %v", ErrSigningFailed, err) } m.EXPECT().Sign( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(&validatorpb.SignResponse{ Status: validatorpb.SignResponse_DENIED, }, nil /*err*/) _, err = k.Sign(context.Background(), nil) if err == nil { t.Fatal(err) } if err != ErrSigningDenied { t.Errorf("Expected %v, received %v", ErrSigningDenied, err) } // Expected signing success. randKey := bls.RandKey() data := []byte("hello-world") sig := randKey.Sign(data) m.EXPECT().Sign( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(&validatorpb.SignResponse{ Status: validatorpb.SignResponse_SUCCEEDED, Signature: sig.Marshal(), }, nil /*err*/) resp, err := k.Sign(context.Background(), nil) require.NoError(t, err) assert.DeepEqual(t, sig.Marshal(), resp.Marshal()) } func TestRemoteKeymanager_FetchValidatingPublicKeys(t *testing.T) { ctrl := gomock.NewController(t) m := mock.NewMockRemoteSignerClient(ctrl) k := &Keymanager{ client: m, } // Expect error handling to work. m.EXPECT().ListValidatingPublicKeys( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(nil, errors.New("could not fetch keys")) _, err := k.FetchValidatingPublicKeys(context.Background()) if err == nil { t.Fatal("Expected error, received nil") } if !strings.Contains(err.Error(), "could not fetch keys") { t.Errorf("Unexpected error %v", err) } // Expect an empty response to return empty keys. m.EXPECT().ListValidatingPublicKeys( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(&validatorpb.ListPublicKeysResponse{ ValidatingPublicKeys: make([][]byte, 0), }, nil /*err*/) keys, err := k.FetchValidatingPublicKeys(context.Background()) require.NoError(t, err) assert.Equal(t, 0, len(keys), "Expected empty response") numKeys := 10 pubKeys := make([][]byte, numKeys) for i := 0; i < numKeys; i++ { key := make([]byte, 48) copy(key, strconv.Itoa(i)) pubKeys[i] = key } m.EXPECT().ListValidatingPublicKeys( gomock.Any(), // ctx gomock.Any(), // epoch ).Return(&validatorpb.ListPublicKeysResponse{ ValidatingPublicKeys: pubKeys, }, nil /*err*/) keys, err = k.FetchValidatingPublicKeys(context.Background()) require.NoError(t, err) rawKeys := make([][]byte, len(keys)) for i := 0; i < len(rawKeys); i++ { rawKeys[i] = keys[i][:] } assert.DeepEqual(t, pubKeys, rawKeys) }