2022-04-11 09:35:06 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2022-06-16 09:12:26 +00:00
|
|
|
"encoding/hex"
|
2022-07-07 14:24:41 +00:00
|
|
|
"errors"
|
2022-04-11 09:35:06 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-07-19 14:48:28 +00:00
|
|
|
containerv2 "github.com/nspcc-dev/neofs-api-go/v2/container"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
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"
|
2022-04-29 06:39:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/internal/util"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2022-07-12 08:01:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container/acl"
|
2022-04-11 09:35:06 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-07-12 08:01:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2022-07-12 08:01:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-07-21 15:04:11 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2022-04-13 08:41:04 +00:00
|
|
|
"go.uber.org/zap"
|
2022-04-11 09:35:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPlacementPolicy = "REP 3"
|
2022-07-12 08:01:21 +00:00
|
|
|
defaultBasicACL = acl.NamePrivate
|
|
|
|
attributeName = "Name"
|
|
|
|
attributeTimestamp = "Timestamp"
|
2022-04-11 09:35:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutContainers handler that creates container in NeoFS.
|
|
|
|
func (a *API) PutContainers(params operations.PutContainerParams, principal *models.Principal) middleware.Responder {
|
2022-07-07 14:24:41 +00:00
|
|
|
st := &SessionToken{
|
|
|
|
BearerToken: BearerToken{
|
|
|
|
Token: string(*principal),
|
|
|
|
Signature: params.XBearerSignature,
|
|
|
|
Key: params.XBearerSignatureKey,
|
|
|
|
},
|
|
|
|
Verb: sessionv2.ContainerVerbPut,
|
|
|
|
}
|
|
|
|
stoken, err := prepareSessionToken(st, *params.WalletConnect)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid session token", err)
|
|
|
|
return operations.NewPutContainerBadRequest().WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:48:28 +00:00
|
|
|
cnrID, err := createContainer(params.HTTPRequest.Context(), a.pool, stoken, ¶ms)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("create container", err)
|
|
|
|
return operations.NewPutContainerBadRequest().WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp operations.PutContainerOKBody
|
2022-07-12 08:01:21 +00:00
|
|
|
resp.ContainerID = util.NewString(cnrID.EncodeToString())
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewPutContainerOK().
|
|
|
|
WithPayload(&resp).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainer handler that returns container info.
|
|
|
|
func (a *API) GetContainer(params operations.GetContainerParams) middleware.Responder {
|
2022-07-07 14:55:13 +00:00
|
|
|
cnrID, err := parseContainerID(params.ContainerID)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 14:55:13 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return operations.NewGetContainerBadRequest().WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
cnrInfo, err := getContainerInfo(params.HTTPRequest.Context(), a.pool, cnrID)
|
2022-07-07 14:55:13 +00:00
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("get container", err)
|
|
|
|
return operations.NewGetContainerBadRequest().WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewGetContainerOK().
|
|
|
|
WithPayload(cnrInfo).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 13:00:04 +00:00
|
|
|
// PutContainerEACL handler that update container eacl.
|
|
|
|
func (a *API) PutContainerEACL(params operations.PutContainerEACLParams, principal *models.Principal) middleware.Responder {
|
|
|
|
cnrID, err := parseContainerID(params.ContainerID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return operations.NewPutContainerEACLBadRequest().WithPayload(resp)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:24:41 +00:00
|
|
|
st := &SessionToken{
|
|
|
|
BearerToken: BearerToken{
|
|
|
|
Token: string(*principal),
|
|
|
|
Signature: params.XBearerSignature,
|
|
|
|
Key: params.XBearerSignatureKey,
|
|
|
|
},
|
|
|
|
Verb: sessionv2.ContainerVerbSetEACL,
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
2022-07-07 14:24:41 +00:00
|
|
|
stoken, err := prepareSessionToken(st, *params.WalletConnect)
|
2022-04-13 13:00:04 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid session token", err)
|
|
|
|
return operations.NewPutContainerEACLBadRequest().WithPayload(resp)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = setContainerEACL(params.HTTPRequest.Context(), a.pool, cnrID, stoken, params.Eacl); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed set container eacl", err)
|
|
|
|
return operations.NewPutContainerEACLBadRequest().WithPayload(resp)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewPutContainerEACLOK().
|
|
|
|
WithPayload(util.NewSuccessResponse()).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainerEACL handler that returns container eacl.
|
|
|
|
func (a *API) GetContainerEACL(params operations.GetContainerEACLParams) middleware.Responder {
|
|
|
|
cnrID, err := parseContainerID(params.ContainerID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return operations.NewGetContainerEACLBadRequest().WithPayload(resp)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := getContainerEACL(params.HTTPRequest.Context(), a.pool, cnrID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
errResp := a.logAndGetErrorResponse("failed to get container eacl", err)
|
|
|
|
return operations.NewGetContainerEACLBadRequest().WithPayload(errResp)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewGetContainerEACLOK().
|
|
|
|
WithPayload(resp).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 15:23:03 +00:00
|
|
|
// ListContainer handler that returns containers.
|
|
|
|
func (a *API) ListContainer(params operations.ListContainersParams) middleware.Responder {
|
|
|
|
ctx := params.HTTPRequest.Context()
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var ownerID user.ID
|
|
|
|
if err := ownerID.DecodeString(params.OwnerID); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid owner id", err)
|
|
|
|
return operations.NewListContainersBadRequest().WithPayload(resp)
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmContainerList
|
|
|
|
prm.SetOwnerID(ownerID)
|
|
|
|
|
|
|
|
ids, err := a.pool.ListContainers(ctx, prm)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("list containers", err)
|
|
|
|
return operations.NewListContainersBadRequest().WithPayload(resp)
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset := int(*params.Offset)
|
|
|
|
size := int(*params.Limit)
|
|
|
|
|
|
|
|
if offset > len(ids)-1 {
|
|
|
|
res := &models.ContainerList{
|
2022-04-29 06:39:24 +00:00
|
|
|
Size: util.NewInteger(0),
|
2022-07-07 14:55:13 +00:00
|
|
|
Containers: []*models.ContainerInfo{},
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewListContainersOK().
|
|
|
|
WithPayload(res).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset+size > len(ids) {
|
|
|
|
size = len(ids) - offset
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &models.ContainerList{
|
2022-04-29 06:39:24 +00:00
|
|
|
Size: util.NewInteger(int64(size)),
|
2022-07-07 14:55:13 +00:00
|
|
|
Containers: make([]*models.ContainerInfo, 0, size),
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids[offset : offset+size] {
|
2022-07-07 14:55:13 +00:00
|
|
|
cnrInfo, err := getContainerInfo(ctx, a.pool, id)
|
2022-04-13 15:23:03 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("get container", err, zap.String("cid", id.String()))
|
|
|
|
return operations.NewListContainersBadRequest().WithPayload(resp)
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
2022-07-07 14:55:13 +00:00
|
|
|
res.Containers = append(res.Containers, cnrInfo)
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewListContainersOK().
|
|
|
|
WithPayload(res).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 08:41:04 +00:00
|
|
|
// DeleteContainer handler that returns container info.
|
|
|
|
func (a *API) DeleteContainer(params operations.DeleteContainerParams, principal *models.Principal) middleware.Responder {
|
2022-07-07 14:24:41 +00:00
|
|
|
st := &SessionToken{
|
|
|
|
BearerToken: BearerToken{
|
|
|
|
Token: string(*principal),
|
|
|
|
Signature: params.XBearerSignature,
|
|
|
|
Key: params.XBearerSignatureKey,
|
|
|
|
},
|
|
|
|
Verb: sessionv2.ContainerVerbDelete,
|
|
|
|
}
|
|
|
|
stoken, err := prepareSessionToken(st, *params.WalletConnect)
|
2022-04-13 08:41:04 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid session token", err)
|
|
|
|
return operations.NewDeleteContainerBadRequest().WithPayload(resp)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnrID, err := parseContainerID(params.ContainerID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return operations.NewDeleteContainerBadRequest().WithPayload(resp)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmContainerDelete
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetContainerID(cnrID)
|
|
|
|
prm.SetSessionToken(stoken)
|
2022-04-13 08:41:04 +00:00
|
|
|
|
|
|
|
if err = a.pool.DeleteContainer(params.HTTPRequest.Context(), prm); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("delete container", err, zap.String("container", params.ContainerID))
|
|
|
|
return operations.NewDeleteContainerBadRequest().WithPayload(resp)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewDeleteContainerOK().
|
|
|
|
WithPayload(util.NewSuccessResponse()).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:55:13 +00:00
|
|
|
func getContainerInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*models.ContainerInfo, error) {
|
2022-04-13 15:23:03 +00:00
|
|
|
var prm pool.PrmContainerGet
|
|
|
|
prm.SetContainerID(cnrID)
|
|
|
|
|
|
|
|
cnr, err := p.GetContainer(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var attrs []*models.Attribute
|
|
|
|
cnr.IterateAttributes(func(key, val string) {
|
|
|
|
attrs = append(attrs, &models.Attribute{
|
|
|
|
Key: util.NewString(key),
|
|
|
|
Value: util.NewString(val),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var sb strings.Builder
|
|
|
|
if err = cnr.PlacementPolicy().WriteStringTo(&sb); err != nil {
|
|
|
|
return nil, fmt.Errorf("writer policy to string: %w", err)
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:55:13 +00:00
|
|
|
return &models.ContainerInfo{
|
|
|
|
ContainerID: util.NewString(cnrID.String()),
|
2022-07-19 14:48:28 +00:00
|
|
|
ContainerName: util.NewString(container.Name(*cnr)),
|
2022-07-12 08:01:21 +00:00
|
|
|
OwnerID: util.NewString(cnr.Owner().String()),
|
|
|
|
BasicACL: util.NewString(cnr.BasicACL().EncodeToString()),
|
2022-08-17 09:22:59 +00:00
|
|
|
CannedACL: friendlyBasicACL(cnr.BasicACL()),
|
2022-07-12 08:01:21 +00:00
|
|
|
PlacementPolicy: util.NewString(sb.String()),
|
2022-07-07 14:55:13 +00:00
|
|
|
Attributes: attrs,
|
2022-07-21 15:04:11 +00:00
|
|
|
Version: util.NewString(getContainerVersion(cnr).String()),
|
2022-07-07 14:55:13 +00:00
|
|
|
}, nil
|
2022-04-13 15:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 09:22:59 +00:00
|
|
|
func friendlyBasicACL(basicACL acl.Basic) string {
|
|
|
|
switch basicACL {
|
|
|
|
case acl.Private:
|
|
|
|
return acl.NamePrivate
|
|
|
|
case acl.PrivateExtended:
|
|
|
|
return acl.NamePrivateExtended
|
|
|
|
case acl.PublicRO:
|
|
|
|
return acl.NamePublicRO
|
|
|
|
case acl.PublicROExtended:
|
|
|
|
return acl.NamePublicROExtended
|
|
|
|
case acl.PublicRW:
|
|
|
|
return acl.NamePublicRW
|
|
|
|
case acl.PublicRWExtended:
|
|
|
|
return acl.NamePublicRWExtended
|
|
|
|
case acl.PublicAppend:
|
|
|
|
return acl.NamePublicAppend
|
|
|
|
case acl.PublicAppendExtended:
|
|
|
|
return acl.NamePublicAppendExtended
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:04:11 +00:00
|
|
|
func getContainerVersion(cnr *container.Container) version.Version {
|
|
|
|
var v2cnr containerv2.Container
|
|
|
|
cnr.WriteToV2(&v2cnr)
|
|
|
|
|
|
|
|
var cnrVersion version.Version
|
|
|
|
v2version := v2cnr.GetVersion()
|
|
|
|
if v2version != nil {
|
|
|
|
cnrVersion = version.Version(*v2version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnrVersion
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func parseContainerID(containerID string) (cid.ID, error) {
|
2022-04-13 08:41:04 +00:00
|
|
|
var cnrID cid.ID
|
2022-07-12 08:01:21 +00:00
|
|
|
if err := cnrID.DecodeString(containerID); err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("parse container id '%s': %w", containerID, err)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
return cnrID, nil
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func setContainerEACL(ctx context.Context, p *pool.Pool, cnrID cid.ID, stoken session.Container, eaclPrm *models.Eacl) error {
|
2022-04-29 06:39:24 +00:00
|
|
|
table, err := util.ToNativeTable(eaclPrm.Records)
|
2022-04-13 13:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
table.SetCID(cnrID)
|
|
|
|
|
|
|
|
var prm pool.PrmContainerSetEACL
|
|
|
|
prm.SetTable(*table)
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.WithinSession(stoken)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
return p.SetEACL(ctx, prm)
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func getContainerEACL(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*models.Eacl, error) {
|
2022-04-13 13:00:04 +00:00
|
|
|
var prm pool.PrmContainerEACL
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetContainerID(cnrID)
|
2022-04-13 13:00:04 +00:00
|
|
|
|
|
|
|
table, err := p.GetEACL(ctx, prm)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
return nil, fmt.Errorf("get eacl: %w", err)
|
2022-04-13 13:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tableResp := &models.Eacl{
|
2022-07-12 08:01:21 +00:00
|
|
|
ContainerID: cnrID.EncodeToString(),
|
2022-04-13 13:00:04 +00:00
|
|
|
Records: make([]*models.Record, len(table.Records())),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, rec := range table.Records() {
|
2022-04-29 06:39:24 +00:00
|
|
|
record, err := util.FromNativeRecord(rec)
|
2022-04-13 13:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't transform record from native: %w", err)
|
|
|
|
}
|
|
|
|
tableResp.Records[i] = record
|
|
|
|
}
|
|
|
|
|
|
|
|
return tableResp, nil
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:48:28 +00:00
|
|
|
func createContainer(ctx context.Context, p *pool.Pool, stoken session.Container, params *operations.PutContainerParams) (cid.ID, error) {
|
2022-04-13 08:41:04 +00:00
|
|
|
request := params.Container
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
if request.PlacementPolicy == "" {
|
|
|
|
request.PlacementPolicy = defaultPlacementPolicy
|
|
|
|
}
|
2022-07-12 08:01:21 +00:00
|
|
|
var policy netmap.PlacementPolicy
|
|
|
|
err := policy.DecodeString(request.PlacementPolicy)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("couldn't parse placement policy: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.BasicACL == "" {
|
|
|
|
request.BasicACL = defaultBasicACL
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var basicACL acl.Basic
|
|
|
|
if err = basicACL.DecodeString(request.BasicACL); err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("couldn't parse basic acl: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
cnr.SetPlacementPolicy(policy)
|
|
|
|
cnr.SetBasicACL(basicACL)
|
|
|
|
cnr.SetOwner(stoken.Issuer())
|
|
|
|
|
|
|
|
container.SetCreationTime(&cnr, time.Now())
|
|
|
|
|
2022-06-15 14:32:28 +00:00
|
|
|
if request.ContainerName != "" {
|
2022-07-12 08:01:21 +00:00
|
|
|
container.SetName(&cnr, request.ContainerName)
|
2022-06-15 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:48:28 +00:00
|
|
|
for _, attr := range request.Attributes {
|
|
|
|
switch *attr.Key {
|
|
|
|
case attributeName, attributeTimestamp,
|
|
|
|
containerv2.SysAttributeName, containerv2.SysAttributeZone:
|
|
|
|
default:
|
|
|
|
cnr.SetAttribute(*attr.Key, *attr.Value)
|
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 14:32:28 +00:00
|
|
|
if *params.NameScopeGlobal { // we don't check for nil because there is default false value
|
2022-07-07 13:33:36 +00:00
|
|
|
if err = checkNNSContainerName(request.ContainerName); err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("invalid container name: %w", err)
|
2022-06-15 14:32:28 +00:00
|
|
|
}
|
2022-07-12 08:01:21 +00:00
|
|
|
|
|
|
|
var domain container.Domain
|
|
|
|
domain.SetName(request.ContainerName)
|
|
|
|
container.WriteDomain(&cnr, domain)
|
2022-04-13 08:41:04 +00:00
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
var prm pool.PrmContainerPut
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetContainer(cnr)
|
|
|
|
prm.WithinSession(stoken)
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
cnrID, err := p.PutContainer(ctx, prm)
|
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("put container: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
return *cnrID, nil
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:33:36 +00:00
|
|
|
func checkNNSContainerName(name string) error {
|
|
|
|
length := len(name)
|
|
|
|
if length < 3 || 255 < length {
|
|
|
|
return fmt.Errorf("invalid length: %d", length)
|
|
|
|
}
|
|
|
|
fragments := strings.Split(name, ".")
|
|
|
|
|
|
|
|
for _, fragment := range fragments {
|
|
|
|
fLength := len(fragment)
|
|
|
|
if fLength < 1 || 63 < fLength {
|
|
|
|
return fmt.Errorf("invalid fragment length: %d", fLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAlNum(fragment[0]) || !isAlNum(fragment[fLength-1]) {
|
|
|
|
return fmt.Errorf("invalid fragment: '%s'", fragment)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < fLength-1; i++ {
|
|
|
|
if fragment[i] != '-' && !isAlNum(fragment[i]) {
|
|
|
|
return fmt.Errorf("invalid fragment: '%s'", fragment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAlNum(c uint8) bool {
|
|
|
|
return c >= 'a' && c <= 'z' || c >= '0' && c <= '9'
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func prepareSessionToken(st *SessionToken, isWalletConnect bool) (session.Container, error) {
|
2022-07-07 14:24:41 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(st.Token)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, fmt.Errorf("can't base64-decode session token: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:24:41 +00:00
|
|
|
signature, err := hex.DecodeString(st.Signature)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, fmt.Errorf("couldn't decode signature: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:24:41 +00:00
|
|
|
ownerKey, err := keys.NewPublicKeyFromString(st.Key)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, fmt.Errorf("couldn't fetch session token owner key: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
body := new(sessionv2.TokenBody)
|
|
|
|
if err = body.Unmarshal(data); err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, fmt.Errorf("can't unmarshal session token: %w", err)
|
2022-07-07 14:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sessionContext, ok := body.GetContext().(*sessionv2.ContainerSessionContext); !ok {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, errors.New("expected container session context but got something different")
|
2022-07-07 14:24:41 +00:00
|
|
|
} else if sessionContext.Verb() != st.Verb {
|
2022-07-12 08:01:21 +00:00
|
|
|
return session.Container{}, fmt.Errorf("invalid container session verb '%s', expected: '%s'", sessionContext.Verb().String(), st.Verb.String())
|
2022-04-15 07:03:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
v2signature := new(refs.Signature)
|
|
|
|
v2signature.SetScheme(refs.ECDSA_SHA512)
|
2022-04-15 07:03:00 +00:00
|
|
|
if isWalletConnect {
|
2022-07-12 08:01:21 +00:00
|
|
|
v2signature.SetScheme(refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT)
|
2022-04-15 07:03:00 +00:00
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
v2signature.SetSign(signature)
|
|
|
|
v2signature.SetKey(ownerKey.Bytes())
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var v2token sessionv2.Token
|
|
|
|
v2token.SetBody(body)
|
|
|
|
v2token.SetSignature(v2signature)
|
|
|
|
|
|
|
|
var stoken session.Container
|
|
|
|
if err = stoken.ReadFromV2(v2token); err != nil {
|
|
|
|
return session.Container{}, fmt.Errorf("read from v2 token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stoken.VerifySignature() {
|
|
|
|
return session.Container{}, fmt.Errorf("invalid signature")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stoken, err
|
|
|
|
}
|