Exemplo n.º 1
0
 def connect(self):
     """ Create a kombu AMQP connection and channel. """
     direction = self.direction
     config = self._config[direction]["broker"]
     params = common.parse_amqp_uri(config["uri"])
     cred = config.get("auth")
     if cred is None:
         cred = credential.new(scheme="none")
     if cred.scheme == "x509":
         ssl_options = dict()
         cred_dict = cred.dict()
         for key, keyval in {
                 "cert": "certfile",
                 "key": "keyfile",
                 "ca": "ca_certs"
         }.items():
             if key in cred_dict:
                 ssl_options[keyval] = cred_dict[key]
         ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
         ssl_options["ssl_version"] = ssl.PROTOCOL_SSLv3
         extra = {
             "ssl": ssl_options,
             "transport_options": {
                 "login_method": "EXTERNAL"
             }
         }
     elif cred.scheme == "plain":
         extra = {
             "userid": cred['name'],
             "password": cred['pass'],
         }
     else:
         # none
         extra = dict()
     # if self._config.get("heartbeat") is not None:
     #    extra["heartbeat"] = self._config["heartbeat"]
     parameters = {
         "hostname": params["host"],
         "port": int(params["port"]),
         "transport": "amqplib",
         "virtual_host": params.get("virtual_host", "rabbitmq")
     }
     timeout_connect = self._config.get("timeout-connect")
     if timeout_connect is not None:
         parameters["connect_timeout"] = timeout_connect
     parameters.update(extra)
     self._connection = connection = self._kombu.BrokerConnection(
         **parameters)
     self._channel = connection.channel()
     LOGGER.debug("%s broker %s:%s: %s" % (
         direction,
         params['host'],
         params['port'],
         self.server_type(),
     ))
     if self._config.get("%s-broker-type" % direction) is None:
         self._config["%s-broker-type" % direction] = self.server_type()
     return True
 def test_creation(self):
     """ Test credential creation. """
     print("checking credential creation")
     for (shouldpass, cred_struct) in create_credential:
         if shouldpass:
             cred = credential.new(**cred_struct)
             rep = cred.dict()
             self.assertEqual(rep, cred_struct,
                              "expected to be equal:\n<%s>\n<%s>" %
                              (cred, cred_struct))
             cred.check()
             continue
         # else
         try:
             cred = credential.new(**cred_struct)
             self.fail("exception should have been raised for:\n<%s>" %
                       cred_struct)
         except InvalidCredential:
             pass
     print("...credential creation ok")
Exemplo n.º 3
0
 def connect(self):
     """ Create a pika AMQP connection and channel. """
     direction = self.direction
     config = self._config[direction]["broker"]
     params = common.parse_amqp_uri(config["uri"])
     cred = config.get("auth")
     if cred is None:
         cred = credential.new(scheme="none")
     if cred.scheme == "x509":
         if self._pika.__version__ < "0.9.6":
             raise AmqpcltError(
                 "x509 authentication not supported in pika %s" %
                 self._pika.__version__)
         ssl_options = dict()
         for key, keyval in {
                 "cert": "certfile",
                 "key": "keyfile",
                 "ca": "ca_certs"
         }.items():
             if key in cred:
                 ssl_options[keyval] = cred[key]
         ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
         ssl_options["ssl_version"] = ssl.PROTOCOL_SSLv3
         extra = {
             "ssl": True,
             "ssl_options": ssl_options,
             "credentials": self._pika.credentials.ExternalCredentials()
         }
     elif cred.scheme == "plain":
         extra = {
             "credentials":
             self._pika.credentials.PlainCredentials(
                 cred['name'], cred['pass']),
         }
     else:
         # none
         extra = dict()
     # if self._config.get("heartbeat") is not None:
     #    extra["heartbeat"] = self._config["heartbeat"]
     parameters = self._pika.connection.ConnectionParameters(
         params['host'].encode(), int(params['port']),
         params.get('virtual_host', "rabbitmq").encode(), **extra)
     self._connection = self._pika.BlockingConnection(parameters)
     self._channel = self._connection.channel()
     self._server_properties = self._connection._impl.server_properties
     LOGGER.debug("%s broker %s:%s: %s %s" %
                  (direction, params['host'], params['port'],
                   self.server_type(), self.server_version()))
     if self._config.get("%s-broker-type" % direction) is None:
         self._config["%s-broker-type" % direction] = self.server_type()
     return True
 def test_prepare(self):
     """ Test prepare. """
     print("checking prepare")
     opt = {'scheme': 'plain', 'name': 'Aladdin', 'pass': '******'}
     cred = credential.new(**opt)
     self.assertEqual(cred.prepare("HTTP.Basic"),
                      "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
                      "HTTP.Basic prepare failed")
     opt = {'scheme': 'plain', 'name': 'Aladdin', 'pass': '******'}
     cred = credential.new(**opt)
     expected = {'user': '******',
                 'passcode': 'open sesame', }
     self.assertEqual(cred.prepare("stomppy.plain"),
                      expected,
                      "stomppy.plain prepare failed")
     opt = {'scheme': 'x509', 'key': 'path/to/key', 'cert': 'path/to/cert'}
     cred = credential.new(**opt)
     expected = {'use_ssl': True,
                 'ssl_key_file': 'path/to/key',
                 'ssl_cert_file': 'path/to/cert', }
     self.assertEqual(cred.prepare("stomppy.x509"),
                      expected,
                      "stomppy.x509 prepare failed")
     print("...prepare ok")
