2018-11-08 19:45:21 +00:00
|
|
|
"""
|
|
|
|
DeskPro API Client
|
|
|
|
"""
|
|
|
|
|
2020-07-26 23:36:27 -05:00
|
|
|
import uuid
|
2018-11-08 19:45:21 +00:00
|
|
|
import re
|
|
|
|
import requests
|
2020-07-15 02:07:01 -05:00
|
|
|
import datetime
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
from django.template import loader
|
|
|
|
from django.conf import settings
|
2020-07-15 02:07:01 -05:00
|
|
|
import django.urls
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
from peeringdb_server.models import DeskProTicket
|
|
|
|
from peeringdb_server.inet import RdapNotFoundError
|
|
|
|
|
|
|
|
|
|
|
|
def ticket_queue(subject, body, user):
|
|
|
|
""" queue a deskpro ticket for creation """
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
ticket = DeskProTicket.objects.create(
|
2020-09-30 01:13:38 +00:00
|
|
|
subject=f"{settings.EMAIL_SUBJECT_PREFIX}{subject}",
|
|
|
|
body=body,
|
|
|
|
user=user,
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class APIError(IOError):
|
|
|
|
def __init__(self, msg, data):
|
2020-07-15 02:07:01 -05:00
|
|
|
super().__init__(msg)
|
2018-11-08 19:45:21 +00:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
|
|
|
|
def ticket_queue_asnauto_skipvq(user, org, net, rir_data):
|
|
|
|
"""
|
|
|
|
queue deskro ticket creation for asn automation action: skip vq
|
|
|
|
"""
|
|
|
|
|
|
|
|
if isinstance(net, dict):
|
|
|
|
net_name = net.get("name")
|
|
|
|
else:
|
|
|
|
net_name = net.name
|
|
|
|
|
|
|
|
if isinstance(org, dict):
|
|
|
|
org_name = org.get("name")
|
|
|
|
else:
|
|
|
|
org_name = org.name
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
ticket_queue(
|
2020-07-15 02:07:01 -05:00
|
|
|
f"[ASNAUTO] Network '{net_name}' approved for existing Org '{org_name}'",
|
2019-12-05 16:57:52 +00:00
|
|
|
loader.get_template("email/notify-pdb-admin-asnauto-skipvq.txt").render(
|
|
|
|
{"user": user, "org": org, "net": net, "rir_data": rir_data}
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ticket_queue_asnauto_affil(user, org, net, rir_data):
|
|
|
|
"""
|
|
|
|
queue deskro ticket creation for asn automation action: affil
|
|
|
|
"""
|
|
|
|
|
|
|
|
ticket_queue(
|
2019-12-05 16:57:52 +00:00
|
|
|
"[ASNAUTO] Ownership claim granted to Org '%s' for user '%s'"
|
|
|
|
% (org.name, user.username),
|
|
|
|
loader.get_template("email/notify-pdb-admin-asnauto-affil.txt").render(
|
|
|
|
{"user": user, "org": org, "net": net, "rir_data": rir_data}
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def ticket_queue_asnauto_create(
|
|
|
|
user, org, net, rir_data, asn, org_created=False, net_created=False
|
|
|
|
):
|
2018-11-08 19:45:21 +00:00
|
|
|
"""
|
|
|
|
queue deskro ticket creation for asn automation action: create
|
|
|
|
"""
|
|
|
|
|
|
|
|
subject = []
|
|
|
|
|
|
|
|
if org_created:
|
|
|
|
subject.append("Organization '%s'" % org.name)
|
|
|
|
if net_created:
|
|
|
|
subject.append("Network '%s'" % net.name)
|
|
|
|
|
|
|
|
if not subject:
|
|
|
|
return
|
|
|
|
subject = ", ".join(subject)
|
|
|
|
|
|
|
|
ticket_queue(
|
|
|
|
"[ASNAUTO] %s created" % subject,
|
|
|
|
loader.get_template(
|
2019-12-05 16:57:52 +00:00
|
|
|
"email/notify-pdb-admin-asnauto-entity-creation.txt"
|
|
|
|
).render(
|
|
|
|
{
|
2018-11-08 19:45:21 +00:00
|
|
|
"user": user,
|
|
|
|
"org": org,
|
|
|
|
"net": net,
|
|
|
|
"asn": asn,
|
|
|
|
"org_created": org_created,
|
|
|
|
"net_created": net_created,
|
2019-12-05 16:57:52 +00:00
|
|
|
"rir_data": rir_data,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ticket_queue_rdap_error(user, asn, error):
|
|
|
|
if isinstance(error, RdapNotFoundError):
|
|
|
|
return
|
2020-07-15 02:07:01 -05:00
|
|
|
error_message = f"{error}"
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
if re.match("(.+) returned 400", error_message):
|
|
|
|
return
|
|
|
|
|
2020-07-15 02:07:01 -05:00
|
|
|
subject = f"[RDAP_ERR] {user.username} - AS{asn}"
|
2018-11-08 19:45:21 +00:00
|
|
|
ticket_queue(
|
|
|
|
subject,
|
2019-12-05 16:57:52 +00:00
|
|
|
loader.get_template("email/notify-pdb-admin-rdap-error.txt").render(
|
|
|
|
{"user": user, "asn": asn, "error_details": error_message}
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
2020-07-15 02:07:01 -05:00
|
|
|
class APIClient:
|
2018-11-08 19:45:21 +00:00
|
|
|
def __init__(self, url, key):
|
|
|
|
self.key = key
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_headers(self):
|
2020-07-15 02:07:01 -05:00
|
|
|
return {"Authorization": f"key {self.key}"}
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
def parse_response(self, response, many=False):
|
|
|
|
r_json = response.json()
|
|
|
|
if "status" in r_json:
|
|
|
|
if r_json["status"] >= 400:
|
|
|
|
raise APIError(r_json["message"], r_json)
|
|
|
|
else:
|
|
|
|
response.raise_for_status()
|
|
|
|
data = r_json["data"]
|
|
|
|
if isinstance(data, list):
|
|
|
|
if many:
|
|
|
|
return r_json["data"]
|
|
|
|
elif data:
|
|
|
|
return data[0]
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get(self, endpoint, param):
|
2019-12-05 16:57:52 +00:00
|
|
|
response = requests.get(
|
2020-07-15 02:07:01 -05:00
|
|
|
f"{self.url}/{endpoint}", params=param, headers=self.auth_headers
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
return self.parse_response(response)
|
|
|
|
|
|
|
|
def create(self, endpoint, param):
|
2019-12-05 16:57:52 +00:00
|
|
|
response = requests.post(
|
2020-07-15 02:07:01 -05:00
|
|
|
f"{self.url}/{endpoint}", json=param, headers=self.auth_headers
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
return self.parse_response(response)
|
|
|
|
|
|
|
|
def require_person(self, user):
|
|
|
|
person = self.get("people", {"primary_email": user.email})
|
|
|
|
if not person:
|
|
|
|
person = self.create(
|
2019-12-05 16:57:52 +00:00
|
|
|
"people",
|
|
|
|
{
|
2018-11-08 19:45:21 +00:00
|
|
|
"primary_email": user.email,
|
|
|
|
"first_name": user.first_name,
|
|
|
|
"last_name": user.last_name,
|
2019-12-05 16:57:52 +00:00
|
|
|
"name": user.full_name,
|
|
|
|
},
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
return person
|
|
|
|
|
|
|
|
def create_ticket(self, ticket):
|
|
|
|
person = self.require_person(ticket.user)
|
2020-07-26 23:36:27 -05:00
|
|
|
|
|
|
|
if not ticket.deskpro_id:
|
|
|
|
ticket_response = self.create(
|
|
|
|
"tickets",
|
|
|
|
{
|
|
|
|
"subject": ticket.subject,
|
|
|
|
"person": {"id": person["id"]},
|
|
|
|
"status": "awaiting_agent",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ticket.deskpro_ref = ticket_response["ref"]
|
|
|
|
ticket.deskpro_id = ticket_response["id"]
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
self.create(
|
2020-09-30 01:13:38 +00:00
|
|
|
f"tickets/{ticket.deskpro_id}/messages",
|
2019-12-05 16:57:52 +00:00
|
|
|
{
|
2018-11-08 19:45:21 +00:00
|
|
|
"message": ticket.body.replace("\n", "<br />\n"),
|
|
|
|
"person": person["id"],
|
2019-12-05 16:57:52 +00:00
|
|
|
"format": "html",
|
|
|
|
},
|
|
|
|
)
|
2020-07-15 02:07:01 -05:00
|
|
|
|
|
|
|
|
2020-07-26 23:36:27 -05:00
|
|
|
class MockAPIClient(APIClient):
|
|
|
|
|
|
|
|
"""
|
|
|
|
A mock api client for the deskpro API
|
|
|
|
|
|
|
|
The IX-F importer uses this when
|
|
|
|
IXF_SEND_TICKETS=False
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.ticket_count = 0
|
|
|
|
|
|
|
|
def get(self, endpoint, param):
|
|
|
|
|
|
|
|
if endpoint == "people":
|
|
|
|
return {"id": 1}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def create(self, endpoint, param):
|
|
|
|
if endpoint == "tickets":
|
|
|
|
self.ticket_count += 1
|
2020-09-30 01:13:38 +00:00
|
|
|
ref = f"{uuid.uuid4()}"
|
2020-07-26 23:36:27 -05:00
|
|
|
return {"ref": ref[:16], "id": self.ticket_count}
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2020-07-15 02:07:01 -05:00
|
|
|
def ticket_queue_deletion_prevented(user, instance):
|
|
|
|
"""
|
|
|
|
queue deskpro ticket to notify about the prevented
|
|
|
|
deletion of an object #696
|
|
|
|
"""
|
|
|
|
|
|
|
|
subject = (
|
|
|
|
f"[PROTECTED] Deletion prevented: "
|
|
|
|
f"{instance.HandleRef.tag}-{instance.id} "
|
|
|
|
f"{instance}"
|
|
|
|
)
|
|
|
|
|
2020-09-29 20:01:47 +00:00
|
|
|
# we don't want to spam DeskPRO with tickets when a user
|
2020-07-15 02:07:01 -05:00
|
|
|
# repeatedly clicks the delete button for an object
|
|
|
|
#
|
|
|
|
# so we check if a ticket has recently been sent for it
|
|
|
|
# and opt out if it falls with in the spam protection
|
|
|
|
# period defined in settings
|
|
|
|
|
|
|
|
period = settings.PROTECTED_OBJECT_NOTIFICATION_PERIOD
|
|
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
max_age = now - datetime.timedelta(hours=period)
|
|
|
|
ticket = DeskProTicket.objects.filter(
|
|
|
|
subject=f"{settings.EMAIL_SUBJECT_PREFIX}{subject}"
|
|
|
|
)
|
|
|
|
ticket = ticket.filter(created__gt=max_age)
|
|
|
|
|
|
|
|
# recent ticket for object exists, bail
|
|
|
|
|
|
|
|
if ticket.exists():
|
|
|
|
return
|
|
|
|
|
|
|
|
model_name = instance.__class__.__name__.lower()
|
|
|
|
|
|
|
|
# create ticket
|
|
|
|
|
|
|
|
ticket_queue(
|
|
|
|
subject,
|
|
|
|
loader.get_template("email/notify-pdb-admin-deletion-prevented.txt").render(
|
|
|
|
{
|
|
|
|
"user": user,
|
|
|
|
"instance": instance,
|
|
|
|
"admin_url": settings.BASE_URL
|
|
|
|
+ django.urls.reverse(
|
|
|
|
f"admin:peeringdb_server_{model_name}_change", args=(instance.id,)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
)
|