elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 1 | // - |
| 2 | // ========================LICENSE_START================================= |
| 3 | // O-RAN-SC |
| 4 | // %% |
| 5 | // Copyright (C) 2021: Nordix Foundation |
| 6 | // %% |
| 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 |
| 10 | // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | // |
| 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=================================== |
| 19 | // |
| 20 | |
| 21 | package restclient |
| 22 | |
| 23 | import ( |
| 24 | "bytes" |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame^] | 25 | "crypto/tls" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 26 | "fmt" |
| 27 | "io" |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame^] | 28 | "math" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 29 | "net/http" |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame^] | 30 | "net/url" |
| 31 | "time" |
| 32 | |
| 33 | "github.com/hashicorp/go-retryablehttp" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | // HTTPClient interface |
| 37 | type HTTPClient interface { |
| 38 | Get(url string) (*http.Response, error) |
| 39 | |
| 40 | Do(*http.Request) (*http.Response, error) |
| 41 | } |
| 42 | |
| 43 | type RequestError struct { |
| 44 | StatusCode int |
| 45 | Body []byte |
| 46 | } |
| 47 | |
| 48 | func (pe RequestError) Error() string { |
| 49 | return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body)) |
| 50 | } |
| 51 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 52 | func Get(url string, client HTTPClient) ([]byte, error) { |
| 53 | if response, err := client.Get(url); err == nil { |
| 54 | if isResponseSuccess(response.StatusCode) { |
| 55 | defer response.Body.Close() |
| 56 | if responseData, err := io.ReadAll(response.Body); err == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 57 | return responseData, nil |
| 58 | } else { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 59 | return nil, err |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 60 | } |
| 61 | } else { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 62 | return nil, getRequestError(response) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 63 | } |
| 64 | } else { |
| 65 | return nil, err |
| 66 | } |
| 67 | } |
| 68 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 69 | func Put(url string, body []byte, client HTTPClient) error { |
| 70 | return do(http.MethodPut, url, body, client) |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 71 | } |
| 72 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 73 | func Post(url string, body []byte, client HTTPClient) error { |
| 74 | return do(http.MethodPost, url, body, client) |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 75 | } |
| 76 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 77 | func do(method string, url string, body []byte, client HTTPClient) error { |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 78 | if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 79 | req.Header.Set("Content-Type", "application/json") |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 80 | if response, respErr := client.Do(req); respErr == nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 81 | if isResponseSuccess(response.StatusCode) { |
| 82 | return nil |
| 83 | } else { |
| 84 | return getRequestError(response) |
| 85 | } |
| 86 | } else { |
| 87 | return respErr |
| 88 | } |
| 89 | } else { |
| 90 | return reqErr |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func isResponseSuccess(statusCode int) bool { |
| 95 | return statusCode >= http.StatusOK && statusCode <= 299 |
| 96 | } |
| 97 | |
| 98 | func getRequestError(response *http.Response) RequestError { |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 99 | defer response.Body.Close() |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 100 | responseData, _ := io.ReadAll(response.Body) |
| 101 | putError := RequestError{ |
| 102 | StatusCode: response.StatusCode, |
| 103 | Body: responseData, |
| 104 | } |
| 105 | return putError |
| 106 | } |
elinuxhenrik | bfce194 | 2021-11-08 15:58:20 +0100 | [diff] [blame^] | 107 | |
| 108 | func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) { |
| 109 | if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil { |
| 110 | return cert, nil |
| 111 | } else { |
| 112 | return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func CreateRetryClient(cert tls.Certificate) *http.Client { |
| 117 | rawRetryClient := retryablehttp.NewClient() |
| 118 | rawRetryClient.RetryWaitMax = time.Minute |
| 119 | rawRetryClient.RetryMax = math.MaxInt |
| 120 | rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert) |
| 121 | |
| 122 | client := rawRetryClient.StandardClient() |
| 123 | return client |
| 124 | } |
| 125 | |
| 126 | func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client { |
| 127 | return &http.Client{ |
| 128 | Timeout: timeout, |
| 129 | Transport: getSecureTransportWithoutVerify(cert), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport { |
| 134 | return &http.Transport{ |
| 135 | TLSClientConfig: &tls.Config{ |
| 136 | Certificates: []tls.Certificate{ |
| 137 | cert, |
| 138 | }, |
| 139 | InsecureSkipVerify: true, |
| 140 | }, |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func IsUrlSecure(configUrl string) bool { |
| 145 | u, _ := url.Parse(configUrl) |
| 146 | return u.Scheme == "https" |
| 147 | } |