2022-04-22 07:18:21 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/internal/neofs/services/tree"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-12 01:48:17 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-22 07:18:21 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
type (
|
|
|
|
TreeClient struct {
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
service tree.TreeServiceClient
|
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
TreeNode struct {
|
|
|
|
ID uint64
|
|
|
|
ObjID *oid.ID
|
|
|
|
TimeStamp uint64
|
|
|
|
Meta map[string]string
|
|
|
|
}
|
|
|
|
)
|
2022-04-22 07:18:21 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
versioningEnabledKV = "versioning_enabled"
|
|
|
|
lockConfigurationKV = "lock_configuration"
|
2022-05-12 01:48:17 +00:00
|
|
|
oidKv = "OID"
|
2022-04-22 07:18:21 +00:00
|
|
|
fileNameKV = "FileName"
|
|
|
|
systemNameKV = "SystemName"
|
|
|
|
|
|
|
|
settingsFileName = "bucket-settings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewTreeClient creates instance of TreeClient using provided address and create grpc connection.
|
|
|
|
func NewTreeClient(addr string) (*TreeClient, error) {
|
|
|
|
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := tree.NewTreeServiceClient(conn)
|
|
|
|
|
|
|
|
return &TreeClient{
|
|
|
|
conn: conn,
|
|
|
|
service: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
func newTreeNode(nodeInfo *tree.GetNodeByPathResponse_Info) (*TreeNode, error) {
|
|
|
|
var objID *oid.ID
|
2022-04-22 07:18:21 +00:00
|
|
|
meta := make(map[string]string, len(nodeInfo.GetMeta()))
|
|
|
|
|
|
|
|
for _, kv := range nodeInfo.GetMeta() {
|
2022-05-12 01:48:17 +00:00
|
|
|
if kv.GetKey() == oidKv {
|
|
|
|
objID = new(oid.ID)
|
|
|
|
err := objID.DecodeString(string(kv.GetValue()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
meta[kv.GetKey()] = string(kv.GetValue())
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
return &TreeNode{
|
|
|
|
ID: nodeInfo.GetNodeId(),
|
|
|
|
ObjID: objID,
|
|
|
|
TimeStamp: nodeInfo.Timestamp,
|
|
|
|
Meta: meta,
|
|
|
|
}, nil
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
func (n *TreeNode) Get(key string) (string, bool) {
|
|
|
|
value, ok := n.Meta[key]
|
2022-04-22 07:18:21 +00:00
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TreeClient) GetSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string) (*data.BucketSettings, error) {
|
|
|
|
keysToReturn := []string{versioningEnabledKV, lockConfigurationKV}
|
2022-05-12 01:48:17 +00:00
|
|
|
node, err := c.getSystemNode(ctx, cnrID, treeID, settingsFileName, keysToReturn)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get node: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := &data.BucketSettings{}
|
|
|
|
|
|
|
|
if versioningEnabledValue, ok := node.Get(versioningEnabledKV); ok {
|
|
|
|
if settings.VersioningEnabled, err = strconv.ParseBool(versioningEnabledValue); err != nil {
|
|
|
|
return nil, fmt.Errorf("settings node: invalid versioning: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lockConfigurationValue, ok := node.Get(lockConfigurationKV); ok {
|
|
|
|
if settings.LockConfiguration, err = parseLockConfiguration(lockConfigurationValue); err != nil {
|
|
|
|
return nil, fmt.Errorf("settings node: invalid lock configuration: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TreeClient) PutSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string, settings *data.BucketSettings) error {
|
2022-05-12 01:48:17 +00:00
|
|
|
node, err := c.getSystemNode(ctx, cnrID, treeID, settingsFileName, []string{})
|
|
|
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil && !isErrNotFound {
|
|
|
|
return fmt.Errorf("couldn't get node: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := metaFromSettings(settings)
|
|
|
|
|
|
|
|
if isErrNotFound {
|
|
|
|
_, err = c.addNode(ctx, cnrID, treeID, 0, meta)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
return c.moveNode(ctx, cnrID, treeID, node.ID, 0, meta)
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TreeClient) Close() error {
|
|
|
|
if c.conn != nil {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func metaFromSettings(settings *data.BucketSettings) map[string]string {
|
|
|
|
results := make(map[string]string, 3)
|
|
|
|
|
|
|
|
results[systemNameKV] = settingsFileName
|
|
|
|
results[versioningEnabledKV] = strconv.FormatBool(settings.VersioningEnabled)
|
|
|
|
results[lockConfigurationKV] = encodeLockConfiguration(settings.LockConfiguration)
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
func (c *TreeClient) getSystemNode(ctx context.Context, cnrID *cid.ID, treeID, path string, meta []string) (*TreeNode, error) {
|
|
|
|
request := &tree.GetNodeByPathRequest{
|
|
|
|
Body: &tree.GetNodeByPathRequest_Body{
|
|
|
|
ContainerId: []byte(cnrID.EncodeToString()),
|
|
|
|
TreeId: treeID,
|
|
|
|
Path: []string{path},
|
|
|
|
Attributes: meta,
|
|
|
|
PathAttribute: systemNameKV,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := c.service.GetNodeByPath(ctx, request)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "not found") {
|
2022-05-12 01:48:17 +00:00
|
|
|
return nil, layer.ErrNodeNotFound
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("couldn't get nodes: %w", err)
|
|
|
|
}
|
2022-05-12 01:48:17 +00:00
|
|
|
if len(resp.Body.GetNodes()) == 0 {
|
|
|
|
return nil, layer.ErrNodeNotFound
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
2022-05-12 01:48:17 +00:00
|
|
|
if len(resp.Body.GetNodes()) != 1 {
|
2022-04-22 07:18:21 +00:00
|
|
|
return nil, fmt.Errorf("found more than one node")
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
return newTreeNode(resp.Body.Nodes[0])
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
func (c *TreeClient) getSystemNodesWithOID(ctx context.Context, cnrID *cid.ID, treeID, path string, meta []string, latestOnly bool) ([]*TreeNode, error) {
|
|
|
|
meta = append(meta, oidKv)
|
|
|
|
|
|
|
|
r := &tree.GetNodeByPathRequest{
|
2022-04-22 07:18:21 +00:00
|
|
|
Body: &tree.GetNodeByPathRequest_Body{
|
|
|
|
ContainerId: []byte(cnrID.EncodeToString()),
|
|
|
|
TreeId: treeID,
|
2022-05-12 01:48:17 +00:00
|
|
|
PathAttribute: systemNameKV,
|
|
|
|
Path: []string{path},
|
2022-04-22 07:18:21 +00:00
|
|
|
Attributes: meta,
|
2022-05-12 01:48:17 +00:00
|
|
|
LatestOnly: latestOnly,
|
|
|
|
AllAttributes: false,
|
2022-04-22 07:18:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
resp, err := c.service.GetNodeByPath(ctx, r)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil {
|
2022-05-12 01:48:17 +00:00
|
|
|
if strings.Contains(err.Error(), "not found") {
|
|
|
|
return nil, layer.ErrNodeNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make([]*TreeNode, 0, len(resp.Body.Nodes))
|
|
|
|
for _, n := range resp.Body.GetNodes() {
|
|
|
|
node, err := newTreeNode(n)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
return nodes, nil
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TreeClient) addNode(ctx context.Context, cnrID *cid.ID, treeID string, parent uint64, meta map[string]string) (uint64, error) {
|
|
|
|
request := &tree.AddRequest{
|
|
|
|
Body: &tree.AddRequest_Body{
|
|
|
|
ContainerId: []byte(cnrID.EncodeToString()),
|
|
|
|
TreeId: treeID,
|
|
|
|
ParentId: parent,
|
|
|
|
Meta: metaToKV(meta),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.service.Add(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.GetBody().GetNodeId(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TreeClient) moveNode(ctx context.Context, cnrID *cid.ID, treeID string, nodeID, parentID uint64, meta map[string]string) error {
|
|
|
|
request := &tree.MoveRequest{
|
|
|
|
Body: &tree.MoveRequest_Body{
|
|
|
|
ContainerId: []byte(cnrID.EncodeToString()),
|
|
|
|
TreeId: treeID,
|
|
|
|
NodeId: nodeID,
|
|
|
|
ParentId: parentID,
|
|
|
|
Meta: metaToKV(meta),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.service.Move(ctx, request)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func metaToKV(meta map[string]string) []*tree.KeyValue {
|
|
|
|
result := make([]*tree.KeyValue, 0, len(meta))
|
|
|
|
|
|
|
|
for key, value := range meta {
|
|
|
|
result = append(result, &tree.KeyValue{Key: key, Value: []byte(value)})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLockConfiguration(value string) (*data.ObjectLockConfiguration, error) {
|
|
|
|
result := &data.ObjectLockConfiguration{}
|
|
|
|
if len(value) == 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lockValues := strings.Split(value, ",")
|
|
|
|
result.ObjectLockEnabled = lockValues[0]
|
|
|
|
|
|
|
|
if len(lockValues) == 1 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lockValues) != 4 {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var days, years int64
|
|
|
|
|
|
|
|
if len(lockValues[1]) > 0 {
|
|
|
|
if days, err = strconv.ParseInt(lockValues[1], 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lockValues[3]) > 0 {
|
|
|
|
if years, err = strconv.ParseInt(lockValues[3], 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Rule = &data.ObjectLockRule{
|
|
|
|
DefaultRetention: &data.DefaultRetention{
|
|
|
|
Days: days,
|
|
|
|
Mode: lockValues[2],
|
|
|
|
Years: years,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeLockConfiguration(conf *data.ObjectLockConfiguration) string {
|
|
|
|
if conf == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Rule == nil || conf.Rule.DefaultRetention == nil {
|
|
|
|
return conf.ObjectLockEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults := conf.Rule.DefaultRetention
|
|
|
|
return fmt.Sprintf("%s,%d,%s,%d", conf.ObjectLockEnabled, defaults.Days, defaults.Mode, defaults.Years)
|
|
|
|
}
|