diff options
author | hongyuzhao <zhao.hongyu@zte.com.cn> | 2020-02-19 16:11:18 +0800 |
---|---|---|
committer | hongyuzhao <zhao.hongyu@zte.com.cn> | 2020-02-19 16:51:13 +0800 |
commit | f173f592ed958f55d992c109553760e3fb12628a (patch) | |
tree | f32b1e8973318f17bf5e93cd061a27a93f9e9580 /catalog | |
parent | 36fa4d4ccf968236872e6d86c0a279ff0d9eb552 (diff) |
Subscription and notification interfaces add http basic auth support
Change-Id: I6b5a54bbd5cb989ece28c9a9344d4cba87ff6270
Issue-ID: MODELING-314
Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
Diffstat (limited to 'catalog')
-rw-r--r-- | catalog/packages/biz/notificationsutil.py | 38 | ||||
-rw-r--r-- | catalog/packages/biz/nsdm_subscription.py | 2 | ||||
-rw-r--r-- | catalog/packages/biz/vnf_pkg_subscription.py | 17 | ||||
-rw-r--r-- | catalog/packages/tests/const.py | 4 | ||||
-rw-r--r-- | catalog/packages/tests/test_nsdm_subscription.py | 5 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_pkg_subscription.py | 3 |
6 files changed, 47 insertions, 22 deletions
diff --git a/catalog/packages/biz/notificationsutil.py b/catalog/packages/biz/notificationsutil.py index 72afe33..a18c4b3 100644 --- a/catalog/packages/biz/notificationsutil.py +++ b/catalog/packages/biz/notificationsutil.py @@ -15,7 +15,9 @@ import logging import uuid import requests +import json from rest_framework import status +from requests.auth import HTTPBasicAuth from catalog.packages import const from catalog.pub.database.models import VnfPackageModel, VnfPkgSubscriptionModel, NsdmSubscriptionModel import catalog.pub.utils.timeutil @@ -72,23 +74,29 @@ class NotificationsUtil(object): serialized_data = self.notifyserializer(data=notification) if not serialized_data.is_valid(): logger.error('Notification Data is invalid:%s.' % serialized_data.errors) - self.post_notification(callbackuri, notification) - - def post_notification(self, callbackuri, notification): - """ - params = auth_info.get("paramsBasic", {}) - username, password = params.get("userName"), params.get("password") - logger.info("Sending notification to %s, %s", callbackuri, params) - resp = None - if username: - resp = requests.post(callbackuri, - data=notification, - auth=HTTPBasicAuth(username, password)) - else: - """ + if sub.auth_info: + self.post_notification(callbackuri, notification, auth_info=json.loads(sub.auth_info)) + else: + self.post_notification(callbackuri, notification) + + def post_notification(self, callbackuri, notification, auth_info=None): try: - resp = requests.post(callbackuri, data=notification, headers={'Connection': 'close'}) + if auth_info: + if const.BASIC in auth_info.get("authType", ''): + params = auth_info.get("paramsBasic", {}) + username = params.get("userName") + password = params.get("password") + resp = requests.post(callbackuri, data=notification, headers={'Connection': 'close'}, + auth=HTTPBasicAuth(username, password)) + elif const.OAUTH2_CLIENT_CREDENTIALS in auth_info.get("authType", ''): + # todo + pass + else: + # todo + pass + else: + resp = requests.post(callbackuri, data=notification, headers={'Connection': 'close'}) if resp.status_code != status.HTTP_204_NO_CONTENT: logger.error("Sending notification to %s failed: %s" % (callbackuri, resp.text)) else: diff --git a/catalog/packages/biz/nsdm_subscription.py b/catalog/packages/biz/nsdm_subscription.py index 72eded6..1673d5f 100644 --- a/catalog/packages/biz/nsdm_subscription.py +++ b/catalog/packages/biz/nsdm_subscription.py @@ -205,7 +205,7 @@ class NsdmSubscription: subscription_save_db = { "subscriptionid": self.subscription_id, "callback_uri": self.callback_uri, - "auth_info": self.authentication, + "auth_info": json.dumps(self.authentication), "links": json.dumps(links) } for filter_type in const.NSDM_NOTIFICATION_FILTERS: diff --git a/catalog/packages/biz/vnf_pkg_subscription.py b/catalog/packages/biz/vnf_pkg_subscription.py index c457bfe..ea57420 100644 --- a/catalog/packages/biz/vnf_pkg_subscription.py +++ b/catalog/packages/biz/vnf_pkg_subscription.py @@ -21,7 +21,7 @@ import uuid from collections import Counter from rest_framework import status - +from requests.auth import HTTPBasicAuth from catalog.packages import const from catalog.pub.database.models import VnfPkgSubscriptionModel from catalog.pub.exceptions import VnfPkgSubscriptionException, \ @@ -62,7 +62,20 @@ class CreateSubscription(object): logger.debug("SubscribeNotification-post::> Sending GET request " "to %s" % self.callback_uri) try: - response = requests.get(self.callback_uri, timeout=2) + if self.authentication: + if const.BASIC in self.authentication.get("authType", ''): + params = self.authentication.get("paramsBasic", {}) + username = params.get("userName") + password = params.get("password") + response = requests.get(self.callback_uri, auth=HTTPBasicAuth(username, password), timeout=2) + elif const.OAUTH2_CLIENT_CREDENTIALS in self.authentication.get("authType", ''): + # todo + pass + else: + # todo + pass + else: + response = requests.get(self.callback_uri, timeout=2) if response.status_code != status.HTTP_204_NO_CONTENT: raise VnfPkgSubscriptionException( "callbackUri %s returns %s status code." % ( diff --git a/catalog/packages/tests/const.py b/catalog/packages/tests/const.py index 79cedfc..6ad2cdb 100644 --- a/catalog/packages/tests/const.py +++ b/catalog/packages/tests/const.py @@ -590,8 +590,8 @@ vnf_subscription_data = { "BASIC" ], "paramsBasic": { - "userName": "string", - "password": "string" + "userName": "admin", + "password": "pwd1234" } } } diff --git a/catalog/packages/tests/test_nsdm_subscription.py b/catalog/packages/tests/test_nsdm_subscription.py index 0d73afd..e5e2b83 100644 --- a/catalog/packages/tests/test_nsdm_subscription.py +++ b/catalog/packages/tests/test_nsdm_subscription.py @@ -19,6 +19,7 @@ import os from django.test import TestCase from rest_framework.test import APIClient from rest_framework import status +from requests.auth import HTTPBasicAuth from catalog.packages.biz.nsdm_subscription import NsdmSubscription from catalog.pub.database.models import NsdmSubscriptionModel @@ -622,7 +623,9 @@ class TestNsdmSubscription(TestCase): } } } - mock_requests_post.assert_called_with(expect_callbackuri, data=expect_notification, headers={'Connection': 'close'}) + mock_requests_post.assert_called_with(expect_callbackuri, data=expect_notification, + auth=HTTPBasicAuth("username", "password"), + headers={'Connection': 'close'}) class NotificationTest(TestCase): diff --git a/catalog/packages/tests/test_vnf_pkg_subscription.py b/catalog/packages/tests/test_vnf_pkg_subscription.py index d2e2b5b..a8fd5d5 100644 --- a/catalog/packages/tests/test_vnf_pkg_subscription.py +++ b/catalog/packages/tests/test_vnf_pkg_subscription.py @@ -20,6 +20,7 @@ import mock from django.test import TestCase from rest_framework import status from rest_framework.test import APIClient +from requests.auth import HTTPBasicAuth import catalog.pub.utils.timeutil from catalog.packages import const @@ -248,7 +249,7 @@ class TestNfPackageSubscription(TestCase): } } mock_requests_post.assert_called_with(vnf_subscription_data["callbackUri"], data=expect_notification, - headers={'Connection': 'close'}) + headers={'Connection': 'close'}, auth=HTTPBasicAuth("admin", "pwd1234")) def test_service_query_single_subscription_not_found(self): try: |