2021-10-15 03:25:38 -05:00
|
|
|
"""
|
|
|
|
Define ix-f import preview, review and post-mortem views.
|
|
|
|
"""
|
|
|
|
|
2019-04-09 09:53:54 -05:00
|
|
|
import base64
|
2021-07-10 10:12:35 -05:00
|
|
|
import json
|
2019-02-15 17:46:40 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2019-04-09 09:53:54 -05:00
|
|
|
from django.contrib.auth import authenticate
|
2021-07-10 10:12:35 -05:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2023-06-20 03:26:06 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django_ratelimit.decorators import ratelimit
|
2019-02-15 17:46:40 +00:00
|
|
|
|
|
|
|
from peeringdb_server import ixf
|
2021-08-18 08:21:22 -05:00
|
|
|
from peeringdb_server.models import IXLan, Network
|
2021-01-13 20:35:07 +00:00
|
|
|
from peeringdb_server.util import check_permissions
|
2019-02-15 17:46:40 +00:00
|
|
|
|
|
|
|
RATELIMITS = settings.RATELIMITS
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
|
2019-04-09 09:53:54 -05:00
|
|
|
def enable_basic_auth(fn):
|
|
|
|
"""
|
2021-10-15 03:25:38 -05:00
|
|
|
A simple decorator to enable basic auth for a specific view.
|
2019-04-09 09:53:54 -05:00
|
|
|
"""
|
2019-12-05 16:57:52 +00:00
|
|
|
|
2019-04-09 09:53:54 -05:00
|
|
|
def wrapped(request, *args, **kwargs):
|
2019-12-05 16:57:52 +00:00
|
|
|
if "HTTP_AUTHORIZATION" in request.META:
|
|
|
|
auth = request.META["HTTP_AUTHORIZATION"].split()
|
2019-04-09 09:53:54 -05:00
|
|
|
if len(auth) == 2:
|
|
|
|
if auth[0].lower() == "basic":
|
2020-08-03 14:51:11 -05:00
|
|
|
username, password = (
|
|
|
|
base64.b64decode(auth[1].encode("utf-8"))
|
|
|
|
.decode("utf-8")
|
|
|
|
.split(":", 1)
|
|
|
|
)
|
2019-04-09 09:53:54 -05:00
|
|
|
request.user = authenticate(username=username, password=password)
|
|
|
|
if not request.user:
|
2019-12-05 16:57:52 +00:00
|
|
|
return JsonResponse(
|
|
|
|
{"non_field_errors": ["Invalid credentials"]}, status=401
|
|
|
|
)
|
2019-04-09 09:53:54 -05:00
|
|
|
return fn(request, *args, **kwargs)
|
2019-12-05 16:57:52 +00:00
|
|
|
|
2019-04-09 09:53:54 -05:00
|
|
|
return wrapped
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
|
2019-09-07 01:08:37 -05:00
|
|
|
def pretty_response(data):
|
2019-12-05 16:57:52 +00:00
|
|
|
return HttpResponse(json.dumps(data, indent=2), content_type="application/json")
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
def error_response(msg, status=400):
|
|
|
|
return JsonResponse({"non_field_errors": [msg]}, status=status)
|
2019-02-15 17:46:40 +00:00
|
|
|
|
2019-09-07 01:08:37 -05:00
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
@ratelimit(
|
2023-06-20 03:26:06 +03:00
|
|
|
key="ip",
|
|
|
|
rate=RATELIMITS["view_import_ixlan_ixf_preview"],
|
|
|
|
group="ixf_preview",
|
|
|
|
block=False,
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2019-04-09 09:53:54 -05:00
|
|
|
@enable_basic_auth
|
2019-02-15 17:46:40 +00:00
|
|
|
def view_import_ixlan_ixf_preview(request, ixlan_id):
|
|
|
|
# check if request was blocked by rate limiting
|
|
|
|
was_limited = getattr(request, "limited", False)
|
|
|
|
if was_limited:
|
2019-12-05 16:57:52 +00:00
|
|
|
return error_response(
|
|
|
|
_("Please wait a bit before requesting " "another ixf import preview."),
|
|
|
|
status=400,
|
|
|
|
)
|
2019-02-15 17:46:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ixlan = IXLan.objects.get(id=ixlan_id)
|
|
|
|
except IXLan.DoesNotExist:
|
2019-09-07 01:08:37 -05:00
|
|
|
return error_response(_("Ixlan not found"), status=404)
|
2019-02-15 17:46:40 +00:00
|
|
|
|
2021-01-13 20:35:07 +00:00
|
|
|
if not check_permissions(request.user, ixlan, "u"):
|
2019-09-07 01:08:37 -05:00
|
|
|
return error_response(_("Permission denied"), status=403)
|
2019-02-15 17:46:40 +00:00
|
|
|
|
|
|
|
importer = ixf.Importer()
|
|
|
|
importer.update(ixlan, save=False)
|
|
|
|
|
2019-09-07 01:08:37 -05:00
|
|
|
return pretty_response(importer.log)
|
|
|
|
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
@ratelimit(
|
2023-06-20 03:26:06 +03:00
|
|
|
key="ip",
|
|
|
|
rate=RATELIMITS["view_import_net_ixf_postmortem"],
|
|
|
|
group="ixf_postmortem",
|
|
|
|
block=False,
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
@enable_basic_auth
|
|
|
|
def view_import_net_ixf_postmortem(request, net_id):
|
|
|
|
# check if request was blocked by rate limiting
|
|
|
|
|
|
|
|
was_limited = getattr(request, "limited", False)
|
|
|
|
if was_limited:
|
2019-12-05 16:57:52 +00:00
|
|
|
return error_response(
|
|
|
|
_("Please wait a bit before requesting " "another IX-F import postmortem."),
|
|
|
|
status=400,
|
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
# load net
|
|
|
|
|
|
|
|
try:
|
|
|
|
net = Network.objects.get(id=net_id, status="ok")
|
|
|
|
except Network.DoesNotExist:
|
|
|
|
return error_response(_("Network not found"), status=404)
|
|
|
|
|
2021-01-13 20:35:07 +00:00
|
|
|
if not check_permissions(request.user, net, "u"):
|
2019-09-07 01:08:37 -05:00
|
|
|
return error_response(_("Permission denied"), status=403)
|
|
|
|
|
|
|
|
# make sure limit is within bounds and a valid number
|
|
|
|
|
|
|
|
try:
|
|
|
|
limit = int(request.GET.get("limit", 25))
|
2021-08-18 08:21:22 -05:00
|
|
|
except Exception:
|
2019-09-07 01:08:37 -05:00
|
|
|
limit = 25
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
if limit < 1:
|
|
|
|
limit = 1
|
|
|
|
|
|
|
|
elif limit > settings.IXF_POSTMORTEM_LIMIT:
|
2019-12-05 16:57:52 +00:00
|
|
|
errors.append(
|
|
|
|
_("Postmortem length cannot exceed {} entries").format(
|
|
|
|
settings.IXF_POSTMORTEM_LIMIT
|
|
|
|
)
|
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
post_mortem = ixf.PostMortem()
|
|
|
|
log = post_mortem.generate(net.asn, limit=limit)
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
return pretty_response({"data": log, "non_field_errors": errors})
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
@ratelimit(
|
2023-06-20 03:26:06 +03:00
|
|
|
key="ip",
|
|
|
|
rate=RATELIMITS["view_import_ixlan_ixf_preview"],
|
|
|
|
group="ixf_preview",
|
|
|
|
block=False,
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
@enable_basic_auth
|
|
|
|
def view_import_net_ixf_preview(request, net_id):
|
|
|
|
# check if request was blocked by rate limiting
|
|
|
|
was_limited = getattr(request, "limited", False)
|
|
|
|
if was_limited:
|
2019-12-05 16:57:52 +00:00
|
|
|
return error_response(
|
|
|
|
_("Please wait a bit before requesting " "another ixf import preview."),
|
|
|
|
status=400,
|
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
net = Network.objects.get(id=net_id, status="ok")
|
|
|
|
except Network.DoesNotExist:
|
|
|
|
return error_response(_("Network not found"), status=404)
|
|
|
|
|
2021-01-13 20:35:07 +00:00
|
|
|
if not check_permissions(request.user, net, "u"):
|
2019-09-07 01:08:37 -05:00
|
|
|
return error_response(_("Permission denied"), status=403)
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
total_log = {"data": [], "errors": []}
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
for ixlan in net.ixlan_set_ixf_enabled:
|
|
|
|
importer = ixf.Importer()
|
2019-12-05 16:57:52 +00:00
|
|
|
importer.cache_only = True
|
2021-08-18 08:21:22 -05:00
|
|
|
importer.update(ixlan, asn=net.asn, save=False)
|
2020-07-15 02:07:01 -05:00
|
|
|
|
|
|
|
# strip suggestions
|
2021-08-18 08:21:22 -05:00
|
|
|
log_data = [i for i in importer.log["data"] if "suggest-" not in i["action"]]
|
2020-07-15 02:07:01 -05:00
|
|
|
|
|
|
|
total_log["data"].extend(log_data)
|
2019-12-05 16:57:52 +00:00
|
|
|
total_log["errors"].extend(
|
2020-07-26 23:36:27 -05:00
|
|
|
[f"{ixlan.ix.name}({ixlan.id}): {err}" for err in importer.log["errors"]]
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2019-09-07 01:08:37 -05:00
|
|
|
|
|
|
|
return pretty_response(total_log)
|