mysqlparser

package
v2.0.0-alpha56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

MySQL Package Documentation

The mysqlparser package encompasses the parser and mapping logic required to read MySql binary messages and capture and test the outputs. Utilized by the hooks package, it assists in redirecting outgoing calls for the purpose of recording or testing the outputs.

SSL Support

Please note that SSL is currently not supported in the MySQL package. To use the package without SSL, you can include the following parameters in your database URL like the following example:

jdbc:mysql://localhost:3306/db_name?useSSL=false&allowPublicKeyRetrieval=true

The following MySQL packet types are handled in the parser:

COM_PING: A ping command sent to the server to check if it's alive and responsive.

COM_STMT_EXECUTE: Executes a prepared statement that was prepared using the COM_STMT_PREPARE command.

COM_STMT_FETCH: Fetches rows from a statement which produced a result set. Used with cursors in server-side prepared statements.

COM_STMT_PREPARE: Prepares a SQL statement for execution.

COM_STMT_CLOSE: Closes a prepared statement, freeing up server resources associated with it.

COM_CHANGE_USER: Changes the user of the current connection and resets the connection state.

MySQLOK: A packet indicating a successful operation. It is usually received after commands like INSERT, UPDATE, DELETE, etc.

MySQLErr: An error packet sent from the server to the client, indicating an error occurred with the last command sent.

RESULT_SET_PACKET: Contains the actual result set data returned by a query. It's a series of packets containing rows and columns of data.

MySQLHandshakeV10: The initial handshake packet sent from the server to the client when a connection is established, containing authentication and connection details.

HANDSHAKE_RESPONSE: The response packet sent by the client in reply to MySQLHandshakeV10, containing client authentication data.

MySQLQuery: Contains a SQL query that is to be executed on the server.

AUTH_SWITCH_REQUEST: Sent by the server to request an authentication method switch during the connection process.

AUTH_SWITCH_RESPONSE: Sent by the client to respond to the AUTH_SWITCH_REQUEST, containing authentication data.

MySQLEOF: An EOF (End Of File) packet that marks the end of a result set or the end of the fields list.

AUTH_MORE_DATA: Sent by the server if it needs more data for authentication (used in plugins).

COM_STMT_SEND_LONG_DATA: Sends data for a column in a row to be inserted/updated in a table using a prepared statement.

COM_STMT_RESET: Resets the data of a prepared statement which was accumulated with COM_STMT_SEND_LONG_DATA commands.

COM_QUIT: Sent by the client to close the connection to the server gracefully.

Documentation

Index

Constants

View Source
const (
	CLIENT_PLUGIN_AUTH                = 0x00080000
	CLIENT_CONNECT_WITH_DB            = 0x00000008
	CLIENT_CONNECT_ATTRS              = 0x00100000
	CLIENT_ZSTD_COMPRESSION_ALGORITHM = 0x00010000
)

Variables

This section is empty.

Functions

func ReadFirstBuffer

func ReadFirstBuffer(clientConn, destConn net.Conn) ([]byte, string, error)

func ReadLengthEncodedIntegers

func ReadLengthEncodedIntegers(data []byte, offset int) (uint64, int)

func Uint24

func Uint24(data []byte) uint32

Types

type AuthSwitchRequestPacket

type AuthSwitchRequestPacket struct {
	StatusTag      byte   `yaml:"status_tag"`
	PluginName     string `yaml:"plugin_name"`
	PluginAuthData string `yaml:"plugin_authdata"`
}

type AuthSwitchResponsePacket

type AuthSwitchResponsePacket struct {
	AuthResponseData string `yaml:"auth_response_data"`
}

type BoundParameter

type BoundParameter struct {
	Type     byte   `yaml:"type"`
	Unsigned byte   `yaml:"unsigned"`
	Value    []byte `yaml:"value"`
}

type COM_STMT_RESET

type COM_STMT_RESET struct {
	StatementID uint32 `yaml:"statement_id"`
}

type COM_STMT_SEND_LONG_DATA

type COM_STMT_SEND_LONG_DATA struct {
	StatementID uint32 `yaml:"statement_id"`
	ParameterID uint16 `yaml:"parameter_id"`
	Data        []byte `yaml:"data"`
}

type CapabilityFlags

type CapabilityFlags uint32

type ColumnDefinition

