Exemple #1
0
 def poll_collection(
         self, page_size: int,
         **kwargs) -> Union[types.GeneratorType, Dict[str, str]]:
     """
     Polls a taxii collection
     :param page_size: size of the request page
     """
     get_objects = self.collection_to_fetch.get_objects
     if isinstance(self.collection_to_fetch, v20.Collection):
         envelope = v20.as_pages(get_objects,
                                 per_request=page_size,
                                 **kwargs)
     else:
         envelope = get_objects(limit=page_size, **kwargs)
     return envelope
def test_get_collection_objects_paged_4(collection):
    obj_return = []
    for x in range(0, 50):
        obj_return.append(json.loads(STIX_OBJECT))

    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[:10]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items */*'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[10:20]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items */*'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[20:30]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items */*'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[30:40]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items */*'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[40:50]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items */*'})
    responses.add(responses.GET, GET_OBJECTS_URL, "",
                  status=406, content_type=MEDIA_TYPE_STIX_V20)
    response = []

    # The status errors (any 400-500) will make it stop
    with pytest.raises(requests.exceptions.HTTPError):
        for bundle in as_pages(collection.get_objects, per_request=10):
            response.extend(bundle.get("objects", []))

    assert len(response) == 50
Exemple #3
0
    def fetch_stix_objects_from_api(self, test: bool = False, **kwargs):
        """Retrieves all entries from the feed.

        Args:
            test: Whether it was called during clicking the test button or not - designed to save time.

        """
        data = []

        server = Server(url=self._base_url, auth=TokenAuth(key=self._api_key), verify=self._verify,
                        proxies=self._proxies)

        for api_root in server.api_roots:
            for collection in api_root.collections:
                for bundle in as_pages(collection.get_objects, per_request=100, **kwargs):
                    data.extend(bundle.get('objects'))
                    if test:
                        return data

        self.objects_data[kwargs.get('type')] = data
def test_get_collection_objects_paged_1(collection):
    obj_return = []
    for x in range(0, 50):
        obj_return.append(json.loads(STIX_OBJECT))

    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[:10]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items 0-9/50'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[10:20]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items 10-19/50'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[20:30]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items 20-29/50'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[30:40]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items 30-39/50'})
    responses.add(responses.GET, GET_OBJECTS_URL,
                  json.dumps({"type": "bundle", "spec_version": "2.0",
                              "id": "bundle--5d0092c5-5f74-4287-9642-33f4c354e56d",
                              "objects": obj_return[40:50]}),
                  status=200, content_type=MEDIA_TYPE_STIX_V20,
                  headers={'Content-Range': 'items 40-49/50'})
    response = []

    for bundle in as_pages(collection.get_objects, per_request=10):
        response.extend(bundle.get("objects", []))

    assert len(response) == 50
Exemple #5
0
    def get_stix_objects(self, test: bool = False) -> list:
        """Retrieves all entries from the feed.

        Args:
            test: Whether it was called during clicking the test button or not - designed to save time.
        Returns:
            A list of stix objects, containing the indicators.
        """
        data = []
        server = Server(url=self._base_url,
                        auth=TokenAuth(key=self._api_key),
                        verify=self._verify,
                        proxies=self._proxies)

        for api_root in server.api_roots:
            for collection in api_root.collections:
                for bundle in as_pages(collection.get_objects,
                                       per_request=100):
                    data.extend(bundle.get('objects'))
                    if test:
                        break
        return data
Exemple #6
0
 def poll_collection(
         self, page_size: int,
         **kwargs) -> Dict[str, Union[types.GeneratorType, Dict[str, str]]]:
     """
     Polls a taxii collection
     :param page_size: size of the request page
     """
     types_envelopes = {}
     get_objects = self.collection_to_fetch.get_objects
     if len(self.objects_to_fetch
            ) > 1:  # when fetching one type no need to fetch relationship
         self.objects_to_fetch.append('relationship')
     for obj_type in self.objects_to_fetch:
         kwargs['type'] = obj_type
         if isinstance(self.collection_to_fetch, v20.Collection):
             envelope = v20.as_pages(get_objects,
                                     per_request=page_size,
                                     **kwargs)
         else:
             envelope = get_objects(limit=page_size, **kwargs)
         if envelope:
             types_envelopes[obj_type] = envelope
     return types_envelopes
    def fetch_stix_objects_from_api(self,
                                    test: bool = False,
                                    limit: int = -1,
                                    **kwargs):
        """Retrieves all entries from the feed.

        Args:
            test: Whether it was called during clicking the test button or not - designed to save time.
            limit: number of indicators for get command
        """
        data: list = []
        for api_root in self.server.api_roots:
            for collection in api_root.collections:
                for bundle in as_pages(collection.get_objects,
                                       per_request=100,
                                       **kwargs):
                    data.extend(bundle.get('objects') or [])
                    if test and limit < len(data):
                        return data

        if test:
            return data
        self.objects_data[kwargs.get('type')] = data