Skip to content

Commit

Permalink
added client support for exporting dns records as bind file refs #29
Browse files Browse the repository at this point in the history
  • Loading branch information
infinityofspace committed Aug 24, 2024
1 parent d441db6 commit f5ee81e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
47 changes: 40 additions & 7 deletions pkb_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from typing import Optional, List
from urllib.parse import urljoin

import dns.resolver
import requests

from pkb_client.dns import DNSRecord, DNSRestoreMode, DNSRecordType, DNS_RECORDS_WITH_PRIORITY
from pkb_client.dns import DNSRecord, DNSRestoreMode, DNSRecordType, DNSFileFormat, DNS_RECORDS_WITH_PRIORITY
from pkb_client.domain import DomainInfo
from pkb_client.forwarding import URLForwarding, URLForwardingType
from pkb_client.ssl_cert import SSLCertBundle
Expand Down Expand Up @@ -322,13 +323,19 @@ def dns_retrieve_all(self,
raise PKBClientException(response_json.get("status", "Unknown status"),
response_json.get("message", "Unknown message"))

def dns_export(self, domain: str, filename: str, **kwargs) -> bool:
def dns_export(self,
domain: str,
filename: str,
type: DNSFileFormat = DNSFileFormat.JSON,
**kwargs) -> bool:
"""
Export all DNS record from the given domain as json to a file.
This method does not represent a Porkbun API method.
Export all DNS record from the given domain to a file. This method does not represent a Porkbun API method.
If the file format is JSON, the DNS records with all custom fields like notes are exported.
In case the file format is BIND, a BIND zone file is created, which only contains the essential information.
:param domain: the domain for which the DNS record should be retrieved and saved
:param filename: the filename where to save the exported DNS records
:param type: the file format in which the DNS records should be saved
:return: True if everything went well
"""
Expand All @@ -344,9 +351,35 @@ def dns_export(self, domain: str, filename: str, **kwargs) -> bool:

filepath = Path(filename)
if filepath.exists():
raise Exception("File already exists. Please try another filename")
with open(filepath, "w") as f:
json.dump(dns_records_dict, f)
logging.warning("file already exists, overwriting...")

if type == DNSFileFormat.JSON:
with open(filepath, "w") as f:
json.dump(dns_records_dict, f, default=lambda o: o.__dict__, indent=4)
elif type == DNSFileFormat.BIND:
# domain header
bind_file_content = f"$ORIGIN {domain}"

# SOA record
soa_records = dns.resolver.resolve(domain, "SOA")
if soa_records:
soa_record = soa_records[0]
bind_file_content += f"\n@ IN SOA {soa_record.mname} {soa_record.rname} ({soa_record.serial} {soa_record.refresh} {soa_record.retry} {soa_record.expire} {soa_record.minimum})"

# records
for record in dns_records:
# name record class ttl record type record data
if record.prio:
record_content = f"{record.prio} {record.content}"
else:
record_content = record.content
bind_file_content += f"\n{record.name} IN {record.ttl} {record.type} {record_content}"

with open(filepath, "w") as f:
f.write(bind_file_content)
else:
raise ValueError("Unknown file type")

logging.info("export finished")

return True
Expand Down
3 changes: 3 additions & 0 deletions pkb_client/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class DNSRecordType(str, Enum):
TLSA = "TLSA"
CAA = "CAA"

def __str__(self):
return self.value


DNS_RECORDS_WITH_PRIORITY = {DNSRecordType.MX, DNSRecordType.SRV}

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
setuptools>=39.0.1
requests>=2.20.0
sphinx~=7.0
dnspython~=2.6
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
python_requires=">=3.8",
install_requires=[
"setuptools>=39.0.1",
"requests>=2.20.0"
"requests>=2.20.0",
"dnspython~=2.6"
],
entry_points={
"console_scripts": [
Expand Down

0 comments on commit f5ee81e

Please sign in to comment.