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.

18.6 KB
Raw Blame
import json
import threading
import dataclasses
from collections.abc import Iterator
from datetime import UTC, datetime
from typing import Any, Optional
from urllib.parse import urlencode, urljoin, urlparse

import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter

from products.warehouse_sources.sources.sdk import (
    ResumableSourceManager,
    SourceResponse,
    _is_host_safe,
    is_cloud,
    make_tracked_session,
    retry_with_deadline,
)
from products.warehouse_sources.sources.teamcity.settings import (
    DOWNLOAD_CHUNK_BYTES,
    FAN_OUT_BUILD_FINISH_DATE_FIELD,
    FAN_OUT_BUILD_ID_FIELD,
    MAX_PAGES_PER_BUILD,
    MAX_PAGES_PER_WALK,
    MAX_RESPONSE_BYTES,
    MAX_RESPONSE_DOWNLOAD_SECONDS,
    TEAMCITY_ENDPOINTS,
    TeamCityEndpointConfig,
)

TEAMCITY_API_PATH = "/app/rest"

# TeamCity's compact ISO-8601 timestamp format, e.g. "20260715T160948+0000".
TEAMCITY_DATETIME_FORMAT = "%Y%m%dT%H%M%S%z"

# Slim parent payload for the occurrence fan-out: only the fields the fan-out itself needs.
FAN_OUT_PARENT_FIELDS = "count,nextHref,build(id,finishDate)"

REQUEST_TIMEOUT_SECONDS = 60


class TeamCityRetryableError(Exception):
    pass


@dataclasses.dataclass(frozen=False)
class TeamCityResumeConfig:
    # Server-relative nextHref of the next page to fetch (for fan-out endpoints, the next
    # page of the parent builds walk). None means "start from the first page".
    next_href: str | None = None


def normalize_host(host: str) -> str:
    """Reduce user input to a validated TeamCity server root URL.

    Accepts "https://teamcity.example.com", a bare host, or a URL with a context path
    (TeamCity is often served under one, e.g. "https://ci.example.com/teamcity"), with or
    without a trailing "/app/rest". Raises ``ValueError`` on anything that could retarget
    the stored token (userinfo, query, fragment) or that isn't a plain http(s) URL.
    """
    cleaned = host.strip().rstrip("/")
    if not cleaned:
        raise ValueError("TeamCity server URL is required")
    if "://" not in cleaned:
        cleaned = f"https://{cleaned}"

    parsed = urlparse(cleaned)
    if parsed.scheme not in ("http", "https"):
        raise ValueError(f"Invalid TeamCity server URL: {host!r}. Use an http(s) URL.")
    if not parsed.hostname:
        raise ValueError(
            f"Invalid TeamCity server URL: {host!r}. Enter your server URL, e.g. https://teamcity.example.com."
        )
    if parsed.username or parsed.password or parsed.query or parsed.fragment:
        raise ValueError(
            f"Invalid TeamCity server URL: {host!r}. Enter just the server URL, without credentials or query parameters."
        )

    path = parsed.path.rstrip("/")
    if path.endswith(TEAMCITY_API_PATH):
        path = path[: -len(TEAMCITY_API_PATH)]

    return f"{parsed.scheme}://{parsed.netloc}{path}"


def _server_root(host: str) -> str:
    """Scheme + netloc only — nextHref values are server-relative and already carry any context path."""
    parsed = urlparse(normalize_host(host))
    return f"{parsed.scheme}://{parsed.netloc}"


def _resolve_next_href(server_root: str, next_href: str) -> str:
    """Resolve a pagination cursor, pinning it to the validated server.

    ``nextHref`` comes from the remote response (and, on resume, from stored state), and
    ``urljoin`` would honor an absolute or scheme-relative value — letting a compromised or
    attacker-controlled server retarget the next authenticated request (Bearer token attached)
    at an internal host. Resolve against the server root, then require the result to stay on
    the same scheme and netloc. Redirects are pinned off at the session too (defense in depth).
    """
    resolved = urljoin(server_root, next_href)
    root = urlparse(server_root)
    target = urlparse(resolved)
    if (target.scheme, target.netloc) != (root.scheme, root.netloc):
        raise ValueError(f"TeamCity pagination cursor points off the configured server: {next_href!r}")
    return resolved


def _api_base(host: str) -> str:
    return f"{normalize_host(host)}{TEAMCITY_API_PATH}"


