Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

feat: add HealthCheck method for aws service #304

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type awsClient interface {
Sign(input *kms.SignInput) (*kms.SignOutput, error)
GetPublicKey(input *kms.GetPublicKeyInput) (*kms.GetPublicKeyOutput, error)
Verify(input *kms.VerifyInput) (*kms.VerifyOutput, error)
ListKeys(input *kms.ListKeysInput) (*kms.ListKeysOutput, error)
}

type metricsProvider interface {
Expand Down Expand Up @@ -86,6 +87,22 @@ func (s *Service) Get(keyID string) (interface{}, error) {
return keyID, nil
}

// HealthCheck check kms.
func (s *Service) HealthCheck() error {
var limit int64 = 1

result, err := s.client.ListKeys(&kms.ListKeysInput{Limit: &limit})
if err != nil {
return err
}

if len(result.Keys) == 0 {
return fmt.Errorf("list of keys are empty")
}

return nil
}

// ExportPubKeyBytes export public key.
func (s *Service) ExportPubKeyBytes(keyURI string) ([]byte, arieskms.KeyType, error) {
startTime := time.Now()
Expand Down
74 changes: 74 additions & 0 deletions pkg/aws/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,71 @@ func TestSign(t *testing.T) {
})
}

func TestHealthCheck(t *testing.T) {
t.Run("success", func(t *testing.T) {
endpoint := localhost
awsSession, err := session.NewSession(&aws.Config{
Endpoint: &endpoint,
Region: aws.String("ca"),
CredentialsChainVerboseErrors: aws.Bool(true),
})
require.NoError(t, err)

svc := New(awsSession, &mockMetrics{})

keyID := "key1"

svc.client = &mockAWSClient{listKeysFunc: func(input *kms.ListKeysInput) (*kms.ListKeysOutput, error) {
return &kms.ListKeysOutput{
Keys: []*kms.KeyListEntry{{KeyId: &keyID}},
}, nil
}}

err = svc.HealthCheck()
require.NoError(t, err)
})

t.Run("failed to list keys", func(t *testing.T) {
endpoint := localhost
awsSession, err := session.NewSession(&aws.Config{
Endpoint: &endpoint,
Region: aws.String("ca"),
CredentialsChainVerboseErrors: aws.Bool(true),
})
require.NoError(t, err)

svc := New(awsSession, &mockMetrics{})

svc.client = &mockAWSClient{listKeysFunc: func(input *kms.ListKeysInput) (*kms.ListKeysOutput, error) {
return nil, fmt.Errorf("failed to list keys")
}}

err = svc.HealthCheck()
require.Error(t, err)
require.Contains(t, err.Error(), "failed to list keys")
})

t.Run("empty keys", func(t *testing.T) {
endpoint := localhost
awsSession, err := session.NewSession(&aws.Config{
Endpoint: &endpoint,
Region: aws.String("ca"),
CredentialsChainVerboseErrors: aws.Bool(true),
})
require.NoError(t, err)

svc := New(awsSession, &mockMetrics{})

svc.client = &mockAWSClient{listKeysFunc: func(input *kms.ListKeysInput) (*kms.ListKeysOutput, error) {
return &kms.ListKeysOutput{}, nil
}}

err = svc.HealthCheck()
require.Error(t, err)
require.Contains(t, err.Error(), "list of keys are empty")
})
}

func TestGet(t *testing.T) {
t.Run("success", func(t *testing.T) {
endpoint := localhost
Expand Down Expand Up @@ -230,6 +295,7 @@ type mockAWSClient struct {
signFunc func(input *kms.SignInput) (*kms.SignOutput, error)
getPublicKeyFunc func(input *kms.GetPublicKeyInput) (*kms.GetPublicKeyOutput, error)
verifyFunc func(input *kms.VerifyInput) (*kms.VerifyOutput, error)
listKeysFunc func(input *kms.ListKeysInput) (*kms.ListKeysOutput, error)
}

func (m *mockAWSClient) Sign(input *kms.SignInput) (*kms.SignOutput, error) {
Expand All @@ -256,6 +322,14 @@ func (m *mockAWSClient) Verify(input *kms.VerifyInput) (*kms.VerifyOutput, error
return nil, nil
}

func (m *mockAWSClient) ListKeys(input *kms.ListKeysInput) (*kms.ListKeysOutput, error) {
if m.listKeysFunc != nil {
return m.listKeysFunc(input)
}

return nil, nil
}

type mockMetrics struct{}

func (m *mockMetrics) SignCount() {
Expand Down