Esempio n. 1
0
    def _process_url(self, url):
        """Parse the AMQP URL passed in and return the configuration
        information in a dictionary of values.

        The URL format is as follows:

            amqp[s]://username:password@host:port/virtual_host[?query string]

        Values in the URL such as the virtual_host should be URL encoded or
        quoted just as a URL would be in a web browser. The default virtual
        host / in RabbitMQ should be passed as %2F.

        Default values:

            - If port is omitted, port 5762 is used for AMQP and port 5671 is
              used for AMQPS
            - If username or password is omitted, the default value is guest
            - If the virtual host is omitted, the default value of %2F is used

        Query string options:

            - heartbeat_interval
            - locale
            - ssl_cacert - Path to CA certificate file
            - ssl_cert - Path to client certificate file
            - ssl_key - Path to client certificate key
            - ssl_validation - Server certificate validation requirements (1)
            - ssl_version - SSL version to use (2)

            (1) Should be one of three values:

               - ignore - Ignore the cert if provided (default)
               - optional - Cert is validated if provided
               - required - Cert is required and validated

            (2) Should be one of four values:

              - SSLv2
              - SSLv3
              - SSLv23
              - TLSv1

        :param str url: The AMQP url passed in
        :rtype: dict
        :raises: ValueError

        """
        parsed = utils.urlparse(url)

        # Ensure the protocol scheme is what is expected
        if parsed.scheme not in list(self.PORTS.keys()):
            raise ValueError('Unsupported protocol: %s' % parsed.scheme)

        # Toggle the SSL flag based upon the URL scheme
        use_ssl = True if parsed.scheme == 'amqps' else False

        # Ensure that SSL is available if SSL is requested
        if use_ssl and not ssl:
            LOGGER.warning('SSL requested but not available, disabling')
            use_ssl = False

        # Use the default ports if one is not specified
        port = parsed.port or (self.PORTS[AMQPS]
                               if parsed.scheme == AMQPS else self.PORTS[AMQP])

        # Set the vhost to be after the base slash if it was specified
        vhost = parsed.path[1:] if parsed.path else self.DEFAULT_VHOST

        # If the path was just the base path, set the vhost to the default
        if not vhost:
            vhost = self.DEFAULT_VHOST

        # Parse the query string
        query_values = utils.parse_qs(parsed.query)

        # Make sure the heartbeat is an int if it is not None
        heartbeat = int(query_values.get('heartbeat_interval', [None])[0] or 0)

        # Return the configuration dictionary to use when connecting
        return {
            'host': parsed.hostname,
            'port': port,
            'virtual_host': utils.unquote(vhost),
            'username': parsed.username or self.GUEST,
            'password': parsed.password or self.GUEST,
            'heartbeat': heartbeat,
            'locale': query_values.get('locale', [None])[0],
            'ssl': use_ssl,
            'ssl_cacert': query_values.get('ssl_cacert', [None])[0],
            'ssl_cert': query_values.get('ssl_cert', [None])[0],
            'ssl_key': query_values.get('ssl_key', [None])[0],
            'ssl_validation': self._get_ssl_validation(query_values),
            'ssl_version': self._get_ssl_version(query_values)
        }
