mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-01 20:55:16 +09:00
Report services and domains per server
The server list could not say what a server carries. It now returns services_count and domains_count for every row, computed for the whole list in two queries and limited to the services the caller may see, so a count never reveals something they have no access to.
This commit is contained in:
1 parent
ebf19e8a9e
commit
4fa5caef79
4 files changed
+165
-1
No files matched your search
@@ -15,6 +15,7 @@ from sqlalchemy.orm import joinedload
|
||||
from app import db, limiter
|
||||
from app.services import permission_levels
|
||||
from app.services.server_metrics_service import latest_metrics_by_server
|
||||
from app.services.server_workload_service import workload_counts_by_server
|
||||
from app.api._query import apply_query, QueryParseError
|
||||
from app.api._request import json_object
|
||||
from app.models.server import Server, ServerGroup, ServerMetrics, ServerCommand, AgentSession
|
||||
@@ -357,6 +358,13 @@ def list_servers():
|
||||
(server.id for server in servers), order_by='id',
|
||||
)
|
||||
|
||||
# What each server carries, when the caller wants it. Two queries for the
|
||||
# whole list, scoped to the services this caller may see.
|
||||
count_keys = [key for key in ('services_count', 'domains_count')
|
||||
if fields is None or key in fields]
|
||||
workload = (workload_counts_by_server((server.id for server in servers), user, ws_id)
|
||||
if count_keys else {})
|
||||
|
||||
result = []
|
||||
for server in servers:
|
||||
server_dict = server.to_dict(fields=fields)
|
||||
@@ -364,6 +372,10 @@ def list_servers():
|
||||
metrics = metrics_by_server.get(server.id)
|
||||
if metrics:
|
||||
server_dict['metrics'] = metrics.to_dict()
|
||||
counts = workload.get(server.id)
|
||||
if counts:
|
||||
for key in count_keys:
|
||||
server_dict[key] = counts[key]
|
||||
result.append(server_dict)
|
||||
|
||||
return jsonify(result)
|
||||
|
||||
@@ -476,7 +476,10 @@ class Server(TimestampMixin, JsonColumnMixin, db.Model):
|
||||
# Emitted by to_dict but not mapped columns: each costs a relationship load
|
||||
# or a JSON walk, so `$select` has to know they exist in order to decline
|
||||
# them. Kept next to to_dict so the two cannot drift.
|
||||
DERIVED_FIELDS = ('group_name', 'onboarding_progress', 'latest_metrics')
|
||||
# `services_count` / `domains_count` are added by the list route (they
|
||||
# need the caller's access scope), not by to_dict.
|
||||
DERIVED_FIELDS = ('group_name', 'onboarding_progress', 'latest_metrics',
|
||||
'services_count', 'domains_count')
|
||||
|
||||
def to_dict(self, include_metrics=False, fields=None):
|
||||
"""Serialize the server.
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
"""What each server carries: its services and the domains pointing at them.
|
||||
|
||||
The server list shows these as columns, so they are computed for the whole
|
||||
list at once (two queries, however many servers) rather than per row.
|
||||
"""
|
||||
from collections import Counter
|
||||
|
||||
from app.models import Application
|
||||
from app.models.domain import Domain
|
||||
from app.services.workspace_service import WorkspaceService
|
||||
|
||||
|
||||
def workload_counts_by_server(server_ids, user, workspace_id=None):
|
||||
"""Return ``{server_id: {'services_count': n, 'domains_count': n}}``.
|
||||
|
||||
Only services the caller may see are counted, by the same scoping the
|
||||
service list applies: a count must not reveal that a server runs something
|
||||
the caller has no access to. A service with no ``server_id`` runs on the
|
||||
panel host itself and belongs to no row here.
|
||||
"""
|
||||
ids = list(server_ids)
|
||||
if not ids or user is None:
|
||||
return {}
|
||||
|
||||
services = WorkspaceService.scope_query(
|
||||
Application.query_active(), Application, user,
|
||||
workspace_id=workspace_id, owner_attr='user_id',
|
||||
grant_resource_type='application',
|
||||
).filter(Application.server_id.in_(ids))
|
||||
server_of = dict(services.with_entities(Application.id, Application.server_id).all())
|
||||
|
||||
service_counts = Counter(server_of.values())
|
||||
domain_counts = Counter()
|
||||
if server_of:
|
||||
domain_rows = (Domain.query_active()
|
||||
.filter(Domain.application_id.in_(list(server_of)))
|
||||
.with_entities(Domain.application_id)
|
||||
.all())
|
||||
for (application_id,) in domain_rows:
|
||||
domain_counts[server_of[application_id]] += 1
|
||||
|
||||
return {
|
||||
server_id: {
|
||||
'services_count': service_counts.get(server_id, 0),
|
||||
'domains_count': domain_counts.get(server_id, 0),
|
||||
}
|
||||
for server_id in ids
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
"""The server list reports how many services and domains each server carries."""
|
||||
from app import db
|
||||
from app.models.domain import Domain
|
||||
|
||||
from tests.factories import headers_for, make_application, make_server, make_user
|
||||
|
||||
|
||||
def _rows(client, headers, query=''):
|
||||
resp = client.get(f'/api/v1/servers{query}', headers=headers)
|
||||
assert resp.status_code == 200
|
||||
return {row['name']: row for row in resp.get_json()}
|
||||
|
||||
|
||||
def test_list_counts_services_and_domains_per_server(app, client):
|
||||
with app.app_context():
|
||||
admin = make_user(db, role='admin')
|
||||
busy = make_server(db, name='busy')
|
||||
make_server(db, name='idle')
|
||||
web = make_application(db, name='web', user_id=admin.id, server_id=busy.id)
|
||||
make_application(db, name='api', user_id=admin.id, server_id=busy.id)
|
||||
# Runs on the panel host: belongs to no server row.
|
||||
make_application(db, name='local', user_id=admin.id, server_id=None)
|
||||
db.session.add_all([
|
||||
Domain(name='a.example.test', application_id=web.id),
|
||||
Domain(name='b.example.test', application_id=web.id),
|
||||
])
|
||||
db.session.commit()
|
||||
headers = headers_for(admin)
|
||||
|
||||
rows = _rows(client, headers)
|
||||
assert (rows['busy']['services_count'], rows['busy']['domains_count']) == (2, 2)
|
||||
assert (rows['idle']['services_count'], rows['idle']['domains_count']) == (0, 0)
|
||||
|
||||
|
||||
def test_deleted_services_and_domains_are_not_counted(app, client):
|
||||
with app.app_context():
|
||||
admin = make_user(db, role='admin')
|
||||
box = make_server(db, name='box')
|
||||
kept = make_application(db, name='kept', user_id=admin.id, server_id=box.id)
|
||||
gone = make_application(db, name='gone', user_id=admin.id, server_id=box.id)
|
||||
live = Domain(name='live.example.test', application_id=kept.id)
|
||||
dead = Domain(name='dead.example.test', application_id=kept.id)
|
||||
db.session.add_all([live, dead])
|
||||
db.session.commit()
|
||||
gone.soft_delete()
|
||||
dead.soft_delete()
|
||||
db.session.commit()
|
||||
headers = headers_for(admin)
|
||||
|
||||
row = _rows(client, headers)['box']
|
||||
assert (row['services_count'], row['domains_count']) == (1, 1)
|
||||
|
||||
|
||||
def test_counts_only_include_services_the_caller_can_see(app):
|
||||
"""Asked at the service: a non-admin's count covers their own services
|
||||
only, while an admin's covers everything on the server."""
|
||||
from app.services.server_workload_service import workload_counts_by_server
|
||||
|
||||
with app.app_context():
|
||||
admin = make_user(db, role='admin')
|
||||
owner = make_user(db, role='developer')
|
||||
other = make_user(db, role='developer')
|
||||
box = make_server(db, name='shared')
|
||||
mine = make_application(db, name='mine', user_id=owner.id, server_id=box.id)
|
||||
theirs = make_application(db, name='theirs', user_id=other.id, server_id=box.id)
|
||||
db.session.add_all([
|
||||
Domain(name='mine.example.test', application_id=mine.id),
|
||||
Domain(name='theirs.example.test', application_id=theirs.id),
|
||||
])
|
||||
db.session.commit()
|
||||
|
||||
assert workload_counts_by_server([box.id], owner)[box.id] == {
|
||||
'services_count': 1, 'domains_count': 1}
|
||||
assert workload_counts_by_server([box.id], admin)[box.id] == {
|
||||
'services_count': 2, 'domains_count': 2}
|
||||
assert workload_counts_by_server([box.id], None) == {}
|
||||
assert workload_counts_by_server([], admin) == {}
|
||||
|
||||
|
||||
def test_select_can_ask_for_just_the_counts(app, client):
|
||||
with app.app_context():
|
||||
admin = make_user(db, role='admin')
|
||||
box = make_server(db, name='box')
|
||||
make_application(db, name='web', user_id=admin.id, server_id=box.id)
|
||||
headers = headers_for(admin)
|
||||
|
||||
resp = client.get('/api/v1/servers?$select=name,services_count', headers=headers)
|
||||
assert resp.status_code == 200
|
||||
row = resp.get_json()[0]
|
||||
assert row['services_count'] == 1
|
||||
assert 'domains_count' not in row
|
||||
|
||||
|
||||
def test_narrow_select_skips_the_counts(app, client):
|
||||
with app.app_context():
|
||||
admin = make_user(db, role='admin')
|
||||
make_server(db, name='box')
|
||||
headers = headers_for(admin)
|
||||
|
||||
row = client.get('/api/v1/servers?$select=name', headers=headers).get_json()[0]
|
||||
assert 'services_count' not in row and 'domains_count' not in row
|
||||
Reference in new issue
Block a user