Skip to content

Commit cd823ae

Browse files
committed
Issue #42: Remove encryption framework
1 parent 9c86937 commit cd823ae

File tree

5 files changed

+7
-94
lines changed

5 files changed

+7
-94
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Obtaining an answer of
6969
- [**CORS**](https://germ.gitbook.io/ws4sqlite/documentation/configuration-file#corsorigin) mode, configurable per-db;
7070
- [**Scheduled tasks**](https://germ.gitbook.io/ws4sqlite/documentation/sched_tasks), cron-like and/or at startup, also configurable per-db;
7171
- Scheduled tasks can be: backup (with rotation), vacuum and/or a set of SQL statements;
72-
- Builtin [**encryption**](https://germ.gitbook.io/ws4sqlite/documentation/encryption) of fields, given a symmetric key;
7372
- Provide [**initialization statements**](https://germ.gitbook.io/ws4sqlite/documentation/configuration-file#initstatements) to execute when a DB is created;
7473
- [**WAL**](https://sqlite.org/wal.html) mode enabled by default, can be [disabled](https://germ.gitbook.io/ws4sqlite/documentation/configuration-file#disablewalmode);
7574
- [**Quite fast**](features/performances.md)!

src/go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.20
44

55
require (
66
github.com/gofiber/fiber/v2 v2.52.0
7+
github.com/iancoleman/orderedmap v0.3.0
78
github.com/lnquy/cron v1.1.1
89
github.com/mitchellh/go-homedir v1.1.0
9-
github.com/proofrock/crypgo v1.2.1
1010
github.com/proofrock/go-mylittlelogger v0.4.0
1111
github.com/robfig/cron/v3 v3.0.1
1212
github.com/wI2L/jettison v0.7.4
@@ -18,7 +18,6 @@ require (
1818
github.com/andybalholm/brotli v1.1.0 // indirect
1919
github.com/dustin/go-humanize v1.0.1 // indirect
2020
github.com/google/uuid v1.6.0 // indirect
21-
github.com/iancoleman/orderedmap v0.3.0 // indirect
2221
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
2322
github.com/klauspost/compress v1.17.6 // indirect
2423
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -30,7 +29,6 @@ require (
3029
github.com/valyala/bytebufferpool v1.0.0 // indirect
3130
github.com/valyala/fasthttp v1.51.0 // indirect
3231
github.com/valyala/tcplisten v1.0.0 // indirect
33-
golang.org/x/crypto v0.19.0 // indirect
3432
golang.org/x/mod v0.15.0 // indirect
3533
golang.org/x/sys v0.17.0 // indirect
3634
golang.org/x/tools v0.17.0 // indirect

src/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
3737
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
3838
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
3939
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
40-
github.com/proofrock/crypgo v1.2.1 h1:5d/JxYO8VGzgpQea2b87nsFDwN1uOdAvLRX1ECQPcDI=
41-
github.com/proofrock/crypgo v1.2.1/go.mod h1:FyJn1X+WEggBEC5IQQkWs3dExYoCFlJESEOjYFRVXhQ=
4240
github.com/proofrock/go-mylittlelogger v0.4.0 h1:nroZv7+Y9iQQn+wfh00GVqxiaXXCZR9xH2ErInIfAMM=
4341
github.com/proofrock/go-mylittlelogger v0.4.0/go.mod h1:XYdRJNt34V6ze+LNzFAGjWB27M1dfsYPoMcgCPBwugg=
4442
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
@@ -58,8 +56,6 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
5856
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
5957
github.com/wI2L/jettison v0.7.4 h1:ptjriu75R/k5RAZO0DJzy2t55f7g+dPiBxBY38icaKg=
6058
github.com/wI2L/jettison v0.7.4/go.mod h1:O+F+T7X7ZN6kTsd167Qk4aZMC8jNrH48SMedNmkfPb0=
61-
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
62-
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
6359
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
6460
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
6561
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=

src/structs.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"database/sql"
2121
"encoding/json"
2222
"fmt"
23-
"github.com/iancoleman/orderedmap"
2423
"sync"
24+
25+
"github.com/iancoleman/orderedmap"
2526
)
2627

2728
// This is the ws4sqlite error type
@@ -106,20 +107,12 @@ type credentials struct {
106107
Password string `json:"password"`
107108
}
108109

109-
type requestItemCrypto struct {
110-
Password string `json:"password"`
111-
Fields []string `json:"fields"`
112-
CompressionLevel int `json:"compressionLevel"`
113-
}
114-
115110
type requestItem struct {
116111
Query string `json:"query"`
117112
Statement string `json:"statement"`
118113
NoFail bool `json:"noFail"`
119114
Values map[string]json.RawMessage `json:"values"`
120115
ValuesBatch []map[string]json.RawMessage `json:"valuesBatch"`
121-
Encoder *requestItemCrypto `json:"encoder"`
122-
Decoder *requestItemCrypto `json:"decoder"`
123116
}
124117

125118
type request struct {

src/web_service.go

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"context"
2121
"database/sql"
2222
"errors"
23-
"github.com/iancoleman/orderedmap"
2423
"strings"
2524
"time"
2625

26+
"github.com/iancoleman/orderedmap"
27+
2728
"github.com/gofiber/fiber/v2"
28-
"github.com/proofrock/crypgo"
2929
)
3030

3131
// 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 {
5050
return c.Status(ret.Code).JSON(ret)
5151
}
5252

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-
9753
// For a single query item, deals with a failure, determining if it must invalidate all of the transaction
9854
// or just report an error in the single query. In the former case, fails fast (panics), else it appends
9955
// 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
10763
// Processes a query, and returns a suitable responseItem
10864
//
10965
// 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) {
11167
resultSet := make([]orderedmap.OrderedMap, 0)
11268

11369
rows, err := tx.Query(query, vals2nameds(values)...)
@@ -132,11 +88,6 @@ func processWithResultSet(tx *sql.Tx, query string, decoder *requestItemCrypto,
13288
toAdd.Set(fields[i], values[i])
13389
}
13490

135-
if decoder != nil {
136-
if err := decrypt(*decoder, toAdd); err != nil {
137-
return nil, err
138-
}
139-
}
14091
resultSet = append(resultSet, *toAdd)
14192
}
14293

@@ -264,16 +215,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
264215

265216
hasResultSet := txItem.Query != ""
266217

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-
277218
if len(txItem.Values) != 0 && len(txItem.ValuesBatch) != 0 {
278219
reportError(errors.New("cannot specify both values and valuesBatch"), fiber.StatusBadRequest, i, txItem.NoFail, ret.Results)
279220
continue
@@ -323,13 +264,6 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
323264
continue
324265
}
325266

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-
333267
valuesBatch = append(valuesBatch, values)
334268
}
335269

@@ -348,17 +282,10 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
348282
continue
349283
}
350284

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-
358285
if hasResultSet {
359286
// Query
360287
// 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)
362289
if err != nil {
363290
reportError(err, fiber.StatusInternalServerError, i, txItem.NoFail, ret.Results)
364291
continue

0 commit comments

Comments
 (0)