def get_objects(self, api_root, id_, filter_args, allowed_filters,
                    start_index, end_index):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            objs = []
            for collection in collections:
                if "id" in collection and id_ == collection["id"]:

                    if filter_args:
                        full_filter = BasicFilter(filter_args)
                        objs.extend(
                            full_filter.process_filter(
                                collection.get("objects", []),
                                allowed_filters,
                                collection.get("manifest", []),
                            ), )
                    else:
                        objs.extend(collection.get("objects", []))

            count = len(objs)
            result = objs[start_index:end_index]

            return count, create_bundle(result)
Example #2
0
 def get_objects(self,
                 accept=MEDIA_TYPE_STIX_V20,
                 start=0,
                 per_request=0,
                 **filter_kwargs):
     self._verify_can_read()
     query_params = _filter_kwargs_to_query_params(filter_kwargs)
     assert isinstance(query_params, dict)
     full_filter = BasicFilter(query_params)
     objs = full_filter.process_filter(
         self.objects,
         ("id", "type", "version"),
         self.manifests,
         100,
     )[0]
     if objs:
         resp = Response()
         resp.status_code = 200
         resp.headers["Content-Range"] = f"items 0-{len(objs)}/{len(objs)}"
         resp.encoding = "utf-8"
         resp._content = bytes(
             stix2.v20.Bundle(objects=objs).serialize(ensure_ascii=False),
             resp.encoding)
         return resp
     else:
         resp = Response()
         resp.status_code = 404
         resp.raise_for_status()
Example #3
0
    def get_object(self, id, **filter_kwargs):
        self._verify_can_read()
        query_params = _filter_kwargs_to_query_params(filter_kwargs)
        assert isinstance(query_params, dict)
        full_filter = BasicFilter(query_params)

        # In this endpoint we must first filter objects by id beforehand.
        objects = [x for x in self.objects if x["id"] == id]
        if objects:
            filtered_objects = full_filter.process_filter(
                objects,
                ("version",),
                self.manifests,
                100,
            )[0]
        else:
            filtered_objects = []
        if filtered_objects:
            return {
                "objects": filtered_objects,
                "more": False,
            }
        else:
            resp = Response()
            resp.status_code = 404
            resp.raise_for_status()
Example #4
0
    def get_object_manifest(self, api_root, id_, filter_args, allowed_filters):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    manifest = collection.get("manifest", [])
                    if filter_args:
                        full_filter = BasicFilter(filter_args)
                        manifest = full_filter.process_filter(
                            manifest, allowed_filters, None)
                    return manifest
Example #5
0
 def get_objects(self, **filter_kwargs):
     self._verify_can_read()
     query_params = _filter_kwargs_to_query_params(filter_kwargs)
     if not isinstance(query_params, dict):
         query_params = json.loads(query_params)
     full_filter = BasicFilter(query_params or {})
     objs = full_filter.process_filter(self.objects,
                                       ("id", "type", "version"), [])
     if objs:
         return Bundle(objects=objs)
     else:
         resp = Response()
         resp.status_code = 404
         resp.raise_for_status()
Example #6
0
 def get_object(self, id, version=None):
     self._verify_can_read()
     query_params = None
     if version:
         query_params = _filter_kwargs_to_query_params({"version": version})
     if query_params:
         query_params = json.loads(query_params)
     full_filter = BasicFilter(query_params or {})
     objs = full_filter.process_filter(self.objects, ("version", ), [])
     if objs:
         return Bundle(objects=objs)
     else:
         resp = Response()
         resp.status_code = 404
         resp.raise_for_status()
Example #7
0
    def get_object(self, api_root, id_, object_id, filter_args,
                   allowed_filters):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            objs = []
            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    for obj in collection.get("objects", []):
                        if object_id == obj["id"]:
                            objs.append(obj)
                if filter_args:
                    full_filter = BasicFilter(filter_args)
                    objs = full_filter.process_filter(
                        objs, allowed_filters, collection.get("manifest", []))
            return create_bundle(objs)
 def get_objects(self, **filter_kwargs):
     self._verify_can_read()
     query_params = _filter_kwargs_to_query_params(filter_kwargs)
     assert isinstance(query_params, dict)
     full_filter = BasicFilter(query_params)
     objs = full_filter.process_filter(
         self.objects,
         ("id", "type", "version"),
         self.manifests,
         100,
     )[0]
     if objs:
         return stix2.v21.Bundle(objects=objs)
     else:
         resp = Response()
         resp.status_code = 404
         resp.raise_for_status()
Example #9
0
    def get_object_manifest(self, api_root, id_, filter_args, allowed_filters,
                            start_index, end_index):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    manifest = collection.get("manifest", [])
                    if filter_args:
                        full_filter = BasicFilter(filter_args)
                        manifest = full_filter.process_filter(
                            manifest, allowed_filters, None)
                    count = len(manifest)
                    result = manifest[start_index:end_index]

                    return count, result
Example #10
0
    def get_objects(self, api_root, id_, filter_args, allowed_filters):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            objs = []
            for collection in collections:
                if "id" in collection and id_ == collection["id"]:

                    if filter_args:
                        full_filter = BasicFilter(filter_args)
                        objs.extend(
                            full_filter.process_filter(
                                collection.get("objects", []), allowed_filters,
                                collection.get("manifest", [])))
                    else:
                        objs.extend(collection.get("objects", []))
            return create_bundle(objs)
    def get_object_manifest(self, api_root, collection_id, filter_args,
                            allowed_filters, start_index, end_index):
        self.update_discovery_config()

        if self.validate_requested_api_root(api_root):
            count, collections = self.get_collections(api_root, 0, -1)

            for collection in collections:
                if 'id' in collection and collection_id == collection['id']:
                    manifest = collection.get('manifest', [])
                    if filter_args:
                        full_filter = BasicFilter(filter_args)
                        manifest = full_filter.process_filter(
                            manifest, allowed_filters, None)

                    count = len(manifest)

                    manifest = manifest if end_index == -1 else manifest[
                        start_index:end_index]

                    return count, manifest
    def get_objects_without_bundle(self, api_root, collection_id, filter_args,
                                   allowed_filters):
        self.update_discovery_config()

        if self.validate_requested_api_root(api_root):
            # Get the collection
            collection = None
            num_collections, collections = self.get_collections(
                api_root, 0, -1)

            for c in collections:
                if 'id' in c and collection_id == c['id']:
                    collection = c
                    break

            if not collection:
                raise ProcessingError(
                    "collection for api-root '{}' was not found".format(
                        api_root), 500)

            # Add the objects to the collection
            collection['objects'] = self.with_cache(api_root)

            # Filter the collection
            filtered_objects = []

            if filter_args:
                full_filter = BasicFilter(filter_args)
                filtered_objects.extend(
                    full_filter.process_filter(collection.get('objects', []),
                                               allowed_filters,
                                               collection.get('manifest', [])))
            else:
                filtered_objects.extend(collection.get('objects', []))

            return filtered_objects