Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

11.6 KB
Raw Blame
"""Lago transport layer.

Lago is an open-source usage-based billing platform offered both as Lago Cloud
(``https://api.getlago.com``) and self-hosted (a customer-supplied host), so the API base URL
must be configurable. Auth is a single Bearer API key. List endpoints are page-number paginated
(``page`` / ``per_page``) and wrap their records under a resource key alongside a ``meta`` object
that carries ``total_pages`` / ``next_page``. Billing entities and the analytics reports are the
exception: they return the whole collection in one body with no ``meta``.

Every stream is full-refresh. Lago's REST API exposes no universal server-side ``created_at`` /
``updated_at`` cursor across resources — only a handful of endpoints offer ad-hoc date filters
(e.g. ``issuing_date_from`` on invoices) that filter on a business date rather than a monotonic
record-creation timestamp, so they are unsafe to treat as an incremental cursor. Incremental sync
can be layered on later for a specific endpoint once its server-side filter is verified against the
live API.

Charges and wallet transactions are only listed per parent (a plan, a wallet), so those tables fan
out over a fresh listing of the parent on every sync.

Pagination, retries, auth-header redaction, and the redirect / off-host SSRF guards are provided by
the shared ``rest_source`` framework (page-number paginator, ``allowed_hosts`` host-pinning,
``allow_redirects=False``). The DNS-based internal-IP check for customer-supplied self-hosted hosts
has no framework equivalent, so it stays here as a run-time pre-check.
"""

import re
import dataclasses
from collections.abc import Callable, Iterable, Iterator
from typing import Any, Optional
from urllib.parse import quote, urlencode, urlparse

import requests

from products.warehouse_sources.sources.lago.settings import LAGO_ENDPOINTS, LagoEndpointConfig
from products.warehouse_sources.sources.sdk import (
    ClientConfig,
    Endpoint,
    PageNumberPaginator,
    RESTAPIConfig,
    ResumableSourceManager,
    SourceResponse,
    _is_host_safe,
    build_dependent_resource,
    make_tracked_session,
    rest_api_resource,
)

DEFAULT_API_HOST = "https://api.getlago.com"
API_VERSION_PATH = "/api/v1"

HOST_NOT_ALLOWED_ERROR = "Lago API URL is not allowed"


class LagoHostNotAllowedError(Exception):
    pass


@dataclasses.dataclass(frozen=True)
class LagoResumeConfig:
    # The next page to fetch on resume. Persisted after each page is yielded, so a crash before
    # this write leaves the previous value in place and the last page is re-yielded (Lago merges
    # dedupe on `lago_id`).
    next_page: Optional[int] = None
    # Fan-out tables checkpoint the shape `build_dependent_resource` emits: which parents finished
    # and where the current one stopped.
    fanout_state: Optional[dict[str, Any]] = None


def normalize_base_url(api_url: Optional[str]) -> str:
    """Turn whatever the user typed into a ``<scheme>://<host>/api/v1`` base URL.

    Blank → Lago Cloud. Accepts bare hosts (``billing.example.com``), full URLs with or without a
    scheme, and values that already include the ``/api/v1`` suffix.
    """
    raw = (api_url or "").strip()
    if not raw:
        raw = DEFAULT_API_HOST
    if not re.match(r"^https?://", raw, flags=re.IGNORECASE):
        raw = f"https://{raw}"
    raw = raw.rstrip("/")
    # Drop a trailing version segment the user may have pasted in, then re-add the version we target.
    raw = re.sub(r"/api/v\d+$", "", raw)
    return f"{raw}{API_VERSION_PATH}"


def _host_of(base_url: str) -> str:
    return (urlparse(base_url).hostname or "").lower()


def _encode_plan_code(row: dict[str, Any]) -> dict[str, Any]:
    return {**row, "_path_code": quote(str(row.get("code", "")), safe="")}


_PARENT_DATA_MAPS: dict[str, Callable[[dict[str, Any]], dict[str, Any]]] = {"plans": _encode_plan_code}


def _get_headers(api_key: str) -> dict[str, str]:
    return {
        "Authorization": f"Bearer {api_key}",
        "Accept": "application/json",
        "Content-Type": "application/json",
    }


