194 lines
5.8 KiB
Python
194 lines
5.8 KiB
Python
from django.conf import settings
|
|
from django.contrib.auth import get_user_model
|
|
from django.http import Http404, JsonResponse
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.utils.functional import cached_property
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.decorators.debug import (
|
|
sensitive_post_parameters,
|
|
sensitive_variables,
|
|
)
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from .access import customer_for_user
|
|
from .credential_views import DatabaseCredentialView
|
|
from .installation_features import panel_feature_allowed
|
|
from .models import (
|
|
DatabaseLogin,
|
|
HostingDatabase,
|
|
PhpMyAdminGrant,
|
|
ServiceCredentialAccess,
|
|
)
|
|
from .service_credentials import (
|
|
CredentialKind,
|
|
CredentialOutcome,
|
|
can_reveal_credentials,
|
|
enabled_credential_access,
|
|
fetch_service_credentials,
|
|
)
|
|
from .signon import (
|
|
bridge_ticket,
|
|
consume_signon_grant,
|
|
issue_signon_grant,
|
|
signon_actor,
|
|
)
|
|
|
|
|
|
def phpmyadmin_available(request):
|
|
return bool(
|
|
panel_feature_allowed("PHPMYADMIN_URL")
|
|
and settings.PHPMYADMIN_BRIDGE_KEY
|
|
and enabled_credential_access(request, CredentialKind.DATABASE)
|
|
)
|
|
|
|
|
|
def active_database_logins():
|
|
return DatabaseLogin.objects.filter(
|
|
finished_at__isnull=False, problem="", action="grant"
|
|
)
|
|
|
|
|
|
class PhpMyAdminLaunch(DatabaseCredentialView):
|
|
http_method_names = ["post"]
|
|
secondary = False
|
|
|
|
@cached_property
|
|
def selected_login(self):
|
|
if not self.secondary:
|
|
return None
|
|
return get_object_or_404(
|
|
active_database_logins().select_related("database__account"),
|
|
pk=self.kwargs["pk"],
|
|
database__in=self.get_queryset().filter(
|
|
account__services_suspended=False
|
|
),
|
|
)
|
|
|
|
@cached_property
|
|
def resource(self):
|
|
return (
|
|
self.selected_login.database
|
|
if self.secondary
|
|
else super().resource
|
|
)
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
response = super().dispatch(request, *args, **kwargs)
|
|
response["Referrer-Policy"] = "origin"
|
|
return response
|
|
|
|
@sensitive_variables()
|
|
def post(self, request, *args, **kwargs):
|
|
if not phpmyadmin_available(request):
|
|
raise Http404
|
|
token = issue_signon_grant(
|
|
PhpMyAdminGrant,
|
|
request.user,
|
|
database=self.resource,
|
|
database_login=self.selected_login,
|
|
)
|
|
return render(
|
|
request,
|
|
"hosting/service_signon.html",
|
|
{
|
|
"destination": settings.PHPMYADMIN_URL
|
|
+ "/coulobre-signon.php",
|
|
"ticket": token,
|
|
"product": "phpMyAdmin",
|
|
"resource": (
|
|
self.selected_login.spec.name
|
|
if self.secondary
|
|
else self.resource.spec.name
|
|
),
|
|
"ticket_field": "ticket",
|
|
"account": self.account,
|
|
},
|
|
)
|
|
|
|
|
|
def actor_has_database_access(actor, database):
|
|
if (
|
|
database.account.retired_at is not None
|
|
or database.account.services_suspended
|
|
or not can_reveal_credentials(actor, CredentialKind.DATABASE)
|
|
):
|
|
return False
|
|
customer = customer_for_user(actor)
|
|
return customer is None or database.account.customer_id == customer.pk
|
|
|
|
|
|
@csrf_exempt
|
|
@require_POST
|
|
@never_cache
|
|
@sensitive_post_parameters("ticket")
|
|
@sensitive_variables()
|
|
def redeem_phpmyadmin(request):
|
|
if not phpmyadmin_available(request):
|
|
raise Http404
|
|
token = bridge_ticket(request, settings.PHPMYADMIN_BRIDGE_KEY)
|
|
grant = consume_signon_grant(
|
|
PhpMyAdminGrant, token, "database__account", "database_login"
|
|
)
|
|
if grant is None:
|
|
raise Http404
|
|
actor = signon_actor(grant, grant.actor)
|
|
if actor is None or not actor_has_database_access(actor, grant.database):
|
|
raise Http404
|
|
|
|
database = grant.database
|
|
resource = database.spec
|
|
if grant.database_login_id is not None:
|
|
login = (
|
|
active_database_logins()
|
|
.filter(pk=grant.database_login_id, database=database)
|
|
.select_related("database")
|
|
.first()
|
|
)
|
|
if login is None:
|
|
raise Http404
|
|
resource = login.spec
|
|
audit = ServiceCredentialAccess.objects.create(
|
|
actor=grant.actor,
|
|
actor_username=grant.actor.get_username(),
|
|
hosting_account_id=database.account_id,
|
|
kind=CredentialKind.DATABASE,
|
|
resource=resource.name,
|
|
method=ServiceCredentialAccess.Method.PHPMYADMIN,
|
|
outcome=CredentialOutcome.REQUESTED,
|
|
)
|
|
try:
|
|
credentials = fetch_service_credentials(resource)
|
|
except (OSError, ValueError):
|
|
audit.outcome = CredentialOutcome.UNAVAILABLE
|
|
audit.save(update_fields=["outcome"])
|
|
return JsonResponse(
|
|
{"error": "Database login unavailable."}, status=503
|
|
)
|
|
current = (
|
|
HostingDatabase.objects.select_related("account")
|
|
.filter(pk=database.pk)
|
|
.first()
|
|
)
|
|
actor = signon_actor(
|
|
grant, get_user_model().objects.filter(pk=grant.actor_id).first()
|
|
)
|
|
if (
|
|
current is None
|
|
or actor is None
|
|
or current.spec != database.spec
|
|
or not actor_has_database_access(actor, current)
|
|
or (
|
|
grant.database_login_id is not None
|
|
and not active_database_logins()
|
|
.filter(pk=grant.database_login_id, database=current)
|
|
.exists()
|
|
)
|
|
):
|
|
audit.outcome = CredentialOutcome.UNAVAILABLE
|
|
audit.save(update_fields=["outcome"])
|
|
raise Http404
|
|
audit.outcome = CredentialOutcome.REVEALED
|
|
audit.save(update_fields=["outcome"])
|
|
return JsonResponse(credentials)
|