77import json
88import uuid
99import base64
10+ import logging
1011
1112from pycti .opencti_stix2 import OpenCTIStix2
1213
@@ -15,28 +16,18 @@ class OpenCTI:
1516 """
1617 Python API for OpenCTI
1718 :param url: OpenCTI URL
18- :param key: The API key
19- :param verbose: Log all requests. Defaults to None
20- :param stdout: Display log to stdout. Defaults to None
19+ :param token: The API key
2120 """
2221
23- def __init__ (self , url , key , log_file = '' , verbose = True , stdout = True ):
22+ def __init__ (self , url , token ):
2423 self .api_url = url + '/graphql'
25- self .log_file = log_file
26- self .verbose = verbose
27- self .stdout = stdout
2824 self .request_headers = {
29- 'Authorization' : 'Bearer ' + key ,
25+ 'Authorization' : 'Bearer ' + token ,
3026 'Content-Type' : 'application/json'
3127 }
3228
3329 def log (self , message ):
34- if self .stdout :
35- print (message )
36- if self .verbose and len (self .log_file ) > 0 :
37- file = open (self .log_file , 'a' )
38- file .write ('[' + datetime .datetime .today ().strftime ('%Y-%m-%d %H:%M:%S' ) + '] ' + message + "\n " )
39- file .close ()
30+ logging .info ('[' + datetime .datetime .today ().strftime ('%Y-%m-%d %H:%M:%S' ) + '] ' + message + "\n " )
4031
4132 def query (self , query , variables = {}):
4233 r = requests .post (self .api_url , json = {'query' : query , 'variables' : variables }, headers = self .request_headers )
@@ -409,7 +400,8 @@ def get_stix_relation_by_id(self, id):
409400 result = self .query (query , {'id' : id })
410401 return result ['data' ]['stixRelation' ]
411402
412- def get_stix_relations (self , from_id = None , to_id = None , type = 'stix_relation' , first_seen = None , last_seen = None , inferred = False ):
403+ def get_stix_relations (self , from_id = None , to_id = None , type = 'stix_relation' , first_seen = None , last_seen = None ,
404+ inferred = False ):
413405 self .log ('Getting relations, from: ' + from_id + ', to: ' + to_id + '...' )
414406 if type == 'revoked-by' :
415407 return []
@@ -1717,8 +1709,10 @@ def create_incident_if_not_exists(self,
17171709 object_result = self .check_existing_stix_domain_entity (stix_id , name , 'Incident' )
17181710 if object_result is not None :
17191711 self .update_stix_domain_entity_field (object_result ['id' ], 'name' , name )
1720- description is not None and self .update_stix_domain_entity_field (object_result ['id' ], 'description' , description )
1721- first_seen is not None and self .update_stix_domain_entity_field (object_result ['id' ], 'first_seen' , first_seen )
1712+ description is not None and self .update_stix_domain_entity_field (object_result ['id' ], 'description' ,
1713+ description )
1714+ first_seen is not None and self .update_stix_domain_entity_field (object_result ['id' ], 'first_seen' ,
1715+ first_seen )
17221716 last_seen is not None and self .update_stix_domain_entity_field (object_result ['id' ], 'last_seen' , last_seen )
17231717 return object_result
17241718 else :
@@ -2509,7 +2503,8 @@ def create_course_of_action(self, name, description, id=None, stix_id=None, crea
25092503 })
25102504 return result ['data' ]['courseOfActionAdd' ]
25112505
2512- def create_course_of_action_if_not_exists (self , name , description , id = None , stix_id = None , created = None , modified = None ):
2506+ def create_course_of_action_if_not_exists (self , name , description , id = None , stix_id = None , created = None ,
2507+ modified = None ):
25132508 object_result = self .check_existing_stix_domain_entity (stix_id , name , 'Course-Of-Action' )
25142509 if object_result is not None :
25152510 return object_result
0 commit comments