diff --git a/docs/deployment.md b/docs/deployment.md index fa2bc0b..ddb5e27 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -3491,6 +3491,27 @@ other panel as well. If a panel secret changes, exchange its new public key and re-enroll it. Each panel keeps its own hosting accounts and can operate while another panel is unreachable. +#### View hosting accounts across panels + +On each owning panel, open **Enrolled panels**, select **Hosting inventory +sharing** on the source panel's row, and choose **Share hosting accounts**. +Only the mapped local operator can enable sharing. This grants read-only +access to active account names, primary domains and status, not to +customer profiles or service credentials. Sharing is directional and off +by default, including for previously enrolled peers. + +On the source panel, open **Hosting accounts → All nodes** to search and +filter by the owning panel. The combined view shows the first page from +each node. Select a node to page through all of its results. The node +column uses short hostnames unless they collide. Hover or focus the node +label to read the full host shown by the enrollment. Account links open +the owning panel, which still checks its own operator sign-in. + +If a peer is unavailable, doesn't share its inventory or hasn't been +upgraded to a version that supports it, the list marks the results as +incomplete. **This node** continues to show the regular local list. The +cluster inventory is not exposed to customers or managers. + ### Set up hosting features Open **About Coulobre → Configure features** as a superuser with a verified diff --git a/src/coulobre/hosting/cluster_account_views.py b/src/coulobre/hosting/cluster_account_views.py new file mode 100644 index 0000000..9147fec --- /dev/null +++ b/src/coulobre/hosting/cluster_account_views.py @@ -0,0 +1,62 @@ +from django.http import Http404 +from django.utils.decorators import method_decorator +from django.views.decorators.cache import never_cache +from django.views.generic import TemplateView + +from .account_administration import OperatorAuthorization +from .cluster_accounts import ( + MAX_OFFSET, + PAGE_SIZE, + collect_inventory, + inventory_nodes, + search_term, +) + + +@method_decorator(never_cache, name="dispatch") +class ClusterAccountList(OperatorAuthorization, TemplateView): + template_name = "hosting/cluster_account_list.html" + + def get_context_data(self, **kwargs): + nodes = inventory_nodes(self.request) + if not nodes: + raise Http404 + selected = self.request.GET.get("node", "") + subset = [ + node for node in nodes if not selected or node.key == selected + ] + if not subset: + raise Http404 + offset = self.request.GET.get("offset", "0") + if ( + not offset.isascii() + or not offset.isdecimal() + or len(offset) > len(str(MAX_OFFSET)) + or int(offset) > MAX_OFFSET + or int(offset) % PAGE_SIZE + or (int(offset) and not selected) + ): + raise Http404 + offset = int(offset) + try: + search = search_term(self.request.GET.get("q", "")) + except ValueError: + raise Http404 from None + pages = collect_inventory(subset, self.request.user, search, offset) + return super().get_context_data( + cluster_view=True, + cluster_nodes=nodes, + pages=pages, + unavailable_nodes=[ + page["node"] for page in pages if page["unavailable"] + ], + selected_node=selected, + search=search, + previous_offset=offset - PAGE_SIZE if offset else None, + next_offset=( + offset + PAGE_SIZE + if selected and pages[0]["more"] and offset < MAX_OFFSET + else None + ), + **kwargs, + ) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 7335d36..41c6062 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -1400,3 +1400,13 @@ dd { .workspace-list li + li { margin-top: 0.4rem; } + +.hosting-node-full { + display: none; + overflow-wrap: anywhere; +} + +.hosting-node:hover .hosting-node-full, +.hosting-node:focus .hosting-node-full { + display: block; +} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index 9c3dc85..32cf2ed 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -13,6 +13,7 @@ {% endif %} {% endblock %} +{% block page_tabs %}{% include "hosting/hosting_account_tabs.html" %}{% endblock %} {% block page_context %}
{% endif %} + {% if local_hosting_node %}{% include "hosting/hosting_node.html" with node=local_hosting_node only %}{% endif %} {% include "hosting/account_status.html" %} {% if is_operator and perms.hosting.view_customer %}{{ account.customer|default_if_none:"No customer linked" }}{% endif %} {% if is_tenant or perms.hosting.view_hostingplan %} diff --git a/src/coulobre/hosting/templates/hosting/cluster_account_list.html b/src/coulobre/hosting/templates/hosting/cluster_account_list.html new file mode 100644 index 0000000..12d1d94 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/cluster_account_list.html @@ -0,0 +1,65 @@ +{% extends "hosting/workspace_page.html" %} +{% block title %}Hosting accounts · Coulobre Panel{% endblock %} +{% block page_title %}Hosting accounts{% endblock %} +{% block page_metadata %} +

