aboutsummaryrefslogtreecommitdiffstats
path: root/certServiceK8sExternalProvider/src
diff options
context:
space:
mode:
authorPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>2020-11-04 13:07:24 +0100
committerPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>2020-11-05 11:13:30 +0100
commitaf2527b17fb5f5242360cd908e4cae852b1f2512 (patch)
treea08341ee9ccd0b456b82d3f3034cdda34068ff42 /certServiceK8sExternalProvider/src
parent15446c2ccfc117a0feb5a5249c2e62714b2cd3d7 (diff)
[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add API exceptions handling
- Added handling exceptions from CertService API Issue-ID: OOM-2559 Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com> Change-Id: I18f00fd7e17f96b2e73b81370b54fe33f10039c3
Diffstat (limited to 'certServiceK8sExternalProvider/src')
-rw-r--r--certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go18
-rw-r--r--certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go35
2 files changed, 41 insertions, 12 deletions
diff --git a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go
index 15b90624..4806c4a1 100644
--- a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go
+++ b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go
@@ -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 {
diff --git a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go
index 06fc4792..f2b5032f 100644
--- a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go
+++ b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go
@@ -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)
}