X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fusecases%2Foruclosedlooprecovery%2Fgoversion%2Finternal%2Frestclient%2Fclient.go;h=fdd0549ad8a15466760c0d1a09f79eb267e077ef;hb=3efd775e0cea53bcb8c9afb96b375f98dffbafc8;hp=7932bfac6446bfb2cc7e0ca79b3e3537cb286f74;hpb=0af0d6013c831bfea032c34ce85e1dcf28b10855;p=nonrtric.git diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go index 7932bfac..fdd0549a 100644 --- a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go +++ b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go @@ -22,9 +22,15 @@ package restclient import ( "bytes" + "crypto/tls" "fmt" "io" + "math" "net/http" + "net/url" + "time" + + "github.com/hashicorp/go-retryablehttp" ) type RequestError struct { @@ -32,6 +38,10 @@ type RequestError struct { Body []byte } +func (e RequestError) Error() string { + return fmt.Sprintf("error response with status: %v and body: %v", e.StatusCode, string(e.Body)) +} + // HTTPClient interface type HTTPClient interface { Get(url string) (*http.Response, error) @@ -39,10 +49,6 @@ type HTTPClient interface { Do(*http.Request) (*http.Response, error) } -func (pe RequestError) Error() string { - return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body)) -} - func PutWithoutAuth(url string, body []byte, client HTTPClient) error { return do(http.MethodPut, url, body, client) } @@ -55,6 +61,40 @@ func Delete(url string, client HTTPClient) error { return do(http.MethodDelete, url, nil, client) } +func CreateClientCertificate(certPath string, keyPath string) (tls.Certificate, error) { + if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil { + return cert, nil + } else { + return tls.Certificate{}, fmt.Errorf("cannot create x509 keypair from cert file %s and key file %s due to: %v", certPath, keyPath, err) + } +} + +func CreateRetryClient(cert tls.Certificate) *http.Client { + rawRetryClient := retryablehttp.NewClient() + rawRetryClient.RetryWaitMax = time.Minute + rawRetryClient.RetryMax = math.MaxInt + rawRetryClient.HTTPClient.Transport = getSecureTransportWithoutVerify(cert) + + client := rawRetryClient.StandardClient() + return client +} + +func IsUrlSecure(configUrl string) bool { + u, _ := url.Parse(configUrl) + return u.Scheme == "https" +} + +func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport { + return &http.Transport{ + TLSClientConfig: &tls.Config{ + Certificates: []tls.Certificate{ + cert, + }, + InsecureSkipVerify: true, + }, + } +} + func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error { if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil { if body != nil {