package services import ( "context" "errors" "fmt" "io" "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" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer" treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree" grpcService "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree/service" ) type GetNodeByPathResponseInfoWrapper struct { response *grpcService.GetNodeByPathResponse_Info } 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() } func (n GetNodeByPathResponseInfoWrapper) GetMeta() []tree.Meta { res := make([]tree.Meta, len(n.response.Meta)) for i, value := range n.response.Meta { res[i] = value } return res } type GetSubTreeResponseBodyWrapper struct { response *grpcService.GetSubTreeResponse_Body } 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() } func (n GetSubTreeResponseBodyWrapper) GetMeta() []tree.Meta { res := make([]tree.Meta, len(n.response.Meta)) for i, value := range n.response.Meta { res[i] = value } return res } type PoolWrapper struct { p *treepool.Pool } func NewPoolWrapper(p *treepool.Pool) *PoolWrapper { return &PoolWrapper{p: p} } func (w *PoolWrapper) GetNodes(ctx context.Context, prm *tree.GetNodesParams) ([]tree.NodeResponse, error) { poolPrm := treepool.GetNodesParams{ CID: prm.BktInfo.CID, TreeID: prm.TreeID, Path: prm.Path, Meta: prm.Meta, PathAttribute: tree.FileNameKey, LatestOnly: prm.LatestOnly, AllAttrs: prm.AllAttrs, BearerToken: getBearer(ctx, prm.BktInfo), } nodes, err := w.p.GetNodes(ctx, poolPrm) if err != nil { return nil, handleError(err) } res := make([]tree.NodeResponse, len(nodes)) for i, info := range nodes { res[i] = GetNodeByPathResponseInfoWrapper{info} } return res, nil } func (w *PoolWrapper) GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) ([]tree.NodeResponse, error) { poolPrm := treepool.GetSubTreeParams{ CID: bktInfo.CID, TreeID: treeID, RootID: rootID, Depth: depth, BearerToken: getBearer(ctx, bktInfo), } subTreeReader, err := w.p.GetSubTree(ctx, poolPrm) if err != nil { return nil, handleError(err) } var subtree []tree.NodeResponse node, err := subTreeReader.Next() for err == nil { subtree = append(subtree, GetSubTreeResponseBodyWrapper{node}) node, err = subTreeReader.Next() } if err != nil && err != io.EOF { return nil, handleError(err) } return subtree, nil } func (w *PoolWrapper) AddNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, parent uint64, meta map[string]string) (uint64, error) { nodeID, err := w.p.AddNode(ctx, treepool.AddNodeParams{ CID: bktInfo.CID, TreeID: treeID, Parent: parent, Meta: meta, BearerToken: getBearer(ctx, bktInfo), }) return nodeID, handleError(err) } func (w *PoolWrapper) AddNodeByPath(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path []string, meta map[string]string) (uint64, error) { nodeID, err := w.p.AddNodeByPath(ctx, treepool.AddNodeByPathParams{ CID: bktInfo.CID, TreeID: treeID, Path: path, Meta: meta, PathAttribute: tree.FileNameKey, BearerToken: getBearer(ctx, bktInfo), }) return nodeID, handleError(err) } func (w *PoolWrapper) MoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID, parentID uint64, meta map[string]string) error { return handleError(w.p.MoveNode(ctx, treepool.MoveNodeParams{ CID: bktInfo.CID, TreeID: treeID, NodeID: nodeID, ParentID: parentID, Meta: meta, BearerToken: getBearer(ctx, bktInfo), })) } func (w *PoolWrapper) RemoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error { return handleError(w.p.RemoveNode(ctx, treepool.RemoveNodeParams{ CID: bktInfo.CID, TreeID: treeID, NodeID: nodeID, BearerToken: getBearer(ctx, bktInfo), })) } 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 { if bd.Gate.BearerToken.Impersonate() || bktInfo.Owner.Equals(bearer.ResolveIssuer(*bd.Gate.BearerToken)) { return bd.Gate.BearerToken.Marshal() } } } return nil } func handleError(err error) error { if err == nil { return nil } if errors.Is(err, treepool.ErrNodeNotFound) { return fmt.Errorf("%w: %s", tree.ErrNodeNotFound, err.Error()) } if errors.Is(err, treepool.ErrNodeAccessDenied) { return fmt.Errorf("%w: %s", tree.ErrNodeAccessDenied, err.Error()) } return err }