diff --git a/cmd/neofs-rest-gw/integration_test.go b/cmd/neofs-rest-gw/integration_test.go index d556c46..0f793d3 100644 --- a/cmd/neofs-rest-gw/integration_test.go +++ b/cmd/neofs-rest-gw/integration_test.go @@ -24,7 +24,8 @@ import ( "github.com/nspcc-dev/neofs-rest-gw/gen/restapi" "github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations" "github.com/nspcc-dev/neofs-rest-gw/handlers" - walletconnect "github.com/nspcc-dev/neofs-rest-gw/wallet-connect" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" + "github.com/nspcc-dev/neofs-rest-gw/internal/wallet-connect" "github.com/nspcc-dev/neofs-sdk-go/container" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/eacl" @@ -252,8 +253,8 @@ func restObjectPut(ctx context.Context, t *testing.T, clientPool *pool.Pool, cnr } req := &models.ObjectUpload{ - ContainerID: handlers.NewString(cnrID.String()), - FileName: handlers.NewString("newFile.txt"), + ContainerID: util.NewString(cnrID.String()), + FileName: util.NewString("newFile.txt"), Payload: base64.StdEncoding.EncodeToString([]byte(content)), Attributes: []*models.Attribute{{ Key: &attrKey, @@ -473,9 +474,9 @@ func restObjectsSearch(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *c search := &models.SearchFilters{ Filters: []*models.SearchFilter{ { - Key: handlers.NewString(userKey), + Key: util.NewString(userKey), Match: models.NewSearchMatch(models.SearchMatchMatchStringEqual), - Value: handlers.NewString(userValue), + Value: util.NewString(userValue), }, }, } @@ -609,7 +610,7 @@ func restContainerEACLPut(ctx context.Context, t *testing.T, clientPool *pool.Po table, err := clientPool.GetEACL(ctx, prm) require.NoError(t, err) - expectedTable, err := handlers.ToNativeTable(req.Records) + expectedTable, err := util.ToNativeTable(req.Records) require.NoError(t, err) expectedTable.SetCID(cnrID) @@ -633,7 +634,7 @@ func restContainerEACLGet(ctx context.Context, t *testing.T, p *pool.Pool, cnrID require.Equal(t, cnrID.String(), responseTable.ContainerID) - actualTable, err := handlers.ToNativeTable(responseTable.Records) + actualTable, err := util.ToNativeTable(responseTable.Records) require.NoError(t, err) actualTable.SetCID(cnrID) @@ -662,7 +663,7 @@ func restContainerList(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *c require.Equal(t, len(ids), int(*list.Size)) expected := &models.ContainerBaseInfo{ - ContainerID: handlers.NewString(cnrID.String()), + ContainerID: util.NewString(cnrID.String()), Name: containerName, } @@ -768,7 +769,7 @@ func restContainerPut(ctx context.Context, t *testing.T, clientPool *pool.Pool) } req := operations.PutContainerBody{ - ContainerName: handlers.NewString("cnr"), + ContainerName: util.NewString("cnr"), } body, err := json.Marshal(&req) require.NoError(t, err) diff --git a/handlers/auth.go b/handlers/auth.go index 2b77a43..ac9ead8 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -11,6 +11,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neofs-rest-gw/gen/models" "github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/pool" ) @@ -39,7 +40,7 @@ func (a *API) PostAuth(params operations.AuthParams) middleware.Responder { func prepareObjectToken(params operations.AuthParams, pool *pool.Pool) (*models.TokenResponse, error) { ctx := params.HTTPRequest.Context() - btoken, err := ToNativeObjectToken(params.Token) + btoken, err := util.ToNativeObjectToken(params.Token) if err != nil { return nil, fmt.Errorf("couldn't transform token to native: %w", err) } @@ -58,7 +59,7 @@ func prepareObjectToken(params operations.AuthParams, pool *pool.Pool) (*models. var resp models.TokenResponse resp.Type = models.NewTokenType(models.TokenTypeObject) - resp.Token = NewString(base64.StdEncoding.EncodeToString(binaryBearer)) + resp.Token = util.NewString(base64.StdEncoding.EncodeToString(binaryBearer)) return &resp, nil } @@ -79,7 +80,7 @@ func prepareContainerTokens(params operations.AuthParams, pool *pool.Pool, key * var resp models.TokenResponse resp.Type = models.NewTokenType(models.TokenTypeContainer) - stoken, err := ToNativeContainerToken(params.Token) + stoken, err := util.ToNativeContainerToken(params.Token) if err != nil { return nil, fmt.Errorf("couldn't transform rule to native session token: %w", err) } @@ -101,7 +102,7 @@ func prepareContainerTokens(params operations.AuthParams, pool *pool.Pool, key * return nil, fmt.Errorf("couldn't marshal session token: %w", err) } - resp.Token = NewString(base64.StdEncoding.EncodeToString(binaryToken)) + resp.Token = util.NewString(base64.StdEncoding.EncodeToString(binaryToken)) return &resp, nil } diff --git a/handlers/auth_test.go b/handlers/auth_test.go index bbcd149..09c5b89 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -11,6 +11,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neofs-rest-gw/gen/models" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/stretchr/testify/require" ) @@ -35,7 +36,7 @@ func TestSign(t *testing.T) { }}, } - btoken, err := ToNativeObjectToken(b) + btoken, err := util.ToNativeObjectToken(b) require.NoError(t, err) ownerKey, err := keys.NewPublicKeyFromString(pubKeyHex) diff --git a/handlers/containers.go b/handlers/containers.go index 71ae68d..f4d650a 100644 --- a/handlers/containers.go +++ b/handlers/containers.go @@ -5,7 +5,6 @@ import ( "crypto/ecdsa" "encoding/base64" "fmt" - walletconnect "github.com/nspcc-dev/neofs-rest-gw/wallet-connect" "net/http" "strconv" "strings" @@ -17,6 +16,8 @@ import ( sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-rest-gw/gen/models" "github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" + "github.com/nspcc-dev/neofs-rest-gw/internal/wallet-connect" "github.com/nspcc-dev/neofs-sdk-go/acl" "github.com/nspcc-dev/neofs-sdk-go/container" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" @@ -52,7 +53,7 @@ func (a *API) PutContainers(params operations.PutContainerParams, principal *mod } var resp operations.PutContainerOKBody - resp.ContainerID = NewString(cnrID.String()) + resp.ContainerID = util.NewString(cnrID.String()) return operations.NewPutContainerOK().WithPayload(&resp) } @@ -67,17 +68,17 @@ func (a *API) GetContainer(params operations.GetContainerParams) middleware.Resp attrs := make([]*models.Attribute, len(cnr.Attributes())) for i, attr := range cnr.Attributes() { attrs[i] = &models.Attribute{ - Key: NewString(attr.Key()), - Value: NewString(attr.Value()), + Key: util.NewString(attr.Key()), + Value: util.NewString(attr.Value()), } } resp := &models.ContainerInfo{ - ContainerID: NewString(params.ContainerID), - Version: NewString(cnr.Version().String()), - OwnerID: NewString(cnr.OwnerID().String()), - BasicACL: NewString(acl.BasicACL(cnr.BasicACL()).String()), - PlacementPolicy: NewString(strings.Join(policy.Encode(cnr.PlacementPolicy()), " ")), + ContainerID: util.NewString(params.ContainerID), + Version: util.NewString(cnr.Version().String()), + OwnerID: util.NewString(cnr.OwnerID().String()), + BasicACL: util.NewString(acl.BasicACL(cnr.BasicACL()).String()), + PlacementPolicy: util.NewString(strings.Join(policy.Encode(cnr.PlacementPolicy()), " ")), Attributes: attrs, } @@ -104,7 +105,7 @@ func (a *API) PutContainerEACL(params operations.PutContainerEACLParams, princip if err = setContainerEACL(params.HTTPRequest.Context(), a.pool, cnrID, stoken, params.Eacl); err != nil { a.log.Error("failed set container eacl", zap.Error(err)) - return operations.NewPutContainerEACLBadRequest().WithPayload(NewError(err)) + return operations.NewPutContainerEACLBadRequest().WithPayload(util.NewError(err)) } return operations.NewPutContainerEACLOK() @@ -151,7 +152,7 @@ func (a *API) ListContainer(params operations.ListContainersParams) middleware.R if offset > len(ids)-1 { res := &models.ContainerList{ - Size: NewInteger(0), + Size: util.NewInteger(0), Containers: []*models.ContainerBaseInfo{}, } return operations.NewListContainersOK().WithPayload(res) @@ -162,7 +163,7 @@ func (a *API) ListContainer(params operations.ListContainersParams) middleware.R } res := &models.ContainerList{ - Size: NewInteger(int64(size)), + Size: util.NewInteger(int64(size)), Containers: make([]*models.ContainerBaseInfo, 0, size), } @@ -188,13 +189,13 @@ func (a *API) DeleteContainer(params operations.DeleteContainerParams, principal stoken, err := prepareSessionToken(bt, *params.WalletConnect) if err != nil { a.log.Error("failed parse session token", zap.Error(err)) - return operations.NewDeleteContainerBadRequest().WithPayload(NewError(err)) + return operations.NewDeleteContainerBadRequest().WithPayload(util.NewError(err)) } cnrID, err := parseContainerID(params.ContainerID) if err != nil { a.log.Error("failed get container id", zap.Error(err)) - return operations.NewDeleteContainerBadRequest().WithPayload(NewError(err)) + return operations.NewDeleteContainerBadRequest().WithPayload(util.NewError(err)) } var prm pool.PrmContainerDelete @@ -203,7 +204,7 @@ func (a *API) DeleteContainer(params operations.DeleteContainerParams, principal if err = a.pool.DeleteContainer(params.HTTPRequest.Context(), prm); err != nil { a.log.Error("failed delete container", zap.String("container", params.ContainerID), zap.Error(err)) - return operations.NewDeleteContainerBadRequest().WithPayload(NewError(err)) + return operations.NewDeleteContainerBadRequest().WithPayload(util.NewError(err)) } return operations.NewDeleteContainerNoContent() @@ -218,7 +219,7 @@ func getContainerBaseInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*mod return nil, err } - baseInfo := &models.ContainerBaseInfo{ContainerID: NewString(cnrID.String())} + baseInfo := &models.ContainerBaseInfo{ContainerID: util.NewString(cnrID.String())} for _, attr := range cnr.Attributes() { if attr.Key() == container.AttributeName { @@ -258,7 +259,7 @@ func parseContainerID(containerID string) (*cid.ID, error) { } func setContainerEACL(ctx context.Context, p *pool.Pool, cnrID *cid.ID, stoken *session.Token, eaclPrm *models.Eacl) error { - table, err := ToNativeTable(eaclPrm.Records) + table, err := util.ToNativeTable(eaclPrm.Records) if err != nil { return err } @@ -287,7 +288,7 @@ func getContainerEACL(ctx context.Context, p *pool.Pool, cnrID *cid.ID) (*models } for i, rec := range table.Records() { - record, err := FromNativeRecord(rec) + record, err := util.FromNativeRecord(rec) if err != nil { return nil, fmt.Errorf("couldn't transform record from native: %w", err) } diff --git a/handlers/objects.go b/handlers/objects.go index 66499ad..85f52c1 100644 --- a/handlers/objects.go +++ b/handlers/objects.go @@ -14,7 +14,8 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-rest-gw/gen/models" "github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations" - walletconnect "github.com/nspcc-dev/neofs-rest-gw/wallet-connect" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" + "github.com/nspcc-dev/neofs-rest-gw/internal/wallet-connect" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object/address" @@ -67,12 +68,12 @@ func (a *API) PutObjects(params operations.PutObjectParams, principal *models.Pr objID, err := a.pool.PutObject(ctx, prmPut) if err != nil { a.log.Error("put object", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } var resp models.Address resp.ContainerID = params.Object.ContainerID - resp.ObjectID = NewString(objID.String()) + resp.ObjectID = util.NewString(objID.String()) return operations.NewPutObjectOK().WithPayload(&resp) } @@ -91,7 +92,7 @@ func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *mo btoken, err := getBearerToken(principal, params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect) if err != nil { a.log.Error("get bearer token", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } var prm pool.PrmObjectHead @@ -101,21 +102,21 @@ func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *mo objInfo, err := a.pool.HeadObject(ctx, prm) if err != nil { a.log.Error("head object", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } var resp models.ObjectInfo - resp.ContainerID = NewString(params.ContainerID) - resp.ObjectID = NewString(params.ObjectID) - resp.OwnerID = NewString(objInfo.OwnerID().String()) + resp.ContainerID = util.NewString(params.ContainerID) + resp.ObjectID = util.NewString(params.ObjectID) + resp.OwnerID = util.NewString(objInfo.OwnerID().String()) resp.Attributes = make([]*models.Attribute, len(objInfo.Attributes())) - resp.ObjectSize = NewInteger(int64(objInfo.PayloadSize())) - resp.PayloadSize = NewInteger(0) + resp.ObjectSize = util.NewInteger(int64(objInfo.PayloadSize())) + resp.PayloadSize = util.NewInteger(0) for i, attr := range objInfo.Attributes() { resp.Attributes[i] = &models.Attribute{ - Key: NewString(attr.Key()), - Value: NewString(attr.Value()), + Key: util.NewString(attr.Key()), + Value: util.NewString(attr.Value()), } } @@ -127,7 +128,7 @@ func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *mo if params.RangeOffset != nil || params.RangeLength != nil { if params.RangeOffset == nil || params.RangeLength == nil { a.log.Error("both offset and length must be provided") - return errorResponse.WithPayload(NewError(fmt.Errorf("both offset and length must be provided"))) + return errorResponse.WithPayload(util.NewError(fmt.Errorf("both offset and length must be provided"))) } offset = uint64(*params.RangeOffset) length = uint64(*params.RangeLength) @@ -144,7 +145,7 @@ func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *mo rangeRes, err := a.pool.ObjectRange(ctx, prmRange) if err != nil { a.log.Error("range object", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } defer func() { @@ -158,15 +159,15 @@ func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *mo payloadSize, err := io.Copy(encoder, rangeRes) if err != nil { a.log.Error("encode object payload", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } if err = encoder.Close(); err != nil { a.log.Error("close encoder", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } resp.Payload = sb.String() - resp.PayloadSize = NewInteger(payloadSize) + resp.PayloadSize = util.NewInteger(payloadSize) return operations.NewGetObjectInfoOK().WithPayload(&resp) } @@ -185,7 +186,7 @@ func (a *API) DeleteObject(params operations.DeleteObjectParams, principal *mode btoken, err := getBearerToken(principal, params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect) if err != nil { a.log.Error("failed to get bearer token", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } var prm pool.PrmObjectDelete @@ -194,7 +195,7 @@ func (a *API) DeleteObject(params operations.DeleteObjectParams, principal *mode if err = a.pool.DeleteObject(ctx, prm); err != nil { a.log.Error("failed to delete object", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } return operations.NewDeleteObjectNoContent() @@ -214,13 +215,13 @@ func (a *API) SearchObjects(params operations.SearchObjectsParams, principal *mo btoken, err := getBearerToken(principal, params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect) if err != nil { a.log.Error("failed to get bearer token", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } - filters, err := ToNativeFilters(params.SearchFilters) + filters, err := util.ToNativeFilters(params.SearchFilters) if err != nil { a.log.Error("failed to transform to native", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } var prm pool.PrmObjectSearch @@ -231,7 +232,7 @@ func (a *API) SearchObjects(params operations.SearchObjectsParams, principal *mo resSearch, err := a.pool.SearchObjects(ctx, prm) if err != nil { a.log.Error("failed to search objects", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } offset := int(*params.Offset) @@ -261,11 +262,11 @@ func (a *API) SearchObjects(params operations.SearchObjectsParams, principal *mo } if err != nil { a.log.Error("failed to search objects", zap.Error(err)) - return errorResponse.WithPayload(NewError(err)) + return errorResponse.WithPayload(util.NewError(err)) } list := &models.ObjectList{ - Size: NewInteger(int64(len(objects))), + Size: util.NewInteger(int64(len(objects))), Objects: objects, } @@ -288,8 +289,8 @@ func headObjectBaseInfo(ctx context.Context, p *pool.Pool, cnrID *cid.ID, objID resp := &models.ObjectBaseInfo{ Address: &models.Address{ - ContainerID: NewString(cnrID.String()), - ObjectID: NewString(objID.String()), + ContainerID: util.NewString(cnrID.String()), + ObjectID: util.NewString(objID.String()), }, } diff --git a/handlers/util.go b/handlers/util.go index 36a4753..a6b168f 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -9,9 +9,8 @@ import ( "strings" "time" - "github.com/nspcc-dev/neofs-rest-gw/gen/models" - objectv2 "github.com/nspcc-dev/neofs-api-go/v2/object" + "github.com/nspcc-dev/neofs-rest-gw/gen/models" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/pool" @@ -217,15 +216,3 @@ func updateExpirationHeader(headers map[string]string, durations *epochDurations numEpoch := expDuration.Milliseconds() / epochDuration headers[objectv2.SysAttributeExpEpoch] = strconv.FormatInt(int64(durations.currentEpoch)+numEpoch, 10) } - -func NewString(val string) *string { - return &val -} - -func NewInteger(val int64) *int64 { - return &val -} - -func NewError(err error) models.Error { - return models.Error(err.Error()) -} diff --git a/handlers/transformers.go b/internal/util/transformers.go similarity index 97% rename from handlers/transformers.go rename to internal/util/transformers.go index 926d249..251b26d 100644 --- a/handlers/transformers.go +++ b/internal/util/transformers.go @@ -1,14 +1,14 @@ -package handlers +package util import ( "encoding/hex" "fmt" - "github.com/nspcc-dev/neofs-sdk-go/object" sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-rest-gw/gen/models" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/eacl" + "github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/nspcc-dev/neofs-sdk-go/token" ) @@ -429,3 +429,18 @@ func ToNativeFilters(fs *models.SearchFilters) (object.SearchFilters, error) { return filters, nil } + +// NewString returns pointer to provided string. +func NewString(val string) *string { + return &val +} + +// NewInteger returns pointer to provided int. +func NewInteger(val int64) *int64 { + return &val +} + +// NewError wraps error into models.Error. +func NewError(err error) models.Error { + return models.Error(err.Error()) +} diff --git a/wallet-connect/wallet_connect.go b/internal/wallet-connect/wallet_connect.go similarity index 100% rename from wallet-connect/wallet_connect.go rename to internal/wallet-connect/wallet_connect.go diff --git a/wallet-connect/wallet_connect_test.go b/internal/wallet-connect/wallet_connect_test.go similarity index 93% rename from wallet-connect/wallet_connect_test.go rename to internal/wallet-connect/wallet_connect_test.go index e3c112a..490cbad 100644 --- a/wallet-connect/wallet_connect_test.go +++ b/internal/wallet-connect/wallet_connect_test.go @@ -3,12 +3,13 @@ package walletconnect import ( "crypto/ecdsa" "encoding/hex" + "testing" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neofs-rest-gw/gen/models" - "github.com/nspcc-dev/neofs-rest-gw/handlers" + "github.com/nspcc-dev/neofs-rest-gw/internal/util" "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/stretchr/testify/require" - "testing" ) const devenvPrivateKey = "1dd37fba80fec4e6a6f13fd708d8dcb3b29def768017052f6c930fa1c5d90bbb" @@ -31,7 +32,7 @@ func TestSign(t *testing.T) { }}, } - btoken, err := handlers.ToNativeObjectToken(b) + btoken, err := util.ToNativeObjectToken(b) require.NoError(t, err) ownerKey, err := keys.NewPublicKeyFromString(pubKeyHex)