Esempio n. 2
0
    def _process_url(self, url):
        """Parse the AMQP URL passed in and return the configuration
        information in a dictionary of values.

        The URL format is as follows:

            amqp[s]://username:password@host:port/virtual_host[?query string]

        Values in the URL such as the virtual_host should be URL encoded or
        quoted just as a URL would be in a web browser. The default virtual
        host / in RabbitMQ should be passed as %2F.

        Default values:

            - If port is omitted, port 5762 is used for AMQP and port 5671 is
              used for AMQPS
            - If username or password is omitted, the default value is guest
            - If the virtual host is omitted, the default value of %2F is used

        Query string options:

            - heartbeat_interval
            - locale
            - ssl_cacert - Path to CA certificate file
            - ssl_cert - Path to client certificate file
            - ssl_key - Path to client certificate key
            - ssl_validation - Server certificate validation requirements (1)
            - ssl_version - SSL version to use (2)

            (1) Should be one of three values:

               - ignore - Ignore the cert if provided (default)
               - optional - Cert is validated if provided
               - required - Cert is required and validated

            (2) Should be one of four values:

              - SSLv2
              - SSLv3
              - SSLv23
              - TLSv1

        :param str url: The AMQP url passed in
        :rtype: dict
        :raises: ValueError

        """
        parsed = utils.urlparse(url)

        # Ensure the protocol scheme is what is expected
        if parsed.scheme not in list(self.PORTS.keys()):
            raise ValueError('Unsupported protocol: %s' % parsed.scheme)

        # Toggle the SSL flag based upon the URL scheme
        use_ssl = True if parsed.scheme == 'amqps' else False

        # Ensure that SSL is available if SSL is requested
        if use_ssl and not ssl:
            LOGGER.warning('SSL requested but not available, disabling')
            use_ssl = False

        # Use the default ports if one is not specified
        port = parsed.port or (self.PORTS[AMQPS] if parsed.scheme == AMQPS
                               else self.PORTS[AMQP])

        # Set the vhost to be after the base slash if it was specified
        vhost = parsed.path[1:] if parsed.path else self.DEFAULT_VHOST

        # If the path was just the base path, set the vhost to the default
        if not vhost:
            vhost = self.DEFAULT_VHOST

        # Parse the query string
        query_values = utils.parse_qs(parsed.query)

        # Make sure the heartbeat is an int if it is not None
        heartbeat = int(query_values.get('heartbeat_interval', [None])[0] or 0)

        # Return the configuration dictionary to use when connecting
        return {'host': parsed.hostname,
                'port': port,
                'virtual_host': utils.unquote(vhost),
                'username': parsed.username or self.GUEST,
                'password': parsed.password or self.GUEST,
                'heartbeat': heartbeat,
                'locale': query_values.get('locale', [None])[0],
                'ssl': use_ssl,
                'ssl_cacert': query_values.get('ssl_cacert', [None])[0],
                'ssl_cert': query_values.get('ssl_cert', [None])[0],
                'ssl_key': query_values.get('ssl_key', [None])[0],
                'ssl_validation': self._get_ssl_validation(query_values),
                'ssl_version': self._get_ssl_version(query_values)}
Esempio n. 3
0
    def _process_url(self, url):
        """Parse the AMQP URL passed in and return the configuration
        information in a dictionary of values.

        The URL format is as follows:

            amqp[s]://username:password@host:port/virtual_host[?query string]

        Values in the URL such as the virtual_host should be URL encoded or
        quoted just as a URL would be in a web browser. The default virtual
        host / in RabbitMQ should be passed as %2F.

        Default values:

            - If port is omitted, port 5762 is used for AMQP and port 5671 is
              used for AMQPS
            - If username or password is omitted, the default value is guest
            - If the virtual host is omitted, the default value of %2F is used

        Query string options:

            - heartbeat
            - channel_max
            - frame_max
            - locale
            - cacertfile - Path to CA certificate file
            - certfile - Path to client certificate file
            - keyfile - Path to client certificate key
            - verify - Server certificate validation requirements (1)
            - ssl_version - SSL version to use (2)

            (1) Should be one of three values:

               - ignore - Ignore the cert if provided (default)
               - optional - Cert is validated if provided
               - required - Cert is required and validated

            (2) Should be one of four values:

              - SSLv2
              - SSLv3
              - SSLv23
              - TLSv1

        :param str url: The AMQP url passed in
        :rtype: dict
        :raises: ValueError

        """
        parsed = utils.urlparse(url)

        self._validate_uri_scheme(parsed.scheme)

        # Toggle the SSL flag based upon the URL scheme and if SSL is enabled
        use_ssl = True if parsed.scheme == 'amqps' and ssl else False

        # Ensure that SSL is available if SSL is requested
        if parsed.scheme == 'amqps' and not ssl:
            LOGGER.warning('SSL requested but not available, disabling')

        # Figure out the port as specified by the scheme
        scheme_port = self.PORTS[AMQPS] if parsed.scheme == AMQPS \
            else self.PORTS[AMQP]

        # Set the vhost to be after the base slash if it was specified
        vhost = self.DEFAULT_VHOST
        if parsed.path:
            vhost = parsed.path[1:] or self.DEFAULT_VHOST

        # Parse the query string
        qargs = utils.parse_qs(parsed.query)

        # Return the configuration dictionary to use when connecting
        return {
            'host':
            parsed.hostname,
            'port':
            parsed.port or scheme_port,
            'virtual_host':
            utils.unquote(vhost),
            'username':
            parsed.username or self.GUEST,
            'password':
            parsed.password or self.GUEST,
            'timeout':
            self._qargs_int('timeout', qargs, self.DEFAULT_TIMEOUT),
            'heartbeat':
            self._qargs_int('heartbeat', qargs,
                            self.DEFAULT_HEARTBEAT_INTERVAL),
            'frame_max':
            self._qargs_int('frame_max', qargs, spec.FRAME_MAX_SIZE),
            'channel_max':
            self._qargs_int('channel_max', qargs, self.DEFAULT_CHANNEL_MAX),
            'locale':
            self._qargs_value('locale', qargs),
            'ssl':
            use_ssl,
            'cacertfile':
            self._qargs_mk_value(['cacertfile', 'ssl_cacert'], qargs),
            'certfile':
            self._qargs_mk_value(['certfile', 'ssl_cert'], qargs),
            'keyfile':
            self._qargs_mk_value(['keyfile', 'ssl_key'], qargs),
            'verify':
            self._qargs_ssl_validation(qargs),
            'ssl_version':
            self._qargs_ssl_version(qargs)
        }
