@@ -253,10 +253,18 @@ def _data_handler(self, json_data) -> None:
253253 event_data = json_data ["event" ]
254254 entity_id = event_data .get ("entity_id" )
255255 entity_type = event_data .get ("entity_type" )
256+ validation_mode = event_data .get ("validation_mode" , "workbench" )
256257 # Set the API headers
257- work_id = json_data ["internal" ]["work_id" ]
258+ internal_data = json_data ["internal" ]
259+ work_id = internal_data ["work_id" ]
260+ draft_id = internal_data .get ("draft_id" , "" )
258261 self .helper .work_id = work_id
259262
263+ self .helper .validation_mode = validation_mode
264+ self .helper .draft_id = draft_id
265+ self .helper .api .set_draft_id (draft_id )
266+ self .helper .api_impersonate .set_draft_id (draft_id )
267+
260268 self .helper .playbook = None
261269 self .helper .enrichment_shared_organizations = None
262270 if self .helper .connect_type == "INTERNAL_ENRICHMENT" :
@@ -952,6 +960,8 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
952960 "Connector registered with ID" , {"id" : self .connect_id }
953961 )
954962 self .work_id = None
963+ self .validation_mode = "workbench"
964+ self .draft_id = None
955965 self .playbook = None
956966 self .enrichment_shared_organizations = None
957967 self .connector_id = connector_configuration ["id" ]
@@ -1550,6 +1560,7 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
15501560 """send a stix2 bundle to the API
15511561
15521562 :param work_id: a valid work id
1563+ :param draft_id: a draft context to send the bundle to
15531564 :param bundle: valid stix2 bundle
15541565 :type bundle:
15551566 :param entities_types: list of entities, defaults to None
@@ -1563,6 +1574,8 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
15631574 :rtype: list
15641575 """
15651576 work_id = kwargs .get ("work_id" , self .work_id )
1577+ validation_mode = kwargs .get ("validation_mode" , self .validation_mode )
1578+ draft_id = kwargs .get ("draft_id" , self .draft_id )
15661579 entities_types = kwargs .get ("entities_types" , None )
15671580 update = kwargs .get ("update" , False )
15681581 event_version = kwargs .get ("event_version" , None )
@@ -1627,14 +1640,23 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
16271640 # Upload workbench in case of pending validation
16281641 if not file_name and work_id :
16291642 file_name = f"{ work_id } .json"
1643+
16301644 if self .connect_validate_before_import and not bypass_validation and file_name :
1631- self .api .upload_pending_file (
1632- file_name = file_name ,
1633- data = bundle ,
1634- mime_type = "application/json" ,
1635- entity_id = entity_id ,
1636- )
1637- return []
1645+ if validation_mode == "workbench" :
1646+ self .api .upload_pending_file (
1647+ file_name = file_name ,
1648+ data = bundle ,
1649+ mime_type = "application/json" ,
1650+ entity_id = entity_id ,
1651+ )
1652+ return []
1653+ elif validation_mode == "draft" and not draft_id :
1654+ draft_id = self .api .create_draft (
1655+ draft_name = file_name , entity_id = entity_id
1656+ )
1657+ if not draft_id :
1658+ self .connector_logger .error ("Draft couldn't be created" )
1659+ return []
16381660
16391661 # If directory setup, write the bundle to the target directory
16401662 if bundle_send_to_directory and bundle_send_to_directory_path is not None :
@@ -1749,6 +1771,7 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
17491771 entities_types = entities_types ,
17501772 sequence = sequence ,
17511773 update = update ,
1774+ draft_id = draft_id ,
17521775 )
17531776 channel .close ()
17541777 pika_connection .close ()
@@ -1774,11 +1797,14 @@ def _send_bundle(self, channel, bundle, **kwargs) -> None:
17741797 :type entities_types: list, optional
17751798 :param update: whether to update data in the database, defaults to False
17761799 :type update: bool, optional
1800+ :param draft_id: if draft_id is set, bundle must be set in draft context
1801+ :type draft_id:
17771802 """
17781803 work_id = kwargs .get ("work_id" , None )
17791804 sequence = kwargs .get ("sequence" , 0 )
17801805 update = kwargs .get ("update" , False )
17811806 entities_types = kwargs .get ("entities_types" , None )
1807+ draft_id = kwargs .get ("draft_id" , None )
17821808
17831809 if entities_types is None :
17841810 entities_types = []
@@ -1800,6 +1826,7 @@ def _send_bundle(self, channel, bundle, **kwargs) -> None:
18001826 "utf-8"
18011827 ),
18021828 "update" : update ,
1829+ "draft_id" : draft_id ,
18031830 }
18041831 if work_id is not None :
18051832 message ["work_id" ] = work_id
0 commit comments