basic logic

This commit is contained in:
zandercymatics 2024-12-03 14:07:15 -07:00
parent 28b964cc47
commit 366ecb97d9
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 98 additions and 1 deletions

View file

@ -86,6 +86,10 @@ secret_registry_key = b64decode(secret("REGISTRY_KEY", ""))
secret_registry_key_passphrase = secret("REGISTRY_KEY_PASSPHRASE", "")
secret_registry_hostname = secret("REGISTRY_HOSTNAME")
# PROTOTYPE: Used for DNS hosting
secret_registry_tenant_key = secret("REGISTRY_TENANT_KEY", None)
secret_registry_tenant_id = secret("REGISTRY_TENANT_ID", None)
# region: Basic Django Config-----------------------------------------------###
# Build paths inside the project like this: BASE_DIR / "subdir".
@ -685,6 +689,8 @@ SECRET_REGISTRY_CERT = secret_registry_cert
SECRET_REGISTRY_KEY = secret_registry_key
SECRET_REGISTRY_KEY_PASSPHRASE = secret_registry_key_passphrase
SECRET_REGISTRY_HOSTNAME = secret_registry_hostname
SECRET_REGISTRY_TENANT_KEY = secret_registry_tenant_key
SECRET_REGISTRY_TENANT_ID = secret_registry_tenant_id
# endregion
# region: Security and Privacy----------------------------------------------###

View file

@ -1,10 +1,11 @@
from itertools import zip_longest
import logging
import ipaddress
import requests
import re
from datetime import date
from typing import Optional
from django.conf import settings
from django_fsm import FSMField, transition, TransitionNotAllowed # type: ignore
from django.db import models
@ -307,6 +308,90 @@ class Domain(TimeStampedModel, DomainHelper):
To update the expiration date, use renew_domain method."""
raise NotImplementedError()
def create_dns_record(self, dns_record_dict):
print(f"what is the key? {settings.SECRET_REGISTRY_TENANT_KEY}")
# Cloudflare API endpoints
base_url = "https://api.cloudflare.com/client/v4"
headers = {
"Authorization": f"Bearer {settings.SECRET_REGISTRY_TENANT_KEY}",
"Content-Type": "application/json"
}
if settings.IS_PRODUCTION:
if self.name == "igorville.gov":
# do stuff
pass
else:
logger.warning(f"create_dns_record was called for domain {self.name}")
else:
pass
# TODO - check if these things exist before doing stuff
# 1. Get tenant details
# Note: we can grab this more generally but lets be specific to keep things safe.
tenant_id = settings.SECRET_REGISTRY_TENANT_ID
account_name = f"account-{self.name}"
# 2. Create account under tenant
account_response = requests.post(
f"{base_url}/accounts",
headers=headers,
json={
"name": account_name,
"type": "enterprise",
"unit": {"id": tenant_id}
}
)
account_response.raise_for_status()
account_response_json = account_response.json()
account_id = account_response_json["result"]["id"]
logger.info(f"Created account: {account_response_json}")
# 3. Create zone under account
zone_response = requests.post(
f"{base_url}/zones",
headers=headers,
json={
"name": self.name,
"account": {"id": account_id},
"type": "full"
}
)
zone_response.raise_for_status()
zone_response_json = zone_response.json()
zone_id = zone_response_json["result"]["id"]
logger.info(f"Created zone: {zone_id}")
# 4. Add zone subscription
subscription_response = requests.post(
f"{base_url}/zones/{zone_id}/subscription",
headers=headers,
json={
"rate_plan": {"id": "PARTNERS_ENT"},
"frequency": "annual"
}
)
subscription_response.raise_for_status()
subscription_response_json = subscription_response.json()
logger.info(f"Created subscription: {subscription_response_json}")
# 5. Create DNS record
dns_response = requests.post(
f"{base_url}/zones/{zone_id}/dns_records",
headers=headers,
json=dns_record_dict
)
dns_response.raise_for_status()
dns_response_json = dns_response.json()
logger.info(f"Created DNS record: {dns_response_json}")
return {
"tenant_id": tenant_id,
"account_id": account_id,
"zone_id": zone_id,
"dns_record_id": dns_response_json["result"]["id"]
}
def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR):
"""
Renew the domain to a length and unit of time relative to the current

View file

@ -455,6 +455,12 @@ class DomainDNSView(DomainBaseView):
template_name = "domain_dns.html"
def get_context_data(self, **kwargs):
"""Adds custom context."""
context = super().get_context_data(**kwargs)
context["dns_prototype_flag"] = flag_is_active_for_user(self.request.user, "dns_prototype_flag")
return context
class DomainNameserversView(DomainFormBaseView):
"""Domain nameserver editing view."""