-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finishing up initial release of this code
- Adds the remaining prereqs for an ol-django app - Adds the migration for storing the MaxMind data - Moves the tests to where they should go - Added a new management command to create GeoIP2 assignments for private IPv4 nets
- Loading branch information
Showing
16 changed files
with
339 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ target( | |
"oauth_toolkit_extensions", | ||
"openedx", | ||
"payment_gateway", | ||
"geoip", | ||
] | ||
], | ||
) |
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,26 @@ | ||
resources( | ||
name="files", | ||
sources=[ | ||
"CHANGELOG.md", | ||
"README.md", | ||
"py.typed", | ||
], | ||
) | ||
|
||
python_sources( | ||
name="geoip", | ||
sources=["**/*.py"], | ||
dependencies=[ | ||
":files", | ||
"//:reqs#setuptools", | ||
], | ||
) | ||
|
||
python_distribution( | ||
name="mitol-django-geoip", | ||
dependencies=[":geoip"], | ||
provides=setup_py( | ||
name="mitol-django-geoip", | ||
description="Django application to handle IP-based geolocation", | ||
), | ||
) |
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,5 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project uses date-based versioning. |
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
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 |
---|---|---|
@@ -1,6 +1,17 @@ | ||
from django.apps import AppConfig | ||
"""GeoIP app AppConfigs""" | ||
import os | ||
|
||
from mitol.common.apps import BaseApp | ||
|
||
class GeoipConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "geoip" | ||
|
||
class GeoIPApp(BaseApp): | ||
"""Default configuration for the GeoIP app""" | ||
|
||
name = "mitol.geoip" | ||
label = "geoip" | ||
verbose_name = "GeoIP" | ||
|
||
required_settings = [] | ||
|
||
# necessary because this is a namespaced app | ||
path = os.path.dirname(os.path.abspath(__file__)) |
37 changes: 37 additions & 0 deletions
37
src/mitol/geoip/changelog.d/20231025_162713_jkachel_geoip_app.rst
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,37 @@ | ||
.. A new scriv changelog fragment. | ||
.. | ||
.. Uncomment the header that is right (remove the leading dots). | ||
.. | ||
.. Removed | ||
.. ------- | ||
.. | ||
.. - A bullet item for the Removed category. | ||
.. | ||
Added | ||
----- | ||
|
||
- Migrated the MaxMind GeoIP code from xPRO into a ol-django app. | ||
- Added a new management command (`create_private_maxmind_data`) to create GeoIP data for local networks. | ||
|
||
.. | ||
.. Changed | ||
.. ------- | ||
.. | ||
.. - A bullet item for the Changed category. | ||
.. | ||
.. Deprecated | ||
.. ---------- | ||
.. | ||
.. - A bullet item for the Deprecated category. | ||
.. | ||
.. Fixed | ||
.. ----- | ||
.. | ||
.. - A bullet item for the Fixed category. | ||
.. | ||
.. Security | ||
.. -------- | ||
.. | ||
.. - A bullet item for the Security category. | ||
.. |
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
75 changes: 75 additions & 0 deletions
75
src/mitol/geoip/management/commands/create_private_maxmind_data.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,75 @@ | ||
""" | ||
Creates a set of netblocks for the private IPv4 netblocks, which aren't in the | ||
MaxMind dataset. | ||
The ISO code must match one that we've imported from MaxMind, so if you haven't | ||
imported the geonames data yet, this will not work (since we have to map the | ||
created netblock to a geoname anyway). | ||
""" | ||
|
||
import ipaddress | ||
from decimal import Decimal | ||
|
||
from django.core.management import BaseCommand, CommandError | ||
|
||
from mitol.geoip.models import Geoname, NetBlock | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Creates MaxMind assignments for the private IPv4 netblocks. | ||
""" | ||
|
||
help = "Creates MaxMind assignments for the private IPv4 netblocks." | ||
MAX_BIGINTEGERFIELD = 9223372036854775807 | ||
|
||
def add_arguments(self, parser) -> None: | ||
parser.add_argument( | ||
"iso", | ||
type=str, | ||
help="The ISO 3166 alpha2 code to assign these to.", | ||
) | ||
|
||
parser.add_argument( | ||
"--remove", | ||
action="store_true", | ||
help="Remove the local address netblocks rather than create them.", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
try: | ||
geoname = Geoname.objects.filter(country_iso_code=kwargs["iso"]).first() | ||
except Geoname.DoesNotExist: | ||
raise CommandError( | ||
f"Could not find a Geoname record for {kwargs['iso']} - have you imported the MaxMind databases?" | ||
) | ||
|
||
netblocks = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] | ||
|
||
if "remove" in kwargs and kwargs["remove"]: | ||
removed_count = NetBlock.objects.filter(network__in=netblocks).delete() | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"{removed_count[0]} netblocks removed!") | ||
) | ||
else: | ||
for netblock in netblocks: | ||
network = ipaddress.ip_network(netblock) | ||
|
||
(_, created) = NetBlock.objects.update_or_create( | ||
network=netblock, | ||
defaults={ | ||
"is_ipv6": False, | ||
"decimal_ip_start": Decimal(int(network[0])), | ||
"decimal_ip_end": Decimal(int(network[-1])), | ||
"ip_start": network[0], | ||
"ip_end": network[-1], | ||
"geoname_id": geoname.geoname_id, | ||
}, | ||
) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"{'Created' if created else 'Updated'} record for {netblock} for ISO {kwargs['iso']}" | ||
) | ||
) |
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
Oops, something went wrong.