Esempio n. 4
0
    def _process_url(self, url):
        """Parse the AMQP URL passed in and return the configuration
        information in a dictionary of values.

        The URL format is as follows:

            amqp[s]://username:password@host:port/virtual_host[?query string]

        Values in the URL such as the virtual_host should be URL encoded or
        quoted just as a URL would be in a web browser. The default virtual
        host / in RabbitMQ should be passed as %2F.

        Default values:

            - If port is omitted, port 5762 is used for AMQP and port 5671 is
              used for AMQPS
            - If username or password is omitted, the default value is guest
            - If the virtual host is omitted, the default value of %2F is used

        Query string options:

            - heartbeat
            - channel_max
            - frame_max
            - locale
            - cacertfile - Path to CA certificate file
            - certfile - Path to client certificate file
            - keyfile - Path to client certificate key
            - verify - Server certificate validation requirements (1)
            - ssl_version - SSL version to use (2)

            (1) Should be one of three values:

               - ignore - Ignore the cert if provided (default)
               - optional - Cert is validated if provided
               - required - Cert is required and validated

            (2) Should be one of four values:

              - SSLv2
              - SSLv3
              - SSLv23
              - TLSv1

        :param str url: The AMQP url passed in
        :rtype: dict
        :raises: ValueError

        """
        parsed = utils.urlparse(url)

        # Ensure the protocol scheme is what is expected
        if parsed.scheme not in list(self.PORTS.keys()):
            raise ValueError('Unsupported protocol: %s' % parsed.scheme)

        # Toggle the SSL flag based upon the URL scheme
        use_ssl = True if parsed.scheme == 'amqps' else False

        # Ensure that SSL is available if SSL is requested
        if use_ssl and not ssl:
            LOGGER.warning('SSL requested but not available, disabling')
            use_ssl = False

        # Use the default ports if one is not specified
        port = parsed.port or (self.PORTS[AMQPS] if parsed.scheme == AMQPS
                               else self.PORTS[AMQP])

        # Set the vhost to be after the base slash if it was specified
        vhost = parsed.path[1:] if parsed.path else self.DEFAULT_VHOST

        # If the path was just the base path, set the vhost to the default
        if not vhost:
            vhost = self.DEFAULT_VHOST

        # Parse the query string
        query_values = utils.parse_qs(parsed.query)

        channel_max = int(query_values.get('channel_max', [None])[0] or
                          self.DEFAULT_CHANNEL_MAX)
        frame_max = int(query_values.get('frame_max', [None])[0] or
                        specification.FRAME_MAX_SIZE)
        heartbeat_interval = int(query_values.get('heartbeat', [None])[0] or
                                 self.DEFAULT_HEARTBEAT_INTERVAL)
        # DEFAULT_TIMEOUT does not have to be 0, so explicitly setting 0
        # (False-ish) should not lead to using it, thus no "or" here but
        # the precise check against None.
        timeout = query_values.get('timeout', [None])[0]
        timeout = self.DEFAULT_TIMEOUT if timeout is None else float(timeout)

        # Return the configuration dictionary to use when connecting
        return {'host': parsed.hostname,
                'port': port,
                'virtual_host': utils.unquote(vhost),
                'username': parsed.username or self.GUEST,
                'password': parsed.password or self.GUEST,
                'timeout': timeout,
                'heartbeat': heartbeat_interval,
                'frame_max': frame_max,
                'channel_max': channel_max,
                'locale': query_values.get('locale', [None])[0],
                'ssl': use_ssl,
                'cacertfile': (query_values.get('cacertfile', [None])[0] or
                               query_values.get('ssl_cacert', [None])[0]),
                'certfile': (query_values.get('certfile', [None])[0] or
                             query_values.get('ssl_cert', [None])[0]),
                'keyfile': (query_values.get('keyfile', [None])[0] or
                            query_values.get('ssl_key', [None])[0]),
                'verify': self._get_ssl_validation(query_values),
                'ssl_version': self._get_ssl_version(query_values)}
