Beispiel #1
0
def _publish_new_action(user_id, remote_addr, action):
    if remote_addr == settings.LOCAL_REMOTE_ADDR:
        return

    user = User.get(user_id)
    user_type = NORMAL_USER
    username = None
    if user:
        username = user.username
        if (user.is_application
                or username in settings.APPLICATION_USE_USER_TOKEN_USER_LIST):
            user_type = APPLICATION_USER
    is_subscriable = any(item[0] == TYPE_APPLICATION
                         for item in action.action_indices)

    severity = SEVERITY_DANGEROUS
    action_name = action_types[action.action_type]
    if action_name in settings.DANGEROUS_ACTION_NAMES_EXCLUDE_LIST:
        severity = SEVERITY_NORMAL

    insensitive_data = remove_sensitive_data(action.action_data)
    try:
        new_action_detected.send(AuditLog,
                                 action_type=action.action_type,
                                 username=username,
                                 user_type=user_type,
                                 action_data=insensitive_data,
                                 is_subscriable=is_subscriable,
                                 severity=severity)
    except Exception:
        logger.exception('Unexpected error of publish webhook event')
        capture_exception(data=None)
Beispiel #2
0
def test_exception_off(sentry_client, mocker, turn_off):
    func = mocker.patch.object(sentry_client,
                               'captureException',
                               autospec=True)
    turn_off(SWITCH_ENABLE_SENTRY_EXCEPTION)
    capture_exception()
    assert not func.called
Beispiel #3
0
def test_exception_on(sentry_client, mocker):
    func = mocker.patch.object(sentry_client,
                               'captureException',
                               autospec=True)
    mocker.patch('huskar_api.extras.raven.raven_client', sentry_client)
    capture_exception()
    func.assert_called_once_with()
Beispiel #4
0
 def declare_upstream_from_request(self, request_data):
     if not g.auth.is_application or not g.cluster_name:
         return
     if not switch.is_switched_on(SWITCH_ENABLE_DECLARE_UPSTREAM):
         return
     route_management = RouteManagement(huskar_client, g.auth.username,
                                        g.cluster_name)
     application_names = frozenset(request_data.get(SERVICE_SUBDOMAIN, []))
     try:
         route_management.declare_upstream(application_names)
     except Exception:
         capture_exception(level=logging.WARNING)
Beispiel #5
0
 def worker():
     while self._running:
         try:
             func = self.hook_queue.get(timeout=1)
             self._empty.clear()
             try:
                 func()
             except Exception:
                 logger.exception('Webhook task unexpected failed.')
                 capture_exception(data=None)
         except Empty:
             self._empty.set()
             continue
Beispiel #6
0
    def _is_time_to_clean(self):
        condition = settings.TREE_HOLDER_CLEANER_CONDITION
        if not condition:
            return False

        cpu = self._get_cpu_percent()
        memory = self._get_virtual_memory_percent()
        # e.g. 'cpu < 50 and memory > 90'
        condition = condition.replace('cpu',
                                      str(cpu)).replace('memory', str(memory))
        try:
            return eval(condition, {}, {})
        except BaseException as e:
            logger.error('invalid tree holder cleaner condition: %r %s',
                         condition, e)
            capture_exception('invalid tree holder cleaner condition')
            return False
Beispiel #7
0
def _process_instance_list(application_name, cluster_name, new_instance_list,
                           instance_key_index_map):
    # Find inclusive keys
    inclusive_keys = frozenset()
    _key = MAGIC_CONFIG_KEYS['batch_config.inclusive_keys']
    key = (application_name, cluster_name, _key)
    if key in instance_key_index_map:
        instance = new_instance_list[instance_key_index_map[key]]
        try:
            inclusive_keys = frozenset(json.loads(instance['value']))
        except (KeyError, ValueError, TypeError):
            capture_exception()

    # Process instance list
    if not inclusive_keys:
        return new_instance_list

    return [i for i in new_instance_list if i['key'] in inclusive_keys]
