2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-06 20:43:53 +01:00
|
|
|
package pointerdb
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-12 09:52:32 +01:00
|
|
|
"encoding/base64"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-07-27 07:02:59 +01:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-08-27 18:28:16 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-05-15 01:31:26 +01:00
|
|
|
"go.uber.org/zap"
|
2018-10-11 15:35:55 +01:00
|
|
|
"google.golang.org/grpc"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
2018-10-11 15:35:55 +01:00
|
|
|
"google.golang.org/grpc/metadata"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2018-08-22 16:07:00 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
"storj.io/storj/pkg/auth"
|
2018-10-04 22:00:19 +01:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-09 15:39:14 +01:00
|
|
|
pointerdbAuth "storj.io/storj/pkg/pointerdb/auth"
|
2018-10-11 15:35:55 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-30 19:57:50 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
2018-08-22 16:07:00 +01:00
|
|
|
var (
|
2018-08-27 18:28:16 +01:00
|
|
|
mon = monkit.Package()
|
2018-08-27 15:04:46 +01:00
|
|
|
segmentError = errs.Class("segment error")
|
2018-08-22 16:07:00 +01:00
|
|
|
)
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Server implements the network state RPC service
|
|
|
|
type Server struct {
|
2018-10-11 15:35:55 +01:00
|
|
|
DB storage.KeyValueStore
|
|
|
|
logger *zap.Logger
|
|
|
|
config Config
|
|
|
|
cache *overlay.Cache
|
|
|
|
identity *provider.FullIdentity
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2018-10-11 15:35:55 +01:00
|
|
|
func NewServer(db storage.KeyValueStore, cache *overlay.Cache, logger *zap.Logger, c Config, identity *provider.FullIdentity) *Server {
|
2018-05-15 01:31:26 +01:00
|
|
|
return &Server{
|
2018-10-11 15:35:55 +01:00
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
config: c,
|
|
|
|
cache: cache,
|
|
|
|
identity: identity,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
func (s *Server) validateAuth(ctx context.Context) error {
|
|
|
|
APIKey, ok := auth.GetAPIKey(ctx)
|
|
|
|
if !ok || !pointerdbAuth.ValidateAPIKey(string(APIKey)) {
|
2018-08-27 18:28:16 +01:00
|
|
|
s.logger.Error("unauthorized request: ", zap.Error(status.Errorf(codes.Unauthenticated, "Invalid API credential")))
|
|
|
|
return status.Errorf(codes.Unauthenticated, "Invalid API credential")
|
2018-06-04 17:45:07 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:35:55 +01:00
|
|
|
func (s *Server) appendSignature(ctx context.Context) error {
|
2018-10-30 16:24:46 +00:00
|
|
|
signature, err := auth.GenerateSignature(s.identity.ID.Bytes(), s.identity)
|
2018-10-11 15:35:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if signature == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
base64 := base64.StdEncoding
|
|
|
|
encodedSignature := base64.EncodeToString(signature)
|
|
|
|
return grpc.SetHeader(ctx, metadata.Pairs("signature", encodedSignature))
|
|
|
|
}
|
|
|
|
|
2018-08-27 15:04:46 +01:00
|
|
|
func (s *Server) validateSegment(req *pb.PutRequest) error {
|
2018-10-08 21:09:28 +01:00
|
|
|
min := s.config.MinRemoteSegmentSize
|
2018-08-27 15:04:46 +01:00
|
|
|
remote := req.GetPointer().Remote
|
2018-10-08 21:09:28 +01:00
|
|
|
remoteSize := req.GetPointer().GetSize()
|
2018-08-27 15:04:46 +01:00
|
|
|
|
2018-10-08 21:09:28 +01:00
|
|
|
if remote != nil && remoteSize < int64(min) {
|
|
|
|
return segmentError.New("remote segment size %d less than minimum allowed %d", remoteSize, min)
|
2018-08-27 15:04:46 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:09:28 +01:00
|
|
|
max := s.config.MaxInlineSegmentSize
|
|
|
|
inlineSize := len(req.GetPointer().InlineSegment)
|
|
|
|
|
2018-08-27 15:04:46 +01:00
|
|
|
if inlineSize > max {
|
|
|
|
return segmentError.New("inline segment size %d greater than maximum allowed %d", inlineSize, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
// Put formats and hands off a key/value (path/pointer) to be saved to boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Put(ctx context.Context, req *pb.PutRequest) (resp *pb.PutResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-08-27 15:04:46 +01:00
|
|
|
err = s.validateSegment(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// Update the pointer with the creation date
|
|
|
|
req.GetPointer().CreationDate = ptypes.TimestampNow()
|
|
|
|
|
|
|
|
pointerBytes, err := proto.Marshal(req.GetPointer())
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err marshaling pointer", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// TODO(kaloyan): make sure that we know we are overwriting the pointer!
|
|
|
|
// In such case we should delete the pieces of the old segment if it was
|
|
|
|
// a remote one.
|
|
|
|
if err = s.DB.Put([]byte(req.GetPath()), pointerBytes); err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Error("err putting pointer", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.PutResponse{}, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get formats and hands off a file path to get from boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (resp *pb.GetResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
s.logger.Debug("entering pointerdb get")
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:35:55 +01:00
|
|
|
if err = s.appendSignature(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
pointerBytes, err := s.DB.Get([]byte(req.GetPath()))
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-08-14 16:22:29 +01:00
|
|
|
if storage.ErrKeyNotFound.Has(err) {
|
|
|
|
return nil, status.Errorf(codes.NotFound, err.Error())
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
s.logger.Error("err getting pointer", zap.Error(err))
|
2018-05-30 03:47:40 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:00:19 +01:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(pointerBytes, pointer)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Error unmarshaling pointer")
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-30 16:24:46 +00:00
|
|
|
|
|
|
|
pba, err := s.getPayerBandwidthAllocation(ctx)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err getting payer bandwidth allocation", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:00:19 +01:00
|
|
|
nodes := []*pb.Node{}
|
|
|
|
|
|
|
|
var r = &pb.GetResponse{
|
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nil,
|
2018-10-30 16:24:46 +00:00
|
|
|
Pba: pba,
|
2018-10-04 22:00:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !s.config.Overlay || pointer.Remote == nil {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, piece := range pointer.Remote.RemotePieces {
|
|
|
|
node, err := s.cache.Get(ctx, piece.NodeId)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Error getting node from cache")
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &pb.GetResponse{
|
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nodes,
|
2018-10-30 16:24:46 +00:00
|
|
|
Pba: pba,
|
2018-10-04 22:00:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
// List returns all Path keys in the Pointers bucket
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) List(ctx context.Context, req *pb.ListRequest) (resp *pb.ListResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
var prefix storage.Key
|
|
|
|
if req.Prefix != "" {
|
|
|
|
prefix = storage.Key(req.Prefix)
|
|
|
|
if prefix[len(prefix)-1] != storage.Delimiter {
|
|
|
|
prefix = append(prefix, storage.Delimiter)
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
rawItems, more, err := storage.ListV2(s.DB, storage.ListOptions{
|
|
|
|
Prefix: prefix, //storage.Key(req.Prefix),
|
|
|
|
StartAfter: storage.Key(req.StartAfter),
|
|
|
|
EndBefore: storage.Key(req.EndBefore),
|
|
|
|
Recursive: req.Recursive,
|
|
|
|
Limit: int(req.Limit),
|
|
|
|
IncludeValue: req.MetaFlags != meta.None,
|
|
|
|
})
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
2018-09-07 15:20:15 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, "ListV2: %v", err)
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var items []*pb.ListResponse_Item
|
2018-09-07 15:20:15 +01:00
|
|
|
for _, rawItem := range rawItems {
|
2018-09-09 07:34:23 +01:00
|
|
|
items = append(items, s.createListItem(rawItem, req.MetaFlags))
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ListResponse{Items: items, More: more}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
// createListItem creates a new list item with the given path. It also adds
|
|
|
|
// the metadata according to the given metaFlags.
|
2018-09-09 07:34:23 +01:00
|
|
|
func (s *Server) createListItem(rawItem storage.ListItem, metaFlags uint32) *pb.ListResponse_Item {
|
2018-09-07 15:20:15 +01:00
|
|
|
item := &pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
Path: rawItem.Key.String(),
|
2018-09-07 15:20:15 +01:00
|
|
|
IsPrefix: rawItem.IsPrefix,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-09-07 15:20:15 +01:00
|
|
|
if item.IsPrefix {
|
|
|
|
return item
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
err := s.setMetadata(item, rawItem.Value, metaFlags)
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Warn("err retrieving metadata", zap.Error(err))
|
|
|
|
}
|
|
|
|
return item
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// getMetadata adds the metadata to the given item pointer according to the
|
|
|
|
// given metaFlags
|
2018-09-07 15:20:15 +01:00
|
|
|
func (s *Server) setMetadata(item *pb.ListResponse_Item, data []byte, metaFlags uint32) (err error) {
|
|
|
|
if metaFlags == meta.None || len(data) == 0 {
|
2018-07-27 07:02:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := &pb.Pointer{}
|
2018-09-07 15:20:15 +01:00
|
|
|
err = proto.Unmarshal(data, pr)
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
// Start with an empty pointer to and add only what's requested in
|
|
|
|
// metaFlags to safe to transfer payload
|
|
|
|
item.Pointer = &pb.Pointer{}
|
|
|
|
if metaFlags&meta.Modified != 0 {
|
|
|
|
item.Pointer.CreationDate = pr.GetCreationDate()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
if metaFlags&meta.Expiration != 0 {
|
|
|
|
item.Pointer.ExpirationDate = pr.GetExpirationDate()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
if metaFlags&meta.Size != 0 {
|
|
|
|
item.Pointer.Size = pr.GetSize()
|
|
|
|
}
|
|
|
|
if metaFlags&meta.UserDefined != 0 {
|
|
|
|
item.Pointer.Metadata = pr.GetMetadata()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Delete formats and hands off a file path to delete from boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Delete(ctx context.Context, req *pb.DeleteRequest) (resp *pb.DeleteResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-06 20:43:53 +01:00
|
|
|
s.logger.Debug("entering pointerdb delete")
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
err = s.DB.Delete([]byte(req.GetPath()))
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-06-29 21:06:25 +01:00
|
|
|
s.logger.Error("err deleting path and pointer", zap.Error(err))
|
2018-05-30 03:47:40 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
s.logger.Debug("deleted pointer at path: " + req.GetPath())
|
2018-05-30 03:47:40 +01:00
|
|
|
return &pb.DeleteResponse{}, nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
// Iterate iterates over items based on IterateRequest
|
|
|
|
func (s *Server) Iterate(ctx context.Context, req *pb.IterateRequest, f func(it storage.Iterator) error) error {
|
|
|
|
opts := storage.IterateOptions{
|
2018-10-11 15:35:55 +01:00
|
|
|
Prefix: storage.Key(req.Prefix),
|
|
|
|
First: storage.Key(req.First),
|
2018-10-09 17:09:33 +01:00
|
|
|
Recurse: req.Recurse,
|
|
|
|
Reverse: req.Reverse,
|
|
|
|
}
|
|
|
|
return s.DB.Iterate(opts, f)
|
2018-10-11 15:35:55 +01:00
|
|
|
}
|
2018-10-30 16:24:46 +00:00
|
|
|
|
|
|
|
func (s *Server) getPayerBandwidthAllocation(ctx context.Context) (*pb.PayerBandwidthAllocation, error) {
|
|
|
|
payer := s.identity.ID.Bytes()
|
|
|
|
|
|
|
|
// TODO(michal) should be replaced with renter id when available
|
|
|
|
peerIdentity, err := provider.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-30 19:03:41 +00:00
|
|
|
pbad := &pb.PayerBandwidthAllocation_Data{
|
|
|
|
SatelliteId: payer,
|
|
|
|
UplinkId: peerIdentity.ID.Bytes(),
|
|
|
|
// TODO: Action: pb.PayerBandwidthAllocation_GET, // Action should be a GET or a PUT
|
|
|
|
}
|
2018-10-30 16:24:46 +00:00
|
|
|
|
|
|
|
data, err := proto.Marshal(pbad)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signature, err := auth.GenerateSignature(data, s.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pb.PayerBandwidthAllocation{Signature: signature, Data: data}, nil
|
|
|
|
}
|