forked from keploy/keploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres_transcoder.go
376 lines (329 loc) · 10.1 KB
/
postgres_transcoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package postgresparser
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
"github.com/jackc/pgproto3/v2"
"go.keploy.io/server/pkg/models"
"go.uber.org/zap"
)
type BackendWrapper struct {
BackendWrapper models.Backend
}
type FrontendWrapper struct {
FrontendWrapper models.Frontend
}
func NewBackend() *BackendWrapper {
return &BackendWrapper{}
}
func NewFrontend() *FrontendWrapper {
return &FrontendWrapper{}
}
func checkScram(encoded []byte, log *zap.Logger) bool {
// encoded, err := PostgresDecoder(packet)
// check if payload contains SCRAM-SHA-256
messageType := encoded[0]
log.Debug("Message Type: %c\n", zap.String("messageType", string(messageType)))
if messageType == 'N' {
return false
}
// Print the message payload (for simplicity, the payload is printed as a string)
payload := string(encoded[5:])
if messageType == 'R' {
if strings.Contains(payload, "SCRAM-SHA") {
log.Debug("scram packet")
return true
}
}
return false
}
func isStartupPacket(packet []byte) bool {
protocolVersion := binary.BigEndian.Uint32(packet[4:8])
// printStartupPacketDetails(packet)
return protocolVersion == 196608 // 3.0 in PostgreSQL
}
func isRegularPacket(packet []byte) bool {
messageType := packet[0]
return messageType == 'Q' || messageType == 'P' || messageType == 'D' || messageType == 'C' || messageType == 'E'
}
func printStartupPacketDetails(packet []byte) {
// Print key-value pairs (for simplicity, only one key-value pair is shown)
keyStart := 8
for keyStart < len(packet) && packet[keyStart] != 0 {
keyEnd := keyStart
for keyEnd < len(packet) && packet[keyEnd] != 0 {
keyEnd++
}
key := string(packet[keyStart:keyEnd])
valueStart := keyEnd + 1
valueEnd := valueStart
for valueEnd < len(packet) && packet[valueEnd] != 0 {
valueEnd++
}
value := string(packet[valueStart:valueEnd])
fmt.Printf("Key: %s, Value: %s\n", key, value)
keyStart = valueEnd + 1
}
}
func printRegularPacketDetails(packet []byte) {
messageType := packet[0]
fmt.Printf("Message Type: %c\n", messageType)
// Print the message payload (for simplicity, the payload is printed as a string)
payload := string(packet[5:])
fmt.Printf("Payload: %s\n", payload)
}
const (
AuthTypeOk = 0
AuthTypeCleartextPassword = 3
AuthTypeMD5Password = 5
AuthTypeSCMCreds = 6
AuthTypeGSS = 7
AuthTypeGSSCont = 8
AuthTypeSSPI = 9
AuthTypeSASL = 10
AuthTypeSASLContinue = 11
AuthTypeSASLFinal = 12
)
const ProtocolVersionNumber uint32 = 196608 // Replace with actual version number if different
// PG Response Packet Transcoder
func (b *BackendWrapper) TranslateToReadableBackend(msgBody []byte) (pgproto3.FrontendMessage, error) {
// fmt.Println("msgType", b.BackendWrapper.MsgType)
var msg pgproto3.FrontendMessage
switch b.BackendWrapper.MsgType {
case 'B':
msg = &b.BackendWrapper.Bind
case 'C':
msg = &b.BackendWrapper.Close
case 'D':
msg = &b.BackendWrapper.Describe
case 'E':
msg = &b.BackendWrapper.Execute
case 'F':
msg = &b.BackendWrapper.FunctionCall
case 'f':
msg = &b.BackendWrapper.CopyFail
case 'd':
msg = &b.BackendWrapper.CopyData
case 'c':
msg = &b.BackendWrapper.CopyDone
case 'H':
msg = &b.BackendWrapper.Flush
case 'P':
msg = &b.BackendWrapper.Parse
case 'p':
switch b.BackendWrapper.AuthType {
case pgproto3.AuthTypeSASL:
msg = &pgproto3.SASLInitialResponse{}
case pgproto3.AuthTypeSASLContinue:
msg = &pgproto3.SASLResponse{}
case pgproto3.AuthTypeSASLFinal:
msg = &pgproto3.SASLResponse{}
case pgproto3.AuthTypeGSS, pgproto3.AuthTypeGSSCont:
msg = &pgproto3.GSSResponse{}
case pgproto3.AuthTypeCleartextPassword, pgproto3.AuthTypeMD5Password:
fallthrough
default:
// to maintain backwards compatability
msg = &pgproto3.PasswordMessage{}
}
case 'Q':
msg = &b.BackendWrapper.Query
case 'S':
msg = &b.BackendWrapper.Sync
case 'X':
msg = &b.BackendWrapper.Terminate
default:
return nil, fmt.Errorf("unknown message type: %c", b.BackendWrapper.MsgType)
}
// fmt.Println("msg--", msgBody)
err := msg.Decode(msgBody[5:])
if b.BackendWrapper.MsgType == 'P' {
*msg.(*pgproto3.Parse) = b.BackendWrapper.Parse
}
// bits := msg.Encode([]byte{})
// // println("Length of bits", len(bits), "Length of msgBody", len(msgBody))
// if len(bits) != len(msgBody) {
// fmt.Println("Encoded Data doesn't match the original data ..")
// }
return msg, err
}
func (f *FrontendWrapper) TranslateToReadableResponse(msgBody []byte, logger *zap.Logger) (pgproto3.BackendMessage, error) {
f.FrontendWrapper.BodyLen = int(binary.BigEndian.Uint32(msgBody[1:])) - 4
// println("bodylen", f.FrontendWrapper.BodyLen, "of msgtype", msgBody[0])
f.FrontendWrapper.MsgType = msgBody[0]
var msg pgproto3.BackendMessage
switch f.FrontendWrapper.MsgType {
case '1':
msg = &f.FrontendWrapper.ParseComplete
case '2':
msg = &f.FrontendWrapper.BindComplete
case '3':
msg = &f.FrontendWrapper.CloseComplete
case 'A':
msg = &f.FrontendWrapper.NotificationResponse
case 'c':
msg = &f.FrontendWrapper.CopyDone
case 'C':
msg = &f.FrontendWrapper.CommandComplete
case 'd':
msg = &f.FrontendWrapper.CopyData
case 'D':
msg = &f.FrontendWrapper.DataRow
logger.Debug("Data Row", zap.String("data", string(msgBody)))
case 'E':
msg = &f.FrontendWrapper.ErrorResponse
case 'G':
msg = &f.FrontendWrapper.CopyInResponse
case 'H':
msg = &f.FrontendWrapper.CopyOutResponse
case 'I':
msg = &f.FrontendWrapper.EmptyQueryResponse
case 'K':
msg = &f.FrontendWrapper.BackendKeyData
case 'n':
msg = &f.FrontendWrapper.NoData
case 'N':
msg = &f.FrontendWrapper.NoticeResponse
case 'R':
var err error
msg, err = f.findAuthenticationMessageType(msgBody)
if err != nil {
return nil, err
}
case 's':
msg = &f.FrontendWrapper.PortalSuspended
case 'S':
msg = &f.FrontendWrapper.ParameterStatus
case 't':
msg = &f.FrontendWrapper.ParameterDescription
case 'T':
msg = &f.FrontendWrapper.RowDescription
case 'V':
msg = &f.FrontendWrapper.FunctionCallResponse
case 'W':
msg = &f.FrontendWrapper.CopyBothResponse
case 'Z':
msg = &f.FrontendWrapper.ReadyForQuery
default:
return nil, fmt.Errorf("unknown message type: %c", f.FrontendWrapper.MsgType)
}
logger.Debug("msgFrontend", zap.String("msgFrontend", string(msgBody)))
err := msg.Decode(msgBody[5:])
if err != nil {
logger.Error("Error from decoding request message ..", zap.Error(err))
}
bits := msg.Encode([]byte{})
// println("Length of bits", len(bits), "Length of msgBody", len(msgBody))
if len(bits) != len(msgBody) {
logger.Debug("Encoded Data doesn't match the original data ..")
}
return msg, err
}
func (f *FrontendWrapper) findAuthenticationMessageType(src []byte) (pgproto3.BackendMessage, error) {
if len(src) < 4 {
return nil, errors.New("authentication message too short")
}
authType, err := parseAuthType(src)
if err != nil {
return nil, err
}
f.FrontendWrapper.AuthType = authType
switch f.FrontendWrapper.AuthType {
case pgproto3.AuthTypeOk:
return &f.FrontendWrapper.AuthenticationOk, nil
case pgproto3.AuthTypeCleartextPassword:
return &f.FrontendWrapper.AuthenticationCleartextPassword, nil
case pgproto3.AuthTypeMD5Password:
return &f.FrontendWrapper.AuthenticationMD5Password, nil
case pgproto3.AuthTypeSCMCreds:
return nil, errors.New("AuthTypeSCMCreds is unimplemented")
case pgproto3.AuthTypeGSS:
return &f.FrontendWrapper.AuthenticationGSS, nil
case pgproto3.AuthTypeGSSCont:
return &f.FrontendWrapper.AuthenticationGSSContinue, nil
case pgproto3.AuthTypeSSPI:
return nil, errors.New("AuthTypeSSPI is unimplemented")
case pgproto3.AuthTypeSASL:
return &f.FrontendWrapper.AuthenticationSASL, nil
case pgproto3.AuthTypeSASLContinue:
return &f.FrontendWrapper.AuthenticationSASLContinue, nil
case pgproto3.AuthTypeSASLFinal:
return &f.FrontendWrapper.AuthenticationSASLFinal, nil
default:
return nil, fmt.Errorf("unknown authentication type: %d", f.FrontendWrapper.AuthType)
}
}
// GetAuthType returns the authType used in the current state of the frontend.
// See SetAuthType for more information.
func parseAuthType(buffer []byte) (int32, error) {
// Create a bytes reader from the buffer
reader := bytes.NewReader(buffer)
// Skip the message type (1 byte) as you know it's 'R'
reader.Seek(1, 0)
// Read the length of the message (4 bytes)
var length int32
err := binary.Read(reader, binary.BigEndian, &length)
if err != nil {
return 0, err
}
// Read the auth type code (4 bytes)
var authType int32
err = binary.Read(reader, binary.BigEndian, &authType)
if err != nil {
return 0, err
}
return authType, nil
}
const (
minStartupPacketLen = 4 // minStartupPacketLen is a single 32-bit int version or code.
maxStartupPacketLen = 10000 // maxStartupPacketLen is MAX_STARTUP_PACKET_LENGTH from PG source.
sslRequestNumber = 80877103
cancelRequestCode = 80877102
gssEncReqNumber = 80877104
)
func (b *BackendWrapper) DecodeStartupMessage(buf []byte) (pgproto3.FrontendMessage, error) {
reader := pgproto3.NewByteReader(buf)
buf, err := reader.Next(4)
if err != nil {
return nil, err
}
msgSize := int(binary.BigEndian.Uint32(buf) - 4)
if msgSize < minStartupPacketLen || msgSize > maxStartupPacketLen {
return nil, fmt.Errorf("invalid length of startup packet: %d", msgSize)
}
buf, err = reader.Next(msgSize)
if err != nil {
return nil, fmt.Errorf("invalid length of startup packet: %d", msgSize)
}
code := binary.BigEndian.Uint32(buf)
switch code {
case ProtocolVersionNumber:
err := b.BackendWrapper.StartupMessage.Decode(buf)
if err != nil {
return nil, err
}
return &b.BackendWrapper.StartupMessage, nil
case sslRequestNumber:
err := b.BackendWrapper.SSlRequest.Decode(buf)
if err != nil {
return nil, err
}
return &b.BackendWrapper.SSlRequest, nil
case cancelRequestCode:
err := b.BackendWrapper.CancelRequest.Decode(buf)
if err != nil {
return nil, err
}
return &b.BackendWrapper.CancelRequest, nil
case gssEncReqNumber:
err := b.BackendWrapper.GssEncRequest.Decode(buf)
if err != nil {
return nil, err
}
return &b.BackendWrapper.GssEncRequest, nil
default:
return nil, fmt.Errorf("unknown startup message code: %d", code)
}
}