Example #1
0
    def check_prerequisites(self):
        # Check if the required tools are accessible
        self.check_mylogin_requisites()

        # Check MySQL server version - Must be 5.6.25 or higher
        if not \
           (self.servers.get_server(0).check_version_compat(5, 7, 8) or
            (self.servers.get_server(0).check_version_compat(5, 6, 25) and
             not self.servers.get_server(0).check_version_compat(5, 7, 0))):
            raise MUTLibError("Test requires server version higher than 5.6.25"
                              " but lower than 5.7.0 or higher than 5.7.7")

        # .mylogin.cnf must be at his default location
        self.mylogin_src = os.path.normpath(
            os.path.join(my_login_config_path(), MYLOGIN_FILE))
        if not os.path.exists(self.mylogin_src):
            raise MUTLibError("Test requires file .mylogin.cnf at his default"
                              " location: {0}".format(self.mylogin_src))

        # Check the required number of servers
        return self.check_num_servers(0)
 def test_my_login_config_path(self):
     # Check if the path (directory) to .mylogin.cnf exists
     path = my_login_config_path()
     self.assertTrue(os.path.isdir(path))
Example #3
0
def parse_connection(connection_values, my_defaults_reader=None, options=None):
    """Parse connection values.

    The function parses a connection specification of one of the forms::

      - user[:password]@host[:port][:socket]
      - login-path[:port][:socket]

    A dictionary is returned containing the connection parameters. The
    function is designed so that it shall be possible to use it with a
    ``connect`` call in the following manner::

      options = parse_connection(spec)
      conn = mysql.connector.connect(**options)

    conn_values[in]         Connection values in the form:
                            user:password@host:port:socket
                            or login-path:port:socket
    my_defaults_reader[in]  Instance of MyDefaultsReader to read the
                            information of the login-path from configuration
                            files. By default, the value is None.
    options[in]             Dictionary of options (e.g. basedir), from the used
                            utility. By default, it set with an empty
                            dictionary. Note: also supports options values
                            from optparse.

    Notes:

    This method validates IPv4 addresses and standard IPv6 addresses.

    This method accepts quoted host portion strings. If the host is marked
    with quotes, the code extracts this without validation and assigns it to
    the host variable in the returned tuple. This allows users to specify host
    names and IP addresses that are outside of the supported validation.

    Returns dictionary (user, passwd, host, port, socket)
            or raise an exception if parsing error
    """
    if options is None:
        options = {}

    def _match(pattern, search_str):
        """Returns the groups from string search or raise FormatError if it
        does not match with the pattern.
        """
        grp = pattern.match(search_str)
        if not grp:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        return grp.groups()

    # SSL options, must not be overwritten with those from options.
    ssl_ca = None
    ssl_cert = None
    ssl_key = None
    ssl = None

    # Split on the '@' to determine the connection string format.
    # The user/password may have the '@' character, split by last occurrence.
    conn_format = connection_values.rsplit('@', 1)

    if len(conn_format) == 1:
        # No '@' so try config-path and login-path

        # The config_path and login-path collide on their first element and
        # only differs on their secondary optional values.
        # 1. Try match config_path and his optional value group. If both
        #    matches and the connection data can be retrieved, return the data.
        #    If errors occurs in this step raise them immediately.
        # 2. If config_path matches but group does not, and connection data
        #    can not be retrieved, do not raise errors and try to math
        #    login_path on step 4.
        # 3. If connection data is retrieved on step 2, then try login_path on
        #    next step to overwrite values from the new configuration.
        # 4. If login_path matches, check is .mylogin.cnf exists, if it doesn't
        #    and data configuration was found verify it  for missing values and
        #    continue if they are not any missing.
        # 5. If .mylogin.cnf exists and data configuration is found, overwrite
        #    any previews value from config_path if there is any.
        # 6. If login_path matches a secondary value but the configuration data
        #    could not be retrieved, do not continue and raise any error.
        # 7. In case errors have occurred trying to get data from config_path,
        #    and group did not matched, and in addition no secondary value,
        #    matched from login_path (port of socket) mention that config_path
        #    and login_path were not able to retrieve the connection data.

        # try login_path and overwrite the values.
        # Handle the format: config-path[[group]]
        config_path, group = _match(_CONN_CONFIGPATH, conn_format[0])
        port = None
        socket = None
        config_path_data = None
        login_path_data = None
        config_path_err_msg = None
        login_path = None
        if config_path:
            try:
                # If errors occurs, and group matched: raise any errors as the
                # group is exclusive of config_path.
                config_path_data = handle_config_path(config_path, group)
            except UtilError as err:
                if group:
                    raise
                else:
                    # Convert first letter to lowercase to include in error
                    # message with the correct case.
                    config_path_err_msg = \
                        err.errmsg[0].lower() + err.errmsg[1:]

        if group is None:
            # the conn_format can still be a login_path so continue
            # No '@' then handle has in the format: login-path[:port][:socket]
            login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0])

            # Check if the login configuration file (.mylogin.cnf) exists
            if login_path and not my_login_config_exists():
                if not config_path_data:
                    util_err_msg = (".mylogin.cnf was not found at is default "
                                    "location: {0}. Please configure your "
                                    "login-path data before using it (use the "
                                    "mysql_config_editor tool)."
                                    "".format(my_login_config_path()))
                    if config_path_err_msg and not (port or socket):
                        util_err_msg = ("{0} In addition, {1}"
                                        "").format(util_err_msg,
                                                   config_path_err_msg)
                    raise UtilError(util_err_msg)

            else:
                # If needed, create a MyDefaultsReader and search for
                # my_print_defaults tool.
                if not my_defaults_reader:
                    try:
                        my_defaults_reader = MyDefaultsReader(options)
                    except UtilError as err:
                        if config_path_err_msg and not (port or socket):
                            util_err_msg = ("{0} In addition, {1}"
                                            "").format(err.errmsg,
                                                       config_path_err_msg)
                            raise UtilError(util_err_msg)
                        else:
                            raise

                elif not my_defaults_reader.tool_path:
                    my_defaults_reader.search_my_print_defaults_tool()

                # Check if the my_print_default tool is able to read a
                # login-path from the mylogin configuration file
                if not my_defaults_reader.check_login_path_support():
                    util_err_msg = ("the used my_print_defaults tool does not "
                                    "support login-path options: {0}. "
                                    "Please confirm that the location to a "
                                    "tool with login-path support is included "
                                    "in the PATH (at the beginning)."
                                    "".format(my_defaults_reader.tool_path))
                    if config_path_err_msg and not (port or socket):
                        util_err_msg = ("{0} In addition, {1}"
                                        "").format(util_err_msg,
                                                   config_path_err_msg)
                    raise UtilError(util_err_msg)

                # Read and parse the login-path data (i.e., user, password and
                # host)
                login_path_data = my_defaults_reader.get_group_data(login_path)

        if config_path_data or login_path_data:
            if config_path_data:
                if not login_path_data:
                    login_path_data = config_path_data
                else:
                    # Overwrite values from login_path_data
                    config_path_data.update(login_path_data)
                    login_path_data = config_path_data

            user = login_path_data.get('user', None)
            passwd = login_path_data.get('password', None)
            host = login_path_data.get('host', None)
            if not port:
                port = login_path_data.get('port', None)
            if not socket:
                socket = login_path_data.get('socket', None)

            if os.name == "posix" and socket is not None:
                # if we are on unix systems and used a socket, hostname can be
                # safely assumed as being localhost so it is not required
                required_options = ('user', 'socket')
                host = 'localhost' if host is None else host
            else:
                required_options = ('user', 'host', 'port')

            missing_options = [
                opt for opt in required_options if locals()[opt] is None
            ]
            # If we are on unix and port is missing, user might have specified
            # a socket instead
            if os.name == "posix" and "port" in missing_options:
                i = missing_options.index("port")
                if socket:  # If we have a socket, port is not needed
                    missing_options.pop(i)
                else:
                    # if we don't have neither a port nor a socket, we need
                    # either a port or a socket
                    missing_options[i] = "port or socket"

            if missing_options:
                message = ",".join(missing_options)
                if len(missing_options) > 1:
                    comma_idx = message.rfind(",")
                    message = "{0} and {1}".format(message[:comma_idx],
                                                   message[comma_idx + 1:])
                pluralize = "s" if len(missing_options) > 1 else ""
                raise UtilError("Missing connection value{0} for "
                                "{1} option{0}".format(pluralize, message))

            # optional options, available only on config_path_data
            if config_path_data:
                ssl_ca = config_path_data.get('ssl-ca', None)
                ssl_cert = config_path_data.get('ssl-cert', None)
                ssl_key = config_path_data.get('ssl-key', None)
                ssl = config_path_data.get('ssl', None)

        else:
            if login_path and not config_path:
                raise UtilError("No login credentials found for login-path: "
                                "{0}. Please review the used connection string"
                                ": {1}".format(login_path, connection_values))
            elif not login_path and config_path:
                raise UtilError("No login credentials found for config-path: "
                                "{0}. Please review the used connection string"
                                ": {1}".format(login_path, connection_values))
            elif login_path and config_path:
                raise UtilError("No login credentials found for either "
                                "login-path: '{0}' nor config-path: '{1}'. "
                                "Please review the used connection string: {2}"
                                "".format(login_path, config_path,
                                          connection_values))

    elif len(conn_format) == 2:

        # Check to see if the user attempted to pass a list of connections.
        # This is true if there is at least one comma and multiple @ symbols.
        if ((connection_values.find(',') > 0)
                and (connection_values.find('@') > 1)):
            raise FormatError(_MULTIPLE_CONNECTIONS.format(connection_values))

        # Handle as in the format: user[:password]@host[:port][:socket]
        userpass, hostportsock = conn_format

        # Get user, password
        match = _CONN_USERPASS.match(userpass)
        if not match:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        user = match.group('user')
        if user is None:
            # No password provided
            user = match.group('suser').rstrip(':')
        passwd = match.group('passwd')

        # Handle host, port and socket
        if len(hostportsock) <= 0:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

        if hostportsock[0] in ['"', "'"]:
            # need to strip the quotes
            host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock)
            if host[0] == '"':
                host = host.strip('"')
            if host[0] == "'":
                host = host.strip("'")

        else:
            host, port, socket, _ = parse_server_address(hostportsock)

    else:
        # Unrecognized format
        raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

    # Get character-set from options
    if isinstance(options, dict):
        charset = options.get("charset", None)
        # If one SSL option was found before, not mix with those in options.
        if not ssl_cert and not ssl_ca and not ssl_key and not ssl:
            ssl_cert = options.get("ssl_cert", None)
            ssl_ca = options.get("ssl_ca", None)
            ssl_key = options.get("ssl_key", None)
            ssl = options.get("ssl", None)

    else:
        # options is an instance of optparse.Values
        try:
            charset = options.charset  # pylint: disable=E1103
        except AttributeError:
            charset = None
        # If one SSL option was found before, not mix with those in options.
        if not ssl_cert and not ssl_ca and not ssl_key and not ssl:
            try:
                ssl_cert = options.ssl_cert  # pylint: disable=E1103
            except AttributeError:
                ssl_cert = None
            try:
                ssl_ca = options.ssl_ca  # pylint: disable=E1103
            except AttributeError:
                ssl_ca = None
            try:
                ssl_key = options.ssl_key  # pylint: disable=E1103
            except AttributeError:
                ssl_key = None
            try:
                ssl = options.ssl  # pylint: disable=E1103
            except AttributeError:
                ssl = None

    # Set parsed connection values
    connection = {
        "user": user,
        "host": host,
        "port": int(port) if port else 3306,
        "passwd": passwd if passwd else ''
    }

    if charset:
        connection["charset"] = charset
    if ssl_cert:
        connection["ssl_cert"] = ssl_cert
    if ssl_ca:
        connection["ssl_ca"] = ssl_ca
    if ssl_key:
        connection["ssl_key"] = ssl_key
    if ssl:
        connection["ssl"] = ssl
    # Handle optional parameters. They are only stored in the dict if
    # they were provided in the specifier.
    if socket is not None and os.name == "posix":
        connection['unix_socket'] = socket

    return connection