type ColumnDefinition struct {
	PacketHeader PacketHeader `yaml:"packet_header"`
	Catalog      string       `yaml:"catalog"`
	Schema       string       `yaml:"schema"`
	Table        string       `yaml:"table"`
	OrgTable     string       `yaml:"org_table"`
	Name         string       `yaml:"name"`
	OrgName      string       `yaml:"org_name"`
	NextLength   uint64       `yaml:"next_length"`
	CharacterSet uint16       `yaml:"character_set"`
	ColumnLength uint32       `yaml:"column_length"`
	ColumnType   byte         `yaml:"column_type"`
	Flags        uint16       `yaml:"flags"`
	Decimals     byte         `yaml:"decimals"`
	DefaultValue string       `yaml:"string"`
}

type ComChangeUserPacket

type ComChangeUserPacket struct {
	User         string `yaml:"user"`
	Auth         []byte `yaml:"auth"`
	Db           string `yaml:"db"`
	CharacterSet uint8  `yaml:"character_set"`
	AuthPlugin   string `yaml:"auth_plugin"`
}

type ComInitDbPacket

type ComInitDbPacket struct {
	Status byte
	DbName string
}

type ComPingPacket

type ComPingPacket struct {
}

type ComStmtCloseAndPrepare

type ComStmtCloseAndPrepare struct {
	StmtClose   ComStmtClosePacket
	StmtPrepare ComStmtPreparePacket1
}

type ComStmtClosePacket

type ComStmtClosePacket struct {
	Status      byte
	StatementID uint32
}

type ComStmtExecute

type ComStmtExecute struct {
	StatementID    uint32           `yaml:"statement_id"`
	Flags          byte             `yaml:"flags"`
	IterationCount uint32           `yaml:"iteration_count"`
	NullBitmap     []byte           `yaml:"null_bitmap"`
	ParamCount     uint16           `yaml:"param_count"`
	Parameters     []BoundParameter `yaml:"parameters"`
}

type ComStmtFetchPacket

type ComStmtFetchPacket struct {
	StatementID uint32 `yaml:"statement_id"`
	RowCount    uint32 `yaml:"row_count"`
	Info        string `yaml:"info"`
}

type ComStmtPreparePacket

type ComStmtPreparePacket struct {
	Query string
}

type ComStmtPreparePacket1

type ComStmtPreparePacket1 struct {
	Header []byte
	Query  string
}

type EOFPacket

type EOFPacket struct {
	Header      byte   `yaml:"header"`
	Warnings    uint16 `yaml:"warnings"`
	StatusFlags uint16 `yaml:"status_flags"`
}

type ERRPacket

type ERRPacket struct {
	Header         byte   `yaml:"header"`
	ErrorCode      uint16 `yaml:"error_code"`
	SQLStateMarker string `yaml:"sql_state_marker"`
	SQLState       string `yaml:"sql_state"`
	ErrorMessage   string `yaml:"error_message"`
}

type HandshakeResponse

type HandshakeResponse struct {
	CapabilityFlags      uint32            `yaml:"capability_flags"`
	MaxPacketSize        uint32            `yaml:"max_packet_size"`
	CharacterSet         uint8             `yaml:"character_set"`
	Reserved             [23]byte          `yaml:"reserved"`
	Username             string            `yaml:"username"`
	AuthData             []byte            `yaml:"auth_data"`
	Database             string            `yaml:"database"`
	AuthPluginName       string            `yaml:"auth_plugin_name"`
	ConnectAttributes    map[string]string `yaml:"connect_attributes"`
	ZstdCompressionLevel byte              `yaml:"zstdcompressionlevel"`
}

type HandshakeResponseOk

type HandshakeResponseOk struct {
	PacketIndicator string        `yaml:"packet_indicator"`
	PluginDetails   PluginDetails `yaml:"plugin_details"`
	RemainingBytes  []byte        `yaml:"remaining_bytes"`
}

type HandshakeV10Packet

type HandshakeV10Packet struct {
	ProtocolVersion uint8  `yaml:"protocol_version"`
	ServerVersion   string `yaml:"server_version"`
	ConnectionID    uint32 `yaml:"connection_id"`
	AuthPluginData  []byte `yaml:"auth_plugin_data"`
	CapabilityFlags uint32 `yaml:"capability_flags"`
	CharacterSet    uint8  `yaml:"character_set"`
	StatusFlags     uint16 `yaml:"status_flags"`
	AuthPluginName  string `yaml:"auth_plugin_name"`
}

func (*HandshakeV10Packet) GetAuthMethod

func (packet *HandshakeV10Packet) GetAuthMethod() string

func (*HandshakeV10Packet) ShouldUseSSL

func (packet *HandshakeV10Packet) ShouldUseSSL() bool

