Segment validation (#280)

* WIP adding inline segment size validation

* add better error handling

* updates error handling and removes unnecessary logs

* update minimum inline size check

* update check if condition and inline min config type
This commit is contained in:
Dylan Lott 2018-08-27 08:04:46 -06:00 committed by Brandon Iglesias
parent 2e5b54a435
commit 7e52c81ebc
2 changed files with 31 additions and 2 deletions

View File

@ -18,6 +18,8 @@ import (
// PointerDB responsibility // PointerDB responsibility
type Config struct { type Config struct {
DatabaseURL string `help:"the database connection string to use" default:"bolt://$CONFDIR/pointerdb.db"` DatabaseURL string `help:"the database connection string to use" default:"bolt://$CONFDIR/pointerdb.db"`
MinInlineSegmentSize int64 `default:"1240" help:"minimum inline segment size"`
MaxInlineSegmentSize int `default:"8000" help:"maximum inline segment size"`
} }
// Run implements the provider.Responsibility interface // Run implements the provider.Responsibility interface
@ -35,7 +37,7 @@ func (c Config) Run(ctx context.Context, server *provider.Provider) error {
} }
defer func() { _ = bdb.Close() }() defer func() { _ = bdb.Close() }()
proto.RegisterPointerDBServer(server.GRPC(), NewServer(bdb, zap.L())) proto.RegisterPointerDBServer(server.GRPC(), NewServer(bdb, zap.L(), c))
return server.Run(ctx) return server.Run(ctx)
} }

View File

@ -7,6 +7,7 @@ import (
"context" "context"
"reflect" "reflect"
"github.com/zeebo/errs"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes"
"go.uber.org/zap" "go.uber.org/zap"
@ -24,6 +25,7 @@ import (
var ( var (
mon = monkit.Package() mon = monkit.Package()
segmentError = errs.Class("segment error")
) )
// ListPageLimit is the maximum number of items that will be returned by a list // ListPageLimit is the maximum number of items that will be returned by a list
@ -31,17 +33,20 @@ var (
// TODO(kaloyan): make it configurable // TODO(kaloyan): make it configurable
const ListPageLimit = 1000 const ListPageLimit = 1000
// Server implements the network state RPC service // Server implements the network state RPC service
type Server struct { type Server struct {
DB storage.KeyValueStore DB storage.KeyValueStore
logger *zap.Logger logger *zap.Logger
config Config
} }
// NewServer creates instance of Server // NewServer creates instance of Server
func NewServer(db storage.KeyValueStore, logger *zap.Logger) *Server { func NewServer(db storage.KeyValueStore, logger *zap.Logger, c Config) *Server {
return &Server{ return &Server{
DB: db, DB: db,
logger: logger, logger: logger,
config: c,
} }
} }
@ -53,11 +58,33 @@ func (s *Server) validateAuth(APIKey []byte) error {
return nil return nil
} }
func (s *Server) validateSegment(req *pb.PutRequest) error {
min := s.config.MinInlineSegmentSize
max := s.config.MaxInlineSegmentSize
inlineSize := len(req.GetPointer().InlineSegment)
remote := req.GetPointer().Remote
if remote != nil && req.GetPointer().GetSize() < min {
return segmentError.New("inline segment size %d less than minimum allowed %d", inlineSize, min)
}
if inlineSize > max {
return segmentError.New("inline segment size %d greater than maximum allowed %d", inlineSize, max)
}
return nil
}
// Put formats and hands off a key/value (path/pointer) to be saved to boltdb // Put formats and hands off a key/value (path/pointer) to be saved to boltdb
func (s *Server) Put(ctx context.Context, req *pb.PutRequest) (resp *pb.PutResponse, err error) { func (s *Server) Put(ctx context.Context, req *pb.PutRequest) (resp *pb.PutResponse, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
s.logger.Debug("entering pointerdb put") s.logger.Debug("entering pointerdb put")
err = s.validateSegment(req)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
if err = s.validateAuth(req.GetAPIKey()); err != nil { if err = s.validateAuth(req.GetAPIKey()); err != nil {
return nil, err return nil, err
} }