Example #4
0
def parse_connection(connection_values, my_defaults_reader=None, options={}):
    """Parse connection values.

    The function parses a connection specification of one of the forms::

      - user[:password]@host[:port][:socket]
      - login-path[:port][:socket]

    A dictionary is returned containing the connection parameters. The
    function is designed so that it shall be possible to use it with a
    ``connect`` call in the following manner::

      options = parse_connection(spec)
      conn = mysql.connector.connect(**options)

    conn_values[in]         Connection values in the form:
                            user:password@host:port:socket
                            or login-path:port:socket
    my_defaults_reader[in]  Instance of MyDefaultsReader to read the
                            information of the login-path from configuration
                            files. By default, the value is None.
    options[in]             Dictionary of options (e.g. basedir), from the used
                            utility. By default, it set with an empty
                            dictionary. Note: also supports options values
                            from optparse.

    Notes:

    This method validates IPv4 addresses and standard IPv6 addresses.

    This method accepts quoted host portion strings. If the host is marked
    with quotes, the code extracts this without validation and assigns it to
    the host variable in the returned tuple. This allows users to specify host
    names and IP addresses that are outside of the supported validation.

    Returns dictionary (user, passwd, host, port, socket)
            or raise an exception if parsing error
    """

    def _match(pattern, search_str):
        grp = pattern.match(search_str)
        if not grp:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        return grp.groups()

    # Split on the '@' to determine the connection string format.
    conn_format = connection_values.split('@')

    if len(conn_format) == 1:
        # No '@' then handle has in the format: login-path[:port][:socket]
        login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0])

        #Check if the login configuration file (.mylogin.cnf) exists
        if login_path and not my_login_config_exists():
            raise UtilError(".mylogin.cnf was not found at is default "
                            "location: %s."
                            "Please configure your login-path data before "
                            "using it (use the mysql_config_editor tool)."
                            % my_login_config_path())

        # If needed, create a MyDefaultsReader and search for my_print_defaults
        # tool.
        if not my_defaults_reader:
            my_defaults_reader = MyDefaultsReader(options)
        elif not my_defaults_reader.tool_path:
            my_defaults_reader.search_my_print_defaults_tool()

        # Check if the my_print_default tool is able to read a login-path from
        # the mylogin configuration file
        if not my_defaults_reader.check_login_path_support():
            raise UtilError("the used my_print_defaults tool does not "
                            "support login-path options: %s. "
                            "Please confirm that the location to a tool with "
                            "login-path support is included in the PATH "
                            "(at the beginning)."
                            % my_defaults_reader.tool_path)

        # Read and parse the login-path data (i.e., user, password and host)
        login_path_data = my_defaults_reader.get_group_data(login_path)

        if login_path_data:
            user = login_path_data.get('user', None)
            passwd = login_path_data.get('password', None)
            host = login_path_data.get('host', None)
            if not port:
                port = login_path_data.get('port', 3306)
            if not socket:
                socket = login_path_data.get('socket', None)
        else:
            raise UtilError("No login credentials found for login-path: %s. "
                            "Please review the used connection string: %s"
                            % (login_path, connection_values))

    elif len(conn_format) == 2:

        # Handle as in the format: user[:password]@host[:port][:socket]
        userpass, hostportsock = conn_format

        # Get user, password
        user, passwd = _match(_CONN_USERPASS, userpass)

        # Handle host, port and socket
        if len(hostportsock) <= 0:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

        if hostportsock[0] in ['"', "'"]:
            # need to strip the quotes
            host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock)
            if host[0] == '"':
                host = host.strip('"')
            if host[0] == "'":
                host = host.strip("'")
        elif len(hostportsock.split(":")) <= 3:  # if fewer colons, must be IPv4
            host, port, socket = _match(_CONN_IPv4, hostportsock)
        else:
            host, port, socket = _match(_CONN_IPv6, hostportsock)

    else:
        # Unrecognized format
        raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

    # Set parsed connection values
    connection = {
        "user"   : user,
        "host"   : host,
        "port"   : int(port) if port else 3306,
        "passwd" : passwd if passwd else ''
    }

    # Handle optional parameters. They are only stored in the dict if
    # they were provided in the specifier.
    if socket is not None and os.name == "posix":
        connection['unix_socket'] = socket

    return connection
