Ejemplo n.º 1
0
def log_event(instance, event=None):
    if isinstance(instance, AuditEvent):
        return

    try:
        d = get_audit_crud_dict(instance, event)
        if d:
            if AUDIT_TO_FILE:
                write_entry(d)
            from audit_logging.utils import log_event
            user_details = getattr(audit_logging_thread_local, 'user_details',
                                   {})
            logger.debug(
                'Got user_details from audit_logging_thread_local: {}'.format(
                    str(user_details).encode('utf-8')))
            resource = d.get('resource')
            resource_type = resource.get('type',
                                         'unknown') if resource else 'unknown'
            resource_uuid = resource.get('id',
                                         'unknown') if resource else 'unknown'
            log_event(event=event,
                      resource_type=resource_type,
                      resource_uuid=resource_uuid,
                      user_details=user_details)
        else:
            logger.debug(
                'get_audit_crud_dict() returned nothing (normal if {} not in AUDIT_MODELS)'
                .format(str(instance).encode('utf-8')))
    except Exception as ex:
        logger.exception('Exception during audit event.')
Ejemplo n.º 2
0
def log_model_event(instance,
                    event=None,
                    user_details: types.UserDetails = None):
    if isinstance(instance, AuditEvent):
        return
    user_details = user_details or dict()
    try:
        audit_record = get_audit_resource_record(instance, event)
        if user_details:
            audit_record.update(user_details)
        resource = audit_record.get("resource")
        if resource:
            if AUDIT_TO_FILE:
                write_entry(audit_record)
            resource_type = resource.get("resource_type")
            resource_uuid = resource.get("resource_uuid")
            log_event(event=event,
                      resource_type=resource_type,
                      resource_uuid=resource_uuid,
                      user_details=user_details)
        else:
            logger.debug(
                "get_audit_resource_record returned nothing (normal if %s not in AUDIT_MODELS)",
                str(instance).encode("utf-8"),
            )
    except Exception:
        logger.exception("Exception during audit event.")
Ejemplo n.º 3
0
def log_event(instance, event=None):
    if isinstance(instance, AuditEvent):
        return

    try:
        d = get_audit_crud_dict(instance, event)
        if d:
            if AUDIT_TO_FILE:
                write_entry(d)


#             audit_event = AuditEvent(
#                 event=event
#             )
#             if d.get('user_details'):
#                 logger.debug('got user_details from instance to log: {}'.format(d.get('user_details')))
#                 if d.get('user_details').get('username'):
#                     audit_event.username = d['user_details']['username']
#                 if d.get('user_details').get('email'):
#                     audit_event.email = d['user_details']['email']
#                 if d.get('user_details').get('fullname'):
#                     audit_event.fullname = d['user_details']['fullname']
#                 if d.get('user_details').get('superuser'):
#                     audit_event.superuser = d['user_details']['superuser']
#                 if d.get('user_details').get('staff'):
#                     audit_event.staff = d['user_details']['staff']
#             if d.get('resource'):
#                 logger.debug('got resource details from instance to log: {}'.format(d.get('resource')))
#                 if d.get('resource').get('type'):
#                     audit_event.resource_type = d['resource']['type']
#                 if d.get('resource').get('id'):
#                     logger.debug('setting resource_uuid to {}'.format(d['resource']['id']))
#                     audit_event.resource_uuid = d['resource']['id']
#                 if d.get('resource').get('title'):
#                     audit_event.resource_title = d['resource']['title']
#                 if d.get('resource').get('username'):
#                     audit_event.username = d['resource']['username']
#             audit_event.save()
            from audit_logging.utils import log_event
            user_details = getattr(audit_logging_thread_local, 'user_details',
                                   {})
            logger.debug(
                'Got user_details from audit_logging_thread_local: {} ')
            resource = d.get('resource')
            resource_type = resource.get('type',
                                         'unknown') if resource else 'unknown'
            resource_uuid = resource.get('id',
                                         'unknown') if resource else 'unknown'
            log_event(event=event,
                      resource_type=resource_type,
                      resource_uuid=resource_uuid,
                      user_details=user_details)
        else:
            logger.debug(
                'get_audit_crud_dict() returned nothing (normal if {} not in AUDIT_MODELS)'
                .format(instance))
    except Exception as ex:
        logger.exception('Exception during audit event.')
Ejemplo n.º 4
0
def write_audit_record(request, user, event, audit_updates=None, **kwargs):
    try:
        audit_record: types.AuditRecord = get_audit_login_dict(
            request, user, event)
        if audit_updates:
            audit_record.update(audit_updates)
        if audit_record:
            if AUDIT_TO_FILE:
                write_entry(audit_record)
            AuditEvent.objects.create(**audit_record)
    except Exception:
        logger.exception("user_logged_in signal failed to create audit event",
                         exc_info=True)
Ejemplo n.º 5
0
def user_login_failed(sender, credentials, **kwargs):
    """
    signal to catch failed logins and log them in the audit log
    """
    try:
        event = 'failed_login'
        user_model = get_user_model()
        d = {
            "event_time_gmt": get_time_gmt(),
            "event": event,
            "username": credentials[user_model.USERNAME_FIELD],
        }
        if AUDIT_TO_FILE:
            write_entry(d)
        login_event = AuditEvent(
            event=event,
            username=d['username'],
        )
        login_event.save()
    except:
        pass
Ejemplo n.º 6
0
def user_logged_in(sender, request, user, **kwargs):
    """
    signal to catch logins and log them in the audit log
    """
    try:
        event = 'login'
        d = get_audit_login_dict(request, user, event)
        if d:
            if AUDIT_TO_FILE:
                write_entry(d)
            login_event = AuditEvent(
                event=event,
                username=d['user_details']['username'],
                ip=d['user_details']['ip'],
                email=d['user_details']['email'],
                fullname=d['user_details']['fullname'],
                superuser=d['user_details']['superuser'],
                staff=d['user_details']['staff'],
            )
            login_event.save()
    except:
        pass