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"
36 type RequestError struct {
41 func (e RequestError) Error() string {
42 return fmt.Sprintf("error response with status: %v and body: %v", e.StatusCode, string(e.Body))
45 // HTTPClient interface
46 //go:generate mockery --name HTTPClient
47 type HTTPClient interface {
48 Get(url string) (*http.Response, error)
50 Do(*http.Request) (*http.Response, error)
53 func PutWithoutAuth(url string, body []byte, client HTTPClient) error {
54 return do(http.MethodPut, url, body, client)
57 func Put(url string, body string, client HTTPClient, userName string, password string) error {
58 return do(http.MethodPut, url, []byte(body), client, userName, password)
61 func Delete(url string, client HTTPClient) error {
62 return do(http.MethodDelete, url, nil, client)
65 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
66 if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
69 return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
73 func CreateRetryClient(cert tls.Certificate) *http.Client {
74 rawRetryClient := retryablehttp.NewClient()
75 rawRetryClient.RetryWaitMax = time.Minute
76 rawRetryClient.RetryMax = math.MaxInt
77 rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
79 client := rawRetryClient.StandardClient()
83 func IsUrlSecure(configUrl string) bool {
84 u, _ := url.Parse(configUrl)
85 return u.Scheme == "https"
88 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
89 return &http.Transport{
90 TLSClientConfig: &tls.Config{
91 Certificates: []tls.Certificate{
94 InsecureSkipVerify: true,
99 func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error {
100 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
102 req.Header.Set("Content-Type", "application/json; charset=utf-8")
104 if len(userInfo) > 0 {
105 req.SetBasicAuth(userInfo[0], userInfo[1])
107 if response, respErr := client.Do(req); respErr == nil {
108 if isResponseSuccess(response.StatusCode) {
111 return getResponseError(response)
121 func isResponseSuccess(statusCode int) bool {
122 return statusCode >= http.StatusOK && statusCode <= 299
125 func getResponseError(response *http.Response) RequestError {
126 defer response.Body.Close()
127 responseData, _ := io.ReadAll(response.Body)
128 putError := RequestError{
129 StatusCode: response.StatusCode,