2026-03-14 11:54:49 -03:00
import os
from django.conf import settings
2026-03-12 14:15:14 -03:00
from django.contrib import messages
from django.contrib.auth.decorators import login_required
2026-11-18 17:33:44 -03:00
from django.db.models import F , ProtectedError
2026-11-18 17:46:49 -03:00
from django.http import JsonResponse
2026-03-12 14:15:14 -03:00
from django.shortcuts import render , get_object_or_404 , redirect
from django.urls import reverse
2026-11-18 17:46:49 -03:00
from django.utils.formats import date_format
from django.utils.timezone import localtime
2026-03-12 14:15:14 -03:00
from django.utils.translation import gettext as _
2026-03-14 11:54:49 -03:00
from django.views.decorators.http import require_POST
2026-03-12 14:15:14 -03:00
2026-03-14 11:54:49 -03:00
from app_gateway.caddy_config_export import export_caddy_config
2026-11-18 17:46:49 -03:00
from app_gateway.certificate_status import inspect_certificate
2026-03-12 14:15:14 -03:00
from app_gateway.forms import (
ApplicationForm , ApplicationHostForm , AccessPolicyForm ,
2026-11-17 15:59:40 -03:00
ApplicationPolicyForm , ApplicationRouteForm , DnsChallengeCredentialForm
2026-03-12 14:15:14 -03:00
)
from app_gateway.models import (
2026-11-17 15:59:40 -03:00
Application , ApplicationHost , AccessPolicy , ApplicationPolicy , ApplicationRoute , DnsChallengeCredential ,
RESERVED_APP_NAME
2026-03-12 14:15:14 -03:00
)
2026-03-16 17:08:50 -03:00
from app_gateway.setup_defaults import create_default_entries
2026-03-12 14:15:14 -03:00
from user_manager.models import UserAcl
@login_required
def view_app_gateway_list ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 20 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
2026-03-16 17:08:50 -03:00
create_default_entries ()
2026-03-12 14:15:14 -03:00
applications = Application . objects . all () . order_by ( 'name' )
hosts = ApplicationHost . objects . all () . order_by ( 'hostname' )
access_policies = AccessPolicy . objects . all () . order_by ( 'name' )
app_policies = ApplicationPolicy . objects . all () . order_by ( 'application__name' )
2026-11-17 15:59:40 -03:00
dns_credentials = DnsChallengeCredential . objects . all () . order_by ( 'name' )
2026-03-12 14:15:14 -03:00
tab = request . GET . get ( 'tab' , 'applications' )
context = {
'applications' : applications ,
'hosts' : hosts ,
'access_policies' : access_policies ,
'app_policies' : app_policies ,
2026-11-17 15:59:40 -03:00
'dns_credentials' : dns_credentials ,
2026-03-12 14:15:14 -03:00
'active_tab' : tab ,
2026-03-17 15:43:21 -03:00
'caddy_enabled' : settings . CADDY_ENABLED ,
2026-03-12 14:15:14 -03:00
}
return render ( request , 'app_gateway/app_gateway_list.html' , context )
2026-03-12 18:41:21 -03:00
@login_required
def view_application_details ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 20 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_uuid = request . GET . get ( 'uuid' )
application = get_object_or_404 ( Application , uuid = application_uuid )
hosts = application . hosts . all () . order_by ( 'hostname' )
routes = application . routes . all () . order_by ( 'order' , 'path_prefix' )
2026-03-16 17:29:39 -03:00
is_reserved = application . name == RESERVED_APP_NAME
2026-03-12 18:41:21 -03:00
context = {
'application' : application ,
'hosts' : hosts ,
'routes' : routes ,
2026-03-16 17:29:39 -03:00
'is_reserved' : is_reserved ,
2026-11-18 17:46:49 -03:00
'caddy_enabled' : settings . CADDY_ENABLED ,
2026-03-12 18:41:21 -03:00
'page_title' : _ ( 'Application Details' ),
}
return render ( request , 'app_gateway/application_details.html' , context )
2026-11-18 17:46:49 -03:00
@login_required
def view_application_certificate_status ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 20 ) . exists ():
return JsonResponse ({ 'error' : _ ( 'Access Denied' )}, status = 403 )
application = get_object_or_404 ( Application , uuid = request . GET . get ( 'uuid' ))
if not settings . CADDY_ENABLED :
return JsonResponse ({ 'caddy_enabled' : False , 'hosts' : {}})
hosts_status = {}
for application_host in application . hosts . all ():
certificate = inspect_certificate ( application_host . hostname )
expiration = ''
if certificate [ 'not_after' ]:
expiration = _ ( 'expires %(date)s ' ) % {
'date' : date_format ( localtime ( certificate [ 'not_after' ]), 'SHORT_DATETIME_FORMAT' )
}
if certificate [ 'status' ] == 'trusted' :
css_class , label = 'success' , _ ( 'Trusted' )
detail = f " { certificate [ 'issuer' ] } , { expiration } "
elif certificate [ 'status' ] == 'untrusted' and application_host . certificate_type == 'self_signed' :
css_class , label = 'secondary' , _ ( 'Self-signed' )
detail = expiration
elif certificate [ 'status' ] == 'untrusted' :
css_class , label = 'warning' , _ ( 'Using fallback' )
detail = _ ( 'The trusted certificate could not be issued yet, %(expiration)s ' ) % { 'expiration' : expiration }
else :
css_class , label = 'danger' , _ ( 'Unavailable' )
detail = _ ( 'No certificate is being served for this hostname.' )
hosts_status [ str ( application_host . uuid )] = { 'css_class' : css_class , 'label' : label , 'detail' : detail }
return JsonResponse ({ 'caddy_enabled' : True , 'hosts' : hosts_status })
2026-03-12 14:15:14 -03:00
@login_required
def view_manage_application ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_uuid = request . GET . get ( 'uuid' )
if application_uuid :
application = get_object_or_404 ( Application , uuid = application_uuid )
2026-03-16 17:24:20 -03:00
if application . name == RESERVED_APP_NAME :
messages . error ( request , _ ( 'The WireGuard WebAdmin application cannot be modified.' ))
return redirect ( reverse ( 'view_application' ) + f '?uuid= { application . uuid } ' )
2026-03-12 14:15:14 -03:00
title = _ ( 'Edit Application' )
else :
application = None
title = _ ( 'Create Application' )
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=applications'
form = ApplicationForm ( request . POST or None , instance = application , cancel_url = cancel_url )
if form . is_valid ():
form . save ()
messages . success ( request , _ ( 'Application saved successfully.' ))
return redirect ( cancel_url )
2026-03-13 20:12:29 -03:00
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
<h5>Application</h5>
<p>Define the main details of the application you want to expose through the gateway.</p>
<ul>
<li><strong>Name</strong>: A unique internal identifier for this application (e.g., "wiki", "crm"). Contains only letters, numbers, hyphens, or underscores.</li>
<li><strong>Display Name</strong>: A friendly, human-readable name for display purposes.</li>
<li><strong>Upstream</strong>: The destination URL where requests will be forwarded (e.g., <code>http://10.188.18.27:3000</code>). Must start with <code>http://</code> or <code>https://</code>.</li>
</ul>
''' )
}
2026-03-12 14:15:14 -03:00
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
2026-03-13 20:12:29 -03:00
'form_description' : form_description ,
2026-03-12 14:15:14 -03:00
}
return render ( request , 'generic_form.html' , context )
@login_required
def view_delete_application ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application = get_object_or_404 ( Application , uuid = request . GET . get ( 'uuid' ))
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=applications'
2026-03-16 17:24:20 -03:00
if application . name == RESERVED_APP_NAME :
messages . error ( request , _ ( 'The WireGuard WebAdmin application cannot be deleted.' ))
return redirect ( reverse ( 'view_application' ) + f '?uuid= { application . uuid } ' )
2026-03-12 14:15:14 -03:00
if request . method == 'POST' :
application . delete ()
messages . success ( request , _ ( 'Application deleted successfully.' ))
return redirect ( cancel_url )
context = {
'application' : application ,
'title' : _ ( 'Delete Application' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to delete the application " %(name)s "?' ) % { 'name' : application . display_name }
}
return render ( request , 'generic_delete_confirmation.html' , context )
@login_required
def view_manage_application_host ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_host_uuid = request . GET . get ( 'uuid' )
2026-03-12 18:41:21 -03:00
application_uuid = request . GET . get ( 'application_uuid' )
2026-03-12 14:15:14 -03:00
if application_host_uuid :
application_host = get_object_or_404 ( ApplicationHost , uuid = application_host_uuid )
2026-03-12 18:41:21 -03:00
application = application_host . application
2026-03-12 14:15:14 -03:00
title = _ ( 'Edit Application Host' )
else :
application_host = None
2026-03-12 18:41:21 -03:00
application = get_object_or_404 ( Application , uuid = application_uuid )
2026-03-12 14:15:14 -03:00
title = _ ( 'Add Application Host' )
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } #hosts'
2026-03-12 14:15:14 -03:00
2026-11-17 15:59:40 -03:00
is_reserved = application . name == RESERVED_APP_NAME
if is_reserved and not application_host :
2026-03-16 17:24:20 -03:00
messages . error ( request , _ ( 'The WireGuard WebAdmin application cannot be modified.' ))
return redirect ( cancel_url )
2026-11-17 15:59:40 -03:00
form = ApplicationHostForm (
request . POST or None , instance = application_host , cancel_url = cancel_url , is_reserved = is_reserved
)
2026-03-12 14:15:14 -03:00
if form . is_valid ():
2026-03-12 18:41:21 -03:00
host = form . save ( commit = False )
host . application = application
host . save ()
2026-03-12 14:15:14 -03:00
messages . success ( request , _ ( 'Application Host saved successfully.' ))
return redirect ( cancel_url )
2026-11-17 15:59:40 -03:00
form_description_content = _ ( '''
<h5>Certificate</h5>
<p>Defines how the TLS certificate for this hostname is issued.</p>
<ul>
2026-11-18 17:33:44 -03:00
<li><strong>Trusted certificate (HTTP validation)</strong>: Issued through ACME (e.g. Let's Encrypt) and validated over HTTP. The hostname must point to this server and ports 80/443 must be reachable from the internet.</li>
<li><strong>Trusted certificate (DNS validation)</strong>: Issued through ACME and validated with a DNS record created using the selected DNS credential. The server does not need to be reachable from the internet.</li>
<li><strong>Self-signed certificate</strong>: Issued by the internal Caddy CA. Browsers will show a security warning.</li>
2026-11-17 15:59:40 -03:00
</ul>
2026-11-18 17:33:44 -03:00
<p>When the fallback option is enabled and a trusted certificate cannot be issued, a self-signed certificate is used until the next successful attempt. Without the fallback, the connection fails until the certificate is issued.</p>
<p>Use <strong>Retry certificate</strong> in the host list to request a new attempt right away, for example after fixing the DNS or firewall.</p>
2026-11-17 15:59:40 -03:00
''' )
if is_reserved :
form_description_content += _ ( '''
<div class="alert alert-warning">
This hostname is used to access WireGuard WebAdmin. If the certificate cannot be issued,
your browser will show a security warning until the problem is fixed.
</div>
''' )
2026-03-12 14:15:14 -03:00
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
2026-11-17 15:59:40 -03:00
'form_description' : { 'size' : 'col-lg-6' , 'content' : form_description_content },
2026-03-12 14:15:14 -03:00
}
return render ( request , 'generic_form.html' , context )
@login_required
def view_delete_application_host ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_host = get_object_or_404 ( ApplicationHost , uuid = request . GET . get ( 'uuid' ))
2026-03-12 18:41:21 -03:00
application = application_host . application
2026-03-12 14:15:14 -03:00
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } #hosts'
2026-03-12 14:15:14 -03:00
2026-03-16 17:24:20 -03:00
if application . name == RESERVED_APP_NAME :
messages . error ( request , _ ( 'The WireGuard WebAdmin application cannot be modified.' ))
return redirect ( cancel_url )
2026-03-12 14:15:14 -03:00
if request . method == 'POST' :
application_host . delete ()
messages . success ( request , _ ( 'Application Host deleted successfully.' ))
return redirect ( cancel_url )
context = {
'application_host' : application_host ,
'title' : _ ( 'Delete Application Host' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to delete the host " %(hostname)s "?' ) % { 'hostname' : application_host . hostname }
}
return render ( request , 'generic_delete_confirmation.html' , context )
2026-03-13 20:12:29 -03:00
@login_required
def view_select_policy_type ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
context = {
'page_title' : _ ( 'Select Access Policy Type' ),
}
return render ( request , 'app_gateway/access_policy_type_select.html' , context )
2026-03-12 14:15:14 -03:00
@login_required
def view_manage_access_policy ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
access_policy_uuid = request . GET . get ( 'uuid' )
2026-03-13 20:12:29 -03:00
policy_type = request . GET . get ( 'policy_type' )
2026-03-12 14:15:14 -03:00
if access_policy_uuid :
access_policy = get_object_or_404 ( AccessPolicy , uuid = access_policy_uuid )
title = _ ( 'Edit Access Policy' )
2026-03-13 20:12:29 -03:00
policy_type = access_policy . policy_type
2026-03-12 14:15:14 -03:00
else :
access_policy = None
title = _ ( 'Create Access Policy' )
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=policies'
2026-03-13 20:12:29 -03:00
form = AccessPolicyForm ( request . POST or None , instance = access_policy , cancel_url = cancel_url , policy_type = policy_type )
2026-03-12 14:15:14 -03:00
if form . is_valid ():
form . save ()
messages . success ( request , _ ( 'Access Policy saved successfully.' ))
return redirect ( cancel_url )
2026-03-13 20:12:29 -03:00
if policy_type == 'public' :
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
<h5>Public Policy</h5>
<p>A Public policy allows access to the application without requiring any authentication.</p>
''' )
}
elif policy_type == 'deny' :
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
<h5>Deny Policy</h5>
<p>A Deny policy blocks all access to the matched routes.</p>
''' )
}
else :
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
<h5>Protected Policy</h5>
<p>A Protected policy requires users to authenticate before accessing the application.</p>
<ul>
<li><strong>Allowed Groups</strong>: Limits access to specific user groups. Note: Using groups requires selecting an Authentication Method of type "Local Password".</li>
<li><strong>Authentication Methods</strong>: Specify which methods users can use to authenticate (e.g., Local Password, TOTP, OIDC).</li>
</ul>
''' )
}
2026-03-12 14:15:14 -03:00
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
2026-03-13 20:12:29 -03:00
'form_description' : form_description ,
2026-03-12 14:15:14 -03:00
}
2026-03-13 20:12:29 -03:00
return render ( request , 'generic_form.html' , context )
2026-03-12 14:15:14 -03:00
@login_required
def view_delete_access_policy ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
access_policy = get_object_or_404 ( AccessPolicy , uuid = request . GET . get ( 'uuid' ))
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=policies'
if request . method == 'POST' :
2026-03-13 20:12:29 -03:00
try :
access_policy . delete ()
messages . success ( request , _ ( 'Access Policy deleted successfully.' ))
except ProtectedError :
messages . error ( request , _ ( 'Cannot delete this Access Policy because it is currently in use by an Application Route or Application Default Policy.' ))
2026-03-12 14:15:14 -03:00
return redirect ( cancel_url )
context = {
'access_policy' : access_policy ,
'title' : _ ( 'Delete Access Policy' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to delete the access policy " %(name)s "?' ) % { 'name' : access_policy . name }
}
return render ( request , 'generic_delete_confirmation.html' , context )
@login_required
def view_manage_application_policy ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_policy_uuid = request . GET . get ( 'uuid' )
2026-03-12 18:41:21 -03:00
application_uuid = request . GET . get ( 'application_uuid' )
2026-03-12 14:15:14 -03:00
if application_policy_uuid :
application_policy = get_object_or_404 ( ApplicationPolicy , uuid = application_policy_uuid )
2026-03-12 18:41:21 -03:00
application = application_policy . application
2026-03-12 14:15:14 -03:00
title = _ ( 'Edit Application Default Policy' )
else :
application_policy = None
2026-03-12 18:41:21 -03:00
application = get_object_or_404 ( Application , uuid = application_uuid )
2026-03-12 14:15:14 -03:00
title = _ ( 'Set Application Default Policy' )
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } '
2026-03-12 14:15:14 -03:00
form = ApplicationPolicyForm ( request . POST or None , instance = application_policy , cancel_url = cancel_url )
if form . is_valid ():
2026-03-12 18:41:21 -03:00
policy_config = form . save ( commit = False )
policy_config . application = application
policy_config . save ()
2026-03-12 14:15:14 -03:00
messages . success ( request , _ ( 'Application Default Policy saved successfully.' ))
return redirect ( cancel_url )
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
}
return render ( request , 'generic_form.html' , context )
@login_required
def view_delete_application_policy ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_policy = get_object_or_404 ( ApplicationPolicy , uuid = request . GET . get ( 'uuid' ))
2026-03-12 18:41:21 -03:00
application = application_policy . application
2026-03-12 14:15:14 -03:00
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } '
2026-03-12 14:15:14 -03:00
if request . method == 'POST' :
application_policy . delete ()
messages . success ( request , _ ( 'Application Default Policy deleted successfully.' ))
return redirect ( cancel_url )
context = {
'application_policy' : application_policy ,
'title' : _ ( 'Delete Application Default Policy' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to remove the default policy for " %(name)s "?' ) % {
'name' : application_policy . application . display_name
}
}
return render ( request , 'generic_delete_confirmation.html' , context )
@login_required
def view_manage_application_route ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_route_uuid = request . GET . get ( 'uuid' )
2026-03-12 18:41:21 -03:00
application_uuid = request . GET . get ( 'application_uuid' )
2026-03-12 14:15:14 -03:00
if application_route_uuid :
application_route = get_object_or_404 ( ApplicationRoute , uuid = application_route_uuid )
2026-03-12 18:41:21 -03:00
application = application_route . application
2026-03-12 14:15:14 -03:00
title = _ ( 'Edit Application Route' )
else :
application_route = None
2026-03-12 18:41:21 -03:00
application = get_object_or_404 ( Application , uuid = application_uuid )
2026-03-12 14:15:14 -03:00
title = _ ( 'Add Application Route' )
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } #routes'
2026-03-12 14:15:14 -03:00
form = ApplicationRouteForm ( request . POST or None , instance = application_route , cancel_url = cancel_url )
if form . is_valid ():
2026-03-12 18:41:21 -03:00
route = form . save ( commit = False )
route . application = application
route . save ()
2026-03-12 14:15:14 -03:00
messages . success ( request , _ ( 'Application Route saved successfully.' ))
return redirect ( cancel_url )
2026-03-12 18:41:21 -03:00
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
<h5>Application Route</h5>
<p>A Route defines a path prefix within this Application that requires a specific Access Policy.</p>
<ul>
<li><strong>Route Name</strong>: An internal identifier for this route (e.g., "public_api", "admin_area"). Used for reference and exports.</li>
<li><strong>Path Prefix</strong>: The URL path that triggers this route (e.g., <code>/api/</code> or <code>/admin/</code>). Use <code>/</code> to match all remaining paths.</li>
<li><strong>Policy</strong>: The Access Policy that will be enforced when a user accesses this path.</li>
<li><strong>Order</strong>: Determines the priority of this route when evaluating the request. Lower numbers are evaluated first. If multiple routes match a path, the one with the lowest order wins.</li>
</ul>
''' )
}
2026-03-12 14:15:14 -03:00
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
2026-03-12 18:41:21 -03:00
'form_description' : form_description ,
2026-03-12 14:15:14 -03:00
}
return render ( request , 'generic_form.html' , context )
@login_required
def view_delete_application_route ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_route = get_object_or_404 ( ApplicationRoute , uuid = request . GET . get ( 'uuid' ))
2026-03-12 18:41:21 -03:00
application = application_route . application
2026-03-12 14:15:14 -03:00
2026-03-12 18:41:21 -03:00
cancel_url = reverse ( 'view_application' ) + f '?uuid= { application . uuid } #routes'
2026-03-12 14:15:14 -03:00
if request . method == 'POST' :
application_route . delete ()
messages . success ( request , _ ( 'Application Route deleted successfully.' ))
return redirect ( cancel_url )
context = {
'application_route' : application_route ,
'title' : _ ( 'Delete Application Route' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to delete the route " %(name)s " ( %(path)s )?' ) % {
'name' : application_route . name ,
'path' : application_route . path_prefix
}
}
return render ( request , 'generic_delete_confirmation.html' , context )
2026-03-14 11:54:49 -03:00
2026-11-18 17:33:44 -03:00
@login_required
@require_POST
def view_retry_application_host_certificate ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
application_host = get_object_or_404 ( ApplicationHost , uuid = request . GET . get ( 'uuid' ))
redirect_url = reverse ( 'view_application' ) + f '?uuid= { application_host . application . uuid } #hosts'
if application_host . certificate_type == 'self_signed' :
messages . error ( request , _ ( 'This host uses a self-signed certificate.' ))
return redirect ( redirect_url )
if not settings . CADDY_ENABLED :
messages . error ( request , _ ( 'This action is not available because Caddy is not enabled.' ))
return redirect ( redirect_url )
ApplicationHost . objects . filter ( pk = application_host . pk ) . update ( certificate_generation = F ( 'certificate_generation' ) + 1 )
export_caddy_config ( '/caddy_json_export/' )
if settings . DEBUG :
export_caddy_config ( os . path . join ( settings . BASE_DIR , 'containers' , 'caddy' , 'config_files' ))
messages . success ( request , _ (
'A new certificate will be requested for %(hostname)s . The current configuration was exported.'
) % { 'hostname' : application_host . hostname })
return redirect ( redirect_url )
2026-11-17 15:59:40 -03:00
@login_required
def view_manage_dns_credential ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
dns_credential_uuid = request . GET . get ( 'uuid' )
if dns_credential_uuid :
dns_credential = get_object_or_404 ( DnsChallengeCredential , uuid = dns_credential_uuid )
title = _ ( 'Edit DNS Credential' )
else :
dns_credential = None
title = _ ( 'Add DNS Credential' )
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=dns_credentials'
form = DnsChallengeCredentialForm ( request . POST or None , instance = dns_credential , cancel_url = cancel_url )
if form . is_valid ():
form . save ()
messages . success ( request , _ ( 'DNS Credential saved successfully.' ))
return redirect ( cancel_url )
context = {
'form' : form ,
'title' : title ,
'page_title' : title ,
}
return render ( request , 'app_gateway/dns_credential_form.html' , context )
@login_required
def view_delete_dns_credential ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
dns_credential = get_object_or_404 ( DnsChallengeCredential , uuid = request . GET . get ( 'uuid' ))
cancel_url = reverse ( 'app_gateway_list' ) + '?tab=dns_credentials'
if request . method == 'POST' :
try :
dns_credential . delete ()
messages . success ( request , _ ( 'DNS Credential deleted successfully.' ))
except ProtectedError :
messages . error ( request , _ ( 'Cannot delete this DNS Credential because it is currently in use by an Application Host.' ))
return redirect ( cancel_url )
context = {
'dns_credential' : dns_credential ,
'title' : _ ( 'Delete DNS Credential' ),
'cancel_url' : cancel_url ,
'text' : _ ( 'Are you sure you want to delete the DNS credential " %(name)s "?' ) % { 'name' : dns_credential . display_name }
}
return render ( request , 'generic_delete_confirmation.html' , context )
2026-03-14 11:54:49 -03:00
@login_required
@require_POST
def view_export_caddy_config ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
redirect_url = reverse ( 'app_gateway_list' ) + '?tab=applications'
2026-03-17 15:43:21 -03:00
if not settings . CADDY_ENABLED :
messages . error ( request , _ (
'Configuration export is not available because Caddy is not enabled. '
'To use App Gateway and Gatekeeper, start the application using docker-compose-caddy.yml.'
))
return redirect ( redirect_url )
2026-03-14 11:54:49 -03:00
2026-03-17 15:43:21 -03:00
export_caddy_config ( '/caddy_json_export/' )
if settings . DEBUG :
export_caddy_config ( os . path . join ( settings . BASE_DIR , 'containers' , 'caddy' , 'config_files' ))
messages . success ( request , _ ( 'Configuration exported successfully.' ))
2026-03-14 11:54:49 -03:00
return redirect ( redirect_url )