2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package netstate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-05-15 01:31:26 +01:00
|
|
|
"go.uber.org/zap"
|
2018-06-04 17:45:07 +01:00
|
|
|
"google.golang.org/grpc"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
"storj.io/storj/netstate/auth"
|
2018-05-30 03:47:40 +01:00
|
|
|
pb "storj.io/storj/protos/netstate"
|
2018-05-15 01:31:26 +01:00
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the network state RPC service
|
|
|
|
type Server struct {
|
|
|
|
DB DB
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
|
|
|
func NewServer(db DB, logger *zap.Logger) *Server {
|
|
|
|
return &Server{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB interface allows more modular unit testing
|
|
|
|
// and makes it easier in the future to substitute
|
|
|
|
// db clients other than bolt
|
|
|
|
type DB interface {
|
2018-05-30 03:47:40 +01:00
|
|
|
Put(boltdb.PointerEntry) error
|
2018-05-15 01:31:26 +01:00
|
|
|
Get([]byte) ([]byte, error)
|
|
|
|
List() ([][]byte, error)
|
|
|
|
Delete([]byte) error
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
func (s *Server) validateAuth(APIKeyBytes []byte) error {
|
|
|
|
if !auth.ValidateAPIKey(string(APIKeyBytes)) {
|
|
|
|
s.logger.Error("unauthorized request: ", zap.Error(grpc.Errorf(codes.Unauthenticated, "Invalid API credential")))
|
|
|
|
return grpc.Errorf(codes.Unauthenticated, "Invalid API credential")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Put formats and hands off a file path to be saved to boltdb
|
2018-05-30 03:47:40 +01:00
|
|
|
func (s *Server) Put(ctx context.Context, putReq *pb.PutRequest) (*pb.PutResponse, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
s.logger.Debug("entering netstate put")
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKeyBytes := []byte(putReq.APIKey)
|
|
|
|
if err := s.validateAuth(APIKeyBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
pointerBytes, err := proto.Marshal(putReq.Pointer)
|
|
|
|
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-05-30 03:47:40 +01:00
|
|
|
pe := boltdb.PointerEntry{
|
|
|
|
Path: putReq.Path,
|
|
|
|
Pointer: pointerBytes,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
if err := s.DB.Put(pe); err != nil {
|
|
|
|
s.logger.Error("err putting pointer", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
s.logger.Debug("put to the db: " + string(pe.Path))
|
|
|
|
|
|
|
|
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-05-30 03:47:40 +01:00
|
|
|
func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
s.logger.Debug("entering netstate get")
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKeyBytes := []byte(req.APIKey)
|
|
|
|
if err := s.validateAuth(APIKeyBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
pointerBytes, err := s.DB.Get(req.Path)
|
2018-06-04 17:45:07 +01:00
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err getting file", 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-05-30 03:47:40 +01:00
|
|
|
return &pb.GetResponse{
|
|
|
|
Pointer: pointerBytes,
|
2018-05-15 01:31:26 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
// List calls the bolt client's List function and returns all Path keys in the Pointers bucket
|
|
|
|
func (s *Server) List(ctx context.Context, req *pb.ListRequest) (*pb.ListResponse, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
s.logger.Debug("entering netstate list")
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKeyBytes := []byte(req.APIKey)
|
|
|
|
if err := s.validateAuth(APIKeyBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
pathKeys, err := s.DB.List()
|
2018-06-04 17:45:07 +01:00
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Error("err listing path keys", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Debug("path keys retrieved")
|
|
|
|
return &pb.ListResponse{
|
|
|
|
// pathKeys is an array of byte arrays
|
|
|
|
Paths: pathKeys,
|
2018-05-15 01:31:26 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete formats and hands off a file path to delete from boltdb
|
2018-05-30 03:47:40 +01:00
|
|
|
func (s *Server) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.DeleteResponse, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
s.logger.Debug("entering netstate delete")
|
|
|
|
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKeyBytes := []byte(req.APIKey)
|
|
|
|
if err := s.validateAuth(APIKeyBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
err := s.DB.Delete(req.Path)
|
|
|
|
if err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Error("err deleting pointer entry", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Debug("deleted pointer at path: " + string(req.Path))
|
|
|
|
return &pb.DeleteResponse{}, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|