Confirm and audit credential rotation in the panel
This commit is contained in:
1 parent
c2655b57f5
commit
651f041e84
10 files changed
+662
-17
No files matched your search
@@ -1,29 +1,41 @@
|
||||
from datetime import timedelta
|
||||
from uuid import uuid4
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core import signing
|
||||
from django.core.exceptions import PermissionDenied, ValidationError
|
||||
from django.db import connection, transaction
|
||||
from django.db.models import F
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.debug import sensitive_variables
|
||||
|
||||
from coulobre.credential_rotation import valid_generation
|
||||
from coulobre.service_credentials import (
|
||||
CREDENTIAL_SOCKET,
|
||||
CREDENTIAL_TIMEOUT_SECONDS,
|
||||
CREDENTIALS_UNAVAILABLE,
|
||||
MAIL_CREDENTIAL_SOCKET,
|
||||
ROTATION_TIMEOUT_SECONDS,
|
||||
ServiceCredentialRequest,
|
||||
)
|
||||
|
||||
from .access import customer_for_user, reseller_for_user
|
||||
from .models import ServiceCredentialAccess
|
||||
from .node_client import apply_on_local_node
|
||||
from .resource_requests import lock_hosting_account, require_active_services
|
||||
|
||||
ATTEMPT_LIMIT = 5
|
||||
ATTEMPT_WINDOW = timedelta(minutes=5)
|
||||
CredentialKind = ServiceCredentialAccess.Kind
|
||||
CredentialOutcome = ServiceCredentialAccess.Outcome
|
||||
CredentialMethod = ServiceCredentialAccess.Method
|
||||
ROTATION_TICKET_SALT = "coulobre.credential-rotation"
|
||||
UNCONFIRMED_ROTATION = (
|
||||
CredentialOutcome.REQUESTED,
|
||||
CredentialOutcome.UNAVAILABLE,
|
||||
)
|
||||
|
||||
CREDENTIAL_PERMISSIONS = {
|
||||
CredentialKind.DATABASE: (
|
||||
@@ -37,6 +49,10 @@ CREDENTIAL_PERMISSIONS = {
|
||||
"hosting.reveal_mailbox_credentials",
|
||||
),
|
||||
}
|
||||
ROTATION_PERMISSIONS = {
|
||||
CredentialKind.DATABASE: "hosting.rotate_database_credentials",
|
||||
CredentialKind.MAILBOX: "hosting.rotate_mailbox_credentials",
|
||||
}
|
||||
|
||||
|
||||
class ConfirmCredentialAccessForm(forms.Form):
|
||||
@@ -53,7 +69,20 @@ def enabled_credential_access(request, kind):
|
||||
return False
|
||||
if kind == CredentialKind.DATABASE:
|
||||
return settings.LOCAL_DATABASE_CREDENTIALS
|
||||
return settings.LOCAL_MAILBOX_CREDENTIALS
|
||||
return (
|
||||
settings.REMOTE_MAILBOX_CREDENTIALS
|
||||
or settings.LOCAL_MAILBOX_CREDENTIALS
|
||||
)
|
||||
|
||||
|
||||
def enabled_rotation(request, kind):
|
||||
if not enabled_credential_access(request, kind):
|
||||
return False
|
||||
if kind == CredentialKind.DATABASE:
|
||||
return settings.LOCAL_DATABASE_CREDENTIAL_ROTATION
|
||||
if settings.REMOTE_MAILBOX_CREDENTIALS:
|
||||
return settings.REMOTE_MAILBOX_CREDENTIAL_ROTATION
|
||||
return settings.LOCAL_MAILBOX_CREDENTIAL_ROTATION
|
||||
|
||||
|
||||
def can_reveal_credentials(user, kind):
|
||||
@@ -66,9 +95,24 @@ def can_reveal_credentials(user, kind):
|
||||
return user.has_perms(CREDENTIAL_PERMISSIONS[kind])
|
||||
|
||||
|
||||
def can_rotate_credentials(user, kind):
|
||||
return can_reveal_credentials(user, kind) and (
|
||||
customer_for_user(user) is not None
|
||||
or user.has_perm(ROTATION_PERMISSIONS[kind])
|
||||
)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
@sensitive_variables()
|
||||
def verify_credential_access(user, password, *, kind, resource, account_id):
|
||||
def verify_credential_access(
|
||||
user,
|
||||
password,
|
||||
*,
|
||||
kind,
|
||||
resource,
|
||||
account_id,
|
||||
method=CredentialMethod.REVEAL,
|
||||
):
|
||||
users = get_user_model().objects.filter(pk=user.pk, is_active=True)
|
||||
if connection.vendor == "sqlite":
|
||||
# Serialize confirmations before counting failed attempts.
|
||||
@@ -89,6 +133,7 @@ def verify_credential_access(user, password, *, kind, resource, account_id):
|
||||
accepted = actor.check_password(password)
|
||||
return ServiceCredentialAccess.objects.create(
|
||||
actor=actor,
|
||||
method=method,
|
||||
actor_username=actor.get_username(),
|
||||
hosting_account_id=account_id,
|
||||
kind=kind,
|
||||
@@ -101,15 +146,28 @@ def verify_credential_access(user, password, *, kind, resource, account_id):
|
||||
|
||||
|
||||
@sensitive_variables()
|
||||
def fetch_service_credentials(resource):
|
||||
spec = ServiceCredentialRequest(resource)
|
||||
reply = apply_on_local_node(
|
||||
def send_credential_request(spec):
|
||||
remote = (
|
||||
spec.kind == CredentialKind.MAILBOX
|
||||
and settings.REMOTE_MAILBOX_CREDENTIALS
|
||||
)
|
||||
return apply_on_local_node(
|
||||
spec,
|
||||
CREDENTIAL_SOCKET,
|
||||
MAIL_CREDENTIAL_SOCKET if remote else CREDENTIAL_SOCKET,
|
||||
"credential node",
|
||||
"service credential request",
|
||||
timeout=CREDENTIAL_TIMEOUT_SECONDS,
|
||||
timeout=(
|
||||
ROTATION_TIMEOUT_SECONDS
|
||||
if spec.operation == "rotate"
|
||||
else CREDENTIAL_TIMEOUT_SECONDS
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@sensitive_variables()
|
||||
def fetch_service_credentials(resource, rotation=None):
|
||||
spec = ServiceCredentialRequest(resource) if rotation is None else rotation
|
||||
reply = send_credential_request(spec)
|
||||
keys = ("user", "password")
|
||||
if spec.kind == CredentialKind.DATABASE:
|
||||
keys += ("database", "host", "socket")
|
||||
@@ -128,3 +186,93 @@ def fetch_service_credentials(resource):
|
||||
) or any(reply[key] != value for key, value in expected.items()):
|
||||
raise ValueError(CREDENTIALS_UNAVAILABLE)
|
||||
return {key: reply[key] for key in keys}
|
||||
|
||||
|
||||
class ConfirmCredentialRotationForm(ConfirmCredentialAccessForm):
|
||||
ticket = forms.CharField(max_length=500, widget=forms.HiddenInput)
|
||||
|
||||
|
||||
def rotation_history(account_id, kind=None, resource=None):
|
||||
events = ServiceCredentialAccess.objects.filter(
|
||||
hosting_account_id=account_id,
|
||||
method=CredentialMethod.ROTATE,
|
||||
rotation_id__isnull=False,
|
||||
).exclude(outcome=CredentialOutcome.BLOCKED)
|
||||
if kind is not None:
|
||||
events = events.filter(kind=kind, resource=resource)
|
||||
return events.order_by("-pk")
|
||||
|
||||
|
||||
def unresolved_rotation_exists(account_id):
|
||||
seen = set()
|
||||
for event in rotation_history(account_id).values_list(
|
||||
"kind", "resource", "outcome"
|
||||
):
|
||||
kind, resource, outcome = event
|
||||
target = kind, resource
|
||||
if target not in seen and outcome in UNCONFIRMED_ROTATION:
|
||||
return True
|
||||
seen.add(target)
|
||||
return False
|
||||
|
||||
|
||||
@sensitive_variables()
|
||||
def rotation_ticket(resource, account_id, user_id):
|
||||
manifest = ServiceCredentialRequest(resource.spec, "generation")
|
||||
last = rotation_history(account_id, manifest.kind, str(resource)).first()
|
||||
if last is not None and last.outcome in UNCONFIRMED_ROTATION:
|
||||
rotation_id = str(last.rotation_id)
|
||||
previous_id = (
|
||||
str(last.previous_rotation_id)
|
||||
if last.previous_rotation_id
|
||||
else None
|
||||
)
|
||||
else:
|
||||
reply = send_credential_request(manifest)
|
||||
if "generation" not in reply or not valid_generation(
|
||||
reply["generation"]
|
||||
):
|
||||
raise ValueError(CREDENTIALS_UNAVAILABLE)
|
||||
rotation_id, previous_id = str(uuid4()), reply["generation"]
|
||||
return signing.dumps(
|
||||
[
|
||||
account_id,
|
||||
manifest.kind,
|
||||
resource.pk,
|
||||
user_id,
|
||||
rotation_id,
|
||||
previous_id,
|
||||
],
|
||||
salt=ROTATION_TICKET_SALT,
|
||||
)
|
||||
|
||||
|
||||
def parse_rotation(ticket, resource, account_id, actor):
|
||||
payload = signing.loads(ticket, salt=ROTATION_TICKET_SALT, max_age=3600)
|
||||
if not isinstance(payload, list) or len(payload) != 6:
|
||||
raise signing.BadSignature
|
||||
rotation = ServiceCredentialRequest(resource.spec, "rotate", *payload[4:])
|
||||
if payload[:4] != [account_id, rotation.kind, resource.pk, actor.pk]:
|
||||
raise signing.BadSignature
|
||||
return rotation
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def reserve_rotation(event, rotation, customer_id):
|
||||
account = lock_hosting_account(event.hosting_account_id, customer_id)
|
||||
require_active_services(account)
|
||||
latest = rotation_history(account.pk, event.kind, event.resource).first()
|
||||
if (
|
||||
latest is not None
|
||||
and str(latest.rotation_id) != rotation.rotation_id
|
||||
and (
|
||||
latest.outcome in UNCONFIRMED_ROTATION
|
||||
or str(latest.rotation_id) != rotation.previous_id
|
||||
)
|
||||
):
|
||||
raise ValidationError(
|
||||
"Open a new password change form for this resource."
|
||||
)
|
||||
event.rotation_id = rotation.rotation_id
|
||||
event.previous_rotation_id = rotation.previous_id
|
||||
event.save(update_fields=["rotation_id", "previous_rotation_id"])
|
||||
Reference in new issue
Block a user