From 352d5201e378c64304446e854e75e9ef64f1d092 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Apr 2023 18:48:29 -0700 Subject: [PATCH 1/7] add ID prefixing to endpoints. --- api/handler/card.go | 5 ++ api/handler/common.go | 97 +++++++++++++++++++++ api/handler/common_test.go | 38 +++++++++ api/handler/login.go | 22 ++++- api/handler/quotes.go | 4 + api/handler/transact.go | 7 +- api/handler/user.go | 40 ++++++++- go.mod | 2 +- go.sum | 4 + pkg/model/prefix_id.go | 167 +++++++++++++++++++++++++++++++++++++ 10 files changed, 380 insertions(+), 6 deletions(-) create mode 100644 api/handler/common_test.go create mode 100644 pkg/model/prefix_id.go diff --git a/api/handler/card.go b/api/handler/card.go index fcfaa12e..befc5448 100644 --- a/api/handler/card.go +++ b/api/handler/card.go @@ -36,6 +36,11 @@ func (card card) GetAll(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ UserId, PlatformId string }{userId, platformId}, &userId, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + res, err := card.Service.FetchSavedCards(ctx, userId, platformId) if err != nil { libcommon.LogStringError(c, err, "cards: get All") diff --git a/api/handler/common.go b/api/handler/common.go index 76e7ed83..bd2b4e7e 100644 --- a/api/handler/common.go +++ b/api/handler/common.go @@ -1,7 +1,9 @@ package handler import ( + "errors" "net/http" + "reflect" "regexp" "strings" "time" @@ -154,3 +156,98 @@ func DefaultErrorHandler(c echo.Context, err error, handlerName string) error { return httperror.InternalError(c) } + +var modelIdPrefixes = map[string]string{ + "User": "user", + "Platform": "platform", + "Network": "network", + "Asset": "asset", + "Device": "device", + "Contact": "contact", + "Location": "location", + "Instrument": "instrument", + "TxLeg": "txleg", + "Transaction": "tx", + "AuthStrategy": "auth", + "ApiKey": "apikey", + "Contract": "contract", +} + +// var relationalIdPrefixes = map[string][]string{ +// "UserToPlatform": {"user", "platform"}, +// "ContactToPlatform": {"contact", "platform"}, +// } + +var relationalIdPrefixes = map[string]string{ + "UserId": "user", + "PlatformId": "platform", + "ContactId": "contact", + "DeviceId": "device", + "InstrumentId": "instrument", + "NetworkId": "network", + "OriginTxLegId": "txleg", + "DestinationTxLegId": "txleg", + "ReceiptTxId": "txleg", + "ResponseTxId": "txleg", + "AssetId": "asset", +} + +func SanitizeIdInput(model interface{}) error { + // Model ID + stype := reflect.ValueOf(model).Elem() + field := stype.FieldByName("Id") + if !field.IsValid() { + // return errors.New("model does not contain an id") + // model may be relational, continue + } else { + prefix, ok := modelIdPrefixes[stype.Type().Name()] + if !ok { + return errors.New("model unknown") + } + if field.String()[:len(prefix)+1] != prefix+"_" { + return errors.New("input missing prefix " + prefix + "_") + } + field.SetString(field.String()[len(prefix)+1:]) + } + + // Relational IDs + for fieldName, prefix := range relationalIdPrefixes { + field := stype.FieldByName(fieldName) + if !field.IsValid() { + continue + } + if field.String()[:len(prefix)+1] != prefix+"_" { + return errors.New("input missing prefix " + prefix + "_") + } + field.SetString(field.String()[len(prefix)+1:]) + } + + return nil +} + +func SanitizeIdOutput(model interface{}) error { + // Model ID + stype := reflect.ValueOf(model).Elem() + field := stype.FieldByName("Id") + if !field.IsValid() { + // return errors.New("model does not contain an id") + // Model may be relational, continue + } else { + prefix, ok := modelIdPrefixes[stype.Type().Name()] + if !ok { + return errors.New("model unknown") + } + field.SetString(prefix + "_" + field.String()) + } + + // Relational IDs + for fieldName, prefix := range relationalIdPrefixes { + field := stype.FieldByName(fieldName) + if !field.IsValid() { + continue + } + field.SetString(prefix + "_" + field.String()) + } + + return nil +} diff --git a/api/handler/common_test.go b/api/handler/common_test.go new file mode 100644 index 00000000..177568cc --- /dev/null +++ b/api/handler/common_test.go @@ -0,0 +1,38 @@ +package handler + +import ( + "testing" + + "github.com/String-xyz/string-api/pkg/model" + "github.com/stretchr/testify/assert" +) + +func TestSanitizeModelInput(t *testing.T) { + m := model.User{Id: "user_0923840923840923840923"} + err := SanitizeIdInput(&m) + assert.NoError(t, err) + assert.Equal(t, "0923840923840923840923", m.Id) +} + +func TestSanitizeModelOutput(t *testing.T) { + m := model.User{Id: "0923840923840923840923"} + err := SanitizeIdOutput(&m) + assert.NoError(t, err) + assert.Equal(t, "user_0923840923840923840923", m.Id) +} + +func TestSanitizeRelationalModelInput(t *testing.T) { + m := model.ContactToPlatform{ContactId: "contact_123", PlatformId: "platform_456"} + err := SanitizeIdInput(&m) + assert.NoError(t, err) + assert.Equal(t, "123", m.ContactId) + assert.Equal(t, "456", m.PlatformId) +} + +func TestSanitizeRelationalModelOutput(t *testing.T) { + m := model.ContactToPlatform{ContactId: "123", PlatformId: "456"} + err := SanitizeIdOutput(&m) + assert.NoError(t, err) + assert.Equal(t, "contact_123", m.ContactId) + assert.Equal(t, "platform_456", m.PlatformId) +} diff --git a/api/handler/login.go b/api/handler/login.go index 8f2b3481..6a5b0ae2 100644 --- a/api/handler/login.go +++ b/api/handler/login.go @@ -62,6 +62,11 @@ func (l login) VerifySignature(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ PlatformId string }{platformId}, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + strBypassDevice := c.QueryParam("bypassDevice") bypassDevice := strBypassDevice == "true" // convert to bool. default is false @@ -117,6 +122,11 @@ func (l login) VerifySignature(c echo.Context) error { return httperror.InternalError(c) } + err = libcommon.SanitizeIdOutput(&resp.User) + if err != nil { + libcommon.LogStringError(c, err, "RefreshToken: unable to sanitize id output") + return httperror.InternalError(c) + } return c.JSON(http.StatusOK, resp) } @@ -127,8 +137,13 @@ func (l login) RefreshToken(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ PlatformId string }{platformId}, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + var body model.RefreshTokenPayload - err := c.Bind(&body) + err = c.Bind(&body) if err != nil { libcommon.LogStringError(c, err, "login: binding body") return httperror.BadRequestError(c) @@ -164,6 +179,11 @@ func (l login) RefreshToken(c echo.Context) error { return httperror.InternalError(c) } + err = libcommon.SanitizeIdOutput(&resp.User) + if err != nil { + libcommon.LogStringError(c, err, "RefreshToken: unable to sanitize id output") + return httperror.InternalError(c) + } return c.JSON(http.StatusOK, resp) } diff --git a/api/handler/quotes.go b/api/handler/quotes.go index f6e9162c..71545ee3 100644 --- a/api/handler/quotes.go +++ b/api/handler/quotes.go @@ -52,6 +52,10 @@ func (q quote) Quote(c echo.Context) error { if !ok { return httperror.InternalError(c, "missing or invalid platformId") } + err = libcommon.SanitizeIdInput(&struct{ PlatformId string }{platformId}, &platformId) + if err != nil { + return httperror.InternalError(c, "Failed to sanitize platform id") + } res, err := q.Service.Quote(ctx, body, platformId) if err != nil { diff --git a/api/handler/transact.go b/api/handler/transact.go index b3363e3a..b110d200 100644 --- a/api/handler/transact.go +++ b/api/handler/transact.go @@ -42,9 +42,14 @@ func (t transaction) Transact(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ UserId, DeviceId, PlatformId string }{userId, deviceId, platformId}, &userId, &deviceId, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + var body model.ExecutionRequest - err := c.Bind(&body) + err = c.Bind(&body) if err != nil { libcommon.LogStringError(c, err, "transact: execute bind") return httperror.BadRequestError(c) diff --git a/api/handler/user.go b/api/handler/user.go index b20de2cb..56be3081 100644 --- a/api/handler/user.go +++ b/api/handler/user.go @@ -43,9 +43,14 @@ func (u user) Create(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ PlatformId string }{platformId}, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + ctx := c.Request().Context() var body model.WalletSignaturePayloadSigned - err := c.Bind(&body) + err = c.Bind(&body) if err != nil { libcommon.LogStringError(c, err, "user:create user bind") return httperror.BadRequestError(c) @@ -88,6 +93,11 @@ func (u user) Create(c echo.Context) error { return httperror.InternalError(c) } + libcommon.SanitizeIdOutput(&resp.User) + if err != nil { + libcommon.LogStringError(c, err, "user: unable to sanitize id output") + return httperror.InternalError(c) + } return c.JSON(http.StatusOK, resp) } @@ -98,6 +108,10 @@ func (u user) Status(c echo.Context) error { return httperror.Unauthorized(c) } + err := libcommon.SanitizeIdInput(&struct{ UserId string }{userId}, &userId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } status, err := u.userService.GetStatus(ctx, userId) if err != nil { libcommon.LogStringError(c, err, "user: get status") @@ -123,12 +137,22 @@ func (u user) Update(c echo.Context) error { _, userId := validUserId(IdParam(c), c) + err = libcommon.SanitizeIdInput(&struct{ UserId string }{userId}, &userId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + user, err := u.userService.Update(ctx, userId, body) if err != nil { libcommon.LogStringError(c, err, "user: update") return httperror.InternalError(c) } + err = libcommon.SanitizeIdOutput(&user) + if err != nil { + libcommon.LogStringError(c, err, "user: failed to sanitize id output") + return httperror.InternalError(c) + } return c.JSON(http.StatusOK, user) } @@ -152,7 +176,12 @@ func (u user) VerifyEmail(c echo.Context) error { return httperror.BadRequestError(c, "Invalid email") } - err := u.verificationService.SendEmailVerification(ctx, platformId, userId, email) + err := libcommon.SanitizeIdInput(&struct{ UserId, PlatformId string }{userId, platformId}, &userId, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + + err = u.verificationService.SendEmailVerification(ctx, platformId, userId, email) if err != nil { libcommon.LogStringError(c, err, "user: email verification") @@ -178,9 +207,14 @@ func (u user) PreValidateEmail(c echo.Context) error { return httperror.InternalError(c, "missing or invalid platformId") } + err := libcommon.SanitizeIdInput(&struct{ UserId, PlatformId string }{userId, platformId}, &userId, &platformId) + if err != nil { + return httperror.BadRequestError(c, err.Error()) + } + // Get email from body var body model.PreValidateEmail - err := c.Bind(&body) + err = c.Bind(&body) if err != nil { libcommon.LogStringError(c, err, "user: pre validate email bind") return httperror.BadRequestError(c) diff --git a/go.mod b/go.mod index 3e8b94a4..a94112c7 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 - github.com/String-xyz/go-lib v1.5.0 + github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b github.com/aws/aws-sdk-go v1.44.168 github.com/aws/aws-sdk-go-v2/config v1.18.7 github.com/aws/aws-sdk-go-v2/service/ssm v1.33.4 diff --git a/go.sum b/go.sum index 3883690e..77f3143b 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,10 @@ github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpz github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/String-xyz/go-lib v1.4.1-0.20230423225120-a83fd6932b48 h1:RONXDVve/NRh48kNFHljciz5PTtAuFc9z+jcSnhpVAc= +github.com/String-xyz/go-lib v1.4.1-0.20230423225120-a83fd6932b48/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= +github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b h1:k1PfOPsrE5h0Yn82oUhFoUBjV6Ms/hSjs5zz2Uf5uok= +github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/String-xyz/go-lib v1.5.0 h1:Qb5kw2gyfQceIqNzNFopf5BIFqbzkSNN6Aop9i2SmVc= github.com/String-xyz/go-lib v1.5.0/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o= diff --git a/pkg/model/prefix_id.go b/pkg/model/prefix_id.go new file mode 100644 index 00000000..bda69631 --- /dev/null +++ b/pkg/model/prefix_id.go @@ -0,0 +1,167 @@ +package model + +import ( + "database/sql/driver" + "errors" + "reflect" + "strings" +) + +type UserId string +type PlatformId string +type NetworkId string +type AssetId string +type DeviceId string +type ContactId string +type LocationId string +type InstrumentId string +type TxLegId string +type TransactionId string +type AuthStrategyId string +type ApiKeyId string +type ContractId string + +func (id UserId) Value() (driver.Value, error) { return value(id) } +func (id PlatformId) Value() (driver.Value, error) { return value(id) } +func (id NetworkId) Value() (driver.Value, error) { return value(id) } +func (id AssetId) Value() (driver.Value, error) { return value(id) } +func (id DeviceId) Value() (driver.Value, error) { return value(id) } +func (id ContactId) Value() (driver.Value, error) { return value(id) } +func (id LocationId) Value() (driver.Value, error) { return value(id) } +func (id InstrumentId) Value() (driver.Value, error) { return value(id) } +func (id TxLegId) Value() (driver.Value, error) { return value(id) } +func (id TransactionId) Value() (driver.Value, error) { return value(id) } +func (id AuthStrategyId) Value() (driver.Value, error) { return value(id) } +func (id ApiKeyId) Value() (driver.Value, error) { return value(id) } +func (id ContractId) Value() (driver.Value, error) { return value(id) } + +func (id UserId) Scan(src interface{}) error { return scan(id, src) } +func (id PlatformId) Scan(src interface{}) error { return scan(id, src) } +func (id NetworkId) Scan(src interface{}) error { return scan(id, src) } +func (id AssetId) Scan(src interface{}) error { return scan(id, src) } +func (id DeviceId) Scan(src interface{}) error { return scan(id, src) } +func (id ContactId) Scan(src interface{}) error { return scan(id, src) } +func (id LocationId) Scan(src interface{}) error { return scan(id, src) } +func (id InstrumentId) Scan(src interface{}) error { return scan(id, src) } +func (id TxLegId) Scan(src interface{}) error { return scan(id, src) } +func (id TransactionId) Scan(src interface{}) error { return scan(id, src) } +func (id AuthStrategyId) Scan(src interface{}) error { return scan(id, src) } +func (id ApiKeyId) Scan(src interface{}) error { return scan(id, src) } +func (id ContractId) Scan(src interface{}) error { return scan(id, src) } + +// func Trim(id string) string { +// parts := strings.Split(id, "_") +// if len(parts) < 2 { +// return "" +// } +// return parts[1] +// } + +func Trim(id interface{}) string { + str, ok := id.(string) + if !ok { + return "" + } + parts := strings.Split(str, "_") + if len(parts) < 2 { + return "" + } + return parts[1] +} + +var typeToPrefix = map[string]string{ + "UserId": "user", + "PlatformId": "platform", + "NetworkId": "network", + "AssetId": "asset", + "DeviceId": "device", + "ContactId": "contact", + "LocationId": "location", + "InstrumentId": "instrument", + "TxLegId": "txleg", + "TransactionId": "tx", + "AuthStrategyId": "auth", + "ApiKeyId": "apikey", + "ContractId": "contract", +} + +func value(id interface{}) (driver.Value, error) { + t := reflect.TypeOf(id).Name() + prefix, ok := typeToPrefix[t] + if !ok { + return "", errors.New("unknown type") + } + if id.(string)[0:len(prefix)+1] != prefix+"_" { + return nil, errors.New("invalid prefix") + } + return []byte(id.(string)[len(prefix)+1:]), nil +} + +func scan(destination interface{}, src interface{}) error { + t := reflect.TypeOf(destination).Name() + prefix, ok := typeToPrefix[t] + if !ok { + return errors.New("unknown type") + } + switch v := src.(type) { + case string: + destination = prefix + "_" + v + case []byte: + destination = prefix + "_" + string(v) + case nil: + destination = "" + } + return nil +} + +//////// + +// type ID struct { +// Prefix string +// Id string +// } + +// func (id ID) ToString() string { +// return id.Id +// } + +// func (id *ID) Scan(src interface{}) error { +// switch v := src.(type) { +// case string: +// id.Id = v +// case []byte: +// id.Id = string(v) +// case nil: +// id.Id = "" +// } +// return nil +// } + +// func (id ID) Value() (driver.Value, error) { +// return []byte(id.Id), nil +// } + +// // Marshal the ID +// func (id ID) MarshalJSON() ([]byte, error) { +// return []byte(id.Id), nil +// } + +// // Unmarshal the ID +// func (id *ID) UnmarshalJSON(data []byte) error { +// id.Id = string(data) +// return nil +// } + +// type UserId struct{ ID } +// type PlatformId struct{ ID } +// type NetworkId struct{ ID } +// type AssetId struct{ ID } +// type DeviceId struct{ ID } +// type ContactId struct{ ID } +// type LocationId struct{ ID } +// type InstrumentId struct{ ID } +// type TxLegId struct{ ID } +// type TransactionId struct{ ID } +// type AuthStrategyId struct{ ID } +// type ApiKeyId struct{ ID } +// type ContractId struct{ ID } From b0f8d7734c5bc8edd80d6178ec543c4c508ed1cf Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Apr 2023 18:55:05 -0700 Subject: [PATCH 2/7] moved prefixing to go-lib --- api/handler/common.go | 97 -------------------------------------- api/handler/common_test.go | 38 --------------- 2 files changed, 135 deletions(-) delete mode 100644 api/handler/common_test.go diff --git a/api/handler/common.go b/api/handler/common.go index bd2b4e7e..76e7ed83 100644 --- a/api/handler/common.go +++ b/api/handler/common.go @@ -1,9 +1,7 @@ package handler import ( - "errors" "net/http" - "reflect" "regexp" "strings" "time" @@ -156,98 +154,3 @@ func DefaultErrorHandler(c echo.Context, err error, handlerName string) error { return httperror.InternalError(c) } - -var modelIdPrefixes = map[string]string{ - "User": "user", - "Platform": "platform", - "Network": "network", - "Asset": "asset", - "Device": "device", - "Contact": "contact", - "Location": "location", - "Instrument": "instrument", - "TxLeg": "txleg", - "Transaction": "tx", - "AuthStrategy": "auth", - "ApiKey": "apikey", - "Contract": "contract", -} - -// var relationalIdPrefixes = map[string][]string{ -// "UserToPlatform": {"user", "platform"}, -// "ContactToPlatform": {"contact", "platform"}, -// } - -var relationalIdPrefixes = map[string]string{ - "UserId": "user", - "PlatformId": "platform", - "ContactId": "contact", - "DeviceId": "device", - "InstrumentId": "instrument", - "NetworkId": "network", - "OriginTxLegId": "txleg", - "DestinationTxLegId": "txleg", - "ReceiptTxId": "txleg", - "ResponseTxId": "txleg", - "AssetId": "asset", -} - -func SanitizeIdInput(model interface{}) error { - // Model ID - stype := reflect.ValueOf(model).Elem() - field := stype.FieldByName("Id") - if !field.IsValid() { - // return errors.New("model does not contain an id") - // model may be relational, continue - } else { - prefix, ok := modelIdPrefixes[stype.Type().Name()] - if !ok { - return errors.New("model unknown") - } - if field.String()[:len(prefix)+1] != prefix+"_" { - return errors.New("input missing prefix " + prefix + "_") - } - field.SetString(field.String()[len(prefix)+1:]) - } - - // Relational IDs - for fieldName, prefix := range relationalIdPrefixes { - field := stype.FieldByName(fieldName) - if !field.IsValid() { - continue - } - if field.String()[:len(prefix)+1] != prefix+"_" { - return errors.New("input missing prefix " + prefix + "_") - } - field.SetString(field.String()[len(prefix)+1:]) - } - - return nil -} - -func SanitizeIdOutput(model interface{}) error { - // Model ID - stype := reflect.ValueOf(model).Elem() - field := stype.FieldByName("Id") - if !field.IsValid() { - // return errors.New("model does not contain an id") - // Model may be relational, continue - } else { - prefix, ok := modelIdPrefixes[stype.Type().Name()] - if !ok { - return errors.New("model unknown") - } - field.SetString(prefix + "_" + field.String()) - } - - // Relational IDs - for fieldName, prefix := range relationalIdPrefixes { - field := stype.FieldByName(fieldName) - if !field.IsValid() { - continue - } - field.SetString(prefix + "_" + field.String()) - } - - return nil -} diff --git a/api/handler/common_test.go b/api/handler/common_test.go deleted file mode 100644 index 177568cc..00000000 --- a/api/handler/common_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package handler - -import ( - "testing" - - "github.com/String-xyz/string-api/pkg/model" - "github.com/stretchr/testify/assert" -) - -func TestSanitizeModelInput(t *testing.T) { - m := model.User{Id: "user_0923840923840923840923"} - err := SanitizeIdInput(&m) - assert.NoError(t, err) - assert.Equal(t, "0923840923840923840923", m.Id) -} - -func TestSanitizeModelOutput(t *testing.T) { - m := model.User{Id: "0923840923840923840923"} - err := SanitizeIdOutput(&m) - assert.NoError(t, err) - assert.Equal(t, "user_0923840923840923840923", m.Id) -} - -func TestSanitizeRelationalModelInput(t *testing.T) { - m := model.ContactToPlatform{ContactId: "contact_123", PlatformId: "platform_456"} - err := SanitizeIdInput(&m) - assert.NoError(t, err) - assert.Equal(t, "123", m.ContactId) - assert.Equal(t, "456", m.PlatformId) -} - -func TestSanitizeRelationalModelOutput(t *testing.T) { - m := model.ContactToPlatform{ContactId: "123", PlatformId: "456"} - err := SanitizeIdOutput(&m) - assert.NoError(t, err) - assert.Equal(t, "contact_123", m.ContactId) - assert.Equal(t, "platform_456", m.PlatformId) -} From 0dac6537fc73a044a58c6aacca42c8366a943280 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Apr 2023 18:56:17 -0700 Subject: [PATCH 3/7] delete unused code --- pkg/model/prefix_id.go | 167 ----------------------------------------- 1 file changed, 167 deletions(-) delete mode 100644 pkg/model/prefix_id.go diff --git a/pkg/model/prefix_id.go b/pkg/model/prefix_id.go deleted file mode 100644 index bda69631..00000000 --- a/pkg/model/prefix_id.go +++ /dev/null @@ -1,167 +0,0 @@ -package model - -import ( - "database/sql/driver" - "errors" - "reflect" - "strings" -) - -type UserId string -type PlatformId string -type NetworkId string -type AssetId string -type DeviceId string -type ContactId string -type LocationId string -type InstrumentId string -type TxLegId string -type TransactionId string -type AuthStrategyId string -type ApiKeyId string -type ContractId string - -func (id UserId) Value() (driver.Value, error) { return value(id) } -func (id PlatformId) Value() (driver.Value, error) { return value(id) } -func (id NetworkId) Value() (driver.Value, error) { return value(id) } -func (id AssetId) Value() (driver.Value, error) { return value(id) } -func (id DeviceId) Value() (driver.Value, error) { return value(id) } -func (id ContactId) Value() (driver.Value, error) { return value(id) } -func (id LocationId) Value() (driver.Value, error) { return value(id) } -func (id InstrumentId) Value() (driver.Value, error) { return value(id) } -func (id TxLegId) Value() (driver.Value, error) { return value(id) } -func (id TransactionId) Value() (driver.Value, error) { return value(id) } -func (id AuthStrategyId) Value() (driver.Value, error) { return value(id) } -func (id ApiKeyId) Value() (driver.Value, error) { return value(id) } -func (id ContractId) Value() (driver.Value, error) { return value(id) } - -func (id UserId) Scan(src interface{}) error { return scan(id, src) } -func (id PlatformId) Scan(src interface{}) error { return scan(id, src) } -func (id NetworkId) Scan(src interface{}) error { return scan(id, src) } -func (id AssetId) Scan(src interface{}) error { return scan(id, src) } -func (id DeviceId) Scan(src interface{}) error { return scan(id, src) } -func (id ContactId) Scan(src interface{}) error { return scan(id, src) } -func (id LocationId) Scan(src interface{}) error { return scan(id, src) } -func (id InstrumentId) Scan(src interface{}) error { return scan(id, src) } -func (id TxLegId) Scan(src interface{}) error { return scan(id, src) } -func (id TransactionId) Scan(src interface{}) error { return scan(id, src) } -func (id AuthStrategyId) Scan(src interface{}) error { return scan(id, src) } -func (id ApiKeyId) Scan(src interface{}) error { return scan(id, src) } -func (id ContractId) Scan(src interface{}) error { return scan(id, src) } - -// func Trim(id string) string { -// parts := strings.Split(id, "_") -// if len(parts) < 2 { -// return "" -// } -// return parts[1] -// } - -func Trim(id interface{}) string { - str, ok := id.(string) - if !ok { - return "" - } - parts := strings.Split(str, "_") - if len(parts) < 2 { - return "" - } - return parts[1] -} - -var typeToPrefix = map[string]string{ - "UserId": "user", - "PlatformId": "platform", - "NetworkId": "network", - "AssetId": "asset", - "DeviceId": "device", - "ContactId": "contact", - "LocationId": "location", - "InstrumentId": "instrument", - "TxLegId": "txleg", - "TransactionId": "tx", - "AuthStrategyId": "auth", - "ApiKeyId": "apikey", - "ContractId": "contract", -} - -func value(id interface{}) (driver.Value, error) { - t := reflect.TypeOf(id).Name() - prefix, ok := typeToPrefix[t] - if !ok { - return "", errors.New("unknown type") - } - if id.(string)[0:len(prefix)+1] != prefix+"_" { - return nil, errors.New("invalid prefix") - } - return []byte(id.(string)[len(prefix)+1:]), nil -} - -func scan(destination interface{}, src interface{}) error { - t := reflect.TypeOf(destination).Name() - prefix, ok := typeToPrefix[t] - if !ok { - return errors.New("unknown type") - } - switch v := src.(type) { - case string: - destination = prefix + "_" + v - case []byte: - destination = prefix + "_" + string(v) - case nil: - destination = "" - } - return nil -} - -//////// - -// type ID struct { -// Prefix string -// Id string -// } - -// func (id ID) ToString() string { -// return id.Id -// } - -// func (id *ID) Scan(src interface{}) error { -// switch v := src.(type) { -// case string: -// id.Id = v -// case []byte: -// id.Id = string(v) -// case nil: -// id.Id = "" -// } -// return nil -// } - -// func (id ID) Value() (driver.Value, error) { -// return []byte(id.Id), nil -// } - -// // Marshal the ID -// func (id ID) MarshalJSON() ([]byte, error) { -// return []byte(id.Id), nil -// } - -// // Unmarshal the ID -// func (id *ID) UnmarshalJSON(data []byte) error { -// id.Id = string(data) -// return nil -// } - -// type UserId struct{ ID } -// type PlatformId struct{ ID } -// type NetworkId struct{ ID } -// type AssetId struct{ ID } -// type DeviceId struct{ ID } -// type ContactId struct{ ID } -// type LocationId struct{ ID } -// type InstrumentId struct{ ID } -// type TxLegId struct{ ID } -// type TransactionId struct{ ID } -// type AuthStrategyId struct{ ID } -// type ApiKeyId struct{ ID } -// type ContractId struct{ ID } From 53a77cf23da441c69afa1abb0cd275ffab951ff2 Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 24 Apr 2023 12:03:14 -0700 Subject: [PATCH 4/7] err= --- api/handler/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/handler/user.go b/api/handler/user.go index 56be3081..cc4360c6 100644 --- a/api/handler/user.go +++ b/api/handler/user.go @@ -93,7 +93,7 @@ func (u user) Create(c echo.Context) error { return httperror.InternalError(c) } - libcommon.SanitizeIdOutput(&resp.User) + err = libcommon.SanitizeIdOutput(&resp.User) if err != nil { libcommon.LogStringError(c, err, "user: unable to sanitize id output") return httperror.InternalError(c) From c39802841e3d9d4bd5ce2b0f0faa9ce7253a8df6 Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 24 Apr 2023 12:33:44 -0700 Subject: [PATCH 5/7] use go-lib 1.7.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a94112c7..91e41731 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 - github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b + github.com/String-xyz/go-lib v1.7.0 github.com/aws/aws-sdk-go v1.44.168 github.com/aws/aws-sdk-go-v2/config v1.18.7 github.com/aws/aws-sdk-go-v2/service/ssm v1.33.4 diff --git a/go.sum b/go.sum index 77f3143b..7119f20f 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,8 @@ github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b h1:k1PfOPsrE5h github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/String-xyz/go-lib v1.5.0 h1:Qb5kw2gyfQceIqNzNFopf5BIFqbzkSNN6Aop9i2SmVc= github.com/String-xyz/go-lib v1.5.0/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= +github.com/String-xyz/go-lib v1.7.0 h1:dDJpeqLDK0BBP6Db+upPwySmcxcmvOlnxb+PXFBHzfM= +github.com/String-xyz/go-lib v1.7.0/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o= github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= From 26208fc19e974d8520073cf10ce3f41fa0a8f9ff Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 24 Apr 2023 12:56:16 -0700 Subject: [PATCH 6/7] update libmiddleware.Tracer() call --- api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index b7d8b74d..907d599e 100644 --- a/api/api.go +++ b/api/api.go @@ -63,7 +63,7 @@ func StartInternal(config APIConfig) { } func baseMiddleware(logger *zerolog.Logger, e *echo.Echo) { - e.Use(libmiddleware.Tracer()) + e.Use(libmiddleware.Tracer("string-api")) e.Use(libmiddleware.CORS()) e.Use(libmiddleware.RequestId()) e.Use(libmiddleware.Recover()) From f3d370c1859659b96a53936b0d185ca92c78633e Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 24 Apr 2023 14:20:13 -0700 Subject: [PATCH 7/7] go mod tidy --- go.sum | 6 ------ 1 file changed, 6 deletions(-) diff --git a/go.sum b/go.sum index 88af74c3..e77d14e3 100644 --- a/go.sum +++ b/go.sum @@ -26,12 +26,6 @@ github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpz github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= -github.com/String-xyz/go-lib v1.4.1-0.20230423225120-a83fd6932b48 h1:RONXDVve/NRh48kNFHljciz5PTtAuFc9z+jcSnhpVAc= -github.com/String-xyz/go-lib v1.4.1-0.20230423225120-a83fd6932b48/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= -github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b h1:k1PfOPsrE5h0Yn82oUhFoUBjV6Ms/hSjs5zz2Uf5uok= -github.com/String-xyz/go-lib v1.4.1-0.20230424000125-2b5b91f64c9b/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= -github.com/String-xyz/go-lib v1.6.0 h1:Wf6wX0wpKbg620RAfSMnp8mHLFc/GHq7Ru1/JNfm+RE= -github.com/String-xyz/go-lib v1.6.0/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/String-xyz/go-lib v1.7.0 h1:dDJpeqLDK0BBP6Db+upPwySmcxcmvOlnxb+PXFBHzfM= github.com/String-xyz/go-lib v1.7.0/go.mod h1:TFAJPYo6YXvk3A1p1WkFuoN5k1wGHbRTxuOg9KLjpUI= github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o=