diff --git a/CHANGELOG.md b/CHANGELOG.md index e6bdf9b..e0f2605 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 8f812c1..50a6987 100644 --- a/README.md +++ b/README.md @@ -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) +BuffaLogs Map Page *For further examples: [Wiki - Example](https://github.com/certego/BuffaLogs/wiki/3.-Example)* diff --git a/buffalogs/buffalogs/settings/settings.py b/buffalogs/buffalogs/settings/settings.py index 29ab44e..ff2ebb3 100644 --- a/buffalogs/buffalogs/settings/settings.py +++ b/buffalogs/buffalogs/settings/settings.py @@ -169,7 +169,7 @@ USE_I18N = True -# USE_TZ = True +USE_TZ = True # Static files (CSS, JavaScript, Images) diff --git a/buffalogs/impossible_travel/modules/impossible_travel.py b/buffalogs/impossible_travel/modules/impossible_travel.py index 4d5d060..c936bc6 100644 --- a/buffalogs/impossible_travel/modules/impossible_travel.py +++ b/buffalogs/impossible_travel/modules/impossible_travel.py @@ -1,4 +1,5 @@ import logging +from datetime import datetime from django.conf import settings from django.utils import timezone @@ -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: @@ -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 diff --git a/buffalogs/impossible_travel/tests/test_impossible_travel.py b/buffalogs/impossible_travel/tests/test_impossible_travel.py index 7b68a25..60399d8 100644 --- a/buffalogs/impossible_travel/tests/test_impossible_travel.py +++ b/buffalogs/impossible_travel/tests/test_impossible_travel.py @@ -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") diff --git a/django-buffalogs/buffalogs.egg-info/PKG-INFO b/django-buffalogs/buffalogs.egg-info/PKG-INFO index 7377fdf..b630b69 100644 --- a/django-buffalogs/buffalogs.egg-info/PKG-INFO +++ b/django-buffalogs/buffalogs.egg-info/PKG-INFO @@ -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 diff --git a/django-buffalogs/setup.cfg b/django-buffalogs/setup.cfg index ab14d01..9dcdc18 100644 --- a/django-buffalogs/setup.cfg +++ b/django-buffalogs/setup.cfg @@ -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 diff --git a/docs/static/homepage_buffalogs.png b/docs/static/homepage_buffalogs.png new file mode 100644 index 0000000..eb9218c Binary files /dev/null and b/docs/static/homepage_buffalogs.png differ diff --git a/docs/static/map_buffalogs.png b/docs/static/map_buffalogs.png new file mode 100644 index 0000000..2522cfc Binary files /dev/null and b/docs/static/map_buffalogs.png differ