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 //go:generate mockery --name HTTPClient
46 type HTTPClient interface {
47 Get(url string) (*http.Response, error)
49 Do(*http.Request) (*http.Response, error)
52 func PutWithoutAuth(url string, body []byte, client HTTPClient) error {
53 return do(http.MethodPut, url, body, client)
56 func Put(url string, body string, client HTTPClient, userName string, password string) error {
57 return do(http.MethodPut, url, []byte(body), client, userName, password)
60 func Delete(url string, client HTTPClient) error {
61 return do(http.MethodDelete, url, nil, client)
64 func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) {
65 if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
68 return tls.Certificate{}, fmt.Errorf("cannot create x509 key pair from cert file %s and key file %s due to: %v", certPath, keyPath, err)
72 func CreateRetryClient(cert tls.Certificate) *http.Client {
73 rawRetryClient := retryablehttp.NewClient()
74 rawRetryClient.RetryWaitMax = time.Minute
75 rawRetryClient.RetryMax = math.MaxInt
76 rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert)
78 client := rawRetryClient.StandardClient()
82 func IsUrlSecure(configUrl string) bool {
83 u, _ := url.Parse(configUrl)
84 return u.Scheme == "https"
87 func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport {
88 return &http.Transport{
89 TLSClientConfig: &tls.Config{
90 Certificates: []tls.Certificate{
93 InsecureSkipVerify: true,
98 func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error {
99 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
101 req.Header.Set("Content-Type", "application/json; charset=utf-8")
103 if len(userInfo) > 0 {
104 req.SetBasicAuth(userInfo[0], userInfo[1])
106 if response, respErr := client.Do(req); respErr == nil {
107 if isResponseSuccess(response.StatusCode) {
110 return getResponseError(response)
120 func isResponseSuccess(statusCode int) bool {
121 return statusCode >= http.StatusOK && statusCode <= 299
124 func getResponseError(response *http.Response) RequestError {
125 defer response.Body.Close()
126 responseData, _ := io.ReadAll(response.Body)
127 putError := RequestError{
128 StatusCode: response.StatusCode,