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-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// PointerEntry - Path and Pointer are saved as a key/value pair to a `storage.KeyValueStore`.
|
|
|
|
type PointerEntry struct {
|
|
|
|
Path []byte
|
|
|
|
Pointer []byte
|
|
|
|
}
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Server implements the network state RPC service
|
|
|
|
type Server struct {
|
2018-06-13 19:22:32 +01:00
|
|
|
DB storage.KeyValueStore
|
2018-05-15 01:31:26 +01:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2018-06-13 19:22:32 +01:00
|
|
|
func NewServer(db storage.KeyValueStore, logger *zap.Logger) *Server {
|
2018-05-15 01:31:26 +01:00
|
|
|
return &Server{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-13 19:22:32 +01:00
|
|
|
pe := PointerEntry{
|
2018-05-30 03:47:40 +01:00
|
|
|
Path: putReq.Path,
|
|
|
|
Pointer: pointerBytes,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
if err := s.DB.Put(pe.Path, pe.Pointer); 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())
|
|
|
|
}
|
|
|
|
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
|
2018-06-13 19:22:32 +01:00
|
|
|
Paths: pathKeys.ByteSlices(),
|
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
|
|
|
}
|