Exemplo n.º 1
0
    def __init_structure__(self, classname, valuedict=None, **kwargs):
        # create dictionary for model instance
        self.__instdict__ = {}
        # Check static model structure
        if not self.__structure__.get(classname):
            # if not - create it
            self.__structure__[classname] = {}
            # Specify the collection name
            self.__collection__[classname] = classname.lower() + 's'
            log.debug('Creating structure for model %s' % classname)
            # Browse model for properties
            for key in dir(self):
                if not non_record.match(key):
                    value = getattr(self, key)
                    if hasattr(value, '__isvalue__'):
                        value.set_key(key)
                        self.__structure__[classname][key] \
                            = value.get_default()
        if valuedict:
            self.__instdict__ = valuedict
        else:
            self.__instdict__ = copy.deepcopy(self.__structure__[classname])

        for k in kwargs:
            if k == 'salt':
                continue
            elif k in self.__instdict__:
                if hasattr(kwargs[k], 'get_values'):
                    self.__instdict__[k] = kwargs[k].get_values()
                else:
                    self.__instdict__[k] = kwargs[k]
            else:
                raise Exception('There is no such value \'%s\' in %s model.' %
                                (k, classname))
Exemplo n.º 2
0
    def __init__(self, response_dict):
        # Set standard response attributes
        super(XResponse, self).__init__()
        self.status = 200  # 200 OK, it's default, but anyway...
        self.headerlist = [('Content-type', 'application/x-javascript')]
        self.charset = 'utf-8'

        # Set and unset cookies
        # Set cookies

        try:
            set_cookies_dict = response_dict.get(SET_COOKIES)
        except:
            raise

        log.debug('response_dict2 is %s. Set-cookies dict is %s'
                  % (response_dict, set_cookies_dict))
        if set_cookies_dict:
            for cookie in set_cookies_dict:
                log.debug('Try to set cookie %s to value %s'
                          % (cookie, set_cookies_dict[cookie]))
                self.set_cookie(cookie, str(set_cookies_dict[cookie]))
            response_dict.pop(SET_COOKIES)

        # Unset cookies
        unset_cookies_dict = response_dict.get(UNSET_COOKIES)
        if unset_cookies_dict:
            for cookie in unset_cookies_dict:
                self.delete_cookie(cookie)
            response_dict.pop(UNSET_COOKIES)
        self.text = json.dumps(response_dict, default=datahandler,indent=True,sort_keys=True)
Exemplo n.º 3
0
    def __init__(self, response_dict):
        # Set standard response attributes
        super(XResponse, self).__init__()
        self.status = 200  # 200 OK, it's default, but anyway...
        self.headerlist = [('Content-type', 'application/x-javascript')]
        self.charset = 'utf-8'

        # Set and unset cookies
        # Set cookies

        try:
            set_cookies_dict = response_dict.get(SET_COOKIES)
        except:
            raise

        log.debug('response_dict2 is %s. Set-cookies dict is %s' %
                  (response_dict, set_cookies_dict))
        if set_cookies_dict:
            for cookie in set_cookies_dict:
                log.debug('Try to set cookie %s to value %s' %
                          (cookie, set_cookies_dict[cookie]))
                self.set_cookie(cookie, str(set_cookies_dict[cookie]))
            response_dict.pop(SET_COOKIES)

        # Unset cookies
        unset_cookies_dict = response_dict.get(UNSET_COOKIES)
        if unset_cookies_dict:
            for cookie in unset_cookies_dict:
                self.delete_cookie(cookie)
            response_dict.pop(UNSET_COOKIES)
        self.text = unicode(
            json.dumps(response_dict,
                       default=datahandler,
                       indent=True,
                       sort_keys=True))
    def __init_structure__(self, classname, valuedict=None, **kwargs):
        # create dictionary for model instance
        self.__instdict__ = {}
        # Check static model structure
        if not self.__structure__.get(classname):
        # if not - create it
            self.__structure__[classname] = {}
            # Specify the collection name
            self.__collection__[classname] = classname.lower() + 's'
            log.debug('Creating structure for model %s' % classname)
            # Browse model for properties
            for key in dir(self):
                if not non_record.match(key):
                    value = getattr(self, key)
                    if hasattr(value, '__isvalue__'):
                        value.set_key(key)
                        self.__structure__[classname][key] \
                            = value.get_default()
        if valuedict:
            self.__instdict__ = valuedict
        else:
            self.__instdict__ = copy.deepcopy(self.__structure__[classname])

        for k in kwargs:
            if k == 'salt':
                continue
            elif k in self.__instdict__:
                if hasattr(kwargs[k], 'get_values'):
                    self.__instdict__[k] = kwargs[k].get_values()
                else:
                    self.__instdict__[k] = kwargs[k]
            else:
                raise Exception('There is no such value \'%s\' in %s model.'
                                % (k, classname))
