2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2021-08-10 12:08:15 +00:00
|
|
|
"bytes"
|
2020-08-03 11:48:33 +00:00
|
|
|
"context"
|
2021-05-28 20:48:23 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-20 10:14:17 +00:00
|
|
|
"fmt"
|
2020-08-03 11:48:33 +00:00
|
|
|
"io"
|
2020-10-24 13:09:22 +00:00
|
|
|
"net/url"
|
2021-08-17 11:23:49 +00:00
|
|
|
"strings"
|
2020-08-03 15:08:55 +00:00
|
|
|
"time"
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-10 11:19:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2022-02-08 15:34:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/notifications"
|
2022-01-11 10:09:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/resolver"
|
2021-06-21 10:54:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2021-11-15 12:56:16 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-08 16:54:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2021-08-27 12:48:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-08-03 11:48:33 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
layer struct {
|
2021-08-18 13:48:58 +00:00
|
|
|
pool pool.Pool
|
|
|
|
log *zap.Logger
|
2021-10-19 15:08:07 +00:00
|
|
|
anonKey AnonymousKey
|
2022-01-11 10:09:34 +00:00
|
|
|
resolver *resolver.BucketResolver
|
2022-02-08 15:34:31 +00:00
|
|
|
ncontroller *notifications.Controller
|
2021-09-10 06:56:56 +00:00
|
|
|
listsCache *cache.ObjectsListCache
|
|
|
|
objCache *cache.ObjectsCache
|
|
|
|
namesCache *cache.ObjectsNameCache
|
|
|
|
bucketCache *cache.BucketCache
|
|
|
|
systemCache *cache.SystemCache
|
2020-10-19 01:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 09:16:05 +00:00
|
|
|
Config struct {
|
2022-02-08 15:34:31 +00:00
|
|
|
ChainAddress string
|
|
|
|
Caches *CachesConfig
|
|
|
|
AnonKey AnonymousKey
|
|
|
|
Resolver *resolver.BucketResolver
|
|
|
|
NotificationController *notifications.Controller
|
2021-11-22 09:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
// AnonymousKey contains data for anonymous requests.
|
|
|
|
AnonymousKey struct {
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// CachesConfig contains params for caches.
|
|
|
|
CachesConfig struct {
|
|
|
|
Objects *cache.Config
|
|
|
|
ObjectsList *cache.Config
|
|
|
|
Names *cache.Config
|
|
|
|
Buckets *cache.Config
|
|
|
|
System *cache.Config
|
2021-08-16 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// Params stores basic API parameters.
|
2020-10-19 01:04:37 +00:00
|
|
|
Params struct {
|
2021-05-28 20:48:23 +00:00
|
|
|
Pool pool.Pool
|
|
|
|
Logger *zap.Logger
|
|
|
|
Timeout time.Duration
|
|
|
|
Key *ecdsa.PrivateKey
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// GetObjectParams stores object get request parameters.
|
2020-08-03 11:48:33 +00:00
|
|
|
GetObjectParams struct {
|
2021-08-10 10:03:09 +00:00
|
|
|
Range *RangeParams
|
2021-09-10 06:56:56 +00:00
|
|
|
ObjectInfo *data.ObjectInfo
|
2021-08-19 06:55:22 +00:00
|
|
|
Writer io.Writer
|
|
|
|
VersionID string
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObjectParams stores object head request parameters.
|
|
|
|
HeadObjectParams struct {
|
|
|
|
Bucket string
|
|
|
|
Object string
|
|
|
|
VersionID string
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
// RangeParams stores range header request parameters.
|
|
|
|
RangeParams struct {
|
|
|
|
Start uint64
|
|
|
|
End uint64
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// PutObjectParams stores object put request parameters.
|
2020-08-03 11:48:33 +00:00
|
|
|
PutObjectParams struct {
|
|
|
|
Bucket string
|
|
|
|
Object string
|
|
|
|
Size int64
|
|
|
|
Reader io.Reader
|
|
|
|
Header map[string]string
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:29:44 +00:00
|
|
|
// PutVersioningParams stores object copy request parameters.
|
|
|
|
PutVersioningParams struct {
|
2021-08-10 08:58:40 +00:00
|
|
|
Bucket string
|
|
|
|
Settings *BucketSettings
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
// PutCORSParams stores PutCORS request parameters.
|
|
|
|
PutCORSParams struct {
|
2021-10-13 18:50:02 +00:00
|
|
|
BktInfo *data.BucketInfo
|
|
|
|
Reader io.Reader
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
// BucketSettings stores settings such as versioning.
|
|
|
|
BucketSettings struct {
|
2021-08-09 14:29:44 +00:00
|
|
|
VersioningEnabled bool
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// CopyObjectParams stores object copy request parameters.
|
2020-08-03 11:48:33 +00:00
|
|
|
CopyObjectParams struct {
|
2021-09-10 06:56:56 +00:00
|
|
|
SrcObject *data.ObjectInfo
|
2020-08-03 11:48:33 +00:00
|
|
|
DstBucket string
|
|
|
|
DstObject string
|
2021-07-01 07:45:55 +00:00
|
|
|
SrcSize int64
|
2020-08-19 23:31:04 +00:00
|
|
|
Header map[string]string
|
2021-11-25 15:05:58 +00:00
|
|
|
Range *RangeParams
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
// CreateBucketParams stores bucket create request parameters.
|
|
|
|
CreateBucketParams struct {
|
2022-01-11 12:33:09 +00:00
|
|
|
Name string
|
|
|
|
ACL uint32
|
|
|
|
Policy *netmap.PlacementPolicy
|
|
|
|
EACL *eacl.Table
|
|
|
|
SessionToken *session.Token
|
|
|
|
LocationConstraint string
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
// PutBucketACLParams stores put bucket acl request parameters.
|
|
|
|
PutBucketACLParams struct {
|
|
|
|
Name string
|
|
|
|
EACL *eacl.Table
|
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
// DeleteBucketParams stores delete bucket request parameters.
|
|
|
|
DeleteBucketParams struct {
|
|
|
|
Name string
|
|
|
|
}
|
2021-10-04 14:30:38 +00:00
|
|
|
|
|
|
|
// PutSystemObjectParams stores putSystemObject parameters.
|
|
|
|
PutSystemObjectParams struct {
|
|
|
|
BktInfo *data.BucketInfo
|
|
|
|
ObjName string
|
|
|
|
Metadata map[string]string
|
|
|
|
Prefix string
|
2021-10-13 18:50:02 +00:00
|
|
|
Reader io.Reader
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 19:18:58 +00:00
|
|
|
// ListObjectVersionsParams stores list objects versions parameters.
|
|
|
|
ListObjectVersionsParams struct {
|
|
|
|
Bucket string
|
|
|
|
Delimiter string
|
|
|
|
KeyMarker string
|
|
|
|
MaxKeys int
|
|
|
|
Prefix string
|
|
|
|
VersionIDMarker string
|
|
|
|
Encode string
|
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
// VersionedObject stores info about objects to delete.
|
2021-08-10 12:08:15 +00:00
|
|
|
VersionedObject struct {
|
2021-09-07 06:17:12 +00:00
|
|
|
Name string
|
|
|
|
VersionID string
|
|
|
|
DeleteMarkVersion string
|
|
|
|
Error error
|
2021-08-10 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 08:04:42 +00:00
|
|
|
// PutTaggingParams stores tag set params.
|
|
|
|
PutTaggingParams struct {
|
2021-09-10 06:56:56 +00:00
|
|
|
ObjectInfo *data.ObjectInfo
|
2021-08-17 08:04:42 +00:00
|
|
|
TagSet map[string]string
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// NeoFS provides basic NeoFS interface.
|
2020-08-03 15:08:55 +00:00
|
|
|
NeoFS interface {
|
2022-02-08 16:54:04 +00:00
|
|
|
Get(ctx context.Context, address *address.Address) (*object.Object, error)
|
2020-08-03 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// Client provides S3 API client interface.
|
2020-08-03 11:48:33 +00:00
|
|
|
Client interface {
|
2020-08-03 15:08:55 +00:00
|
|
|
NeoFS
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
EphemeralKey() *keys.PublicKey
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
PutBucketVersioning(ctx context.Context, p *PutVersioningParams) (*data.ObjectInfo, error)
|
2021-08-10 08:58:40 +00:00
|
|
|
GetBucketVersioning(ctx context.Context, name string) (*BucketSettings, error)
|
2021-08-09 14:29:44 +00:00
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
PutBucketCORS(ctx context.Context, p *PutCORSParams) error
|
2021-10-13 18:50:02 +00:00
|
|
|
GetBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) (*data.CORSConfiguration, error)
|
2021-10-04 14:32:35 +00:00
|
|
|
DeleteBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) error
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
ListBuckets(ctx context.Context) ([]*data.BucketInfo, error)
|
|
|
|
GetBucketInfo(ctx context.Context, name string) (*data.BucketInfo, error)
|
2021-07-21 11:59:46 +00:00
|
|
|
GetBucketACL(ctx context.Context, name string) (*BucketACL, error)
|
|
|
|
PutBucketACL(ctx context.Context, p *PutBucketACLParams) error
|
2021-06-23 20:21:15 +00:00
|
|
|
CreateBucket(ctx context.Context, p *CreateBucketParams) (*cid.ID, error)
|
2021-06-23 20:25:00 +00:00
|
|
|
DeleteBucket(ctx context.Context, p *DeleteBucketParams) error
|
2020-08-03 11:48:33 +00:00
|
|
|
|
|
|
|
GetObject(ctx context.Context, p *GetObjectParams) error
|
2021-09-10 06:56:56 +00:00
|
|
|
GetObjectInfo(ctx context.Context, p *HeadObjectParams) (*data.ObjectInfo, error)
|
|
|
|
GetObjectTagging(ctx context.Context, p *data.ObjectInfo) (map[string]string, error)
|
2021-08-17 11:57:24 +00:00
|
|
|
GetBucketTagging(ctx context.Context, bucket string) (map[string]string, error)
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
PutObject(ctx context.Context, p *PutObjectParams) (*data.ObjectInfo, error)
|
2021-08-17 08:04:42 +00:00
|
|
|
PutObjectTagging(ctx context.Context, p *PutTaggingParams) error
|
2021-08-17 11:57:24 +00:00
|
|
|
PutBucketTagging(ctx context.Context, bucket string, tagSet map[string]string) error
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
CopyObject(ctx context.Context, p *CopyObjectParams) (*data.ObjectInfo, error)
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
ListObjectsV1(ctx context.Context, p *ListObjectsParamsV1) (*ListObjectsInfoV1, error)
|
|
|
|
ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*ListObjectsInfoV2, error)
|
2021-07-05 19:18:58 +00:00
|
|
|
ListObjectVersions(ctx context.Context, p *ListObjectVersionsParams) (*ListObjectVersionsInfo, error)
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
DeleteObjects(ctx context.Context, bucket string, objects []*VersionedObject) ([]*VersionedObject, error)
|
2021-09-10 06:56:56 +00:00
|
|
|
DeleteObjectTagging(ctx context.Context, p *data.ObjectInfo) error
|
2021-08-17 11:57:24 +00:00
|
|
|
DeleteBucketTagging(ctx context.Context, bucket string) error
|
2021-11-25 15:05:58 +00:00
|
|
|
|
|
|
|
CompleteMultipartUpload(ctx context.Context, p *CompleteMultipartParams) (*data.ObjectInfo, error)
|
|
|
|
UploadPart(ctx context.Context, p *UploadPartParams) (*data.ObjectInfo, error)
|
|
|
|
UploadPartCopy(ctx context.Context, p *UploadCopyParams) (*data.ObjectInfo, error)
|
|
|
|
ListMultipartUploads(ctx context.Context, p *ListMultipartUploadsParams) (*ListMultipartUploadsInfo, error)
|
|
|
|
AbortMultipartUpload(ctx context.Context, p *UploadInfoParams) error
|
|
|
|
ListParts(ctx context.Context, p *ListPartsParams) (*ListPartsInfo, error)
|
|
|
|
GetUploadInitInfo(ctx context.Context, p *UploadInfoParams) (*data.ObjectInfo, error)
|
2022-02-17 18:58:51 +00:00
|
|
|
|
|
|
|
PutBucketNotificationConfiguration(ctx context.Context, p *PutBucketNotificationConfigurationParams) error
|
|
|
|
GetBucketNotificationConfiguration(ctx context.Context, bktInfo *data.BucketInfo) (*data.NotificationConfiguration, error)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
const (
|
2022-01-11 10:09:34 +00:00
|
|
|
tagPrefix = "S3-Tag-"
|
|
|
|
tagEmptyMark = "\\"
|
2021-08-17 11:57:24 +00:00
|
|
|
)
|
2021-08-17 08:04:42 +00:00
|
|
|
|
2021-08-10 12:08:15 +00:00
|
|
|
func (t *VersionedObject) String() string {
|
|
|
|
return t.Name + ":" + t.VersionID
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// DefaultCachesConfigs returns filled configs.
|
|
|
|
func DefaultCachesConfigs() *CachesConfig {
|
|
|
|
return &CachesConfig{
|
|
|
|
Objects: cache.DefaultObjectsConfig(),
|
|
|
|
ObjectsList: cache.DefaultObjectsListConfig(),
|
|
|
|
Names: cache.DefaultObjectsNameConfig(),
|
|
|
|
Buckets: cache.DefaultBucketConfig(),
|
|
|
|
System: cache.DefaultSystemConfig(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// NewLayer creates instance of layer. It checks credentials
|
2020-08-03 11:48:33 +00:00
|
|
|
// and establishes gRPC connection with node.
|
2021-11-22 09:16:05 +00:00
|
|
|
func NewLayer(log *zap.Logger, conns pool.Pool, config *Config) Client {
|
2020-08-03 11:48:33 +00:00
|
|
|
return &layer{
|
2021-09-10 06:56:56 +00:00
|
|
|
pool: conns,
|
|
|
|
log: log,
|
2021-11-22 09:16:05 +00:00
|
|
|
anonKey: config.AnonKey,
|
2022-01-11 10:09:34 +00:00
|
|
|
resolver: config.Resolver,
|
2021-11-22 09:16:05 +00:00
|
|
|
listsCache: cache.NewObjectsListCache(config.Caches.ObjectsList),
|
2022-02-08 15:34:31 +00:00
|
|
|
ncontroller: config.NotificationController,
|
2021-11-22 09:16:05 +00:00
|
|
|
objCache: cache.New(config.Caches.Objects),
|
|
|
|
namesCache: cache.NewObjectsNameCache(config.Caches.Names),
|
|
|
|
bucketCache: cache.NewBucketCache(config.Caches.Buckets),
|
|
|
|
systemCache: cache.NewSystemCache(config.Caches.System),
|
2020-11-24 07:01:38 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
func (n *layer) EphemeralKey() *keys.PublicKey {
|
|
|
|
return n.anonKey.Key.PublicKey()
|
|
|
|
}
|
|
|
|
|
2022-02-08 15:34:31 +00:00
|
|
|
func (n *layer) IsNotificationEnabled() bool {
|
|
|
|
return n.ncontroller != nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:52:16 +00:00
|
|
|
// IsAuthenticatedRequest check if access box exists in current request.
|
|
|
|
func IsAuthenticatedRequest(ctx context.Context) bool {
|
|
|
|
_, ok := ctx.Value(api.BoxData).(*accessbox.Box)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
// Owner returns owner id from BearerToken (context) or from client owner.
|
|
|
|
func (n *layer) Owner(ctx context.Context) *owner.ID {
|
2021-07-16 12:35:07 +00:00
|
|
|
if data, ok := ctx.Value(api.BoxData).(*accessbox.Box); ok && data != nil && data.Gate != nil {
|
|
|
|
return data.Gate.BearerToken.Issuer()
|
2020-11-27 12:36:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 14:41:01 +00:00
|
|
|
return owner.NewIDFromPublicKey((*ecdsa.PublicKey)(n.EphemeralKey()))
|
2020-11-27 12:36:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:56:16 +00:00
|
|
|
// CallOptions returns []pool.CallOption options: client.WithBearer or client.WithKey (if request is anonymous).
|
|
|
|
func (n *layer) CallOptions(ctx context.Context) []pool.CallOption {
|
2021-07-16 12:35:07 +00:00
|
|
|
if data, ok := ctx.Value(api.BoxData).(*accessbox.Box); ok && data != nil && data.Gate != nil {
|
2021-11-15 12:56:16 +00:00
|
|
|
return []pool.CallOption{pool.WithBearer(data.Gate.BearerToken)}
|
2021-06-15 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:56:16 +00:00
|
|
|
return []pool.CallOption{pool.WithKey(&n.anonKey.Key.PrivateKey)}
|
2021-06-15 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
// Get NeoFS Object by address (should be used by auth.Center).
|
|
|
|
func (n *layer) Get(ctx context.Context, addr *address.Address) (*object.Object, error) {
|
|
|
|
return n.objectGet(ctx, addr)
|
2020-08-03 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 10:14:17 +00:00
|
|
|
// GetBucketInfo returns bucket info by name.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) GetBucketInfo(ctx context.Context, name string) (*data.BucketInfo, error) {
|
2020-10-24 13:09:22 +00:00
|
|
|
name, err := url.QueryUnescape(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if bktInfo := n.bucketCache.Get(name); bktInfo != nil {
|
|
|
|
return bktInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 09:16:05 +00:00
|
|
|
containerID, err := n.ResolveBucket(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Debug("bucket not found", zap.Error(err))
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchBucket)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:03 +00:00
|
|
|
return n.containerInfo(ctx, containerID)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
// GetBucketACL returns bucket acl info by name.
|
|
|
|
func (n *layer) GetBucketACL(ctx context.Context, name string) (*BucketACL, error) {
|
|
|
|
inf, err := n.GetBucketInfo(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
eacl, err := n.GetContainerEACL(ctx, inf.CID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BucketACL{
|
|
|
|
Info: inf,
|
|
|
|
EACL: eacl,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutBucketACL put bucket acl by name.
|
|
|
|
func (n *layer) PutBucketACL(ctx context.Context, param *PutBucketACLParams) error {
|
|
|
|
inf, err := n.GetBucketInfo(ctx, param.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.setContainerEACLTable(ctx, inf.CID, param.EACL)
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
// ListBuckets returns all user containers. Name of the bucket is a container
|
|
|
|
// id. Timestamp is omitted since it is not saved in neofs container.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) ListBuckets(ctx context.Context) ([]*data.BucketInfo, error) {
|
2020-08-03 11:48:33 +00:00
|
|
|
return n.containerList(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject from storage.
|
|
|
|
func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error {
|
2022-02-08 16:54:04 +00:00
|
|
|
var params getParams
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
params.w = p.Writer
|
|
|
|
params.oid = p.ObjectInfo.ID
|
|
|
|
params.cid = p.ObjectInfo.CID
|
2020-10-19 01:04:37 +00:00
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
if p.Range != nil {
|
2022-02-08 16:54:04 +00:00
|
|
|
if p.Range.Start > p.Range.End {
|
|
|
|
panic("invalid range")
|
|
|
|
}
|
|
|
|
|
|
|
|
params.off = p.Range.Start
|
|
|
|
params.ln = p.Range.End - p.Range.Start + 1
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
err := n.objectWritePayload(ctx, params)
|
2021-05-20 10:14:17 +00:00
|
|
|
if err != nil {
|
2021-08-18 13:48:58 +00:00
|
|
|
n.objCache.Delete(p.ObjectInfo.Address())
|
2021-08-27 21:33:50 +00:00
|
|
|
return fmt.Errorf("couldn't get object, cid: %s : %w", p.ObjectInfo.CID, err)
|
2021-05-20 10:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectInfo returns meta information about the object.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) GetObjectInfo(ctx context.Context, p *HeadObjectParams) (*data.ObjectInfo, error) {
|
2021-08-10 10:03:09 +00:00
|
|
|
bkt, err := n.GetBucketInfo(ctx, p.Bucket)
|
2021-08-09 14:29:44 +00:00
|
|
|
if err != nil {
|
2020-11-27 12:36:15 +00:00
|
|
|
n.log.Error("could not fetch bucket info", zap.Error(err))
|
2020-08-03 11:48:33 +00:00
|
|
|
return nil, err
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:03:09 +00:00
|
|
|
if len(p.VersionID) == 0 {
|
2021-08-13 14:13:14 +00:00
|
|
|
return n.headLastVersionIfNotDeleted(ctx, bkt, p.Object)
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 09:40:41 +00:00
|
|
|
return n.headVersion(ctx, bkt, p)
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
// PutObject into storage.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*data.ObjectInfo, error) {
|
2021-08-10 12:08:15 +00:00
|
|
|
bkt, err := n.GetBucketInfo(ctx, p.Bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.objectPut(ctx, bkt, p)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:01 +00:00
|
|
|
// GetObjectTagging from storage.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) GetObjectTagging(ctx context.Context, oi *data.ObjectInfo) (map[string]string, error) {
|
|
|
|
bktInfo := &data.BucketInfo{
|
2021-08-17 11:23:01 +00:00
|
|
|
Name: oi.Bucket,
|
2021-08-27 21:33:50 +00:00
|
|
|
CID: oi.CID,
|
2021-08-17 11:23:01 +00:00
|
|
|
Owner: oi.Owner,
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:30:38 +00:00
|
|
|
objInfo, err := n.headSystemObject(ctx, bktInfo, oi.TagsObject())
|
2021-08-17 11:23:01 +00:00
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
return formTagSet(objInfo), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketTagging from storage.
|
|
|
|
func (n *layer) GetBucketTagging(ctx context.Context, bucketName string) (map[string]string, error) {
|
|
|
|
bktInfo, err := n.GetBucketInfo(ctx, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:30:38 +00:00
|
|
|
objInfo, err := n.headSystemObject(ctx, bktInfo, formBucketTagObjectName(bucketName))
|
2021-08-17 11:57:24 +00:00
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formTagSet(objInfo), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func formTagSet(objInfo *data.ObjectInfo) map[string]string {
|
2021-08-17 11:23:01 +00:00
|
|
|
var tagSet map[string]string
|
|
|
|
if objInfo != nil {
|
|
|
|
tagSet = make(map[string]string, len(objInfo.Headers))
|
|
|
|
for k, v := range objInfo.Headers {
|
|
|
|
if strings.HasPrefix(k, tagPrefix) {
|
2021-08-17 11:57:24 +00:00
|
|
|
if v == tagEmptyMark {
|
|
|
|
v = ""
|
|
|
|
}
|
2021-08-17 11:23:01 +00:00
|
|
|
tagSet[strings.TrimPrefix(k, tagPrefix)] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 11:57:24 +00:00
|
|
|
return tagSet
|
2021-08-17 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 08:04:42 +00:00
|
|
|
// PutObjectTagging into storage.
|
|
|
|
func (n *layer) PutObjectTagging(ctx context.Context, p *PutTaggingParams) error {
|
2021-09-10 06:56:56 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2021-08-17 08:04:42 +00:00
|
|
|
Name: p.ObjectInfo.Bucket,
|
2021-08-27 21:33:50 +00:00
|
|
|
CID: p.ObjectInfo.CID,
|
2021-08-17 08:04:42 +00:00
|
|
|
Owner: p.ObjectInfo.Owner,
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:30:38 +00:00
|
|
|
s := &PutSystemObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjName: p.ObjectInfo.TagsObject(),
|
|
|
|
Metadata: p.TagSet,
|
|
|
|
Prefix: tagPrefix,
|
2021-10-13 18:50:02 +00:00
|
|
|
Reader: nil,
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := n.putSystemObject(ctx, s); err != nil {
|
2021-08-17 08:04:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:57:24 +00:00
|
|
|
// PutBucketTagging into storage.
|
|
|
|
func (n *layer) PutBucketTagging(ctx context.Context, bucketName string, tagSet map[string]string) error {
|
|
|
|
bktInfo, err := n.GetBucketInfo(ctx, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:30:38 +00:00
|
|
|
s := &PutSystemObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjName: formBucketTagObjectName(bucketName),
|
|
|
|
Metadata: tagSet,
|
|
|
|
Prefix: tagPrefix,
|
2021-10-13 18:50:02 +00:00
|
|
|
Reader: nil,
|
2021-10-04 14:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = n.putSystemObject(ctx, s); err != nil {
|
2021-08-17 11:57:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:49 +00:00
|
|
|
// DeleteObjectTagging from storage.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) DeleteObjectTagging(ctx context.Context, p *data.ObjectInfo) error {
|
2021-08-19 12:33:02 +00:00
|
|
|
bktInfo, err := n.GetBucketInfo(ctx, p.Bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.deleteSystemObject(ctx, bktInfo, p.TagsObject())
|
2021-08-17 11:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucketTagging from storage.
|
|
|
|
func (n *layer) DeleteBucketTagging(ctx context.Context, bucketName string) error {
|
|
|
|
bktInfo, err := n.GetBucketInfo(ctx, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-19 12:33:02 +00:00
|
|
|
return n.deleteSystemObject(ctx, bktInfo, formBucketTagObjectName(bucketName))
|
2021-08-17 11:23:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
// CopyObject from one bucket into another bucket.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) CopyObject(ctx context.Context, p *CopyObjectParams) (*data.ObjectInfo, error) {
|
2020-08-03 11:48:33 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := n.GetObject(ctx, &GetObjectParams{
|
2021-08-10 10:03:09 +00:00
|
|
|
ObjectInfo: p.SrcObject,
|
|
|
|
Writer: pw,
|
2021-11-25 15:05:58 +00:00
|
|
|
Range: p.Range,
|
2020-08-03 11:48:33 +00:00
|
|
|
})
|
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
if err = pw.CloseWithError(err); err != nil {
|
|
|
|
n.log.Error("could not get object", zap.Error(err))
|
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return n.PutObject(ctx, &PutObjectParams{
|
|
|
|
Bucket: p.DstBucket,
|
|
|
|
Object: p.DstObject,
|
2021-07-01 07:45:55 +00:00
|
|
|
Size: p.SrcSize,
|
2020-08-03 11:48:33 +00:00
|
|
|
Reader: pr,
|
2021-07-01 07:45:55 +00:00
|
|
|
Header: p.Header,
|
2020-08-03 11:48:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:41:52 +00:00
|
|
|
// DeleteObject removes all objects with passed nice name.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, obj *VersionedObject) *VersionedObject {
|
2020-10-19 01:04:37 +00:00
|
|
|
var (
|
|
|
|
err error
|
2022-02-08 16:54:04 +00:00
|
|
|
ids []*oid.ID
|
2020-10-19 01:04:37 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 09:02:08 +00:00
|
|
|
p := &PutObjectParams{
|
|
|
|
Object: obj.Name,
|
|
|
|
Reader: bytes.NewReader(nil),
|
|
|
|
Header: map[string]string{},
|
2021-08-10 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 09:02:08 +00:00
|
|
|
versioningEnabled := n.isVersioningEnabled(ctx, bkt)
|
2021-08-10 12:08:15 +00:00
|
|
|
|
2022-01-19 09:02:08 +00:00
|
|
|
// Current implementation doesn't consider "unversioned" mode (so any deletion creates "delete-mark" object).
|
|
|
|
// The reason is difficulties to determinate whether versioning mode is "unversioned" or "suspended".
|
|
|
|
|
|
|
|
if obj.VersionID == unversionedObjectVersionID || !versioningEnabled && len(obj.VersionID) == 0 {
|
|
|
|
p.Header[versionsUnversionedAttr] = "true"
|
|
|
|
versions, err := n.headVersions(ctx, bkt, obj.Name)
|
2021-09-07 06:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
obj.Error = err
|
|
|
|
return obj
|
|
|
|
}
|
2022-01-19 09:02:08 +00:00
|
|
|
last := versions.getLast(FromUnversioned())
|
|
|
|
if last == nil {
|
|
|
|
obj.Error = errors.GetAPIError(errors.ErrInvalidVersion)
|
|
|
|
return obj
|
2020-08-22 02:41:58 +00:00
|
|
|
}
|
2022-01-19 09:02:08 +00:00
|
|
|
p.Header[VersionsDeleteMarkAttr] = last.Version()
|
|
|
|
|
|
|
|
for _, unversioned := range versions.unversioned() {
|
|
|
|
ids = append(ids, unversioned.ID)
|
|
|
|
}
|
|
|
|
} else if len(obj.VersionID) != 0 {
|
|
|
|
version, err := n.checkVersionsExist(ctx, bkt, obj)
|
2021-08-10 12:08:15 +00:00
|
|
|
if err != nil {
|
2021-09-07 06:17:12 +00:00
|
|
|
obj.Error = err
|
|
|
|
return obj
|
2020-08-22 02:41:58 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
ids = []*oid.ID{version.ID}
|
2022-01-19 09:02:08 +00:00
|
|
|
if version.Headers[VersionsDeleteMarkAttr] == DelMarkFullObject {
|
|
|
|
obj.DeleteMarkVersion = version.Version()
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Header[versionsDelAttr] = obj.VersionID
|
|
|
|
p.Header[VersionsDeleteMarkAttr] = version.Version()
|
|
|
|
} else {
|
|
|
|
p.Header[VersionsDeleteMarkAttr] = DelMarkFullObject
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := n.objectPut(ctx, bkt, p)
|
|
|
|
if err != nil {
|
|
|
|
obj.Error = err
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
if len(obj.VersionID) == 0 {
|
|
|
|
obj.DeleteMarkVersion = objInfo.Version()
|
2020-08-20 19:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
2021-08-10 12:08:15 +00:00
|
|
|
if err = n.objectDelete(ctx, bkt.CID, id); err != nil {
|
2021-09-07 06:17:12 +00:00
|
|
|
obj.Error = err
|
|
|
|
return obj
|
2020-08-20 19:41:52 +00:00
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
if err = n.DeleteObjectTagging(ctx, &data.ObjectInfo{ID: id, Bucket: bkt.Name, Name: obj.Name}); err != nil {
|
2021-09-07 06:17:12 +00:00
|
|
|
obj.Error = err
|
|
|
|
return obj
|
2021-08-17 11:23:49 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
2021-09-01 16:10:52 +00:00
|
|
|
n.listsCache.CleanCacheEntriesContainingObject(obj.Name, bkt.CID)
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
return obj
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjects from the storage.
|
2021-09-07 06:17:12 +00:00
|
|
|
func (n *layer) DeleteObjects(ctx context.Context, bucket string, objects []*VersionedObject) ([]*VersionedObject, error) {
|
2021-08-10 12:08:15 +00:00
|
|
|
bkt, err := n.GetBucketInfo(ctx, bucket)
|
|
|
|
if err != nil {
|
2021-09-07 06:17:12 +00:00
|
|
|
return nil, err
|
2021-08-10 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
for i, obj := range objects {
|
|
|
|
objects[i] = n.deleteObject(ctx, bkt, obj)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 06:17:12 +00:00
|
|
|
return objects, nil
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
|
|
|
func (n *layer) CreateBucket(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) {
|
2022-02-15 09:06:00 +00:00
|
|
|
bktInfo, err := n.GetBucketInfo(ctx, p.Name)
|
2021-07-07 14:56:29 +00:00
|
|
|
if err != nil {
|
2021-08-09 08:53:58 +00:00
|
|
|
if errors.IsS3Error(err, errors.ErrNoSuchBucket) {
|
2021-07-07 14:56:29 +00:00
|
|
|
return n.createContainer(ctx, p)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:06:00 +00:00
|
|
|
if p.SessionToken != nil && bktInfo.Owner.Equal(p.SessionToken.OwnerID()) {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrBucketAlreadyOwnedByYou)
|
|
|
|
}
|
|
|
|
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrBucketAlreadyExists)
|
2021-06-23 20:21:15 +00:00
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
2021-11-22 09:16:05 +00:00
|
|
|
func (n *layer) ResolveBucket(ctx context.Context, name string) (*cid.ID, error) {
|
|
|
|
cnrID := cid.New()
|
|
|
|
if err := cnrID.Parse(name); err != nil {
|
2022-01-11 10:09:34 +00:00
|
|
|
return n.resolver.Resolve(ctx, name)
|
2021-11-22 09:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:25:00 +00:00
|
|
|
func (n *layer) DeleteBucket(ctx context.Context, p *DeleteBucketParams) error {
|
|
|
|
bucketInfo, err := n.GetBucketInfo(ctx, p.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
objects, err := n.listSortedObjects(ctx, allObjectParams{Bucket: bucketInfo})
|
2021-08-10 12:08:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(objects) != 0 {
|
2021-08-11 10:02:13 +00:00
|
|
|
return errors.GetAPIError(errors.ErrBucketNotEmpty)
|
2021-08-10 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if err = n.deleteContainer(ctx, bucketInfo.CID); err != nil {
|
|
|
|
return err
|
2021-08-10 08:58:40 +00:00
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
n.bucketCache.Delete(bucketInfo.Name)
|
|
|
|
return nil
|
2021-08-09 14:29:44 +00:00
|
|
|
}
|