def validate_credentials(
    api_url: Optional[str], api_key: str, schema_name: Optional[str] = None, team_id: Optional[int] = None
) -> tuple[bool, str | None]:
    """Probe a cheap list endpoint to confirm the Bearer token is genuine.

    At source-create (``schema_name is None``) a 403 is accepted: the token is valid but may lack
    permission for this particular probe. A scoped probe (``schema_name`` set) treats 403 as a hard
    failure.

    Kept hand-rolled rather than routed through ``validate_via_probe`` because the probe must run
    with ``allow_redirects=False`` and reject any 3xx — the validated host could 3xx to an internal
    address, defeating the internal-IP check below (SSRF). ``validate_via_probe`` follows redirects.
    """
    base_url = normalize_base_url(api_url)
    host = _host_of(base_url)

    if not host:
        return False, "Invalid Lago API URL"

    # The host is fully customer-controlled for self-hosted deployments, so block hosts that resolve
    # to private/internal addresses (SSRF). Only enforced on cloud — see _is_host_safe.
    if team_id is not None:
        host_ok, host_err = _is_host_safe(host, team_id)
        if not host_ok:
            return False, host_err or HOST_NOT_ALLOWED_ERROR

    url = f"{base_url}/customers?{urlencode({'per_page': 1, 'page': 1})}"
    try:
        # Don't follow redirects: the validated host could 3xx to an internal address, defeating
        # the host check above (SSRF).
        response = make_tracked_session().get(url, headers=_get_headers(api_key), timeout=10, allow_redirects=False)
    except requests.exceptions.RequestException as e:
        return False, str(e)

    if response.is_redirect or response.is_permanent_redirect:
        return False, HOST_NOT_ALLOWED_ERROR

    if response.status_code == 200:
        return True, None

    if response.status_code == 401:
        return False, "Invalid Lago API key"

    if response.status_code == 403:
        if schema_name is None:
            # Valid token, missing permission for this probe — let source creation through.
            return True, None
        return False, "Lago API key lacks the required permissions for this endpoint"

    if response.status_code == 404:
        # Lago's 404 body is a bare "Not Found" that gives the user nothing to act on. A 404 on the
        # customers probe almost always means the API URL doesn't point at a Lago instance.
        return False, (
            "PostHog reached the host, but it returned 404 Not Found for the Lago API. "
            "Check the API URL points to your Lago instance (leave it blank to use Lago Cloud), then try again."
        )

    # Any other status: report the code without echoing the raw upstream body, which can be
    # unactionable (a bare "Not Found") or reflect request details back to the user.
    return (
        False,
        f"Lago rejected the request (HTTP {response.status_code}). Check the API key and API URL, then try again.",
    )


def lago_source(
    api_url: Optional[str],
    api_key: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[LagoResumeConfig],
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config: LagoEndpointConfig = LAGO_ENDPOINTS[endpoint]
    base_url = normalize_base_url(api_url)
    host = _host_of(base_url)

    client_config: ClientConfig = {
        "base_url": base_url,
        # Auth (Bearer) is supplied via the framework auth config so its value is redacted from
        # logs and raised errors; only the non-secret accept/content headers are set here.
        "headers": {"Accept": "application/json", "Content-Type": "application/json"},
        "auth": {"type": "bearer", "token": api_key},
        # Pin every request to the base host and refuse redirects: a self-hosted host is
        # customer-controlled, so a tampered pagination link or a 3xx to an internal address must
        # not carry the Authorization header off-host (SSRF). `allowed_hosts=[]` means
        # "same host as base_url only".
        "allowed_hosts": [],
        "allow_redirects": False,
        # Page-number pagination; `meta.total_pages` stops after the last page so no extra empty
        # page is fetched. `stop_after_empty_page` (default) covers a 0-row / missing-key body.
        "paginator": PageNumberPaginator(base_page=1, total_path="meta.total_pages"),
    }

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None

    resource: Iterable[Any]
    if config.fanout is not None:
        parent_config = LAGO_ENDPOINTS[config.fanout.parent_name]

        def save_fanout_checkpoint(state: Optional[dict[str, Any]]) -> None:
            if state:
                resumable_source_manager.save_state(LagoResumeConfig(fanout_state=state))

        resource = build_dependent_resource(
            endpoint_configs=LAGO_ENDPOINTS,
            child_endpoint=endpoint,
            fanout=config.fanout,
            client_config=client_config,
            path_format_values={},
            team_id=team_id,
            job_id=job_id,
            db_incremental_field_last_value=None,
            page_size_param="per_page",
            parent_endpoint_extra={"data_selector": parent_config.data_key},
            child_endpoint_extra={"data_selector": config.data_key},
            parent_data_map=_PARENT_DATA_MAPS.get(config.fanout.parent_name),
            resume_hook=save_fanout_checkpoint,
            initial_paginator_state=resume.fanout_state if resume else None,
        )
    else:
        endpoint_config: Endpoint = {"path": config.path, "data_selector": config.data_key}
        if config.paginated:
            endpoint_config["params"] = {"per_page": config.page_size}
        else:
            endpoint_config["paginator"] = "single_page"

        rest_config: RESTAPIConfig = {
            "client": client_config,
            "resources": [{"name": endpoint, "endpoint": endpoint_config}],
        }

        def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
            # Persist only when a next page remains; save AFTER a page is yielded so a crash re-yields
            # the last page (merge dedupes on `lago_id`) rather than skipping it.
            if state and state.get("page") is not None:
                resumable_source_manager.save_state(LagoResumeConfig(next_page=int(state["page"])))

        resource = rest_api_resource(
            rest_config,
            team_id,
            job_id,
            db_incremental_field_last_value,
            resume_hook=save_checkpoint,
            initial_paginator_state={"page": resume.next_page} if resume and resume.next_page is not None else None,
        )

    def items() -> Iterator[list[dict[str, Any]]]:
        # Re-check at run time (not just at source-create) in case the URL was edited or now resolves
        # to an internal address (SSRF / DNS rebinding). Only enforced on cloud.
        host_ok, host_err = _is_host_safe(host, team_id)
        if not host_ok:
            raise LagoHostNotAllowedError(host_err or HOST_NOT_ALLOWED_ERROR)
        yield from resource

    return SourceResponse(
        name=endpoint,
        items=items,
        primary_keys=config.primary_keys,
        # Full-refresh replace: Lago exposes no `sort` param and no incremental cursor, so there is
        # no watermark to checkpoint. The default ascending mode is harmless here.
        sort_mode="asc",
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="month" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
    )