Esempio n. 5
0
    def _process_url(self, url):
        """Parse the AMQP URL passed in and return the configuration
        information in a dictionary of values.

        The URL format is as follows:

            amqp[s]://username:password@host:port/virtual_host[?query string]

        Values in the URL such as the virtual_host should be URL encoded or
        quoted just as a URL would be in a web browser. The default virtual
        host / in RabbitMQ should be passed as %2F.

        Default values:

            - If port is omitted, port 5762 is used for AMQP and port 5671 is
              used for AMQPS
            - If username or password is omitted, the default value is guest
            - If the virtual host is omitted, the default value of %2F is used

        Query string options:

            - heartbeat
            - channel_max
            - frame_max
            - locale
            - cacertfile - Path to CA certificate file
            - certfile - Path to client certificate file
            - keyfile - Path to client certificate key
            - verify - Server certificate validation requirements (1)
            - ssl_version - SSL version to use (2)

            (1) Should be one of three values:

               - ignore - Ignore the cert if provided (default)
               - optional - Cert is validated if provided
               - required - Cert is required and validated

            (2) Should be one of four values:

              - SSLv2
              - SSLv3
              - SSLv23
              - TLSv1

        :param str url: The AMQP url passed in
        :rtype: dict
        :raises: ValueError

        """
        parsed = utils.urlparse(url)

        self._validate_uri_scheme(parsed.scheme)

        # Toggle the SSL flag based upon the URL scheme and if SSL is enabled
        use_ssl = True if parsed.scheme == 'amqps' and ssl else False

        # Ensure that SSL is available if SSL is requested
        if parsed.scheme == 'amqps' and not ssl:
            LOGGER.warning('SSL requested but not available, disabling')

        # Figure out the port as specified by the scheme
        scheme_port = self.PORTS[AMQPS] if parsed.scheme == AMQPS \
            else self.PORTS[AMQP]

        # Set the vhost to be after the base slash if it was specified
        vhost = self.DEFAULT_VHOST
        if parsed.path:
            vhost = parsed.path[1:] or self.DEFAULT_VHOST

        # Parse the query string
        qargs = utils.parse_qs(parsed.query)

        # Return the configuration dictionary to use when connecting
        return {
            'host': parsed.hostname,
            'port': parsed.port or scheme_port,
            'virtual_host': utils.unquote(vhost),
            'username': urlparse.unquote(parsed.username or self.GUEST),
            'password': urlparse.unquote(parsed.password or self.GUEST),
            'timeout': self._qargs_int('timeout', qargs, self.DEFAULT_TIMEOUT),
            'heartbeat': self._qargs_int('heartbeat', qargs,
                                         self.DEFAULT_HEARTBEAT_INTERVAL),
            'frame_max': self._qargs_int('frame_max', qargs,
                                         spec.FRAME_MAX_SIZE),
            'channel_max': self._qargs_int('channel_max', qargs,
                                           self.DEFAULT_CHANNEL_MAX),
            'locale': self._qargs_value('locale', qargs),
            'ssl': use_ssl,
            'cacertfile': self._qargs_mk_value(['cacertfile', 'ssl_cacert'],
                                               qargs),
            'certfile': self._qargs_mk_value(['certfile', 'ssl_cert'], qargs),
            'keyfile': self._qargs_mk_value(['keyfile', 'ssl_key'], qargs),
            'verify': self._qargs_ssl_validation(qargs),
            'ssl_version': self._qargs_ssl_version(qargs)}
Esempio n. 6
0
 def test_parse_qs(self):
     self.assertDictEqual(utils.parse_qs(self.QUERY),
                          {'heartbeat_interval': ['1']})