-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes done to add new attribute allowed_ip_addresses to siteprofile…
… and validating client ip against it
- Loading branch information
1 parent
20301bb
commit d7c4b39
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -734,6 +734,7 @@ class Meta: | |
"show_metadeploy_wordmark", | ||
"company_logo", | ||
"favicon", | ||
"allowed_ip_addresses", | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from metadeploy.api.models import SiteProfile | ||
from . import current_site_id | ||
from django.http import HttpResponseForbidden | ||
|
||
class IPRestrictMiddleware: | ||
def getSiteProfile(self): | ||
profile = SiteProfile.objects.filter(site=current_site_id()).first() | ||
return profile | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
client_ip = request.META.get('REMOTE_ADDR', None) | ||
profile = self.getSiteProfile() | ||
|
||
if hasattr(profile, "allowed_ip_addresses") and profile.allowed_ip_addresses: | ||
if client_ip not in profile.allowed_ip_addresses: | ||
return HttpResponseForbidden("You don't have permission to access this resource.") | ||
|
||
response = self.get_response(request) | ||
return response |
70 changes: 70 additions & 0 deletions
70
metadeploy/multitenancy/tests/test_iprestrict_middleware.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from metadeploy.multitenancy.iprestrict_middleware import IPRestrictMiddleware | ||
from django.contrib.sites.models import Site | ||
from metadeploy.api.models import SiteProfile | ||
from django.test import RequestFactory, TestCase | ||
from unittest.mock import patch | ||
|
||
|
||
class IPRestrictionMiddlewaretest(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
|
||
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile') | ||
def test_ip_restrict_middleware_with_matching_allowed_client_ip(self, mock_site_profile_get): | ||
request = self.factory.get('/test') | ||
request.META["REMOTE_ADDR"] = "127.0.0.1" | ||
|
||
site = Site.objects.create(name="Test") | ||
mock_site_profile = SiteProfile() | ||
mock_site_profile.site = site | ||
mock_site_profile.name = site.name | ||
mock_site_profile.allowed_ip_addresses = '["127.0.0.1"]' | ||
mock_site_profile_get.return_value = mock_site_profile | ||
|
||
response = IPRestrictMiddleware(lambda x: x)(request) | ||
assert response == request | ||
|
||
|
||
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile') | ||
def test_ip_restrict_middleware_without_matching_allowed_client_ip(self, mock_site_profile_get): | ||
request = self.factory.get('/test') | ||
request.META["REMOTE_ADDR"] = "127.0.0.2" | ||
|
||
site = Site.objects.create(name="Test") | ||
mock_site_profile = SiteProfile() | ||
mock_site_profile.site = site | ||
mock_site_profile.name = site.name | ||
mock_site_profile.allowed_ip_addresses = '["127.0.0.1"]' | ||
mock_site_profile_get.return_value = mock_site_profile | ||
|
||
response = IPRestrictMiddleware(lambda x: x)(request) | ||
assert response.status_code == 403 | ||
|
||
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile') | ||
def test_ip_restrict_middleware_without_allowed_list(self, mock_site_profile_get): | ||
request = self.factory.get('/test') | ||
request.META["REMOTE_ADDR"] = "127.0.0.1" | ||
|
||
site = Site.objects.create(name="Test") | ||
mock_site_profile = SiteProfile() | ||
mock_site_profile.site = site | ||
mock_site_profile.name = site.name | ||
mock_site_profile_get.return_value = mock_site_profile | ||
|
||
response = IPRestrictMiddleware(lambda x: x)(request) | ||
assert response == request | ||
|
||
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile') | ||
def test_ip_restrict_middleware_with_allowed_list_none(self, mock_site_profile_get): | ||
request = self.factory.get('/test') | ||
request.META["REMOTE_ADDR"] = "127.0.0.1" | ||
|
||
site = Site.objects.create(name="Test") | ||
mock_site_profile = SiteProfile() | ||
mock_site_profile.site = site | ||
mock_site_profile.name = site.name | ||
mock_site_profile.allowed_ip_addresses = None | ||
mock_site_profile_get.return_value = mock_site_profile | ||
|
||
response = IPRestrictMiddleware(lambda x: x)(request) | ||
assert response == request |