Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change binary lists in mysql mocks to base64 strings for readability #1276

Merged
merged 12 commits into from
Jan 10, 2024
Merged
225 changes: 118 additions & 107 deletions pkg/models/mysql.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/authMoreData.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

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

func decodeAuthMoreData(data []byte) (*NextAuthPacket, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/authSwitchRequestPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

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

func decodeAuthSwitchRequest(data []byte) (*AuthSwitchRequestPacket, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

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

func decodeAuthSwitchResponse(data []byte) (*AuthSwitchResponsePacket, error) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/proxy/integrations/mysqlparser/comChangeUserPacket.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package mysqlparser

import (
"encoding/base64"
"errors"
"strings"
)

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"`
User string `json:"user,omitempty" yaml:"user,omitempty,flow"`
Auth string `json:"auth,omitempty" yaml:"auth,omitempty,flow"`
Db string `json:"db,omitempty" yaml:"db,omitempty,flow"`
CharacterSet uint8 `json:"character_set,omitempty" yaml:"character_set,omitempty,flow"`
AuthPlugin string `json:"auth_plugin,omitempty" yaml:"auth_plugin,omitempty,flow"`
}

func decodeComChangeUser(data []byte) (ComChangeUserPacket, error) {
Expand All @@ -32,7 +33,7 @@ func decodeComChangeUser(data []byte) (ComChangeUserPacket, error) {

return ComChangeUserPacket{
User: user,
Auth: auth,
Auth: base64.StdEncoding.EncodeToString(auth),
Db: db,
CharacterSet: characterSet,
AuthPlugin: authPlugin,
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/comFetchPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

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

func decodeComStmtFetch(data []byte) (ComStmtFetchPacket, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/integrations/mysqlparser/comInitDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

type ComInitDbPacket struct {
Status byte
DbName string
Status byte `json:"status,omitempty" yaml:"status,omitempty"`
DbName string `json:"db_name,omitempty" yaml:"db_name,omitempty"`
}

func decodeComInitDb(data []byte) (*ComInitDbPacket, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/integrations/mysqlparser/comStmtCloseMoreData.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type ComStmtPreparePacket1 struct {
Header []byte
Query string
Header []byte `json:"header,omitempty" yaml:"header,omitempty"`
Query string `json:"query,omitempty" yaml:"query,omitempty"`
}

type ComStmtCloseAndPrepare struct {
StmtClose ComStmtClosePacket
StmtPrepare ComStmtPreparePacket1
StmtClose ComStmtClosePacket `json:"stmt_close,omitempty" yaml:"stmt_close,omitempty"`
StmtPrepare ComStmtPreparePacket1 `json:"stmt_prepare,omitempty" yaml:"stmt_prepare,omitempty"`
}

func decodeComStmtCloseMoreData(data []byte) (*ComStmtCloseAndPrepare, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/integrations/mysqlparser/comStmtClosePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type ComStmtClosePacket struct {
Status byte
StatementID uint32
Status byte `json:"status,omitempty" yaml:"status,omitempty"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty"`
}

func decodeComStmtClose(data []byte) (*ComStmtClosePacket, error) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/proxy/integrations/mysqlparser/comStmtPrepareOk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

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"`
Status byte `json:"status,omitempty" yaml:"status,omitempty,flow"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
NumColumns uint16 `json:"num_columns,omitempty" yaml:"num_columns,omitempty,flow"`
NumParams uint16 `json:"num_params,omitempty" yaml:"num_params,omitempty,flow"`
WarningCount uint16 `json:"warning_count,omitempty" yaml:"warning_count,omitempty,flow"`
ColumnDefs []ColumnDefinition `json:"column_definitions,omitempty" yaml:"column_definitions,omitempty,flow"`
ParamDefs []ColumnDefinition `json:"param_definitions,omitempty" yaml:"param_definitions,omitempty,flow"`
}

func decodeComStmtPrepareOk(data []byte) (*StmtPrepareOk, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/comStmtPreparePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type ComStmtPreparePacket struct {
Query string
Query string `json:"query,omitempty" yaml:"query,omitempty,flow"`
}

func decodeComStmtPrepare(data []byte) (*ComStmtPreparePacket, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/comStmtResetPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type COM_STMT_RESET struct {
StatementID uint32 `yaml:"statement_id"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
}

func decodeComStmtReset(packet []byte) (*COM_STMT_RESET, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package mysqlparser

import (
"encoding/base64"
"encoding/binary"
"fmt"
)

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

func decodeComStmtSendLongData(packet []byte) (COM_STMT_SEND_LONG_DATA, error) {
Expand All @@ -21,6 +22,6 @@ func decodeComStmtSendLongData(packet []byte) (COM_STMT_SEND_LONG_DATA, error) {
return COM_STMT_SEND_LONG_DATA{
StatementID: stmtID,
ParameterID: paramID,
Data: data,
Data: base64.StdEncoding.EncodeToString(data),
}, nil
}
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/encryptPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

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

func decodeEncryptPassword(data []byte) (string, *PasswordData, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/eofPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

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

func decodeMYSQLEOF(data []byte) (*EOFPacket, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/proxy/integrations/mysqlparser/errPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

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"`
Header byte `json:"header,omitempty" yaml:"header,omitempty,flow"`
ErrorCode uint16 `json:"error_code,omitempty" yaml:"error_code,omitempty,flow"`
SQLStateMarker string `json:"sql_state_marker,omitempty" yaml:"sql_state_marker,omitempty,flow"`
SQLState string `json:"sql_state,omitempty" yaml:"sql_state,omitempty,flow"`
ErrorMessage string `json:"error_message,omitempty" yaml:"error_message,omitempty,flow"`
}

func decodeMySQLErr(data []byte) (*ERRPacket, error) {
Expand Down
24 changes: 14 additions & 10 deletions pkg/proxy/integrations/mysqlparser/executePacket.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package mysqlparser

import (
"encoding/base64"
"encoding/binary"
"fmt"
)

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"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
Flags byte `json:"flags,omitempty" yaml:"flags,omitempty,flow"`
IterationCount uint32 `json:"iteration_count,omitempty" yaml:"iteration_count,omitempty,flow"`
NullBitmap string `json:"null_bitmap,omitempty" yaml:"null_bitmap,omitempty,flow"`
ParamCount uint16 `json:"param_count,omitempty" yaml:"param_count,omitempty,flow"`
Parameters []BoundParameter `json:"parameters,omitempty" yaml:"parameters,omitempty,flow"`
}

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

func decodeComStmtExecute(packet []byte) (ComStmtExecute, error) {
// removed the print statement for cleanliness
if len(packet) < 14 { // the minimal size of the packet without parameters should be 14, not 13
return ComStmtExecute{}, fmt.Errorf("packet length less than 14 bytes")
}
var NullBitmapValue []byte

stmtExecute := ComStmtExecute{}
stmtExecute.StatementID = binary.LittleEndian.Uint32(packet[1:5])
Expand All @@ -35,7 +38,8 @@ func decodeComStmtExecute(packet []byte) (ComStmtExecute, error) {
if len(packet) > 14 {
nullBitmapLength := int((stmtExecute.ParamCount + 7) / 8)

stmtExecute.NullBitmap = packet[10 : 10+nullBitmapLength]
NullBitmapValue = packet[10 : 10+nullBitmapLength]
stmtExecute.NullBitmap = base64.StdEncoding.EncodeToString(NullBitmapValue)
stmtExecute.ParamCount = binary.LittleEndian.Uint16(packet[10+nullBitmapLength:])

// in case new parameters are bound, the new types and values are sent
Expand Down
16 changes: 9 additions & 7 deletions pkg/proxy/integrations/mysqlparser/handshakeResponseOkPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package mysqlparser

import (
"bytes"
"encoding/base64"
"fmt"

"go.keploy.io/server/pkg/models"
)

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

func decodeHandshakeResponseOk(data []byte) (*HandshakeResponseOk, error) {
Expand Down Expand Up @@ -65,15 +66,16 @@ func decodeHandshakeResponseOk(data []byte) (*HandshakeResponseOk, error) {
Type: authType,
Message: message,
},
RemainingBytes: remainingBytes,
RemainingBytes: base64.StdEncoding.EncodeToString(remainingBytes),
}, nil
}

func encodeHandshakeResponseOk(packet *models.MySQLHandshakeResponseOk) ([]byte, error) {
var buf bytes.Buffer
var payload []byte
RemainingBytesValue, _ := base64.StdEncoding.DecodeString(packet.RemainingBytes)
if packet.PluginDetails.Type == "PublicKeyAuthentication" {
publicKeydata := []byte(packet.RemainingBytes)
publicKeydata := []byte(RemainingBytesValue)

// Calculate the payload length
payloadLength := len(publicKeydata) + 1 // +1 for the MySQL protocol version byte
Expand Down Expand Up @@ -123,8 +125,8 @@ func encodeHandshakeResponseOk(packet *models.MySQLHandshakeResponseOk) ([]byte,
}

// Write remaining bytes if available
if len(packet.RemainingBytes) > 0 {
buf.Write(packet.RemainingBytes)
if len(RemainingBytesValue) > 0 {
buf.Write(RemainingBytesValue)
}

// Create header
Expand Down
Loading
Loading