nebulabroadcast-core 6.1.6__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.
- nebulabroadcast_core-6.1.6/PKG-INFO +89 -0
- nebulabroadcast_core-6.1.6/README.md +64 -0
- nebulabroadcast_core-6.1.6/nebula/__init__.py +91 -0
- nebulabroadcast_core-6.1.6/nebula/__main__.py +8 -0
- nebulabroadcast_core-6.1.6/nebula/common.py +64 -0
- nebulabroadcast_core-6.1.6/nebula/config.py +115 -0
- nebulabroadcast_core-6.1.6/nebula/context.py +112 -0
- nebulabroadcast_core-6.1.6/nebula/enum.py +184 -0
- nebulabroadcast_core-6.1.6/nebula/exceptions.py +90 -0
- nebulabroadcast_core-6.1.6/nebula/filetypes.py +80 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/__init__.py +0 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/aux.py +62 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/avatars.py +117 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/create_new_event.py +130 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/email.py +126 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/fulltext.py +16 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/scheduling.py +159 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/tokens.py +105 -0
- nebulabroadcast_core-6.1.6/nebula/helpers/validators.py +45 -0
- nebulabroadcast_core-6.1.6/nebula/messaging.py +24 -0
- nebulabroadcast_core-6.1.6/nebula/metadata/__init__.py +0 -0
- nebulabroadcast_core-6.1.6/nebula/metadata/format.py +72 -0
- nebulabroadcast_core-6.1.6/nebula/metadata/normalize.py +105 -0
- nebulabroadcast_core-6.1.6/nebula/metadata/utils.py +62 -0
- nebulabroadcast_core-6.1.6/nebula/objects/__init__.py +0 -0
- nebulabroadcast_core-6.1.6/nebula/objects/asset/__init__.py +288 -0
- nebulabroadcast_core-6.1.6/nebula/objects/asset/access.py +46 -0
- nebulabroadcast_core-6.1.6/nebula/objects/asset/changes.py +79 -0
- nebulabroadcast_core-6.1.6/nebula/objects/asset/keys.py +37 -0
- nebulabroadcast_core-6.1.6/nebula/objects/asset/query.py +223 -0
- nebulabroadcast_core-6.1.6/nebula/objects/base.py +310 -0
- nebulabroadcast_core-6.1.6/nebula/objects/bin/__init__.py +52 -0
- nebulabroadcast_core-6.1.6/nebula/objects/event/__init__.py +25 -0
- nebulabroadcast_core-6.1.6/nebula/objects/item/__init__.py +76 -0
- nebulabroadcast_core-6.1.6/nebula/objects/user/__init__.py +348 -0
- nebulabroadcast_core-6.1.6/nebula/objects/utils.py +21 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/__init__.py +7 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/cli.py +5 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/common.py +14 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/frontend.py +85 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/library.py +154 -0
- nebulabroadcast_core-6.1.6/nebula/plugins/solver.py +239 -0
- nebulabroadcast_core-6.1.6/nebula/py.typed +0 -0
- nebulabroadcast_core-6.1.6/nebula/query/__init__.py +50 -0
- nebulabroadcast_core-6.1.6/nebula/query/cursor.py +48 -0
- nebulabroadcast_core-6.1.6/nebula/query/fields.py +126 -0
- nebulabroadcast_core-6.1.6/nebula/query/models.py +101 -0
- nebulabroadcast_core-6.1.6/nebula/query/query.py +395 -0
- nebulabroadcast_core-6.1.6/nebula/settings/__init__.py +113 -0
- nebulabroadcast_core-6.1.6/nebula/settings/common.py +10 -0
- nebulabroadcast_core-6.1.6/nebula/settings/metatypes.py +79 -0
- nebulabroadcast_core-6.1.6/nebula/settings/models.py +959 -0
- nebulabroadcast_core-6.1.6/nebula/sql.py +66 -0
- nebulabroadcast_core-6.1.6/nebula/storages.py +114 -0
- nebulabroadcast_core-6.1.6/nebula/templates/email/base.jinja2 +47 -0
- nebulabroadcast_core-6.1.6/nebula/templates/email/components.jinja2 +130 -0
- nebulabroadcast_core-6.1.6/nebula/templates/email/password-reset.jinja2 +19 -0
- nebulabroadcast_core-6.1.6/nebula/templates/email/user-invite.jinja2 +34 -0
- nebulabroadcast_core-6.1.6/nebula/transcription.py +27 -0
- nebulabroadcast_core-6.1.6/nebula/utils/__init__.py +54 -0
- nebulabroadcast_core-6.1.6/nebula/utils/hashing.py +25 -0
- nebulabroadcast_core-6.1.6/nebula/utils/strings.py +196 -0
- nebulabroadcast_core-6.1.6/nebula/utils/timeutils.py +189 -0
- nebulabroadcast_core-6.1.6/nebula/utils/xml.py +15 -0
- nebulabroadcast_core-6.1.6/nebula/version.py +1 -0
- nebulabroadcast_core-6.1.6/pyproject.toml +145 -0
- nebulabroadcast_core-6.1.6/pyproject.toml.orig +149 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nebulabroadcast-core
|
|
3
|
+
Version: 6.1.6
|
|
4
|
+
Summary: Async domain library of the Nebula broadcast automation system
|
|
5
|
+
Author: Nebula Broadcast
|
|
6
|
+
Author-email: Nebula Broadcast <info@nebulabroadcast.com>
|
|
7
|
+
License-Expression: GPL-3.0-or-later
|
|
8
|
+
Classifier: Framework :: AsyncIO
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
10
|
+
Classifier: Typing :: Typed
|
|
11
|
+
Requires-Dist: aiofiles>=25.1.0
|
|
12
|
+
Requires-Dist: asyncpg>=0.31.0
|
|
13
|
+
Requires-Dist: httpx2>=2.16.0
|
|
14
|
+
Requires-Dist: jinja2>=3.1.6
|
|
15
|
+
Requires-Dist: mistune>=3.2.0
|
|
16
|
+
Requires-Dist: nx-framework[postgres,redis]>=0.0.9
|
|
17
|
+
Requires-Dist: pydantic>=2.12.5
|
|
18
|
+
Requires-Dist: python-dotenv>=1.2.1
|
|
19
|
+
Requires-Dist: unidecode>=1.4.0
|
|
20
|
+
Requires-Python: >=3.14
|
|
21
|
+
Project-URL: Homepage, https://nebulabroadcast.com
|
|
22
|
+
Project-URL: Documentation, https://docs.nebulabroadcast.com
|
|
23
|
+
Project-URL: Source, https://github.com/nebulabroadcast/nebula
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# nebulabroadcast-core
|
|
27
|
+
|
|
28
|
+
The async domain library of [Nebula](https://nebulabroadcast.com), the open
|
|
29
|
+
source broadcast automation system: assets, rundown objects, users,
|
|
30
|
+
settings, queries, messaging, database and redis access. It is the
|
|
31
|
+
`nebula` package the Nebula server is built on, published so that
|
|
32
|
+
stand-alone services can work with a Nebula site the same way the server
|
|
33
|
+
does.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
pip install nebulabroadcast-core==6.1.6
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Use the version of the server your site runs: the package is released
|
|
40
|
+
with every server release and they share the version number.
|
|
41
|
+
|
|
42
|
+
## Example
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import nebula
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def main() -> None:
|
|
49
|
+
async for asset in nebula.Asset.iterate(
|
|
50
|
+
filter={"key": "id_folder", "op": "in", "value": [1]},
|
|
51
|
+
sort=["title"],
|
|
52
|
+
):
|
|
53
|
+
print(asset.id, asset["title"])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
nebula.run(main())
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`nebula.run()` loads the site settings from the database and runs the
|
|
60
|
+
coroutine. Code run this way acts as the system: no user, no access
|
|
61
|
+
restrictions.
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
The same environment variables as the server (a `.env` file is read too):
|
|
66
|
+
|
|
67
|
+
| Variable | Default |
|
|
68
|
+
|---|---|
|
|
69
|
+
| `NEBULA_POSTGRES_URL` | `postgresql://nebula:nebula@postgres:5432/nebula` |
|
|
70
|
+
| `NEBULA_REDIS_URL` | `redis://redis:6379/0` |
|
|
71
|
+
| `NEBULA_SITE_NAME` | `nebula` |
|
|
72
|
+
| `NEBULA_LOG_LEVEL` | `DEBUG` |
|
|
73
|
+
|
|
74
|
+
## Supported API
|
|
75
|
+
|
|
76
|
+
The names exported by `nebula` itself (`nebula.Asset`, `nebula.User`,
|
|
77
|
+
`nebula.Item`, `nebula.Bin`, `nebula.Event`, `nebula.settings`,
|
|
78
|
+
`nebula.db`, `nebula.redis`, `nebula.msg`, `nebula.run`, the exceptions...)
|
|
79
|
+
and `nebula.query`. Everything else (`nebula.helpers`, submodules) is
|
|
80
|
+
internal and may change in any release.
|
|
81
|
+
|
|
82
|
+
This is not the synchronous `nebula` package of
|
|
83
|
+
[nebula-worker](https://github.com/nebulabroadcast/nebula-worker). Both
|
|
84
|
+
are imported as `nebula`, so they can't be installed in the same
|
|
85
|
+
environment.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
GPL-3.0-or-later
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# nebulabroadcast-core
|
|
2
|
+
|
|
3
|
+
The async domain library of [Nebula](https://nebulabroadcast.com), the open
|
|
4
|
+
source broadcast automation system: assets, rundown objects, users,
|
|
5
|
+
settings, queries, messaging, database and redis access. It is the
|
|
6
|
+
`nebula` package the Nebula server is built on, published so that
|
|
7
|
+
stand-alone services can work with a Nebula site the same way the server
|
|
8
|
+
does.
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
pip install nebulabroadcast-core==6.1.6
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Use the version of the server your site runs: the package is released
|
|
15
|
+
with every server release and they share the version number.
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import nebula
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def main() -> None:
|
|
24
|
+
async for asset in nebula.Asset.iterate(
|
|
25
|
+
filter={"key": "id_folder", "op": "in", "value": [1]},
|
|
26
|
+
sort=["title"],
|
|
27
|
+
):
|
|
28
|
+
print(asset.id, asset["title"])
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
nebula.run(main())
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`nebula.run()` loads the site settings from the database and runs the
|
|
35
|
+
coroutine. Code run this way acts as the system: no user, no access
|
|
36
|
+
restrictions.
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
The same environment variables as the server (a `.env` file is read too):
|
|
41
|
+
|
|
42
|
+
| Variable | Default |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `NEBULA_POSTGRES_URL` | `postgresql://nebula:nebula@postgres:5432/nebula` |
|
|
45
|
+
| `NEBULA_REDIS_URL` | `redis://redis:6379/0` |
|
|
46
|
+
| `NEBULA_SITE_NAME` | `nebula` |
|
|
47
|
+
| `NEBULA_LOG_LEVEL` | `DEBUG` |
|
|
48
|
+
|
|
49
|
+
## Supported API
|
|
50
|
+
|
|
51
|
+
The names exported by `nebula` itself (`nebula.Asset`, `nebula.User`,
|
|
52
|
+
`nebula.Item`, `nebula.Bin`, `nebula.Event`, `nebula.settings`,
|
|
53
|
+
`nebula.db`, `nebula.redis`, `nebula.msg`, `nebula.run`, the exceptions...)
|
|
54
|
+
and `nebula.query`. Everything else (`nebula.helpers`, submodules) is
|
|
55
|
+
internal and may change in any release.
|
|
56
|
+
|
|
57
|
+
This is not the synchronous `nebula` package of
|
|
58
|
+
[nebula-worker](https://github.com/nebulabroadcast/nebula-worker). Both
|
|
59
|
+
are imported as `nebula`, so they can't be installed in the same
|
|
60
|
+
environment.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
GPL-3.0-or-later
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"DB",
|
|
3
|
+
"Asset",
|
|
4
|
+
"BadGatewayException",
|
|
5
|
+
"BadRequestException",
|
|
6
|
+
"Bin",
|
|
7
|
+
"CLIPlugin",
|
|
8
|
+
"ConflictException",
|
|
9
|
+
"Event",
|
|
10
|
+
"ForbiddenException",
|
|
11
|
+
"Item",
|
|
12
|
+
"LoginFailedException",
|
|
13
|
+
"NebulaException",
|
|
14
|
+
"NotFoundException",
|
|
15
|
+
"NotImplementedException",
|
|
16
|
+
"RequestSettingsReload",
|
|
17
|
+
"Storage",
|
|
18
|
+
"UnauthorizedException",
|
|
19
|
+
"User",
|
|
20
|
+
"ValidationException",
|
|
21
|
+
"__version__",
|
|
22
|
+
"config",
|
|
23
|
+
"context",
|
|
24
|
+
"db",
|
|
25
|
+
"initialize",
|
|
26
|
+
"log",
|
|
27
|
+
"msg",
|
|
28
|
+
"redis",
|
|
29
|
+
"run",
|
|
30
|
+
"settings",
|
|
31
|
+
"storages",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
import asyncio
|
|
35
|
+
from collections.abc import Coroutine
|
|
36
|
+
from typing import Any
|
|
37
|
+
|
|
38
|
+
from nx.db import DB, db
|
|
39
|
+
from nx.logging import LogLevel
|
|
40
|
+
from nx.logging import logger as log
|
|
41
|
+
from nx.redis import redis
|
|
42
|
+
|
|
43
|
+
from . import context
|
|
44
|
+
from .config import config
|
|
45
|
+
from .exceptions import (
|
|
46
|
+
BadGatewayException,
|
|
47
|
+
BadRequestException,
|
|
48
|
+
ConflictException,
|
|
49
|
+
ForbiddenException,
|
|
50
|
+
LoginFailedException,
|
|
51
|
+
NebulaException,
|
|
52
|
+
NotFoundException,
|
|
53
|
+
NotImplementedException,
|
|
54
|
+
RequestSettingsReload,
|
|
55
|
+
UnauthorizedException,
|
|
56
|
+
ValidationException,
|
|
57
|
+
)
|
|
58
|
+
from .messaging import msg
|
|
59
|
+
from .objects.asset import Asset
|
|
60
|
+
from .objects.bin import Bin
|
|
61
|
+
from .objects.event import Event
|
|
62
|
+
from .objects.item import Item
|
|
63
|
+
from .objects.user import User
|
|
64
|
+
from .plugins import CLIPlugin
|
|
65
|
+
from .settings import load_settings, settings
|
|
66
|
+
from .storages import Storage, storages
|
|
67
|
+
from .version import __version__
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def initialize() -> None:
|
|
71
|
+
"""Configure the shared nx singletons (log, redis) with nebula-specific values."""
|
|
72
|
+
log.level = LogLevel[config.log_level.upper()]
|
|
73
|
+
redis.channel = f"nebula-{config.site_name}"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
initialize()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def run(entrypoint: Coroutine[Any, Any, Any]) -> None:
|
|
80
|
+
"""Run a coroutine in the event loop.
|
|
81
|
+
|
|
82
|
+
This function is used to run the main entrypoint of CLI scripts.
|
|
83
|
+
It loads the settings and starts the event loop and runs a
|
|
84
|
+
given entrypoint coroutine.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
async def run_async() -> None:
|
|
88
|
+
await load_settings()
|
|
89
|
+
await entrypoint
|
|
90
|
+
|
|
91
|
+
asyncio.run(run_async())
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import importlib.util
|
|
3
|
+
import inspect
|
|
4
|
+
import json
|
|
5
|
+
import random
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from types import ModuleType
|
|
9
|
+
from typing import Any, Literal, TypeVar, cast
|
|
10
|
+
|
|
11
|
+
T = TypeVar("T", bound=type)
|
|
12
|
+
|
|
13
|
+
SerializableValue = int | float | str | bool | dict[str, Any] | list[Any] | None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def json_loads(data: str | bytes) -> Any:
|
|
17
|
+
"""Load JSON data."""
|
|
18
|
+
return json.loads(data)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def json_dumps(data: SerializableValue) -> str:
|
|
22
|
+
"""Dump JSON data."""
|
|
23
|
+
return json.dumps(data)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def hash_data(data: SerializableValue) -> str:
|
|
27
|
+
"""Create a SHA-256 hash from arbitrary (json-serializable) data."""
|
|
28
|
+
if not isinstance(data, str):
|
|
29
|
+
data = json_dumps(data)
|
|
30
|
+
return hashlib.sha256(data.encode("utf-8")).hexdigest()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def create_hash() -> str:
|
|
34
|
+
"""Create a pseudo-random hash (used as and access token)."""
|
|
35
|
+
return hash_data([time.time(), random.random()]) # noqa: S311
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def sql_list(lst: list[Any], t: Literal["int", "str"] = "int") -> str:
|
|
39
|
+
flist = [f"{val}" for val in lst] if t == "int" else [f"'{val}'" for val in lst]
|
|
40
|
+
return f"({','.join(flist)})"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def import_module(name: str, path: str) -> ModuleType:
|
|
44
|
+
if (spec := importlib.util.spec_from_file_location(name, path)) is None:
|
|
45
|
+
raise ModuleNotFoundError(f"Module {name} not found")
|
|
46
|
+
if (module := importlib.util.module_from_spec(spec)) is None:
|
|
47
|
+
raise ImportError(f"Module {name} cannot be imported")
|
|
48
|
+
if spec.loader is None:
|
|
49
|
+
raise ImportError(f"Module {name} cannot be imported. No loader found.")
|
|
50
|
+
sys.modules[spec.name] = module
|
|
51
|
+
spec.loader.exec_module(module)
|
|
52
|
+
return module
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def classes_from_module[T: type](superclass: T, module: ModuleType) -> list[T]:
|
|
56
|
+
classes: list[T] = []
|
|
57
|
+
for name in dir(module):
|
|
58
|
+
# It could be anything at this point
|
|
59
|
+
obj = getattr(module, name)
|
|
60
|
+
if not inspect.isclass(obj) or obj is superclass:
|
|
61
|
+
continue
|
|
62
|
+
if issubclass(obj, superclass):
|
|
63
|
+
classes.append(cast("T", obj))
|
|
64
|
+
return classes
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
__all__ = ["NebulaConfigModel", "config"]
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import cast
|
|
5
|
+
|
|
6
|
+
import dotenv
|
|
7
|
+
from nx.config import ConfigModel, ConfigProxy
|
|
8
|
+
from pydantic import Field, PostgresDsn
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class NebulaConfigModel(ConfigModel):
|
|
12
|
+
postgres_url: PostgresDsn = PostgresDsn(
|
|
13
|
+
"postgresql://nebula:nebula@postgres:5432/nebula"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
# nx.config.ConfigModel defaults log_stack to True, which appends the
|
|
17
|
+
# caller's module/func/file/line to every single log line. Too verbose
|
|
18
|
+
# for normal operation, so default it off (NEBULA_LOG_STACK=true to
|
|
19
|
+
# re-enable e.g. while debugging).
|
|
20
|
+
log_stack: bool = False
|
|
21
|
+
|
|
22
|
+
site_name: str = Field(
|
|
23
|
+
"nebula",
|
|
24
|
+
description="",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
motd: str = Field(
|
|
28
|
+
"",
|
|
29
|
+
description="Message of the day",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
frontend_dir: str = Field(
|
|
33
|
+
"/frontend",
|
|
34
|
+
description="Path to the frontend directory",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
plugin_dir: str = Field(
|
|
38
|
+
"/plugins",
|
|
39
|
+
description="Path to the plugin directory",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
session_secret: str = Field(
|
|
43
|
+
default_factory=lambda: os.urandom(32).hex(),
|
|
44
|
+
description="Session secret. MUST be set when the server is scaled",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
max_failed_login_attempts: int = Field(
|
|
48
|
+
10,
|
|
49
|
+
description="Maximum number of failed login attempts before the IP is banned",
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
failed_login_ban_time: int = Field(
|
|
53
|
+
1800,
|
|
54
|
+
description="Time in seconds for which the IP is banned "
|
|
55
|
+
"after too many failed login attempts",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
server_url: str | None = Field(
|
|
59
|
+
None,
|
|
60
|
+
description="Public URL of the server (e.g. https://nebula.example.com), "
|
|
61
|
+
"used in links sent by email. Password reset emails need it",
|
|
62
|
+
)
|
|
63
|
+
trusted_proxies: int = Field(
|
|
64
|
+
1,
|
|
65
|
+
ge=0,
|
|
66
|
+
description="Number of reverse proxies in front of the server. "
|
|
67
|
+
"X-Forwarded-For is trusted only from a private address. "
|
|
68
|
+
"0 never trusts it",
|
|
69
|
+
)
|
|
70
|
+
geoip_db_path: str | None = Field(
|
|
71
|
+
None,
|
|
72
|
+
description=(
|
|
73
|
+
"Path to the GeoIP database file. "
|
|
74
|
+
"If not set, geolocation features will be disabled"
|
|
75
|
+
),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
enable_experimental: bool = Field(
|
|
79
|
+
False,
|
|
80
|
+
description="Enable experimental features",
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def postgres(self) -> str:
|
|
85
|
+
"""Deprecated alias for postgres_url."""
|
|
86
|
+
return str(self.postgres_url)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def redis(self) -> str:
|
|
90
|
+
"""Deprecated alias for redis_url."""
|
|
91
|
+
return str(self.redis_url)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Older Nebula deployments set NEBULA_POSTGRES / NEBULA_REDIS as bare
|
|
95
|
+
# connection strings. nx.config.ConfigModel expects NEBULA_POSTGRES_URL /
|
|
96
|
+
# NEBULA_REDIS_URL instead, so alias the old names to keep existing
|
|
97
|
+
# .env files and deployments working unmodified.
|
|
98
|
+
_ENV_ALIASES = {
|
|
99
|
+
"NEBULA_POSTGRES": "NEBULA_POSTGRES_URL",
|
|
100
|
+
"NEBULA_REDIS": "NEBULA_REDIS_URL",
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _load_config() -> NebulaConfigModel:
|
|
105
|
+
dotenv.load_dotenv()
|
|
106
|
+
for old, new in _ENV_ALIASES.items():
|
|
107
|
+
if old in os.environ and new not in os.environ:
|
|
108
|
+
os.environ[new] = os.environ[old]
|
|
109
|
+
|
|
110
|
+
proxy = ConfigProxy[NebulaConfigModel]()
|
|
111
|
+
proxy.initialize(NebulaConfigModel, "NEBULA")
|
|
112
|
+
return cast("NebulaConfigModel", proxy)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
config = _load_config()
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"RequestContext",
|
|
3
|
+
"current_initiator",
|
|
4
|
+
"current_user",
|
|
5
|
+
"get_request_context",
|
|
6
|
+
"is_system",
|
|
7
|
+
"request_context",
|
|
8
|
+
"require_access",
|
|
9
|
+
"set_default_system",
|
|
10
|
+
"system_context",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
import contextlib
|
|
14
|
+
import dataclasses
|
|
15
|
+
from collections.abc import Callable, Iterator
|
|
16
|
+
from contextvars import ContextVar
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from typing import TYPE_CHECKING
|
|
19
|
+
|
|
20
|
+
from nebula.exceptions import ForbiddenException, UnauthorizedException
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from nebula.objects.user import User
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class RequestContext:
|
|
28
|
+
"""Who/what is behind the code currently executing.
|
|
29
|
+
|
|
30
|
+
Set by the server for the duration of a request (see
|
|
31
|
+
server.middleware.context.RequestContextMiddleware) or manually by
|
|
32
|
+
CLI tools and background jobs, which have no request to attach to.
|
|
33
|
+
|
|
34
|
+
`system` marks trusted code (CLI tools, services, server background
|
|
35
|
+
jobs) that skips access control. A context created for a request must
|
|
36
|
+
leave it False.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
user: User | None = None
|
|
40
|
+
initiator: str | None = None
|
|
41
|
+
system: bool = False
|
|
42
|
+
# Token of the session the request is authenticated with, if any
|
|
43
|
+
session_token: str | None = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
_request_context: ContextVar[RequestContext | None] = ContextVar(
|
|
47
|
+
"_request_context", default=None
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Whether code running with no context set is trusted. True for CLI tools
|
|
51
|
+
# and services; the server process turns it off at startup, so anything
|
|
52
|
+
# reaching the server without a context (e.g. a websocket) is untrusted.
|
|
53
|
+
_default_system = True
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def set_default_system(value: bool) -> None:
|
|
57
|
+
"""Set whether code running without a context is trusted (process-wide)."""
|
|
58
|
+
global _default_system # noqa: PLW0603
|
|
59
|
+
_default_system = value
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def get_request_context() -> RequestContext:
|
|
63
|
+
"""Return the current RequestContext, or an empty one if none is set."""
|
|
64
|
+
return _request_context.get() or RequestContext(system=_default_system)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def is_system() -> bool:
|
|
68
|
+
"""Return True if the code currently executing is trusted (skips ACL)."""
|
|
69
|
+
return get_request_context().system
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def current_user() -> User | None:
|
|
73
|
+
"""Return the user behind the code currently executing, if any."""
|
|
74
|
+
return get_request_context().user
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def current_initiator() -> str | None:
|
|
78
|
+
"""Return the client ID of whoever/whatever triggered the current code."""
|
|
79
|
+
return get_request_context().initiator
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@contextlib.contextmanager
|
|
83
|
+
def request_context(context: RequestContext) -> Iterator[None]:
|
|
84
|
+
"""Set the RequestContext for the duration of the `with` block."""
|
|
85
|
+
token = _request_context.set(context)
|
|
86
|
+
try:
|
|
87
|
+
yield
|
|
88
|
+
finally:
|
|
89
|
+
_request_context.reset(token)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@contextlib.contextmanager
|
|
93
|
+
def system_context() -> Iterator[None]:
|
|
94
|
+
"""Run the `with` block as trusted code, keeping user and initiator."""
|
|
95
|
+
context = dataclasses.replace(get_request_context(), system=True)
|
|
96
|
+
with request_context(context):
|
|
97
|
+
yield
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def require_access(allowed: Callable[[User], bool], detail: str) -> None:
|
|
101
|
+
"""Raise unless the acting user passes the check.
|
|
102
|
+
|
|
103
|
+
Trusted (system) code always passes. Without a user, raises 401;
|
|
104
|
+
when the check fails, raises 403 with the given detail.
|
|
105
|
+
"""
|
|
106
|
+
context = get_request_context()
|
|
107
|
+
if context.system:
|
|
108
|
+
return
|
|
109
|
+
if context.user is None:
|
|
110
|
+
raise UnauthorizedException("Authentication required")
|
|
111
|
+
if not allowed(context.user):
|
|
112
|
+
raise ForbiddenException(detail)
|