Exemplo n.º 5
0
def index(request, path_info, path):
    partial_response = False
    path_info = path_info.replace('%28', '(')\
                         .replace('%29', ')')\
                         .replace('%20', ' ')
    response = BaseResponse()
    # define a file extansion
    base, ext = os.path.splitext(path_info)  # Get the file extansion
    mime_type = MIME_TYPES.get(ext, 'text/plain')
    if not mime_type:
        raise Exception("unknown doc, or something like that :-P: %s" % ext)
    static_file_path = os.path.join(path, path_info)
    # Check if this path exists
    if not os.path.exists(static_file_path):
        error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s"\
                    % path_info
        log.debug('not found: %s' % static_file_path)
        return Error404(error_msg)
    # configure response
    static_file = open(static_file_path, 'rb')  # Open file
    # Here we try to handle Range parameter

    content_offset = 0
    content_end = 0
    request_range = request.headers.get('Range')
    if request_range:
        range_bytes = request_range.replace('bytes=', '')
        range_bytes = range_bytes.split('-')
        if len(range_bytes) > 2:
            raise Exception('Wrong http Range parameter "%s"' % request_range)
        content_offset = toInt(range_bytes[0])
        content_end = toInt(range_bytes[1])
        partial_response = True

    static_content = static_file.read()
    if content_end <= 0 or content_end >= len(static_content):
        content_end = len(static_content) - 1
    response.body = static_content[content_offset:content_end + 1]

    response.charset = 'utf-8'
    response.headerlist = []
    response.headerlist.append(('Content-type', mime_type))

    if ext in ['.jpg', '.gif', '.png', '.tiff', '.mp3', '.ogg', '.ttf']:
        response.headerlist.append(('Access-Control-Allow-Origin', '*'))

    if partial_response:
        response.status = 206
        response.headerlist.append(
            ('Content-Range', 'bytes %i-%i/%i' %
             (content_offset, content_end, len(static_content))))
    st = os.stat(static_file_path)
    response.headerlist.append(('Last-modified', formatdate(st.st_mtime)))
    response.headerlist.append(('Cache-control', 'max-age=626560472'))
    response.conditional_response = True
    response.md5_etag()
    return response
