@@ -20,12 +20,12 @@ import (
20
20
"context"
21
21
"database/sql"
22
22
"errors"
23
- "github.com/iancoleman/orderedmap"
24
23
"strings"
25
24
"time"
26
25
26
+ "github.com/iancoleman/orderedmap"
27
+
27
28
"github.com/gofiber/fiber/v2"
28
- "github.com/proofrock/crypgo"
29
29
)
30
30
31
31
// Catches the panics and converts the argument in a struct that Fiber uses to
@@ -50,50 +50,6 @@ func errHandler(c *fiber.Ctx, err error) error {
50
50
return c .Status (ret .Code ).JSON (ret )
51
51
}
52
52
53
- // Scans the values for a db request and encrypts them as needed
54
- func encrypt (encoder requestItemCrypto , values map [string ]interface {}) error {
55
- for i := range encoder .Fields {
56
- sval , ok := values [encoder.Fields [i ]].(string )
57
- if ! ok {
58
- return errors .New ("attempting to encrypt a non-string field" )
59
- }
60
- var eval string
61
- var err error
62
- if encoder .CompressionLevel < 1 {
63
- eval , err = crypgo .Encrypt (encoder .Password , sval )
64
- } else if encoder .CompressionLevel < 20 {
65
- eval , err = crypgo .CompressAndEncrypt (encoder .Password , sval , encoder .CompressionLevel )
66
- } else {
67
- return errors .New ("compression level is in the range 0-19" )
68
- }
69
- if err != nil {
70
- return err
71
- }
72
- values [encoder.Fields [i ]] = eval
73
- }
74
- return nil
75
- }
76
-
77
- // Scans the results from a db request and decrypts them as needed
78
- func decrypt (decoder requestItemCrypto , results * orderedmap.OrderedMap ) error {
79
- if decoder .CompressionLevel > 0 {
80
- return errors .New ("cannot specify compression level for decryption" )
81
- }
82
- for i := range decoder .Fields {
83
- // sval, ok := results[decoder.Fields[i]].(string)
84
- sval , ok := results .Get (decoder .Fields [i ])
85
- if ! ok {
86
- return errors .New ("attempting to decrypt a non-string field" )
87
- }
88
- dval , err := crypgo .Decrypt (decoder .Password , sval .(string ))
89
- if err != nil {
90
- return err
91
- }
92
- results .Set (decoder .Fields [i ], dval )
93
- }
94
- return nil
95
- }
96
-
97
53
// For a single query item, deals with a failure, determining if it must invalidate all of the transaction
98
54
// or just report an error in the single query. In the former case, fails fast (panics), else it appends
99
55
// the error to the response items, so the caller needs to return7continue
@@ -107,7 +63,7 @@ func reportError(err error, code int, reqIdx int, noFail bool, results []respons
107
63
// Processes a query, and returns a suitable responseItem
108
64
//
109
65
// This method is needed to execute properly the defers.
110
- func processWithResultSet (tx * sql.Tx , query string , decoder * requestItemCrypto , values map [string ]interface {}) (* responseItem , error ) {
66
+ func processWithResultSet (tx * sql.Tx , query string , values map [string ]interface {}) (* responseItem , error ) {
111
67
resultSet := make ([]orderedmap.OrderedMap , 0 )
112
68
113
69
rows , err := tx .Query (query , vals2nameds (values )... )
@@ -132,11 +88,6 @@ func processWithResultSet(tx *sql.Tx, query string, decoder *requestItemCrypto,
132
88
toAdd .Set (fields [i ], values [i ])
133
89
}
134
90
135
- if decoder != nil {
136
- if err := decrypt (* decoder , toAdd ); err != nil {
137
- return nil , err
138
- }
139
- }
140
91
resultSet = append (resultSet , * toAdd )
141
92
}
142
93
@@ -264,16 +215,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
264
215
265
216
hasResultSet := txItem .Query != ""
266
217
267
- if hasResultSet && txItem .Encoder != nil {
268
- reportError (errors .New ("cannot specify an encoder for a query" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
269
- continue
270
- }
271
-
272
- if ! hasResultSet && txItem .Decoder != nil {
273
- reportError (errors .New ("cannot specify a decoder for a statement" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
274
- continue
275
- }
276
-
277
218
if len (txItem .Values ) != 0 && len (txItem .ValuesBatch ) != 0 {
278
219
reportError (errors .New ("cannot specify both values and valuesBatch" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
279
220
continue
@@ -323,13 +264,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
323
264
continue
324
265
}
325
266
326
- if txItem .Encoder != nil {
327
- if err := encrypt (* txItem .Encoder , values ); err != nil {
328
- reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
329
- continue
330
- }
331
- }
332
-
333
267
valuesBatch = append (valuesBatch , values )
334
268
}
335
269
@@ -348,17 +282,10 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
348
282
continue
349
283
}
350
284
351
- if txItem .Encoder != nil {
352
- if err := encrypt (* txItem .Encoder , values ); err != nil {
353
- reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
354
- continue
355
- }
356
- }
357
-
358
285
if hasResultSet {
359
286
// Query
360
287
// Externalized in a func so that defer rows.Close() actually runs
361
- retWR , err := processWithResultSet (tx , sqll , txItem . Decoder , values )
288
+ retWR , err := processWithResultSet (tx , sqll , values )
362
289
if err != nil {
363
290
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
364
291
continue
0 commit comments