Example #5
0
def parse_connection(connection_values, my_defaults_reader=None, options=None):
    """Parse connection values.

    The function parses a connection specification of one of the forms::

      - user[:password]@host[:port][:socket]
      - login-path[:port][:socket]

    A dictionary is returned containing the connection parameters. The
    function is designed so that it shall be possible to use it with a
    ``connect`` call in the following manner::

      options = parse_connection(spec)
      conn = mysql.connector.connect(**options)

    conn_values[in]         Connection values in the form:
                            user:password@host:port:socket
                            or login-path:port:socket
    my_defaults_reader[in]  Instance of MyDefaultsReader to read the
                            information of the login-path from configuration
                            files. By default, the value is None.
    options[in]             Dictionary of options (e.g. basedir), from the used
                            utility. By default, it set with an empty
                            dictionary. Note: also supports options values
                            from optparse.

    Notes:

    This method validates IPv4 addresses and standard IPv6 addresses.

    This method accepts quoted host portion strings. If the host is marked
    with quotes, the code extracts this without validation and assigns it to
    the host variable in the returned tuple. This allows users to specify host
    names and IP addresses that are outside of the supported validation.

    Returns dictionary (user, passwd, host, port, socket)
            or raise an exception if parsing error
    """
    if options is None:
        options = {}

    def _match(pattern, search_str):
        """Returns the groups from string search or raise FormatError if it
        does not match with the pattern.
        """
        grp = pattern.match(search_str)
        if not grp:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        return grp.groups()

    # SSL options, must not be overwritten with those from options.
    ssl_ca = None
    ssl_cert = None
    ssl_key = None
    ssl = None

    # Split on the '@' to determine the connection string format.
    # The user/password may have the '@' character, split by last occurrence.
    conn_format = connection_values.rsplit('@', 1)

    if len(conn_format) == 1:
        # No '@' so try config-path and login-path

        # The config_path and login-path collide on their first element and
        # only differs on their secondary optional values.
        # 1. Try match config_path and his optional value group. If both
        #    matches and the connection data can be retrieved, return the data.
        #    If errors occurs in this step raise them immediately.
        # 2. If config_path matches but group does not, and connection data
        #    can not be retrieved, do not raise errors and try to math
        #    login_path on step 4.
        # 3. If connection data is retrieved on step 2, then try login_path on
        #    next step to overwrite values from the new configuration.
        # 4. If login_path matches, check is .mylogin.cnf exists, if it doesn't
        #    and data configuration was found verify it  for missing values and
        #    continue if they are not any missing.
        # 5. If .mylogin.cnf exists and data configuration is found, overwrite
        #    any previews value from config_path if there is any.
        # 6. If login_path matches a secondary value but the configuration data
        #    could not be retrieved, do not continue and raise any error.
        # 7. In case errors have occurred trying to get data from config_path,
        #    and group did not matched, and in addition no secondary value,
        #    matched from login_path (port of socket) mention that config_path
        #    and login_path were not able to retrieve the connection data.

        # try login_path and overwrite the values.
        # Handle the format: config-path[[group]]
        config_path, group = _match(_CONN_CONFIGPATH, conn_format[0])
        port = None
        socket = None
        config_path_data = None
        login_path_data = None
        config_path_err_msg = None
        login_path = None
        if config_path:
            try:
                # If errors occurs, and group matched: raise any errors as the
                # group is exclusive of config_path.
                config_path_data = handle_config_path(config_path, group)
            except UtilError as err:
                if group:
                    raise
                else:
                    # Convert first letter to lowercase to include in error
                    # message with the correct case.
                    config_path_err_msg = \
                        err.errmsg[0].lower() + err.errmsg[1:]

        if group is None:
            # the conn_format can still be a login_path so continue
            # No '@' then handle has in the format: login-path[:port][:socket]
            login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0])

            # Check if the login configuration file (.mylogin.cnf) exists
            if login_path and not my_login_config_exists():
                if not config_path_data:
                    util_err_msg = (".mylogin.cnf was not found at is default "
                                    "location: {0}. Please configure your "
                                    "login-path data before using it (use the "
                                    "mysql_config_editor tool)."
                                    "".format(my_login_config_path()))
                    if config_path_err_msg and not (port or socket):
                        util_err_msg = ("{0} In addition, {1}"
                                        "").format(util_err_msg,
                                                   config_path_err_msg)
                    raise UtilError(util_err_msg)

            else:
                # If needed, create a MyDefaultsReader and search for
                # my_print_defaults tool.
                if not my_defaults_reader:
                    try:
                        my_defaults_reader = MyDefaultsReader(options)
                    except UtilError as err:
                        if config_path_err_msg and not (port or socket):
                            util_err_msg = ("{0} In addition, {1}"
                                            "").format(err.errmsg,
                                                       config_path_err_msg)
                            raise UtilError(util_err_msg)
                        else:
                            raise

                elif not my_defaults_reader.tool_path:
                    my_defaults_reader.search_my_print_defaults_tool()

                # Check if the my_print_default tool is able to read a
                # login-path from the mylogin configuration file
                if not my_defaults_reader.check_login_path_support():
                    util_err_msg = ("the used my_print_defaults tool does not "
                                    "support login-path options: {0}. "
                                    "Please confirm that the location to a "
                                    "tool with login-path support is included "
                                    "in the PATH (at the beginning)."
                                    "".format(my_defaults_reader.tool_path))
                    if config_path_err_msg and not (port or socket):
                        util_err_msg = ("{0} In addition, {1}"
                                        "").format(util_err_msg,
                                                   config_path_err_msg)
                    raise UtilError(util_err_msg)

                # Read and parse the login-path data (i.e., user, password and
                # host)
                login_path_data = my_defaults_reader.get_group_data(login_path)

        if config_path_data or login_path_data:
            if config_path_data:
                if not login_path_data:
                    login_path_data = config_path_data
                else:
                    # Overwrite values from login_path_data
                    config_path_data.update(login_path_data)
                    login_path_data = config_path_data

            user = login_path_data.get('user', None)
            passwd = login_path_data.get('password', None)
            host = login_path_data.get('host', None)
            if not port:
                port = login_path_data.get('port', None)
            if not socket:
                socket = login_path_data.get('socket', None)

            if os.name == "posix" and socket is not None:
                # if we are on unix systems and used a socket, hostname can be
                # safely assumed as being localhost so it is not required
                required_options = ('user', 'socket')
                host = 'localhost' if host is None else host
            else:
                required_options = ('user', 'host', 'port')

            missing_options = [opt for opt in required_options
                               if locals()[opt] is None]
            # If we are on unix and port is missing, user might have specified
            # a socket instead
            if os.name == "posix" and "port" in missing_options:
                i = missing_options.index("port")
                if socket:  # If we have a socket, port is not needed
                    missing_options.pop(i)
                else:
                    # if we don't have neither a port nor a socket, we need
                    # either a port or a socket
                    missing_options[i] = "port or socket"

            if missing_options:
                message = ",".join(missing_options)
                if len(missing_options) > 1:
                    comma_idx = message.rfind(",")
                    message = "{0} and {1}".format(message[:comma_idx],
                                                   message[comma_idx + 1:])
                pluralize = "s" if len(missing_options) > 1 else ""
                raise UtilError("Missing connection value{0} for "
                                "{1} option{0}".format(pluralize, message))

            # optional options, available only on config_path_data
            if config_path_data:
                ssl_ca = config_path_data.get('ssl-ca', None)
                ssl_cert = config_path_data.get('ssl-cert', None)
                ssl_key = config_path_data.get('ssl-key', None)
                ssl = config_path_data.get('ssl', None)

        else:
            if login_path and not config_path:
                raise UtilError("No login credentials found for login-path: "
                                "{0}. Please review the used connection string"
                                ": {1}".format(login_path, connection_values))
            elif not login_path and config_path:
                raise UtilError("No login credentials found for config-path: "
                                "{0}. Please review the used connection string"
                                ": {1}".format(login_path, connection_values))
            elif login_path and config_path:
                raise UtilError("No login credentials found for either "
                                "login-path: '{0}' nor config-path: '{1}'. "
                                "Please review the used connection string: {2}"
                                "".format(login_path, config_path,
                                          connection_values))

    elif len(conn_format) == 2:

        # Handle as in the format: user[:password]@host[:port][:socket]
        userpass, hostportsock = conn_format

        # Get user, password
        match = _CONN_USERPASS.match(userpass)
        if not match:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        user = match.group('user')
        if user is None:
            # No password provided
            user = match.group('suser').rstrip(':')
        passwd = match.group('passwd')

        # Handle host, port and socket
        if len(hostportsock) <= 0:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

        if hostportsock[0] in ['"', "'"]:
            # need to strip the quotes
            host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock)
            if host[0] == '"':
                host = host.strip('"')
            if host[0] == "'":
                host = host.strip("'")

        else:
            host, port, socket, _ = parse_server_address(hostportsock)

    else:
        # Unrecognized format
        raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

    # Get character-set from options
    if isinstance(options, dict):
        charset = options.get("charset", None)
        # If one SSL option was found before, not mix with those in options.
        if not ssl_cert and not ssl_ca and not ssl_key and not ssl:
            ssl_cert = options.get("ssl_cert", None)
            ssl_ca = options.get("ssl_ca", None)
            ssl_key = options.get("ssl_key", None)
            ssl = options.get("ssl", None)

    else:
        # options is an instance of optparse.Values
        try:
            charset = options.charset  # pylint: disable=E1103
        except AttributeError:
            charset = None
        # If one SSL option was found before, not mix with those in options.
        if not ssl_cert and not ssl_ca and not ssl_key and not ssl:
            try:
                ssl_cert = options.ssl_cert  # pylint: disable=E1103
            except AttributeError:
                ssl_cert = None
            try:
                ssl_ca = options.ssl_ca  # pylint: disable=E1103
            except AttributeError:
                ssl_ca = None
            try:
                ssl_key = options.ssl_key  # pylint: disable=E1103
            except AttributeError:
                ssl_key = None
            try:
                ssl = options.ssl  # pylint: disable=E1103
            except AttributeError:
                ssl = None

    # Set parsed connection values
    connection = {
        "user": user,
        "host": host,
        "port": int(port) if port else 3306,
        "passwd": passwd if passwd else ''
    }

    if charset:
        connection["charset"] = charset
    if ssl_cert:
        connection["ssl_cert"] = ssl_cert
    if ssl_ca:
        connection["ssl_ca"] = ssl_ca
    if ssl_key:
        connection["ssl_key"] = ssl_key
    if ssl:
        connection["ssl"] = ssl
    # Handle optional parameters. They are only stored in the dict if
    # they were provided in the specifier.
    if socket is not None and os.name == "posix":
        connection['unix_socket'] = socket

    return connection