type MySQLPacket

type MySQLPacket struct {
	Header  MySQLPacketHeader `yaml:"header"`
	Payload []byte            `yaml:"payload"`
}

func (*MySQLPacket) Encode

func (p *MySQLPacket) Encode() ([]byte, error)

type MySQLPacketHeader

type MySQLPacketHeader struct {
	PayloadLength uint32 `yaml:"payload_length"` // MySQL packet payload length
	SequenceID    uint8  `yaml:"sequence_id"`    // MySQL packet sequence ID
}

func DecodeMySQLPacket

func DecodeMySQLPacket(packet MySQLPacket, logger *zap.Logger, destConn net.Conn) (string, MySQLPacketHeader, interface{}, error)

type MySqlParser

type MySqlParser struct {
	// contains filtered or unexported fields
}

func NewMySqlParser

func NewMySqlParser(logger *zap.Logger, hooks *hooks.Hook, delay uint64) *MySqlParser

func (*MySqlParser) OutgoingType

func (sql *MySqlParser) OutgoingType(buffer []byte) bool

func (*MySqlParser) ProcessOutgoing

func (sql *MySqlParser) ProcessOutgoing(requestBuffer []byte, clientConn, destConn net.Conn, ctx context.Context)

type NextAuthPacket

type NextAuthPacket struct {
	PluginData byte `yaml:"plugin_data"`
}

type OKPacket

type OKPacket struct {
	AffectedRows uint64 `json:"affected_rows,omitempty" yaml:"affected_rows"`
	LastInsertID uint64 `json:"last_insert_id,omitempty" yaml:"last_insert_id"`
	StatusFlags  uint16 `json:"status_flags,omitempty" yaml:"status_flags"`
	Warnings     uint16 `json:"warnings,omitempty" yaml:"warnings"`
	Info         string `json:"info,omitempty" yaml:"info"`
}

type PacketHeader

type PacketHeader struct {
	PacketLength     uint8 `yaml:"packet_length"`
	PacketSequenceID uint8 `yaml:"packet_sequence_id"`
}

common

type PacketType2

type PacketType2 struct {
	Field1 byte `yaml:"field1"`
	Field2 byte `yaml:"field2"`
	Field3 byte `yaml:"field3"`
}

type PasswordData

type PasswordData struct {
	PayloadLength uint32 `yaml:"payload_length"`
	SequenceID    byte   `yaml:"sequence_id"`
	Payload       []byte `yaml:"payload"`
}

type PluginDetails

type PluginDetails struct {
	Type    string `yaml:"type"`
	Message string `yaml:"message"`
}

type QueryPacket

type QueryPacket struct {
	Command byte   `yaml:"command"`
	Query   string `yaml:"query"`
}

type ResultSet

type ResultSet struct {
	Columns             []*ColumnDefinition `yaml:"columns"`
	Rows                []*Row              `yaml:"rows"`
	EOFPresent          bool                `yaml:"eofPresent"`
	PaddingPresent      bool                `yaml:"paddingPresent"`
	EOFPresentFinal     bool                `yaml:"eofPresentFinal"`
	PaddingPresentFinal bool                `yaml:"paddingPresentFinal"`
	OptionalPadding     bool                `yaml:"optionalPadding"`
	OptionalEOFBytes    []byte              `yaml:"optionalEOFBytes"`
	EOFAfterColumns     []byte              `yaml:"eofAfterColumns"`
}

type Row

type Row struct {
	Header  RowHeader             `yaml:"header"`
	Columns []RowColumnDefinition `yaml:"row_column_definition"`
}

type RowColumnDefinition

type RowColumnDefinition struct {
	Type  models.FieldType `yaml:"type"`
	Name  string           `yaml:"name"`
	Value interface{}      `yaml:"value"`
}

type RowDataPacket

type RowDataPacket struct {
	Data []byte `yaml:"data"`
}

type RowHeader

type RowHeader struct {
	PacketLength int   `yaml:"packet_length"`
	SequenceID   uint8 `yaml:"sequence_id"`
}

type StmtPrepareOk

type StmtPrepareOk struct {
	Status       byte               `yaml:"status"`
	StatementID  uint32             `yaml:"statement_id"`
	NumColumns   uint16             `yaml:"num_columns"`
	NumParams    uint16             `yaml:"num_params"`
	WarningCount uint16             `yaml:"warning_count"`
	ColumnDefs   []ColumnDefinition `yaml:"column_definitions"`
	ParamDefs    []ColumnDefinition `yaml:"param_definitions"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL