From 8d79debc863b5f6725cef4df96f7e50f57b33633 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Thu, 14 Nov 2024 19:05:55 +0530 Subject: [PATCH] refactor: remove DocType `DKIM Key` --- .../mail_server/doctype/dkim_key/__init__.py | 0 .../mail_server/doctype/dkim_key/dkim_key.js | 8 -- .../doctype/dkim_key/dkim_key.json | 118 ---------------- .../mail_server/doctype/dkim_key/dkim_key.py | 131 ------------------ .../doctype/dkim_key/test_dkim_key.py | 9 -- 5 files changed, 266 deletions(-) delete mode 100644 mail_server/mail_server/doctype/dkim_key/__init__.py delete mode 100644 mail_server/mail_server/doctype/dkim_key/dkim_key.js delete mode 100644 mail_server/mail_server/doctype/dkim_key/dkim_key.json delete mode 100644 mail_server/mail_server/doctype/dkim_key/dkim_key.py delete mode 100644 mail_server/mail_server/doctype/dkim_key/test_dkim_key.py diff --git a/mail_server/mail_server/doctype/dkim_key/__init__.py b/mail_server/mail_server/doctype/dkim_key/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mail_server/mail_server/doctype/dkim_key/dkim_key.js b/mail_server/mail_server/doctype/dkim_key/dkim_key.js deleted file mode 100644 index c9e653d..0000000 --- a/mail_server/mail_server/doctype/dkim_key/dkim_key.js +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors -// For license information, please see license.txt - -// frappe.ui.form.on("DKIM Key", { -// refresh(frm) { - -// }, -// }); diff --git a/mail_server/mail_server/doctype/dkim_key/dkim_key.json b/mail_server/mail_server/doctype/dkim_key/dkim_key.json deleted file mode 100644 index 32064cc..0000000 --- a/mail_server/mail_server/doctype/dkim_key/dkim_key.json +++ /dev/null @@ -1,118 +0,0 @@ -{ - "actions": [], - "creation": "2024-08-30 23:34:24.393298", - "doctype": "DocType", - "engine": "InnoDB", - "field_order": [ - "section_break_pfdy", - "enabled", - "section_break_cu1l", - "domain_name", - "column_break_mylf", - "key_size", - "section_break_ktxa", - "private_key", - "column_break_hqz9", - "public_key" - ], - "fields": [ - { - "fieldname": "section_break_pfdy", - "fieldtype": "Section Break" - }, - { - "default": "1", - "depends_on": "eval: !doc.__islocal", - "fieldname": "enabled", - "fieldtype": "Check", - "label": "Enabled", - "read_only": 1, - "search_index": 1 - }, - { - "fieldname": "section_break_cu1l", - "fieldtype": "Section Break" - }, - { - "fieldname": "domain_name", - "fieldtype": "Link", - "in_list_view": 1, - "in_standard_filter": 1, - "label": "Domain Name", - "no_copy": 1, - "options": "Mail Domain Registry", - "reqd": 1, - "search_index": 1, - "set_only_once": 1 - }, - { - "fieldname": "column_break_mylf", - "fieldtype": "Column Break" - }, - { - "fieldname": "key_size", - "fieldtype": "Select", - "in_list_view": 1, - "in_standard_filter": 1, - "label": "Key Size (Bits)", - "no_copy": 1, - "options": "\n1024\n2048\n4096", - "set_only_once": 1 - }, - { - "fieldname": "section_break_ktxa", - "fieldtype": "Section Break" - }, - { - "fieldname": "private_key", - "fieldtype": "Text", - "label": "Private Key", - "no_copy": 1, - "read_only": 1, - "set_only_once": 1 - }, - { - "fieldname": "column_break_hqz9", - "fieldtype": "Column Break" - }, - { - "fieldname": "public_key", - "fieldtype": "Text", - "label": "Public Key", - "no_copy": 1, - "read_only": 1, - "set_only_once": 1 - } - ], - "in_create": 1, - "index_web_pages_for_search": 1, - "links": [ - { - "group": "Reference", - "link_doctype": "DNS Record", - "link_fieldname": "attached_to_docname" - } - ], - "modified": "2024-10-19 14:05:56.620178", - "modified_by": "Administrator", - "module": "Mail Server", - "name": "DKIM Key", - "owner": "Administrator", - "permissions": [ - { - "create": 1, - "delete": 1, - "email": 1, - "export": 1, - "print": 1, - "read": 1, - "report": 1, - "role": "System Manager", - "share": 1, - "write": 1 - } - ], - "sort_field": "creation", - "sort_order": "DESC", - "states": [] -} \ No newline at end of file diff --git a/mail_server/mail_server/doctype/dkim_key/dkim_key.py b/mail_server/mail_server/doctype/dkim_key/dkim_key.py deleted file mode 100644 index 8e71b6a..0000000 --- a/mail_server/mail_server/doctype/dkim_key/dkim_key.py +++ /dev/null @@ -1,131 +0,0 @@ -# Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors -# For license information, please see license.txt - -import frappe -from frappe import _, generate_hash -from frappe.model.document import Document -from frappe.utils import cint -from frappe.utils.caching import request_cache - -from mail_server.mail_server.doctype.dns_record.dns_record import create_or_update_dns_record - - -class DKIMKey(Document): - def autoname(self) -> None: - self.name = f"{self.domain_name.replace('.', '-')}-{generate_hash(length=10)}" - - def validate(self) -> None: - self.validate_domain_name() - self.validate_key_size() - self.generate_dkim_keys() - - def after_insert(self) -> None: - self.create_or_update_dns_record() - self.disable_existing_dkim_keys() - - def on_trash(self) -> None: - if frappe.session.user != "Administrator": - frappe.throw(_("Only Administrator can delete DKIM Key.")) - - def validate_domain_name(self) -> None: - """Validates the Domain Name.""" - - if not self.domain_name: - frappe.throw(_("Domain Name is mandatory")) - - def validate_key_size(self) -> None: - """Validates the Key Size.""" - - if self.key_size: - if cint(self.key_size) < 1024: - frappe.throw(_("Key Size must be greater than 1024.")) - else: - self.key_size = frappe.db.get_single_value( - "Mail Server Settings", "default_dkim_key_size", cache=True - ) - - def generate_dkim_keys(self) -> None: - """Generates the DKIM Keys.""" - - self.private_key, self.public_key = generate_dkim_keys(cint(self.key_size)) - - def create_or_update_dns_record(self) -> None: - """Creates or Updates the DNS Record.""" - - frappe.flags.enqueue_dns_record_update = True - create_or_update_dns_record( - host=f"{self.domain_name.replace('.', '-')}._domainkey", - type="TXT", - value=f"v=DKIM1; k=rsa; p={self.public_key}", - category="Sending Record", - attached_to_doctype=self.doctype, - attached_to_docname=self.name, - ) - - def disable_existing_dkim_keys(self) -> None: - """Disables the existing DKIM Keys.""" - - DKIM_KEY = frappe.qb.DocType("DKIM Key") - ( - frappe.qb.update(DKIM_KEY) - .set(DKIM_KEY.enabled, 0) - .where( - (DKIM_KEY.enabled == 1) - & (DKIM_KEY.name != self.name) - & (DKIM_KEY.domain_name == self.domain_name) - ) - ).run() - - -def create_dkim_key(domain_name: str, key_size: int | None = None) -> "DKIMKey": - """Creates a DKIM Key document.""" - - doc = frappe.new_doc("DKIM Key") - doc.enabled = 1 - doc.domain_name = domain_name - doc.key_size = key_size - doc.flags.ignore_links = True - doc.save(ignore_permissions=True) - - return doc - - -def generate_dkim_keys(key_size: int = 1024) -> tuple[str, str]: - """Generates the DKIM Keys.""" - - def get_filtered_dkim_key(key_pem: str) -> str: - """Returns the filtered DKIM Key.""" - - key_pem = "".join(key_pem.split()) - key_pem = ( - key_pem.replace("-----BEGINPUBLICKEY-----", "") - .replace("-----ENDPUBLICKEY-----", "") - .replace("-----BEGINRSAPRIVATEKEY-----", "") - .replace("----ENDRSAPRIVATEKEY-----", "") - ) - - return key_pem - - from cryptography.hazmat.backends import default_backend - from cryptography.hazmat.primitives import serialization - from cryptography.hazmat.primitives.asymmetric import rsa - - private_key = rsa.generate_private_key( - public_exponent=65537, key_size=key_size, backend=default_backend() - ) - public_key = private_key.public_key() - - private_key_pem = private_key.private_bytes( - encoding=serialization.Encoding.PEM, - format=serialization.PrivateFormat.TraditionalOpenSSL, - encryption_algorithm=serialization.NoEncryption(), - ).decode() - public_key_pem = public_key.public_bytes( - encoding=serialization.Encoding.PEM, - format=serialization.PublicFormat.SubjectPublicKeyInfo, - ).decode() - - private_key = private_key_pem - public_key = get_filtered_dkim_key(public_key_pem) - - return private_key, public_key diff --git a/mail_server/mail_server/doctype/dkim_key/test_dkim_key.py b/mail_server/mail_server/doctype/dkim_key/test_dkim_key.py deleted file mode 100644 index f186cad..0000000 --- a/mail_server/mail_server/doctype/dkim_key/test_dkim_key.py +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and Contributors -# See license.txt - -# import frappe -from frappe.tests.utils import FrappeTestCase - - -class TestDKIMKey(FrappeTestCase): - pass