X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Frestclient%2FHTTPClient.go;h=a7582c2ba71150e5a9608792a2ef88892c1f480d;hb=db7b5c801bdc96889aea18ab190f2b72fa8d8e06;hp=2b3a0cf3b7644b30aba34754fa33fa4c4d687c06;hpb=f1cee0f81c6bc482f73182c8f4c903e8376381e8;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/restclient/HTTPClient.go b/dmaap-mediator-producer/internal/restclient/HTTPClient.go index 2b3a0cf3..a7582c2b 100644 --- a/dmaap-mediator-producer/internal/restclient/HTTPClient.go +++ b/dmaap-mediator-producer/internal/restclient/HTTPClient.go @@ -22,11 +22,21 @@ package restclient import ( "bytes" + "crypto/tls" "fmt" "io" + "math" "net/http" + "net/url" + "time" + + "github.com/hashicorp/go-retryablehttp" + log "github.com/sirupsen/logrus" ) +const ContentTypeJSON = "application/json" +const ContentTypePlain = "text/plain" + // HTTPClient interface type HTTPClient interface { Get(url string) (*http.Response, error) @@ -61,16 +71,16 @@ func Get(url string, client HTTPClient) ([]byte, error) { } func Put(url string, body []byte, client HTTPClient) error { - return do(http.MethodPut, url, body, client) + return do(http.MethodPut, url, body, ContentTypeJSON, client) } -func Post(url string, body []byte, client HTTPClient) error { - return do(http.MethodPost, url, body, client) +func Post(url string, body []byte, contentType string, client HTTPClient) error { + return do(http.MethodPost, url, body, contentType, client) } -func do(method string, url string, body []byte, client HTTPClient) error { +func do(method string, url string, body []byte, contentType string, client HTTPClient) error { if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil { - req.Header.Set("Content-Type", "application/json; charset=utf-8") + req.Header.Set("Content-Type", contentType) if response, respErr := client.Do(req); respErr == nil { if isResponseSuccess(response.StatusCode) { return nil @@ -98,3 +108,70 @@ 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.Logger = leveledLogger{} + 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" +} + +// Used to get leveled logging in the RetryClient +type leveledLogger struct { +} + +func (ll leveledLogger) Error(msg string, keysAndValues ...interface{}) { + log.WithFields(getFields(keysAndValues)).Error(msg) +} +func (ll leveledLogger) Info(msg string, keysAndValues ...interface{}) { + log.WithFields(getFields(keysAndValues)).Info(msg) +} +func (ll leveledLogger) Debug(msg string, keysAndValues ...interface{}) { + log.WithFields(getFields(keysAndValues)).Debug(msg) +} +func (ll leveledLogger) Warn(msg string, keysAndValues ...interface{}) { + log.WithFields(getFields(keysAndValues)).Warn(msg) +} + +func getFields(keysAndValues []interface{}) log.Fields { + fields := log.Fields{} + for i := 0; i < len(keysAndValues); i = i + 2 { + fields[fmt.Sprint(keysAndValues[i])] = keysAndValues[i+1] + } + return fields +}