def test_no_proxy_connect(self):
     # Open connection without proxy
     proxy = AVProxy()
     #     using an url
     response = proxy.open("http://python.org")
     assert response is not None
     del response
     #     using a request
     request = urllib2.Request("http://python.org")
     response = proxy.open(request)
     assert response is not None
Пример #2
0
    def make_request(self, url):
        """Make a request against the OTX server
        Args:
            url (str): The url with the request.
        Returns:
            response_data(json): The OTX response
            Raise an exception when something is wrong
        """
        proxy = AVProxy()
        if proxy is None:
            api_log.error("Connection error with AVProxy")
        try:
            request = urllib2.Request(url)
            request.add_header('X-OTX-API-KEY', self.key)
            response = proxy.open(request, timeout=20, retries=3)
            response_data = json.loads(response.read(), encoding="utf-8")
        except urllib2.URLError as err:
            if err.code == 403:
                raise InvalidAPIKey("Invalid API Key")
            elif err.code == 400:
                raise BadRequest("Bad Request")
            else:
                raise Exception(str(err))
        except Exception as err:
            raise Exception(str(err))

        return response_data
Пример #3
0
    def make_request(self, url):
        """Make a request against the OTX server
        Args:
            url (str): The url with the request.
        Returns:
            response_data(json): The OTX response
            Raise an exception when something is wrong
        """
        proxy = AVProxy()
        if proxy is None:
            api_log.error("Connection error with AVProxy")
        try:
            request = urllib2.Request(url)
            request.add_header('X-OTX-API-KEY', self.key)
            response = proxy.open(request, timeout=20, retries=3)
            response_data = json.loads(response.read(), encoding="utf-8")
        except urllib2.URLError as err:
            if err.code == 403:
                raise InvalidAPIKey("Invalid API Key")
            elif err.code == 400:
                raise BadRequest("Bad Request")
            else:
                raise Exception(str(err))
        except Exception as err:
            raise Exception(str(err))

        return response_data
Пример #4
0
def get_message_center_messages():
    """Retrieves the list of messages from the MCServer
    Args:
        None
    Returns:
        messag_list: List of messages"""
    messages = []
    conn_failed = False

    try:
        # First we need to retrieve the list of tags for the current system.
        # If this call fails, we shouldn't make the mcserver request
        proxy = AVProxy()
        if proxy is None:
            app.logger.error("Connection error with AVProxy")

        system_tags = get_system_tags()
        if len(system_tags) == 0:
            return []
        revision = get_latest_message_revision()
        msg_filter = "filters=%s" % ','.join(system_tags)
        if revision is not None:
            msg_filter += "&revision=%s" % revision

        url = 'https://%s:%s/messages?%s' % (
            app.config['MESSAGE_CENTER_SERVER'],
            app.config['MESSAGE_CENTER_PORT'], msg_filter)
        request = urllib2.Request(url)
        response = proxy.open(request, timeout=20, retries=3)
        response_data = json.loads(response.read())
        response_code = response.getcode()

        if response_code != 200:
            app.logger.warning("Invalid reponse from the mcserver %s:%s" %
                               (response_code, response_data))
        for field in ['data', 'status', 'signature']:
            if field not in response_data:
                return []

        # Validate the data
        if not verify_sign(response_data['signature'], response_data['data']):
            app.logger.warning(
                "Cannot verify the data comimg from the mcserver")
            return [], False

        messages = ast.literal_eval(b64decode(response_data['data']))
        if 'revision' in response_data:
            save_messages_revision(response_data['revision'])

    except (URLError, HTTPError):
        conn_failed = True
        app.logger.error("Cannot connect to the Message Center Server")

    except Exception:
        import traceback
        app.logger.error(
            "An error occurred while retrieving the Message Center Server messages: %s"
            % str(traceback.format_exc()))

    return messages, conn_failed
Пример #5
0
    def test_no_proxy_connect_with_request(self, mock_opener):
        """ AVProxy: Open connection without proxy using request """
        expected_response = 'response OK'
        mock_opener.return_value.open.return_value = expected_response

        proxy = AVProxy()
        request = urllib2.Request("http://python.org")
        response = proxy.open(request)
        self.assertEqual(expected_response, response)
        call_args, _ = mock_opener.return_value.open.call_args
        req_obj_from_call = call_args[0]
        self.assertEqual({}, req_obj_from_call.headers)
        self.assertIsInstance(req_obj_from_call, urllib2.Request)
Пример #6
0
    def test_no_proxy_connect_with_url(self, mock_opener):
        """ AVProxy: Open connection without proxy using url """
        expected_response = 'response OK'
        mock_opener.return_value.open.return_value = expected_response

        proxy = AVProxy()
        response = proxy.open("http://python.org", timeout=2)
        self.assertEqual(expected_response, response)
        call_args, call_kwargs = mock_opener.return_value.open.call_args
        req_obj_from_call = call_args[0]
        self.assertEqual(AVProxy.USER_AGENT, req_obj_from_call.get_header('User-agent'))
        self.assertIsInstance(req_obj_from_call, urllib2.Request)
        self.assertEqual(call_kwargs, {'timeout': 2})
Пример #7
0
    def test_no_proxy_connect_with_request(self, mock_opener):
        """ AVProxy: Open connection without proxy using request """
        expected_response = 'response OK'
        mock_opener.return_value.open.return_value = expected_response

        proxy = AVProxy()
        request = urllib2.Request("http://python.org")
        response = proxy.open(request)
        self.assertEqual(expected_response, response)
        call_args, _ = mock_opener.return_value.open.call_args
        req_obj_from_call = call_args[0]
        self.assertEqual({}, req_obj_from_call.headers)
        self.assertIsInstance(req_obj_from_call, urllib2.Request)
Пример #8
0
    def test_no_proxy_connect_with_url(self, mock_opener):
        """ AVProxy: Open connection without proxy using url """
        expected_response = 'response OK'
        mock_opener.return_value.open.return_value = expected_response

        proxy = AVProxy()
        response = proxy.open("http://python.org", timeout=2)
        self.assertEqual(expected_response, response)
        call_args, call_kwargs = mock_opener.return_value.open.call_args
        req_obj_from_call = call_args[0]
        self.assertEqual(AVProxy.USER_AGENT,
                         req_obj_from_call.get_header('User-agent'))
        self.assertIsInstance(req_obj_from_call, urllib2.Request)
        self.assertEqual(call_kwargs, {'timeout': 2})
Пример #9
0
def get_message_center_messages():
    """Retrieves the list of messages from the MCServer
    Args:
        None
    Returns:
        messag_list: List of messages"""
    messages = []
    conn_failed = False

    try:
        # First we need to retrieve the list of tags for the current system.
        # If this call fails, we shouldn't make the mcserver request
        proxy = AVProxy()
        if proxy is None:
            app.logger.error("Connection error with AVProxy")

        system_tags = get_system_tags()
        if len(system_tags) == 0:
            return []
        revision = get_latest_message_revision()
        msg_filter = "filters=%s" % ",".join(system_tags)
        if revision is not None:
            msg_filter += "&revision=%s" % revision

        url = "https://%s:%s/messages?%s" % (
            app.config["MESSAGE_CENTER_SERVER"],
            app.config["MESSAGE_CENTER_PORT"],
            msg_filter,
        )
        request = urllib2.Request(url)
        response = proxy.open(request, timeout=20, retries=3)
        response_data = json.loads(response.read())
        response_code = response.getcode()

        if response_code != 200:
            app.logger.warning("Invalid reponse from the mcserver %s:%s" % (response_code, response_data))
        for field in ["data", "status", "signature"]:
            if field not in response_data:
                return []

        # Validate the data
        if not verify_sign(response_data["signature"], response_data["data"]):
            app.logger.warning("Cannot verify the data comimg from the mcserver")
            return [], False

        messages = ast.literal_eval(b64decode(response_data["data"]))
        if "revision" in response_data:
            save_messages_revision(response_data["revision"])

    except (URLError, HTTPError):
        conn_failed = True
        app.logger.error("Cannot connect to the Message Center Server")

    except Exception:
        import traceback

        app.logger.error(
            "An error occurred while retrieving the Message Center Server messages: %s" % str(traceback.format_exc())
        )

    return messages, conn_failed
 def test_bad_proxy_connect_url_auth(self):
     # Open connection through proxy with authentication
     proxy = AVProxy(proxy_file=Test.content[2][0])
     response = proxy.open("http://python.org", timeout=0.5)
 def test_bad_proxy_connect_request(self):
     # Open connection through proxy without authentication
     proxy = AVProxy(proxy_file=Test.content[0][0])
     request = urllib2.Request("http://python.org")
     response = proxy.open(request, timeout=1)
 def test_no_proxy_connect_url_aut(self):
     # Bad Proxy with retries
     proxy = AVProxy(proxy_file=Test.content[2][0])
     response = proxy.open("http://python.org", timeout=0.5, retries=1)
 def test_no_proxy_connect_retry(self):
     # No proxy with retries
     proxy = AVProxy()
     response = proxy.open("http://python.org", retries=0.5)
     assert response is not None