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.

7.2 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceFieldSelectConfig,
    SourceFieldSelectConfigOption,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)
from products.warehouse_sources.sources.veracode._config import VeracodeSourceConfig
from products.warehouse_sources.sources.veracode.settings import ENDPOINTS, INCREMENTAL_FIELDS, VERACODE_ENDPOINTS
from products.warehouse_sources.sources.veracode.veracode import (
    VeracodeResumeConfig,
    validate_credentials as validate_veracode_credentials,
    veracode_source,
)


@SourceRegistry.register
class VeracodeSource(ResumableSource[VeracodeSourceConfig, VeracodeResumeConfig]):
    codebase_detection = detect(env=("^VERACODE_",), pypi=("veracode-api-signing",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.VERACODE

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.VERACODE,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Veracode",
            caption=(
                "Connect Veracode with an API service account's **API ID** and **secret key**, generated under "
                "**Account settings → API credentials** in the Veracode Platform. Requests are signed with "
                "Veracode's HMAC scheme.\n\n"
                "The service account needs the **Results API** and **Applications API** roles to read the "
                "application portfolio and findings. Pick the region your Veracode account lives in — data is "
                "isolated per region."
            ),
            docsUrl="https://posthog.com/docs/cdp/sources/veracode",
            iconPath="/static/services/veracode.png",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_id",
                        label="API ID",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="api_secret",
                        label="API secret key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldSelectConfig(
                        name="region",
                        label="Region",
                        required=True,
                        defaultValue="com",
                        options=[
                            SourceFieldSelectConfigOption(label="US commercial (api.veracode.com)", value="com"),
                            SourceFieldSelectConfigOption(label="European (api.veracode.eu)", value="eu"),
                            SourceFieldSelectConfigOption(label="US federal (api.veracode.us)", value="us"),
                        ],
                    ),
                ],
            ),
            releaseStatus=ReleaseStatus.BETA,
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.veracode.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized": (
                "Veracode rejected the credentials. Generate a fresh API ID and secret key for the service "
                "account in the Veracode Platform (Account settings → API credentials) and reconnect."
            ),
            "403 Client Error: Forbidden": (
                "The Veracode service account is missing the roles needed to sync this data. Grant the Results "
                "API and Applications API roles to the account, then reconnect."
            ),
        }

    def get_schemas(
        self,
        config: VeracodeSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        schemas = [
            SourceSchema(
                name=name,
                supports_incremental=bool(INCREMENTAL_FIELDS.get(name)),
                supports_append=bool(INCREMENTAL_FIELDS.get(name)),
                incremental_fields=INCREMENTAL_FIELDS.get(name, []),
                should_sync_default=endpoint.should_sync_default,
            )
            for name, endpoint in VERACODE_ENDPOINTS.items()
        ]

        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]

        return schemas

    def validate_credentials(
        self,
        config: VeracodeSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        ok, status_code = validate_veracode_credentials(config.api_id, config.api_secret, config.region)
        if ok:
            return True, None

        # A 403 means the token is genuine but the service account lacks a role. Accept it at
        # source-create (the user may only have granted roles for the tables they want) and only
        # surface it when validating a specific schema.
        if status_code == 403 and schema_name is None:
            return True, None

        if status_code in (401, 403):
            return False, "Veracode rejected the credentials. Check the API ID, secret key, region, and account roles."

        return False, "Could not reach Veracode with the provided credentials."

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[VeracodeResumeConfig]:
        return ResumableSourceManager[VeracodeResumeConfig](inputs, VeracodeResumeConfig)

    def source_for_pipeline(
        self,
        config: VeracodeSourceConfig,
        resumable_source_manager: ResumableSourceManager[VeracodeResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        if inputs.schema_name not in ENDPOINTS:
            raise ValueError(f"Unknown Veracode endpoint: {inputs.schema_name}")

        return veracode_source(
            api_id=config.api_id,
            api_secret=config.api_secret,
            region=config.region,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )