roughed in key data form and template, etc

This commit is contained in:
David Kennedy 2023-10-03 11:16:31 -04:00
parent 9153f01759
commit f3fcc19de8
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
6 changed files with 262 additions and 25 deletions

View file

@ -8,4 +8,6 @@ from .domain import (
DomainDnssecForm,
DomainDsdataFormset,
DomainDsdataForm,
DomainKeydataFormset,
DomainKeydataForm,
)

View file

@ -0,0 +1,19 @@
# common.py
# Q: What are the options?
ALGORITHM_CHOICES = [
(1, "ERSA/MD5 [RSAMD5]"),
(2 , "Diffie-Hellman [DH]"),
(3 ,"DSA/SHA-1 [DSA]"),
(5 ,"RSA/SHA-1 [RSASHA1]"),
]
# Q: What are the options?
DIGEST_TYPE_CHOICES = [
(0, "Reserved"),
(1, "SHA-256"),
]
# Flag choices
FLAG_CHOICES = [
(0, "0"),
(256, "256"),
(257, "257"),
]

View file

@ -1,13 +1,13 @@
"""Forms for domain management."""
from django import forms
from django.core.validators import RegexValidator
from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator
from django.forms import formset_factory
from phonenumber_field.widgets import RegionalPhoneNumberWidget
from ..models import Contact, DomainInformation
from .common import ALGORITHM_CHOICES, DIGEST_TYPE_CHOICES, FLAG_CHOICES
class DomainAddUserForm(forms.Form):
@ -149,20 +149,6 @@ class DomainDnssecForm(forms.Form):
class DomainDsdataForm(forms.Form):
"""Form for adding or editing a security email to a domain."""
# Q: What are the options?
ALGORITHM_CHOICES = [
(1, "ERSA/MD5 [RSAMD5]"),
(2 , "Diffie-Hellman [DH]"),
(3 ,"DSA/SHA-1 [DSA]"),
(5 ,"RSA/SHA-1 [RSASHA1]"),
]
# Q: What are the options?
DIGEST_TYPE_CHOICES = [
(0, "Reserved"),
(1, "SHA-256"),
]
# TODO: ds key data
# has_ds_key_data = forms.TypedChoiceField(
# required=True,
@ -174,10 +160,8 @@ class DomainDsdataForm(forms.Form):
required=True,
label="Key tag",
validators=[
RegexValidator(
"^[0-9]{5}(?:-[0-9]{4})?$|^$",
message="Accepted range 0-65535.",
)
MinValueValidator(0, "Value must be between 0 and 65535"),
MaxValueValidator(65535, "Value must be between 0 and 65535"),
],
)
@ -230,7 +214,51 @@ DomainDsdataFormset = formset_factory(
)
# TODO:
# class DomainKeyDataForm(forms.Form):
class DomainKeydataForm(forms.Form):
"""Form for adding or editing DNSSEC key data."""
# TODO: ds key data
# has_ds_key_data = forms.TypedChoiceField(
# required=True,
# label="DS Data record type",
# choices=[(False, "DS Data"), (True, "DS Data with Key Data")],
# )
flag = forms.TypedChoiceField(
required=True,
label="Flag",
choices=FLAG_CHOICES,
)
protocol = forms.IntegerField(
max_value=3,
min_value=3,
initial=3,
required=True,
disabled=True,
)
algorithm = forms.TypedChoiceField(
required=True,
label="Algorithm",
choices=[(None, "--Select--")] + ALGORITHM_CHOICES,
)
# """"""
pub_key = forms.CharField(
required=True,
label="Pub key",
)
delete = forms.BooleanField(
required=False,
label="Delete",
)
# TODO: Conditional DS Key Data fields
DomainKeydataFormset = formset_factory(
DomainKeydataForm,
extra=1,
)

View file