Exemplo n.º 6
0
def index(request, path_info, path, auth=None):
    partial_response = False
    response = BaseResponse()
    # define a file extansion
    base, ext = os.path.splitext(path_info)  # Get the file extansion
    mime_type = MIME_TYPES.get(ext, 'text/plain')
    if not mime_type:
        raise Exception("unknown doc, or something like that :-P: %s" % ext)
    static_file_path = os.path.join(path, path_info)
    # Check if this path exists
    if not os.path.exists(static_file_path):
        error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s"\
                    % path_info
        log.debug('not found: %s' % static_file_path)
        return Error404(error_msg)
    # configure response
    static_file = open(static_file_path, 'rb')  # Open file
    # Here we try to handle Range parameter

    content_offset = 0
    content_end = 0
    request_range = request.headers.get('Range')
    if request_range:
        range_bytes = request_range.replace('bytes=', '')
        range_bytes = range_bytes.split('-')
        if len(range_bytes) > 2:
            raise Exception('Wrong http Range parameter "%s"' % request_range)
        content_offset = toInt(range_bytes[0])
        content_end = toInt(range_bytes[1])
        partial_response = True

    static_content = static_file.read()
    if content_end <= 0 or content_end >= len(static_content):
        content_end = len(static_content) - 1
    response.body = static_content[content_offset: content_end + 1]

    response.charset = 'utf-8'
    response.headerlist = []
    response.headerlist.append(('Content-type', mime_type))

    if ext in ['.jpg', '.gif', '.png', '.tiff', '.mp3', '.ogg', '.ttf']:
        response.headerlist.append(('Access-Control-Allow-Origin', '*'))

    if partial_response:
        response.status = 206
        response.headerlist.append(
            ('Content-Range',
             'bytes %i-%i/%i' % (content_offset, content_end,
                                 len(static_content))))
    st = os.stat(static_file_path)
    response.headerlist.append(('Last-modified', formatdate(st.st_mtime)))
    response.headerlist.append(('Cache-control', 'max-age=626560472'))
    response.conditional_response = True
    response.md5_etag()
    return response
Exemplo n.º 7
0
def worker(n):
    cli = bernhard.Client(**RIEMANN_ARGS)
    while True:
        try:
            target, metric = RIEMANN_QUEUE.get()
            target = target.partition('?')[0]
            target = target.replace('/', '.')
            event = {'service': target, 'metric': metric}
            event.update(RIEMANN_SOURCE)
            result = cli.send(event=event)
            log.debug("Sent %s result %s", target, result)
        except Exception as e:
            log.error(e)
Exemplo n.º 8
0
    def update(self, storage=None, **kwargs):
        """
        update time expire
        """
        id = mkey(REDIS_NAMESPACE, self.collection_name, self.id)

        if 'expire' in kwargs:
            if TIME_TO_OVERWRITE_CLIENT_COOKIE > self.RedisConn.ttl(id):
                result = self.RedisConn.expire(id, kwargs['expire'])
                return result
            else:
                log.debug('non_update_SESSION')
        else:
            raise Exception('unknown action!!!')
    def update(self, storage=None, **kwargs):
        """
        update time expire
        """
        id = mkey(REDIS_NAMESPACE, self.collection_name, self.id)

        if 'expire' in kwargs:
            if TIME_TO_OVERWRITE_CLIENT_COOKIE > self.RedisConn.ttl(id):
                result = self.RedisConn.expire(id, kwargs['expire'])
                return result
            else:
                log.debug('non_update_SESSION')
        else:
            raise Exception('unknown action!!!')
def worker(n):
    cli = bernhard.Client(**RIEMANN_ARGS)
    while True:
        try:
            target, metric = RIEMANN_QUEUE.get()
            target = target.partition('?')[0]
            target = target.replace('/', '.')
            event = {'service': target,
                     'metric': metric}
            event.update(RIEMANN_SOURCE)
            result = cli.send(event=event)
            log.debug("Sent %s result %s", target, result)
        except Exception as e:
            log.error(e)
Exemplo n.º 11
0
 def unregister(self):
     log.debug('unregistering event %s in system' % self.id)
     NOODLES_EVENTS_LIST.pop(self.id)
Exemplo n.º 12
0
 def firing(self, event_data=None):
     if event_data is None:
         event_data = {}
     event_msg = json.dumps({'event_id': self.id, 'event_data': event_data})
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, event_msg)
     log.debug('Event %s is firing', self.id)
Exemplo n.º 13
0
 def register(self, callback):
     """Register event in system """
     log.debug('registering event %s in system' % self.id)
     NOODLES_EVENTS_LIST[self.id] = self
     self.callback = callback