Example #6
0
def parse_connection(connection_values, my_defaults_reader=None, options={}):
    """Parse connection values.

    The function parses a connection specification of one of the forms::

      - user[:password]@host[:port][:socket]
      - login-path[:port][:socket]

    A dictionary is returned containing the connection parameters. The
    function is designed so that it shall be possible to use it with a
    ``connect`` call in the following manner::

      options = parse_connection(spec)
      conn = mysql.connector.connect(**options)

    conn_values[in]         Connection values in the form:
                            user:password@host:port:socket
                            or login-path:port:socket
    my_defaults_reader[in]  Instance of MyDefaultsReader to read the
                            information of the login-path from configuration
                            files. By default, the value is None.
    options[in]             Dictionary of options (e.g. basedir), from the used
                            utility. By default, it set with an empty
                            dictionary. Note: also supports options values
                            from optparse.

    Notes:

    This method validates IPv4 addresses and standard IPv6 addresses.

    This method accepts quoted host portion strings. If the host is marked
    with quotes, the code extracts this without validation and assigns it to
    the host variable in the returned tuple. This allows users to specify host
    names and IP addresses that are outside of the supported validation.

    Returns dictionary (user, passwd, host, port, socket)
            or raise an exception if parsing error
    """
    def _match(pattern, search_str):
        grp = pattern.match(search_str)
        if not grp:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))
        return grp.groups()

    # Split on the '@' to determine the connection string format.
    conn_format = connection_values.split('@')

    if len(conn_format) == 1:
        # No '@' then handle has in the format: login-path[:port][:socket]
        login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0])

        #Check if the login configuration file (.mylogin.cnf) exists
        if login_path and not my_login_config_exists():
            raise UtilError(".mylogin.cnf was not found at is default "
                            "location: %s."
                            "Please configure your login-path data before "
                            "using it (use the mysql_config_editor tool)." %
                            my_login_config_path())

        # If needed, create a MyDefaultsReader and search for my_print_defaults
        # tool.
        if not my_defaults_reader:
            my_defaults_reader = MyDefaultsReader(options)
        elif not my_defaults_reader.tool_path:
            my_defaults_reader.search_my_print_defaults_tool()

        # Check if the my_print_default tool is able to read a login-path from
        # the mylogin configuration file
        if not my_defaults_reader.check_login_path_support():
            raise UtilError("the used my_print_defaults tool does not "
                            "support login-path options: %s. "
                            "Please confirm that the location to a tool with "
                            "login-path support is included in the PATH "
                            "(at the beginning)." %
                            my_defaults_reader.tool_path)

        # Read and parse the login-path data (i.e., user, password and host)
        login_path_data = my_defaults_reader.get_group_data(login_path)

        if login_path_data:
            user = login_path_data.get('user', None)
            passwd = login_path_data.get('password', None)
            host = login_path_data.get('host', None)
            if not port:
                port = login_path_data.get('port', 3306)
            if not socket:
                socket = login_path_data.get('socket', None)
        else:
            raise UtilError("No login credentials found for login-path: %s. "
                            "Please review the used connection string: %s" %
                            (login_path, connection_values))

    elif len(conn_format) == 2:

        # Handle as in the format: user[:password]@host[:port][:socket]
        userpass, hostportsock = conn_format

        # Get user, password
        user, passwd = _match(_CONN_USERPASS, userpass)

        # Handle host, port and socket
        if len(hostportsock) <= 0:
            raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

        if hostportsock[0] in ['"', "'"]:
            # need to strip the quotes
            host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock)
            if host[0] == '"':
                host = host.strip('"')
            if host[0] == "'":
                host = host.strip("'")
        elif len(
                hostportsock.split(":")) <= 3:  # if fewer colons, must be IPv4
            host, port, socket = _match(_CONN_IPv4, hostportsock)
        else:
            host, port, socket = _match(_CONN_IPv6, hostportsock)

    else:
        # Unrecognized format
        raise FormatError(_BAD_CONN_FORMAT.format(connection_values))

    # Set parsed connection values
    connection = {
        "user": user,
        "host": host,
        "port": int(port) if port else 3306,
        "passwd": passwd if passwd else ''
    }

    # Handle optional parameters. They are only stored in the dict if
    # they were provided in the specifier.
    if socket is not None and os.name == "posix":
        connection['unix_socket'] = socket

    return connection
 def test_my_login_config_path(self):
     # Check if the path (directory) to .mylogin.cnf exists
     path = my_login_config_path()
     self.assertTrue(os.path.isdir(path))