CovertMark.data.mongo module

class CovertMark.data.mongo.MongoDBManager(db_server='mongodb://localhost:27017/')[source]

Bases: object

A manager for the MongoDB used to store trace data, both for temporary working and long term storage, as demanded.

count_packets(collection_name, query_params={})[source]

Return the number of query-matched packets in the named collection.

Parameters:
  • collection_name (str) – name of the queried collection.
  • query_params (dict) – query written in MongoDB query object format.
Returns:

the number of packets found matching the query parameters.

delete_collection(collection_name)[source]

Delete the index and the trace collection associated with collection_name.

Parameters:collection_name (str) – the name of the collection to be deleted.
Returns:True if deleted where appropriate, False otherwise.
delete_packets(collection_name, query_params)[source]

Delete matched packets in the named collection.

Parameters:
  • collection_name (str) – name of the queried collection.
  • query_params (str) – query written in MongoDB query object format.
Returns:

packets deleted matching the query parameters.

distinct_packets(collection_name, field_name)[source]

Return the number of distinct fields of a column in the named collection.

Parameters:
  • collection_name (str) – name of the queried collection.
  • field_name (str) – name of column to count distinct packets.
Returns:

the number of distinct fields found.

find_packets(collection_name, query_params, max_r=0)[source]

Return matched packets in the named collection up to a max of max_r packets.

Parameters:
  • collection_name (str) – name of the queried collection.
  • query_params (dict) – query written in MongoDB query object format.
  • max_r (int) – maximum number of returned packets, <= 0 means unlimited.
Returns:

packets found matching the query parameters.

static generate_name()[source]

Generate a packet collection name in the format of ‘traces(yyyymmdd)random-hex-string’.

Returns:a random collection name.
insert_packets(packets, collection_name='')[source]

Insert a list of fomatted packets. Should be used only by parser.PCAPParser.load_packet_info(), as format checking is not done here.

Parameters:
  • packets (list) – see docstring of that function for input format.
  • collection_name (str) – The name of the collection to be inserted into, create a new collection with random name if unspecified.
Returns:

dict containing collection name and inserted count if insertion successful, False otherwise.

list_collections()[source]

Return all valid collections.

Returns:a list of valid collections with attributes.
static log_error()[source]

Utility function to log database errors.

lookup_collection(collection_name)[source]

Check whether a collection by the name exists in MongoDB.

Parameters:collection_name (str) – the name of collection checked.
Returns:True if collection name exists, False otherwise.
modify_collection_description(collection_name, description)[source]

Modify the description of a trace collection.

Parameters:
  • collection_name (str) – the name of the collection to be modified.
  • description (str) – the new description of the collection.
Returns:

True if modification successful, False otherwise.

new_collection(description='', input_filters=[])[source]

Create a new trace collection with a name, store and return it.

Parameters:
  • description (str) – a description of this trace collection, empty by default.
  • input_filters (list) – list of tuples (string-format filters, direction) for input filters of this collection.
Returns:

the name of the new collection.