aboutsummaryrefslogtreecommitdiffstats
path: root/src/nvd/endpoints/cpe.py
blob: 9edd23afce4542fc6b4b686d3c9a58f105a802f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""CPE API endpoint."""

from typing import TYPE_CHECKING, AsyncIterator, Optional

from ..models import CPEData, CPEResponse

if TYPE_CHECKING:
    from ..client import NVDClient


class CPEEndpoint:
    """CPE (Common Platform Enumeration) API endpoint."""

    def __init__(self, client: "NVDClient") -> None:
        self.client = client

    async def get_cpe(self, cpe_name_id: str) -> CPEData:
        """Get a specific CPE by UUID.

        Args:
            cpe_name_id: CPE Name UUID

        Returns:
            CPE data object
        """
        response = await self.client.request(
            "GET",
            "/cpes/2.0",
            params={"cpeNameId": cpe_name_id},
            response_model=CPEResponse,
        )
        if not response.products:
            raise ValueError(f"CPE {cpe_name_id} not found")
        return response.products[0].cpe

    async def search_cpes(
        self,
        cpe_name_id: Optional[str] = None,
        cpe_match_string: Optional[str] = None,
        keyword_search: Optional[str] = None,
        keyword_exact_match: Optional[bool] = None,
        last_mod_start_date: Optional[str] = None,
        last_mod_end_date: Optional[str] = None,
        match_criteria_id: Optional[str] = None,
        results_per_page: int = 10000,
        start_index: int = 0,
    ) -> AsyncIterator[CPEData]:
        """Search for CPEs.

        Args:
            cpe_name_id: CPE Name UUID
            cpe_match_string: CPE match string pattern
            keyword_search: Keyword to search in titles and references
            keyword_exact_match: Require exact keyword match
            last_mod_start_date: Last modified start date (ISO-8601)
            last_mod_end_date: Last modified end date (ISO-8601)
            match_criteria_id: Match criteria UUID
            results_per_page: Results per page (max 10000)
            start_index: Starting index for pagination

        Yields:
            CPE data objects
        """
        params = {
            "cpeNameId": cpe_name_id,
            "cpeMatchString": cpe_match_string,
            "keywordSearch": keyword_search,
            "keywordExactMatch": keyword_exact_match,
            "lastModStartDate": last_mod_start_date,
            "lastModEndDate": last_mod_end_date,
            "matchCriteriaId": match_criteria_id,
            "resultsPerPage": results_per_page,
            "startIndex": start_index,
        }

        current_index = start_index
        while True:
            params["startIndex"] = current_index
            response = await self.client.request(
                "GET",
                "/cpes/2.0",
                params=params,
                response_model=CPEResponse,
            )

            for item in response.products:
                yield item.cpe

            # Check if there are more results
            if current_index + response.resultsPerPage >= response.totalResults:
                break

            current_index += response.resultsPerPage