Commit 3802e8f5 by Sarina Canelake

Merge pull request #2779 from edx/sarina/embargo-logging

Add logging to embargo middleware
parents 030004bc f79cefe1
......@@ -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.,
HTTP_X_FORWARDED_FOR).
"""
import logging
import pygeoip
from django.core.exceptions import MiddlewareNotUsed
......@@ -18,6 +18,8 @@ from util.request import course_id_from_url
from embargo.models import EmbargoedCourse, EmbargoedState, IPFilter
log = logging.getLogger(__name__)
class EmbargoMiddleware(object):
"""
......@@ -45,10 +47,15 @@ class EmbargoMiddleware(object):
# if blacklisted, immediately fail
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')
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
# 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:
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')
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