To support SSL mode in CLI 90/3590/2
authorabhijit_onap <abhijit.das.gupta@huawei.com>
Mon, 22 Jun 2020 10:41:28 +0000 (16:11 +0530)
committerabhijit_onap <abhijit.das.gupta@huawei.com>
Mon, 22 Jun 2020 12:35:58 +0000 (18:05 +0530)
Support both HTTP and HTTPS Requests from CLI
Based on environment configuration file.

Environment Variable configurations
1. CertificateKeyFile
2. EALTSSLMode

New File Added
1. httputil.go

Added common.go

Signed-off-by: abhijit_onap <abhijit.das.gupta@huawei.com>
Change-Id: I9d62a544820e9d0fc7bab99870d8f37ff7bff96f

ocd/cli/ealt/cmd/adapter/httphelper.go
ocd/cli/ealt/cmd/adapter/httputil.go [new file with mode: 0644]
ocd/cli/ealt/cmd/common/constant.go

index b9cc9c3..b693143 100644 (file)
@@ -33,12 +33,14 @@ import (
 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
@@ -48,11 +50,11 @@ func HttpGetRequestBuilder(uri string, body []byte) {
        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)
@@ -73,11 +75,12 @@ func HttpDeleteRequestBuilder(uri string, body []byte) {
        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)
@@ -97,11 +100,12 @@ func HttpPostRequestBuilder(uri string, body []byte) error {
        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)
@@ -128,7 +132,7 @@ func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error
        if err != nil {
                log.Fatalln(err)
        }
-
+       client := GetHttpClient()
        response, err := client.Do(request)
        if err != nil {
                log.Fatalln(err)
@@ -189,7 +193,7 @@ func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.
        }
 
        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 {
diff --git a/ocd/cli/ealt/cmd/adapter/httputil.go b/ocd/cli/ealt/cmd/adapter/httputil.go
new file mode 100644 (file)
index 0000000..dc02490
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+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
+}
index 6445c69..5c0807d 100644 (file)
@@ -25,5 +25,7 @@ const (
        InstantiateUri = "/instantiate"
        TerminateUri   = "/terminate"
 
-       PATHSLASH = "/"
+       PATHSLASH       = "/"
+       ApplicationJson = "application/json"
+       ContentType     = "Content-Type"
 )