[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add API exceptions handling 92/114592/3
authorPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Wed, 4 Nov 2020 12:07:24 +0000 (13:07 +0100)
committerPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Thu, 5 Nov 2020 10:13:30 +0000 (11:13 +0100)
- Added handling exceptions from CertService API

Issue-ID: OOM-2559
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I18f00fd7e17f96b2e73b81370b54fe33f10039c3

certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go
certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go

index 15b9062..4806c4a 100644 (file)
@@ -29,7 +29,7 @@ import (
 
 const (
        CsrHeaderName = "CSR"
-       PkHeaderName = "PK"
+       PkHeaderName  = "PK"
 )
 
 type CertServiceClient interface {
@@ -38,7 +38,7 @@ type CertServiceClient interface {
 }
 
 type CertServiceClientImpl struct {
-       healthUrl string
+       healthUrl        string
        certificationUrl string
        httpClient       HTTPClient
 }
@@ -52,6 +52,10 @@ type CertificatesResponse struct {
        TrustedCertificates []string `json:"trustedCertificates"`
 }
 
+type ResponseException struct {
+       ErrorMessage string `json:"errorMessage"`
+}
+
 func (client *CertServiceClientImpl) CheckHealth() error {
        request, err := http.NewRequest("GET", client.healthUrl, nil)
        if err != nil {
@@ -63,14 +67,13 @@ func (client *CertServiceClientImpl) CheckHealth() error {
                return err
        }
 
-       if response.StatusCode != 200 {
+       if response.StatusCode != http.StatusOK {
                return fmt.Errorf("health check retured status code [%d]", response.StatusCode)
        }
 
        return nil
 }
 
-
 func (client *CertServiceClientImpl) GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error) {
 
        request, err := http.NewRequest("GET", client.certificationUrl, nil)
@@ -85,6 +88,13 @@ func (client *CertServiceClientImpl) GetCertificates(csr []byte, key []byte) (*C
                return nil, err
        }
 
+       if response.StatusCode != http.StatusOK {
+               var responseException ResponseException
+               err = json.NewDecoder(response.Body).Decode(&responseException)
+               return nil, fmt.Errorf("CertService API returned status code [%d] and message [%s]",
+                       response.StatusCode, responseException.ErrorMessage)
+       }
+
        var certificatesResponse CertificatesResponse
        err = json.NewDecoder(response.Body).Decode(&certificatesResponse)
        if err != nil {
index 06fc479..f2b5032 100644 (file)
@@ -23,6 +23,7 @@ package certserviceclient
 import (
        "bytes"
        "fmt"
+       "io"
        "io/ioutil"
        "net/http"
        "testing"
@@ -42,14 +43,7 @@ func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T)
        responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
        client := CertServiceClientImpl{
                certificationUrl: certificationUrl,
-               httpClient:       &httpClientMock{
-                       DoFunc: func(req *http.Request) (response *http.Response, e error) {
-                               mockedResponse := &http.Response{
-                                       Body: responseJsonReader,
-                               }
-                               return mockedResponse, nil
-                       },
-               },
+               httpClient:       getMockedClient(responseJsonReader, http.StatusOK),
        }
        response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
        assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
@@ -91,6 +85,19 @@ func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testin
        assert.Error(t, err)
 }
 
+func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
+       responseJson := `{"errorMessage": "CertService API error"}`
+       responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
+       client := CertServiceClientImpl{
+               certificationUrl: certificationUrl,
+               httpClient:       getMockedClient(responseJsonReader, http.StatusNotFound),
+       }
+       response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
+
+       assert.Nil(t, response)
+       assert.Error(t, err)
+}
+
 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
        client := CertServiceClientImpl{
                certificationUrl: certificationUrl,
@@ -143,6 +150,18 @@ func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T)
        assert.Error(t, err)
 }
 
+func getMockedClient(responseJsonReader io.ReadCloser, responseCode int) *httpClientMock {
+       return &httpClientMock{
+               DoFunc: func(req *http.Request) (response *http.Response, e error) {
+                       mockedResponse := &http.Response{
+                               Body:       responseJsonReader,
+                               StatusCode: responseCode,
+                       }
+                       return mockedResponse, nil
+               },
+       }
+}
+
 type httpClientMock struct {
        DoFunc func(*http.Request) (*http.Response, error)
 }