2022-04-11 09:35:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-04-13 08:41:04 +00:00
|
|
|
"net/url"
|
2022-04-11 09:35:06 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-openapi/loads"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/handlers"
|
2022-04-29 06:39:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/internal/util"
|
2022-06-09 09:59:02 +00:00
|
|
|
walletconnect "github.com/nspcc-dev/neofs-rest-gw/internal/wallet-connect"
|
2022-04-11 09:35:06 +00:00
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-06-09 09:59:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/policy"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
devenvPrivateKey = "1dd37fba80fec4e6a6f13fd708d8dcb3b29def768017052f6c930fa1c5d90bbb"
|
|
|
|
testListenAddress = "localhost:8082"
|
|
|
|
testHost = "http://" + testListenAddress
|
2022-04-15 07:03:00 +00:00
|
|
|
testContainerNode = "localhost:8080"
|
|
|
|
testLocalNode = "s01.neofs.devenv:8080"
|
2022-04-13 15:23:03 +00:00
|
|
|
containerName = "test-container"
|
2022-04-15 07:03:00 +00:00
|
|
|
localVersion = "local"
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
walletConnectQuery = "walletConnect"
|
2022-04-14 08:53:13 +00:00
|
|
|
// XBearerSignature header contains base64 encoded signature of the token body.
|
|
|
|
XBearerSignature = "X-Bearer-Signature"
|
|
|
|
// XBearerSignatureKey header contains hex encoded public key that corresponds the signature of the token body.
|
|
|
|
XBearerSignatureKey = "X-Bearer-Signature-Key"
|
2022-06-09 09:59:02 +00:00
|
|
|
// XBearerOwnerID header contains owner id (wallet address) that corresponds the signature of the token body.
|
|
|
|
XBearerOwnerID = "X-Bearer-Owner-Id"
|
2022-04-15 07:03:00 +00:00
|
|
|
|
2022-06-09 09:59:02 +00:00
|
|
|
// tests configuration.
|
2022-04-15 07:03:00 +00:00
|
|
|
useWalletConnect = false
|
2022-07-07 12:59:38 +00:00
|
|
|
useLocalEnvironment = true
|
2022-04-11 09:35:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntegration(t *testing.T) {
|
2022-04-15 07:03:00 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
key, err := keys.NewPrivateKeyFromHex(devenvPrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if useLocalEnvironment {
|
|
|
|
runLocalTests(ctx, t, key)
|
|
|
|
} else {
|
|
|
|
runTestInContainer(ctx, t, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLocalTests(ctx context.Context, t *testing.T, key *keys.PrivateKey) {
|
|
|
|
runTests(ctx, t, key, localVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTestInContainer(rootCtx context.Context, t *testing.T, key *keys.PrivateKey) {
|
2022-04-11 09:35:06 +00:00
|
|
|
aioImage := "nspccdev/neofs-aio-testcontainer:"
|
2022-04-13 08:41:04 +00:00
|
|
|
versions := []string{
|
2022-04-13 13:00:04 +00:00
|
|
|
//"0.24.0",
|
|
|
|
//"0.25.1",
|
|
|
|
//"0.26.1",
|
2022-04-13 13:46:49 +00:00
|
|
|
//"0.27.5",
|
2022-06-09 09:59:02 +00:00
|
|
|
"0.27.7",
|
|
|
|
//"latest",
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
for _, version := range versions {
|
2022-04-15 07:03:00 +00:00
|
|
|
ctx, cancel := context.WithCancel(rootCtx)
|
2022-04-11 09:35:06 +00:00
|
|
|
aioContainer := createDockerContainer(ctx, t, aioImage+version)
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
runTests(ctx, t, key, version)
|
|
|
|
|
|
|
|
err := aioContainer.Terminate(ctx)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-15 07:03:00 +00:00
|
|
|
cancel()
|
2022-04-11 09:35:06 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
func runTests(ctx context.Context, t *testing.T, key *keys.PrivateKey, version string) {
|
|
|
|
node := testContainerNode
|
|
|
|
if version == localVersion {
|
|
|
|
node = testLocalNode
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel := runServer(ctx, t, node)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
clientPool := getPool(ctx, t, key, node)
|
|
|
|
cnrID := createContainer(ctx, t, clientPool, containerName)
|
|
|
|
restrictByEACL(ctx, t, clientPool, cnrID)
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
t.Run("rest auth several tokens "+version, func(t *testing.T) { authTokens(ctx, t) })
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
t.Run("rest put object "+version, func(t *testing.T) { restObjectPut(ctx, t, clientPool, cnrID) })
|
|
|
|
t.Run("rest get object "+version, func(t *testing.T) { restObjectGet(ctx, t, clientPool, cnrID) })
|
|
|
|
t.Run("rest delete object "+version, func(t *testing.T) { restObjectDelete(ctx, t, clientPool, cnrID) })
|
2022-04-18 08:30:34 +00:00
|
|
|
t.Run("rest search objects "+version, func(t *testing.T) { restObjectsSearch(ctx, t, clientPool, cnrID) })
|
2022-04-15 07:03:00 +00:00
|
|
|
|
|
|
|
t.Run("rest put container "+version, func(t *testing.T) { restContainerPut(ctx, t, clientPool) })
|
|
|
|
t.Run("rest get container "+version, func(t *testing.T) { restContainerGet(ctx, t, clientPool, cnrID) })
|
2022-04-18 08:30:34 +00:00
|
|
|
t.Run("rest delete container "+version, func(t *testing.T) { restContainerDelete(ctx, t, clientPool) })
|
2022-04-15 07:03:00 +00:00
|
|
|
t.Run("rest put container eacl "+version, func(t *testing.T) { restContainerEACLPut(ctx, t, clientPool) })
|
|
|
|
t.Run("rest get container eacl "+version, func(t *testing.T) { restContainerEACLGet(ctx, t, clientPool, cnrID) })
|
|
|
|
t.Run("rest list containers "+version, func(t *testing.T) { restContainerList(ctx, t, clientPool, cnrID) })
|
|
|
|
}
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
func createDockerContainer(ctx context.Context, t *testing.T, image string) testcontainers.Container {
|
|
|
|
req := testcontainers.ContainerRequest{
|
|
|
|
Image: image,
|
|
|
|
WaitingFor: wait.NewLogStrategy("aio container started").WithStartupTimeout(30 * time.Second),
|
|
|
|
Name: "aio",
|
|
|
|
Hostname: "aio",
|
|
|
|
NetworkMode: "host",
|
|
|
|
}
|
|
|
|
aioC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: req,
|
|
|
|
Started: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return aioC
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
func runServer(ctx context.Context, t *testing.T, node string) context.CancelFunc {
|
2022-04-11 09:35:06 +00:00
|
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
v := getDefaultConfig(node)
|
2022-04-11 09:35:06 +00:00
|
|
|
l := newLogger(v)
|
|
|
|
|
|
|
|
neofsAPI, err := newNeofsAPI(cancelCtx, l, v)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
api := operations.NewNeofsRestGwAPI(swaggerSpec)
|
|
|
|
server := restapi.NewServer(api, serverConfig(v))
|
|
|
|
|
|
|
|
server.ConfigureAPI(neofsAPI.Configure)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := server.Serve()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
cancel()
|
|
|
|
err := server.Shutdown()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
func defaultHTTPClient() *http.Client {
|
|
|
|
return &http.Client{Timeout: 60 * time.Second}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
func getDefaultConfig(node string) *viper.Viper {
|
2022-04-11 09:35:06 +00:00
|
|
|
v := config()
|
2022-04-15 07:03:00 +00:00
|
|
|
v.SetDefault(cfgPeers+".0.address", node)
|
2022-04-11 09:35:06 +00:00
|
|
|
v.SetDefault(cfgPeers+".0.weight", 1)
|
|
|
|
v.SetDefault(cfgPeers+".0.priority", 1)
|
|
|
|
v.SetDefault(restapi.FlagListenAddress, testListenAddress)
|
2022-04-13 08:41:04 +00:00
|
|
|
v.SetDefault(restapi.FlagWriteTimeout, 60*time.Second)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
func getPool(ctx context.Context, t *testing.T, key *keys.PrivateKey, node string) *pool.Pool {
|
2022-04-11 09:35:06 +00:00
|
|
|
var prm pool.InitParameters
|
2022-04-15 07:03:00 +00:00
|
|
|
prm.AddNode(pool.NewNodeParam(1, node, 1))
|
2022-04-11 09:35:06 +00:00
|
|
|
prm.SetKey(&key.PrivateKey)
|
|
|
|
prm.SetHealthcheckTimeout(5 * time.Second)
|
|
|
|
prm.SetNodeDialTimeout(5 * time.Second)
|
|
|
|
|
|
|
|
clientPool, err := pool.NewPool(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = clientPool.Dial(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return clientPool
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:41:50 +00:00
|
|
|
func getRestrictBearerRecords() []*models.Record {
|
|
|
|
return []*models.Record{
|
|
|
|
formRestrictRecord(models.OperationGET),
|
|
|
|
formRestrictRecord(models.OperationHEAD),
|
|
|
|
formRestrictRecord(models.OperationPUT),
|
|
|
|
formRestrictRecord(models.OperationDELETE),
|
|
|
|
formRestrictRecord(models.OperationSEARCH),
|
|
|
|
formRestrictRecord(models.OperationRANGE),
|
|
|
|
formRestrictRecord(models.OperationRANGEHASH),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formRestrictRecord(op models.Operation) *models.Record {
|
|
|
|
return &models.Record{
|
|
|
|
Operation: models.NewOperation(op),
|
|
|
|
Action: models.NewAction(models.ActionDENY),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}}}
|
|
|
|
}
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
func authTokens(ctx context.Context, t *testing.T) {
|
|
|
|
bearers := []*models.Bearer{
|
|
|
|
{
|
|
|
|
Name: "all-object",
|
|
|
|
Object: []*models.Record{{
|
|
|
|
Operation: models.NewOperation(models.OperationPUT),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "put-container",
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbPUT),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "seteacl-container",
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbSETEACL),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "delete-container",
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbDELETE),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := defaultHTTPClient()
|
|
|
|
makeAuthTokenRequest(ctx, t, bearers, httpClient)
|
|
|
|
}
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
func restObjectPut(ctx context.Context, t *testing.T, clientPool *pool.Pool, cnrID *cid.ID) {
|
2022-04-13 13:46:49 +00:00
|
|
|
bearer := &models.Bearer{
|
2022-04-11 09:35:06 +00:00
|
|
|
Object: []*models.Record{{
|
|
|
|
Operation: models.NewOperation(models.OperationPUT),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
2022-04-21 11:41:50 +00:00
|
|
|
bearer.Object = append(bearer.Object, getRestrictBearerRecords()...)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
httpClient := defaultHTTPClient()
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
content := "content of file"
|
|
|
|
attrKey, attrValue := "User-Attribute", "user value"
|
|
|
|
|
|
|
|
attributes := map[string]string{
|
|
|
|
object.AttributeFileName: "newFile.txt",
|
|
|
|
attrKey: attrValue,
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:52:41 +00:00
|
|
|
req := &models.ObjectUpload{
|
2022-04-29 06:39:24 +00:00
|
|
|
ContainerID: util.NewString(cnrID.String()),
|
|
|
|
FileName: util.NewString("newFile.txt"),
|
2022-04-11 09:35:06 +00:00
|
|
|
Payload: base64.StdEncoding.EncodeToString([]byte(content)),
|
2022-04-20 14:52:41 +00:00
|
|
|
Attributes: []*models.Attribute{{
|
|
|
|
Key: &attrKey,
|
|
|
|
Value: &attrValue,
|
|
|
|
}},
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 14:52:41 +00:00
|
|
|
body, err := json.Marshal(req)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPut, testHost+"/v1/objects?"+query.Encode(), bytes.NewReader(body))
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 13:46:49 +00:00
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-18 08:30:34 +00:00
|
|
|
addr := &models.Address{}
|
2022-04-13 13:46:49 +00:00
|
|
|
doRequest(t, httpClient, request, http.StatusOK, addr)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
var CID cid.ID
|
|
|
|
err = CID.Parse(*addr.ContainerID)
|
|
|
|
require.NoError(t, err)
|
2022-04-13 13:46:49 +00:00
|
|
|
var id oid.ID
|
2022-04-11 09:35:06 +00:00
|
|
|
err = id.Parse(*addr.ObjectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
objectAddress := address.NewAddress()
|
|
|
|
objectAddress.SetContainerID(&CID)
|
2022-04-13 13:46:49 +00:00
|
|
|
objectAddress.SetObjectID(&id)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
var prm pool.PrmObjectGet
|
|
|
|
prm.SetAddress(*objectAddress)
|
|
|
|
res, err := clientPool.GetObject(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
payload := bytes.NewBuffer(nil)
|
2022-04-11 09:35:06 +00:00
|
|
|
_, err = io.Copy(payload, res.Payload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, content, payload.String())
|
|
|
|
|
|
|
|
for _, attribute := range res.Header.Attributes() {
|
|
|
|
require.Equal(t, attributes[attribute.Key()], attribute.Value(), attribute.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
func restObjectGet(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID) {
|
2022-04-20 14:10:43 +00:00
|
|
|
content := []byte("some content")
|
2022-04-13 13:46:49 +00:00
|
|
|
attributes := map[string]string{
|
|
|
|
object.AttributeFileName: "get-obj-name",
|
|
|
|
"user-attribute": "user value",
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:10:43 +00:00
|
|
|
objID := createObject(ctx, t, p, cnrID, attributes, content)
|
2022-04-13 13:46:49 +00:00
|
|
|
|
|
|
|
bearer := &models.Bearer{
|
2022-04-21 11:41:50 +00:00
|
|
|
Object: []*models.Record{
|
|
|
|
{
|
|
|
|
Operation: models.NewOperation(models.OperationHEAD),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: models.NewOperation(models.OperationRANGE),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
2022-04-13 13:46:49 +00:00
|
|
|
}
|
2022-04-21 11:41:50 +00:00
|
|
|
bearer.Object = append(bearer.Object, getRestrictBearerRecords()...)
|
2022-04-13 13:46:49 +00:00
|
|
|
|
|
|
|
httpClient := defaultHTTPClient()
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-13 13:46:49 +00:00
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodGet, testHost+"/v1/objects/"+cnrID.String()+"/"+objID.String()+"?"+query.Encode(), nil)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 13:46:49 +00:00
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
|
|
|
objInfo := &models.ObjectInfo{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, objInfo)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
require.Equal(t, cnrID.String(), *objInfo.ContainerID)
|
|
|
|
require.Equal(t, objID.String(), *objInfo.ObjectID)
|
|
|
|
require.Equal(t, p.OwnerID().String(), *objInfo.OwnerID)
|
|
|
|
require.Equal(t, len(attributes), len(objInfo.Attributes))
|
2022-04-20 14:10:43 +00:00
|
|
|
require.Equal(t, int64(len(content)), *objInfo.ObjectSize)
|
|
|
|
|
|
|
|
contentData, err := base64.StdEncoding.DecodeString(objInfo.Payload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, content, contentData)
|
2022-04-13 13:46:49 +00:00
|
|
|
|
|
|
|
for _, attr := range objInfo.Attributes {
|
|
|
|
require.Equal(t, attributes[*attr.Key], *attr.Value)
|
|
|
|
}
|
2022-04-20 14:10:43 +00:00
|
|
|
|
|
|
|
// check max-payload-size params
|
|
|
|
query = make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
query.Add("max-payload-size", "0")
|
|
|
|
|
|
|
|
request, err = http.NewRequest(http.MethodGet, testHost+"/v1/objects/"+cnrID.String()+"/"+objID.String()+"?"+query.Encode(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
|
|
|
objInfo = &models.ObjectInfo{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, objInfo)
|
|
|
|
require.Empty(t, objInfo.Payload)
|
|
|
|
require.Equal(t, int64(0), *objInfo.PayloadSize)
|
|
|
|
|
|
|
|
// check range params
|
|
|
|
rangeLength := 4
|
|
|
|
query = make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
query.Add("range-offset", "0")
|
|
|
|
query.Add("range-length", strconv.Itoa(rangeLength))
|
|
|
|
|
|
|
|
request, err = http.NewRequest(http.MethodGet, testHost+"/v1/objects/"+cnrID.String()+"/"+objID.String()+"?"+query.Encode(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
|
|
|
objInfo = &models.ObjectInfo{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, objInfo)
|
|
|
|
require.Equal(t, int64(rangeLength), *objInfo.PayloadSize)
|
|
|
|
|
|
|
|
contentData, err = base64.StdEncoding.DecodeString(objInfo.Payload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, content[:rangeLength], contentData)
|
2022-04-13 13:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:53:13 +00:00
|
|
|
func restObjectDelete(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID) {
|
|
|
|
objID := createObject(ctx, t, p, cnrID, nil, []byte("some content"))
|
|
|
|
|
|
|
|
bearer := &models.Bearer{
|
|
|
|
Object: []*models.Record{{
|
|
|
|
Operation: models.NewOperation(models.OperationDELETE),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
Keys: []string{},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
2022-04-21 11:41:50 +00:00
|
|
|
bearer.Object = append(bearer.Object, getRestrictBearerRecords()...)
|
2022-04-14 08:53:13 +00:00
|
|
|
|
|
|
|
httpClient := defaultHTTPClient()
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-14 08:53:13 +00:00
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodDelete, testHost+"/v1/objects/"+cnrID.String()+"/"+objID.String()+"?"+query.Encode(), nil)
|
2022-04-14 08:53:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
2022-07-07 09:29:51 +00:00
|
|
|
resp := &models.SuccessResponse{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, resp)
|
|
|
|
require.True(t, *resp.Success)
|
2022-04-14 08:53:13 +00:00
|
|
|
|
|
|
|
var addr address.Address
|
|
|
|
addr.SetContainerID(cnrID)
|
|
|
|
addr.SetObjectID(objID)
|
|
|
|
|
|
|
|
var prm pool.PrmObjectHead
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
|
|
|
|
_, err = p.HeadObject(ctx, prm)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2022-04-18 08:30:34 +00:00
|
|
|
func restObjectsSearch(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID) {
|
|
|
|
userKey, userValue := "User-Attribute", "user-attribute-value"
|
|
|
|
objectName := "object-name"
|
|
|
|
headers := map[string]string{
|
|
|
|
object.AttributeFileName: objectName,
|
|
|
|
userKey: userValue,
|
|
|
|
}
|
|
|
|
objID := createObject(ctx, t, p, cnrID, headers, []byte("some content"))
|
|
|
|
headers[userKey] = "dummy"
|
|
|
|
_ = createObject(ctx, t, p, cnrID, headers, []byte("some content"))
|
|
|
|
|
|
|
|
bearer := &models.Bearer{
|
|
|
|
Object: []*models.Record{
|
|
|
|
{
|
|
|
|
Operation: models.NewOperation(models.OperationSEARCH),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{Role: models.NewRole(models.RoleOTHERS), Keys: []string{}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: models.NewOperation(models.OperationHEAD),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{Role: models.NewRole(models.RoleOTHERS), Keys: []string{}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: models.NewOperation(models.OperationGET),
|
|
|
|
Action: models.NewAction(models.ActionALLOW),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Targets: []*models.Target{{Role: models.NewRole(models.RoleOTHERS), Keys: []string{}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-04-21 11:41:50 +00:00
|
|
|
bearer.Object = append(bearer.Object, getRestrictBearerRecords()...)
|
2022-04-18 08:30:34 +00:00
|
|
|
|
|
|
|
httpClient := defaultHTTPClient()
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-18 08:30:34 +00:00
|
|
|
|
|
|
|
search := &models.SearchFilters{
|
|
|
|
Filters: []*models.SearchFilter{
|
|
|
|
{
|
2022-04-29 06:39:24 +00:00
|
|
|
Key: util.NewString(userKey),
|
2022-04-18 08:30:34 +00:00
|
|
|
Match: models.NewSearchMatch(models.SearchMatchMatchStringEqual),
|
2022-04-29 06:39:24 +00:00
|
|
|
Value: util.NewString(userValue),
|
2022-04-18 08:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(search)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPost, testHost+"/v1/objects/"+cnrID.String()+"/search?"+query.Encode(), bytes.NewReader(body))
|
|
|
|
require.NoError(t, err)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
|
|
|
resp := &models.ObjectList{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, resp)
|
|
|
|
|
|
|
|
require.Equal(t, 1, int(*resp.Size))
|
|
|
|
require.Len(t, resp.Objects, 1)
|
|
|
|
|
|
|
|
objBaseInfo := resp.Objects[0]
|
|
|
|
require.Equal(t, cnrID.String(), *objBaseInfo.Address.ContainerID)
|
|
|
|
require.Equal(t, objID.String(), *objBaseInfo.Address.ObjectID)
|
|
|
|
require.Equal(t, objectName, objBaseInfo.Name)
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
func doRequest(t *testing.T, httpClient *http.Client, request *http.Request, expectedCode int, model interface{}) {
|
2022-04-11 09:35:06 +00:00
|
|
|
resp, err := httpClient.Do(request)
|
|
|
|
require.NoError(t, err)
|
2022-04-13 13:46:49 +00:00
|
|
|
defer func() {
|
|
|
|
err := resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
if expectedCode != resp.StatusCode {
|
|
|
|
fmt.Println("resp", string(respBody))
|
|
|
|
}
|
|
|
|
require.Equal(t, expectedCode, resp.StatusCode)
|
|
|
|
|
|
|
|
if model == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(respBody, model)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 13:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func restContainerGet(ctx context.Context, t *testing.T, clientPool *pool.Pool, cnrID *cid.ID) {
|
|
|
|
httpClient := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
request, err := http.NewRequest(http.MethodGet, testHost+"/v1/containers/"+cnrID.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
request = request.WithContext(ctx)
|
|
|
|
|
|
|
|
cnrInfo := &models.ContainerInfo{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, cnrInfo)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
require.Equal(t, cnrID.String(), *cnrInfo.ContainerID)
|
|
|
|
require.Equal(t, clientPool.OwnerID().String(), *cnrInfo.OwnerID)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
func restContainerDelete(ctx context.Context, t *testing.T, clientPool *pool.Pool) {
|
|
|
|
cnrID := createContainer(ctx, t, clientPool, "for-delete")
|
|
|
|
|
|
|
|
bearer := &models.Bearer{
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbDELETE),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
httpClient := defaultHTTPClient()
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-13 08:41:04 +00:00
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodDelete, testHost+"/v1/containers/"+cnrID.String()+"?"+query.Encode(), nil)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 08:41:04 +00:00
|
|
|
request = request.WithContext(ctx)
|
2022-04-13 13:00:04 +00:00
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
2022-04-13 08:41:04 +00:00
|
|
|
|
2022-07-07 09:29:51 +00:00
|
|
|
resp := &models.SuccessResponse{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, resp)
|
|
|
|
require.True(t, *resp.Success)
|
2022-04-13 08:41:04 +00:00
|
|
|
|
|
|
|
var prm pool.PrmContainerGet
|
|
|
|
prm.SetContainerID(*cnrID)
|
|
|
|
|
|
|
|
_, err = clientPool.GetContainer(ctx, prm)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "not found")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 13:00:04 +00:00
|
|
|
func restContainerEACLPut(ctx context.Context, t *testing.T, clientPool *pool.Pool) {
|
|
|
|
cnrID := createContainer(ctx, t, clientPool, "for-eacl-put")
|
|
|
|
httpClient := &http.Client{Timeout: 60 * time.Second}
|
|
|
|
bearer := &models.Bearer{
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbSETEACL),
|
|
|
|
},
|
|
|
|
}
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
req := models.Eacl{
|
|
|
|
Records: []*models.Record{{
|
|
|
|
Action: models.NewAction(models.ActionDENY),
|
|
|
|
Filters: []*models.Filter{},
|
|
|
|
Operation: models.NewOperation(models.OperationDELETE),
|
|
|
|
Targets: []*models.Target{{
|
|
|
|
Keys: []string{},
|
|
|
|
Role: models.NewRole(models.RoleOTHERS),
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(&req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPut, testHost+"/v1/containers/"+cnrID.String()+"/eacl?"+query.Encode(), bytes.NewReader(body))
|
2022-04-13 13:00:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
request = request.WithContext(ctx)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
2022-07-07 09:29:51 +00:00
|
|
|
resp := &models.SuccessResponse{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, resp)
|
|
|
|
require.True(t, *resp.Success)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
var prm pool.PrmContainerEACL
|
|
|
|
prm.SetContainerID(*cnrID)
|
|
|
|
|
|
|
|
table, err := clientPool.GetEACL(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-29 06:39:24 +00:00
|
|
|
expectedTable, err := util.ToNativeTable(req.Records)
|
2022-04-13 13:00:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
expectedTable.SetCID(cnrID)
|
|
|
|
|
|
|
|
require.True(t, eacl.EqualTables(*expectedTable, *table))
|
|
|
|
}
|
|
|
|
|
2022-04-14 08:53:13 +00:00
|
|
|
func restContainerEACLGet(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID) {
|
|
|
|
var prm pool.PrmContainerEACL
|
|
|
|
prm.SetContainerID(*cnrID)
|
|
|
|
expectedTable, err := p.GetEACL(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: 60 * time.Second}
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodGet, testHost+"/v1/containers/"+cnrID.String()+"/eacl", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
request = request.WithContext(ctx)
|
|
|
|
|
|
|
|
responseTable := &models.Eacl{}
|
2022-04-13 13:46:49 +00:00
|
|
|
doRequest(t, httpClient, request, http.StatusOK, responseTable)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
require.Equal(t, cnrID.String(), responseTable.ContainerID)
|
|
|
|
|
2022-04-29 06:39:24 +00:00
|
|
|
actualTable, err := util.ToNativeTable(responseTable.Records)
|
2022-04-13 13:00:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
actualTable.SetCID(cnrID)
|
|
|
|
|
|
|
|
require.True(t, eacl.EqualTables(*expectedTable, *actualTable))
|
|
|
|
}
|
|
|
|
|
2022-04-13 15:23:03 +00:00
|
|
|
func restContainerList(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID) {
|
|
|
|
var prm pool.PrmContainerList
|
|
|
|
prm.SetOwnerID(*p.OwnerID())
|
|
|
|
|
|
|
|
ids, err := p.ListContainers(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
httpClient := defaultHTTPClient()
|
|
|
|
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Add("ownerId", p.OwnerID().String())
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodGet, testHost+"/v1/containers?"+query.Encode(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
request = request.WithContext(ctx)
|
|
|
|
|
|
|
|
list := &models.ContainerList{}
|
|
|
|
doRequest(t, httpClient, request, http.StatusOK, list)
|
|
|
|
|
|
|
|
require.Equal(t, len(ids), int(*list.Size))
|
|
|
|
|
|
|
|
expected := &models.ContainerBaseInfo{
|
2022-04-29 06:39:24 +00:00
|
|
|
ContainerID: util.NewString(cnrID.String()),
|
2022-04-13 15:23:03 +00:00
|
|
|
Name: containerName,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(t, list.Containers, expected)
|
|
|
|
}
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
func makeAuthTokenRequest(ctx context.Context, t *testing.T, bearers []*models.Bearer, httpClient *http.Client) []*handlers.BearerToken {
|
2022-04-11 09:35:06 +00:00
|
|
|
key, err := keys.NewPrivateKeyFromHex(devenvPrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-09 09:59:02 +00:00
|
|
|
ownerID := owner.NewIDFromPublicKey((*ecdsa.PublicKey)(key.PublicKey()))
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
data, err := json.Marshal(bearers)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
request, err := http.NewRequest(http.MethodPost, testHost+"/v1/auth", bytes.NewReader(data))
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 08:41:04 +00:00
|
|
|
request = request.WithContext(ctx)
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
2022-06-09 09:59:02 +00:00
|
|
|
request.Header.Add(XBearerOwnerID, ownerID.String())
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
resp, err := httpClient.Do(request)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 08:41:04 +00:00
|
|
|
defer func() {
|
|
|
|
err := resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
rr, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
2022-04-13 08:41:04 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
fmt.Println("auth response", string(rr))
|
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
var stokenResp []*models.TokenResponse
|
|
|
|
err = json.Unmarshal(rr, &stokenResp)
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
fmt.Println("resp tokens:")
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
respTokens := make([]*handlers.BearerToken, len(stokenResp))
|
|
|
|
for i, tok := range stokenResp {
|
|
|
|
isObject, err := handlers.IsObjectToken(bearers[i])
|
|
|
|
require.NoError(t, err)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
require.Equal(t, bearers[i].Name, tok.Name)
|
|
|
|
|
|
|
|
if isObject {
|
|
|
|
require.Equal(t, models.TokenTypeObject, *tok.Type)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, models.TokenTypeContainer, *tok.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
binaryData, err := base64.StdEncoding.DecodeString(*tok.Token)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var bt *handlers.BearerToken
|
|
|
|
if useWalletConnect {
|
|
|
|
bt = signTokenWalletConnect(t, key, binaryData)
|
|
|
|
} else {
|
|
|
|
bt = signToken(t, key, binaryData)
|
|
|
|
}
|
|
|
|
|
|
|
|
respTokens[i] = bt
|
|
|
|
fmt.Printf("%+v\n", bt)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:02:05 +00:00
|
|
|
return respTokens
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
func signToken(t *testing.T, key *keys.PrivateKey, data []byte) *handlers.BearerToken {
|
2022-04-13 08:41:04 +00:00
|
|
|
h := sha512.Sum512(data)
|
|
|
|
x, y, err := ecdsa.Sign(rand.Reader, &key.PrivateKey, h[:])
|
|
|
|
require.NoError(t, err)
|
2022-04-15 07:03:00 +00:00
|
|
|
sign := elliptic.Marshal(elliptic.P256(), x, y)
|
|
|
|
|
|
|
|
return &handlers.BearerToken{
|
|
|
|
Token: base64.StdEncoding.EncodeToString(data),
|
2022-06-16 09:12:26 +00:00
|
|
|
Signature: hex.EncodeToString(sign),
|
2022-04-15 07:03:00 +00:00
|
|
|
Key: hex.EncodeToString(key.PublicKey().Bytes()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func signTokenWalletConnect(t *testing.T, key *keys.PrivateKey, data []byte) *handlers.BearerToken {
|
|
|
|
sm, err := walletconnect.SignMessage(&key.PrivateKey, data[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &handlers.BearerToken{
|
|
|
|
Token: base64.StdEncoding.EncodeToString(data),
|
2022-06-16 09:12:26 +00:00
|
|
|
Signature: hex.EncodeToString(append(sm.Data, sm.Salt...)),
|
2022-04-15 07:03:00 +00:00
|
|
|
Key: hex.EncodeToString(key.PublicKey().Bytes()),
|
|
|
|
}
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func restContainerPut(ctx context.Context, t *testing.T, clientPool *pool.Pool) {
|
|
|
|
bearer := &models.Bearer{
|
|
|
|
Container: &models.Rule{
|
|
|
|
Verb: models.NewVerb(models.VerbPUT),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: 30 * time.Second}
|
2022-07-07 09:02:05 +00:00
|
|
|
bearerTokens := makeAuthTokenRequest(ctx, t, []*models.Bearer{bearer}, httpClient)
|
|
|
|
bearerToken := bearerTokens[0]
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
attrKey, attrValue := "User-Attribute", "user value"
|
|
|
|
userAttributes := map[string]string{
|
|
|
|
attrKey: attrValue,
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:32:28 +00:00
|
|
|
// try to create container without name but with name-scope-global
|
|
|
|
body, err := json.Marshal(&operations.PutContainerBody{})
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
reqURL, err := url.Parse(testHost + "/v1/containers")
|
|
|
|
require.NoError(t, err)
|
|
|
|
query := reqURL.Query()
|
2022-06-15 14:32:28 +00:00
|
|
|
query.Add("name-scope-global", "true")
|
2022-04-15 07:03:00 +00:00
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
2022-04-13 08:41:04 +00:00
|
|
|
reqURL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPut, reqURL.String(), bytes.NewReader(body))
|
2022-06-15 14:32:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
|
|
|
|
|
|
|
doRequest(t, httpClient, request, http.StatusBadRequest, nil)
|
|
|
|
|
|
|
|
// create container with name in local scope
|
|
|
|
body, err = json.Marshal(&operations.PutContainerBody{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reqURL, err = url.Parse(testHost + "/v1/containers")
|
|
|
|
require.NoError(t, err)
|
|
|
|
query = reqURL.Query()
|
|
|
|
query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect))
|
|
|
|
reqURL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
request, err = http.NewRequest(http.MethodPut, reqURL.String(), bytes.NewReader(body))
|
2022-04-11 09:35:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-13 13:00:04 +00:00
|
|
|
prepareCommonHeaders(request.Header, bearerToken)
|
2022-04-11 09:35:06 +00:00
|
|
|
request.Header.Add("X-Attribute-"+attrKey, attrValue)
|
|
|
|
|
|
|
|
addr := &operations.PutContainerOKBody{}
|
2022-04-13 13:46:49 +00:00
|
|
|
doRequest(t, httpClient, request, http.StatusOK, addr)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
var CID cid.ID
|
|
|
|
err = CID.Parse(*addr.ContainerID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fmt.Println(CID.String())
|
|
|
|
|
|
|
|
var prm pool.PrmContainerGet
|
|
|
|
prm.SetContainerID(CID)
|
|
|
|
|
|
|
|
cnr, err := clientPool.GetContainer(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cnrAttr := make(map[string]string, len(cnr.Attributes()))
|
|
|
|
for _, attribute := range cnr.Attributes() {
|
|
|
|
cnrAttr[attribute.Key()] = attribute.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range userAttributes {
|
|
|
|
require.Equal(t, val, cnrAttr[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:00:04 +00:00
|
|
|
func prepareCommonHeaders(header http.Header, bearerToken *handlers.BearerToken) {
|
|
|
|
header.Add("Content-Type", "application/json")
|
2022-04-14 08:53:13 +00:00
|
|
|
header.Add(XBearerSignature, bearerToken.Signature)
|
2022-04-13 08:41:04 +00:00
|
|
|
header.Add("Authorization", "Bearer "+bearerToken.Token)
|
2022-04-14 08:53:13 +00:00
|
|
|
header.Add(XBearerSignatureKey, bearerToken.Key)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createContainer(ctx context.Context, t *testing.T, clientPool *pool.Pool, name string) *cid.ID {
|
2022-04-11 09:35:06 +00:00
|
|
|
pp, err := policy.Parse("REP 1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cnr := container.New(
|
|
|
|
container.WithPolicy(pp),
|
|
|
|
container.WithCustomBasicACL(0x0FFFFFFF),
|
2022-04-13 08:41:04 +00:00
|
|
|
container.WithAttribute(container.AttributeName, name),
|
2022-04-11 09:35:06 +00:00
|
|
|
container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)))
|
|
|
|
cnr.SetOwnerID(clientPool.OwnerID())
|
|
|
|
|
|
|
|
var waitPrm pool.WaitParams
|
|
|
|
waitPrm.SetPollInterval(3 * time.Second)
|
|
|
|
waitPrm.SetTimeout(15 * time.Second)
|
|
|
|
|
|
|
|
var prm pool.PrmContainerPut
|
|
|
|
prm.SetContainer(*cnr)
|
|
|
|
prm.SetWaitParams(waitPrm)
|
|
|
|
|
|
|
|
CID, err := clientPool.PutContainer(ctx, prm)
|
2022-04-13 08:41:04 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
return CID
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 13:46:49 +00:00
|
|
|
func createObject(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *cid.ID, headers map[string]string, payload []byte) *oid.ID {
|
|
|
|
attributes := make([]object.Attribute, 0, len(headers))
|
|
|
|
|
|
|
|
for key, val := range headers {
|
|
|
|
attr := object.NewAttribute()
|
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
|
|
|
attributes = append(attributes, *attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.New()
|
|
|
|
obj.SetOwnerID(p.OwnerID())
|
|
|
|
obj.SetContainerID(cnrID)
|
|
|
|
obj.SetAttributes(attributes...)
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
var prm pool.PrmObjectPut
|
|
|
|
prm.SetHeader(*obj)
|
|
|
|
|
|
|
|
objID, err := p.PutObject(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return objID
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:00:04 +00:00
|
|
|
func restrictByEACL(ctx context.Context, t *testing.T, clientPool *pool.Pool, cnrID *cid.ID) *eacl.Table {
|
|
|
|
table := eacl.NewTable()
|
2022-04-11 09:35:06 +00:00
|
|
|
table.SetCID(cnrID)
|
|
|
|
|
|
|
|
for op := eacl.OperationGet; op <= eacl.OperationRangeHash; op++ {
|
|
|
|
record := new(eacl.Record)
|
|
|
|
record.SetOperation(op)
|
|
|
|
record.SetAction(eacl.ActionDeny)
|
|
|
|
target := new(eacl.Target)
|
|
|
|
target.SetRole(eacl.RoleOthers)
|
|
|
|
record.SetTargets(*target)
|
|
|
|
table.AddRecord(record)
|
|
|
|
}
|
|
|
|
|
|
|
|
var waitPrm pool.WaitParams
|
|
|
|
waitPrm.SetPollInterval(3 * time.Second)
|
|
|
|
waitPrm.SetTimeout(15 * time.Second)
|
|
|
|
|
|
|
|
var prm pool.PrmContainerSetEACL
|
|
|
|
prm.SetTable(*table)
|
|
|
|
prm.SetWaitParams(waitPrm)
|
|
|
|
|
|
|
|
err := clientPool.SetEACL(ctx, prm)
|
|
|
|
require.NoError(t, err)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
return table
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|