Skip to content

Commit

Permalink
Merge pull request #1 from cipherstash/fix/json-payload
Browse files Browse the repository at this point in the history
Update so Q is sent on insert as nil
  • Loading branch information
fimac authored Oct 23, 2024
2 parents 98d2509 + fb831ae commit 99ec08f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions goeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type EncryptedColumn struct {
P string `json:"p"`
I TableColumn `json:"i"`
V int `json:"v"`
Q string `json:"q"`
Q any `json:"q"`
}

// EncryptedText is a string value to be encrypted
Expand All @@ -46,7 +46,7 @@ type EncryptedBool bool

// Serialize turns a EncryptedText value into a jsonb payload for CipherStash Proxy
func (et EncryptedText) Serialize(table string, column string) ([]byte, error) {
val, err := ToEncryptedColumn(string(et), table, column)
val, err := ToEncryptedColumn(string(et), table, column, nil)
if err != nil {
return nil, fmt.Errorf("error serializing: %v", err)
}
Expand All @@ -69,7 +69,7 @@ func (et *EncryptedText) Deserialize(data []byte) (EncryptedText, error) {

// Serialize turns a EncryptedJsonb value into a jsonb payload for CipherStash Proxy
func (ej EncryptedJsonb) Serialize(table string, column string) ([]byte, error) {
val, err := ToEncryptedColumn(map[string]any(ej), table, column)
val, err := ToEncryptedColumn(map[string]any(ej), table, column, nil)
if err != nil {
return nil, fmt.Errorf("error serializing: %v", err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func (ej *EncryptedJsonb) Deserialize(data []byte) (EncryptedJsonb, error) {

// Serialize turns a EncryptedInt value into a jsonb payload for CipherStash Proxy
func (et EncryptedInt) Serialize(table string, column string) ([]byte, error) {
val, err := ToEncryptedColumn(int(et), table, column)
val, err := ToEncryptedColumn(int(et), table, column, nil)
if err != nil {
return nil, fmt.Errorf("error serializing: %v", err)
}
Expand All @@ -124,7 +124,7 @@ func (et *EncryptedInt) Deserialize(data []byte) (EncryptedInt, error) {

// Serialize turns a EncryptedBool value into a jsonb payload for CipherStash Proxy
func (eb EncryptedBool) Serialize(table string, column string) ([]byte, error) {
val, err := ToEncryptedColumn(bool(eb), table, column)
val, err := ToEncryptedColumn(bool(eb), table, column, nil)
if err != nil {
return nil, fmt.Errorf("error serializing: %v", err)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func JsonbQuery(value any, table string, column string) ([]byte, error) {
}

// serializeQuery produces a jsonb payload used by EQL query functions to perform search operations like equality checks, range queries, and unique constraints.
func serializeQuery(value any, table string, column string, queryType string) ([]byte, error) {
func serializeQuery(value any, table string, column string, queryType any) ([]byte, error) {
query, err := ToEncryptedColumn(value, table, column, queryType)
if err != nil {
return nil, fmt.Errorf("error converting to EncryptedColumn: %v", err)
Expand All @@ -186,16 +186,26 @@ func serializeQuery(value any, table string, column string, queryType string) ([
}

// ToEncryptedColumn converts a plaintext value to a string, and returns the EncryptedColumn struct for inserting into a database.
func ToEncryptedColumn(value any, table string, column string, queryType ...string) (EncryptedColumn, error) {
str, err := convertToString(value)
if err != nil {
return EncryptedColumn{}, fmt.Errorf("error: %v", err)
}
data := EncryptedColumn{K: "pt", P: str, I: TableColumn{T: table, C: column}, V: 1}
if queryType != nil {
data.Q = queryType[0]
func ToEncryptedColumn(value any, table string, column string, queryType any) (EncryptedColumn, error) {
if queryType == nil {
str, err := convertToString(value)
if err != nil {
return EncryptedColumn{}, fmt.Errorf("error: %v", err)
}

data := EncryptedColumn{K: "pt", P: str, I: TableColumn{T: table, C: column}, V: 1, Q: nil}

return data, nil
} else {

Check failure on line 199 in goeql.go

View workflow job for this annotation

GitHub Actions / Run test suite

if block ends with a return statement, so drop this else and outdent its block
str, err := convertToString(value)
if err != nil {
return EncryptedColumn{}, fmt.Errorf("error: %v", err)
}

data := EncryptedColumn{K: "pt", P: str, I: TableColumn{T: table, C: column}, V: 1, Q: queryType}

return data, nil
}
return data, nil
}

func convertToString(value any) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion goeql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func TestToEncryptedColumn(t *testing.T) {
}

for _, tt := range tests {
ec, err := ToEncryptedColumn(tt.value, tt.table, tt.column)
ec, err := ToEncryptedColumn(tt.value, tt.table, tt.column, nil)
if err != nil {
t.Fatalf("ToEncryptedColumn returned error: %v", err)
}
Expand Down

0 comments on commit 99ec08f

Please sign in to comment.