Ejemplo n.º 1
0
    def test_send_recv(self):
        msg = 'ScalrMessage'
        msg2 = 'InternalMessage'
        expected_msg = None

        def listener(message, queue):
            self.assertFalse(expected_msg is None)
            self.assertEqual(message.name, expected_msg)

        self.consumer.listeners.append(listener)
        self.consumer2.listeners.append(listener)

        expected_msg = msg
        self.producer.send(Queues.CONTROL, P2pMessage(msg))
        expected_msg = msg2
        self.producer2.send(Queues.CONTROL, P2pMessage(msg2))
Ejemplo n.º 2
0
            def _msg_without_sensitive_data(self, message):
                msg_copy = P2pMessage(message.name, message.meta.copy(),
                                      deepcopy(message.body))
                msg_copy.id = message.id

                if 'platform_access_data' in msg_copy.body:
                    del msg_copy.body['platform_access_data']

                if 'global_variables' in msg_copy.body:
                    glob_vars = msg_copy.body.get('global_variables', []) or []
                    i = 0
                    for v in list(glob_vars):
                        if v.get('private'):
                            del glob_vars[i]
                            i -= 1
                        elif 'private' in v:
                            del glob_vars[i]['private']
                        i += 1

                if 'chef' in msg_copy.body:
                    try:
                        del msg_copy.body['chef']['validator_name']
                        del msg_copy.body['chef']['validator_key']
                    except (KeyError, TypeError):
                        pass
                return msg_copy
Ejemplo n.º 3
0
    def test_send_recv(self):
        exp_name = 'TestMessage'
        exp_queue = 'TestQueue'

        def listener(message, queue):
            self.assertEqual(message.name, exp_name)
            self.assertEqual(queue, exp_queue)

        self.consumer.listeners.append(listener)

        msg = P2pMessage(exp_name)
        self.producer.send(exp_queue, msg)
Ejemplo n.º 4
0
        class RequestHandler(BaseHTTPRequestHandler):
            consumer = None
            '''
            @cvar consumer: Message consumer instance
            @type consumer: P2pMessageConsumer
            '''
            def do_POST(self):
                logger = logging.getLogger(__name__)

                queue = os.path.basename(self.path)
                rawmsg = self.rfile.read(int(self.headers["Content-length"]))
                logger.debug("Received ingoing message in queue: '%s'", queue)

                try:
                    for f in self.consumer.filters['protocol']:
                        rawmsg = f(self.consumer, queue, rawmsg)
                        try:
                            if isinstance(rawmsg, str):
                                h = HTMLParser.HTMLParser()
                                rawmsg = h.unescape(rawmsg).encode('utf-8')
                        except:
                            logger.debug('%s',
                                         sys.exc_info()[1],
                                         sys.exc_info()[2])

                except (BaseException, Exception), e:
                    err = 'Message consumer protocol filter raises exception: %s' % str(
                        e)
                    logger.info(
                        err
                    )  # Downshift level, cause HTTP scanners do a lot of flood
                    self.send_response(400, str(e))
                    return

                try:
                    logger.debug("Decoding message: %s", rawmsg)
                    message = P2pMessage()

                    mime_type = self.headers.get('Content-Type',
                                                 'application/xml')
                    format = ('application/json'
                              in mime_type) and 'json' or 'xml'
                    if 'json' == format:
                        message.fromjson(rawmsg)
                    else:
                        message.fromxml(rawmsg)

                except (BaseException, Exception), e:
                    err = "Cannot decode message. error: %s; raw message: %s" % (
                        str(e), rawmsg)
                    logger.exception(err)
                    self.send_response(400, err)
                    return
Ejemplo n.º 5
0
 def test_empty_excludes(self):
     xml = """<?xml version="1.0"?><message id="ad851a7b-6512-45db-b0a5-77f915aada32" name="Rebundle"><meta/><body><role_name>scalarizr-centos5.2</role_name><bundle_task_id>45</bundle_task_id><excludes/></body></message>"""
     msg = P2pMessage()
     msg.fromxml(xml)
     self.assertTrue(msg.excludes is None)
