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.

15.6 KB
Raw Blame
from dataclasses import field
from typing import Literal, Optional

from products.warehouse_sources.sources.sdk import DependentEndpointConfig, IncrementalField, ResponseAction, frozen

# Page size for the deployments fan-out (`take` param). Coolify's other list endpoints take no
# pagination params at all and return the full collection in one response.
DEPLOYMENTS_PAGE_SIZE = 100

# Hard ceiling on the `skip` offset walked per application. The instance is customer-controlled,
# so a hostile or broken server could report an ever-growing `count` with non-empty pages forever;
# this cap makes the walk self-terminate. Generous enough not to truncate a real deploy history.
MAX_DEPLOYMENTS_OFFSET = 100_000

# `/audit-events` pages with `page`/`per_page` (100 is the server maximum).
AUDIT_EVENTS_PAGE_SIZE = 100

# Same safety ceiling as MAX_DEPLOYMENTS_OFFSET, in pages of AUDIT_EVENTS_PAGE_SIZE rows.
MAX_AUDIT_EVENTS_PAGES = 10_000

# A parent row deleted between its listing and the child fetch answers 404; skip it instead of
# failing the whole table.
_IGNORE_404: list[ResponseAction] = [{"status_code": 404, "action": "ignore"}]


@frozen
class CoolifyEndpointConfig:
    name: str
    # Path relative to the API base ({instance}/api/v1). A fan-out child's path carries the
    # `{...}` placeholder its `fanout.resolve_param` binds.
    path: str
    # jsonpath into the response body where the list of records lives. "$" for the endpoints
    # that answer with a bare array.
    data_selector: str = "$"
    primary_keys: list[str] = field(default_factory=lambda: ["uuid"])
    # A stable creation timestamp to partition by. None for resources the API doesn't stamp
    # with one. Never a mutable field like `updated_at`.
    partition_key: Optional[str] = None
    # Response keys Coolify itself hides from tokens without the `read:sensitive`/`root` ability
    # (each model's `$hidden` list): credentials, connection URLs, compose/dockerfile contents,
    # and deploy logs. A privileged token returns them, so they're dropped from every record
    # (recursively) before storage, and their presence also disables HTTP sample capture for the
    # endpoint. Stripping them for every token also keeps the table shape independent of the
    # token's abilities.
    sensitive_fields: frozenset[str] = frozenset()
    # False keeps the endpoint's raw response out of HTTP sample capture even when nothing is
    # stripped from storage, e.g. team member emails. Implied by `sensitive_fields`.
    captures_http_samples: bool = True
    # "skip_take" pages with `skip`/`take` and a `count` total (deployments), "page" with
    # `page`/`per_page` and a `last_page` total (audit events). Every other endpoint answers with
    # the full collection in one response.
    pagination: Literal["none", "skip_take", "page"] = "none"
    # Set on endpoints that are fetched once per row of a parent endpoint. A parent that itself
    # fans out makes a two-level chain.
    fanout: Optional[DependentEndpointConfig] = None
    # Read by the fan-out helper's `FanoutEndpointLike` protocol. No Coolify endpoint takes a
    # server-side time filter, so the incremental members stay empty for every endpoint.
    page_size: int = DEPLOYMENTS_PAGE_SIZE
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    default_incremental_field: Optional[str] = None


# Server settings a `read:sensitive`/`root` token can see but a read-only token cannot (Coolify's
# own model `$hidden` list, plus the log-drain destinations): the Sentinel APM token and its
# custom collector URL, the raw log-drain forwarder config (which can embed its own credentials
# for Axiom/New Relic/a custom endpoint), and the log-drain parser script. `servers` returns
# these directly; `applications` embeds the same server settings object under its destination, so
# both endpoints strip this set.
SERVER_SENSITIVE_FIELDS: frozenset[str] = frozenset(
    {
        "logdrain_axiom_api_key",
        "logdrain_newrelic_license_key",
        "sentinel_token",
        "sentinel_custom_url",
        "logdrain_custom_config",
        "logdrain_custom_config_parser",
    }
)

