|
| 1 | +"""Base configuration shared across all DPI NG capabilities.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from dataclasses import dataclass, field |
| 6 | + |
| 7 | +from .auth import AuthProvider, ClientCertificateAuth |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +_URL_PATTERN = re.compile(r"^https?://[^\s/$.?#].[^\s]*$") |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class BaseCapabilityConfig: |
| 16 | + """Base configuration for a DPI NG capability client. |
| 17 | +
|
| 18 | + Subclasses add a ``service_path`` (and any other capability-specific fields) |
| 19 | + on top of these shared fields. |
| 20 | +
|
| 21 | + Args: |
| 22 | + base_url: URL of the DPI external service router |
| 23 | + (e.g. ``https://api.service.<region>.ngdpi.dpp.cloud.sap``). |
| 24 | + This URL can be found in the credentials of the ``data-privacy-integration`` |
| 25 | + service instance. |
| 26 | + auth: Authentication strategy - one of BearerTokenAuth, ClientCredentialsAuth, |
| 27 | + or ClientCertificateAuth. |
| 28 | + timeout: HTTP request timeout in seconds (default 30). |
| 29 | + verify_ssl: Verify TLS certificates - set False only in local dev. |
| 30 | + Overridden by ``ClientCertificateAuth`` when a custom ``ca_file`` is provided. |
| 31 | + tenant_id: Tenant identifier sent as the ``x-tenant-id`` HTTP header. |
| 32 | + **Required** for ``ClientCertificateAuth`` — mTLS does not carry a |
| 33 | + tenant claim, so the service router needs it to route requests to the |
| 34 | + correct tenant. Must not be provided for ``BearerTokenAuth`` or |
| 35 | + ``ClientCredentialsAuth``, which already embed the tenant identity in |
| 36 | + the token. |
| 37 | + """ |
| 38 | + |
| 39 | + base_url: str |
| 40 | + auth: AuthProvider |
| 41 | + timeout: float = 30.0 |
| 42 | + verify_ssl: bool = True |
| 43 | + tenant_id: str | None = field(default=None) |
| 44 | + |
| 45 | + def __post_init__(self) -> None: |
| 46 | + """Validate config after dataclass construction. |
| 47 | +
|
| 48 | + Raises: |
| 49 | + ValueError: If *base_url* is not a valid HTTP(S) URL, *auth* is not |
| 50 | + an ``AuthProvider`` instance, ``ClientCertificateAuth`` is used without |
| 51 | + *tenant_id*, or *tenant_id* is provided with a non-cert auth type. |
| 52 | + """ |
| 53 | + logger.info("Invoked BaseCapabilityConfig.__post_init__") |
| 54 | + if not _URL_PATTERN.match(self.base_url): |
| 55 | + logger.error("Invalid base_url — value=%r", self.base_url) |
| 56 | + raise ValueError( |
| 57 | + f"base_url must be a valid HTTP(S) URL, got: {self.base_url!r}" |
| 58 | + ) |
| 59 | + if not isinstance(self.auth, AuthProvider): |
| 60 | + logger.error( |
| 61 | + "auth is not an AuthProvider instance — type=%s", type(self.auth) |
| 62 | + ) |
| 63 | + raise ValueError("auth must be an AuthProvider instance") |
| 64 | + self.base_url = self.base_url.rstrip("/") |
| 65 | + is_cert_auth = isinstance(self.auth, ClientCertificateAuth) |
| 66 | + if is_cert_auth and not self.tenant_id: |
| 67 | + logger.error("tenant_id is required for ClientCertificateAuth") |
| 68 | + raise ValueError("tenant_id is required when using ClientCertificateAuth") |
| 69 | + if not is_cert_auth and self.tenant_id is not None: |
| 70 | + logger.error("tenant_id is not applicable for %s", type(self.auth).__name__) |
| 71 | + raise ValueError( |
| 72 | + f"tenant_id must not be set for {type(self.auth).__name__}; " |
| 73 | + "it is only valid for ClientCertificateAuth" |
| 74 | + ) |
| 75 | + logger.debug( |
| 76 | + "Config validated — base_url=%s verify_ssl=%s", |
| 77 | + self.base_url, |
| 78 | + self.verify_ssl, |
| 79 | + ) |
| 80 | + logger.info("Exiting BaseCapabilityConfig.__post_init__") |
0 commit comments