Skip to content

Commit 3deb0ef

Browse files
authored
[fix] Use counter's reply name for CoA
Also added error handling when the NAS name field does not contain a valid ip.
1 parent b162832 commit 3deb0ef

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

openwisp_radius/tasks.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from openwisp_utils.admin_theme.email import send_email
1616
from openwisp_utils.tasks import OpenwispCeleryTask
1717

18+
from . import settings as app_settings
1819
from .radclient.client import RadClient
1920
from .utils import get_one_time_login_url, load_model
2021

@@ -128,17 +129,28 @@ def get_radsecret_from_radacct(rad_acct):
128129
'name', 'secret'
129130
)
130131
for nas in qs.iterator():
131-
if ipaddress.ip_address(rad_acct.nas_ip_address) in ipaddress.ip_network(
132-
nas.name
133-
):
134-
return nas.secret
132+
try:
133+
if ipaddress.ip_address(
134+
rad_acct.nas_ip_address
135+
) in ipaddress.ip_network(nas.name):
136+
return nas.secret
137+
except ValueError:
138+
logger.warning(
139+
f'Failed to parse NAS IP network for "{nas.id}" object. Skipping!'
140+
)
141+
142+
def get_radius_reply_name(check):
143+
try:
144+
return app_settings.CHECK_ATTRIBUTE_COUNTERS_MAP[check.attribute].reply_name
145+
except KeyError:
146+
return check.attribute
135147

136148
def get_radius_attributes():
137149
attributes = {}
138150
rad_group_checks = RadiusGroupCheck.objects.filter(group_id=new_group_id)
139151
if rad_group_checks:
140152
for check in rad_group_checks:
141-
attributes[check.attribute] = f'{check.value}'
153+
attributes[get_radius_reply_name(check)] = f'{check.value}'
142154
elif (
143155
not rad_group_checks
144156
and RadiusGroup.objects.filter(id=new_group_id).exists()
@@ -147,7 +159,7 @@ def get_radius_attributes():
147159
# Unset attributes set by the previous group.
148160
rad_group_checks = RadiusGroupCheck.objects.filter(group_id=old_group_id)
149161
for check in rad_group_checks:
150-
attributes[check.attribute] = ''
162+
attributes[get_radius_reply_name(check)] = ''
151163
return attributes
152164

153165
try:

openwisp_radius/tests/test_models.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,34 @@ def test_perform_change_of_authorization_celery_task_failures(
975975
)
976976
mocked_logger.reset_mock()
977977

978-
self._create_nas(
979-
name='127.0.0.1',
978+
nas = self._create_nas(
979+
name='NAS',
980980
organization=org,
981981
short_name='test',
982982
type='Virtual',
983983
secret='testing123',
984984
)
985985

986+
with self.subTest('Test NAS name does not contain IP network'):
987+
perform_change_of_authorization(
988+
user_id=user.id,
989+
old_group_id=user_group.id,
990+
new_group_id=power_user_group.id,
991+
)
992+
self.assertEqual(
993+
mocked_logger.call_args_list[0][0][0],
994+
f'Failed to parse NAS IP network for "{nas.id}" object. Skipping!',
995+
)
996+
self.assertEqual(
997+
mocked_logger.call_args_list[1][0][0],
998+
f'Failed to find RADIUS secret for "{session.unique_id}"'
999+
' RadiusAccounting object. Skipping CoA operation'
1000+
' for this session.',
1001+
)
1002+
mocked_logger.reset_mock()
1003+
1004+
nas.name = '127.0.0.1'
1005+
nas.save()
9861006
with self.subTest('Test RadClient encountered error while sending CoA packet'):
9871007
perform_change_of_authorization(
9881008
user_id=user.id,
@@ -1026,8 +1046,8 @@ def test_change_of_authorization(self, mocked_radclient, *args):
10261046
mocked_radclient.assert_called_with(
10271047
{
10281048
'User-Name': user.username,
1029-
'Max-Daily-Session': '',
1030-
'Max-Daily-Session-Traffic': '',
1049+
'Session-Timeout': '',
1050+
'ChilliSpot-Max-Total-Octets': '',
10311051
}
10321052
)
10331053
rad_acct.refresh_from_db()
@@ -1042,8 +1062,8 @@ def test_change_of_authorization(self, mocked_radclient, *args):
10421062
mocked_radclient.assert_called_with(
10431063
{
10441064
'User-Name': user.username,
1045-
'Max-Daily-Session': '10800',
1046-
'Max-Daily-Session-Traffic': '3000000000',
1065+
'Session-Timeout': '10800',
1066+
'ChilliSpot-Max-Total-Octets': '3000000000',
10471067
}
10481068
)
10491069
rad_acct.refresh_from_db()

0 commit comments

Comments
 (0)