Commit f79cefe1 by Sarina Canelake

Add logging to embargo middleware

parent c4401fdc
...@@ -7,7 +7,7 @@ experiencing mysterious problems with embargoing, please check that your ...@@ -7,7 +7,7 @@ experiencing mysterious problems with embargoing, please check that your
reverse proxy is setting any of the well known client IP address headers (ex., reverse proxy is setting any of the well known client IP address headers (ex.,
HTTP_X_FORWARDED_FOR). HTTP_X_FORWARDED_FOR).
""" """
import logging
import pygeoip import pygeoip
from django.core.exceptions import MiddlewareNotUsed from django.core.exceptions import MiddlewareNotUsed
...@@ -18,6 +18,8 @@ from util.request import course_id_from_url ...@@ -18,6 +18,8 @@ from util.request import course_id_from_url
from embargo.models import EmbargoedCourse, EmbargoedState, IPFilter from embargo.models import EmbargoedCourse, EmbargoedState, IPFilter
log = logging.getLogger(__name__)
class EmbargoMiddleware(object): class EmbargoMiddleware(object):
""" """
...@@ -45,10 +47,15 @@ class EmbargoMiddleware(object): ...@@ -45,10 +47,15 @@ class EmbargoMiddleware(object):
# if blacklisted, immediately fail # if blacklisted, immediately fail
if ip_addr in IPFilter.current().blacklist_ips: if ip_addr in IPFilter.current().blacklist_ips:
log.info("Embargo: Restricting IP address %s to course %s because IP is blacklisted.", ip_addr, course_id)
return redirect('embargo') return redirect('embargo')
country_code_from_ip = pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr) country_code_from_ip = pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr)
is_embargoed = country_code_from_ip in EmbargoedState.current().embargoed_countries_list is_embargoed = country_code_from_ip in EmbargoedState.current().embargoed_countries_list
# Fail if country is embargoed and the ip address isn't explicitly whitelisted # Fail if country is embargoed and the ip address isn't explicitly whitelisted
if is_embargoed and ip_addr not in IPFilter.current().whitelist_ips: if is_embargoed and ip_addr not in IPFilter.current().whitelist_ips:
log.info(
"Embargo: Restricting IP address %s to course %s because IP is from country %s.",
ip_addr, course_id, country_code_from_ip
)
return redirect('embargo') return redirect('embargo')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment