2024-08-05 12:20:44 +02:00
|
|
|
"""A minimalist client to synchronize with mailbox provisioning API."""
|
|
|
|
|
|
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core import exceptions
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from urllib3.util import Retry
|
|
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
adapter = requests.adapters.HTTPAdapter(
|
|
|
|
|
max_retries=Retry(
|
|
|
|
|
total=4,
|
|
|
|
|
backoff_factor=0.1,
|
|
|
|
|
status_forcelist=[500, 502],
|
|
|
|
|
allowed_methods=["PATCH"],
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
session = requests.Session()
|
|
|
|
|
session.mount("http://", adapter)
|
|
|
|
|
session.mount("https://", adapter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DimailAPIClient:
|
|
|
|
|
"""A dimail-API client."""
|
|
|
|
|
|
2024-08-26 19:10:43 +02:00
|
|
|
API_URL = settings.MAIL_PROVISIONING_API_URL
|
2024-09-09 19:03:59 +02:00
|
|
|
API_CREDENTIALS = settings.MAIL_PROVISIONING_API_CREDENTIALS
|
2024-08-26 19:10:43 +02:00
|
|
|
|
2024-08-30 15:49:04 +02:00
|
|
|
def get_headers(self):
|
2024-09-09 19:03:59 +02:00
|
|
|
"""
|
|
|
|
|
Build headers dictionary. Requires MAIL_PROVISIONING_API_CREDENTIALS setting,
|
|
|
|
|
to get a token from dimail /token/ endpoint.
|
|
|
|
|
"""
|
2024-08-05 12:20:44 +02:00
|
|
|
headers = {"Content-Type": "application/json"}
|
|
|
|
|
|
|
|
|
|
response = requests.get(
|
2024-08-26 19:10:43 +02:00
|
|
|
f"{self.API_URL}/token/",
|
2024-09-09 19:03:59 +02:00
|
|
|
headers={"Authorization": f"Basic {self.API_CREDENTIALS}"},
|
2024-09-03 18:29:28 +02:00
|
|
|
timeout=20,
|
2024-08-05 12:20:44 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-06 17:18:27 +02:00
|
|
|
if response.status_code == status.HTTP_200_OK:
|
|
|
|
|
headers["Authorization"] = f"Bearer {response.json()['access_token']}"
|
|
|
|
|
logger.info("Token succesfully granted by mail-provisioning API.")
|
|
|
|
|
return headers
|
|
|
|
|
|
2024-09-03 18:29:28 +02:00
|
|
|
if response.status_code == status.HTTP_403_FORBIDDEN:
|
|
|
|
|
logger.error(
|
2024-08-30 15:49:04 +02:00
|
|
|
"[DIMAIL] 403 Forbidden: Could not retrieve a token,\
|
|
|
|
|
please check 'MAIL_PROVISIONING_API_CREDENTIALS' setting.",
|
2024-08-05 12:20:44 +02:00
|
|
|
)
|
2024-09-09 19:03:59 +02:00
|
|
|
raise exceptions.PermissionDenied(
|
|
|
|
|
"Token denied. Please check your MAIL_PROVISIONING_API_CREDENTIALS."
|
|
|
|
|
)
|
2024-08-05 12:20:44 +02:00
|
|
|
|
2024-09-06 17:18:27 +02:00
|
|
|
return self.pass_dimail_unexpected_response(response)
|
2024-08-05 12:20:44 +02:00
|
|
|
|
|
|
|
|
def send_mailbox_request(self, mailbox):
|
|
|
|
|
"""Send a CREATE mailbox request to mail provisioning API."""
|
|
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
|
"givenName": mailbox.first_name,
|
|
|
|
|
"surName": mailbox.last_name,
|
|
|
|
|
"displayName": f"{mailbox.first_name} {mailbox.last_name}",
|
|
|
|
|
}
|
2024-09-09 19:03:59 +02:00
|
|
|
headers = self.get_headers()
|
2024-08-05 12:20:44 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = session.post(
|
2024-08-26 19:10:43 +02:00
|
|
|
f"{self.API_URL}/domains/{mailbox.domain}/mailboxes/{mailbox.local_part}/",
|
2024-08-05 12:20:44 +02:00
|
|
|
json=payload,
|
2024-09-09 19:03:59 +02:00
|
|
|
headers=headers,
|
2024-08-05 12:20:44 +02:00
|
|
|
verify=True,
|
|
|
|
|
timeout=10,
|
|
|
|
|
)
|
2024-08-26 19:10:43 +02:00
|
|
|
except requests.exceptions.ConnectionError as error:
|
2024-08-05 12:20:44 +02:00
|
|
|
logger.error(
|
|
|
|
|
"Connection error while trying to reach %s.",
|
2024-08-26 19:10:43 +02:00
|
|
|
self.API_URL,
|
|
|
|
|
exc_info=error,
|
2024-08-05 12:20:44 +02:00
|
|
|
)
|
2024-08-26 19:10:43 +02:00
|
|
|
raise error
|
2024-08-05 12:20:44 +02:00
|
|
|
|
|
|
|
|
if response.status_code == status.HTTP_201_CREATED:
|
|
|
|
|
extra = {"response": response.content.decode("utf-8")}
|
|
|
|
|
# This a temporary broken solution. Password will soon be sent
|
|
|
|
|
# from OX servers but their prod is not ready.
|
|
|
|
|
# In the meantime, we log mailbox info (including password !)
|
|
|
|
|
logger.info(
|
|
|
|
|
"Mailbox successfully created on domain %s",
|
|
|
|
|
mailbox.domain.name,
|
|
|
|
|
extra=extra,
|
|
|
|
|
)
|
2024-09-03 18:29:28 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
if response.status_code == status.HTTP_403_FORBIDDEN:
|
2024-08-08 18:28:01 +02:00
|
|
|
raise exceptions.PermissionDenied(
|
2024-08-30 15:49:04 +02:00
|
|
|
"Permission denied. Please check your MAIL_PROVISIONING_API_CREDENTIALS."
|
2024-08-26 19:10:43 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-06 17:18:27 +02:00
|
|
|
return self.pass_dimail_unexpected_response(response)
|
|
|
|
|
|
|
|
|
|
def pass_dimail_unexpected_response(self, response):
|
|
|
|
|
"""Raise error when encountering an unexpected error in dimail."""
|
|
|
|
|
error_content = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
logger.error("[DIMAIL] unexpected error : %s", error_content)
|
|
|
|
|
raise SystemError(f"Unexpected response from dimail: {error_content}")
|