diff options
author | Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com> | 2021-07-16 14:53:14 +0200 |
---|---|---|
committer | Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com> | 2021-07-20 14:27:31 +0200 |
commit | 57d9b2c0a7956306e54234233b8330628ac9f960 (patch) | |
tree | 2e923594373ea1a531d5211fc3bbe066fd8f0425 /certServiceK8sExternalProvider/src/certserviceclient | |
parent | 62cacd0f91bca52fcdce37b1f46a13757dc1dbd8 (diff) |
[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
- add csr and key params to SignCertificateModel
- correct handling error when signing csr fails
- create factory for SignCertificateModel
Issue-ID: OOM-2753
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I9bc296dfc999de0390ec90a00cbaa9dd82c89265
Diffstat (limited to 'certServiceK8sExternalProvider/src/certserviceclient')
3 files changed, 27 insertions, 27 deletions
diff --git a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go index f4cc9991..ad0bdbb9 100644 --- a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go +++ b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client.go @@ -37,9 +37,9 @@ const ( ) type CertServiceClient interface { - GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error) + GetCertificates(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) + UpdateCertificate(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) CheckHealth() error - UpdateCertificate(csr []byte, key []byte, signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) } type CertServiceClientImpl struct { @@ -80,29 +80,28 @@ func (client *CertServiceClientImpl) CheckHealth() error { return nil } -func (client *CertServiceClientImpl) GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error) { - +func (client *CertServiceClientImpl) GetCertificates(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { request, err := http.NewRequest("GET", client.certificationUrl, nil) if err != nil { return nil, err } - request.Header.Add(CsrHeaderName, base64.StdEncoding.EncodeToString(csr)) - request.Header.Add(PkHeaderName, base64.StdEncoding.EncodeToString(key)) + request.Header.Add(CsrHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.FilteredCsr)) + request.Header.Add(PkHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.PrivateKeyBytes)) return client.executeRequest(request) } -func (client *CertServiceClientImpl) UpdateCertificate(csr []byte, key []byte, signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { +func (client *CertServiceClientImpl) UpdateCertificate(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { request, err := http.NewRequest("GET", client.updateUrl, nil) if err != nil { return nil, err } - request.Header.Add(CsrHeaderName, base64.StdEncoding.EncodeToString(csr)) - request.Header.Add(PkHeaderName, base64.StdEncoding.EncodeToString(key)) - request.Header.Add(OldPkHeaderName, signCertificateModel.OldPrivateKey) - request.Header.Add(OldCertificateHeaderName, signCertificateModel.OldCertificate) + request.Header.Add(CsrHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.FilteredCsr)) + request.Header.Add(PkHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.PrivateKeyBytes)) + request.Header.Add(OldPkHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.OldPrivateKeyBytes)) + request.Header.Add(OldCertificateHeaderName, base64.StdEncoding.EncodeToString(signCertificateModel.OldCertificateBytes)) return client.executeRequest(request) } diff --git a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_mock.go b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_mock.go index a6fec1fd..0550c8fa 100644 --- a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_mock.go +++ b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_mock.go @@ -23,16 +23,16 @@ package certserviceclient import "onap.org/oom-certservice/k8s-external-provider/src/model" type CertServiceClientMock struct { - GetCertificatesFunc func(csr []byte, key []byte) (*CertificatesResponse, error) - UpdateCertificateFunc func(csr []byte, key []byte, signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) + GetCertificatesFunc func(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) + UpdateCertificateFunc func(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) } -func (client *CertServiceClientMock) UpdateCertificate(csr []byte, key []byte, signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { - return client.UpdateCertificateFunc(csr, key, signCertificateModel) +func (client *CertServiceClientMock) UpdateCertificate(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { + return client.UpdateCertificateFunc(signCertificateModel) } -func (client *CertServiceClientMock) GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error) { - return client.GetCertificatesFunc(csr, key) +func (client *CertServiceClientMock) GetCertificates(signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error) { + return client.GetCertificatesFunc(signCertificateModel) } func (client *CertServiceClientMock) CheckHealth() error { diff --git a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go index e1c6bb91..86562c01 100644 --- a/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go +++ b/certServiceK8sExternalProvider/src/certserviceclient/cert_service_client_test.go @@ -46,7 +46,7 @@ func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) certificationUrl: certificationUrl, httpClient: getMockedClient(responseJsonReader, http.StatusOK), } - response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes) + response, _ := client.GetCertificates(getTestSignCertificateModel()) assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain) assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates) } @@ -65,7 +65,7 @@ func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) }, }, } - response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes) + response, err := client.GetCertificates(getTestSignCertificateModel()) assert.Nil(t, response) assert.Error(t, err) @@ -80,7 +80,7 @@ func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testin }, }, } - response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes) + response, err := client.GetCertificates(getTestSignCertificateModel()) assert.Nil(t, response) assert.Error(t, err) @@ -93,7 +93,7 @@ func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing. certificationUrl: certificationUrl, httpClient: getMockedClient(responseJsonReader, http.StatusNotFound), } - response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes) + response, err := client.GetCertificates(getTestSignCertificateModel()) assert.Nil(t, response) assert.Error(t, err) @@ -107,12 +107,11 @@ func Test_UpdateCertificates_shouldParseCertificateResponseCorrectly(t *testing. httpClient: getMockedClient(responseJsonReader, http.StatusOK), } - response, _ := client.UpdateCertificate(testdata.CsrBytes, testdata.PkBytes, getTestSignCertificateModel()) + response, _ := client.UpdateCertificate(getTestSignCertificateModel()) assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain) assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates) } - func Test_UpdateCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) { client := CertServiceClientImpl{ updateUrl: certificateUpdateUrl, @@ -122,7 +121,7 @@ func Test_UpdateCertificates_shouldReturnError_whenHttpClientReturnsError(t *tes }, }, } - response, err := client.UpdateCertificate(testdata.CsrBytes, testdata.PkBytes, getTestSignCertificateModel()) + response, err := client.UpdateCertificate(getTestSignCertificateModel()) assert.Nil(t, response) assert.Error(t, err) @@ -135,7 +134,7 @@ func Test_UpdateCertificates_shouldReturnError_whenResponseOtherThan200(t *testi updateUrl: updateEndpoint, httpClient: getMockedClient(responseJsonReader, http.StatusNotFound), } - response, err := client.UpdateCertificate(testdata.CsrBytes, testdata.PkBytes, getTestSignCertificateModel()) + response, err := client.UpdateCertificate(getTestSignCertificateModel()) assert.Nil(t, response) assert.Error(t, err) @@ -215,8 +214,10 @@ func (client httpClientMock) Do(req *http.Request) (*http.Response, error) { func getTestSignCertificateModel() model.SignCertificateModel { testSignCertificateModel := model.SignCertificateModel{ - OldCertificate: testdata.OldCertificateEncoded, - OldPrivateKey: testdata.OldPrivateKeyEncoded, + FilteredCsr: testdata.CsrBytes, + PrivateKeyBytes: testdata.PkBytes, + OldCertificateBytes: testdata.OldCertificateBytes, + OldPrivateKeyBytes: testdata.OldPrivateKeyBytes, } return testSignCertificateModel } |