Build filter function map
parent
71d42b0e51
commit
7b46283a5c
|
@ -20,8 +20,8 @@ import uuid
|
||||||
|
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
from filters import (FILTER_OPS, FILTER_VALUE_TYPES, STIX_COMMON_FIELDS,
|
from stix2.sources.filters import (FILTER_OPS, FILTER_VALUE_TYPES,
|
||||||
STIX_COMMON_FILTERS_MAP)
|
STIX_COMMON_FIELDS, STIX_COMMON_FILTERS_MAP)
|
||||||
|
|
||||||
|
|
||||||
def make_id():
|
def make_id():
|
||||||
|
@ -273,7 +273,7 @@ class DataSource(object):
|
||||||
clean = False
|
clean = False
|
||||||
break
|
break
|
||||||
|
|
||||||
match = STIX_COMMON_FILTERS_MAP[filter_.field](filter_, stix_obj)
|
match = STIX_COMMON_FILTERS_MAP[filter_.field.split('.')[0]](filter_, stix_obj)
|
||||||
if not match:
|
if not match:
|
||||||
clean = False
|
clean = False
|
||||||
break
|
break
|
||||||
|
|
|
@ -13,8 +13,6 @@ or if cleaner solution possible.
|
||||||
import collections
|
import collections
|
||||||
import types
|
import types
|
||||||
|
|
||||||
import filters
|
|
||||||
|
|
||||||
# Currently, only STIX 2.0 common SDO fields (that are not complex objects)
|
# Currently, only STIX 2.0 common SDO fields (that are not complex objects)
|
||||||
# are supported for filtering on
|
# are supported for filtering on
|
||||||
STIX_COMMON_FIELDS = [
|
STIX_COMMON_FIELDS = [
|
||||||
|
@ -180,11 +178,11 @@ def check_labels_filter(filter_, stix_obj):
|
||||||
|
|
||||||
|
|
||||||
def check_modified_filter(filter_, stix_obj):
|
def check_modified_filter(filter_, stix_obj):
|
||||||
return _timestamp_filter(filter_, stix_obj["created"])
|
return _timestamp_filter(filter_, stix_obj["modified"])
|
||||||
|
|
||||||
|
|
||||||
def check_object_markings_ref_filter(filter_, stix_obj):
|
def check_object_marking_refs_filter(filter_, stix_obj):
|
||||||
for marking_id in stix_obj["object_market_refs"]:
|
for marking_id in stix_obj["object_marking_refs"]:
|
||||||
r = _id_filter(filter_, marking_id)
|
r = _id_filter(filter_, marking_id)
|
||||||
if r:
|
if r:
|
||||||
return r
|
return r
|
||||||
|
@ -199,29 +197,8 @@ def check_type_filter(filter_, stix_obj):
|
||||||
return _string_filter(filter_, stix_obj["type"])
|
return _string_filter(filter_, stix_obj["type"])
|
||||||
|
|
||||||
|
|
||||||
# script to collect STIX common field filter
|
# Create mapping of field names to filter functions
|
||||||
# functions and create mapping to them
|
for name, obj in dict(globals()).items():
|
||||||
|
if "check_" in name and isinstance(obj, types.FunctionType):
|
||||||
"""
|
field_name = "_".join(name.split("_")[1:-1])
|
||||||
MK: I want to build the filter name -> filter function dictionary
|
STIX_COMMON_FILTERS_MAP[field_name] = obj
|
||||||
dynamically whenever it is imported. By enumerating the functions
|
|
||||||
in this module, extracting the "check*" functions and making
|
|
||||||
pointers to them. But having issues getting an interable of the
|
|
||||||
modules entities. globals() works but returns an active dictionary
|
|
||||||
so iterating over it is a no go
|
|
||||||
"""
|
|
||||||
|
|
||||||
for entity in dir(filters):
|
|
||||||
if "check_" in str(entity) and isinstance(filters.__dict__.get(entity), types.FunctionType):
|
|
||||||
field_name = entity.split("_")[1].split("_")[0]
|
|
||||||
STIX_COMMON_FILTERS_MAP[field_name] = filters.__dict__.get(entity)
|
|
||||||
|
|
||||||
# Tried this to, didnt work ##############
|
|
||||||
"""
|
|
||||||
import sys
|
|
||||||
for entity in dir(sys.modules[__name__]):
|
|
||||||
print(entity)
|
|
||||||
if "check_" in str(entity) and type(entity) == "function":
|
|
||||||
print(sys.modules[__name__].__dict__.get(entity))
|
|
||||||
STIX_COMMON_FILTERS_MAP[str(entity)] = sys.modules[__name__].__dict__.get(entity)
|
|
||||||
"""
|
|
||||||
|
|
|
@ -24,7 +24,8 @@ import os
|
||||||
from stix2validator import validate_string
|
from stix2validator import validate_string
|
||||||
|
|
||||||
from stix2 import Bundle
|
from stix2 import Bundle
|
||||||
from stix2.sources import DataSink, DataSource, DataStore, Filter
|
from stix2.sources import DataSink, DataSource, DataStore
|
||||||
|
from stix2.sources.filters import Filter
|
||||||
|
|
||||||
|
|
||||||
class MemoryStore(DataStore):
|
class MemoryStore(DataStore):
|
||||||
|
|
|
@ -12,7 +12,8 @@ TODO: Test everything
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from stix2.sources import DataSink, DataSource, DataStore, Filter, make_id
|
from stix2.sources import DataSink, DataSource, DataStore, make_id
|
||||||
|
from stix2.sources.filters import Filter
|
||||||
|
|
||||||
TAXII_FILTERS = ['added_after', 'id', 'type', 'version']
|
TAXII_FILTERS = ['added_after', 'id', 'type', 'version']
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,8 @@ import pytest
|
||||||
from taxii2client import Collection
|
from taxii2client import Collection
|
||||||
|
|
||||||
from stix2.sources import (CompositeDataSource, DataSink, DataSource,
|
from stix2.sources import (CompositeDataSource, DataSink, DataSource,
|
||||||
DataStore, Filter, make_id, taxii)
|
DataStore, make_id, taxii)
|
||||||
|
from stix2.sources.filters import Filter
|
||||||
from stix2.sources.memory import MemorySource
|
from stix2.sources.memory import MemorySource
|
||||||
|
|
||||||
COLLECTION_URL = 'https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/'
|
COLLECTION_URL = 'https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/'
|
||||||
|
|
Loading…
Reference in New Issue