qodalis-cli-server-plugin-users 1.0.0b15__tar.gz → 1.0.0b19__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/PKG-INFO +3 -2
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/pyproject.toml +3 -2
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/__init__.py +3 -1
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/cli_users_builder.py +49 -7
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/__init__.py +0 -2
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/models.py +4 -54
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/server_protection.py +4 -1
- qodalis_cli_server_plugin_users-1.0.0b19/qodalis_cli_users/stores/in_memory_audit_store.py +16 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/interfaces.py +9 -27
- qodalis_cli_server_plugin_users-1.0.0b15/qodalis_cli_users/controllers/audit_controller.py +0 -58
- qodalis_cli_server_plugin_users-1.0.0b15/qodalis_cli_users/stores/in_memory_audit_store.py +0 -64
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/.gitignore +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/LICENSE +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/_http.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/_routing.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/groups_controller.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/sessions_controller.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/tokens_controller.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/controllers/users_controller.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/errors.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/external_request.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/middleware/__init__.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/middleware/auth_middleware.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/permissions.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/services/__init__.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/services/auth_service.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/services/password_hasher.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/services/users_service.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/shell_access.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/__init__.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/in_memory_api_token_store.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/in_memory_credential_store.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/in_memory_group_store.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/in_memory_session_store.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/stores/in_memory_user_store.py +0 -0
- {qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/qodalis_cli_users/validation.py +0 -0
{qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/PKG-INFO
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: qodalis-cli-server-plugin-users
|
|
3
|
-
Version: 1.0.
|
|
4
|
-
Summary: User management plugin for Qodalis CLI servers — authentication, sessions, groups
|
|
3
|
+
Version: 1.0.0b19
|
|
4
|
+
Summary: User management plugin for Qodalis CLI servers — authentication, sessions, groups and API tokens, recorded in the audit plugin's log
|
|
5
5
|
Project-URL: Homepage, https://github.com/qodalis-solutions/cli-server-python
|
|
6
6
|
Project-URL: Repository, https://github.com/qodalis-solutions/cli-server-python
|
|
7
7
|
Project-URL: Issues, https://github.com/qodalis-solutions/cli-server-python/issues
|
|
@@ -19,3 +19,4 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Requires-Python: >=3.10
|
|
21
21
|
Requires-Dist: fastapi>=0.100.0
|
|
22
|
+
Requires-Dist: qodalis-cli-server-plugin-audit>=1.0.0b17
|
{qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/pyproject.toml
RENAMED
|
@@ -4,8 +4,8 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "qodalis-cli-server-plugin-users"
|
|
7
|
-
version = "1.0.
|
|
8
|
-
description = "User management plugin for Qodalis CLI servers — authentication, sessions, groups
|
|
7
|
+
version = "1.0.0b19"
|
|
8
|
+
description = "User management plugin for Qodalis CLI servers — authentication, sessions, groups and API tokens, recorded in the audit plugin's log"
|
|
9
9
|
readme = { text = "", content-type = "text/plain" }
|
|
10
10
|
license = "MIT"
|
|
11
11
|
requires-python = ">=3.10"
|
|
@@ -24,6 +24,7 @@ classifiers = [
|
|
|
24
24
|
"Programming Language :: Python :: 3.13",
|
|
25
25
|
]
|
|
26
26
|
dependencies = [
|
|
27
|
+
"qodalis-cli-server-plugin-audit>=1.0.0b17",
|
|
27
28
|
"fastapi>=0.100.0",
|
|
28
29
|
]
|
|
29
30
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""Qodalis CLI users plugin — user management, authentication, permissions,
|
|
2
|
-
sessions, groups and audit
|
|
2
|
+
sessions, groups and API tokens. Its events are recorded in the audit plugin
|
|
3
|
+
(``qodalis_cli_audit``), which it mounts; ``AuditEntry``, ``AuditQueryOptions``,
|
|
4
|
+
``IAuditStore`` and ``InMemoryAuditStore`` are re-exported from there."""
|
|
3
5
|
|
|
4
6
|
from __future__ import annotations
|
|
5
7
|
|
|
@@ -8,8 +8,8 @@ Usage::
|
|
|
8
8
|
.set_session_timeout(30 * 60 * 1000)
|
|
9
9
|
.build()
|
|
10
10
|
)
|
|
11
|
-
# Protects every server route/socket
|
|
12
|
-
#
|
|
11
|
+
# Protects every server route/socket, scopes user events to the user's
|
|
12
|
+
# own event sockets and mounts the audit plugin (its events' log).
|
|
13
13
|
result.mount_plugin(users_plugin)
|
|
14
14
|
|
|
15
15
|
# in the app lifespan / startup hook (running event loop):
|
|
@@ -25,14 +25,16 @@ import logging
|
|
|
25
25
|
from dataclasses import dataclass, field
|
|
26
26
|
from typing import Any, Optional
|
|
27
27
|
|
|
28
|
-
from fastapi import APIRouter
|
|
28
|
+
from fastapi import APIRouter, Request
|
|
29
|
+
|
|
30
|
+
from qodalis_cli_audit import CliAuditBuilder, CliAuditPlugin
|
|
29
31
|
|
|
30
|
-
from .controllers.audit_controller import create_audit_router
|
|
31
32
|
from .controllers.groups_controller import create_groups_router
|
|
32
33
|
from .controllers.sessions_controller import create_sessions_router
|
|
33
34
|
from .controllers.tokens_controller import create_tokens_router
|
|
34
35
|
from .controllers.users_controller import create_users_router
|
|
35
36
|
from .middleware.auth_middleware import create_auth_dependencies
|
|
37
|
+
from .errors import UsersApiError
|
|
36
38
|
from .models import ExternalAuthAdapter
|
|
37
39
|
from .server_protection import ServerProtectionMiddleware, _normalize_base
|
|
38
40
|
from .shell_access import UsersShellAccess
|
|
@@ -50,7 +52,6 @@ from .services.auth_service import (
|
|
|
50
52
|
)
|
|
51
53
|
from .services.users_service import UsersService
|
|
52
54
|
from .stores.in_memory_api_token_store import InMemoryApiTokenStore
|
|
53
|
-
from .stores.in_memory_audit_store import InMemoryAuditStore
|
|
54
55
|
from .stores.in_memory_credential_store import InMemoryCredentialStore
|
|
55
56
|
from .stores.in_memory_group_store import InMemoryGroupStore
|
|
56
57
|
from .stores.in_memory_session_store import InMemorySessionStore
|
|
@@ -112,6 +113,9 @@ class CliUsersPlugin:
|
|
|
112
113
|
_session_sockets: list = field(default_factory=list, repr=False)
|
|
113
114
|
_session_check_interval: Optional[float] = field(default=None, repr=False)
|
|
114
115
|
_external_check_task: Optional[asyncio.Task] = field(default=None, repr=False)
|
|
116
|
+
#: The audit plugin the users plugin records its events in, mounted with
|
|
117
|
+
#: it (or joining the audit plugin the host mounted, see :meth:`on_mount`).
|
|
118
|
+
audit_plugin: Optional[CliAuditPlugin] = None
|
|
115
119
|
_seeded: bool = field(default=False, repr=False)
|
|
116
120
|
_seed_lock: Optional[asyncio.Lock] = field(default=None, repr=False)
|
|
117
121
|
|
|
@@ -200,6 +204,30 @@ class CliUsersPlugin:
|
|
|
200
204
|
set_shell_provider = getattr(server, "set_shell_access_provider", None)
|
|
201
205
|
if callable(set_shell_provider):
|
|
202
206
|
set_shell_provider(UsersShellAccess(self.auth_service))
|
|
207
|
+
self._mount_audit(server)
|
|
208
|
+
|
|
209
|
+
def _mount_audit(self, server: Any) -> None:
|
|
210
|
+
"""Mount the audit plugin with the users plugin, so a host that only
|
|
211
|
+
mounts the users plugin keeps its audit log. When the host mounted an
|
|
212
|
+
audit plugin of its own, this one joins it: one log, in the store set
|
|
213
|
+
on either side (the default in-memory store otherwise). Without server
|
|
214
|
+
protection the users plugin still guards the audit API (``audit:read``
|
|
215
|
+
/ ``audit:clear``)."""
|
|
216
|
+
audit = self.audit_plugin
|
|
217
|
+
mount = getattr(server, "mount_plugin", None)
|
|
218
|
+
if audit is None or not callable(mount):
|
|
219
|
+
return
|
|
220
|
+
if not self.protect_server and audit.authorize is None:
|
|
221
|
+
audit.authorize = self.authorize_audit
|
|
222
|
+
mount(audit)
|
|
223
|
+
|
|
224
|
+
async def authorize_audit(self, request: Request, permission: str) -> Any:
|
|
225
|
+
"""The users plugin's guard of the audit API (a session or token with
|
|
226
|
+
``permission``), used when it does not protect the whole server."""
|
|
227
|
+
ctx = await self.require_auth(request)
|
|
228
|
+
if not ctx.can(permission):
|
|
229
|
+
raise UsersApiError(403, f"missing permission: {permission}")
|
|
230
|
+
return ctx
|
|
203
231
|
|
|
204
232
|
@property
|
|
205
233
|
def external_socket_check_interval(self) -> float:
|
|
@@ -324,6 +352,8 @@ class CliUsersBuilder:
|
|
|
324
352
|
return self
|
|
325
353
|
|
|
326
354
|
def set_audit_store(self, store: IAuditStore) -> CliUsersBuilder:
|
|
355
|
+
"""Keep the audit log in ``store``: the same as setting the store of
|
|
356
|
+
the audit plugin (``CliAuditBuilder().set_store(store)``)."""
|
|
327
357
|
self._audit_store = store
|
|
328
358
|
return self
|
|
329
359
|
|
|
@@ -438,11 +468,15 @@ class CliUsersBuilder:
|
|
|
438
468
|
def build(self, broadcast_fn: BroadcastFn | None = None) -> CliUsersPlugin:
|
|
439
469
|
"""Build the plugin. ``broadcast_fn`` receives JSON-encoded events
|
|
440
470
|
(e.g. ``session-kicked``); it may be sync or async."""
|
|
471
|
+
audit_builder = CliAuditBuilder()
|
|
472
|
+
if self._audit_store is not None:
|
|
473
|
+
audit_builder.set_store(self._audit_store)
|
|
474
|
+
audit_plugin = audit_builder.build()
|
|
441
475
|
auth_service = AuthService(
|
|
442
476
|
self._user_store or InMemoryUserStore(),
|
|
443
477
|
self._session_store or InMemorySessionStore(),
|
|
444
478
|
self._credential_store or InMemoryCredentialStore(),
|
|
445
|
-
|
|
479
|
+
audit_plugin.store,
|
|
446
480
|
self._group_store or InMemoryGroupStore(),
|
|
447
481
|
options=AuthServiceOptions(
|
|
448
482
|
session_timeout_ms=self._session_timeout_ms,
|
|
@@ -479,8 +513,17 @@ class CliUsersBuilder:
|
|
|
479
513
|
default_admin_password=self._default_admin_password,
|
|
480
514
|
cleanup_interval_ms=self._cleanup_interval_ms,
|
|
481
515
|
protect_server=self._protect_server,
|
|
516
|
+
audit_plugin=audit_plugin,
|
|
482
517
|
)
|
|
483
518
|
|
|
519
|
+
# The users plugin records its events in the audit plugin's store,
|
|
520
|
+
# also after it joined the host's audit plugin (another store).
|
|
521
|
+
def follow_audit_store(store: IAuditStore) -> None:
|
|
522
|
+
auth_service.audit_store = store
|
|
523
|
+
plugin.audit_store = store
|
|
524
|
+
|
|
525
|
+
audit_plugin.on_store_changed(follow_audit_store)
|
|
526
|
+
|
|
484
527
|
# Requests made before startup() (e.g. apps without a lifespan) still
|
|
485
528
|
# see the seeded built-ins.
|
|
486
529
|
get_auth, require_permission, get_auth_pending = create_auth_dependencies(
|
|
@@ -499,6 +542,5 @@ class CliUsersBuilder:
|
|
|
499
542
|
router.include_router(create_groups_router(
|
|
500
543
|
auth_service, users_service, get_auth, require_permission,
|
|
501
544
|
))
|
|
502
|
-
router.include_router(create_audit_router(auth_service, require_permission))
|
|
503
545
|
router.include_router(create_tokens_router(auth_service, get_auth))
|
|
504
546
|
return plugin
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
"""Users plugin controllers."""
|
|
2
2
|
|
|
3
|
-
from .audit_controller import create_audit_router
|
|
4
3
|
from .groups_controller import create_groups_router
|
|
5
4
|
from .sessions_controller import create_sessions_router
|
|
6
5
|
from .tokens_controller import create_tokens_router
|
|
7
6
|
from .users_controller import create_users_router
|
|
8
7
|
|
|
9
8
|
__all__ = [
|
|
10
|
-
"create_audit_router",
|
|
11
9
|
"create_groups_router",
|
|
12
10
|
"create_sessions_router",
|
|
13
11
|
"create_tokens_router",
|
|
@@ -17,6 +17,10 @@ if TYPE_CHECKING:
|
|
|
17
17
|
|
|
18
18
|
from pydantic import BaseModel, ConfigDict
|
|
19
19
|
|
|
20
|
+
# The audit contract lives in the abstractions package (re-exported here for
|
|
21
|
+
# code written against earlier versions of the users plugin).
|
|
22
|
+
from qodalis_cli_server_abstractions.audit import AuditEntry, AuditQueryOptions
|
|
23
|
+
|
|
20
24
|
|
|
21
25
|
def now_ms() -> int:
|
|
22
26
|
"""Current time in epoch milliseconds."""
|
|
@@ -182,60 +186,6 @@ class ApiToken:
|
|
|
182
186
|
return result
|
|
183
187
|
|
|
184
188
|
|
|
185
|
-
@dataclass
|
|
186
|
-
class AuditEntry:
|
|
187
|
-
id: str = field(default_factory=_new_id)
|
|
188
|
-
timestamp: int = field(default_factory=now_ms)
|
|
189
|
-
event: str = ""
|
|
190
|
-
username: str = ""
|
|
191
|
-
details: str | None = None
|
|
192
|
-
actor: str | None = None
|
|
193
|
-
metadata: dict[str, Any] | None = None
|
|
194
|
-
user_id: str | None = None
|
|
195
|
-
actor_id: str | None = None
|
|
196
|
-
#: What the event is about when it is not a user: a group
|
|
197
|
-
#: (``group:admins``), a job, or a host's own object such as a role.
|
|
198
|
-
target: str | None = None
|
|
199
|
-
#: The host's own values for this entry (e.g. the old and new value of a
|
|
200
|
-
#: setting), returned by the audit API. Values must serialize to JSON.
|
|
201
|
-
properties: dict[str, Any] | None = None
|
|
202
|
-
|
|
203
|
-
def to_dict(self) -> dict[str, Any]:
|
|
204
|
-
result: dict[str, Any] = {
|
|
205
|
-
"id": self.id,
|
|
206
|
-
"timestamp": self.timestamp,
|
|
207
|
-
"event": self.event,
|
|
208
|
-
"username": self.username,
|
|
209
|
-
}
|
|
210
|
-
if self.user_id is not None:
|
|
211
|
-
result["userId"] = self.user_id
|
|
212
|
-
if self.actor_id is not None:
|
|
213
|
-
result["actorId"] = self.actor_id
|
|
214
|
-
if self.details is not None:
|
|
215
|
-
result["details"] = self.details
|
|
216
|
-
if self.actor is not None:
|
|
217
|
-
result["actor"] = self.actor
|
|
218
|
-
if self.target is not None:
|
|
219
|
-
result["target"] = self.target
|
|
220
|
-
if self.properties is not None:
|
|
221
|
-
result["properties"] = self.properties
|
|
222
|
-
if self.metadata is not None:
|
|
223
|
-
result["metadata"] = self.metadata
|
|
224
|
-
return result
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
@dataclass
|
|
228
|
-
class AuditQueryOptions:
|
|
229
|
-
limit: int | None = None
|
|
230
|
-
event: str | None = None
|
|
231
|
-
#: The user the events are about (exact, case-insensitive).
|
|
232
|
-
username: str | None = None
|
|
233
|
-
#: The user who performed the actions (exact, case-insensitive).
|
|
234
|
-
actor: str | None = None
|
|
235
|
-
#: Entries to skip, for paging (newest first).
|
|
236
|
-
offset: int | None = None
|
|
237
|
-
|
|
238
|
-
|
|
239
189
|
@dataclass
|
|
240
190
|
class Group:
|
|
241
191
|
id: str = field(default_factory=_new_id)
|
|
@@ -40,7 +40,7 @@ PUBLIC_PATHS: list[tuple[re.Pattern[str], frozenset[str] | None]] = [
|
|
|
40
40
|
|
|
41
41
|
#: Routes of the users plugin itself — they authenticate in their own
|
|
42
42
|
#: dependencies (with CSRF and password-change gating).
|
|
43
|
-
SELF_PROTECTED = re.compile(r"^/api/v1/qcli/(sessions|users|groups|
|
|
43
|
+
SELF_PROTECTED = re.compile(r"^/api/v1/qcli/(sessions|users|groups|tokens)(/.*)?$")
|
|
44
44
|
|
|
45
45
|
#: HTTP permission rules, first match wins: (path, methods or None, permission).
|
|
46
46
|
HTTP_RULES: list[Rule] = [
|
|
@@ -49,6 +49,9 @@ HTTP_RULES: list[Rule] = [
|
|
|
49
49
|
(re.compile(r"^/api/(v1/)?qcli/data-explorer(/.*)?$"), None, "data:query"),
|
|
50
50
|
(re.compile(r"^/api/(v1/)?qcli/jobs(/.*)?$"), frozenset({"GET", "HEAD"}), "jobs:read"),
|
|
51
51
|
(re.compile(r"^/api/(v1/)?qcli/jobs(/.*)?$"), None, "jobs:manage"),
|
|
52
|
+
# Audit plugin (the users plugin mounts it).
|
|
53
|
+
(re.compile(r"^/api/v1/qcli/audit/?$"), frozenset({"GET", "HEAD"}), "audit:read"),
|
|
54
|
+
(re.compile(r"^/api/v1/qcli/audit/?$"), None, "audit:clear"),
|
|
52
55
|
(re.compile(r"^/api/(v1/)?qcli/execute(/stream)?/?$"), None, "commands:execute"),
|
|
53
56
|
# MCP endpoint (tools run commands; each tool also needs the processor's
|
|
54
57
|
# required permissions).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""The in-memory audit store moved to the audit plugin
|
|
2
|
+
(``qodalis_cli_audit``); re-exported here for earlier imports."""
|
|
3
|
+
|
|
4
|
+
from qodalis_cli_audit.in_memory_audit_store import (
|
|
5
|
+
FAILED_LOGIN_EVENT,
|
|
6
|
+
MAX_ENTRIES,
|
|
7
|
+
MAX_FAILED_LOGIN_ENTRIES,
|
|
8
|
+
InMemoryAuditStore,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"FAILED_LOGIN_EVENT",
|
|
13
|
+
"MAX_ENTRIES",
|
|
14
|
+
"MAX_FAILED_LOGIN_ENTRIES",
|
|
15
|
+
"InMemoryAuditStore",
|
|
16
|
+
]
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
"""Abstract store interfaces for the users plugin.
|
|
2
2
|
|
|
3
|
-
Implement these to persist users, sessions, credentials, groups and
|
|
4
|
-
|
|
5
|
-
protections) live in the plugin; stores are plain persistence.
|
|
3
|
+
Implement these to persist users, sessions, credentials, groups and API
|
|
4
|
+
tokens in your own storage. All business rules (permissions, cascades,
|
|
5
|
+
protections) live in the plugin; stores are plain persistence. The audit
|
|
6
|
+
log's ``IAuditStore`` is in ``qodalis_cli_server_abstractions.audit`` (the
|
|
7
|
+
audit plugin keeps the log), re-exported here.
|
|
6
8
|
"""
|
|
7
9
|
|
|
8
10
|
from __future__ import annotations
|
|
9
11
|
|
|
10
12
|
from abc import ABC, abstractmethod
|
|
13
|
+
|
|
14
|
+
# The audit store contract lives in the abstractions package (re-exported).
|
|
15
|
+
from qodalis_cli_server_abstractions.audit import IAuditStore
|
|
16
|
+
|
|
11
17
|
from ..models import (
|
|
12
18
|
ApiToken,
|
|
13
|
-
AuditEntry,
|
|
14
|
-
AuditQueryOptions,
|
|
15
19
|
Credentials,
|
|
16
20
|
Group,
|
|
17
21
|
Session,
|
|
@@ -146,28 +150,6 @@ class ICredentialStore(ABC):
|
|
|
146
150
|
...
|
|
147
151
|
|
|
148
152
|
|
|
149
|
-
class IAuditStore(ABC):
|
|
150
|
-
"""Persistence interface for audit log entries."""
|
|
151
|
-
|
|
152
|
-
@abstractmethod
|
|
153
|
-
async def log(self, entry: AuditEntry) -> None:
|
|
154
|
-
...
|
|
155
|
-
|
|
156
|
-
@abstractmethod
|
|
157
|
-
async def get_entries(self, options: AuditQueryOptions | None = None) -> list[AuditEntry]:
|
|
158
|
-
"""Entries newest-first, filtered by ``options``."""
|
|
159
|
-
|
|
160
|
-
@abstractmethod
|
|
161
|
-
async def clear(self) -> None:
|
|
162
|
-
...
|
|
163
|
-
|
|
164
|
-
async def prune(self, before: int) -> int:
|
|
165
|
-
"""Deletes the entries recorded before ``before`` (Unix time in ms);
|
|
166
|
-
returns how many. Run it on a schedule to keep the log bounded. The
|
|
167
|
-
default keeps everything and returns 0."""
|
|
168
|
-
return 0
|
|
169
|
-
|
|
170
|
-
|
|
171
153
|
class IGroupStore(ABC):
|
|
172
154
|
"""Persistence interface for user groups. Lookups are case-insensitive."""
|
|
173
155
|
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"""Audit controller — ``/audit``."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from typing import Callable
|
|
6
|
-
|
|
7
|
-
from fastapi import APIRouter, Depends, Query, Response
|
|
8
|
-
|
|
9
|
-
from ..models import AuditQueryOptions
|
|
10
|
-
from ..permissions import AUDIT_CLEAR, AUDIT_READ
|
|
11
|
-
from ..services.auth_service import AuthContext, AuthService
|
|
12
|
-
from ._routing import collection, new_router
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
#: Audit entries returned when the request names no limit.
|
|
16
|
-
DEFAULT_AUDIT_LIMIT = 100
|
|
17
|
-
|
|
18
|
-
#: The most audit entries one request returns.
|
|
19
|
-
MAX_AUDIT_LIMIT = 1000
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def create_audit_router(
|
|
23
|
-
auth_service: AuthService,
|
|
24
|
-
require_permission: Callable[[str], Callable],
|
|
25
|
-
) -> APIRouter:
|
|
26
|
-
router = new_router("/audit", "audit")
|
|
27
|
-
audit_store = auth_service.audit_store
|
|
28
|
-
|
|
29
|
-
@collection(router, "GET")
|
|
30
|
-
async def list_entries(
|
|
31
|
-
event: str | None = None,
|
|
32
|
-
username: str | None = None,
|
|
33
|
-
actor: str | None = None,
|
|
34
|
-
limit: int | None = Query(default=None, ge=0),
|
|
35
|
-
offset: int | None = Query(default=None, ge=0),
|
|
36
|
-
ctx: AuthContext = Depends(require_permission(AUDIT_READ)),
|
|
37
|
-
):
|
|
38
|
-
"""Audit entries, newest first (``?event=&username=&actor=&limit=&offset=``):
|
|
39
|
-
the newest 100 without a limit, at most 1000, so a database store never
|
|
40
|
-
loads the whole log for one request."""
|
|
41
|
-
entries = await audit_store.get_entries(
|
|
42
|
-
AuditQueryOptions(
|
|
43
|
-
event=event or None,
|
|
44
|
-
username=username or None,
|
|
45
|
-
actor=actor or None,
|
|
46
|
-
limit=DEFAULT_AUDIT_LIMIT if limit is None else min(limit, MAX_AUDIT_LIMIT),
|
|
47
|
-
offset=offset or None,
|
|
48
|
-
),
|
|
49
|
-
)
|
|
50
|
-
return [e.to_dict() for e in entries]
|
|
51
|
-
|
|
52
|
-
@collection(router, "DELETE", status_code=204)
|
|
53
|
-
async def clear_entries(ctx: AuthContext = Depends(require_permission(AUDIT_CLEAR))):
|
|
54
|
-
await audit_store.clear()
|
|
55
|
-
await auth_service.audit("audit_cleared", ctx.user.name, actor=ctx.user.name)
|
|
56
|
-
return Response(status_code=204)
|
|
57
|
-
|
|
58
|
-
return router
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"""In-memory implementation of IAuditStore.
|
|
2
|
-
|
|
3
|
-
Keeps the last 1000 entries, with failed logins capped separately (last 200)
|
|
4
|
-
so a flood of failed attempts cannot evict other history.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
from __future__ import annotations
|
|
8
|
-
|
|
9
|
-
import itertools
|
|
10
|
-
from collections import deque
|
|
11
|
-
|
|
12
|
-
from ..models import AuditEntry, AuditQueryOptions
|
|
13
|
-
from .interfaces import IAuditStore
|
|
14
|
-
|
|
15
|
-
MAX_ENTRIES = 1000
|
|
16
|
-
MAX_FAILED_LOGIN_ENTRIES = 200
|
|
17
|
-
FAILED_LOGIN_EVENT = "login_failed"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class InMemoryAuditStore(IAuditStore):
|
|
21
|
-
def __init__(
|
|
22
|
-
self,
|
|
23
|
-
max_entries: int = MAX_ENTRIES,
|
|
24
|
-
max_failed_login_entries: int = MAX_FAILED_LOGIN_ENTRIES,
|
|
25
|
-
) -> None:
|
|
26
|
-
self._seq = itertools.count()
|
|
27
|
-
self._entries: deque[tuple[int, AuditEntry]] = deque(maxlen=max_entries)
|
|
28
|
-
self._failed: deque[tuple[int, AuditEntry]] = deque(maxlen=max_failed_login_entries)
|
|
29
|
-
|
|
30
|
-
async def log(self, entry: AuditEntry) -> None:
|
|
31
|
-
target = self._failed if entry.event == FAILED_LOGIN_EVENT else self._entries
|
|
32
|
-
target.append((next(self._seq), entry))
|
|
33
|
-
|
|
34
|
-
async def get_entries(self, options: AuditQueryOptions | None = None) -> list[AuditEntry]:
|
|
35
|
-
merged = sorted([*self._entries, *self._failed], key=lambda x: x[0], reverse=True)
|
|
36
|
-
result = [e for _, e in merged] # newest first
|
|
37
|
-
if options:
|
|
38
|
-
if options.event:
|
|
39
|
-
ev = options.event.lower()
|
|
40
|
-
result = [e for e in result if e.event.lower() == ev]
|
|
41
|
-
if options.username:
|
|
42
|
-
un = options.username.lower()
|
|
43
|
-
result = [e for e in result if e.username.lower() == un]
|
|
44
|
-
if options.actor:
|
|
45
|
-
actor = options.actor.lower()
|
|
46
|
-
result = [e for e in result if (e.actor or "").lower() == actor]
|
|
47
|
-
if options.offset is not None and options.offset > 0:
|
|
48
|
-
result = result[options.offset:]
|
|
49
|
-
if options.limit is not None and options.limit > 0:
|
|
50
|
-
result = result[: options.limit]
|
|
51
|
-
return result
|
|
52
|
-
|
|
53
|
-
async def prune(self, before: int) -> int:
|
|
54
|
-
removed = 0
|
|
55
|
-
for entries in (self._entries, self._failed):
|
|
56
|
-
kept = [item for item in entries if item[1].timestamp >= before]
|
|
57
|
-
removed += len(entries) - len(kept)
|
|
58
|
-
entries.clear()
|
|
59
|
-
entries.extend(kept)
|
|
60
|
-
return removed
|
|
61
|
-
|
|
62
|
-
async def clear(self) -> None:
|
|
63
|
-
self._entries.clear()
|
|
64
|
-
self._failed.clear()
|
{qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/.gitignore
RENAMED
|
File without changes
|
{qodalis_cli_server_plugin_users-1.0.0b15 → qodalis_cli_server_plugin_users-1.0.0b19}/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|