Fully HTTPS support in the catalog-be 56/94356/3
authork.kedron <k.kedron@partner.samsung.com>
Tue, 27 Aug 2019 16:10:09 +0000 (18:10 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Fri, 30 Aug 2019 07:22:26 +0000 (07:22 +0000)
Fully HTTPS support:
-Updated jvm configuration to support call to
the SDC components using HTTPS.
-Checkstyle in the recipes
-Add support for disableHttp flag in the chef script
-Add support for change the http to https in the python script
-Fixed the --schema param in the importONAPNormativeALL and
upgradeONAPNormative python scripts
-Checkstyle in the importX python script
-Disabled pycurl.SSL_VERIFYHOST to fix communication with the be component

Issue-ID: SDC-2501
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I588ba21638083694932a1af272f3bcbaa98268a7

13 files changed:
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/1_create_consumer_and_user.rb
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/2_check_Backend.rb
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/3_import_Normatives.rb
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/check_Backend_Health.py.erb
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/consumers.py.erb
catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/user.py.erb
catalog-be/sdc-backend/chef-repo/cookbooks/sdc-catalog-be/attributes/default.rb
catalog-be/sdc-backend/chef-repo/cookbooks/sdc-catalog-be/recipes/BE_2_setup_configuration.rb
catalog-be/sdc-backend/startup.sh
catalog-be/src/main/resources/scripts/import/tosca/importNormativeElements.py
catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py
catalog-be/src/main/resources/scripts/import/tosca/importONAPNormativeAll.py
catalog-be/src/main/resources/scripts/import/tosca/upgradeONAPNormative.py

index 14085bd..97d140a 100644 (file)
@@ -1,34 +1,45 @@
-template "/tmp/user.py" do
+if node['disableHttp']
+  protocol = "https"
+  be_port = node['BE']['https_port']
+else
+  protocol = "http"
+  be_port = node['BE']['http_port']
+end
+
+
+template "/var/tmp/user.py" do
     source "user.py.erb"
     sensitive true
     mode 0755
     variables({
+      :protocol => protocol,
       :be_ip => node['Nodes']['BE'],
-      :be_port => node['BE']['http_port']
+      :be_port => be_port
     })
 end
 
-bash "excuting-create_user" do
+bash "executing-create_user" do
    code <<-EOH
-     python /tmp/user.py
+     python /var/tmp/user.py
      rc=$?
      if [[ $rc != 0 ]]; then exit $rc; fi
    EOH
 end
 
-template "/tmp/consumers.py" do
+template "/var/tmp/consumers.py" do
     source "consumers.py.erb"
     sensitive true
     mode 0755
     variables({
+      :protocol => protocol,
       :be_ip => node['Nodes']['BE'],
-      :be_port => node['BE']['http_port']
+      :be_port => be_port
     })
 end
 
-bash "excuting-consumers" do
+bash "executing-consumers" do
    code <<-EOH
-     python /tmp/consumers.py
+     python /var/tmp/consumers.py
      rc=$?
      if [[ $rc != 0 ]]; then exit $rc; fi
    EOH
index b929637..0a551f2 100644 (file)
@@ -1,16 +1,25 @@
-template "/tmp/check_Backend_Health.py" do
+if node['disableHttp']
+  protocol = "https"
+  be_port = node['BE']['https_port']
+else
+  protocol = "http"
+  be_port = node['BE']['http_port']
+end
+
+template "/var/tmp/check_Backend_Health.py" do
     source "check_Backend_Health.py.erb"
     sensitive true
     mode 0755
     variables({
+      :protocol => protocol,
       :be_ip => node['Nodes']['BE'],
-      :be_port => node['BE']['http_port']
+      :be_port => be_port
     })
 end
 
-bash "excuting-check_Backend_Health" do
+bash "executing-check_Backend_Health" do
    code <<-EOH
-     python /tmp/check_Backend_Health.py
+     python /var/tmp/check_Backend_Health.py
      rc=$?
      if [[ $rc != 0 ]]; then exit $rc; fi
    EOH
index e8dc967..b640b3a 100644 (file)
@@ -1,37 +1,46 @@
-cookbook_file "/tmp/normatives.tar.gz" do
+be_ip=node['Nodes'][:BE]
+
+if node['disableHttp']
+  protocol = "https"
+  be_port = node['BE']['https_port']
+  param="-i #{be_ip} -p #{be_port} --scheme #{protocol}"
+else
+  protocol = "http"
+  be_port = node['BE']['http_port']
+  param="-i #{be_ip} -p #{be_port}"
+end
+
+cookbook_file "/var/tmp/normatives.tar.gz" do
       source "normatives.tar.gz"
    end
 
-be_ip=node['Nodes'][:BE]
-be_port=node['BE']['http_port']
-
-bash "excuting-import_Normatives" do
+bash "executing-import_Normatives" do
    code <<-EOH
      set -x
-     cd /tmp
-     tar xvfz /tmp/normatives.tar.gz
-     cd /tmp/normatives/scripts/import/tosca/
+     cd /var/tmp
+     tar xvfz /var/tmp/normatives.tar.gz
+     cd /var/tmp/normatives/scripts/import/tosca/
      /bin/chmod +x *.py
 
      # executing the normatives
      # add --debug=true to the importNormativeAll.py arguments to enable debug
 
      check_normative="/tmp/check_normative.out"
-     status_code=$(curl -s -o ${check_normative} -w "%{http_code}\\n" -X GET -H 'Content-Type: application/json;charset=UTF-8' -H 'USER_ID: jh0003' -H 'X-ECOMP-RequestID: cbe744a0-037b-458f-aab5-df6e543c4090' "http://#{be_ip}:#{be_port}/sdc2/rest/v1/screen")
+     status_code=$(curl -k -s -o ${check_normative} -w "%{http_code}\\n" -X GET -H 'Content-Type: application/json;charset=UTF-8' -H 'USER_ID: jh0003' -H 'X-ECOMP-RequestID: cbe744a0-037b-458f-aab5-df6e543c4090' "#{protocol}://#{be_ip}:#{be_port}/sdc2/rest/v1/screen")
      if [ "$status_code" != 200 ] ; then
         exit "$status_code"
      fi
 
-     #curl -s -X GET -H "Content-Type: application/json;charset=UTF-8" -H "USER_ID: jh0003" -H "X-ECOMP-RequestID: cbe744a0-037b-458f-aab5-df6e543c4090" "http://#{be_ip}:#{be_port}/sdc2/rest/v1/screen" > ${check_normative}
+     #curl -s -X GET -H "Content-Type: application/json;charset=UTF-8" -H "USER_ID: jh0003" -H "X-ECOMP-RequestID: cbe744a0-037b-458f-aab5-df6e543c4090" "#{protocol}://#{be_ip}:#{be_port}/sdc2/rest/v1/screen" > ${check_normative}
 
      resources_len=`cat ${check_normative}| jq '.["resources"]|length'`
      mkdir -p /var/lib/jetty/logs
      if [ $resources_len -eq 0 ] ; then
-        python importONAPNormativeAll.py -i #{be_ip} -p #{be_port} > /var/lib/jetty/logs/importNormativeAll.log
+        python importONAPNormativeAll.py #{param} > /var/lib/jetty/logs/importNormativeAll.log
             rc=$?
             if [[ $rc != 0 ]]; then exit $rc; fi
      else
-        python upgradeONAPNormative.py -i #{be_ip}  -p #{be_port} > /var/lib/jetty/logs/upgradeNormative.log
+        python upgradeONAPNormative.py #{param} > /var/lib/jetty/logs/upgradeNormative.log
             rc=$?
             if [[ $rc != 0 ]]; then exit $rc; fi
      fi
index 60e2da1..eb77f93 100644 (file)
@@ -7,6 +7,7 @@ from datetime import datetime
 beStat=0
 BE_IP="<%= @be_ip %>"
 BE_PORT="<%= @be_port %>"
+PROTOCOL="<%= @protocol %>"
 
 class bcolors:
     HEADER    = '\033[95m'
@@ -23,7 +24,7 @@ class bcolors:
 #    Functions
 ##############################
 def checkBackend():
-    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
+    command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
     (out, err) = proc.communicate()
index c593320..798d793 100644 (file)
@@ -6,6 +6,7 @@ from datetime import datetime
 
 BE_IP="<%= @be_ip %>"
 BE_PORT="<%= @be_port %>"
+PROTOCOL="<%= @protocol %>"
 
 class bcolors:
     HEADER    = '\033[95m'
@@ -22,7 +23,7 @@ class bcolors:
 #    Functions
 ##############################
 def checkBackend():
-    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i http://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
+    command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
     (out, err) = proc.communicate()
@@ -31,7 +32,7 @@ def checkBackend():
 
 
 def checkConsumer(consumerName):
-    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\"   http://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/" + consumerName
+    command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/" + consumerName
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
     (out, err) = proc.communicate()
@@ -41,7 +42,7 @@ def checkConsumer(consumerName):
 
 def createConsumer( consumerName, consumerSalt, consumerPass ):
     print '[INFO] ' + consumerName
-    command="curl -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" http://"  + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
+    command="curl -k -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://"  + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
 
index a97e451..f17ac8a 100644 (file)
@@ -6,6 +6,7 @@ from datetime import datetime
 
 BE_IP="<%= @be_ip %>"
 BE_PORT="<%= @be_port %>"
+PROTOCOL="<%= @protocol %>"
 
 class bcolors:
     HEADER    = '\033[95m'
@@ -22,7 +23,7 @@ class bcolors:
 #    Functions
 ##############################
 def checkBackend():
-    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
+    command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
     (out, err) = proc.communicate()
@@ -31,7 +32,7 @@ def checkBackend():
 
 
 def checkUser(userName):
-    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\"   http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/" + userName
+    command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/" + userName
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
     (out, err) = proc.communicate()
@@ -43,7 +44,7 @@ def checkUser(userName):
 
 def createUser( firstName, lastName, userId , email_dom , role ):
     print '[INFO] create first:[' + firstName + '], last:[' + lastName + '], Id:[' + userId + '], email:[' + userId + '@' + email_dom + '], role:[' + role +']'
-    command="curl -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/ -d '{\"firstName\": '" + firstName + "', \"lastName\": '" + lastName + "',\"userId\": '" + userId + "',\"email\": '" + userId + "@" + email_dom + "',\"role\": '" + role + "'}'"
+    command="curl -k -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/ -d '{\"firstName\": '" + firstName + "', \"lastName\": '" + lastName + "',\"userId\": '" + userId + "',\"email\": '" + userId + "@" + email_dom + "',\"role\": '" + role + "'}'"
 
     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
     (out, err) = proc.communicate()
index af1726a..d919088 100644 (file)
@@ -5,6 +5,8 @@ default['FE'][:http_port] = 8181
 default['FE'][:https_port] = 9443
 default['disableHttp'] = true
 default['cassandra'][:truststore_password] = "Aa123456"
+# TO CHANGE THE TRUSTSTORE CERT THE JVM CONFIGURATION
+# MUST BE ALSO CHANGE IN THE startup.sh FILE
 default['jetty'][:keystore_pwd] = "rTIS;B4kM]2GHcNK2c3B4&Ng"
 default['jetty'][:keymanager_pwd] = "rTIS;B4kM]2GHcNK2c3B4&Ng"
 default['jetty'][:truststore_pwd] = "Y,f975ZNJfVZhV*{+Y[}pA?0"
index 20c144c..6f455fc 100644 (file)
@@ -2,11 +2,11 @@
 replication_factor=node['cassandra']['replication_factor']
 
 if node['Pair_EnvName'] == ""
-    janusgraph_dcname_with_rep = node['cassandra']['datacenter_name'] + ","   + replication_factor.to_s
-    conf_dcname_with_rep  = node['cassandra']['datacenter_name'] + "','" + replication_factor.to_s
+   janusgraph_dcname_with_rep = node['cassandra']['datacenter_name'] + "," + replication_factor.to_s
+   conf_dcname_with_rep = node['cassandra']['datacenter_name'] + "','" + replication_factor.to_s
 else
-    janusgraph_dcname_with_rep = node['cassandra']['datacenter_name'] + ","   + replication_factor.to_s + "," + node['cassandra']['cluster_name']   + node['Pair_EnvName'] + ","   + replication_factor.to_s
-    conf_dcname_with_rep  = node['cassandra']['datacenter_name'] + "','" + replication_factor.to_s + "','" + node['cassandra']['cluster_name'] + node['Pair_EnvName'] + "','" + replication_factor.to_s
+   janusgraph_dcname_with_rep = node['cassandra']['datacenter_name'] + "," + replication_factor.to_s + "," + node['cassandra']['cluster_name'] + node['Pair_EnvName'] + "," + replication_factor.to_s
+   conf_dcname_with_rep = node['cassandra']['datacenter_name'] + "','" + replication_factor.to_s + "','" + node['cassandra']['cluster_name'] + node['Pair_EnvName'] + "','" + replication_factor.to_s
 end
 
 
index b294137..1797c75 100644 (file)
@@ -1,6 +1,13 @@
 #!/bin/sh
 
-JAVA_OPTIONS=" ${JAVA_OPTIONS} -Dconfig.home=${JETTY_BASE}/config -Dlog.home=${JETTY_BASE}/logs -Dlogback.configurationFile=${JETTY_BASE}/config/catalog-be/logback.xml -Dconfiguration.yaml=${JETTY_BASE}/config/catalog-be/configuration.yaml -Donboarding_configuration.yaml=${JETTY_BASE}/config/onboarding-be/onboarding_configuration.yaml"
+JAVA_OPTIONS=" ${JAVA_OPTIONS} \
+               -Dconfig.home=${JETTY_BASE}/config -Dlog.home=${JETTY_BASE}/logs \
+               -Dlogback.configurationFile=${JETTY_BASE}/config/catalog-be/logback.xml \
+               -Dconfiguration.yaml=${JETTY_BASE}/config/catalog-be/configuration.yaml \
+               -Donboarding_configuration.yaml=${JETTY_BASE}/config/onboarding-be/onboarding_configuration.yaml \
+               -Djavax.net.ssl.trustStore=${JETTY_BASE}/etc/org.onap.sdc.trust.jks \
+               -Djavax.net.ssl.trustStorePassword=Y,f975ZNJfVZhV*{+Y[}pA?0 \
+               -Djetty.console-capture.dir=${JETTY_BASE}/logs"
 
 cd /root/chef-solo
 chef-solo -c solo.rb -E ${ENVNAME}
index 453ee3a..3d5e9fd 100644 (file)
@@ -17,9 +17,11 @@ from importCommon import *
 #              python importUsers.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                         #
 #                                                                                                                                                                                                                                                                                                                                                      #
 #################################################################################################################################################################################
-def import_element(scheme, be_host, be_port, admin_user, exit_on_success, file_dir, url_suffix, element_name, element_form_name,
+def import_element(scheme, be_host, be_port, admin_user, exit_on_success, file_dir, url_suffix, element_name,
+                   element_form_name,
                    with_metadata=False):
-    result = createNormativeElement(scheme, be_host, be_port, admin_user, file_dir, url_suffix, element_name, element_form_name, with_metadata)
+    result = createNormativeElement(scheme, be_host, be_port, admin_user, file_dir, url_suffix, element_name,
+                                    element_form_name, with_metadata)
     print_frame_line()
     print_name_and_return_code(result[0], result[1])
     print_frame_line()
@@ -31,7 +33,6 @@ def import_element(scheme, be_host, be_port, admin_user, exit_on_success, file_d
             error_and_exit(0, None)
 
 
-
 def createNormativeElement(scheme, be_host, be_port, admin_user, file_dir, url_suffix, element_name, element_form_name,
                            with_metadata=False):
     try:
@@ -40,34 +41,37 @@ def createNormativeElement(scheme, be_host, be_port, admin_user, file_dir, url_s
         c = pycurl.Curl()
 
         url = scheme + '://' + be_host + ':' + be_port + url_suffix
-        c.setopt(c.URL, url)
-        c.setopt(c.POST, 1)
+        c.setopt(pycurl.URL, url)
+        c.setopt(pycurl.POST, 1)
 
         admin_header = 'USER_ID: ' + admin_user
         c.setopt(pycurl.HTTPHEADER, [admin_header])
 
         type_file_name = file_dir + "/" + element_name
 
-        multi_part_form_data = create_multipart_form_data(element_form_name, type_file_name, with_metadata, element_name)
+        multi_part_form_data = create_multipart_form_data(element_form_name, type_file_name, with_metadata,
+                                                          element_name)
 
         c.setopt(pycurl.HTTPPOST, multi_part_form_data)
-        c.setopt(c.WRITEFUNCTION, buffer.write)
+        c.setopt(pycurl.WRITEFUNCTION, buffer.write)
 
         if scheme == 'https':
-            c.setopt(c.SSL_VERIFYPEER, 0)
+            # security "man in middle" vulnerability
+            c.setopt(pycurl.SSL_VERIFYPEER, 0)
+            c.setopt(pycurl.SSL_VERIFYHOST, 0)
 
         c.perform()
 
-        http_res = c.getinfo(c.RESPONSE_CODE)
+        http_res = c.getinfo(pycurl.RESPONSE_CODE)
         if http_res is not None:
             debug("http response=", http_res)
         debug("response buffer", buffer.getvalue())
         c.close()
-        return (element_name, http_res, buffer.getvalue())
+        return element_name, http_res, buffer.getvalue()
 
     except Exception as inst:
         print("ERROR=" + str(inst))
-        return (element_name, None, None)
+        return element_name, None, None
 
 
 def create_multipart_form_data(element_form_name, type_file_name, with_metadata, element_name):
index 511e841..b74cead 100644 (file)
@@ -19,153 +19,158 @@ import importCommon
 #########################################################################################################################################################################################
 
 def createNormativeType(scheme, beHost, bePort, adminUser, fileDir, ELEMENT_NAME, updateversion):
-       
-       try:
-               log("in create normative type ", ELEMENT_NAME)
-               debug("userId", adminUser)
-               debug("fileDir", fileDir)
-               
-               buffer = StringIO()
-               c = pycurl.Curl()
-
-               url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart'
-               if updateversion != None:
-                       url += '?createNewVersion=' + updateversion
-               c.setopt(c.URL, url)
-               c.setopt(c.POST, 1)             
-
-               adminHeader = 'USER_ID: ' + adminUser
-               #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
-               c.setopt(pycurl.HTTPHEADER, [adminHeader])
-
-               yml_path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".yml"
-               path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".zip"
-
-               zf = zipfile.ZipFile(path, "w")
-               zf.write(yml_path, ELEMENT_NAME + '.yml')
-               zf.close()
-
-               debug(path)
-               CURRENT_JSON_FILE=fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json"
-               #sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${CURRENT_JSON_FILE}
-
-               jsonFile = open(CURRENT_JSON_FILE)
-               
-               debug("before load json")
-               json_data = json.load(jsonFile, strict=False)
-               debug(json_data)
-       
-               jsonAsStr = json.dumps(json_data)
-
-               send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
-               debug(send)
-               c.setopt(pycurl.HTTPPOST, send)         
-
-               #data = json.dumps(user)
-               #c.setopt(c.POSTFIELDS, data)   
-
-               if scheme == 'https':
-                       c.setopt(c.SSL_VERIFYPEER, 0)
-
-               #c.setopt(c.WRITEFUNCTION, lambda x: None)
-               c.setopt(c.WRITEFUNCTION, buffer.write)
-               #print("before perform")        
-               res = c.perform()
-       
-               #print("Before get response code")      
-               httpRes = c.getinfo(c.RESPONSE_CODE)
-               if (httpRes != None):
-                       debug("http response=", httpRes)
-               #print('Status: ' + str(responseCode))
-               debug(buffer.getvalue())
-               c.close()
-
-               return (ELEMENT_NAME, httpRes, buffer.getvalue())
-
-       except Exception as inst:
-               print("ERROR=" + str(inst))
-               return (ELEMENT_NAME, None, None)                               
+    try:
+        log("in create normative type ", ELEMENT_NAME)
+        debug("userId", adminUser)
+        debug("fileDir", fileDir)
+
+        buffer = StringIO()
+        c = pycurl.Curl()
+
+        url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart'
+        if updateversion != None:
+            url += '?createNewVersion=' + updateversion
+        c.setopt(pycurl.URL, url)
+        c.setopt(pycurl.POST, 1)
+
+        adminHeader = 'USER_ID: ' + adminUser
+        # c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
+        c.setopt(pycurl.HTTPHEADER, [adminHeader])
+
+        yml_path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".yml"
+        path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".zip"
+
+        zf = zipfile.ZipFile(path, "w")
+        zf.write(yml_path, ELEMENT_NAME + '.yml')
+        zf.close()
+
+        debug(path)
+        CURRENT_JSON_FILE = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json"
+        # sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${CURRENT_JSON_FILE}
+
+        jsonFile = open(CURRENT_JSON_FILE)
+
+        debug("before load json")
+        json_data = json.load(jsonFile, strict=False)
+        debug(json_data)
+
+        jsonAsStr = json.dumps(json_data)
+
+        send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
+        debug(send)
+        c.setopt(pycurl.HTTPPOST, send)
+
+        # data = json.dumps(user)
+        # c.setopt(c.POSTFIELDS, data)
+
+        if scheme == 'https':
+            # security "man in middle" vulnerability
+            c.setopt(pycurl.SSL_VERIFYPEER, 0)
+            c.setopt(pycurl.SSL_VERIFYHOST, 0)
+
+        # c.setopt(c.WRITEFUNCTION, lambda x: None)
+        c.setopt(pycurl.WRITEFUNCTION, buffer.write)
+        # print("before perform")
+        c.perform()
+
+        # print("Before get response code")
+        httpRes = c.getinfo(pycurl.RESPONSE_CODE)
+        if (httpRes != None):
+            debug("http response=", httpRes)
+        # print('Status: ' + str(responseCode))
+        debug(buffer.getvalue())
+        c.close()
+
+        return ELEMENT_NAME, httpRes, buffer.getvalue()
+
+    except Exception as inst:
+        print("ERROR=" + str(inst))
+        return ELEMENT_NAME, None, None
 
 
 def usage():
-       print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] [-v <true|false> | --updateversion=<true|false>]'
+    print sys.argv[0], \
+        '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | ' \
+        '--ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] ' \
+        '[-v <true|false> | --updateversion=<true|false>]'
 
 
 def importNormativeTypes(scheme, beHost, bePort, adminUser, fileDir, updateversion):
-       
-       normativeTypes = [ "root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database", "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer", "port", "network"]
-       #normativeTypes = [ "root" ]
-       responseCodes = [200, 201]
-       
-       if(updateversion == 'false'):
-               responseCodes = [200, 201, 409]
-       
-        results = []
-        for normativeType in normativeTypes:
-                result = createNormativeType(scheme, beHost, bePort, adminUser, fileDir, normativeType, updateversion)
-                results.append(result)
-                if ( result[1] == None or result[1] not in responseCodes ):
-                       print "Failed creating normative type " + normativeType + ". " + str(result[1])                                 
-       return results
+    normativeTypes = ["root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database",
+                      "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer",
+                      "port", "network"]
+    # normativeTypes = [ "root" ]
+    responseCodes = [200, 201]
+
+    if (updateversion == 'false'):
+        responseCodes = [200, 201, 409]
+
+    results = []
+    for normativeType in normativeTypes:
+        result = createNormativeType(scheme, beHost, bePort, adminUser, fileDir, normativeType, updateversion)
+        results.append(result)
+        if (result[1] == None or result[1] not in responseCodes):
+            print "Failed creating normative type " + normativeType + ". " + str(result[1])
+    return results
 
 
 def main(argv):
-       print 'Number of arguments:', len(sys.argv), 'arguments.'
-
-       beHost = 'localhost' 
-       bePort = '8080'
-       adminUser = 'jh0003'
-       updateversion = 'true'
-       scheme = 'http'
-
-       try:
-               opts, args = getopt.getopt(argv,"i:p:u:v:h:s:",["ip=","port=","user=","updateversion=","scheme="])
-       except getopt.GetoptError:
-               usage()
-               error_and_exit(2, 'Invalid input')
-                
-       for opt, arg in opts:
-       #print opt, arg
-               if opt == '-h':
-                       usage()                        
-                       sys.exit(3)
-               elif opt in ("-i", "--ip"):
-                       beHost = arg
-               elif opt in ("-p", "--port"):
-                       bePort = arg
-               elif opt in ("-u", "--user"):
-                       adminUser = arg
-               elif opt in ("-s", "--scheme"):
-                       scheme = arg
-               elif opt in ("-v", "--updateversion"):
-                       if (arg.lower() == "false" or arg.lower() == "no"):
-                               updateversion = 'false'
-
-       print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser, ', updateversion =', updateversion
-       
-       if ( beHost == None ):
-               usage()
-               sys.exit(3)
-
-       results = importNormativeTypes(scheme, beHost, bePort, adminUser, "../../../import/tosca/normative-types/", updateversion)
-
-       print "-----------------------------"
-       for result in results:
-               print "{0:20} | {1:6}".format(result[0], result[1])
-       print "-----------------------------"
-       
-       responseCodes = [200, 201]
-       
-       if(updateversion == 'false'):
-               responseCodes = [200, 201, 409]
-       
-       failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
-       if (len(failedNormatives) > 0):
-               error_and_exit(1, None)
-       else:
-               error_and_exit(0, None)
+    print 'Number of arguments:', len(sys.argv), 'arguments.'
+
+    beHost = 'localhost'
+    bePort = '8080'
+    adminUser = 'jh0003'
+    updateversion = 'true'
+    scheme = 'http'
+
+    try:
+        opts, args = getopt.getopt(argv, "i:p:u:v:h:s:", ["ip=", "port=", "user=", "updateversion=", "scheme="])
+    except getopt.GetoptError:
+        usage()
+        error_and_exit(2, 'Invalid input')
+
+    for opt, arg in opts:
+        # print opt, arg
+        if opt == '-h':
+            usage()
+            sys.exit(3)
+        elif opt in ("-i", "--ip"):
+            beHost = arg
+        elif opt in ("-p", "--port"):
+            bePort = arg
+        elif opt in ("-u", "--user"):
+            adminUser = arg
+        elif opt in ("-s", "--scheme"):
+            scheme = arg
+        elif opt in ("-v", "--updateversion"):
+            if (arg.lower() == "false" or arg.lower() == "no"):
+                updateversion = 'false'
+
+    print 'scheme =', scheme, ', be host =', beHost, ', be port =', bePort, ', user =', adminUser, ', updateversion =', updateversion
+
+    if (beHost == None):
+        usage()
+        sys.exit(3)
+
+    results = importNormativeTypes(scheme, beHost, bePort, adminUser, "../../../import/tosca/normative-types/",
+                                   updateversion)
+
+    print "-----------------------------"
+    for result in results:
+        print "{0:20} | {1:6}".format(result[0], result[1])
+    print "-----------------------------"
+
+    responseCodes = [200, 201]
+
+    if (updateversion == 'false'):
+        responseCodes = [200, 201, 409]
+
+    failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
+    if (len(failedNormatives) > 0):
+        error_and_exit(1, None)
+    else:
+        error_and_exit(0, None)
 
 
 if __name__ == "__main__":
-        main(sys.argv[1:])
-
+    main(sys.argv[1:])
index 30b5a54..3e9103a 100644 (file)
@@ -63,9 +63,11 @@ def main(argv):
        updateversion = 'true'
        importCommon.debugFlag = False 
        scheme = 'http'
+       opts = []
 
        try:
-               opts, args = getopt.getopt(argv,"i:p:u:d:v:h",["ip=","port=","user=","debug=","updateversion="])
+               opts, args = getopt.getopt(argv, "i:p:u:d:v:h:s",
+                                                                  ["scheme=", "ip=", "port=", "user=", "debug=", "updateversion="])
        except getopt.GetoptError:
                usage()
                error_and_exit(2, 'Invalid input')
index c6d217f..b4447c2 100644 (file)
@@ -59,9 +59,11 @@ def main(argv):
        updateOnapVersion = 'false'
        importCommon.debugFlag = False
        scheme = 'http'
+       opts = []
 
        try:
-               opts, args = getopt.getopt(argv,"i:p:u:d:h",["ip=","port=","user=","debug="])
+               opts, args = getopt.getopt(argv, "i:p:u:d:v:h:s",
+                                                                  ["scheme=", "ip=", "port=", "user=", "debug=", "updateversion="])
        except getopt.GetoptError:
                usage()
                error_and_exit(2, 'Invalid input')
@@ -103,7 +105,7 @@ def main(argv):
        importCategories(scheme, beHost, bePort, adminUser, False, fileLocation)
 
        fileLocation = baseFileLocation + "relationship-types/"
-        importNormativeRelationships(scheme, beHost, bePort, adminUser, False, fileLocation)
+       importNormativeRelationships(scheme, beHost, bePort, adminUser, False, fileLocation)
 
        fileLocation = baseFileLocation + "data-types/"
        importDataTypes(scheme, beHost, bePort, adminUser, False, fileLocation)