Beispiel #8
0
def trace_remote_http_call(url):
    start_at = int(time.time() * 1000)
    domain = urlparse.urlparse(url).hostname
    try:
        yield
    except HTTPError as e:
        response = e.response
        status_code = response.status_code
        if response.status_code >= 500:
            monitor_client.increment('remote_http_call.error',
                                     tags={
                                         'type': 'internal_error',
                                         'domain': domain,
                                         'status_code': str(status_code),
                                     })
            message = 'Remote HTTP API Internal Server Error'
            capture_message(message,
                            level=logging.WARNING,
                            extra={
                                'url': url,
                                'status_code': status_code,
                                'body': repr(response.content),
                            })
        raise
    except (Timeout, ConnectionError) as e:
        if isinstance(e, Timeout):
            _type = 'timeout'
        else:
            _type = 'connection_error'
        monitor_client.increment('remote_http_call.error',
                                 tags={
                                     'type': _type,
                                     'domain': domain,
                                     'status_code': 'unknown',
                                 })
        capture_exception(level=logging.WARNING, extra={'url': url})
        raise
    finally:
        monitor_client.timing('remote_http_call.timer',
                              int(time.time() * 1000) - start_at,
                              tags={'domain': domain})
Beispiel #9
0
def test_ignore_send_error(mocker):
    def is_switched_on(switch_name, default=True):
        return default

    mocker.patch.object(switch, 'is_switched_on', is_switched_on)

    mocker.patch(
        'huskar_api.extras.raven.raven_client',
        mocker.MagicMock(
            captureMessage=mocker.MagicMock(side_effect=Exception),
            captureException=mocker.MagicMock(side_effect=Exception)))

    assert capture_message('test') is None
    assert capture_exception('error') is None
Beispiel #10
0
 def record_errors(self, error):
     logger.exception(error)
     capture_exception(data=None)
Beispiel #11
0
    def post(self, application_name, cluster_name, key):
        """Updates the weight of specified service instance.

        :param application_name: The name of application.
        :param cluster_name: The name of cluster.
        :param key: The key of service instance.
        :form weight: The target weight of instance. It should be a positive
                      integer.
        :form ephemeral: Whether the modification be ephemeral or persistent.
                         Must be ephemeral (``1``) for now.
        :form ttl: When ephemeral is ``1``, will use this value as ttl.
                   default: 5 * 60 s.
        :<header Authorization: Huskar Token (See :ref:`token`)
        :status 400: The request body is invalid.
        :status 404: The instance is not found.
        :status 409: There is another request has modified the instance.
        :status 200: The weight is updated successfully.
        """
        check_application_auth(application_name, Authority.WRITE)
        check_cluster_name(cluster_name, application_name)
        validate_fields(instance_schema, {
            'application': application_name,
            'cluster': cluster_name,
            'key': key,
        })

        weight = request.form.get('weight', type=int)
        if not weight or weight < 0:
            abort(400, 'weight must be a positive integer')
        weight = unicode(weight)
        ephemeral = request.form.get('ephemeral', type=int)
        if ephemeral != 1:
            abort(400, 'ephemeral must be "1" for now')

        im = InstanceManagement(huskar_client, application_name,
                                SERVICE_SUBDOMAIN)
        instance, _ = im.get_instance(cluster_name, key, resolve=False)

        if instance.stat is None:
            abort(
                404, '%s %s/%s/%s does not exist' % (
                    SERVICE_SUBDOMAIN,
                    application_name,
                    cluster_name,
                    key,
                ))

        try:
            old_data = instance.data
            new_data = json.loads(instance.data)
            meta = new_data.setdefault('meta', {})
            meta['weight'] = weight
            new_data = json.dumps(new_data)
            instance.data = new_data
        except (ValueError, TypeError):
            capture_exception()  # unexpected exception should be captured
            abort(500)

        try:
            instance.save()
        except OutOfSyncError:
            abort(
                409, '%s %s/%s/%s has been modified by another request' % (
                    SERVICE_SUBDOMAIN,
                    application_name,
                    cluster_name,
                    key,
                ))

        audit_log.emit(audit_log.types.UPDATE_SERVICE,
                       application_name=application_name,
                       cluster_name=cluster_name,
                       key=key,
                       old_data=old_data,
                       new_data=new_data)

        return api_response()