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"
|
2019-01-04 16:26:26 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/x509"
|
2018-11-08 16:04:52 +00:00
|
|
|
"time"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-01-09 15:02:03 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
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-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"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"
|
2019-01-04 16:26:26 +00:00
|
|
|
"storj.io/storj/pkg/peertls"
|
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-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
|
|
|
logger *zap.Logger
|
2019-01-18 15:10:21 +00:00
|
|
|
service *Service
|
2018-10-11 15:35:55 +01:00
|
|
|
cache *overlay.Cache
|
2019-01-18 15:10:21 +00:00
|
|
|
config Config
|
2018-10-11 15:35:55 +01:00
|
|
|
identity *provider.FullIdentity
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2019-01-18 15:10:21 +00:00
|
|
|
func NewServer(logger *zap.Logger, service *Service, cache *overlay.Cache, config Config, identity *provider.FullIdentity) *Server {
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: reorder arguments
|
2018-05-15 01:31:26 +01:00
|
|
|
return &Server{
|
2018-10-11 15:35:55 +01:00
|
|
|
logger: logger,
|
2019-01-18 15:10:21 +00:00
|
|
|
service: service,
|
2018-10-11 15:35:55 +01:00
|
|
|
cache: cache,
|
2019-01-18 15:10:21 +00:00
|
|
|
config: config,
|
2018-10-11 15:35:55 +01:00
|
|
|
identity: identity,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Close closes resources
|
|
|
|
func (s *Server) Close() error { return nil }
|
|
|
|
|
|
|
|
// TODO: ZZZ temporarily disabled until endpoint and service split
|
|
|
|
const disableAuth = true
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
func (s *Server) validateAuth(ctx context.Context) error {
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: ZZZ temporarily disabled until endpoint and service split
|
|
|
|
if disableAuth {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
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-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-11-20 17:09:35 +00:00
|
|
|
remoteSize := req.GetPointer().GetSegmentSize()
|
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
|
|
|
}
|
|
|
|
|
2019-01-14 21:19:15 +00:00
|
|
|
max := s.config.MaxInlineSegmentSize.Int()
|
2018-10-08 21:09:28 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
if err = s.service.Put(req.GetPath(), req.GetPointer()); 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-09 15:39:14 +01:00
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
pointer, err := s.service.Get(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-11-26 18:21:44 +00:00
|
|
|
pba, err := s.PayerBandwidthAllocation(ctx, &pb.PayerBandwidthAllocationRequest{Action: pb.PayerBandwidthAllocation_GET})
|
2018-10-30 16:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err getting payer bandwidth allocation", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
authorization, err := s.getSignedMessage()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err getting signed message", 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{
|
2018-11-05 15:12:19 +00:00
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nil,
|
2018-11-26 18:21:44 +00:00
|
|
|
Pba: pba.GetPba(),
|
2018-11-05 15:12:19 +00:00
|
|
|
Authorization: authorization,
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-02 18:47:34 +00:00
|
|
|
for _, v := range nodes {
|
|
|
|
v.Type.DPanicOnInvalid("pdb server Get")
|
|
|
|
}
|
2018-10-04 22:00:19 +01:00
|
|
|
r = &pb.GetResponse{
|
2018-11-05 15:12:19 +00:00
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nodes,
|
2018-11-26 18:21:44 +00:00
|
|
|
Pba: pba.GetPba(),
|
2018-11-05 15:12:19 +00:00
|
|
|
Authorization: authorization,
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
items, more, err := s.service.List(req.Prefix, req.StartAfter, req.EndBefore, req.Recursive, req.Limit, req.MetaFlags)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ListResponse{Items: items, More: more}, nil
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
err = s.service.Delete(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-11-29 20:59:26 +00:00
|
|
|
|
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
|
2019-01-10 12:07:08 +00:00
|
|
|
func (s *Server) Iterate(ctx context.Context, req *pb.IterateRequest, f func(it storage.Iterator) error) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
return s.service.Iterate(req.Prefix, req.First, req.Recurse, req.Reverse, f)
|
2018-10-11 15:35:55 +01:00
|
|
|
}
|
2018-10-30 16:24:46 +00:00
|
|
|
|
2018-11-26 18:21:44 +00:00
|
|
|
// PayerBandwidthAllocation returns PayerBandwidthAllocation struct, signed and with given action type
|
2019-01-10 12:07:08 +00:00
|
|
|
func (s *Server) PayerBandwidthAllocation(ctx context.Context, req *pb.PayerBandwidthAllocationRequest) (pba *pb.PayerBandwidthAllocationResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = s.validateAuth(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
payer := s.identity.ID
|
2018-10-30 16:24:46 +00:00
|
|
|
|
|
|
|
// TODO(michal) should be replaced with renter id when available
|
2019-01-04 16:26:26 +00:00
|
|
|
// retrieve the public key
|
|
|
|
pi, err := provider.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, ok := pi.Leaf.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, peertls.ErrUnsupportedKey.New("%T", pi.Leaf.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
pubbytes, err := x509.MarshalPKIXPublicKey(pk)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Can't Marshal Public Key for PayerBandwidthAllocation: %+v", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:02:03 +00:00
|
|
|
serialNum, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:30:55 +00:00
|
|
|
created := time.Now().Unix()
|
|
|
|
|
|
|
|
// convert ttl from days to seconds
|
|
|
|
ttl := s.config.BwExpiration
|
|
|
|
ttl *= 86400
|
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
pbad := &pb.PayerBandwidthAllocation_Data{
|
2019-01-10 18:30:55 +00:00
|
|
|
SatelliteId: payer,
|
|
|
|
UplinkId: pi.ID,
|
|
|
|
CreatedUnixSec: created,
|
|
|
|
ExpirationUnixSec: created + int64(ttl),
|
|
|
|
Action: req.GetAction(),
|
|
|
|
SerialNumber: serialNum.String(),
|
|
|
|
PubKey: pubbytes,
|
2018-10-30 19:03:41 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2018-11-26 18:21:44 +00:00
|
|
|
return &pb.PayerBandwidthAllocationResponse{Pba: &pb.PayerBandwidthAllocation{Signature: signature, Data: data}}, nil
|
2018-10-30 16:24:46 +00:00
|
|
|
}
|
2018-11-05 15:12:19 +00:00
|
|
|
|
|
|
|
func (s *Server) getSignedMessage() (*pb.SignedMessage, error) {
|
|
|
|
signature, err := auth.GenerateSignature(s.identity.ID.Bytes(), s.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth.NewSignedMessage(signature, s.identity)
|
|
|
|
}
|