|
| 1 | +import os |
| 2 | +from abc import ABC |
| 3 | +from datetime import timedelta |
| 4 | +from pathlib import Path |
| 5 | +from typing import Annotated, Literal |
| 6 | + |
| 7 | +from pydantic import BaseModel, ConfigDict, Field, HttpUrl, PlainSerializer |
| 8 | +from pydantic_settings import ( |
| 9 | + BaseSettings, |
| 10 | + DotEnvSettingsSource, |
| 11 | + PydanticBaseSettingsSource, |
| 12 | + SettingsConfigDict, |
| 13 | + YamlConfigSettingsSource, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class BaseConfigModel(BaseModel, ABC): |
| 18 | + """Base class for global config models |
| 19 | + To prevent attributes from being modified after initialization. |
| 20 | + """ |
| 21 | + |
| 22 | + model_config = ConfigDict(extra="allow", frozen=True, validate_default=True) |
| 23 | + |
| 24 | + |
| 25 | +class SettingsLoader(BaseSettings): |
| 26 | + model_config = SettingsConfigDict( |
| 27 | + frozen=True, |
| 28 | + extra="allow", |
| 29 | + env_nested_delimiter="_", |
| 30 | + env_nested_max_split=1, |
| 31 | + enable_decoding=False, |
| 32 | + ) |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def settings_customise_sources( |
| 36 | + cls, |
| 37 | + settings_cls: type[BaseSettings], |
| 38 | + init_settings: PydanticBaseSettingsSource, |
| 39 | + env_settings: PydanticBaseSettingsSource, |
| 40 | + dotenv_settings: PydanticBaseSettingsSource, |
| 41 | + file_secret_settings: PydanticBaseSettingsSource, |
| 42 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 43 | + """Customise the sources of settings for the connector. |
| 44 | +
|
| 45 | + This method is called by the Pydantic BaseSettings class to determine the order of sources. |
| 46 | + The configuration come in this order either from: |
| 47 | + 1. Environment variables |
| 48 | + 2. YAML file |
| 49 | + 3. .env file |
| 50 | + 4. Default values |
| 51 | +
|
| 52 | + The variables loading order will remain the same as in `pycti.get_config_variable()`: |
| 53 | + 1. If a config.yml file is found, the order will be: `ENV VAR` → config.yml → default value |
| 54 | + 2. If a .env file is found, the order will be: `ENV VAR` → .env → default value |
| 55 | + """ |
| 56 | + _main_path = os.curdir |
| 57 | + |
| 58 | + settings_cls.model_config["env_file"] = f"{_main_path}/../.env" |
| 59 | + |
| 60 | + if not settings_cls.model_config["yaml_file"]: |
| 61 | + if Path(f"{_main_path}/config.yml").is_file(): |
| 62 | + settings_cls.model_config["yaml_file"] = f"{_main_path}/config.yml" |
| 63 | + if Path(f"{_main_path}/../config.yml").is_file(): |
| 64 | + settings_cls.model_config["yaml_file"] = f"{_main_path}/../config.yml" |
| 65 | + |
| 66 | + if Path(settings_cls.model_config["yaml_file"] or "").is_file(): # type: ignore |
| 67 | + return ( |
| 68 | + env_settings, |
| 69 | + YamlConfigSettingsSource(settings_cls), |
| 70 | + ) |
| 71 | + if Path(settings_cls.model_config["env_file"] or "").is_file(): # type: ignore |
| 72 | + return ( |
| 73 | + env_settings, |
| 74 | + DotEnvSettingsSource(settings_cls), |
| 75 | + ) |
| 76 | + return (env_settings,) |
| 77 | + |
| 78 | + |
| 79 | +LogLevelToLower = Annotated[ |
| 80 | + Literal["debug", "info", "warn", "error"], |
| 81 | + PlainSerializer(lambda v: "".join(v), return_type=str), |
| 82 | +] |
| 83 | + |
| 84 | +HttpUrlToString = Annotated[HttpUrl, PlainSerializer(str, return_type=str)] |
| 85 | +TimedeltaInSeconds = Annotated[ |
| 86 | + timedelta, PlainSerializer(lambda v: int(v.total_seconds()), return_type=int) |
| 87 | +] |
| 88 | + |
| 89 | + |
| 90 | +class ConfigLoaderOAEV(BaseConfigModel): |
| 91 | + """OpenAEV/OpenAEV platform configuration settings. |
| 92 | +
|
| 93 | + Contains URL and authentication token for connecting to the OpenAEV platform. |
| 94 | + """ |
| 95 | + |
| 96 | + url: HttpUrlToString = Field( |
| 97 | + description="The OpenAEV platform URL.", |
| 98 | + ) |
| 99 | + token: str = Field( |
| 100 | + description="The token for the OpenAEV platform.", |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +class ConfigLoaderCollector(BaseConfigModel): |
| 105 | + """Base collector configuration settings. |
| 106 | +
|
| 107 | + Contains common collector settings including identification, logging, |
| 108 | + scheduling, and platform information. |
| 109 | + """ |
| 110 | + |
| 111 | + id: str = Field(description="ID of the collector.") |
| 112 | + |
| 113 | + name: str = Field(description="Name of the collector") |
| 114 | + |
| 115 | + log_level: LogLevelToLower | None = Field( |
| 116 | + default="error", |
| 117 | + description="Determines the verbosity of the logs.", |
| 118 | + ) |
| 119 | + period: timedelta | None = Field( |
| 120 | + default=timedelta(minutes=1), |
| 121 | + description="Duration between two scheduled runs of the collector (ISO 8601 format).", |
| 122 | + ) |
| 123 | + icon_filepath: str | None = Field( |
| 124 | + description="Path to the icon file of the collector.", |
| 125 | + ) |
0 commit comments