From 366ecb97d94e93bd2af53e82bbe5cd2ca50b1581 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:07:15 -0700 Subject: [PATCH] basic logic --- src/registrar/config/settings.py | 6 +++ src/registrar/models/domain.py | 87 +++++++++++++++++++++++++++++++- src/registrar/views/domain.py | 6 +++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/registrar/config/settings.py b/src/registrar/config/settings.py index a18a813f1..bcf4d79d6 100644 --- a/src/registrar/config/settings.py +++ b/src/registrar/config/settings.py @@ -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----------------------------------------------### diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 7fdc56971..2718a225e 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -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 diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 9bf6f5313..b65cd93be 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -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."""