Skip to content

Commit

Permalink
[change] Hash calling_station_id
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed May 6, 2024
1 parent 17909e6 commit c862fba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
17 changes: 3 additions & 14 deletions openwisp_radius/integrations/monitoring/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
import logging

from celery import shared_task
Expand All @@ -8,6 +7,8 @@
from django.utils import timezone
from swapper import load_model

from .utils import clean_registration_method, sha1_hash

Metric = load_model('monitoring', 'Metric')
Chart = load_model('monitoring', 'Chart')
RegisteredUser = load_model('openwisp_radius', 'RegisteredUser')
Expand All @@ -20,18 +21,6 @@
logger = logging.getLogger(__name__)


def sha1_hash(input_string):
sha1 = hashlib.sha1()
sha1.update(input_string.encode('utf-8'))
return sha1.hexdigest()


def clean_registration_method(method):
if method == '':
method = 'unspecified'
return method


def _get_user_signup_metric(organization_id, registration_method):
metric, _ = Metric._get_or_create(
configuration='user_signups',
Expand Down Expand Up @@ -239,7 +228,7 @@ def post_save_radiusaccounting(
extra_tags={
'organization_id': organization_id,
'method': registration_method,
'calling_station_id': calling_station_id,
'calling_station_id': sha1_hash(calling_station_id),
'called_station_id': called_station_id,
'location_id': location_id,
},
Expand Down
7 changes: 4 additions & 3 deletions openwisp_radius/integrations/monitoring/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from openwisp_radius.tests.mixins import BaseTransactionTestCase

from ..migrations import create_general_metrics
from ..utils import sha1_hash
from .mixins import CreateDeviceMonitoringMixin

TASK_PATH = 'openwisp_radius.integrations.monitoring.tasks'
Expand Down Expand Up @@ -62,7 +63,7 @@ def test_post_save_radiusaccounting(self, *args):
content_type=ContentType.objects.get_for_model(self.device_model),
extra_tags={
'called_station_id': device.mac_address,
'calling_station_id': '00:00:00:00:00:00',
'calling_station_id': sha1_hash('00:00:00:00:00:00'),
'location_id': str(device_loc.location.id),
'method': reg_user.method,
'organization_id': str(self.default_org.id),
Expand Down Expand Up @@ -129,7 +130,7 @@ def test_post_save_radius_accounting_device_not_found(self, mocked_logger):
content_type=None,
extra_tags={
'called_station_id': '11:22:33:44:55:66',
'calling_station_id': '00:00:00:00:00:00',
'calling_station_id': sha1_hash('00:00:00:00:00:00'),
'location_id': None,
'method': reg_user.method,
'organization_id': str(self.default_org.id),
Expand Down Expand Up @@ -206,7 +207,7 @@ def test_post_save_radius_accounting_registereduser_not_found(self, mocked_logge
content_type=ContentType.objects.get_for_model(self.device_model),
extra_tags={
'called_station_id': device.mac_address,
'calling_station_id': '00:00:00:00:00:00',
'calling_station_id': sha1_hash('00:00:00:00:00:00'),
'location_id': str(device_loc.location.id),
'method': 'unspecified',
'organization_id': str(self.default_org.id),
Expand Down
13 changes: 13 additions & 0 deletions openwisp_radius/integrations/monitoring/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
from datetime import datetime, timedelta

from django.utils import timezone
Expand All @@ -21,3 +22,15 @@ def get_datetime_filter_start_date():
def get_datetime_filter_stop_date():
stop_date = timezone.localdate() + timedelta(days=1)
return _get_formatted_datetime_string(stop_date)


def sha1_hash(input_string):
sha1 = hashlib.sha1()
sha1.update(input_string.encode('utf-8'))
return sha1.hexdigest()


def clean_registration_method(method):
if method == '':
method = 'unspecified'
return method

0 comments on commit c862fba

Please sign in to comment.