Skip to content

Commit

Permalink
refactor: add some null check while parsing pg request (#1796)
Browse files Browse the repository at this point in the history
* refactor: add some null check while parsing pg request

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>

* chore: fix linting issue

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>

---------

Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>
  • Loading branch information
Sarthak160 committed Apr 9, 2024
1 parent 485a7e5 commit b659fc8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pkg/core/proxy/integrations/generic/match.go
Expand Up @@ -12,6 +12,12 @@ import (
"go.keploy.io/server/v2/pkg/models"
)

// fuzzyMatch performs a fuzzy matching algorithm to find the best matching mock for the given request.
// It takes a context, a request buffer, and a mock database as input parameters.
// The function iterates over the mocks in the database and applies the fuzzy matching algorithm to find the best match.
// If a match is found, it returns the corresponding response mock and a boolean value indicating success.
// If no match is found, it returns false and a nil response.
// If an error occurs during the matching process, it returns an error.
func fuzzyMatch(ctx context.Context, reqBuff [][]byte, mockDb integrations.MockMemDb) (bool, []models.GenericPayload, error) {
for {
select {
Expand Down
12 changes: 7 additions & 5 deletions pkg/core/proxy/integrations/postgres/v1/encode.go
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"context"
"encoding/binary"
"fmt"
"io"
"net"
"strconv"
Expand Down Expand Up @@ -169,13 +170,14 @@ func encodePostgres(ctx context.Context, logger *zap.Logger, reqBuf []byte, clie
if !isStartupPacket(buffer) && len(buffer) > 5 {
bufferCopy := buffer
for i := 0; i < len(bufferCopy)-5; {
logger.Debug("Inside the if condition")
pg.BackendWrapper.MsgType = buffer[i]
logger.Debug("Inside the Pg request for loop")
pg.BackendWrapper.BodyLen = int(binary.BigEndian.Uint32(buffer[i+1:])) - 4
if len(buffer) < (i + pg.BackendWrapper.BodyLen + 5) {
utils.LogError(logger, nil, "failed to translate the postgres request message due to shorter network packet buffer")
utils.LogError(logger, nil, "failed to translate the postgres request message due to shorter network packet buffer. Length of buffer is "+fmt.Sprint(len(buffer))+" buffer value :"+string(buffer)+" and pg.BackendWrapper.BodyLen is "+fmt.Sprint(pg.BackendWrapper.BodyLen))
break
}
pg.BackendWrapper.MsgType = buffer[i]

msg, err = pg.translateToReadableBackend(buffer[i:(i + pg.BackendWrapper.BodyLen + 5)])
if err != nil && buffer[i] != 112 {
utils.LogError(logger, err, "failed to translate the request message to readable")
Expand Down Expand Up @@ -240,7 +242,7 @@ func encodePostgres(ctx context.Context, logger *zap.Logger, reqBuf []byte, clie
logger.Debug("failed to decode the response message in proxy for postgres dependency", zap.Error(err))
}

if len(afterEncoded) != len(buffer) && pgMock.PacketTypes[0] != "p" {
if len(afterEncoded) != len(buffer) && len(pgMock.PacketTypes) > 0 && pgMock.PacketTypes[0] != "p" {
logger.Debug("the length of the encoded buffer is not equal to the length of the original buffer", zap.Any("after_encoded", len(afterEncoded)), zap.Any("buffer", len(buffer)))
pgMock.Payload = bufStr
}
Expand Down Expand Up @@ -367,7 +369,7 @@ func encodePostgres(ctx context.Context, logger *zap.Logger, reqBuf []byte, clie
if err != nil {
logger.Debug("failed to decode the response message in proxy for postgres dependency", zap.Error(err))
}
if len(afterEncoded) != len(buffer) && pgMock.PacketTypes[0] != "R" {
if len(afterEncoded) != len(buffer) && len(pgMock.PacketTypes) > 0 && pgMock.PacketTypes[0] != "R" {
logger.Debug("the length of the encoded buffer is not equal to the length of the original buffer", zap.Any("after_encoded", len(afterEncoded)), zap.Any("buffer", len(buffer)))
pgMock.Payload = bufStr
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/core/proxy/integrations/postgres/v1/util.go
Expand Up @@ -38,6 +38,10 @@ func postgresDecoderFrontend(response models.Frontend) ([]byte, error) {
case string('c'):
msg = &pgproto3.CopyDone{}
case string('C'):
if len(response.CommandCompletes) == 0 {
cc++
continue
}
msg = &pgproto3.CommandComplete{
CommandTag: response.CommandCompletes[cc].CommandTag,
CommandTagType: response.CommandCompletes[cc].CommandTagType,
Expand Down
8 changes: 4 additions & 4 deletions pkg/service/record/record.go
Expand Up @@ -15,7 +15,7 @@ import (
"golang.org/x/sync/errgroup"
)

type recorder struct {
type Recorder struct {
logger *zap.Logger
testDB TestDB
mockDB MockDB
Expand All @@ -25,7 +25,7 @@ type recorder struct {
}

func New(logger *zap.Logger, testDB TestDB, mockDB MockDB, telemetry Telemetry, instrumentation Instrumentation, config config.Config) Service {
return &recorder{
return &Recorder{
logger: logger,
testDB: testDB,
mockDB: mockDB,
Expand All @@ -35,7 +35,7 @@ func New(logger *zap.Logger, testDB TestDB, mockDB MockDB, telemetry Telemetry,
}
}

func (r *recorder) Start(ctx context.Context) error {
func (r *Recorder) Start(ctx context.Context) error {

// creating error group to manage proper shutdown of all the go routines and to propagate the error to the caller
errGrp, _ := errgroup.WithContext(ctx)
Expand Down Expand Up @@ -242,7 +242,7 @@ func (r *recorder) Start(ctx context.Context) error {
return fmt.Errorf(stopReason)
}

func (r *recorder) StartMock(ctx context.Context) error {
func (r *Recorder) StartMock(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
ctx = context.WithValue(ctx, models.ErrGroupKey, g)
var stopReason string
Expand Down

0 comments on commit b659fc8

Please sign in to comment.