setup_wizard.views ================== .. py:module:: setup_wizard.views .. autoapi-nested-parse:: Views for the users application. Module Contents --------------- .. py:data:: logger .. py:exception:: TrustpointWizardError Bases: :py:obj:`Exception` Custom exception for Trustpoint wizard-related issues. .. py:exception:: TrustpointTlsServerCredentialError(message = 'Trustpoint TLS Server Credential error occurred.') Bases: :py:obj:`Exception` Custom exception for errors related to Trustpoint TLS Server Credentials. This exception is raised when specific issues with the TLS Server Credentials occur, such as missing credentials. .. py:function:: execute_shell_script(script, *args) Execute a shell script with optional arguments. :param script: The path to the shell script to execute. :type script: Path :param \*args: Additional arguments to pass to the script. :type \*args: str :raises FileNotFoundError: If the script does not exist. :raises ValueError: If the script path is not a valid file. :raises subprocess.CalledProcessError: If the script fails to execute. .. py:class:: StartupWizardRedirect Handles redirection logic based on the current state of the setup wizard. This class provides a static method for determining the appropriate redirection URL based on the wizard's state, ensuring users are guided through the setup process. .. py:method:: redirect_by_state(wizard_state) :staticmethod: Redirects the user to the appropriate setup wizard page based on the current state. :param wizard_state: The current state of the setup wizard. :type wizard_state: SetupWizardState :returns: A redirection response to the appropriate page. :rtype: HttpResponseRedirect :raises ValueError: If the wizard state is unrecognized or invalid. .. py:class:: HsmSetupMixin Bases: :py:obj:`trustpoint.logger.LoggerMixin` Mixin that provides common HSM setup functionality for both initial setup and auto restore. .. py:attribute:: request :type: django.http.HttpRequest .. py:method:: form_valid(form) Handle form submission for HSM setup. .. py:method:: get_setup_type() Return the setup type for the HSM script. .. py:method:: get_error_redirect_url() Return the URL to redirect to on error. .. py:method:: get_success_context() Return context string for success messages. .. py:method:: get_expected_wizard_state() Return the expected wizard state for this view. .. py:class:: SetupWizardCryptoStorageView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`management.forms.KeyStorageConfigForm`\ ] View for handling crypto storage setup during the setup wizard. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/crypto_storage_setup.html' .. py:attribute:: form_class .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: form_valid(form) Handle valid form submission and determine next step based on storage type. .. py:method:: form_invalid(form) Handle invalid form submission. .. py:class:: SetupWizardHsmSetupView Bases: :py:obj:`HsmSetupMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.HsmSetupForm`\ ] View for handling HSM setup during the setup wizard. .. py:attribute:: form_class .. py:attribute:: template_name :value: 'setup_wizard/hsm_setup.html' .. py:attribute:: http_method_names :value: ('get', 'post') .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: get_form(form_class = None) Return a form instance with appropriate defaults based on HSM type. .. py:method:: get_context_data(**kwargs) Add HSM type to template context. .. py:method:: get_setup_type() Return the setup type for the HSM script. .. py:method:: get_success_url() Return the success URL after HSM setup. .. py:method:: get_error_redirect_url() Return the URL to redirect to on error. .. py:method:: get_success_context() Return context string for success messages. .. py:method:: get_expected_wizard_state() Return the expected wizard state for this view. .. py:class:: SetupWizardSetupModeView(**kwargs) Bases: :py:obj:`django.views.generic.TemplateView` View for the initial step of the setup wizard. This view is responsible for displaying the initial setup wizard page. It ensures that the application is running in a Docker container and that the setup wizard is in the initial state. If either condition is not met, the user is redirected to the appropriate page, such as the login page or the next setup step. .. attribute:: http_method_names List of HTTP methods allowed for this view. :type: ClassVar[list[str]] .. attribute:: template_name Path to the template used for rendering the initial page. :type: str .. py:attribute:: http_method_names :value: ('get',) .. py:attribute:: template_name :value: 'setup_wizard/setup_mode.html' .. py:method:: get(*args, **kwargs) Handle GET requests for the setup mode wizard page. This method validates the current state of the setup wizard and redirects the user to the appropriate page. If the application is not running in a Docker container, the user is redirected to the login page. :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate setup wizard page or the login page if the setup is not in a Docker container. :rtype: HttpResponse .. py:class:: SetupWizardSelectTlsServerCredentialView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.EmptyForm`\ ] View for selecting the TLS server credential during setup. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/select_tls_server_credential.html' .. py:attribute:: form_class .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: get(*args, **kwargs) Handle GET requests for the TLS server credential selection page. .. py:method:: form_valid(_) Handle form submission for TLS server credential selection. .. py:class:: SetupWizardRestoreOptionsView(**kwargs) Bases: :py:obj:`django.views.generic.TemplateView` View for the restore option during initialization. .. attribute:: http_method_names List of HTTP methods allowed for this view. :type: ClassVar[list[str]] .. attribute:: template_name Path to the template used for rendering the initial page. :type: str .. py:attribute:: http_method_names :value: ('get',) .. py:attribute:: template_name :value: 'setup_wizard/restore_options.html' .. py:method:: get(*args, **kwargs) Handle GET requests for the initial setup wizard page. This method validates the current state of the setup wizard and redirects the user to the appropriate page. If the application is not running in a Docker container, the user is redirected to the login page. :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate setup wizard page or the login page if the setup is not in a Docker container. :rtype: HttpResponse .. py:class:: SetupWizardBackupPasswordView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.BackupPasswordForm`\ ] View for setting up backup password for PKCS#11 token during the setup wizard. This view allows users to set a backup password that can be used to recover the DEK (Data Encryption Key) in case the HSM becomes unavailable. The password is used to derive a BEK (Backup Encryption Key) using Argon2. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/backup_password.html' .. py:attribute:: success_url .. py:attribute:: form_class .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: get_context_data(**kwargs) Add password requirements to the context. .. py:method:: form_valid(form) Handle valid form submission. .. py:method:: form_invalid(form) Handle invalid form submission. .. py:class:: BackupPasswordRecoveryMixin Bases: :py:obj:`trustpoint.logger.LoggerMixin` Mixin that provides backup password recovery functionality. .. py:attribute:: request :type: django.http.HttpRequest .. py:method:: handle_backup_password_recovery(backup_password) Handle DEK recovery using backup password. This method handles two scenarios: 1. Standard recovery: KEK exists, use it to wrap the recovered DEK 2. New KEK scenario: No KEK or KEK doesn't match, generate new KEK first :param backup_password: The backup password provided by user :returns: True if recovery was successful, False otherwise :rtype: bool .. py:class:: AutoRestoreHsmSetupView Bases: :py:obj:`HsmSetupMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.HsmSetupForm`\ ] View for handling HSM setup during auto restore process. This view initializes the SoftHSM token when restoring to a new HSM installation where the old KEK is lost. It uses the wizard_setup_hsm.sh script with 'auto_restore_setup' mode. .. py:attribute:: form_class .. py:attribute:: template_name :value: 'setup_wizard/hsm_setup.html' .. py:attribute:: http_method_names :value: ('get', 'post') .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: get_form(form_class = None) Return a form instance with appropriate defaults based on HSM type. .. py:method:: get_context_data(**kwargs) Add HSM type to template context. .. py:method:: get_setup_type() Return the setup type for the HSM script (auto_restore_setup). .. py:method:: get_success_url() Return the success URL after HSM setup. .. py:method:: get_error_redirect_url() Return the URL to redirect to on error. .. py:method:: get_success_context() Return context string for success messages. .. py:method:: get_expected_wizard_state() Return the expected wizard state for this view. .. py:class:: BackupRestoreView(**kwargs) Bases: :py:obj:`BackupPasswordRecoveryMixin`, :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.View` Upload a dump file and restore the database from it with optional backup password. .. py:method:: post(request) Handle POST requests to upload a backup file and restore the database. .. py:class:: BackupAutoRestorePasswordView Bases: :py:obj:`BackupPasswordRecoveryMixin`, :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.PasswordAutoRestoreForm`\ ] View for handling backup password entry during auto restore process. This view allows users to enter the backup password needed to recover the DEK (Data Encryption Key) during the auto restore process. It validates the current wizard state and processes the password recovery. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/auto_restore_password.html' .. py:attribute:: success_url .. py:attribute:: form_class .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: get_context_data(**kwargs) Add additional context data. .. py:method:: form_valid(form) Handle valid form submission. .. py:method:: form_invalid(form) Handle invalid form submission. .. py:class:: SetupWizardGenerateTlsServerCredentialView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.StartupWizardTlsCertificateForm`\ ] View for generating TLS Server Credentials in the setup wizard. This view handles the generation of TLS Server Credentials as part of the setup wizard. It provides a form for the user to input necessary information such as IP addresses and domain names, and processes the data to generate the required TLS certificates. .. attribute:: http_method_names HTTP methods allowed for this view. :type: ClassVar[list[str]] .. attribute:: template_name Path to the template used for rendering the form. :type: str .. attribute:: form_class The form class used to validate user input. :type: Form .. attribute:: success_url The URL to redirect to upon successful credential generation. :type: str .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/generate_tls_server_credential.html' .. py:attribute:: form_class .. py:attribute:: success_url .. py:method:: dispatch(request, *args, **kwargs) Override the dispatch method to enforce wizard state validation. This method ensures that the user is redirected appropriately based on the current wizard state. If the application is not running in a Docker container, the user is redirected to the login page. :param request: The incoming HTTP request. :type request: HttpRequest :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate page or the next handler in the dispatch chain. :rtype: HttpResponse .. py:method:: form_valid(form) Handle a valid form submission for TLS Server Credential generation. :param form: The validated form containing user input for generating the TLS Server Credential. :returns: Redirect to the success URL upon successful credential generation, or an error page if an exception occurs. :rtype: HttpResponseRedirect :raises TrustpointTlsServerCredentialError: If no TLS server credential is found. :raises subprocess.CalledProcessError: If the associated shell script fails. .. py:class:: SetupWizardImportTlsServerCredentialMethodSelectView(**kwargs) Bases: :py:obj:`django.views.generic.TemplateView` View for selecting the import method for TLS Server Credentials. .. py:attribute:: http_method_names :value: ('get',) .. py:attribute:: template_name :value: 'setup_wizard/import_method_select.html' .. py:method:: dispatch(request, *args, **kwargs) Override the dispatch method to enforce wizard state validation. :param request: The incoming HTTP request. :type request: HttpRequest :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate page or the next handler in the dispatch chain. :rtype: HttpResponse .. py:class:: SetupWizardImportTlsServerCredentialPkcs12View Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`management.forms.TlsAddFileImportPkcs12Form`\ ] View for importing TLS Server Credentials using a PKCS#12 file in the setup wizard. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/import_tls_server_credential.html' .. py:attribute:: form_class .. py:attribute:: success_url .. py:method:: dispatch(request, *args, **kwargs) Override the dispatch method to enforce wizard state validation. :param request: The incoming HTTP request. :type request: HttpRequest :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate page or the next handler in the dispatch chain. :rtype: HttpResponse .. py:method:: form_valid(form) Handle a valid form submission for TLS Server Credential import. :param form: The validated form containing the uploaded PKCS#12 file. :returns: Redirect to the success URL upon successful credential import, or an error page if an exception occurs. :rtype: HttpResponseRedirect .. py:class:: SetupWizardImportTlsServerCredentialSeparateFilesView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`management.forms.TlsAddFileImportSeparateFilesForm`\ ] View for importing TLS Server Credentials using separate files in the setup wizard. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: template_name :value: 'setup_wizard/import_tls_server_credential.html' .. py:attribute:: form_class .. py:attribute:: success_url .. py:method:: dispatch(request, *args, **kwargs) Override the dispatch method to enforce wizard state validation. :param request: The incoming HTTP request. :type request: HttpRequest :param \*args: Additional positional arguments. :type \*args: Any :param \*\*kwargs: Additional keyword arguments. :type \*\*kwargs: Any :returns: A redirect response to the appropriate page or the next handler in the dispatch chain. :rtype: HttpResponse .. py:method:: form_valid(form) Handle a valid form submission for TLS Server Credential import. :param form: The validated form containing the uploaded certificate files. :returns: Redirect to the success URL upon successful credential import, or an error page if an exception occurs. :rtype: HttpResponseRedirect .. py:class:: SetupWizardTlsServerCredentialApplyView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.EmptyForm`\ ] View for handling the application of TLS Server Credentials in the setup wizard. .. attribute:: http_method_names Allowed HTTP methods for this view ('get' and 'post'). :type: list[str] .. attribute:: form_class The form used for processing TLS Server Credential application. :type: Form .. attribute:: template_name The template used to render the view. :type: str .. attribute:: success_url The URL to redirect to upon successful form submission. :type: str .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: form_class .. py:attribute:: template_name :value: 'setup_wizard/tls_server_credential_apply.html' .. py:method:: get_success_url() Return the success URL based on storage type. .. py:method:: get(request, *args, **kwargs) Handle GET requests for the TLS Server Credential application view. :param request: The HTTP request object. :type request: HttpRequest :param \*args: Positional arguments passed to the method. :type \*args: Any :param \*\*kwargs: Keyword arguments passed to the method. :type \*\*kwargs: Any :returns: A redirect response to the appropriate wizard state or the requested page. :rtype: HttpResponse .. py:method:: post(*args, **kwargs) Handle POST requests for the TLS Server Credential application view. :param \*args: Positional arguments passed to the method. :type \*args: Any :param \*\*kwargs: Keyword arguments passed to the method. :type \*\*kwargs: Any :returns: A redirect response to the appropriate page based on the wizard state. :rtype: HttpResponse .. py:method:: form_valid(form) Process a valid form submission during the TLS Server Credential application. :param form: The form instance containing the submitted data. :returns: Redirect to the next step or an error page based on the outcome. :rtype: HttpResponseRedirect .. py:class:: SetupWizardTlsServerCredentialApplyCancelView(**kwargs) Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.View` View for handling the cancellation of TLS Server Credential application. .. attribute:: http_method_names Allowed HTTP methods for this view. .. py:attribute:: http_method_names :value: ('get',) .. py:method:: get(request) Handle GET requests for the TLS Server Credential import view. :param request: The HTTP request object. :returns: A redirect to the next step or an error response. :rtype: HttpResponse .. py:class:: SetupWizardDemoDataView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`setup_wizard.forms.EmptyForm`\ ] View for handling the demo data setup during the setup wizard. This view allows the user to either add demo data to the database or proceed without it. It validates the current wizard state and transitions to the next state upon successful completion. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: form_class .. py:attribute:: template_name :value: 'setup_wizard/demo_data.html' .. py:attribute:: success_url .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: form_valid(form) Handle form submission for demo data setup. .. py:class:: SetupWizardCreateSuperUserView Bases: :py:obj:`trustpoint.logger.LoggerMixin`, :py:obj:`django.views.generic.FormView`\ [\ :py:obj:`django.contrib.auth.forms.UserCreationForm`\ [\ :py:obj:`django.contrib.auth.models.User`\ ]\ ] View for handling the creation of a superuser during the setup wizard. This view is part of the setup wizard process. It allows an admin to create a superuser account, ensuring that the application has at least one administrative user configured. The view validates the input using the `UserCreationForm` and transitions the wizard state upon successful completion. .. py:attribute:: http_method_names :value: ('get', 'post') .. py:attribute:: form_class :type: type[django.contrib.auth.forms.UserCreationForm[django.contrib.auth.models.User]] .. py:attribute:: template_name :value: 'setup_wizard/create_super_user.html' .. py:attribute:: success_url .. py:method:: dispatch(request, *args, **kwargs) Handle request dispatch and wizard state validation. .. py:method:: form_valid(form) Handle form submission for creating a superuser. :param form: The form containing the data for the superuser creation. :returns: Redirect to the next step or login page. :rtype: HttpResponseRedirect