@ -7,6 +7,16 @@
<h1>DS Data</h1>
{% if domain.dnssecdata is not None and domain.dnssecdata.keyData is not None %}
<div class="usa-alert usa-alert--warning usa-alert--slim margin-bottom-3">
<div class="usa-alert__body">
<h4 class="usa-alert__heading">Warning, you cannot add DS Data</h4>
<p class="usa-alert__text">
You cannot add DS Data because you have already added Key Data. Delete your Key Data records in order to add DS Data.
</p>
</div>
</div>
{% else %}
{% include "includes/required_fields.html" %}
<form class="usa-form usa-form--extra-large" method="post" novalidate id="form-container">
@ -67,5 +77,5 @@
>Save
</button>
</form>
{% endif %}
{% endblock %} {# domain_content #}

View file

@ -7,4 +7,75 @@
<h1>Key Data</h1>
{% if domain.dnssecdata is not None and domain.dnssecdata.dsData is not None %}
<div class="usa-alert usa-alert--warning usa-alert--slim margin-bottom-3">
<div class="usa-alert__body">
<h4 class="usa-alert__heading">Warning, you cannot add Key Data</h4>
<p class="usa-alert__text">
You cannot add Key Data because you have already added DS Data. Delete your DS Data records in order to add Key Data.
</p>
</div>
</div>
{% else %}
{% include "includes/required_fields.html" %}
<form class="usa-form usa-form--extra-large" method="post" novalidate id="form-container">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<fieldset class="key-record">
<legend>Key Data record {{forloop.counter}}</legend>
<div class="grid-row grid-gap-2">
<div class="tablet:grid-col-4">
{% with attr_required=True %}
{% input_with_errors form.flag %}
{% endwith %}
</div>
<div class="tablet:grid-col-4">
{% with attr_required=True %}
{% input_with_errors form.protocol %}
{% endwith %}
</div>
<div class="tablet:grid-col-4">
{% with attr_required=True %}
{% input_with_errors form.algorithm %}
{% endwith %}
</div>
</div>
<div class="grid-row">
<div class="grid-col">
{% with attr_required=True %}
{% input_with_errors form.pub_key %}
{% endwith %}
</div>
</div>
<div class="grid-row margin-top-2">
<div class="grid-col">
{% with add_group_class="float-right-tablet" %}
{% input_with_errors form.delete %}
{% endwith %}
</div>
</div>
</fieldset>
{% endfor %}
<button type="button" class="usa-button usa-button--unstyled display-block" id="add-form2">
<svg class="usa-icon" aria-hidden="true" focusable="false" role="img" width="24" height="24">
<use xlink:href="{%static 'img/sprite.svg'%}#add_circle"></use>
</svg><span class="margin-left-05">Add new record</span>
</button>
<button
type="submit"
class="usa-button"
>Save
</button>
</form>
{% endif %}
{% endblock %} {# domain_content #}

View file

@ -31,6 +31,8 @@ from ..forms import (
DomainDnssecForm,
DomainDsdataFormset,
DomainDsdataForm,
DomainKeydataFormset,
DomainKeydataForm,
)
from epplibwrapper import (
@ -371,11 +373,116 @@ class DomainDsdataView(DomainPermissionView, FormMixin):
class DomainKeydataView(DomainPermissionView):
class DomainKeydataView(DomainPermissionView, FormMixin):
"""Domain DNSSEC key data editing view."""
template_name = "domain_keydata.html"
form_class = DomainKeydataFormset
form = DomainKeydataForm
def get_initial(self):
"""The initial value for the form (which is a formset here)."""
domain = self.get_object()
dnssecdata: extensions.DNSSECExtension = domain.dnssecdata
initial_data = []
if dnssecdata is not None:
if dnssecdata.dsData is not None:
# TODO: Throw an error
pass
if dnssecdata.keyData is not None:
# Add existing keydata as initial data
initial_data.extend({"flag": record.flags, "protocol": record.protocol, "algorithm": record.alg, "pub_key": record.pubKey} for record in dnssecdata.keyData)
return initial_data
def get_success_url(self):
"""Redirect to the Key Data page for the domain."""
return reverse("domain-dns-dnssec-keydata", kwargs={"pk": self.object.pk})
def get_context_data(self, **kwargs):
"""Adjust context from FormMixin for formsets."""
context = super().get_context_data(**kwargs)
# use "formset" instead of "form" for the key
context["formset"] = context.pop("form")
return context
def post(self, request, *args, **kwargs):
"""Formset submission posts to this view."""
self.object = self.get_object()
formset = self.get_form()
if formset.is_valid():
return self.form_valid(formset)
else:
#
#
#
# testing delete
try:
for form in formset:
if 'delete' in form.cleaned_data:
logger.debug(f"delete: {form.cleaned_data['delete']}")
else:
logger.debug(f"delete key does not exist, harcoding false")
except KeyError:
logger.debug(f"KeyError: {KeyError}")
#
#
#
#
return self.form_invalid(formset)
def form_valid(self, formset):
"""The formset is valid, perform something with it."""
# Set the nameservers from the formset
dnssecdata = {"keyData":[]}
for form in formset:
try:
#
#
#
# untested
if 'delete' in form.cleaned_data:
if form.cleaned_data['delete'] == False:
pass
else:
# delete key exists and is true, delete this record
logger.debug(f"delete: {form.cleaned_data['delete']}")
else:
logger.debug(f"delete key does not exist, pass")
pass
#
#
#
#
keyrecord = {
"flags": form.cleaned_data["flag"],
"protocol": form.cleaned_data["protocol"],
"alg": form.cleaned_data["algorithm"],
"pubKey": form.cleaned_data["pub_key"],
}
dnssecdata["keyData"].append(common.DNSSECKeyData(**keyrecord))
except KeyError:
# no server information in this field, skip it
pass
domain = self.get_object()
domain.dnssecdata = dnssecdata
messages.success(
self.request, "The Key Data records for this domain have been updated."
)
# superclass has the redirect
return super().form_valid(formset)
class DomainYourContactInformationView(DomainPermissionView, FormMixin):