Bug 1086 fixed. Updated authentication mechanism 97/9097/1
authorAtul Agarwal <atul.agarwal@altran.com>
Wed, 17 Jun 2020 07:49:25 +0000 (07:49 +0000)
committerAtul Agarwal <atul.agarwal@altran.com>
Wed, 17 Jun 2020 07:50:32 +0000 (07:50 +0000)
Change-Id: I151a3082cf00279235446daf5aa590c183305657
Signed-off-by: Atul Agarwal <atul.agarwal@altran.com>
osm_mon/collector/vnf_collectors/vrops/vrops_helper.py
osm_mon/tests/unit/collector/vnf_collectors/vmware/test_vcd_collector.py
osm_mon/tests/unit/collector/vnf_collectors/vmware/test_vio_collector.py
osm_mon/tests/unit/collector/vnf_collectors/vmware/test_vrops_helper.py
osm_mon/tests/unit/collector/vnf_collectors/vmware/vmware_mocks/vrops_token.json [new file with mode: 0644]

index 75819fc..39d3238 100644 (file)
@@ -53,15 +53,32 @@ class vROPS_Helper():
         self.vrops_user = vrops_user
         self.vrops_password = vrops_password
 
+    def get_vrops_token(self):
+        """Fetches token from vrops
+        """
+        auth_url = '/suite-api/api/auth/token/acquire'
+        headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
+        req_body = {"username": self.vrops_user, "password": self.vrops_password}
+        resp = requests.post(self.vrops_site + auth_url,
+                             json=req_body, verify=False,
+                             headers=headers)
+        if resp.status_code != 200:
+            log.error("Failed to get token from vROPS: {} {}".format(resp.status_code,
+                                                                     resp.content))
+            return None
+
+        resp_data = json.loads(resp.content.decode('utf-8'))
+        return resp_data['token']
+
     def get_vm_resource_list_from_vrops(self):
         """ Find all known resource IDs in vROPs
         """
+        auth_token = self.get_vrops_token()
         api_url = '/suite-api/api/resources?resourceKind=VirtualMachine'
-        headers = {'Accept': 'application/json'}
+        headers = {'Accept': 'application/json', 'Authorization': 'vRealizeOpsToken {}'.format(auth_token)}
         resource_list = []
 
         resp = requests.get(self.vrops_site + api_url,
-                            auth=(self.vrops_user, self.vrops_password),
                             verify=False, headers=headers)
 
         if resp.status_code != 200:
@@ -117,10 +134,12 @@ class vROPS_Helper():
             # Now we can make a single call to vROPS to collect all relevant metrics for resources we need to monitor
             api_url = "/suite-api/api/resources/stats?IntervalType=MINUTES&IntervalCount=1"\
                 "&rollUpType=MAX&currentOnly=true{}{}".format(stats_key, resource_ids)
-            headers = {'Accept': 'application/json'}
+
+            auth_token = self.get_vrops_token()
+            headers = {'Accept': 'application/json', 'Authorization': 'vRealizeOpsToken {}'.format(auth_token)}
 
             resp = requests.get(self.vrops_site + api_url,
-                                auth=(self.vrops_user, self.vrops_password), verify=False, headers=headers)
+                                verify=False, headers=headers)
 
             if resp.status_code != 200:
                 log.error("Failed to get Metrics data from vROPS for {} {}".format(resp.status_code,
index 740de74..60c22a3 100644 (file)
@@ -68,6 +68,9 @@ class CollectorTest(TestCase):
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -85,6 +88,9 @@ class CollectorTest(TestCase):
         mock_vm_moref_id.return_value = None
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='404.txt', status_code=404)
@@ -95,6 +101,9 @@ class CollectorTest(TestCase):
         self.vnfd['vdu'][0]['monitoring-param'] = []
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -108,6 +117,9 @@ class CollectorTest(TestCase):
         del self.vnfd['vdu'][0]['monitoring-param']
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -122,6 +134,9 @@ class CollectorTest(TestCase):
         del self.vnfr['vdur'][1]['name']
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
index 215bd53..81e6024 100644 (file)
@@ -63,6 +63,9 @@ class CollectorTest(TestCase):
             ]
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -84,6 +87,9 @@ class CollectorTest(TestCase):
         self.vnfd['vdu'][0]['monitoring-param'] = []
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -97,6 +103,9 @@ class CollectorTest(TestCase):
         del self.vnfd['vdu'][0]['monitoring-param']
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -111,6 +120,9 @@ class CollectorTest(TestCase):
         del self.vnfr['vdur'][1]['name']
         self.mock_db.return_value.get_vnfd.return_value = self.vnfd
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
index 956a19f..846f98b 100644 (file)
@@ -40,6 +40,9 @@ class vROPS_Helper_Resource_List_Test(TestCase):
 
     def test_get_vm_resource_list_from_vrops(self):
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='vrops_resources.json')
@@ -48,6 +51,9 @@ class vROPS_Helper_Resource_List_Test(TestCase):
 
     def test_get_vm_resource_list_from_vrops_http_404(self):
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='404.txt', status_code=404)
@@ -56,6 +62,9 @@ class vROPS_Helper_Resource_List_Test(TestCase):
 
     def test_get_vm_resource_list_from_vrops_bad_json(self):
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources\\?resourceKind=VirtualMachine',
                                response_file='malformed.json')
@@ -83,6 +92,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -102,6 +114,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -123,6 +138,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -142,6 +160,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -159,6 +180,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -176,6 +200,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='OK.json')
@@ -192,6 +219,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
@@ -209,6 +239,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='404.txt', status_code=404)
@@ -226,6 +259,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='404.txt')
@@ -255,6 +291,9 @@ class vROPS_Helper_Get_Metrics_Test(TestCase):
             ]
 
         with requests_mock.Mocker() as mock_requests:
+            mock_http_response(mock_requests, method='POST',
+                               url_pattern='/suite-api/api/auth/token/acquire',
+                               response_file='vrops_token.json')
             mock_http_response(mock_requests,
                                url_pattern='/suite-api/api/resources/stats.*',
                                response_file='vrops_multi.json')
diff --git a/osm_mon/tests/unit/collector/vnf_collectors/vmware/vmware_mocks/vrops_token.json b/osm_mon/tests/unit/collector/vnf_collectors/vmware/vmware_mocks/vrops_token.json
new file mode 100644 (file)
index 0000000..59ea495
--- /dev/null
@@ -0,0 +1,7 @@
+{  
+  "_copyright_comment": "Copyright 2016-2019 VMware Inc.  This file is part of ETSI OSM  All Rights Reserved. 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. For those usages not covered by the Apache License, Version 2.0 please contact:  osslegalrouting@vmware.com",
+  "token": "8f868cca-27cc-43d6-a838-c5467e73ec45::77cea9b2-1e87-490e-b626-e878beeaa23b",  
+  "validity": 1470421325035,  
+  "expiresAt": "Friday, August 5, 2016 6:22:05 PM UTC",  
+  "roles": []
+}