X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Frestclient%2FHTTPClient.go;h=8ccd4b21ca028fe7cf05cf50118856ee27d2d300;hb=bfce194da9644a9498577addd4bd232dd67d0f86;hp=7c762d99e8d5fa36748a8bd3eb9232f87c763c2f;hpb=23ef6be86d3ed4f04dbba48ab41af1a1c40debf3;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/restclient/HTTPClient.go b/dmaap-mediator-producer/internal/restclient/HTTPClient.go index 7c762d99..8ccd4b21 100644 --- a/dmaap-mediator-producer/internal/restclient/HTTPClient.go +++ b/dmaap-mediator-producer/internal/restclient/HTTPClient.go @@ -22,9 +22,15 @@ package restclient import ( "bytes" + "crypto/tls" "fmt" "io" + "math" "net/http" + "net/url" + "time" + + "github.com/hashicorp/go-retryablehttp" ) // HTTPClient interface @@ -98,3 +104,44 @@ func getRequestError(response *http.Response) RequestError { } return putError } + +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 CreateClientWithoutRetry(cert tls.Certificate, timeout time.Duration) *http.Client { + return &http.Client{ + Timeout: timeout, + Transport: getSecureTransportWithoutVerify(cert), + } +} + +func getSecureTransportWithoutVerify(cert tls.Certificate) *http.Transport { + return &http.Transport{ + TLSClientConfig: &tls.Config{ + Certificates: []tls.Certificate{ + cert, + }, + InsecureSkipVerify: true, + }, + } +} + +func IsUrlSecure(configUrl string) bool { + u, _ := url.Parse(configUrl) + return u.Scheme == "https" +}