def _headers(access_token: str) -> dict[str, str]:
    return {"Authorization": f"Bearer {access_token}", "Accept": "application/json"}


def _check_host_safe(host: str, team_id: int) -> None:
    """Vet the server URL at both source-create and sync time.

    On Cloud the Bearer token crosses the public internet, so plain http would expose it to
    on-path capture — require https there (mirrors `custom/source.py:_check_url`). Self-hosted
    PostHog may sit next to an internal TeamCity server over http, so http stays allowed.
    """
    parsed = urlparse(normalize_host(host))
    if is_cloud() and parsed.scheme != "https":
        raise ValueError(f"Invalid TeamCity server URL: {host!r} must use https:// on PostHog Cloud")
    is_safe, error = _is_host_safe(parsed.hostname or "", team_id)
    if not is_safe:
        raise ValueError(f"Invalid TeamCity server URL: {error}")


def _format_teamcity_datetime(value: Any) -> str:
    """Format an incremental cursor as the compact ISO-8601 string TeamCity locators expect."""
    if isinstance(value, datetime):
        aware = value if value.tzinfo is not None else value.replace(tzinfo=UTC)
        return aware.astimezone(UTC).strftime(TEAMCITY_DATETIME_FORMAT)
    return str(value)


def _parse_teamcity_datetime(value: Any) -> Any:
    if isinstance(value, str):
        try:
            return datetime.strptime(value, TEAMCITY_DATETIME_FORMAT)
        except ValueError:
            return value
    return value


def _normalize_row(row: dict[str, Any], timestamp_fields: list[str]) -> dict[str, Any]:
    for timestamp_field in timestamp_fields:
        if timestamp_field in row:
            row[timestamp_field] = _parse_teamcity_datetime(row[timestamp_field])
    return row


def _build_locator(dimensions: dict[str, str]) -> str:
    return ",".join(f"{name}:{value}" for name, value in dimensions.items())


def _incremental_locator_dimensions(
    config: TeamCityEndpointConfig,
    db_incremental_field_last_value: Any,
) -> dict[str, str]:
    """Server-side incremental filter dimensions for an endpoint's locator.

    Verified against a live server: both filters are honored (a future-dated/too-high
    cursor returns 0 rows) and are preserved in nextHref, so every page stays windowed.
    """
    if db_incremental_field_last_value is None:
        return {}
    if config.name == "changes":
        return {"sinceChange": f"(id:{int(db_incremental_field_last_value)})"}
    # builds, and the parent walk of the occurrence fan-outs
    return {"finishDate": f"(date:{_format_teamcity_datetime(db_incremental_field_last_value)},condition:after)"}


def _build_list_url(
    api_base: str,
    path: str,
    locator_dimensions: dict[str, str],
    fields: str,
) -> str:
    params = {"locator": _build_locator(locator_dimensions), "fields": fields}
    return f"{api_base}{path}?{urlencode(params)}"


