diff --git a/cmd/notary-signer/main.go b/cmd/notary-signer/main.go index ef70016b9c..3ca640c522 100644 --- a/cmd/notary-signer/main.go +++ b/cmd/notary-signer/main.go @@ -13,7 +13,7 @@ import ( "os" "path/filepath" "strings" - "time" + "time" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -149,8 +149,8 @@ func main() { log.Fatalf("failed to create a new keydbstore: %v", err) } - health.RegisterPeriodicFunc( - "DB connectable and valid", keyStore.HealthCheck, time.Second * 60) + health.RegisterPeriodicFunc( + "DB connectable and valid", keyStore.HealthCheck, time.Second*60) cryptoService := cryptoservice.NewCryptoService("", keyStore) diff --git a/signer/api/rpc_api.go b/signer/api/rpc_api.go index 626dabe29a..d810fbe271 100644 --- a/signer/api/rpc_api.go +++ b/signer/api/rpc_api.go @@ -18,13 +18,13 @@ import ( //KeyManagementServer implements the KeyManagementServer grpc interface type KeyManagementServer struct { CryptoServices signer.CryptoServiceIndex - HealthChecker func() map[string]string + HealthChecker func() map[string]string } //SignerServer implements the SignerServer grpc interface type SignerServer struct { CryptoServices signer.CryptoServiceIndex - HealthChecker func() map[string]string + HealthChecker func() map[string]string } //CreateKey returns a PublicKey created using KeyManagementServer's SigningService @@ -109,12 +109,13 @@ func (s *KeyManagementServer) GetKeyInfo(ctx context.Context, keyID *pb.KeyID) ( } //CheckHealth returns the HealthStatus with the service -func (s *KeyManagementServer) CheckHealth(ctx context.Context) (*pb.HealthStatus, error) { +func (s *KeyManagementServer) CheckHealth(ctx context.Context, v *pb.Void) (*pb.HealthStatus, error) { + logger := ctxu.GetLogger(ctx) + logger.Debug("CheckHealth: Returning HealthStatus for KeyManagementServer") - logger.Debug("CheckHealth: Returning HealthStatus for KeyManagementServer") - return &pb.HealthStatus{ - Status: s.HealthChecker(), - }, nil + return &pb.HealthStatus{ + Status: s.HealthChecker(), + }, nil } //Sign signs a message and returns the signature using a private key associate with the KeyID from the SignatureRequest @@ -149,10 +150,11 @@ func (s *SignerServer) Sign(ctx context.Context, sr *pb.SignatureRequest) (*pb.S } //CheckHealth returns the HealthStatus with the service -func (s *SignerServer) CheckHealth(ctx context.Context) (*pb.HealthStatus, error) { +func (s *SignerServer) CheckHealth(ctx context.Context, v *pb.Void) (*pb.HealthStatus, error) { + logger := ctxu.GetLogger(ctx) + logger.Debug("CheckHealth: Returning HealthStatus for SignerServer") - logger.Debug("CheckHealth: Returning HealthStatus for SignerServer") - return &pb.HealthStatus{ - Status: s.HealthChecker(), - }, nil + return &pb.HealthStatus{ + Status: s.HealthChecker(), + }, nil } diff --git a/signer/api/rpc_api_test.go b/signer/api/rpc_api_test.go index 3f2c2fed4e..9734af35ed 100644 --- a/signer/api/rpc_api_test.go +++ b/signer/api/rpc_api_test.go @@ -26,10 +26,10 @@ var ( grpcServer *grpc.Server void *pb.Void pr passphrase.Retriever - health= map[string]string { - "db": "ok", - "other": "not ok", - } + health = map[string]string{ + "db": "ok", + "other": "not ok", + } ) func init() { @@ -38,9 +38,16 @@ func init() { cryptoService := cryptoservice.NewCryptoService("", keyStore) cryptoServices := signer.CryptoServiceIndex{data.ED25519Key: cryptoService, data.RSAKey: cryptoService, data.ECDSAKey: cryptoService} void = &pb.Void{} + + fakeHealth := func() map[string]string { + return health + } + //server setup - kms := &api.KeyManagementServer{CryptoServices: cryptoServices} - ss := &api.SignerServer{CryptoServices: cryptoServices} + kms := &api.KeyManagementServer{CryptoServices: cryptoServices, + HealthChecker: fakeHealth} + ss := &api.SignerServer{CryptoServices: cryptoServices, + HealthChecker: fakeHealth} grpcServer = grpc.NewServer() pb.RegisterKeyManagementServer(grpcServer, kms) pb.RegisterSignerServer(grpcServer, ss) @@ -52,17 +59,13 @@ func init() { go grpcServer.Serve(lis) //client setup - conn, err := grpc.Dial("127.0.0.1:7899") + conn, err := grpc.Dial("127.0.0.1:7899", grpc.WithInsecure()) if err != nil { log.Fatalf("fail to dial: %v", err) } - fakeHealth := func() map[string]string { - return health - } - - kmClient = pb.NewKeyManagementClient(conn, fakeHealth) - sClient = pb.NewSignerClient(conn, fakeHealth) + kmClient = pb.NewKeyManagementClient(conn) + sClient = pb.NewSignerClient(conn) } func TestDeleteKeyHandlerReturnsNotFoundWithNonexistentKey(t *testing.T) { @@ -149,3 +152,13 @@ func TestSignReturnsNotFoundOnNonexistKeys(t *testing.T) { assert.Equal(t, grpc.Code(err), codes.NotFound) assert.Nil(t, ret) } + +func TestHealthChecksForServices(t *testing.T) { + sHealthStatus, err := sClient.CheckHealth(context.Background(), void) + assert.Nil(t, err) + assert.Equal(t, health, sHealthStatus.Status) + + kmHealthStatus, err := kmClient.CheckHealth(context.Background(), void) + assert.Nil(t, err) + assert.Equal(t, health, kmHealthStatus.Status) +} diff --git a/signer/keydbstore_test.go b/signer/keydbstore_test.go index 0a9a22df31..8e40a8be60 100644 --- a/signer/keydbstore_test.go +++ b/signer/keydbstore_test.go @@ -27,7 +27,6 @@ var anotherRetriever = func(keyName, alias string, createNew bool, attempts int) return "", false, errors.New("password alias no found") } - func TestCreateRead(t *testing.T) { tempBaseDir, err := ioutil.TempDir("", "notary-test-") defer os.RemoveAll(tempBaseDir) @@ -163,35 +162,34 @@ func TestKeyRotation(t *testing.T) { assert.Error(t, err, "password alias no found") } - func TestDBHealthCheck(t *testing.T) { - tempBaseDir, err := ioutil.TempDir("", "notary-test-") - defer os.RemoveAll(tempBaseDir) + tempBaseDir, err := ioutil.TempDir("", "notary-test-") + defer os.RemoveAll(tempBaseDir) - // We are using SQLite for the tests - db, err := sql.Open("sqlite3", tempBaseDir+"test_db") - assert.NoError(t, err) + // We are using SQLite for the tests + db, err := sql.Open("sqlite3", tempBaseDir+"test_db") + assert.NoError(t, err) - // Create a new KeyDB store - dbStore, err := NewKeyDBStore(retriever, "", "sqlite3", db) - assert.NoError(t, err) + // Create a new KeyDB store + dbStore, err := NewKeyDBStore(retriever, "", "sqlite3", db) + assert.NoError(t, err) - // No key table, health check fails - err = dbStore.HealthCheck() - assert.Error(t, err, "Cannot access table:") + // No key table, health check fails + err = dbStore.HealthCheck() + assert.Error(t, err, "Cannot access table:") - // Ensure that the private_key table exists - dbStore.db.CreateTable(&GormPrivateKey{}) + // Ensure that the private_key table exists + dbStore.db.CreateTable(&GormPrivateKey{}) - // Heath check success because the table exists - err = dbStore.HealthCheck() - assert.NoError(t, err) + // Heath check success because the table exists + err = dbStore.HealthCheck() + assert.NoError(t, err) - // Close the connection - err = dbStore.db.Close() - assert.NoError(t, err) + // Close the connection + err = dbStore.db.Close() + assert.NoError(t, err) - // Heath check fail because the connection is closed - err = dbStore.HealthCheck() - assert.Error(t, err, "Cannot access table:") + // Heath check fail because the connection is closed + err = dbStore.HealthCheck() + assert.Error(t, err, "Cannot access table:") }