Example #1
0
    def _handle_call(self, call_match):
        """Handle a CALL statement

        :param call_match: Result of re.match()
        """
        group, command, args_str = call_match.groups()
        group_command = "{0}.{1}".format(group, command)

        if self._curr_user and \
                not self._curr_user.has_permission('core', group, command):
            self.send_error(
                errorcode.ER_PROCACCESS_DENIED_ERROR,
                "Execute command denied to user '{user}' for "
                "command '{cmd_name}'".format(
                    user=self._curr_user.username, cmd_name=group_command),
                "42000")
            return

        args = []
        kwargs = {}
        if args_str:
            split_args = PARSE_CALL_ARGS.split(args_str)
            for arg in split_args:
                arg = arg.strip()
                kwarg = PARSE_CALL_KWARG.split(arg)
                if len(kwarg) == 2:
                    # we have a keyword argument
                    kwargs[kwarg[0].strip()] = dequote(kwarg[1].strip())
                elif arg == 'NULL':
                    args.append(None)
                else:
                    args.append(dequote(arg.strip()))

        _LOGGER.debug("executing: %s.%s(%s, %s)",
                      group, command,
                      str(args), str(kwargs))

        cmd_exec = self.server.get_command_exec(group_command)

        if not cmd_exec:
            self.send_error_unknown_command()
            return

        try:
            result = cmd_exec(*args, **kwargs)
        except Exception as exc:
            self.send_error(
                errorcode.ER_WRONG_PARAMETERS_TO_PROCEDURE,
                "Failed executing command {cmd_name}: {error}".format(
                    error=str(exc), cmd_name=group_command),
                "22000")
        else:
            if self._format == 'json':
                self.send_json(result, group, command)
            else:
                self.send_cmd_result(group=group, command=command,
                                     cmd_result=result)
Example #2
0
    def handle(self):
        """Handle incoming requests"""
        response = None
        if not self._handshaked:
            if not self._do_hanshake():
                # Bail out when handshake failed.
                return
        elif not self._authenticated:
            self.send_error(errorcode.CR_UNKNOWN_ERROR,
                            "Not authenticated after successful handshake",
                            "HY000")

        if not self._handshaked:
            # Bail out when handshake failed.
            return

        while True:
            data = None
            try:
                packet_type, data = self.read_packet()
            except Exception as exc:
                break

            self.request.update_activity()

            if packet_type == ServerCmd.PING:
                self.send_ok(info='MySQL Fabric')
                continue

            if not data:
                # Nothing to do, get next packet.
                continue

            data = data.strip().decode('utf8')

            # Handle CALL
            if CHECK_CALL.match(data):
                call_match = PARSE_CALL.match(data)
                if call_match:
                    self._handle_call(call_match)
                else:
                    self.send_syntax_error()
                continue

            # Handle SHOW CREATE PROCEDURE
            if CHECK_SHOW_CREATE_PROC.match(data):
                showproc_match = PARSE_SHOW_CREATE_PROC.match(data)
                if showproc_match:
                    self._handle_show_create_procedure(showproc_match)
                else:
                    self.send_syntax_error()
                continue

            # Handle INFORMATION_SCHEMA.ROUTINES
            is_routines_match = PARSE_IS_ROUTINES.match(data)
            if is_routines_match:
                self._handle_information_schema_routines(is_routines_match)
                continue

            # Handle SET
            set_match = PARSE_SET.match(data)
            if set_match and set_match.group(1).lower() in ('format', ):
                format = dequote(set_match.group(2).strip()).strip().lower()
                if format not in ('json', ):
                    self.send_error(
                        errorcode.ER_LOCAL_VARIABLE,
                        "Format '{0}' is not supported".format(format),
                        "42000")
                else:
                    _LOGGER.debug("Format set to %s", format)
                    self._format = format

            # We accept anything else without doing anything
            self.send_packet(self.ok_packet())
Example #3
0
    def handle(self):
        """Handle incoming requests"""
        response = None
        if not self._handshaked:
            if not self._do_hanshake():
                # Bail out when handshake failed.
                return
        elif not self._authenticated:
            self.send_error(
                errorcode.CR_UNKNOWN_ERROR,
                "Not authenticated after successful handshake",
                "HY000")

        if not self._handshaked:
            # Bail out when handshake failed.
            return

        while True:
            data = None
            try:
                packet_type, data = self.read_packet()
            except Exception as exc:
                break

            self.request.update_activity()

            if packet_type == ServerCmd.PING:
                self.send_ok(info='MySQL Fabric')
                continue

            if not data:
                # Nothing to do, get next packet.
                continue

            data = data.strip().decode('utf8')

            # Handle CALL
            if CHECK_CALL.match(data):
                call_match = PARSE_CALL.match(data)
                if call_match:
                    self._handle_call(call_match)
                else:
                    self.send_syntax_error()
                continue

            # Handle SHOW CREATE PROCEDURE
            if CHECK_SHOW_CREATE_PROC.match(data):
                showproc_match = PARSE_SHOW_CREATE_PROC.match(data)
                if showproc_match:
                    self._handle_show_create_procedure(showproc_match)
                else:
                    self.send_syntax_error()
                continue

            # Handle INFORMATION_SCHEMA.ROUTINES
            is_routines_match = PARSE_IS_ROUTINES.match(data)
            if is_routines_match:
                self._handle_information_schema_routines(is_routines_match)
                continue

            # Handle SET
            set_match = PARSE_SET.match(data)
            if set_match and set_match.group(1).lower() in ('format',):
                format = dequote(set_match.group(2).strip()).strip().lower()
                if format not in ('json',):
                    self.send_error(
                        errorcode.ER_LOCAL_VARIABLE,
                        "Format '{0}' is not supported".format(format),
                        "42000"
                    )
                else:
                    _LOGGER.debug("Format set to %s", format)
                    self._format = format

            # We accept anything else without doing anything
            self.send_packet(self.ok_packet())