Exemplo n.º 1
0
    def __init__(self,
                 hostname='127.0.0.1',
                 port=5672,
                 auth=_auth,
                 exchange=_exchange,
                 consumer_key=_consumerKey,
                 producer_exchange=_producerExchange,
                 callbacks=None):
        """
        Message Queue - holds a consumer class and producer class for ease of use
        :param hostname: server ip/hostname to connect
        :param port: port the AMQP Queue is listening
        :param exchange: name of the default exchange
        :param consumer_key: key to consumer
        :param producer_exchange: ...
        :param callbacks: list of functions to call on message receive
        """
        self._exchange = exchange if isinstance(exchange,
                                                str) else self._exchange
        self._consumerKey = consumer_key if isinstance(
            consumer_key, str) else self._consumerKey
        self._producerExchange = producer_exchange if isinstance(
            producer_exchange, str) else self._producerExchange

        self._publish_opts = dict(host=hostname, port=safe_cast(port, int))

        self._consume_opts = dict(host=hostname,
                                  port=safe_cast(port, int),
                                  exchange=self._exchange,
                                  routing_key=self._consumerKey,
                                  callbacks=callbacks)

        self.producer = Producer(**self._publish_opts)
        self.consumer = Consumer(**self._consume_opts)
                    message=response,
                    headers=rtn_headers,
                    exchange="orchestrator",
                    routing_key="response"
                )
        else:
            response = "Destination/Encoding/Orchestrator Socket of command not specified"
            rcv_headers["error"] = True
            print(response)
            producer.publish(
                message=str(response),
                headers=rcv_headers,
                exchange="orchestrator",
                routing_key="response"
            )


if __name__ == "__main__":
    print("Connecting to RabbitMQ...")
    try:
        consumer = Consumer(
            exchange="transport",
            routing_key="http",
            callbacks=[process_message],
            debug=True
        )

    except Exception as err:
        print(f"Consumer Error: {err}")
        consumer.shutdown()
Exemplo n.º 3
0
class MessageQueue:
    _auth = FrozenDict({'username': '******', 'password': '******'})
    _exchange = 'orchestrator'
    _consumerKey = 'response'
    _producerExchange = 'producer_transport'

    def __init__(self,
                 hostname='127.0.0.1',
                 port=5672,
                 auth=_auth,
                 exchange=_exchange,
                 consumer_key=_consumerKey,
                 producer_exchange=_producerExchange,
                 callbacks=None):
        """
        Message Queue - holds a consumer class and producer class for ease of use
        :param hostname: server ip/hostname to connect
        :param port: port the AMQP Queue is listening
        :param exchange: name of the default exchange
        :param consumer_key: key to consumer
        :param producer_exchange: ...
        :param callbacks: list of functions to call on message receive
        """
        self._exchange = exchange if isinstance(exchange,
                                                str) else self._exchange
        self._consumerKey = consumer_key if isinstance(
            consumer_key, str) else self._consumerKey
        self._producerExchange = producer_exchange if isinstance(
            producer_exchange, str) else self._producerExchange

        self._publish_opts = dict(host=hostname, port=safe_cast(port, int))

        self._consume_opts = dict(host=hostname,
                                  port=safe_cast(port, int),
                                  exchange=self._exchange,
                                  routing_key=self._consumerKey,
                                  callbacks=callbacks)

        self.producer = Producer(**self._publish_opts)
        self.consumer = Consumer(**self._consume_opts)

    def send(self, msg, headers, exchange=_producerExchange, routing_key=None):
        """
        Publish a message to the specified que and transport
        :param msg: message to be published
        :param headers: header information for the message being sent
        :param exchange: exchange name
        :param routing_key: routing key name
        :return: None
        """
        headers = headers or {}
        if routing_key is None:
            raise ValueError('Routing Key cannot be None')
        self.producer.publish(message=msg,
                              headers=headers,
                              exchange=exchange,
                              routing_key=routing_key)

    def shutdown(self):
        """
        Shutdown the connection to the queue
        """
        self.consumer.shutdown()
        self.consumer.join()
Exemplo n.º 4
0
# mqtt_transport.py

import os
import paho.mqtt.client as mqtt
import time

from sb_utils import Consumer, safe_cast
from callbacks import Callbacks

# Begin consuming messages from internal message queue
try:
    consumer = Consumer(
        exchange='transport',
        routing_key="mqtt",
        callbacks=[Callbacks.send_mqtt]
    )
except Exception as err:
    print(f"Consumer Error: {err}")
    consumer.shutdown()

# begin listening to a single MQTT socket
client = mqtt.Client()
print("Initializing client...")

# check that certs exist
if os.environ.get('MQTT_TLS_ENABLED', False):
    client.tls_set(
        ca_certs=os.environ.get('MQTT_CAFILE', None),
        certfile=os.environ.get('MQTT_CLIENT_CERT', None),
        keyfile=os.environ.get('MQTT_CLIENT_KEY', None)
    )
