blob: 8ccd4b21ca028fe7cf05cf50118856ee27d2d300 [file] [log] [blame]
elinuxhenrikcce95ff2021-09-05 17:27:02 +02001// -
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
21package restclient
22
23import (
24 "bytes"
elinuxhenrikbfce1942021-11-08 15:58:20 +010025 "crypto/tls"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020026 "fmt"
27 "io"
elinuxhenrikbfce1942021-11-08 15:58:20 +010028 "math"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020029 "net/http"
elinuxhenrikbfce1942021-11-08 15:58:20 +010030 "net/url"
31 "time"
32
33 "github.com/hashicorp/go-retryablehttp"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020034)
35
36// HTTPClient interface
37type HTTPClient interface {
38 Get(url string) (*http.Response, error)
39
40 Do(*http.Request) (*http.Response, error)
41}
42
43type RequestError struct {
44 StatusCode int
45 Body []byte
46}
47
48func (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
elinuxhenrik65a53d22021-09-29 15:41:26 +020052func 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 {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020057 return responseData, nil
58 } else {
elinuxhenrik65a53d22021-09-29 15:41:26 +020059 return nil, err
elinuxhenrikcce95ff2021-09-05 17:27:02 +020060 }
61 } else {
elinuxhenrik65a53d22021-09-29 15:41:26 +020062 return nil, getRequestError(response)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020063 }
64 } else {
65 return nil, err
66 }
67}
68
elinuxhenrik65a53d22021-09-29 15:41:26 +020069func Put(url string, body []byte, client HTTPClient) error {
70 return do(http.MethodPut, url, body, client)
elinuxhenrik28038562021-09-21 15:43:11 +020071}
72
elinuxhenrik65a53d22021-09-29 15:41:26 +020073func Post(url string, body []byte, client HTTPClient) error {
74 return do(http.MethodPost, url, body, client)
elinuxhenrik28038562021-09-21 15:43:11 +020075}
76
elinuxhenrik65a53d22021-09-29 15:41:26 +020077func do(method string, url string, body []byte, client HTTPClient) error {
elinuxhenrik28038562021-09-21 15:43:11 +020078 if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
elinuxhenrikc4960f12021-10-28 16:27:57 +020079 req.Header.Set("Content-Type", "application/json")
elinuxhenrik65a53d22021-09-29 15:41:26 +020080 if response, respErr := client.Do(req); respErr == nil {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020081 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
94func isResponseSuccess(statusCode int) bool {
95 return statusCode >= http.StatusOK && statusCode <= 299
96}
97
98func getRequestError(response *http.Response) RequestError {
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020099 defer response.Body.Close()
elinuxhenrikcce95ff2021-09-05 17:27:02 +0200100 responseData, _ := io.ReadAll(response.Body)
101 putError := RequestError{
102 StatusCode: response.StatusCode,
103 Body: responseData,
104 }
105 return putError
106}
elinuxhenrikbfce1942021-11-08 15:58:20 +0100107
108func 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
116func 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
126func CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client {
127 return &http.Client{
128 Timeout: timeout,
129 Transport: getSecureTransportWithoutVerify(cert),
130 }
131}
132
133func 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
144func IsUrlSecure(configUrl string) bool {
145 u, _ := url.Parse(configUrl)
146 return u.Scheme == "https"
147}