COOLIFY_ENDPOINTS: dict[str, CoolifyEndpointConfig] = {
    "applications": CoolifyEndpointConfig(
        name="applications",
        path="/applications",
        partition_key="created_at",
        # Applications embed their destination server's settings object, so this set also carries
        # every key in SERVER_SENSITIVE_FIELDS (see `servers` below).
        sensitive_fields=frozenset(
            {
                "http_basic_auth_password",
                "manual_webhook_secret_github",
                "manual_webhook_secret_gitlab",
                "manual_webhook_secret_bitbucket",
                "manual_webhook_secret_gitea",
                "dockerfile",
                "docker_compose",
                "docker_compose_raw",
                "custom_labels",
                "domain_dns_statuses",
                "domain_port_overrides",
            }
            | SERVER_SENSITIVE_FIELDS
        ),
    ),
    # One listing across every standalone database type (PostgreSQL, MySQL, MariaDB, MongoDB,
    # Redis, KeyDB, Dragonfly, ClickHouse), so the sensitive set is the union of every type's
    # credential fields.
    "databases": CoolifyEndpointConfig(
        name="databases",
        path="/databases",
        partition_key="created_at",
        sensitive_fields=frozenset(
            {
                "postgres_password",
                "mysql_password",
                "mysql_root_password",
                "mariadb_password",
                "mariadb_root_password",
                "mongo_initdb_root_password",
                "redis_password",
                "keydb_password",
                "dragonfly_password",
                "clickhouse_admin_password",
                "internal_db_url",
                "external_db_url",
                "init_scripts",
            }
        ),
    ),
    # Full deploy history per application. The account-wide `/deployments` endpoint only lists
    # deployments that are currently running, so the history has to be fanned out per app uuid.
    # Rows already carry `application_id` (the numeric id); the parent's `uuid` is projected on
    # as `application_uuid` so deployments join directly to the applications table's key.
    "deployments": CoolifyEndpointConfig(
        name="deployments",
        path="/deployments/applications/{application_uuid}",
        data_selector="deployments",
        primary_keys=["deployment_uuid"],
        partition_key="created_at",
        sensitive_fields=frozenset({"logs", "configuration_snapshot", "configuration_diff"}),
        pagination="skip_take",
        fanout=DependentEndpointConfig(
            parent_name="applications",
            resolve_param="application_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            parent_field_renames={"uuid": "application_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    # Pull request preview environments per application, newest pull request first. Older
    # instances that predate this endpoint answer 404 per application, which leaves the table empty.
    "preview_deployments": CoolifyEndpointConfig(
        name="preview_deployments",
        path="/applications/{application_uuid}/previews",
        partition_key="created_at",
        # The applications listing this walks through carries webhook secrets, so keep it out of
        # HTTP samples.
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="applications",
            resolve_param="application_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            parent_field_renames={"uuid": "application_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    # Newest first by (created_at, id). Only team admins and owners can read the log, so a
    # token created by any other member gets 403 on this endpoint alone.
    "audit_events": CoolifyEndpointConfig(
        name="audit_events",
        path="/audit-events",
        data_selector="data",
        primary_keys=["id"],
        partition_key="created_at",
        # Only a `read:sensitive`/`root` token gets these. `metadata` and `changes` carry the
        # before/after values of the edited resource, which can include its secrets.
        sensitive_fields=frozenset(
            {
                "actor_email",
                "actor_token_id",
                "actor_token_name",
                "metadata",
                "changes",
                "ip_address",
                "user_agent",
            }
        ),
        pagination="page",
        page_size=AUDIT_EVENTS_PAGE_SIZE,
    ),
    # Every execution of every scheduled task on every application:
    # applications -> scheduled tasks -> executions. `message` is the command output, which
    # Coolify hides from tokens without `read:sensitive`.
    "application_task_executions": CoolifyEndpointConfig(
        name="application_task_executions",
        path="/applications/{application_uuid}/scheduled-tasks/{task_uuid}/executions",
        partition_key="created_at",
        sensitive_fields=frozenset({"message"}),
        fanout=DependentEndpointConfig(
            parent_name="application_scheduled_tasks",
            resolve_param="task_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid", "name", "_applications_uuid"],
            parent_field_renames={
                "uuid": "task_uuid",
                "name": "task_name",
                "_applications_uuid": "application_uuid",
            },
            child_response_actions=_IGNORE_404,
        ),
    ),
    # The same walk for services: services -> scheduled tasks -> executions.
    "service_task_executions": CoolifyEndpointConfig(
        name="service_task_executions",
        path="/services/{service_uuid}/scheduled-tasks/{task_uuid}/executions",
        partition_key="created_at",
        sensitive_fields=frozenset({"message"}),
        fanout=DependentEndpointConfig(
            parent_name="service_scheduled_tasks",
            resolve_param="task_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid", "name", "_services_uuid"],
            parent_field_renames={
                "uuid": "task_uuid",
                "name": "task_name",
                "_services_uuid": "service_uuid",
            },
            child_response_actions=_IGNORE_404,
        ),
    ),
    # Every run of every scheduled backup on every standalone database:
    # databases -> backup configurations -> executions.
    "backup_executions": CoolifyEndpointConfig(
        name="backup_executions",
        path="/databases/{database_uuid}/backups/{scheduled_backup_uuid}/executions",
        data_selector="executions",
        partition_key="created_at",
        # The backup configuration listing this walks through is an undocumented raw model dump,
        # so keep it out of HTTP samples.
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="database_backups",
            resolve_param="scheduled_backup_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid", "_databases_uuid"],
            parent_field_renames={"uuid": "scheduled_backup_uuid", "_databases_uuid": "database_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    "projects": CoolifyEndpointConfig(
        name="projects",
        path="/projects",
    ),
    # Coolify selects only `id`, `name`, and `uuid` here. `id` is what `environment_id` on
    # applications, databases, and services points at.
    "project_environments": CoolifyEndpointConfig(
        name="project_environments",
        path="/projects/{project_uuid}/environments",
        fanout=DependentEndpointConfig(
            parent_name="projects",
            resolve_param="project_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            parent_field_renames={"uuid": "project_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    "servers": CoolifyEndpointConfig(
        name="servers",
        path="/servers",
        sensitive_fields=SERVER_SENSITIVE_FIELDS,
    ),
    "services": CoolifyEndpointConfig(
        name="services",
        path="/services",
        partition_key="created_at",
        sensitive_fields=frozenset({"docker_compose", "docker_compose_raw"}),
    ),
    # The containers and databases a service's compose file defines, one listing per service.
    # The services listing these walk through carries the raw compose file, so keep it out of
    # HTTP samples.
    "service_applications": CoolifyEndpointConfig(
        name="service_applications",
        path="/services/{service_uuid}/applications",
        partition_key="created_at",
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="services",
            resolve_param="service_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            parent_field_renames={"uuid": "service_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    "service_databases": CoolifyEndpointConfig(
        name="service_databases",
        path="/services/{service_uuid}/databases",
        partition_key="created_at",
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="services",
            resolve_param="service_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            parent_field_renames={"uuid": "service_uuid"},
            child_response_actions=_IGNORE_404,
        ),
    ),
    "teams": CoolifyEndpointConfig(
        name="teams",
        path="/teams",
        primary_keys=["id"],
        partition_key="created_at",
        # Rows embed the member list with each member's email address; keep that out of
        # diagnostic samples even though it belongs in the table.
        captures_http_samples=False,
    ),
}

ENDPOINTS = tuple(COOLIFY_ENDPOINTS.keys())

# Middle hops of the two-level chains. They are walked to reach their executions but are not
# tables of their own.
COOLIFY_FANOUT_PARENTS: dict[str, CoolifyEndpointConfig] = {
    "application_scheduled_tasks": CoolifyEndpointConfig(
        name="application_scheduled_tasks",
        path="/applications/{application_uuid}/scheduled-tasks",
        fanout=DependentEndpointConfig(
            parent_name="applications",
            resolve_param="application_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            child_response_actions=_IGNORE_404,
        ),
    ),
    "service_scheduled_tasks": CoolifyEndpointConfig(
        name="service_scheduled_tasks",
        path="/services/{service_uuid}/scheduled-tasks",
        fanout=DependentEndpointConfig(
            parent_name="services",
            resolve_param="service_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            child_response_actions=_IGNORE_404,
        ),
    ),
    "database_backups": CoolifyEndpointConfig(
        name="database_backups",
        path="/databases/{database_uuid}/backups",
        fanout=DependentEndpointConfig(
            parent_name="databases",
            resolve_param="database_uuid",
            resolve_field="uuid",
            include_from_parent=["uuid"],
            child_response_actions=_IGNORE_404,
        ),
    ),
}

# Coolify exposes no server-side timestamp filter on any list endpoint, so no endpoint supports
# genuine incremental sync. Kept for parity with the schema-building convention.
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {}