Skip to content

Commit

Permalink
Revert "Expiry new country filter"
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorygold authored Jan 26, 2024
1 parent 7161c7d commit c744b83
Show file tree
Hide file tree
Showing 9 changed files with 5 additions and 73 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
## 1.3.x
## 1.3.0
#### Changes
* Changed the *new_country* alert logic: previously, if the new country has already been reported, the alert wasn't triggered again; now, after *CERTEGO_BUFFALOGS_NEW_COUNTRY_ALERT_FILTER* days from the first alert, it can be triggered again

## 1.2.x
### 1.2.9
#### Bugfix
Expand Down
1 change: 0 additions & 1 deletion buffalogs/buffalogs/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@
CERTEGO_BUFFALOGS_LOGIN_MAX_DAYS = 10
CERTEGO_BUFFALOGS_ALERT_MAX_DAYS = 10
CERTEGO_BUFFALOGS_IP_MAX_DAYS = 7
CERTEGO_BUFFALOGS_NEW_COUNTRY_ALERT_FILTER = 30

# Celery config
CELERY_BROKER_URL = CERTEGO_BUFFALOGS_RABBITMQ_URI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def handle(self, *args, **options):
Login.objects.all().delete()
User.objects.all().delete()
TaskSettings.objects.all().delete()
self.stdout.write(self.style.SUCCESS("All the models have been emptied, except the Config model"))
self.stdout.write(self.style.SUCCESS("All the models have been emptied, expect the Config model"))
13 changes: 0 additions & 13 deletions buffalogs/impossible_travel/modules/login_from_new_country.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging

from django.conf import settings
from django.utils import timezone
from impossible_travel.models import Alert
from impossible_travel.modules import impossible_travel

Expand All @@ -18,19 +16,8 @@ def check_country(self, db_user, login_field):
Check Login from new Country and send alert
"""
alert_info = {}
send_alert = False
new_country = login_field["country"]
if db_user.login_set.filter(country=new_country).count() == 0:
# alert if there are no logins from that country before
send_alert = True
elif db_user.alert_set.filter(name="Login from new country", login_raw_data__country=new_country).exists():
if (
abs((timezone.now() - db_user.alert_set.filter(name="Login from new country", login_raw_data__country=new_country).last().created)).days
) >= settings.CERTEGO_BUFFALOGS_NEW_COUNTRY_ALERT_FILTER:
# or... alert if last "new country" alert for that country is older than NEW_COUNTRY_FILTER days
send_alert = True

if send_alert is True:
time = login_field["timestamp"]
alert_info["alert_name"] = Alert.ruleNameEnum.NEW_COUNTRY
alert_info["alert_desc"] = f"{alert_info['alert_name']} for User: {db_user.username}, at: {time}, from: {new_country}"
Expand Down
48 changes: 1 addition & 47 deletions buffalogs/impossible_travel/tests/test_login_from_new_country.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.test import TestCase
from django.utils import timezone
from impossible_travel.models import Alert, Login, User
from impossible_travel.models import Login, User
from impossible_travel.modules import login_from_new_country


Expand All @@ -26,7 +25,6 @@ def setUpTestData(self):
login.save()

def test_check_country(self):
"""Test to check that no new_country alert is sent if a login from that country already exists in the Login model"""
db_user = User.objects.get(username="Lorena Goldoni")
last_login_user_fields = {
"timestamp": "2023-03-08T17:10:33.358Z",
Expand All @@ -38,7 +36,6 @@ def test_check_country(self):
self.assertIsNone(self.new_country.check_country(db_user, last_login_user_fields))

def test_check_country_alert(self):
"""Test new_country alert to be sent"""
db_user = User.objects.get(username="Lorena Goldoni")
last_login_user_fields = {
"timestamp": "2023-03-08T17:10:33.358Z",
Expand All @@ -49,46 +46,3 @@ def test_check_country_alert(self):
}
alert_result = self.new_country.check_country(db_user, last_login_user_fields)
self.assertEqual("Login from new country", alert_result["alert_name"].value)

def test_check_country_days_filter(self):
"""Test that checks that if the last new_country alert was triggered before than 30 days, it will be sent again"""
# insert previous new_country alert with created time > 30 days before
db_user = User.objects.get(username="Lorena Goldoni")
new_login = {
"timestamp": "2023-07-25T12:00:00+00:00",
"lat": "44.4937",
"lon": "11.3430",
"country": "Italy",
"user_agent": "Mozilla/5.0 (X11; U; Linux i686; es-AR; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8",
}
creation_mock_time = timezone.datetime(2023, 7, 25, 12, 0)
login = Login.objects.create(
user=db_user,
timestamp=new_login["timestamp"],
latitude=new_login["lat"],
longitude=new_login["lon"],
country=new_login["country"],
user_agent=new_login["user_agent"],
)
login.created = creation_mock_time
login.save()
alert = Alert.objects.create(
user_id=db_user.id,
login_raw_data=new_login,
name="Login from new country",
description=f"Login from new country for User: {db_user.username}, at: {new_login['timestamp']}, from: {new_login['country']}",
)
# Set a created time before 30 days
alert.created = creation_mock_time
alert.save()
# add new login from Italy that should triggered a new_country alert
last_login_user_fields = {
"timestamp": timezone.now(),
"lat": "44.4937",
"lon": "11.3430",
"country": "Italy",
"user_agent": "Mozilla/5.0 (X11; U; Linux i686; es-AR; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8",
}
alert_result = self.new_country.check_country(db_user, last_login_user_fields)
# it returns the alert because the last new_country alert from Italy is before 30 days
self.assertEqual("Login from new country", alert_result["alert_name"].value)
4 changes: 1 addition & 3 deletions buffalogs/impossible_travel/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ def test_set_alert_vip_user(self):

def test_process_logs_data_lost(self):
TaskSettings.objects.create(
task_name="process_logs",
start_date=timezone.datetime(2023, 4, 18, 10, 0),
end_date=timezone.datetime(2023, 4, 18, 10, 30, 0),
task_name="process_logs", start_date=timezone.datetime(2023, 4, 18, 10, 0), end_date=timezone.datetime(2023, 4, 18, 10, 30, 0)
)
tasks.process_logs()
new_end_date_expected = timezone.now() - timedelta(minutes=1)
Expand Down
1 change: 0 additions & 1 deletion buffalogs/impossible_travel/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from django.test import Client
from django.urls import reverse
from django.utils import timezone
from impossible_travel.models import Alert, Login, User
from rest_framework.test import APITestCase

Expand Down
2 changes: 1 addition & 1 deletion config/elasticsearch/load_templates.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
curl -X PUT "localhost:9200/_template/example?pretty" -H 'Content-Type: application/json' -d'@./example_template.json'
curl -X PUT "localhost:59200/_template/example?pretty" -H 'Content-Type: application/json' -d'@./example_template.json'
2 changes: 1 addition & 1 deletion examples/random_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def main():
es = Elasticsearch(["http://localhost:9200"])
es = Elasticsearch(["http://localhost:59200"])
common_data_cloud_index = generate_common_data()
write_bulk(es, "cloud", common_data_cloud_index)
common_data_weblog_index = generate_common_data()
Expand Down

0 comments on commit c744b83

Please sign in to comment.