@retry_with_deadline(
    retry=retry_if_exception_type((TeamCityRetryableError, requests.ReadTimeout, requests.ConnectionError)),
    stop=stop_after_attempt(5),
    wait=wait_exponential_jitter(initial=1, max=30),
    reraise=True,
)
def _fetch_page(
    session: requests.Session, url: str, headers: dict[str, str], logger: FilteringBoundLogger
) -> dict[str, Any]:
    # stream=True so the body isn't buffered until we read it under a cap below — the server URL is
    # customer-supplied, so a hostile host could otherwise return an unbounded body and OOM the worker.
    response = session.get(url, headers=headers, timeout=REQUEST_TIMEOUT_SECONDS, stream=True)

    # Throughput is bounded by the customer's own server; back off on 429 and transient 5xx
    # rather than failing the sync.
    if response.status_code == 429 or response.status_code >= 500:
        response.close()
        raise TeamCityRetryableError(f"TeamCity API error (retryable): status={response.status_code}, url={url}")

    # `requests`' timeout only bounds an idle socket, and `raw.read(n)` blocks until it has n
    # bytes — so a hostile host can trickle one byte before each read timeout to keep a read
    # blocked forever while staying under the byte cap, and an in-loop deadline check would never
    # run. A watchdog closes the response when the wall-clock deadline expires, which unblocks the
    # read; we translate that closure into a non-retryable error. Cancel it and close the response
    # in `finally` so a completed or partial hostile stream can't leak the connection.
    timed_out = threading.Event()

    def _abort_on_deadline() -> None:
        timed_out.set()
        response.close()

    watchdog = threading.Timer(MAX_RESPONSE_DOWNLOAD_SECONDS, _abort_on_deadline)
    watchdog.start()
    try:
        if not response.ok:
            # Read only a capped snippet for diagnostics — don't buffer a hostile error body.
            snippet = response.raw.read(500, decode_content=True)
            logger.error(f"TeamCity API error: status={response.status_code}, body={snippet!r}, url={url}")
            response.raise_for_status()

        chunks: list[bytes] = []
        total = 0
        while True:
            try:
                chunk = response.raw.read(DOWNLOAD_CHUNK_BYTES, decode_content=True)
            except Exception:
                # The watchdog closing the socket surfaces here as a read error; report the deadline.
                if timed_out.is_set():
                    raise ValueError(
                        f"TeamCity API response exceeded the {MAX_RESPONSE_DOWNLOAD_SECONDS}s download deadline: url={url}"
                    )
                raise
            if not chunk:
                break
            total += len(chunk)
            if total > MAX_RESPONSE_BYTES:
                raise ValueError(
                    f"TeamCity API returned an oversized response (over {MAX_RESPONSE_BYTES} bytes): url={url}"
                )
            chunks.append(chunk)

        # The read can also finish (e.g. at EOF) right as the deadline fires; treat that as a timeout.
        if timed_out.is_set():
            raise ValueError(
                f"TeamCity API response exceeded the {MAX_RESPONSE_DOWNLOAD_SECONDS}s download deadline: url={url}"
            )

        return json.loads(b"".join(chunks))
    finally:
        watchdog.cancel()
        response.close()


def _paginate(
    session: requests.Session,
    server_root: str,
    first_url: str,
    headers: dict[str, str],
    response_key: str,
    logger: FilteringBoundLogger,
) -> Iterator[tuple[list[dict[str, Any]], str | None]]:
    """Yield ``(items, next_href)`` per page, following TeamCity's nextHref cursor.

    ``nextHref`` is supplied by the remote server, so pagination is bounded two ways to keep a
    misbehaving or malicious host from pinning an import worker: a repeated cursor (the same
    URL seen twice, e.g. a server that returns a fixed nextHref forever) aborts the walk, and a
    hard ``MAX_PAGES_PER_WALK`` cap bounds cursors that keep changing but never terminate.
    """
    url = first_url
    seen: set[str] = set()
    pages = 0
    while True:
        data = _fetch_page(session, url, headers, logger)
        next_href = data.get("nextHref")
        yield data.get(response_key, []), next_href
        if not next_href:
            return
        seen.add(url)
        pages += 1
        next_url = _resolve_next_href(server_root, next_href)
        if next_url in seen:
            logger.warning(f"TeamCity: pagination cursor repeated, aborting walk. url={next_url}, pages={pages}")
            return
        if pages >= MAX_PAGES_PER_WALK:
            logger.warning(f"TeamCity: page cap reached, truncating walk. pages={pages}, url={next_url}")
            return
        url = next_url


