Skip to content

Commit

Permalink
1.2.4 (#78)
Browse files Browse the repository at this point in the history
* Setup config (#70)

* Updated setup_config mgmt command

* Updated CHANGELOG.md

* Added cover

* Update README.md

* Managed values as a list

* Refactoring

* Removed comments

* buffalogs_1_2_2

* 72 exception keyerror ip in process user (#73)

* Fixed KeyError('ip')

* Updated CHANGELOG.md

* Added user.name exists in Elastic query

* Version 1.2.3

* Added new screenshots (#75)

* Fixed ValueError('make_aware expects a naive datetime) (#77)

* Fixed ValueError('make_aware expects a naive datetime)

* fix

* Set USE_TZ = True

* Updated CHANGELOG.md

* Updated CHANGELOG.md

* Version 1.2.4
  • Loading branch information
Lorygold committed Oct 6, 2023
1 parent ded01bd commit 4924463
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 60 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 1.2.x
### 1.2.4
#### Bugfix
* Fixed ValueError('make_aware expects a naive datetime') in calc_distance function setting the timezone to True in the `Login.timestamp` model field
### 1.2.3
#### Bugfix
* Fixed KeyError('ip') in process_user function
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ After that, there are two ways of running BuffaLogs, depending on your system co
* run `docker-compose -f docker-compose.yaml -f docker-compose.elastic.yaml up -d` in order to execute all the containers, included Elasticsearch and Kibana
* Now elasticsearch and kibana are running on the same host with Buffalogs.

![Screenshot 2023-08-09 at 6 49 41 PM](https://github.com/certego/BuffaLogs/assets/33703137/07548d33-3878-4ff3-9cb7-4a6b865d233b)
<img src="docs/static/map_buffalogs.png" width=750 height=400 alt="BuffaLogs Map Page"/>

*For further examples: [Wiki - Example](https://github.com/certego/BuffaLogs/wiki/3.-Example)*

Expand Down
2 changes: 1 addition & 1 deletion buffalogs/buffalogs/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

USE_I18N = True

# USE_TZ = True
USE_TZ = True


# Static files (CSS, JavaScript, Images)
Expand Down
31 changes: 5 additions & 26 deletions buffalogs/impossible_travel/modules/impossible_travel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import datetime

from django.conf import settings
from django.utils import timezone
Expand Down Expand Up @@ -31,10 +32,10 @@ def calc_distance(self, db_user, prev_login, last_login_user_fields):
distance_km = geodesic((prev_login.latitude, prev_login.longitude), (last_login_user_fields["lat"], last_login_user_fields["lon"])).km

if distance_km > settings.CERTEGO_BUFFALOGS_DISTANCE_KM_ACCEPTED:
last_timestamp_datetimeObj = self.validate_timestamp(last_login_user_fields["timestamp"])
prev_timestamp_datetimeObj = timezone.make_aware(prev_login.timestamp)
last_timestamp_datetimeObj_aware = timezone.make_aware(datetime.strptime(last_login_user_fields["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ"))
prev_timestamp_datetimeObj_aware = prev_login.timestamp # already aware in the db

diff_timestamp = last_timestamp_datetimeObj - prev_timestamp_datetimeObj
diff_timestamp = last_timestamp_datetimeObj_aware - prev_timestamp_datetimeObj_aware
diff_timestamp_hours = diff_timestamp.total_seconds() / 3600

if diff_timestamp_hours == 0:
Expand All @@ -43,35 +44,13 @@ def calc_distance(self, db_user, prev_login, last_login_user_fields):
vel = distance_km / diff_timestamp_hours

if vel > settings.CERTEGO_BUFFALOGS_VEL_TRAVEL_ACCEPTED:
# timestamp_validated = self.validate_timestamp(last_login_user_fields["timestamp"])
alert_info["alert_name"] = Alert.ruleNameEnum.IMP_TRAVEL
alert_info[
"alert_desc"
] = f"{alert_info['alert_name']} for User: {db_user.username},\
at: {last_timestamp_datetimeObj}, from: {last_login_user_fields['country']}, previous country: {prev_login.country}, distance covered at {int(vel)} Km/h"
at: {last_timestamp_datetimeObj_aware}, from: {last_login_user_fields['country']}, previous country: {prev_login.country}, distance covered at {int(vel)} Km/h"
return alert_info, int(vel)

def validate_timestamp(self, time):
"""Validate timestamp format
:param time: time to validate
:type time: datetime
:return: timestamp validated with utc timezone aware
:rtype: datetime
"""
try:
timestamp_format = "%Y-%m-%dT%H:%M:%S.%fZ"
timestamp_datetimeObj = timezone.datetime.strptime(str(time), timestamp_format)
except (ValueError, TypeError) as e:
if "decoding to str" in str(e):
timestamp_format = "%Y-%m-%dT%H:%M:%S.000Z"
timestamp_datetimeObj = timezone.datetime.strptime(time, timestamp_format)
if "does not match format" in str(e):
timestamp_format = "%Y-%m-%d %H:%M:%S"
timestamp_datetimeObj = timezone.datetime.strptime(str(time), timestamp_format)
timestamp_aware = timezone.make_aware(timestamp_datetimeObj)
return timestamp_aware

def update_model(self, db_user, new_login):
"""Update DB entry with last login info
Expand Down
30 changes: 0 additions & 30 deletions buffalogs/impossible_travel/tests/test_impossible_travel.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,6 @@ def test_calc_distance_alert(self):
self.assertIn("from: Sudan", result["alert_desc"])
self.assertIn("previous country: United States, distance covered at 10109599 Km/h", result["alert_desc"])

def test_validate_timestamp(self):
# try - format: "%Y-%m-%dT%H:%M:%S.%fZ"
time = "2023-03-08T17:08:33.358Z"
result = self.imp_travel.validate_timestamp(time)
self.assertEqual(2023, result.year)
self.assertEqual(3, result.month)
self.assertEqual(8, result.day)
self.assertEqual(17, result.hour)
self.assertEqual(8, result.minute)
self.assertEqual(33, result.second)
self.assertIsNotNone("UTC", result.tzinfo)
self.assertIsNotNone(result.tzinfo.utcoffset(result))

def test_validate_timestamp_exceptions(self):
time = "2023-03-08 17:08:33"
result = self.imp_travel.validate_timestamp(time)
self.assertEqual(2023, result.year)
self.assertEqual(3, result.month)
self.assertEqual(8, result.day)
self.assertEqual(17, result.hour)
self.assertEqual(8, result.minute)
self.assertEqual(33, result.second)
self.assertIsNotNone("UTC", result.tzinfo)
self.assertIsNotNone(result.tzinfo.utcoffset(result))

def test_validate_timestamp_notvalid(self):
"""Test validate_timestamp() function in case of a not valid datetime format"""
time = "2023-03-08"
self.assertRaises(ValueError, self.imp_travel.validate_timestamp, time)

def test_update_model(self):
"""Test update_model() function for unique login, so with same user_agent and country"""
user_obj = User.objects.get(username="Lorena Goldoni")
Expand Down
2 changes: 1 addition & 1 deletion django-buffalogs/buffalogs.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: buffalogs
Version: 1.2.3
Version: 1.2.4
Summary: A Django app to detect anomaly logins.
Home-page: UNKNOWN
Author: Lorena Goldoni
Expand Down
2 changes: 1 addition & 1 deletion django-buffalogs/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = buffalogs
version = 1.2.3
version = 1.2.4
description = A Django app to detect anomaly logins.
long_description = file: README.rst
author = Lorena Goldoni
Expand Down
Binary file added docs/static/homepage_buffalogs.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/map_buffalogs.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4924463

Please sign in to comment.