Ejemplo n.º 6
0
    def _send0(self,
               queue,
               message,
               success_callback=None,
               fail_callback=None):
        try:
            use_json = __node__['message_format'] == 'json'
            data = message.tojson() if use_json else message.toxml()

            content_type = 'application/%s' % 'json' if use_json else 'xml'
            headers = {'Content-Type': content_type}

            if message.name not in ('Log', 'OperationDefinition',
                                    'OperationProgress', 'OperationResult'):
                msg_copy = P2pMessage(message.name, message.meta.copy(),
                                      deepcopy(message.body))
                try:
                    del msg_copy.body['chef']['validator_name']
                    del msg_copy.body['chef']['validator_key']
                except (KeyError, TypeError):
                    pass
                self._logger.debug(
                    "Delivering message '%s' %s. Json: %s, Headers: %s",
                    message.name, msg_copy.body, use_json, headers)

            for f in self.filters['protocol']:
                data = f(self, queue, data, headers)

            url = self.endpoint + "/" + queue
            req = urllib2.Request(url, data, headers)
            opener = urllib2.build_opener(urltool.HTTPRedirectHandler())
            opener.open(req)

            self._message_delivered(queue, message, success_callback)

        except:
            e = sys.exc_info()[1]
            # Python < 2.6 raise exception on 2xx > 200 http codes except
            if isinstance(e, urllib2.HTTPError):
                if e.code == 201:
                    self._message_delivered(queue, message, success_callback)
                    return

            self._logger.warning("Message '%s' not delivered (message_id: %s)",
                                 message.name, message.id)
            self.fire("send_error", e, queue, message)

            msg = None
            if isinstance(e, urllib2.HTTPError):
                if e.code == 401:
                    self._logger.warn(
                        "Cannot authenticate on message server. %s", e.msg)
                elif e.code == 400:
                    self._logger.warn("Malformed request. %s", e.msg)
                else:
                    self._logger.warn("Cannot post message to %s. %s", url, e)
            elif isinstance(e, urllib2.URLError):
                msg = ("Scalr messaging endpoint '{0}' is unreachable. "
                       "Cause: {1}").format(self.endpoint, e)
            elif isinstance(e, httplib.HTTPException):
                msg = (
                    "Scalr messaging endpoint '{0}' answered with invalid HTTP response. "
                    "Cause: {1}").format(self.endpoint, e)
            else:
                self._logger.warn('Caught exception', exc_info=sys.exc_info())

            if msg:
                self._logger.warn(msg)

            # Call user code
            if fail_callback:
                fail_callback(queue, message, e)
Ejemplo n.º 7
0
        class RequestHandler(BaseHTTPRequestHandler):
            consumer = None
            '''
            @cvar consumer: Message consumer instance
            @type consumer: P2pMessageConsumer
            '''
            def _msg_without_sensitive_data(self, message):
                msg_copy = P2pMessage(message.name, message.meta.copy(),
                                      deepcopy(message.body))
                msg_copy.id = message.id

                if 'platform_access_data' in msg_copy.body:
                    del msg_copy.body['platform_access_data']

                if 'global_variables' in msg_copy.body:
                    glob_vars = msg_copy.body.get('global_variables', []) or []
                    i = 0
                    for v in list(glob_vars):
                        if v.get('private'):
                            del glob_vars[i]
                            i -= 1
                        elif 'private' in v:
                            del glob_vars[i]['private']
                        i += 1

                if 'chef' in msg_copy.body:
                    try:
                        del msg_copy.body['chef']['validator_name']
                        del msg_copy.body['chef']['validator_key']
                    except (KeyError, TypeError):
                        pass
                return msg_copy

            def do_POST(self):
                logger = logging.getLogger(__name__)

                queue = os.path.basename(self.path)
                rawmsg = self.rfile.read(int(self.headers["Content-length"]))
                logger.debug("Received ingoing message in queue: '%s'", queue)

                try:
                    for f in self.consumer.filters['protocol']:
                        rawmsg = f(self.consumer, queue, rawmsg)
                        try:
                            if isinstance(rawmsg, str):
                                h = HTMLParser.HTMLParser()
                                rawmsg = h.unescape(rawmsg).encode('utf-8')
                        except:
                            logger.debug('Caught message parsing error',
                                         exc_info=sys.exc_info())

                except (BaseException, Exception), e:
                    err = 'Message consumer protocol filter raises exception: %s' % str(
                        e)
                    logger.exception(err)
                    self.send_response(201, 'Created')
                    return

                try:
                    #logger.debug("Decoding message: %s", rawmsg)
                    message = P2pMessage()

                    mime_type = self.headers.get('Content-Type',
                                                 'application/xml')
                    format = ('application/json'
                              in mime_type) and 'json' or 'xml'

                    if 'json' == format:
                        message.fromjson(rawmsg)
                    else:
                        message.fromxml(rawmsg)

                    msg_copy = self._msg_without_sensitive_data(message)
                    logger.debug('Decoding message: %s',
                                 msg_copy.tojson(indent=4))

                except (BaseException, Exception), e:
                    err = "Cannot decode message. error: %s; raw message: %s" % (
                        str(e), rawmsg)
                    logger.exception(err)
                    self.send_response(201, 'Created')
                    return