2024-02-26 15:33:26 +01:00
import validators
2024-03-05 11:01:51 +01:00
2024-02-26 15:33:26 +01:00
class Cluster :
2024-03-01 16:30:49 +01:00
def __init__ ( self , uuid , galaxy , description = None , value = None , meta = None ) :
2024-02-26 15:33:26 +01:00
self . uuid = uuid
2024-03-01 16:30:49 +01:00
self . description = description
2024-02-26 15:33:26 +01:00
self . value = value
self . meta = meta
2024-03-01 16:30:49 +01:00
self . galaxy = galaxy # Reference to the Galaxy object this cluster belongs to
2024-03-05 11:01:51 +01:00
self . outbound_relationships = set ( )
self . inbound_relationships = set ( )
self . relationships = set ( )
2024-03-01 16:30:49 +01:00
def add_outbound_relationship ( self , cluster ) :
self . outbound_relationships . add ( cluster )
def add_inbound_relationship ( self , cluster ) :
self . inbound_relationships . add ( cluster )
def save_relationships ( self , relationships ) :
self . relationships = relationships
def generate_entry ( self ) :
entry = " "
entry + = self . _create_title_entry ( )
entry + = self . _create_description_entry ( )
entry + = self . _create_synonyms_entry ( )
entry + = self . _create_uuid_entry ( )
entry + = self . _create_refs_entry ( )
entry + = self . _create_associated_metadata_entry ( )
if self . relationships :
entry + = self . _create_related_entry ( )
return entry
2024-03-05 11:01:51 +01:00
2024-02-26 15:33:26 +01:00
def _create_title_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
entry + = f " ## { self . value } \n "
entry + = f " \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_description_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
2024-02-26 15:33:26 +01:00
if self . description :
2024-03-01 16:30:49 +01:00
entry + = f " { self . description } \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_synonyms_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
2024-02-26 15:33:26 +01:00
if isinstance ( self . meta , dict ) and self . meta . get ( " synonyms " ) :
2024-03-01 16:30:49 +01:00
entry + = f " \n "
entry + = f ' ??? info " Synonyms " \n '
entry + = f " \n "
entry + = f ' " synonyms " in the meta part typically refer to alternate names or labels that are associated with a particular { self . value } . \n \n '
entry + = f " | Known Synonyms | \n "
entry + = f " |---------------------| \n "
2024-02-26 15:33:26 +01:00
synonyms_count = 0
for synonym in sorted ( self . meta [ " synonyms " ] ) :
synonyms_count + = 1
2024-03-01 16:30:49 +01:00
entry + = f " | ` { synonym } ` | \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_uuid_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
2024-02-26 15:33:26 +01:00
if self . uuid :
2024-03-01 16:30:49 +01:00
entry + = f " \n "
entry + = f ' ??? tip " Internal MISP references " \n '
entry + = f " \n "
entry + = f " UUID ` { self . uuid } ` which can be used as unique global reference for ` { self . value } ` in MISP communities and other software using the MISP galaxy \n "
entry + = f " \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_refs_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
2024-02-26 15:33:26 +01:00
if isinstance ( self . meta , dict ) and self . meta . get ( " refs " ) :
2024-03-01 16:30:49 +01:00
entry + = f " \n "
entry + = f ' ??? info " External references " \n '
entry + = f " \n "
2024-02-26 15:33:26 +01:00
for ref in self . meta [ " refs " ] :
if validators . url ( ref ) :
2024-03-01 16:30:49 +01:00
entry + = f " - [ { ref } ]( { ref } ) - :material-archive: :material-arrow-right: [webarchive](https://web.archive.org/web/*/ { ref } ) \n "
2024-02-26 15:33:26 +01:00
else :
2024-03-01 16:30:49 +01:00
entry + = f " - { ref } \n "
2024-02-26 15:33:26 +01:00
2024-03-01 16:30:49 +01:00
entry + = f " \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_associated_metadata_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
2024-02-26 15:33:26 +01:00
if isinstance ( self . meta , dict ) :
excluded_meta = [ " synonyms " , " refs " ]
2024-03-01 16:30:49 +01:00
entry + = f " \n "
entry + = f ' ??? info " Associated metadata " \n '
entry + = f " \n "
entry + = f " |Metadata key {{ .no-filter }} |Value| \n "
entry + = f " |-----------------------------------|-----| \n "
2024-02-26 15:33:26 +01:00
for meta in sorted ( self . meta . keys ( ) ) :
if meta not in excluded_meta :
2024-03-01 16:30:49 +01:00
entry + = f " | { meta } | { self . meta [ meta ] } | \n "
return entry
2024-02-26 15:33:26 +01:00
def _create_related_entry ( self ) :
2024-03-01 16:30:49 +01:00
entry = " "
entry + = f " \n "
entry + = f ' ??? info " Related clusters " \n '
entry + = f " \n "
entry + = f " To see the related clusters, click [here](./relations/ { self . uuid } .md). \n "
2024-03-05 11:01:51 +01:00
return entry