2023-04-03 11:04:35 +00:00
|
|
|
package services
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2023-04-04 14:47:16 +00:00
|
|
|
"sync/atomic"
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
2023-05-19 13:32:17 +00:00
|
|
|
treeClient "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/client"
|
2023-04-03 11:04:35 +00:00
|
|
|
grpcService "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/tree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree"
|
2023-05-19 14:08:59 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2023-03-14 14:31:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-04-03 14:03:10 +00:00
|
|
|
"go.uber.org/zap"
|
2023-03-14 14:31:15 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetNodeByPathResponseInfoWrapper struct {
|
2023-04-03 11:04:35 +00:00
|
|
|
response *grpcService.GetNodeByPathResponse_Info
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetNodeID() uint64 {
|
|
|
|
return n.response.GetNodeId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetParentID() uint64 {
|
|
|
|
return n.response.GetParentId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetTimestamp() uint64 {
|
|
|
|
return n.response.GetTimestamp()
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetMeta() []tree.Meta {
|
|
|
|
res := make([]tree.Meta, len(n.response.Meta))
|
2023-03-14 14:31:15 +00:00
|
|
|
for i, value := range n.response.Meta {
|
|
|
|
res[i] = value
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetSubTreeResponseBodyWrapper struct {
|
2023-04-03 11:04:35 +00:00
|
|
|
response *grpcService.GetSubTreeResponse_Body
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetNodeID() uint64 {
|
|
|
|
return n.response.GetNodeId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetParentID() uint64 {
|
|
|
|
return n.response.GetParentId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetTimestamp() uint64 {
|
|
|
|
return n.response.GetTimestamp()
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetMeta() []tree.Meta {
|
|
|
|
res := make([]tree.Meta, len(n.response.Meta))
|
2023-03-14 14:31:15 +00:00
|
|
|
for i, value := range n.response.Meta {
|
|
|
|
res[i] = value
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-05-19 14:08:59 +00:00
|
|
|
type TreeClient interface {
|
|
|
|
TreeClient(ctx context.Context) (grpcService.TreeServiceClient, error)
|
|
|
|
Address() string
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
type ServiceClientGRPC struct {
|
2023-04-04 14:47:16 +00:00
|
|
|
key *keys.PrivateKey
|
|
|
|
log *zap.Logger
|
2023-05-19 14:08:59 +00:00
|
|
|
clients []TreeClient
|
2023-04-11 12:16:14 +00:00
|
|
|
startIndex int32
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
func (c *ServiceClientGRPC) getStartIndex() int {
|
|
|
|
return int(atomic.LoadInt32(&c.startIndex))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceClientGRPC) setStartIndex(index int) {
|
|
|
|
atomic.StoreInt32(&c.startIndex, int32(index))
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:03:10 +00:00
|
|
|
func (c *ServiceClientGRPC) Endpoints() []string {
|
|
|
|
res := make([]string, len(c.clients))
|
|
|
|
for i, client := range c.clients {
|
2023-05-19 13:32:17 +00:00
|
|
|
res[i] = client.Address()
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTreeServiceClientGRPC(ctx context.Context, endpoints []string, key *keys.PrivateKey, log *zap.Logger, grpcOpts ...grpc.DialOption) (*ServiceClientGRPC, error) {
|
|
|
|
res := &ServiceClientGRPC{
|
|
|
|
key: key,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
firstHealthy := -1
|
2023-04-03 14:03:10 +00:00
|
|
|
|
2023-05-19 14:08:59 +00:00
|
|
|
res.clients = make([]TreeClient, len(endpoints))
|
2023-05-19 13:32:17 +00:00
|
|
|
for i, addr := range endpoints {
|
|
|
|
res.clients[i] = treeClient.NewTreeClient(addr, grpcOpts...)
|
|
|
|
if _, err := res.clients[i].TreeClient(ctx); err != nil {
|
|
|
|
log.Warn("dial tree", zap.String("address", addr), zap.Error(err))
|
2023-04-03 14:03:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-19 13:32:17 +00:00
|
|
|
if firstHealthy == -1 {
|
|
|
|
firstHealthy = i
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
if firstHealthy == -1 {
|
2023-04-03 14:03:10 +00:00
|
|
|
return nil, errors.New("no healthy tree grpc client")
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
res.setStartIndex(firstHealthy)
|
|
|
|
|
2023-04-03 14:03:10 +00:00
|
|
|
return res, nil
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *tree.GetNodesParams) ([]tree.NodeResponse, error) {
|
|
|
|
request := &grpcService.GetNodeByPathRequest{
|
|
|
|
Body: &grpcService.GetNodeByPathRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: p.BktInfo.CID[:],
|
|
|
|
TreeId: p.TreeID,
|
|
|
|
Path: p.Path,
|
|
|
|
Attributes: p.Meta,
|
2023-04-03 11:04:35 +00:00
|
|
|
PathAttribute: tree.FileNameKey,
|
2023-03-14 14:31:15 +00:00
|
|
|
LatestOnly: p.LatestOnly,
|
|
|
|
AllAttributes: p.AllAttrs,
|
|
|
|
BearerToken: getBearer(ctx, p.BktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", p.TreeID),
|
|
|
|
zap.String("method", "GetNodeByPath"))
|
|
|
|
|
2023-04-11 12:16:14 +00:00
|
|
|
var resp *grpcService.GetNodeByPathResponse
|
2023-05-19 13:32:17 +00:00
|
|
|
if err := c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) (inErr error) {
|
|
|
|
resp, inErr = client.GetNodeByPath(ctx, request)
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to get node by path", inErr)
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
res := make([]tree.NodeResponse, len(resp.GetBody().GetNodes()))
|
2023-03-14 14:31:15 +00:00
|
|
|
for i, info := range resp.GetBody().GetNodes() {
|
|
|
|
res[i] = GetNodeByPathResponseInfoWrapper{info}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) ([]tree.NodeResponse, error) {
|
|
|
|
request := &grpcService.GetSubTreeRequest{
|
|
|
|
Body: &grpcService.GetSubTreeRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: bktInfo.CID[:],
|
|
|
|
TreeId: treeID,
|
|
|
|
RootId: rootID,
|
|
|
|
Depth: depth,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", treeID),
|
|
|
|
zap.String("method", "GetSubTree"))
|
|
|
|
|
2023-04-11 12:16:14 +00:00
|
|
|
var cli grpcService.TreeService_GetSubTreeClient
|
2023-05-19 13:32:17 +00:00
|
|
|
if err := c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) (inErr error) {
|
|
|
|
cli, inErr = client.GetSubTree(ctx, request)
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to get sub tree client", inErr)
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
var subtree []tree.NodeResponse
|
2023-03-14 14:31:15 +00:00
|
|
|
for {
|
|
|
|
resp, err := cli.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, handleError("failed to get sub tree", err)
|
|
|
|
}
|
|
|
|
subtree = append(subtree, GetSubTreeResponseBodyWrapper{resp.Body})
|
|
|
|
}
|
|
|
|
|
|
|
|
return subtree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceClientGRPC) AddNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, parent uint64, meta map[string]string) (uint64, error) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request := &grpcService.AddRequest{
|
|
|
|
Body: &grpcService.AddRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: bktInfo.CID[:],
|
|
|
|
TreeId: treeID,
|
|
|
|
ParentId: parent,
|
|
|
|
Meta: metaToKV(meta),
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", treeID),
|
|
|
|
zap.String("method", "Add"))
|
|
|
|
|
2023-04-11 12:16:14 +00:00
|
|
|
var resp *grpcService.AddResponse
|
2023-05-19 13:32:17 +00:00
|
|
|
if err := c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) (inErr error) {
|
|
|
|
resp, inErr = client.Add(ctx, request)
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to add node", inErr)
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp.GetBody().GetNodeId(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceClientGRPC) AddNodeByPath(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path []string, meta map[string]string) (uint64, error) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request := &grpcService.AddByPathRequest{
|
|
|
|
Body: &grpcService.AddByPathRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: bktInfo.CID[:],
|
|
|
|
TreeId: treeID,
|
|
|
|
Path: path,
|
|
|
|
Meta: metaToKV(meta),
|
2023-04-03 11:04:35 +00:00
|
|
|
PathAttribute: tree.FileNameKey,
|
2023-03-14 14:31:15 +00:00
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", treeID),
|
|
|
|
zap.String("method", "AddByPath"))
|
|
|
|
|
2023-04-11 12:16:14 +00:00
|
|
|
var resp *grpcService.AddByPathResponse
|
2023-05-19 13:32:17 +00:00
|
|
|
if err := c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) (inErr error) {
|
|
|
|
resp, inErr = client.AddByPath(ctx, request)
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to add node by path", inErr)
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body := resp.GetBody()
|
|
|
|
if body == nil {
|
|
|
|
return 0, errors.New("nil body in tree service response")
|
|
|
|
} else if len(body.Nodes) == 0 {
|
|
|
|
return 0, errors.New("empty list of added nodes in tree service response")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first node is the leaf that we add, according to tree service docs.
|
|
|
|
return body.Nodes[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceClientGRPC) MoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID, parentID uint64, meta map[string]string) error {
|
2023-04-03 11:04:35 +00:00
|
|
|
request := &grpcService.MoveRequest{
|
|
|
|
Body: &grpcService.MoveRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: bktInfo.CID[:],
|
|
|
|
TreeId: treeID,
|
|
|
|
NodeId: nodeID,
|
|
|
|
ParentId: parentID,
|
|
|
|
Meta: metaToKV(meta),
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", treeID),
|
|
|
|
zap.String("method", "Move"))
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
return c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) error {
|
|
|
|
if _, err := client.Move(ctx, request); err != nil {
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to move node", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-04-03 14:03:10 +00:00
|
|
|
})
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServiceClientGRPC) RemoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error {
|
2023-04-03 11:04:35 +00:00
|
|
|
request := &grpcService.RemoveRequest{
|
|
|
|
Body: &grpcService.RemoveRequest_Body{
|
2023-03-14 14:31:15 +00:00
|
|
|
ContainerId: bktInfo.CID[:],
|
|
|
|
TreeId: treeID,
|
|
|
|
NodeId: nodeID,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := c.signRequest(request.Body, func(key, sign []byte) {
|
2023-04-03 11:04:35 +00:00
|
|
|
request.Signature = &grpcService.Signature{
|
2023-03-14 14:31:15 +00:00
|
|
|
Key: key,
|
|
|
|
Sign: sign,
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 14:51:46 +00:00
|
|
|
log := c.log.With(zap.String("request_id", api.GetRequestID(ctx)), zap.String("tree", treeID),
|
|
|
|
zap.String("method", "Remove"))
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
return c.requestWithRetry(ctx, log, func(client grpcService.TreeServiceClient) error {
|
|
|
|
if _, err := client.Remove(ctx, request); err != nil {
|
2023-04-11 12:16:14 +00:00
|
|
|
return handleError("failed to remove node", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-04-03 14:03:10 +00:00
|
|
|
})
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
func (c *ServiceClientGRPC) requestWithRetry(ctx context.Context, log *zap.Logger, fn func(client grpcService.TreeServiceClient) error) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
cl grpcService.TreeServiceClient
|
|
|
|
)
|
|
|
|
|
|
|
|
start := c.getStartIndex()
|
2023-04-04 14:47:16 +00:00
|
|
|
for i := start; i < start+len(c.clients); i++ {
|
|
|
|
index := i % len(c.clients)
|
2023-05-19 13:32:17 +00:00
|
|
|
if cl, err = c.clients[index].TreeClient(ctx); err == nil {
|
|
|
|
err = fn(cl)
|
|
|
|
}
|
2023-04-03 14:03:10 +00:00
|
|
|
if !shouldTryAgain(err) {
|
2023-05-19 13:32:17 +00:00
|
|
|
c.setStartIndex(index)
|
2023-04-11 12:16:14 +00:00
|
|
|
return err
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
2023-05-19 13:32:17 +00:00
|
|
|
log.Debug("tree request error", zap.String("address", c.clients[index].Address()), zap.Error(err))
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:16:14 +00:00
|
|
|
return err
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldTryAgain(err error) bool {
|
2023-05-18 13:27:30 +00:00
|
|
|
return !(err == nil ||
|
|
|
|
errors.Is(err, tree.ErrNodeNotFound) ||
|
|
|
|
errors.Is(err, tree.ErrNodeAccessDenied))
|
2023-04-03 14:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:04:35 +00:00
|
|
|
func metaToKV(meta map[string]string) []*grpcService.KeyValue {
|
|
|
|
result := make([]*grpcService.KeyValue, 0, len(meta))
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
for key, value := range meta {
|
2023-04-03 11:04:35 +00:00
|
|
|
result = append(result, &grpcService.KeyValue{Key: key, Value: []byte(value)})
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBearer(ctx context.Context, bktInfo *data.BucketInfo) []byte {
|
|
|
|
if bd, ok := ctx.Value(api.BoxData).(*accessbox.Box); ok && bd != nil && bd.Gate != nil {
|
|
|
|
if bd.Gate.BearerToken != nil {
|
2022-10-25 09:30:18 +00:00
|
|
|
if bd.Gate.BearerToken.Impersonate() || bktInfo.Owner.Equals(bearer.ResolveIssuer(*bd.Gate.BearerToken)) {
|
2023-03-14 14:31:15 +00:00
|
|
|
return bd.Gate.BearerToken.Marshal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleError(msg string, err error) error {
|
2023-04-11 12:16:14 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
if strings.Contains(err.Error(), "not found") {
|
2023-04-03 11:04:35 +00:00
|
|
|
return fmt.Errorf("%w: %s", tree.ErrNodeNotFound, err.Error())
|
2023-03-14 14:31:15 +00:00
|
|
|
} else if strings.Contains(err.Error(), "is denied by") {
|
2023-04-03 11:04:35 +00:00
|
|
|
return fmt.Errorf("%w: %s", tree.ErrNodeAccessDenied, err.Error())
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("%s: %w", msg, err)
|
|
|
|
}
|