def _top_level_rows(
    session: requests.Session,
    host: str,
    headers: dict[str, str],
    config: TeamCityEndpointConfig,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[TeamCityResumeConfig],
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    server_root = _server_root(host)

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    if resume is not None and resume.next_href:
        logger.debug(f"TeamCity: resuming {config.name} from {resume.next_href}")
        first_url = _resolve_next_href(server_root, resume.next_href)
    else:
        locator_dimensions = {
            **config.locator_defaults,
            **_incremental_locator_dimensions(config, db_incremental_field_last_value),
            "count": str(config.page_size),
        }
        first_url = _build_list_url(_api_base(host), config.path, locator_dimensions, config.fields)

    for items, next_href in _paginate(session, server_root, first_url, headers, config.response_key, logger):
        if items:
            yield [_normalize_row(item, config.timestamp_fields) for item in items]
        # Save AFTER yielding so a crash re-yields the last page rather than skipping it —
        # merge dedupes on the primary key.
        if next_href:
            resumable_source_manager.save_state(TeamCityResumeConfig(next_href=next_href))


def _occurrence_rows_for_build(
    session: requests.Session,
    host: str,
    headers: dict[str, str],
    config: TeamCityEndpointConfig,
    build: dict[str, Any],
    logger: FilteringBoundLogger,
) -> Iterator[list[dict[str, Any]]]:
    build_id = build["id"]
    build_finish_date = _parse_teamcity_datetime(build.get("finishDate"))
    locator_dimensions = {"build": f"(id:{build_id})", "count": str(config.page_size)}
    first_url = _build_list_url(_api_base(host), config.path, locator_dimensions, config.fields)

    pages = 0
    for items, next_href in _paginate(session, _server_root(host), first_url, headers, config.response_key, logger):
        if items:
            for item in items:
                item[FAN_OUT_BUILD_ID_FIELD] = build_id
                item[FAN_OUT_BUILD_FINISH_DATE_FIELD] = build_finish_date
            yield items

        pages += 1
        if next_href and pages >= MAX_PAGES_PER_BUILD:
            logger.warning(
                f"TeamCity: page cap reached for {config.name}, truncating fan-out. "
                f"build_id={build_id}, pages={pages}, page_size={config.page_size}"
            )
            return
        if not next_href:
            return


def _fan_out_rows(
    session: requests.Session,
    host: str,
    headers: dict[str, str],
    config: TeamCityEndpointConfig,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[TeamCityResumeConfig],
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    builds_config = TEAMCITY_ENDPOINTS["builds"]
    server_root = _server_root(host)

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    if resume is not None and resume.next_href:
        logger.debug(f"TeamCity: resuming {config.name} fan-out from parent page {resume.next_href}")
        first_url = _resolve_next_href(server_root, resume.next_href)
    else:
        locator_dimensions = {
            **builds_config.locator_defaults,
            **_incremental_locator_dimensions(builds_config, db_incremental_field_last_value),
            "count": str(builds_config.page_size),
        }
        first_url = _build_list_url(_api_base(host), builds_config.path, locator_dimensions, FAN_OUT_PARENT_FIELDS)

    for builds, next_href in _paginate(session, server_root, first_url, headers, builds_config.response_key, logger):
        for build in builds:
            yield from _occurrence_rows_for_build(session, host, headers, config, build, logger)
        # Checkpoint at parent-page granularity, after every child of the page has been
        # yielded: a resume re-fans the interrupted page at worst, and merge dedupes.
        if next_href:
            resumable_source_manager.save_state(TeamCityResumeConfig(next_href=next_href))


def get_rows(
    host: str,
    access_token: str,
    endpoint: str,
    team_id: int,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[TeamCityResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    config = TEAMCITY_ENDPOINTS[endpoint]
    _check_host_safe(host, team_id)
    session = make_tracked_session(allow_redirects=False)
    headers = _headers(access_token)

    cursor = db_incremental_field_last_value if config.supports_incremental and should_use_incremental_field else None

    try:
        if config.fan_out_over_builds:
            yield from _fan_out_rows(session, host, headers, config, logger, resumable_source_manager, cursor)
        else:
            yield from _top_level_rows(session, host, headers, config, logger, resumable_source_manager, cursor)
    finally:
        session.close()


def teamcity_source(
    host: str,
    access_token: str,
    endpoint: str,
    team_id: int,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[TeamCityResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config = TEAMCITY_ENDPOINTS[endpoint]

    return SourceResponse(
        name=endpoint,
        items=lambda: get_rows(
            host=host,
            access_token=access_token,
            endpoint=endpoint,
            team_id=team_id,
            logger=logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=should_use_incremental_field,
            db_incremental_field_last_value=db_incremental_field_last_value,
        ),
        primary_keys=config.primary_keys,
        sort_mode=config.sort_mode,
        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,
    )


def validate_credentials(host: str, access_token: str, team_id: int) -> tuple[bool, int | None]:
    """Probe TeamCity's `/app/rest/server` endpoint to confirm the token is genuine.

    Returns ``(ok, status_code)``. ``status_code`` is ``None`` on a transport error. Raises
    ``ValueError`` if the host is malformed or unsafe so the caller can surface a precise
    message.
    """
    _check_host_safe(host, team_id)
    url = f"{_api_base(host)}/server"
    session = make_tracked_session(allow_redirects=False)
    try:
        response = session.get(url, headers=_headers(access_token), timeout=10)
    except Exception:
        return False, None
    finally:
        session.close()
    return response.status_code == 200, response.status_code