Exemplo n.º 5
0
    root = os.path.dirname(os.path.realpath(__file__))

    # Actuator Instance
    actuator = Actuator(root=root)

    # Actuator nsid/profile
    queue = actuator.nsid if len(actuator.nsid) > 0 else [actuator.profile]

    # Begin consuming messages from internal message queue
    consumer = None
    producer = Producer(os.environ.get('QUEUE_HOST', 'localhost'),
                        os.environ.get('QUEUE_PORT', '5672'))

    # Set Queue Bindings
    bindings = {}
    for q in queue:
        bindings[q] = ['actuator', ('actuator_all', 'fanout', 'actuator_all')]

    try:
        consumer = Consumer(
            exchange='actuator',
            # TODO: Get NSID??
            binding=bindings,
            callbacks=[partial(on_message, actuator, producer)])
    except Exception as e:
        print(f'Error {e}')
        consumer.shutdown()

    for sig in signals:
        signal.signal(sig, on_exit)
Exemplo n.º 6
0
    Helper method to organized required headers into the CoAP Request.
    :param request: Request being build
    :param headers: Data from AMQP message which contains data to forward OpenC2 Command.
    """
    dev_host, dev_port = headers["socket"].split(
        ":", 1)  # location of device-side CoAP server
    request.source = (dev_host, safe_cast(dev_port, int, 5683))

    orc_host, orc_port = headers["socket"].split(
        ":", 1)  # location of orchestrator-side CoAP server
    request.destination = (orc_host, safe_cast(orc_port, int, 5683))

    encoding = f"application/{headers['encoding']}"  # Content Serialization
    request.content_type = defines.Content_types[
        encoding]  # using application/json, TODO: add define to openc2+json

    request.mid = headers["correlationID"]  # 16-bit value - correlationID

    return request


if __name__ == "__main__":
    # Begin consuming messages from internal message queue
    try:
        consumer = Consumer(exchange="transport",
                            routing_key="coap",
                            callbacks=[send_coap])
    except Exception as e:
        print(f"Consumer Error: {e}")
        consumer.shutdown()
Exemplo n.º 7
0
    mqtt_conns = ClientsMQTT(
        # TODO: add orc_id to client_id ??
        client_id=f"oif-orc-{orc_id.replace('-', '')[:16]}",
        topics=rsp_topics,
        debug=True)

    # Gather transport from etcd
    transport_cache = EtcdCache(
        host=os.environ.get("ETCD_HOST", "localhost"),
        port=safe_cast(os.environ.get("ETCD_PORT", 2379), int, 2379),
        # Add base of 'orchestrator' ??
        base='transport/MQTT',
        callbacks=[mqtt_conns.update])
    # Begin consuming messages from external MQTT message queue
    mqtt_conns.start(transport_cache.cache)

    # Begin consuming messages from internal AMQP message queue
    print("Connecting to RabbitMQ...")
    consumer = None
    try:
        consumer = Consumer(exchange="producer_transport",
                            routing_key="mqtt",
                            callbacks=[send_mqtt],
                            debug=True)
        consumer.join()
    except Exception as err:
        print(f"Consumer Error: {err}")
        consumer.shutdown()
    mqtt_conns.shutdown()
    transport_cache.shutdown()
Exemplo n.º 8
0
    for child in etcd_client.read(etcd_act_prefix, recursive=True, sorted=True).children:
        key = re.sub(fr'^{etcd_act_prefix}/', '', child.key)
        print(f'KEY: {key} -> {child.value}')
        topics.append(f'oc2/cmd/ap/{key}')
except etcd.EtcdKeyNotFound:
    print('No actuators found')

# Add prefix if specified
topics = ['/'.join(filter(None, [*Config.MQTT_PREFIX.split('/'), *t.split('/')])) for t in topics]

# TODO: add optional etcd watcher via ENV vars

client.user_data_set(topics)
client.on_connect = Callbacks.on_connect
client.on_message = Callbacks.on_message
client.loop_start()

# Begin consuming messages from internal message queue
consumer = None
try:
    consumer = Consumer(
        exchange='transport',
        routing_key="mqtt",
        callbacks=[
            partial(Callbacks.send_mqtt, client=client)
        ]
    )
except Exception as err:
    print(f"Consumer Error: {err}")
    consumer.shutdown()
from sb_utils import Consumer, EtcdCache, safe_cast
from callbacks import Callbacks
from responses import ResponseSubscriptions

if __name__ == '__main__':
    # Initialize responses object
    rsps = ResponseSubscriptions()

    # Gather transport from etcd
    transport_cache = EtcdCache(host=os.environ.get("ETCD_HOST", "localhost"),
                                port=safe_cast(
                                    os.environ.get("ETCD_PORT", 2379), int,
                                    2379),
                                base='transport/MQTT',
                                callbacks=[rsps.update])
    # Begin consuming messages from external MQTT message queue
    rsps.start(transport_cache.cache)

    # Begin consuming messages from internal AMQP message queue
    print("Connecting to RabbitMQ...")
    consumer = None
    try:
        consumer = Consumer(exchange="transport",
                            routing_key="mqtt",
                            callbacks=[Callbacks.send_mqtt],
                            debug=True)
    except Exception as err:
        print(f"Consumer Error: {err}")
        consumer.shutdown()
        rsps.shutdown()