Source code for lomas_core.models.config

from typing import Annotated, Self

from pydantic import (
    BaseModel,
    Field,
    HttpUrl,
    UrlConstraints,
    model_validator,
)

from lomas_core.models.constants import (
    TimeAttackMethod,
)


[docs] class TimeAttack(BaseModel): """BaseModel for configs to prevent timing attacks.""" method: TimeAttackMethod magnitude: float
[docs] class Telemetry(BaseModel): """Telemetry config.""" enabled: bool service_name: Annotated[str | None, Field(default="lomas-server-app")] service_id: Annotated[str | None, Field(default="default-host")] collector_endpoint: Annotated[HttpUrl | None, UrlConstraints(default_port=4317)] = None collector_insecure: Annotated[bool | None, Field(default=False)] collector_log_correlation: Annotated[bool | None, Field(default=False)]
[docs] @model_validator(mode="after") def options_set_if_enabled(self) -> Self: """Ensures that if enabled is set to True, all other fields are specified. Raises: ValueError: If any of the fields is not specified while enabled is True. """ if self.enabled: missing = [k for k, v in dict(self).items() if v is None] if len(missing) > 0: raise ValueError( f"If enabled set to True, all other fields must be specified. Missing fields: {missing}" ) return self