2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
33 "github.com/hashicorp/go-retryablehttp"
34 log "github.com/sirupsen/logrus"
37 const ContentTypeJSON = "application/json"
38 const ContentTypePlain = "text/plain"
40 // HTTPClient interface
41 type HTTPClient interface {
42 Get(url string) (*http.Response, error)
44 Do(*http.Request) (*http.Response, error)
47 type RequestError struct {
52 func (pe RequestError) Error() string {
53 return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
56 func Get(url string, client HTTPClient) ([]byte, error) {
57 if response, err := client.Get(url); err == nil {
58 if isResponseSuccess(response.StatusCode) {
59 defer response.Body.Close()
60 if responseData, err := io.ReadAll(response.Body); err == nil {
61 return responseData, nil
66 return nil, getRequestError(response)
73 func Put(url string, body []byte, client HTTPClient) error {
74 return do(http.MethodPut, url, body, ContentTypeJSON, client)
77 func Post(url string, body []byte, contentType string, client HTTPClient) error {
78 return do(http.MethodPost, url, body, contentType, client)
81 func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
82 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
83 req.Header.Set("Content-Type", contentType)
84 if response, respErr := client.Do(req); respErr == nil {
85 if isResponseSuccess(response.StatusCode) {
88 return getRequestError(response)
98 func isResponseSuccess(statusCode int) bool {
99 return statusCode >= http.StatusOK && statusCode <= 299
102 func getRequestError(response *http.Response) RequestError {
103 defer response.Body.Close()
104 responseData, _ := io.ReadAll(response.Body)
105 putError := RequestError{
106 StatusCode: response.StatusCode,
112 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
113 if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
116 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
120 func CreateRetryClient(cert tls.Certificate) *http.Client {
121 rawRetryClient := retryablehttp.NewClient()
122 rawRetryClient.Logger = leveledLogger{}
123 rawRetryClient.RetryWaitMax = time.Minute
124 rawRetryClient.RetryMax = math.MaxInt
125 rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
127 client := rawRetryClient.StandardClient()
131 func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
134 Transport: getSecureTransportWithoutVerify(cert),
138 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
139 return &http.Transport{
140 TLSClientConfig: &tls.Config{
141 Certificates: []tls.Certificate{
144 InsecureSkipVerify: true,
149 func IsUrlSecure(configUrl string) bool {
150 u, _ := url.Parse(configUrl)
151 return u.Scheme == "https"
154 // Used to get leveled logging in the RetryClient
155 type leveledLogger struct {
158 func (ll leveledLogger) Error(msg string, keysAndValues ...interface{}) {
159 log.WithFields(getFields(keysAndValues)).Error(msg)
161 func (ll leveledLogger) Info(msg string, keysAndValues ...interface{}) {
162 log.WithFields(getFields(keysAndValues)).Info(msg)
164 func (ll leveledLogger) Debug(msg string, keysAndValues ...interface{}) {
165 log.WithFields(getFields(keysAndValues)).Debug(msg)
167 func (ll leveledLogger) Warn(msg string, keysAndValues ...interface{}) {
168 log.WithFields(getFields(keysAndValues)).Warn(msg)
171 func getFields(keysAndValues []interface{}) log.Fields {
172 fields := log.Fields{}
173 for i := 0; i < len(keysAndValues); i = i + 2 {
174 fields[fmt.Sprint(keysAndValues[i])] = keysAndValues[i+1]