var MECMClusterIP = os.Getenv("MECMClusterIP")
var APPLCMPort = os.Getenv("MECMClusterPort")
var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
-var client = http.Client{}
+var sslmode = os.Getenv("EALTSSLMode")
func httpEndPointBuider(uri string) string {
-
- return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
-
+ localURI := strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
+ if sslmode == "1" {
+ return "https://" + localURI
+ }
+ return "http://" + localURI
}
//Function to build the Get Requests for Application Package
uri = httpEndPointBuider(uri)
fmt.Println("Request URL :\t" + uri)
request, err := http.NewRequest(http.MethodGet, uri, bytes.NewBuffer(body))
- request.Header.Set("Content-Type", "application/json")
-
+ request.Header.Set(common.ContentType, common.ApplicationJson)
if err != nil {
log.Fatalln(err)
}
+ client := GetHttpClient()
response, err := client.Do(request)
if err != nil {
log.Fatalln(err)
uri = httpEndPointBuider(uri)
fmt.Println("Request URL :\t" + uri)
request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
- request.Header.Set("Content-Type", "application/json")
+ request.Header.Set(common.ContentType, common.ApplicationJson)
if err != nil {
log.Fatalln(err)
}
+ client := GetHttpClient()
response, err := client.Do(request)
if err != nil {
log.Fatalln(err)
fmt.Println("Request URL :\t" + uri)
fmt.Println("Request Body :\t" + string(body) + "\n")
request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
- request.Header.Set("Content-Type", "application/json")
+ request.Header.Set(common.ContentType, common.ApplicationJson)
if err != nil {
log.Fatalln(err)
}
+ client := GetHttpClient()
response, err := client.Do(request)
if err != nil {
log.Fatalln(err)
if err != nil {
log.Fatalln(err)
}
-
+ client := GetHttpClient()
response, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}
request, err := http.NewRequest(http.MethodPost, uri, requestBody)
- request.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
+ request.Header.Set(common.ContentType, multiPartWriter.FormDataContentType())
//request.Header.Set("Content-Type", "multipart/form-data")
if err != nil {
--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package adapter
+
+import (
+ "crypto/tls"
+ "crypto/x509"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "os"
+)
+
+var rootKeyFile = os.Getenv("CertificateKeyFile")
+var mode = os.Getenv("EALTSSLMode")
+
+func GetHttpClient() http.Client {
+ if mode == "1" {
+ client := getHttpsClient()
+ return client
+ }
+ return http.Client{}
+}
+
+func getHttpsClient() http.Client {
+
+ caCert, err := ioutil.ReadFile(rootKeyFile)
+ if err != nil {
+ log.Fatal(err)
+ }
+ caCertPool := x509.NewCertPool()
+ caCertPool.AppendCertsFromPEM(caCert)
+
+ client := &http.Client{
+ Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{
+ RootCAs: caCertPool,
+ },
+ },
+ }
+ return *client
+}