예제 #1
0
    def load_user_info(self):
        """If a new authorization is requested, check Graylog authentication on
         /roles using the given credentials. Forbidden is expected. This process
         allow to login using tokens.

        :return: result of GET /users/{username} on success else None
        """
        if getattr(g, 'user', None):
            return g.user

        auth = request.authorization
        if not auth:
            g.user = None
            return g.user

        client = RESTClient(
            server=self.app.config['GRAYLOG_API_URL'], **auth
        )
        try:
            client.do_request(method="GET", path="/roles")

        except Unauthorized:
            self.authenticate()
        except Forbidden as exc:
            match = re.search("User \[(.*?)\] not authorized", exc.message)
            if match:
                g.user = client.do_request(
                    method="GET",
                    path="/users/%s" % match.group(1)
                )
                return g.user
예제 #2
0
    def load_user_info(self):
        """If a new authorization is requested, check Graylog authentication on
         /roles using the given credentials. Forbidden is expected. This process
         allow to login using tokens.

        :return: result of GET /users/{username} on success else None
        """
        if getattr(g, 'user', None):
            return g.user

        auth = request.authorization
        if not auth:
            g.user = None
            return g.user

        client = RESTClient(server=self.app.config['GRAYLOG_API_URL'], **auth)
        try:
            client.do_request(method="GET", path="/roles")

        except Unauthorized:
            self.authenticate()
        except Forbidden as exc:
            match = re.search("User \[(.*?)\] not authorized", exc.message)
            if match:
                g.user = client.do_request(method="GET",
                                           path="/users/%s" % match.group(1))
                return g.user
예제 #3
0
class Producer(BaseProducer):
    """Mother class for producers"""
    def __init__(self, config):
        """ Create new Producer instance using provided configuration dict.

        :param dict config: configuration
        """
        self.client = RESTClient(server=config["bootstrap_servers"],
                                 username=config["sasl_plain_username"],
                                 password=config["sasl_plain_password"],
                                 timeout=config.get("timeout", 10))

    def bulk_send(self, topic, kmsgs):
        """ Send a batch of messages

        :param str topic: a kafka topic
        :param ksr.transport.Message kmsgs: Messages to serialize
        :return: Execution result
        :rtype: kser.result.Result
        """

        try:
            self.client.do_request(
                method="POST",
                path="/topic/{}".format(topic),
                data=[dict(Value=k.MARSHMALLOW_SCHEMA.dump(k)) for k in kmsgs])
            return Result(stdout="{} message(s) sent".format(len(kmsgs)))

        except Exception as exc:
            return Result.from_exception(exc)

    def _send(self, topic, kmsg, timeout=60):
        """ Send the message into the given topic

        :param str topic: a kafka topic
        :param ksr.transport.Message kmsg: Message to serialize
        :return: Execution result
        :rtype: kser.result.Result
        """
        try:
            self.client.do_request(method="POST",
                                   params=dict(format="raw"),
                                   path="/topic/{}".format(topic),
                                   data=kmsg.MARSHMALLOW_SCHEMA.dump(kmsg),
                                   timeout=timeout)
            result = Result(uuid=kmsg.uuid,
                            stdout="Message sent: {} ({})".format(
                                kmsg.uuid, kmsg.entrypoint))

        except Exception as exc:
            result = Result.from_exception(exc, kmsg.uuid)

        finally:
            # noinspection PyUnboundLocalVariable
            if result.retcode < 300:
                return self._onsuccess(kmsg=kmsg, result=result)
            else:
                return self._onerror(kmsg=kmsg, result=result)
예제 #4
0
    def __init__(self, config):
        """ Create new Producer instance using provided configuration dict.

        :param dict config: configuration
        """
        self.client = RESTClient(server=config["bootstrap_servers"],
                                 username=config["sasl_plain_username"],
                                 password=config["sasl_plain_password"],
                                 timeout=config.get("timeout", 10))
예제 #5
0
class Consumer(object):
    """Http consumer"""
    REGISTRY = Controller

    def __init__(self, config, topics):
        self.client = RESTClient(server=config["bootstrap_servers"],
                                 username=config["sasl_plain_username"],
                                 password=config["sasl_plain_password"],
                                 timeout=config.get("timeout", 10))
        self.topics = topics

    def call_kafka(self, topic, batch_size=1):
        """Call Kafka"""
        logger.debug("{}.Checking {}".format(self.__class__.__name__, topic))
        data = self.client.do_request(method="GET",
                                      params=dict(limit=batch_size),
                                      path="/topic/{}".format(topic))
        for item in data:
            str_msg = item.get("Value", None)
            if str_msg:
                self.REGISTRY.run(str_msg)

    def run(self, loopinfo=None, batch_size=1):
        """ Run consumer
        """
        logger.info("{}.Starting...".format(self.__class__.__name__))
        if loopinfo:
            while True:
                for topic in self.topics:
                    self.call_kafka(topic, batch_size)
                time.sleep(loopinfo.sleep)
        else:
            for topic in self.topics:
                self.call_kafka(topic, batch_size)
예제 #6
0
파일: lib.py 프로젝트: cdumay/json-sort
def from_remote(src, dst=None):
    """description of fromurl"""
    try:
        file_write(dst,
                   RESTClient(server=src).do_request(method="GET", path=""))
    except Exception as exc:
        oncritical(exc)
예제 #7
0
    def client(self):
        if self._client is None:
            self._client = RESTClient(
                server=self.app.config['GRAYLOG_API_URL'],
                username=self.app.config['GRAYLOG_API_USERNAME'],
                password=self.app.config['GRAYLOG_API_PASSWORD'],
                timeout=self.app.config.get("GRAYLOG_API_TIMEOUT", 10)
            )

        return self._client
예제 #8
0
    def tat_client(self):
        if self._tat_client is None:
            self._tat_client = RESTClient(server=self.configuration['url'],
                                          headers={
                                              "Tat_username":
                                              self.configuration["username"],
                                              "Tat_password":
                                              self.configuration["password"],
                                              "Content-type":
                                              "application/json",
                                          })

        return self._tat_client
예제 #9
0
 def __init__(self, config, topics):
     self.client = RESTClient(server=config["bootstrap_servers"],
                              username=config["sasl_plain_username"],
                              password=config["sasl_plain_password"],
                              timeout=config.get("timeout", 10))
     self.topics = topics
예제 #10
0
 def test_post_a(self):
     client = RESTClient(server="http://jsonplaceholder.typicode.com")
     with self.assertRaises(NotFound):
         client.do_request(method="GET", path="/posts/a")
예제 #11
0
 def test_post_1(self):
     client = RESTClient(server="http://jsonplaceholder.typicode.com")
     data = client.do_request(method="GET", path="/posts/1")
     self.assertEqual(data['userId'], 1)
     self.assertEqual(data['id'], 1)
예제 #12
0
 def test_post_a(self):
     client = RESTClient(server="http://jsonplaceholder.typicode.com")
     with self.assertRaises(NotFound):
         client.do_request(method="GET", path="/posts/a")
예제 #13
0
 def test_post_1(self):
     client = RESTClient(server="http://jsonplaceholder.typicode.com")
     data = client.do_request(method="GET", path="/posts/1")
     self.assertEqual(data['userId'], 1)
     self.assertEqual(data['id'], 1)