Find accounts across enrolled panels. Open an account on the panel that hosts it.

+{% endblock %} +{% block page_actions %} + Add account on this node +{% endblock %} +{% block page_tabs %}{% include "hosting/hosting_account_tabs.html" %}{% endblock %} +{% block page_context %} +
+ + + + + + + {% if search or selected_node %}Clear{% endif %} + +
+{% endblock %} +{% block page_content %} + {% if unavailable_nodes %} +

Results may be incomplete. Could not read the hosting inventory on {% for node in unavailable_nodes %}{{ node.label }}{% if not forloop.last %}, {% endif %}{% endfor %}. Check that the peer is reachable and allows inventory sharing.

+ {% endif %} +
+
+ + + {% for page in pages %} + {% if not page.unavailable %} + + {% for account in page.accounts %} + + + + + + {% empty %} + + + + {% endfor %} + {% if page.more and not selected_node %} + + {% endif %} + + {% endif %} + {% endfor %} +
AccountHosting nodeStatus
{{ account.name }}{% include "hosting/hosting_node.html" with node=page.node only %}{% include "hosting/account_status.html" %}
No matching accounts on {{ page.node.label }}{% if previous_offset is not None %} on this page{% endif %}.
View all results on {{ page.node.label }}
+
+ {% if previous_offset is not None or next_offset is not None %} + + {% endif %} +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/hosting_account_tabs.html b/src/coulobre/hosting/templates/hosting/hosting_account_tabs.html new file mode 100644 index 0000000..190b90e --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/hosting_account_tabs.html @@ -0,0 +1,6 @@ +{% if cluster_nodes %} + +{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/hosting_node.html b/src/coulobre/hosting/templates/hosting/hosting_node.html new file mode 100644 index 0000000..7621d92 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/hosting_node.html @@ -0,0 +1,5 @@ +{% if node.label == node.authority %} + {{ node.label }} +{% else %} + {{ node.label }} +{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/panel_peers.html b/src/coulobre/hosting/templates/hosting/panel_peers.html index ff231f8..07c1be3 100644 --- a/src/coulobre/hosting/templates/hosting/panel_peers.html +++ b/src/coulobre/hosting/templates/hosting/panel_peers.html @@ -45,6 +45,17 @@ {% if request.user.pk == peer.operator_id %} +
Hosting inventory sharing · {% if peer.account_inventory_allowed %}On{% else %}Off{% endif %} +
+ {% csrf_token %} +

Allow the mapped operator on this peer to list this panel's account names, domains and status.

+ {% if peer.account_inventory_allowed %} + + {% else %} + + {% endif %} +
+
{% if peer.status.can_check_packages %}
Check peer packages
diff --git a/src/coulobre/hosting/test_cluster_accounts.py b/src/coulobre/hosting/test_cluster_accounts.py new file mode 100644 index 0000000..9f0d655 --- /dev/null +++ b/src/coulobre/hosting/test_cluster_accounts.py @@ -0,0 +1,166 @@ +from unittest.mock import patch + +from django.contrib.auth import get_user_model +from django.test import TestCase, override_settings +from django.urls import reverse +from django.utils import timezone +from django_otp import DEVICE_ID_SESSION_KEY +from django_otp.plugins.otp_totp.models import TOTPDevice + +from .cluster_accounts import PAGE_SIZE +from .models import HostingAccount, PanelPeer +from .peering import PeerAccessError + + +@override_settings(PASSWORD_RESET_ORIGIN="https://web1.a.cluster.example") +class ClusterAccountTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.operator = get_user_model().objects.create_superuser("mapped") + cls.device = TOTPDevice.objects.create(user=cls.operator, name="test") + cls.local = HostingAccount.objects.create( + name="Local hosting", primary_domain="local.example.test" + ) + cls.peer = PanelPeer.objects.create( + origin="https://web2.cluster.example", + public_key="ab" * 32, + remote_username="remote-operator", + operator=cls.operator, + ) + cls.customer = get_user_model().objects.create_user("customer") + cls.cluster = reverse("panel:cluster_accounts") + cls.local_list = reverse("panel:accounts") + + def sign_in(self): + self.client.force_login(self.operator) + session = self.client.session + session[DEVICE_ID_SESSION_KEY] = self.device.persistent_id + session.save() + + def remote_result(self, *, count=1, more=False): + return { + "accounts": [ + { + "id": number, + "name": f"Remote site {number}", + "primary_domain": f"remote{number}.example.test", + "services_suspended": True, + } + for number in range(1, count + 1) + ], + "more": more, + } + + def test_short_name_and_links_point_to_owner(self): + self.sign_in() + local = self.client.get(self.local_list) + self.assertContains(local, "This node") + self.assertContains(local, "All nodes") + self.assertContains(local, 'aria-label="web1.a.cluster.example"') + self.assertContains(local, ">web1<") + with patch( + "coulobre.hosting.cluster_accounts.peer_call", + return_value=self.remote_result(), + ) as exchange: + response = self.client.get(self.cluster) + self.assertContains(response, self.local.get_absolute_url()) + self.assertContains(response, "Remote site 1") + self.assertContains( + response, + self.peer.origin + reverse("panel:account", kwargs={"pk": 1}), + ) + self.assertContains(response, ">web2<") + self.assertContains(response, 'aria-label="web2.cluster.example"') + self.assertIn("no-store", response.headers["Cache-Control"]) + exchange.assert_called_once_with( + self.peer, "account-inventory", "mapped", '{"q": "", "offset": 0}' + ) + + def test_hostname_collision_only_expands_colliding_labels(self): + PanelPeer.objects.create( + origin="https://web1.b.cluster.example", + public_key="cd" * 32, + remote_username="collision", + operator=self.operator, + ) + self.sign_in() + response = self.client.get(self.cluster, {"node": "local"}) + self.assertContains(response, ">web1.a<") + labels = [node.label for node in response.context["cluster_nodes"]] + self.assertEqual(labels, ["web1.a", "web1.b", "web2"]) + + def test_filter_and_search_visit_only_selected_node_and_can_page(self): + self.sign_in() + with patch( + "coulobre.hosting.cluster_accounts.peer_call", + return_value=self.remote_result(count=PAGE_SIZE, more=True), + ) as exchange: + response = self.client.get( + self.cluster, + {"node": self.peer.pk, "q": "remote", "offset": 20}, + ) + self.assertNotContains(response, self.local.name) + self.assertContains(response, "Previous") + self.assertContains(response, "Next") + self.assertContains(response, "offset=40") + exchange.assert_called_once_with( + self.peer, + "account-inventory", + "mapped", + '{"q": "remote", "offset": 20}', + ) + + def test_unavailable_or_invalid_peer_results_do_not_hide_local_accounts( + self, + ): + self.sign_in() + for result in ( + PeerAccessError("Peer unavailable"), + self.remote_result() | {"unexpected": True}, + ): + with ( + self.subTest(result=result), + patch( + "coulobre.hosting.cluster_accounts.peer_call", + **( + {"side_effect": result} + if isinstance(result, Exception) + else {"return_value": result} + ), + ), + ): + response = self.client.get(self.cluster) + self.assertContains(response, "Results may be incomplete") + self.assertContains(response, self.local.name) + self.assertNotContains(response, "Remote site") + + def test_only_mapped_verified_operators_can_use_inventory(self): + self.client.force_login(self.operator) + self.assertEqual(self.client.get(self.cluster).status_code, 403) + self.client.force_login(self.customer) + self.assertEqual(self.client.get(self.cluster).status_code, 403) + self.sign_in() + other = get_user_model().objects.create_superuser("unmapped") + PanelPeer.objects.filter(pk=self.peer.pk).update(operator=other) + with patch("coulobre.hosting.cluster_accounts.peer_call") as exchange: + self.assertEqual(self.client.get(self.cluster).status_code, 404) + self.assertNotContains( + self.client.get(self.local_list), "All nodes" + ) + exchange.assert_not_called() + + def test_revoked_peer_and_unknown_filters_are_not_available(self): + self.sign_in() + self.assertEqual( + self.client.get(self.cluster, {"node": "999"}).status_code, 404 + ) + self.assertEqual( + self.client.get(self.cluster, {"offset": "20"}).status_code, 404 + ) + PanelPeer.objects.filter(pk=self.peer.pk).update( + revoked_at=timezone.now() + ) + self.assertEqual(self.client.get(self.cluster).status_code, 404) + self.assertNotContains( + self.client.get(self.local_list), "Hosting node" + ) diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index 6ca84b3..25f7526 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -22,6 +22,7 @@ from .account_security import ( ) from .backup_restores import RestoreKind from .certificate_views import customer_certificates, operator_certificates +from .cluster_account_views import ClusterAccountList from .credential_views import DatabaseCredentialView, MailboxCredentialView from .customer_backup_views import CustomerBackupView, CustomerRestoreView from .database_login_views import ( @@ -328,6 +329,11 @@ urlpatterns = [ name="smtp_applications", ), path("", views.AccountList.as_view(), name="accounts"), + path( + "accounts/cluster/", + ClusterAccountList.as_view(), + name="cluster_accounts", + ), path( "service-health/", ServiceHealthView.as_view(), name="service_health" ), diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index f9a75b4..0494e51 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -74,6 +74,7 @@ from .backup_restores import ( retry_remote_mail_restore, ) from .backup_retention import change_backup_retention, protect_backup +from .cluster_accounts import inventory_nodes from .dns_defaults import hosting_address_choices, zone_defaults from .dns_forms import ( DnsZoneEditForm, @@ -400,6 +401,9 @@ class AccountList(TenantScopeMixin, PermissionRequiredMixin, ListView): def get_context_data(self, **kwargs): context = super().get_context_data(search=self.search, **kwargs) + if nodes := inventory_nodes(self.request): + context["cluster_nodes"] = nodes + context["local_hosting_node"] = nodes[0] if not self.search and not self.request.GET.get("customer"): context["onboarding"] = onboarding_context(self.request) context["show_customer_website_link"] = settings.WEBSITE_USAGE diff --git a/src/coulobre/hosting/workspaces.py b/src/coulobre/hosting/workspaces.py index 4c3b3cd..e09e156 100644 --- a/src/coulobre/hosting/workspaces.py +++ b/src/coulobre/hosting/workspaces.py @@ -75,6 +75,7 @@ CUSTOMER_NAV_ROUTES = { } WORKSPACE_PARENT_ROUTES = { + "cluster_accounts": ("accounts", ()), "user_delete": ("all_users", ()), "remote_deploy": ("connected_nodes", ()), "dns_topology": ("dns_defaults", ()),