From a12b30948029090cca83d57cb2a4258df42f3c01 Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko <108219165+mhrynenko@users.noreply.github.com> Date: Fri, 27 Dec 2024 12:58:53 +0200 Subject: [PATCH] update: service value components to use hex encoding instead base64 (#4) --- docs/spec/components/parameters/DataValueParam.yaml | 4 ++-- docs/spec/components/schemas/AddValue.yaml | 4 ++-- docs/spec/components/schemas/Value.yaml | 4 ++-- docs/spec/paths/integrations@zk-biometrics-svc@value.yaml | 6 ++---- internal/data/kv.go | 2 +- internal/data/pg/kv.go | 8 ++++---- internal/service/handlers/add_data.go | 8 ++++---- internal/service/handlers/delete_data.go | 6 +++--- internal/service/handlers/get_data.go | 4 ++-- internal/service/requests/add_data.go | 2 +- internal/service/requests/delete_data.go | 2 +- internal/service/requests/get_data.go | 2 +- resources/model_add_value_attributes.go | 2 +- resources/model_value_attributes.go | 2 +- 14 files changed, 27 insertions(+), 29 deletions(-) diff --git a/docs/spec/components/parameters/DataValueParam.yaml b/docs/spec/components/parameters/DataValueParam.yaml index 14fc24f..4e1fcc9 100644 --- a/docs/spec/components/parameters/DataValueParam.yaml +++ b/docs/spec/components/parameters/DataValueParam.yaml @@ -2,7 +2,7 @@ in: query name: filter[value] required: false description: | - Param to filter data by Base64 encoded value -example: aGVsbG8gd29ybGQ= + Param to filter data by Hex encoded value without 0x prefix +example: 68656c6c6f20776f726c64 schema: type: string \ No newline at end of file diff --git a/docs/spec/components/schemas/AddValue.yaml b/docs/spec/components/schemas/AddValue.yaml index 0521e91..b9f2fb6 100644 --- a/docs/spec/components/schemas/AddValue.yaml +++ b/docs/spec/components/schemas/AddValue.yaml @@ -11,5 +11,5 @@ allOf: properties: value: type: string - description: Base64 encoded data value - example: aGVsbG8gd29ybGQ= \ No newline at end of file + description: Hex encoded data value without 0x prefix + example: 68656c6c6f20776f726c64 \ No newline at end of file diff --git a/docs/spec/components/schemas/Value.yaml b/docs/spec/components/schemas/Value.yaml index 5d53bd5..c224260 100644 --- a/docs/spec/components/schemas/Value.yaml +++ b/docs/spec/components/schemas/Value.yaml @@ -15,5 +15,5 @@ allOf: example: 550e8400-e29b-41d4-a716-446655440000 value: type: string - description: Base64 encoded data value - example: aGVsbG8gd29ybGQ= \ No newline at end of file + description: Hex encoded data value without 0x prefix + example: 68656c6c6f20776f726c64 \ No newline at end of file diff --git a/docs/spec/paths/integrations@zk-biometrics-svc@value.yaml b/docs/spec/paths/integrations@zk-biometrics-svc@value.yaml index 76deb06..cd46ddd 100644 --- a/docs/spec/paths/integrations@zk-biometrics-svc@value.yaml +++ b/docs/spec/paths/integrations@zk-biometrics-svc@value.yaml @@ -40,8 +40,7 @@ delete: summary: Delete value description: | Delete value from storage. If no filter query specified nothing is delete, because one of filters is required. - When filtering by value, service queries by exact match and if any value found deletes it. - Also take into account that value is Base64 encoded, so may contain some escape symbol that should be encoded. + When filtering by value, service queries by exact match and if any value found deletes it. operationId: GetData parameters: - $ref: '#/components/parameters/DataKeyParam' @@ -71,8 +70,7 @@ get: description: | Get value from storage. If no filter query specified any row is returned. When filtering by value, service queries not by exact match, but searching for the nearest by - hammming distance element. Also take into account that value is Base64 encoded, so may contain - some escape symbol that should be encoded. + hammming distance element. operationId: GetData parameters: - $ref: '#/components/parameters/DataKeyParam' diff --git a/internal/data/kv.go b/internal/data/kv.go index 15477d6..710a4d3 100644 --- a/internal/data/kv.go +++ b/internal/data/kv.go @@ -18,7 +18,7 @@ type KVQ interface { FilterByKey(key string) KVQ FilterByValue(value []byte) KVQ - FilterByBase64ValueLength(value string) KVQ + FilterByHexValueLength(value string) KVQ OrderBy(expr sq.Sqlizer, order OrderType) KVQ } diff --git a/internal/data/pg/kv.go b/internal/data/pg/kv.go index ef9b959..f5b36ac 100644 --- a/internal/data/pg/kv.go +++ b/internal/data/pg/kv.go @@ -59,8 +59,8 @@ func (q kvQ) FilterByValue(value []byte) data.KVQ { return q.withFilters(sq.Eq{"value": value}) } -func (q kvQ) FilterByBase64ValueLength(value string) data.KVQ { - return q.withFilters(sq.Expr("LENGTH(value) = LENGTH(DECODE(?, 'base64'))", value)) +func (q kvQ) FilterByHexValueLength(value string) data.KVQ { + return q.withFilters(sq.Expr("LENGTH(value) = LENGTH(DECODE(?, 'hex'))", value)) } func (q kvQ) OrderBy(expr sq.Sqlizer, order data.OrderType) data.KVQ { @@ -77,6 +77,6 @@ func (q kvQ) withFilters(stmt interface{}) data.KVQ { return q } -func HammingDistanceBase64(value string) sq.Sqlizer { - return sq.Expr("hamming_distance(value, DECODE(?, 'base64'))", value) +func HammingDistanceHex(value string) sq.Sqlizer { + return sq.Expr("hamming_distance(value, DECODE(?, 'hex'))", value) } diff --git a/internal/service/handlers/add_data.go b/internal/service/handlers/add_data.go index 8af7642..a3b93f2 100644 --- a/internal/service/handlers/add_data.go +++ b/internal/service/handlers/add_data.go @@ -1,7 +1,7 @@ package handlers import ( - "encoding/base64" + "encoding/hex" "net/http" "github.com/google/uuid" @@ -20,9 +20,9 @@ func AddData(w http.ResponseWriter, r *http.Request) { return } - value, err := base64.StdEncoding.DecodeString(req.Data.Attributes.Value) + value, err := hex.DecodeString(req.Data.Attributes.Value) if err != nil { - Log(r).WithError(err).WithField("value", req.Data.Attributes.Value).Error("failed to decode base64 string") + Log(r).WithError(err).WithField("value", req.Data.Attributes.Value).Error("failed to decode hex string") ape.RenderErr(w, problems.InternalError()) return } @@ -50,7 +50,7 @@ func newKVResponse(kv data.KV) resources.ValueResponse { Type: resources.VALUE, }, Attributes: resources.ValueAttributes{ - Value: base64.StdEncoding.EncodeToString(kv.Value), + Value: hex.EncodeToString(kv.Value), Key: kv.Key, }, }, diff --git a/internal/service/handlers/delete_data.go b/internal/service/handlers/delete_data.go index 3fecf34..6aeddf4 100644 --- a/internal/service/handlers/delete_data.go +++ b/internal/service/handlers/delete_data.go @@ -1,7 +1,7 @@ package handlers import ( - "encoding/base64" + "encoding/hex" "net/http" "github.com/rarimo/zk-biometrics-svc/internal/service/requests" @@ -24,9 +24,9 @@ func DeleteData(w http.ResponseWriter, r *http.Request) { } if req.Value != nil { - value, err := base64.StdEncoding.DecodeString(*req.Value) + value, err := hex.DecodeString(*req.Value) if err != nil { - Log(r).WithError(err).WithField("value", *req.Value).Error("failed to decode Base64 string") + Log(r).WithError(err).WithField("value", *req.Value).Error("failed to decode hex string") ape.RenderErr(w, problems.InternalError()) return } diff --git a/internal/service/handlers/get_data.go b/internal/service/handlers/get_data.go index 4b3a418..bf86962 100644 --- a/internal/service/handlers/get_data.go +++ b/internal/service/handlers/get_data.go @@ -26,8 +26,8 @@ func GetData(w http.ResponseWriter, r *http.Request) { if req.Value != nil { kvQuery = kvQuery. - FilterByBase64ValueLength(*req.Value). - OrderBy(pg.HammingDistanceBase64(*req.Value), data.OrderDesc) + FilterByHexValueLength(*req.Value). + OrderBy(pg.HammingDistanceHex(*req.Value), data.OrderDesc) } kv, err := kvQuery.Get() diff --git a/internal/service/requests/add_data.go b/internal/service/requests/add_data.go index a7eaa9f..140a171 100644 --- a/internal/service/requests/add_data.go +++ b/internal/service/requests/add_data.go @@ -16,6 +16,6 @@ func AddData(r *http.Request) (req resources.AddValueRequest, err error) { return req, val.Errors{ "data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.VALUE)), - "data/attributes/value": val.Validate(req.Data.Attributes.Value, val.Required, is.Base64), + "data/attributes/value": val.Validate(req.Data.Attributes.Value, val.Required, is.Hexadecimal), }.Filter() } diff --git a/internal/service/requests/delete_data.go b/internal/service/requests/delete_data.go index 5fddd9e..3b0abd2 100644 --- a/internal/service/requests/delete_data.go +++ b/internal/service/requests/delete_data.go @@ -27,7 +27,7 @@ func NewDeleteDataRequest(r *http.Request) (req GetDataRequest, err error) { val.When(val.IsEmpty(req.Value), validation.Required.Error(FilterQueryErrMsg)), ), "value": val.Validate(req.Value, - val.When(!val.IsEmpty(req.Value), is.Base64), + val.When(!val.IsEmpty(req.Value), is.Hexadecimal), val.When(val.IsEmpty(req.Key), validation.Required.Error(FilterQueryErrMsg)), ), }.Filter() diff --git a/internal/service/requests/get_data.go b/internal/service/requests/get_data.go index 441d215..648b49b 100644 --- a/internal/service/requests/get_data.go +++ b/internal/service/requests/get_data.go @@ -20,7 +20,7 @@ func NewGetDataRequest(r *http.Request) (req GetDataRequest, err error) { err = val.Errors{ "key": val.Validate(req.Key, val.When(!val.IsEmpty(req.Key), is.UUID)), - "value": val.Validate(req.Value, val.When(!val.IsEmpty(req.Value), is.Base64)), + "value": val.Validate(req.Value, val.When(!val.IsEmpty(req.Value), is.Hexadecimal)), }.Filter() return } diff --git a/resources/model_add_value_attributes.go b/resources/model_add_value_attributes.go index 853022f..226fcb9 100644 --- a/resources/model_add_value_attributes.go +++ b/resources/model_add_value_attributes.go @@ -5,6 +5,6 @@ package resources type AddValueAttributes struct { - // Base64 encoded data value + // Hex encoded data value without 0x prefix Value string `json:"value"` } diff --git a/resources/model_value_attributes.go b/resources/model_value_attributes.go index 014120e..10476a2 100644 --- a/resources/model_value_attributes.go +++ b/resources/model_value_attributes.go @@ -7,6 +7,6 @@ package resources type ValueAttributes struct { // Unique identifier for stored data Key string `json:"key"` - // Base64 encoded data value + // Hex encoded data value without 0x prefix Value string `json:"value"` }