Exemplo n.º 14
0
    def authenticate(self, request):
        """
        Base authentication method, all checks here
        """
        if not request.headers.get('authorization'):
            return False
        if not python_digest.is_digest_credential(
                request.headers.get('authorization')):
            return False
        digest_response = python_digest.parse_digest_credentials(
            request.headers.get('authorization'))
        if not digest_response:
            log.debug('authentication failure: supplied digest credentials'
                      ' could not be parsed: "%s".'
                      % request.headers.get('authorization'))
            return False

        if not digest_response.username:
            return False

        if not digest_response.realm == self.realm:
            log.debug('authentication failure: supplied realm "%s"'
                      'does not match configured realm "%s".'
                      % (digest_response.realm, self.realm))
            return False

        if not python_digest.validate_nonce(
                digest_response.nonce, self.secret_key):
            log.debug('authentication failure: nonce validation failed.')
            return False
        partial_digest = self._account_storage.get_partial_digest(
            digest_response.username)

        if not partial_digest:
            log.debug('authentication failure: no partial digest available'
                      ' for user "%s".' % digest_response.username)
            return False

        calculated_request_digest = python_digest.calculate_request_digest(
            method=request.method, digest_response=digest_response,
            partial_digest=partial_digest)

        if not calculated_request_digest == digest_response.response:
            log.debug('authentication failure: supplied request digest'
                      'does not match calculated request digest.')
            return False

        if not python_digest.validate_uri(
                digest_response.uri, urllib.parse.unquote(request.path)):
            log.debug('authentication failure: digest authentication uri value'
                      '"%s" does not match value "%s" from HTTP'
                      'request line.' % (digest_response.uri, request.path))
            return False

        if not self._account_storage.is_admin(digest_response.username):
            log.debug('authentication failure: user not in operator admin.')
            return False

        if hasattr(request,'session') and request.session.data.nonce != digest_response.nonce:
            if (int(python_digest.get_nonce_timestamp(digest_response.nonce))
                    + self.timeout < time.time()):
                log.debug('authentication failure: attempt to establish'
                          ' a new session with a stale nonce.')
                return False
            if not self._store_nonce(digest_response.nonce, request):
                log.debug('authentication failure: attempt to establish'
                          ' a previously used or nonce count.')
                return False
        request.user = self._account_storage.get_by_login(
            digest_response.username)
        return True
Exemplo n.º 15
0
    def __call__(self, env, start_response):
        websocket = env.get('wsgi.websocket')
        if not websocket:
            self.bad_request()
        self.ws = websocket
        # Endless event loop
        while 1:
            try:
                data = self.ws.receive()
                self.clear_test_data()
            except WebSocketError as e:
                if is_ws_error_abnormal(e):
                    log.error('WebSocket fault: %s' % e.message,
                              extra=self.channel_history)
                break
            except Exception:
                f = Formatter()
                traceback = f.formatException(sys.exc_info())
                log.error('Servlet fault: \n%s' % traceback,
                          extra=self.channel_history)
                break

            if data:
                jd = json.loads(data)
                if jd.get('pkg') \
                        and jd['pkg'].get('data') \
                        and isinstance(jd['pkg']['data'], dict)\
                        and jd['pkg']['data'].get('testData'):
                    self.write_test_data(jd['pkg']['data']['testData'])
                    del jd['pkg']['data']['testData']
                self.channel_history['messages'].append(jd)
                if hasattr(self.session, 'sess') and self.session.sess:
                    self.channel_history['session_id'] = self.session.sess.id
                    self.channel_history['user_id'] = self.session.sess.user_id
                if not jd.get('channel') and jd.get('pkg'):
                    act = jd['pkg'].get('action')
                    assert not act.startswith('_'), "security violation"
                    try:
                        handler = getattr(self, act)
                    except WebSocketError as e:
                        if is_ws_error_abnormal(e):
                            f = Formatter()
                            traceback = f.formatException(sys.exc_info())
                            log.error('Global channel action error: \n%s' %
                                      traceback,
                                      extra=self.channel_history)
                        break
                    assert handler.__name__ == "action_wrapper", \
                        "%s is not allowed to be executed externally." % act
                    handler(jd['pkg']['data'])
                    continue
                if self.check_permissions \
                        and not self.validate_send(jd.get('channel')):
                    jd['result'] = 403
                    if not self.ws.closed:
                        try:
                            self.ws.send(json.dumps(jd))
                        except WebSocketError as e:
                            if is_ws_error_abnormal(e):
                                log.error('WebSocket fault: %s' % e.message,
                                          extra=self.channel_history)
                    continue
                else:
                    self.run_callback('message', ApiSocketMessage(data))
            else:
                log.debug('Web Socket is disconnected')
                self.close_event.set()
            if self.close_event.is_set():
                break
        self.run_callback('close')
