-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from UpstreamDataInc/dev_settings
- Loading branch information
Showing
14 changed files
with
139 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .const import BASE_DIR, DB_MIGRATIONS_LOC, PWD_CXT, SECRET # noqa: F401 | ||
from .schema import GooseBitSettings | ||
|
||
config = GooseBitSettings() | ||
|
||
USERS = {u.username: u for u in config.users} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import secrets | ||
from pathlib import Path | ||
|
||
from argon2 import PasswordHasher | ||
from joserfc.jwk import OctKey | ||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
DB_MIGRATIONS_LOC = BASE_DIR.joinpath("migrations") | ||
|
||
SECRET = OctKey.import_key(secrets.token_hex(16)) | ||
PWD_CXT = PasswordHasher() | ||
|
||
LOGGING_DEFAULT = { | ||
"version": 1, | ||
"formatters": {"simple": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}}, | ||
"handlers": {"console": {"class": "logging.StreamHandler", "formatter": "simple", "level": "DEBUG"}}, | ||
"loggers": { | ||
"tortoise": {"handlers": ["console"], "level": "WARNING", "propagate": True}, | ||
"aiosqlite": {"handlers": ["console"], "level": "WARNING", "propagate": True}, | ||
"multipart": {"handlers": ["console"], "level": "INFO", "propagate": True}, | ||
}, | ||
"root": {"level": "INFO", "handlers": ["console"]}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from pathlib import Path | ||
from typing import Annotated, Iterable | ||
|
||
from pydantic import BaseModel, BeforeValidator, Field | ||
from pydantic_settings import ( | ||
BaseSettings, | ||
PydanticBaseSettingsSource, | ||
SettingsConfigDict, | ||
YamlConfigSettingsSource, | ||
) | ||
|
||
from goosebit.permissions import Permissions, PermissionsBase | ||
|
||
from .const import BASE_DIR, LOGGING_DEFAULT, PWD_CXT | ||
|
||
|
||
def parse_permissions(items: Iterable[str]): | ||
permissions = set() | ||
for p in items: | ||
permissions.update(Permissions.from_str(p)) | ||
return permissions | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
hashed_pwd: Annotated[str, BeforeValidator(PWD_CXT.hash)] = Field(validation_alias="password") | ||
permissions: Annotated[set[PermissionsBase], BeforeValidator(parse_permissions)] | ||
|
||
def get_json_permissions(self): | ||
return [str(p) for p in self.permissions] | ||
|
||
|
||
class PrometheusSettings(BaseModel): | ||
enable: bool = False | ||
port: int = 9090 | ||
|
||
|
||
class MetricsSettings(BaseModel): | ||
prometheus: PrometheusSettings = PrometheusSettings() | ||
|
||
|
||
class GooseBitSettings(BaseSettings): | ||
model_config = SettingsConfigDict(env_prefix="GOOSEBIT_") | ||
|
||
artifacts_dir: Path = BASE_DIR.joinpath("artifacts") | ||
|
||
tenant: str = "DEFAULT" | ||
|
||
poll_time_default: str = "00:01:00" | ||
poll_time_updating: str = "00:00:05" | ||
poll_time_registration: str = "00:00:10" | ||
|
||
users: list[User] = [] | ||
|
||
db_uri: str = f"sqlite:///{BASE_DIR.joinpath('db.sqlite3')}" | ||
|
||
metrics: MetricsSettings = MetricsSettings() | ||
|
||
logging: dict = LOGGING_DEFAULT | ||
|
||
@classmethod | ||
def settings_customise_sources( | ||
cls, | ||
settings_cls: type[BaseSettings], | ||
init_settings: PydanticBaseSettingsSource, | ||
env_settings: PydanticBaseSettingsSource, | ||
dotenv_settings: PydanticBaseSettingsSource, | ||
file_secret_settings: PydanticBaseSettingsSource, | ||
) -> tuple[PydanticBaseSettingsSource, ...]: | ||
return ( | ||
init_settings, | ||
YamlConfigSettingsSource(settings_cls, BASE_DIR.parent.joinpath("settings.yaml")), | ||
env_settings, | ||
file_secret_settings, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
from opentelemetry.exporter.prometheus import PrometheusMetricReader | ||
from prometheus_client import start_http_server | ||
|
||
from goosebit import settings | ||
|
||
PROMETHEUS_PORT = settings.config.get("metrics", {}).get("prometheus", {}).get("port", 9090) | ||
from goosebit.settings import config | ||
|
||
# separate file to enable it as a feature later. | ||
reader = PrometheusMetricReader() | ||
start_http_server(port=PROMETHEUS_PORT, addr="0.0.0.0") | ||
if config.metrics.prometheus.enable: | ||
start_http_server(port=config.metrics.prometheus.port, addr="0.0.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,13 @@ poll_time_updating: 00:00:05 | |
# "device.read", "device.write", "device.delete" | ||
# "rollout.read", "rollout.write", "rollout.delete" | ||
# "home.read" | ||
|
||
users: | ||
- email: [email protected] | ||
- username: [email protected] | ||
password: admin | ||
permissions: | ||
- "*" | ||
- email: [email protected] | ||
- username: [email protected] | ||
password: ops | ||
permissions: | ||
- "home.read" | ||
|
@@ -33,6 +34,7 @@ poll_time_registration: 00:00:10 | |
tenant: DEFAULT | ||
metrics: | ||
prometheus: | ||
enable: false | ||
port: 9090 | ||
logging: | ||
version: 1 | ||
|