1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* ============LICENSE_START=======================================================
* oom-certservice-k8s-external-provider
* ================================================================================
* Copyright (C) 2020-2021 Nokia. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package certserviceclient
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"onap.org/oom-certservice/k8s-external-provider/src/model"
)
const (
CsrHeaderName = "CSR"
PkHeaderName = "PK"
OldPkHeaderName = "OLD_PK"
OldCertificateHeaderName = "OLD_CERT"
)
type CertServiceClient interface {
GetCertificates(csr []byte, key []byte) (*CertificatesResponse, error)
CheckHealth() error
UpdateCertificate(csr []byte, key []byte, signCertificateModel model.SignCertificateModel) (*CertificatesResponse, error)
}
type CertServiceClientImpl struct {
healthUrl string
certificationUrl string
updateUrl string
httpClient HTTPClient
}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type CertificatesResponse struct {
CertificateChain []string `json:"certificateChain"`
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 {
return err
}
response, err := client.httpClient.Do(request)
if err != nil {
return err
}
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)
if err != nil {
return nil, err
}
request.Header.Add(CsrHeaderName, base64.StdEncoding.EncodeToString(csr))
request.Header.Add(PkHeaderName, base64.StdEncoding.EncodeToString(key))
return client.executeRequest(request)
}
func (client *CertServiceClientImpl) UpdateCertificate(csr []byte, key []byte, 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)
return client.executeRequest(request)
}
func (client *CertServiceClientImpl) executeRequest(request *http.Request) (*CertificatesResponse, error) {
response, err := client.httpClient.Do(request)
if err != nil {
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 {
return nil, err
}
return &certificatesResponse, err
}
|