Add ip anonymization

This commit is contained in:
R. Miles McCain 2020-04-11 18:54:03 -04:00
parent bf0d99ccd2
commit 74c87bcc7a
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
4 changed files with 20 additions and 3 deletions

View File

@ -16,6 +16,7 @@ pyyaml = "*"
ua-parser = "*"
user-agents = "*"
django-humanize = "*"
anonymizeip = "*"
[requires]
python_version = "3.6"

10
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "13b07e6285b739fe6f4987482aa448f1773d051de2de4806126a0e4bded150cf"
"sha256": "5c4a63923138fea970c8851ac72d6d6a391db28090a02d738e675775d2c6f26b"
},
"pipfile-spec": 6,
"requires": {
@ -23,6 +23,14 @@
],
"version": "==2.5.2"
},
"anonymizeip": {
"hashes": [
"sha256:491cb94a31bae23294c5b93a13dd5c9ed55be98003c622e76e2fe64d6a4f3e91",
"sha256:e0d446b06b2bbf236394a90b971de403f90f805d14db3a405f8731716acad1fe"
],
"index": "pypi",
"version": "==1.0.0"
},
"asgiref": {
"hashes": [
"sha256:8036f90603c54e93521e5777b2b9a39ba1bad05773fcf2d208f0299d1df58ce5",

View File

@ -7,6 +7,7 @@ from celery import shared_task
from django.conf import settings
from django.core.cache import cache
from django.utils import timezone
from anonymizeip import anonymize_ip
from core.models import Service
@ -44,11 +45,15 @@ def ingress_request(
service_uuid, tracker, time, payload, ip, location, user_agent, identifier=""
):
try:
ip_data = _geoip2_lookup(ip)
service = Service.objects.get(uuid=service_uuid, status=Service.ACTIVE)
log.debug(f"Linked to service {service}")
ip_data = _geoip2_lookup(ip)
log.debug(f"Found geoip2 data")
if service.anonymize_ips:
ip = anonymize_ip(ip)
# Create or update session
session = Session.objects.filter(
service=service,

View File

@ -39,6 +39,9 @@ class Service(models.Model):
max_length=2, choices=SERVICE_STATUSES, default=ACTIVE, db_index=True
)
# Analytics settings
anonymize_ips = models.BooleanField(default=False)
def __str__(self):
return self.name