2020-08-25 11:52:18 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-09-10 15:08:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
2020-08-25 11:52:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-25 11:52:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/client"
|
|
|
|
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-25 11:52:18 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-09-02 11:58:28 +00:00
|
|
|
type delContainerSignWrapper struct {
|
|
|
|
body *v2container.DeleteRequestBody
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
// EACLWithSignature represents eACL table/signature pair.
|
|
|
|
type EACLWithSignature struct {
|
|
|
|
table *eacl.Table
|
|
|
|
|
|
|
|
sig *pkg.Signature
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:10:51 +00:00
|
|
|
var errNilReponseBody = errors.New("response body is nil")
|
|
|
|
|
2020-09-02 11:58:28 +00:00
|
|
|
func (c delContainerSignWrapper) ReadSignedData(bytes []byte) ([]byte, error) {
|
|
|
|
return c.body.GetContainerID().GetValue(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c delContainerSignWrapper) SignedDataSize() int {
|
|
|
|
return len(c.body.GetContainerID().GetValue())
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
// EACL returns eACL table.
|
|
|
|
func (e EACLWithSignature) EACL() *eacl.Table {
|
|
|
|
return e.table
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signature returns table signature.
|
|
|
|
func (e EACLWithSignature) Signature() *pkg.Signature {
|
|
|
|
return e.sig
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-08-25 11:52:18 +00:00
|
|
|
case 2:
|
|
|
|
return c.putContainerV2(ctx, cnr, opts...)
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-08-25 11:52:18 +00:00
|
|
|
case 2:
|
|
|
|
return c.getContainerV2(ctx, id, opts...)
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 09:51:03 +00:00
|
|
|
// GetVerifiedContainerStructure is a wrapper over Client.GetContainer method
|
|
|
|
// which checks if the structure of the resulting container matches its identifier.
|
|
|
|
//
|
|
|
|
// Returns container.ErrIDMismatch if container does not match the identifier.
|
|
|
|
func GetVerifiedContainerStructure(ctx context.Context, c *Client, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
|
|
|
cnr, err := c.GetContainer(ctx, id, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.CalculateID(cnr).Equal(id) {
|
|
|
|
return nil, container.ErrIDMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnr, nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func (c Client) ListContainers(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-08-25 11:52:18 +00:00
|
|
|
case 2:
|
|
|
|
return c.listContainerV2(ctx, owner, opts...)
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]*container.ID, error) {
|
2020-08-31 08:15:46 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
ownerID := new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
return c.ListContainers(ctx, ownerID, opts...)
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) DeleteContainer(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-08-25 11:52:18 +00:00
|
|
|
case 2:
|
2020-09-02 11:58:28 +00:00
|
|
|
return c.delContainerV2(ctx, id, opts...)
|
2020-08-25 11:52:18 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:08 +00:00
|
|
|
func (c Client) GetEACL(ctx context.Context, id *container.ID, opts ...CallOption) (*eacl.Table, error) {
|
2020-11-24 10:22:07 +00:00
|
|
|
v, err := c.getEACL(ctx, id, true, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.table, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Client) GetEACLWithSignature(ctx context.Context, id *container.ID, opts ...CallOption) (*EACLWithSignature, error) {
|
|
|
|
return c.getEACL(ctx, id, false, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Client) getEACL(ctx context.Context, id *container.ID, verify bool, opts ...CallOption) (*EACLWithSignature, error) {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-09-10 15:08:08 +00:00
|
|
|
case 2:
|
2020-11-24 10:22:07 +00:00
|
|
|
resp, err := c.getEACLV2(ctx, id, verify, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &EACLWithSignature{
|
|
|
|
table: eacl.NewTableFromV2(resp.GetEACL()),
|
|
|
|
sig: pkg.NewSignatureFromV2(resp.GetSignature()),
|
|
|
|
}, nil
|
2020-09-10 15:08:08 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-09-10 15:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Client) SetEACL(ctx context.Context, eacl *eacl.Table, opts ...CallOption) error {
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-09-10 15:08:08 +00:00
|
|
|
case 2:
|
|
|
|
return c.setEACLV2(ctx, eacl, opts...)
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return errUnsupportedProtocol
|
2020-09-10 15:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 11:52:18 +00:00
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set transport version
|
2020-11-11 13:50:34 +00:00
|
|
|
cnr.SetVersion(c.remoteNode.Version)
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
// if container owner is not set, then use client key as owner
|
2020-11-11 13:50:34 +00:00
|
|
|
if cnr.OwnerID() == nil {
|
2020-08-31 08:15:46 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
ownerID := new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
2020-11-11 13:50:34 +00:00
|
|
|
cnr.SetOwnerID(ownerID)
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.PutRequestBody)
|
2020-09-15 08:04:36 +00:00
|
|
|
reqBody.SetContainer(cnr.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
// sign container
|
|
|
|
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainer()}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
err := signature.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
2020-08-31 08:15:46 +00:00
|
|
|
containerSignature := new(refs.Signature)
|
2020-08-25 11:52:18 +00:00
|
|
|
containerSignature.SetKey(key)
|
|
|
|
containerSignature.SetSign(sig)
|
|
|
|
reqBody.SetSignature(containerSignature)
|
|
|
|
}, signature.SignWithRFC6979())
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := new(v2container.PutRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Put(ctx, req)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "transport error")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
return container.NewIDFromV2(resp.GetBody().GetContainerID()), nil
|
2020-08-25 11:52:18 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.GetRequestBody)
|
2020-08-28 13:14:04 +00:00
|
|
|
reqBody.SetContainerID(id.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
req := new(v2container.GetRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Get(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2020-09-15 08:04:36 +00:00
|
|
|
return container.NewContainerFromV2(resp.GetBody().GetContainer()), nil
|
2020-08-25 11:52:18 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func (c Client) listContainerV2(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.ListRequestBody)
|
2020-08-31 08:15:46 +00:00
|
|
|
reqBody.SetOwnerID(owner.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
req := new(v2container.ListRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.List(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
result := make([]*container.ID, 0, len(resp.GetBody().GetContainerIDs()))
|
|
|
|
for _, cidV2 := range resp.GetBody().GetContainerIDs() {
|
2020-09-10 09:57:29 +00:00
|
|
|
result = append(result, container.NewIDFromV2(cidV2))
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) delContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:52:18 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.DeleteRequestBody)
|
2020-08-28 13:14:04 +00:00
|
|
|
reqBody.SetContainerID(id.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
// sign container
|
2020-09-02 11:58:28 +00:00
|
|
|
err := signature.SignDataWithHandler(c.key,
|
|
|
|
delContainerSignWrapper{
|
|
|
|
body: reqBody,
|
|
|
|
},
|
|
|
|
func(key []byte, sig []byte) {
|
|
|
|
containerSignature := new(refs.Signature)
|
|
|
|
containerSignature.SetKey(key)
|
|
|
|
containerSignature.SetSign(sig)
|
|
|
|
reqBody.SetSignature(containerSignature)
|
|
|
|
},
|
|
|
|
signature.SignWithRFC6979())
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := new(v2container.DeleteRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Delete(ctx, req)
|
2020-09-10 15:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return errUnsupportedProtocol
|
2020-09-10 15:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
func (c Client) getEACLV2(ctx context.Context, id *container.ID, verify bool, opts ...CallOption) (*v2container.GetExtendedACLResponseBody, error) {
|
2020-09-10 15:08:08 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-10 15:08:08 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.GetExtendedACLRequestBody)
|
|
|
|
reqBody.SetContainerID(id.ToV2())
|
|
|
|
|
|
|
|
req := new(v2container.GetExtendedACLRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.GetExtendedACL(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
body := resp.GetBody()
|
|
|
|
if body == nil {
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errNilReponseBody
|
2020-09-10 15:08:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
if verify {
|
|
|
|
if err := signature.VerifyDataWithSource(
|
|
|
|
v2signature.StableMarshalerWrapper{
|
|
|
|
SM: body.GetEACL(),
|
|
|
|
},
|
|
|
|
func() (key, sig []byte) {
|
|
|
|
s := body.GetSignature()
|
|
|
|
|
|
|
|
return s.GetKey(), s.GetSign()
|
|
|
|
},
|
|
|
|
signature.SignWithRFC6979(),
|
|
|
|
); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "incorrect signature")
|
|
|
|
}
|
2020-11-16 14:35:52 +00:00
|
|
|
}
|
2020-09-10 15:08:08 +00:00
|
|
|
|
2020-11-24 10:22:07 +00:00
|
|
|
return body, nil
|
2020-09-10 15:08:08 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-09-10 15:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Client) setEACLV2(ctx context.Context, eacl *eacl.Table, opts ...CallOption) error {
|
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-10 15:08:08 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.SetExtendedACLRequestBody)
|
|
|
|
reqBody.SetEACL(eacl.ToV2())
|
2020-10-20 12:17:53 +00:00
|
|
|
reqBody.GetEACL().SetVersion(c.remoteNode.Version.ToV2())
|
2020-09-10 15:08:08 +00:00
|
|
|
|
|
|
|
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetEACL()}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-10 15:08:08 +00:00
|
|
|
err := signature.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
|
|
|
eaclSignature := new(refs.Signature)
|
|
|
|
eaclSignature.SetKey(key)
|
|
|
|
eaclSignature.SetSign(sig)
|
|
|
|
reqBody.SetSignature(eaclSignature)
|
|
|
|
}, signature.SignWithRFC6979())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := new(v2container.SetExtendedACLRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.SetExtendedACL(ctx, req)
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return errUnsupportedProtocol
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func v2ContainerClientFromOptions(opts *clientOptions) (cli *v2container.Client, err error) {
|
|
|
|
switch {
|
|
|
|
case opts.grpcOpts.v2ContainerClient != nil:
|
|
|
|
// return value from client cache
|
|
|
|
return opts.grpcOpts.v2ContainerClient, nil
|
|
|
|
|
|
|
|
case opts.grpcOpts.conn != nil:
|
|
|
|
cli, err = v2container.NewClient(v2container.WithGlobalOpts(
|
|
|
|
client.WithGRPCConn(opts.grpcOpts.conn)),
|
|
|
|
)
|
|
|
|
|
|
|
|
case opts.addr != "":
|
|
|
|
cli, err = v2container.NewClient(v2container.WithGlobalOpts(
|
2020-11-23 11:05:27 +00:00
|
|
|
client.WithNetworkAddress(opts.addr),
|
|
|
|
client.WithDialTimeout(opts.dialTimeout),
|
|
|
|
))
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errOptionsLack("Container")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if client correct and save in cache
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.grpcOpts.v2ContainerClient = cli
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|