Exemplo n.º 16
0
 def kill_greenlets(self):
     """ Kill all greenlets associated with this session """
     for green in self.greenlets.values():
         log.debug('Kill greenlet for session')
         gevent.kill(green)
    def __call__(self, env, start_response):
        websocket = env.get('wsgi.websocket')
        if not websocket:
            self.bad_request()
        self.ws = websocket
        # Endless event loop
        while 1:
            try:
                data = self.ws.receive()
                self.clear_test_data()
            except WebSocketError as e:
                if is_ws_error_abnormal(e):
                    log.error('WebSocket fault: %s' % e.message,
                              extra=self.channel_history)
                break
            except Exception:
                f = Formatter()
                traceback = f.formatException(sys.exc_info())
                log.error('Servlet fault: \n%s' % traceback,
                          extra=self.channel_history)
                break

            if data:
                jd = json.loads(data)
                if jd.get('pkg') \
                        and jd['pkg'].get('data') \
                        and isinstance(jd['pkg']['data'], dict)\
                        and jd['pkg']['data'].get('testData'):
                    self.write_test_data(jd['pkg']['data']['testData'])
                    del jd['pkg']['data']['testData']
                self.channel_history['messages'].append(jd)
                if hasattr(self.session, 'sess') and self.session.sess:
                    self.channel_history['session_id'] = self.session.sess.id
                    self.channel_history['user_id'] = self.session.sess.user_id
                if not jd.get('channel') and jd.get('pkg'):
                    act = jd['pkg'].get('action')
                    assert not act.startswith('_'), "security violation"
                    try:
                        handler = getattr(self, act)
                    except WebSocketError as e:
                        if is_ws_error_abnormal(e):
                            f = Formatter()
                            traceback = f.formatException(sys.exc_info())
                            log.error('Global channel action error: \n%s'
                                      % traceback, extra=self.channel_history)
                        break
                    assert handler.__name__ == "action_wrapper", \
                        "%s is not allowed to be executed externally." % act
                    handler(jd['pkg']['data'])
                    continue
                if self.check_permissions \
                        and not self.validate_send(jd.get('channel')):
                    jd['result'] = 403
                    if not self.ws.closed:
                        try:
                            self.ws.send(json.dumps(jd))
                        except WebSocketError as e:
                            if is_ws_error_abnormal(e):
                                log.error('WebSocket fault: %s' % e.message,
                                          extra=self.channel_history)
                    continue
                else:
                    self.run_callback('message', ApiSocketMessage(data))
            else:
                log.debug('Web Socket is disconnected')
                self.close_event.set()
            if self.close_event.is_set():
                break
        self.run_callback('close')
Exemplo n.º 18
0
 def unregister(self):
     log.debug('unregistering event %s in system' % self.id)
     NOODLES_EVENTS_LIST.pop(self.id)
Exemplo n.º 19
0
 def firing(self, event_data=None):
     if event_data is None:
         event_data = {}
     event_msg = json.dumps({'event_id': self.id, 'event_data': event_data})
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, event_msg)
     log.debug('Event %s is firing', self.id)
Exemplo n.º 20
0
 def register(self, callback):
     """Register event in system """
     log.debug('registering event %s in system' % self.id)
     NOODLES_EVENTS_LIST[self.id] = self
     self.callback = callback