Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Auth key usage, sudo user option, test path fix

Juan Denis27d4f839f408
diff --git a/backend/app/api/api_analytics.py b/backend/app/api/api_analytics.py
index 612dac59..e4ff45ef 100644
--- a/backend/app/api/api_analytics.py
+++ b/backend/app/api/api_analytics.py
@@ -1,6 +1,6 @@
"""API analytics endpoints."""
from flask import Blueprint, jsonify, request
-from app.middleware.rbac import admin_required, get_current_user
+from app.middleware.rbac import admin_required, auth_required, get_current_user
from app.services.api_analytics_service import ApiAnalyticsService
from app.services.api_key_service import ApiKeyService
@@ -42,7 +42,7 @@ def timeseries():
@api_analytics_bp.route('/keys/<int:key_id>/usage', methods=['GET'])
-@admin_required
+@auth_required
def key_usage(key_id):
"""Get usage stats for a specific API key."""
period = request.args.get('period', '24h')
diff --git a/backend/app/services/wordpress_service.py b/backend/app/services/wordpress_service.py
index 22204eb8..6aa14bb8 100644
--- a/backend/app/services/wordpress_service.py
+++ b/backend/app/services/wordpress_service.py
@@ -71,7 +71,7 @@ define('WP_AUTO_UPDATE_CORE', 'minor');
return install_result
try:
- cmd = privileged_cmd(['sudo', '-u', user, cls.WP_CLI_PATH, '--path=' + path] + command)
+ cmd = privileged_cmd([cls.WP_CLI_PATH, '--path=' + path] + command, user=user)
result = subprocess.run(
cmd,
capture_output=True,
diff --git a/backend/app/utils/system.py b/backend/app/utils/system.py
index 1c466205..88047f3a 100644
--- a/backend/app/utils/system.py
+++ b/backend/app/utils/system.py
@@ -27,33 +27,40 @@ def _needs_sudo() -> bool:
return True
-def privileged_cmd(cmd: Union[List[str], str]) -> Union[List[str], str]:
+def privileged_cmd(cmd: Union[List[str], str], *, user: Optional[str] = None) -> Union[List[str], str]:
"""Return *cmd* with ``sudo`` prepended when necessary.
Use this when you need the command list for ``Popen`` or other non-``run``
callers. For simple ``subprocess.run`` calls prefer :func:`run_privileged`.
+
+ Pass *user* to run the command as a specific user (``sudo -u <user>``).
"""
if isinstance(cmd, str):
if _needs_sudo() and not cmd.lstrip().startswith('sudo '):
+ if user:
+ return f'sudo -u {user} {cmd}'
return f'sudo {cmd}'
return cmd
cmd = list(cmd)
if _needs_sudo() and cmd[0] != 'sudo':
+ if user:
+ return ['sudo', '-u', user] + cmd
return ['sudo'] + cmd
return cmd
-def run_privileged(cmd: Union[List[str], str], **kwargs) -> subprocess.CompletedProcess:
+def run_privileged(cmd: Union[List[str], str], *, user: Optional[str] = None, **kwargs) -> subprocess.CompletedProcess:
"""Run a command with sudo if the current process is not root.
Prepends ``sudo`` only when needed (not root, not Windows, sudo exists).
+ Pass *user* to run the command as a specific user (``sudo -u <user>``).
Defaults to ``capture_output=True, text=True`` but callers can override.
Returns the raw ``CompletedProcess`` so services keep their existing
error-handling patterns.
"""
- cmd = privileged_cmd(cmd)
+ cmd = privileged_cmd(cmd, user=user)
kwargs.setdefault('capture_output', True)
kwargs.setdefault('text', True)
return subprocess.run(cmd, **kwargs)
diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py
index 6bafcc1e..e727d343 100644
--- a/backend/tests/conftest.py
+++ b/backend/tests/conftest.py
@@ -5,7 +5,7 @@ import sys
import pytest
# Ensure backend root is on path
-_backend = os.path.dirname(os.path.abspath(__file__))
+_backend = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _backend not in sys.path:
sys.path.insert(0, _backend)