Source code for setup_wizard.forms
"""This module contains forms for the setup wizard app."""
from __future__ import annotations
import ipaddress
from typing import TYPE_CHECKING
from django import forms
from django.utils.translation import gettext_lazy as _
if TYPE_CHECKING:
from typing import Any
[docs]
class StartupWizardTlsCertificateForm(forms.Form):
"""The Setup Wizard TLS Certificate Form."""
[docs]
ipv4_addresses = forms.CharField(
label=_('IPv4-Addresses (comma-separated list)'), initial='127.0.0.1, ', required=False
)
[docs]
ipv6_addresses = forms.CharField(label=_('IPv6-Addresses (comma-separated list)'), initial='::1, ', required=False)
[docs]
domain_names = forms.CharField(
label=_('Domain-Names (comma-separated list)'), initial='localhost, ', required=False
)
[docs]
def clean_ipv4_addresses(self) -> list[ipaddress.IPv4Address]:
"""Splits the IPv4 addresses and returns them as a list of strings.
Returns:
A list of the IPv4 addresses or an empty list.
Raises:
ValidationError: If it contains a term that is not a valid IPv4 address.
"""
data = self.cleaned_data['ipv4_addresses'].strip()
if not data:
return []
addresses = data.split(',')
try:
return [ipaddress.IPv4Address(address.strip()) for address in addresses if address.strip() != '']
except ipaddress.AddressValueError as exception:
err_msg = 'Contains an invalid IPv4-Address.'
raise forms.ValidationError(err_msg) from exception
[docs]
def clean_ipv6_addresses(self) -> list[ipaddress.IPv6Address]:
"""Splits the IPv6 addresses and returns them as a list of strings.
Returns:
A list of the IPv6 addresses or an empty list.
Raises:
ValidationError: If it contains a term that is not a valid IPv6 address.
"""
data = self.cleaned_data['ipv6_addresses'].strip()
if not data:
return []
addresses = data.split(',')
try:
return [ipaddress.IPv6Address(address.strip()) for address in addresses if address.strip() != '']
except ipaddress.AddressValueError as exception:
err_msg = 'Contains an invalid IPv6-Address.'
raise forms.ValidationError(err_msg) from exception
[docs]
def clean_domain_names(self) -> list[str]:
"""Splits the domain names and returns them as a list of strings.
Returns:
A list of the domain names or an empty list.
"""
data = self.cleaned_data['domain_names'].strip()
if not data:
return []
domain_names = data.split(',')
# TODO(AlexHx8472): Check for valid domains. # noqa: FIX002
return [domain_name.strip() for domain_name in domain_names if domain_name.strip() != '']
[docs]
def clean(self) -> dict[str, Any]:
"""Checks that at least one SAN entry is set.
Returns:
The cleaned data.
Raises:
ValidationError: If no SAN entry is set.
"""
cleaned_data = super().clean()
if cleaned_data is None:
err_msg = (
'Unexpected error occurred. Failed to get the cleaned_data '
'of the StartupWizardTlsCertificateForm instance.'
)
raise forms.ValidationError(err_msg)
ipv4_addresses = cleaned_data.get('ipv4_addresses')
ipv6_addresses = cleaned_data.get('ipv6_addresses')
domain_names = cleaned_data.get('domain_names')
if not (ipv4_addresses or ipv6_addresses or domain_names):
err_msg = 'At least one SAN entry is required.'
raise forms.ValidationError(err_msg)
return cleaned_data