From 3dde536b0a932c20f8eac9a1020d8723dff233f3 Mon Sep 17 00:00:00 2001 From: abhijit_onap Date: Mon, 22 Jun 2020 16:11:28 +0530 Subject: [PATCH] To support SSL mode in CLI 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 Change-Id: I9d62a544820e9d0fc7bab99870d8f37ff7bff96f --- ocd/cli/ealt/cmd/adapter/httphelper.go | 24 +++++++++------ ocd/cli/ealt/cmd/adapter/httputil.go | 56 ++++++++++++++++++++++++++++++++++ ocd/cli/ealt/cmd/common/constant.go | 4 ++- 3 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 ocd/cli/ealt/cmd/adapter/httputil.go diff --git a/ocd/cli/ealt/cmd/adapter/httphelper.go b/ocd/cli/ealt/cmd/adapter/httphelper.go index b9cc9c3..b693143 100644 --- a/ocd/cli/ealt/cmd/adapter/httphelper.go +++ b/ocd/cli/ealt/cmd/adapter/httphelper.go @@ -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 index 0000000..dc02490 --- /dev/null +++ b/ocd/cli/ealt/cmd/adapter/httputil.go @@ -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 +} diff --git a/ocd/cli/ealt/cmd/common/constant.go b/ocd/cli/ealt/cmd/common/constant.go index 6445c69..5c0807d 100644 --- a/ocd/cli/ealt/cmd/common/constant.go +++ b/ocd/cli/ealt/cmd/common/constant.go @@ -25,5 +25,7 @@ const ( InstantiateUri = "/instantiate" TerminateUri = "/terminate" - PATHSLASH = "/" + PATHSLASH = "/" + ApplicationJson = "application/json" + ContentType = "Content-Type" ) -- 2.16.6