def upsert_domain(domain, source, username=None, campaign=None, confidence=None, bucket_list=None, ticket=None, cache={}): """ Add or update a domain/FQDN. Campaign is assumed to be a list of campaign dictionary objects. :param domain: The domain to add/update. :type domain: str :param source: The name of the source. :type source: str :param username: The user adding/updating the domain. :type username: str :param campaign: The campaign to attribute to this domain. :type campaign: list, str :param confidence: Confidence for the campaign attribution. :type confidence: str :param bucket_list: List of buckets to add to this domain. :type bucket_list: list, str :param ticket: The ticket for this domain. :type ticket: str :param cache: Cached data, typically for performance enhancements during bulk uperations. :type cache: dict :returns: dict with keys: "success" (boolean), "object" the domain that was added, "is_domain_new" (boolean) """ # validate domain and grab root domain (root, domain, error) = get_valid_root_domain(domain) if error: return {'success': False, 'message': error} is_fqdn_domain_new = False is_root_domain_new = False if not campaign: campaign = [] # assume it's a list, but check if it's a string elif isinstance(campaign, basestring): c = EmbeddedCampaign(name=campaign, confidence=confidence, analyst=username) campaign = [c] # assume it's a list, but check if it's a string if isinstance(source, basestring): s = EmbeddedSource() s.name = source instance = EmbeddedSource.SourceInstance() instance.reference = '' instance.method = '' instance.analyst = username instance.date = datetime.datetime.now() s.instances = [instance] source = [s] fqdn_domain = None root_domain = None cached_results = cache.get(form_consts.Domain.CACHED_RESULTS) if cached_results != None: if domain != root: fqdn_domain = cached_results.get(domain) root_domain = cached_results.get(root) else: root_domain = cached_results.get(root) else: #first find the domain(s) if it/they already exist root_domain = Domain.objects(domain=root).first() if domain != root: fqdn_domain = Domain.objects(domain=domain).first() #if they don't exist, create them if not root_domain: root_domain = Domain() root_domain.domain = root root_domain.source = [] root_domain.record_type = 'A' is_root_domain_new = True if cached_results != None: cached_results[root] = root_domain if domain != root and not fqdn_domain: fqdn_domain = Domain() fqdn_domain.domain = domain fqdn_domain.source = [] fqdn_domain.record_type = 'A' is_fqdn_domain_new = True if cached_results != None: cached_results[domain] = fqdn_domain # if new or found, append the new source(s) for s in source: if root_domain: root_domain.add_source(s) if fqdn_domain: fqdn_domain.add_source(s) #campaigns #both root and fqdn get campaigns updated for c in campaign: if root_domain: root_domain.add_campaign(c) if fqdn_domain: fqdn_domain.add_campaign(c) if username: if root_domain: root_domain.analyst = username if fqdn_domain: fqdn_domain.analyst = username if bucket_list: if root_domain: root_domain.add_bucket_list(bucket_list, username) if fqdn_domain: fqdn_domain.add_bucket_list(bucket_list, username) if ticket: if root_domain: root_domain.add_ticket(ticket, username) if fqdn_domain: fqdn_domain.add_ticket(ticket, username) # save try: if root_domain: root_domain.save(username=username) if fqdn_domain: fqdn_domain.save(username=username) except Exception, e: return {'success': False, 'message': e}
def upsert_domain(sdomain, domain, source, username=None, campaign=None, confidence=None, bucket_list=None, ticket=None, cache={}): """ Add or update a domain/FQDN. Campaign is assumed to be a list of campaign dictionary objects. :param sdomain: Response from parsing the domain for a root domain. Will either be an error message or the root domain itself. :type sdomain: str :param domain: The domain to add/update. :type domain: str :param source: The name of the source. :type source: str :param username: The user adding/updating the domain. :type username: str :param campaign: The campaign to attribute to this domain. :type campaign: list, str :param confidence: Confidence for the campaign attribution. :type confidence: str :param bucket_list: List of buckets to add to this domain. :type bucket_list: list, str :param ticket: The ticket for this domain. :type ticket: str :param cache: Cached data, typically for performance enhancements during bulk uperations. :type cache: dict :returns: dict with keys: "success" (boolean), "object" the domain that was added, "is_domain_new" (boolean) """ if sdomain == "no_tld_found_error": #oops... return {'success': False, 'message': "Invalid domain: %s " % sdomain} is_fqdn_domain_new = False is_root_domain_new = False if not campaign: campaign = [] # assume it's a list, but check if it's a string elif isinstance(campaign, basestring): c = EmbeddedCampaign(name=campaign, confidence=confidence, analyst=username) campaign = [c] # assume it's a list, but check if it's a string if isinstance(source, basestring): s = EmbeddedSource() s.name = source instance = EmbeddedSource.SourceInstance() instance.reference = '' instance.method = '' instance.analyst = username instance.date = datetime.datetime.now() s.instances = [instance] source = [s] fqdn_domain = None root_domain = None cached_results = cache.get(form_consts.Domain.CACHED_RESULTS) if cached_results != None: if domain != sdomain: fqdn_domain = cached_results.get(domain) root_domain = cached_results.get(sdomain) else: root_domain = cached_results.get(sdomain) else: #first find the domain(s) if it/they already exist root_domain = Domain.objects(domain=sdomain).first() if domain != sdomain: fqdn_domain = Domain.objects(domain=domain).first() #if they don't exist, create them if not root_domain: root_domain = Domain() root_domain.domain = sdomain.strip() root_domain.source = [] root_domain.record_type = 'A' is_root_domain_new = True if cached_results != None: cached_results[sdomain] = root_domain if domain != sdomain and not fqdn_domain: fqdn_domain = Domain() fqdn_domain.domain = domain.strip() fqdn_domain.source = [] fqdn_domain.record_type = 'A' is_fqdn_domain_new = True if cached_results != None: cached_results[domain] = fqdn_domain # if new or found, append the new source(s) for s in source: if root_domain: root_domain.add_source(s) if fqdn_domain: fqdn_domain.add_source(s) #campaigns #both root and fqdn get campaigns updated for c in campaign: if root_domain: root_domain.add_campaign(c) if fqdn_domain: fqdn_domain.add_campaign(c) if username: if root_domain: root_domain.analyst = username if fqdn_domain: fqdn_domain.analyst = username if bucket_list: if root_domain: root_domain.add_bucket_list(bucket_list, username) if fqdn_domain: fqdn_domain.add_bucket_list(bucket_list, username) if ticket: if root_domain: root_domain.add_ticket(ticket, username) if fqdn_domain: fqdn_domain.add_ticket(ticket, username) # save try: if root_domain: root_domain.save(username=username) if fqdn_domain: fqdn_domain.save(username=username) except Exception, e: return {'success': False, 'message': e}
def upsert_domain(domain, source, username=None, campaign=None, confidence=None, bucket_list=None, ticket=None, cache={}, related_id=None, related_type=None, relationship_type=None): """ Add or update a domain/FQDN. Campaign is assumed to be a list of campaign dictionary objects. :param domain: The domain to add/update. :type domain: str :param source: The name of the source. :type source: str :param username: The user adding/updating the domain. :type username: str :param campaign: The campaign to attribute to this domain. :type campaign: list, str :param confidence: Confidence for the campaign attribution. :type confidence: str :param bucket_list: List of buckets to add to this domain. :type bucket_list: list, str :param ticket: The ticket for this domain. :type ticket: str :param cache: Cached data, typically for performance enhancements during bulk uperations. :type cache: dict :param related_id: ID of object to create relationship with :type related_id: str :param related_type: Type of object to create relationship with :type related_id: str :param relationship_type: Type of relationship to create. :type relationship_type: str :returns: dict with keys: "success" (boolean), "object" the domain that was added, "is_domain_new" (boolean) """ # validate domain and grab root domain (root, domain, error) = get_valid_root_domain(domain) if error: return {'success': False, 'message': error} is_fqdn_domain_new = False is_root_domain_new = False if not campaign: campaign = [] # assume it's a list, but check if it's a string elif isinstance(campaign, basestring): c = EmbeddedCampaign(name=campaign, confidence=confidence, analyst=username) campaign = [c] # assume it's a list, but check if it's a string if isinstance(source, basestring): s = EmbeddedSource() s.name = source instance = EmbeddedSource.SourceInstance() instance.reference = '' instance.method = '' instance.analyst = username instance.date = datetime.datetime.now() s.instances = [instance] source = [s] fqdn_domain = None root_domain = None cached_results = cache.get(form_consts.Domain.CACHED_RESULTS) if cached_results != None: if domain != root: fqdn_domain = cached_results.get(domain) root_domain = cached_results.get(root) else: root_domain = cached_results.get(root) else: #first find the domain(s) if it/they already exist root_domain = Domain.objects(domain=root).first() if domain != root: fqdn_domain = Domain.objects(domain=domain).first() #if they don't exist, create them if not root_domain: root_domain = Domain() root_domain.domain = root root_domain.source = [] root_domain.record_type = 'A' is_root_domain_new = True if cached_results != None: cached_results[root] = root_domain if domain != root and not fqdn_domain: fqdn_domain = Domain() fqdn_domain.domain = domain fqdn_domain.source = [] fqdn_domain.record_type = 'A' is_fqdn_domain_new = True if cached_results != None: cached_results[domain] = fqdn_domain # if new or found, append the new source(s) for s in source: if root_domain: root_domain.add_source(s) if fqdn_domain: fqdn_domain.add_source(s) #campaigns #both root and fqdn get campaigns updated for c in campaign: if root_domain: root_domain.add_campaign(c) if fqdn_domain: fqdn_domain.add_campaign(c) if username: if root_domain: root_domain.analyst = username if fqdn_domain: fqdn_domain.analyst = username if bucket_list: if root_domain: root_domain.add_bucket_list(bucket_list, username) if fqdn_domain: fqdn_domain.add_bucket_list(bucket_list, username) if ticket: if root_domain: root_domain.add_ticket(ticket, username) if fqdn_domain: fqdn_domain.add_ticket(ticket, username) related_obj = None if related_id: related_obj = class_from_id(related_type, related_id) if not related_obj: retVal['success'] = False retVal['message'] = 'Related Object not found.' return retVal # save try: if root_domain: root_domain.save(username=username) if fqdn_domain: fqdn_domain.save(username=username) except Exception, e: return {'success': False, 'message': e}