Exemplo n.º 5
0
 def connect(self):
     """ Create a pika AMQP connection and channel. """
     direction = self.direction
     config = self._config[direction]["broker"]
     params = common.parse_amqp_uri(config["uri"])
     cred = config.get("auth")
     if cred is None:
         cred = credential.new(scheme="none")
     if cred.scheme == "x509":
         if self._pika.__version__ < "0.9.6":
             raise AmqpcltError(
                 "x509 authentication not supported in pika %s" %
                 self._pika.__version__)
         ssl_options = dict()
         for key, keyval in {"cert": "certfile",
                             "key": "keyfile", "ca": "ca_certs"}.items():
             if key in cred:
                 ssl_options[keyval] = cred[key]
         ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
         ssl_options["ssl_version"] = ssl.PROTOCOL_SSLv3
         extra = {
             "ssl": True,
             "ssl_options": ssl_options,
             "credentials": self._pika.credentials.ExternalCredentials()}
     elif cred.scheme == "plain":
         extra = {
             "credentials": self._pika.credentials.PlainCredentials(
                 cred['name'], cred['pass']), }
     else:
         # none
         extra = dict()
     #if self._config.get("heartbeat") is not None:
     #    extra["heartbeat"] = self._config["heartbeat"]
     parameters = self._pika.connection.ConnectionParameters(
         params['host'].encode(),
         int(params['port']),
         params.get('virtual_host', "rabbitmq").encode(), **extra)
     self._connection = connection = self._pika.BlockingConnection(
         parameters)
     self._channel = connection.channel()
     LOGGER.debug(
         "%s broker %s:%s: %s %s" %
         (direction, params['host'], params['port'], self.amqp_type(),
          connection.server_properties.get("version", "UNKNOWN"),))
     if self._config.get("%s-broker-type" % direction) is None:
         self._config["%s-broker-type" % direction] = self.amqp_type()
     return True
Exemplo n.º 6
0
 def connect(self):
     """ Create a kombu AMQP connection and channel. """
     direction = self.direction
     config = self._config[direction]["broker"]
     params = common.parse_amqp_uri(config["uri"])
     cred = config.get("auth")
     if cred is None:
         cred = credential.new(scheme="none")
     if cred.scheme == "x509":
         ssl_options = dict()
         cred_dict = cred.dict()
         for key, keyval in {"cert": "certfile",
                             "key": "keyfile", "ca": "ca_certs"}.items():
             if key in cred_dict:
                 ssl_options[keyval] = cred_dict[key]
         ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
         ssl_options["ssl_version"] = ssl.PROTOCOL_SSLv3
         extra = {"ssl": ssl_options,
                  "transport_options": {"login_method": "EXTERNAL"}}
     elif cred.scheme == "plain":
         extra = {
             "userid": cred['name'],
             "password": cred['pass'], }
     else:
         # none
         extra = dict()
     #if self._config.get("heartbeat") is not None:
     #    extra["heartbeat"] = self._config["heartbeat"]
     parameters = {
         "hostname": params["host"],
         "port": int(params["port"]),
         "transport": "amqplib",
         "virtual_host": params.get("virtual_host", "rabbitmq")
     }
     timeout_connect = self._config.get("timeout-connect")
     if timeout_connect is not None:
         parameters["connect_timeout"] = timeout_connect
     parameters.update(extra)
     self._connection = connection = self._kombu.BrokerConnection(
         **parameters)
     self._channel = connection.channel()
     LOGGER.debug(
         "%s broker %s:%s: %s" %
         (direction, params['host'], params['port'], self.amqp_type(),))
     if self._config.get("%s-broker-type" % direction) is None:
         self._config["%s-broker-type" % direction] = self.amqp_type()
     return True