Ejemplo n.º 1
0
def registrar_remote_data_feeds(request):
    #Given the id of a TAXII service we get the data feeds of the TAXII Client and copy them to the current system.
    feed_managment = TAXIIServices.objects.get(id = request.DATA.get('id'))
    urlParsed = urlparse(feed_managment.feed_managment)

    logger = logging.getLogger('TAXIIApplication.rest.tasks.obtener_remote_data_feeds')

    logger.debug('We get the server data feeds')
    logger.debug('Host: ' + urlParsed.hostname)
    logger.debug('Path: ' + urlParsed.path)
    logger.debug('Port: ' + str(urlParsed.port))

    host = urlParsed.hostname
    path = urlParsed.path
    port = str(urlParsed.port)

    feed_information = tm.FeedInformationRequest(message_id=tm.generate_message_id())
    feed_info_xml = feed_information.to_xml()
    logger.debug('The following message is sent: ' + feed_info_xml)
    client = tc.HttpClient()
    resp = client.callTaxiiService2(host, path, t.VID_TAXII_XML_10, feed_info_xml, port)

    response_message = t.get_message_from_http_response(resp, '0')
    logger.debug("The response was: " + response_message.to_xml())
    try:
        taxii_message = tm.get_message_from_xml(response_message.to_xml())

        logger.debug("Feed Information iteration")
        feed_informations = taxii_message.feed_informations
        for feed in feed_informations:
            logger.debug("Create a new Remote Data Feed")
            remote_df = RemoteDataFeed()
            remote_df.name = feed.feed_name
            logger.debug(feed.feed_name)
            if feed.feed_description == None:
                remote_df.description = "None"
            else:
                remote_df.description = feed.feed_description
            remote_df.producer = host
            remote_df.save()
            i = 0
            logger.debug('We get the subscription methods')
            for sm in feed.subscription_methods:
                protocol_binding = ProtocolBindingId(binding_id = sm.subscription_protocol)
                protocol_binding.save()

                dfsm = DataFeedSubscriptionMethod()
                dfsm.title = feed.feed_name + "_"+str(i)
                dfsm.address = sm.subscription_address
                dfsm.protocol_binding=protocol_binding

                dfsm.save()
                for mb in sm.subscription_message_bindings:
                    msgb = MessageBindingId(binding_id = mb)
                    msgb.save()
                    dfsm.message_bindings.add(msgb)
                dfsm.save()
                remote_df.subscription_methods.add(dfsm)

            logger.debug('We get the Content Bindings')
            for sc in feed.supported_contents:
                cb = ContentBindingId(binding_id = sc )
                cb.save()
                remote_df.supported_content_bindings.add(cb)

            logger.debug('Obtengo los push methods')
            for pm in feed.push_methods:
                pb = ProtocolBindingId(binding_id = pm.push_protocol)
                pb.save()
                mb = MessageBindingId(binding_id = pm.push_message_bindings)
                mb.save()
                dpm = DataFeedPushMethod(protocol_binding = pb, message_binding = mb)
                dpm.save()

                remote_df.push_methods.add(dpm)


            poll_service_instances = []
            logger.debug('We get the Poll Service Instances')
            for psi in feed.polling_service_instances:

                rdfpi = RemoteDataFeedPollInformation()
                rdfpi.address = psi.poll_address

                pb = ProtocolBindingId(binding_id = psi.poll_protocol)
                pb.save()
                rdfpi.protocol_binding = pb
                rdfpi.save()

                logger.debug(psi.poll_message_bindings)
                for msg in psi.poll_message_bindings:
                    msgb = MessageBindingId(binding_id = msg)
                    msgb.save()
                    rdfpi.message_bindings.add(msgb)
                
                rdfpi.save()
                remote_df.poll_service_instances.add(rdfpi)
            logger.debug("Save the remote data feed")
            remote_df.save()

        return Response(status=status.HTTP_201_CREATED)
    except Exception as ex:
        logger.debug( ex)
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 2
0
def poll_request(collection_name, subscription_id, host, path, port):
    #Given the collection name, subscription id, host, path and port we make a poll request to 
    #the client TAXII associated with the host, path and port for the subscription and collection given.
    logger = logging.getLogger('TAXIIApplication.rest.tasks.poll_request')
    logger.debug('Poll information starts')
    logger.debug('Parameters are: ')
    logger.debug('Collection Name: ' + collection_name)
    logger.debug('Subscription Id: ' + str(subscription_id))
    logger.debug('Host: ' + str(host))
    logger.debug('Path: ' + path)
    logger.debug('Port: ' + str(port))

    begin_ts = None
    end_ts = None

    poll_req = tm11.PollRequest(message_id = tm11.generate_message_id(),
                                collection_name = collection_name,
                                exclusive_begin_timestamp_label = begin_ts,
                                inclusive_end_timestamp_label = end_ts,
                                subscription_id = subscription_id)

    poll_req_xml = poll_req.to_xml()
    logger.debug('The following Poll Request message was generated')
    logger.debug('###########################################')
    logger.debug(poll_req_xml)
    logger.debug('###########################################')

    client = tc.HttpClient()
    resp = client.callTaxiiService2(host, path, t.VID_TAXII_XML_10, poll_req_xml, port)
    response_message = t.get_message_from_http_response(resp, '0')

    logger.debug('The response got from the sistem was: ')
    logger.debug('#########################################')
    logger.debug(response_message.to_xml())
    logger.debug('#########################################')

    try:
        taxii_message = tm.get_message_from_xml(response_message.to_xml())
    except Exception as ex:
        logger.debug('The message could not be parsed:s', ex.message)

    if taxii_message.message_type != tm.MSG_POLL_RESPONSE:
        logger.debug('The message is not a TAXII response')
    else:
        logger.debug(taxii_message)
        content_blocks = taxii_message.content_blocks
        logger.debug('We process the Content Blocks')

        for cb in content_blocks:

            tree = etree.parse(StringIO(cb.content))
            import stix.bindings.stix_core as stix_core_binding
            stix_package_obj = stix_core_binding.STIXType().factory()
            stix_package_obj.build(tree.getroot())

            from stix.core import STIXPackage # resolve circular dependencies
            stix_package = STIXPackage().from_obj(stix_package_obj)

            logger.debug(stix_package.stix_header.description)
            logger.debug('El id del stix es ' + str(stix_package._id))
            
            info = ContentBlock.objects.filter(stix_id = stix_package._id)
            if info.exists():
                p = ContentBlock()

                p.description = stix_package.stix_header.description
                p.title = stix_package.stix_header.title
                p.message_id = taxii_message.message_id
                p.origen = collection_name + ' in ' + host             
                c = ContentBindingId(binding_id=cb.content_binding)
                c.save()
                p.content_binding = c
                p.content = cb.content
                p.save()