From 7d30b72d432aa9578fee8dcfac0e8760dec2bef2 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Tue, 21 Feb 2017 10:09:49 -0600 Subject: [PATCH] add restriction database --- client.go | 10 ++++++++++ cmd/pilosactl/main.go | 2 ++ db.go | 5 ++++- fragment.go | 6 +++--- frame.go | 9 +++++++-- pilosa.go | 16 ++++++++++++++++ pilosactl/import.go | 10 ++++++++++ 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 5c24920ff..bc6338053 100644 --- a/client.go +++ b/client.go @@ -149,6 +149,11 @@ func (c *Client) ExecuteQuery(ctx context.Context, db, query string, allowRedire return nil, ErrQueryRequired } + er := ValidateName(db) + if er != nil { + return nil, ErrName + } + // Encode query request. buf, err := proto.Marshal(&internal.QueryRequest{ DB: db, @@ -205,6 +210,11 @@ func (c *Client) ExecutePQL(ctx context.Context, db, query string) (interface{}, }.Encode(), } + er := ValidateName(db) + if er != nil { + return nil, ErrName + } + req, err := http.NewRequest("POST", u.String(), bytes.NewReader([]byte(query))) if err != nil { return nil, err diff --git a/cmd/pilosactl/main.go b/cmd/pilosactl/main.go index 331459c31..7c25d62cb 100644 --- a/cmd/pilosactl/main.go +++ b/cmd/pilosactl/main.go @@ -290,6 +290,7 @@ func (cmd *ExportCommand) Run(ctx context.Context) error { logger := log.New(cmd.Stderr, "", log.LstdFlags) // Validate arguments. + fmt.Print("1213224") if cmd.Database == "" { return pilosa.ErrDatabaseRequired } else if cmd.Frame == "" { @@ -959,6 +960,7 @@ func (cmd *BenchCommand) Run(ctx context.Context) error { // runSetBit executes a benchmark of random SetBit() operations. func (cmd *BenchCommand) runSetBit(ctx context.Context, client *pilosa.Client) error { + fmt.Print("Nodb") if cmd.N == 0 { return errors.New("operation count required") } else if cmd.Database == "" { diff --git a/db.go b/db.go index 2a4f1d6e0..9d4c6532a 100644 --- a/db.go +++ b/db.go @@ -274,7 +274,10 @@ func (db *DB) createFrameIfNotExists(name string) (*Frame, error) { } func (db *DB) newFrame(path, name string) *Frame { - f := NewFrame(path, db.name, name) + f, err := NewFrame(path, db.name, name) + if err != nil { + return nil + } f.LogOutput = db.LogOutput f.stats = db.stats.WithTags(fmt.Sprintf("frame:%s", name)) return f diff --git a/fragment.go b/fragment.go index 1181f1739..9bc542f93 100644 --- a/fragment.go +++ b/fragment.go @@ -213,11 +213,11 @@ func (f *Fragment) openCache() error { // Determine cache type from frame name. if strings.HasSuffix(f.frame, FrameSuffixRank) { c := NewRankCache() - c.ThresholdLength = 50000 - c.ThresholdIndex = 45000 + c.ThresholdLength = 500000 + c.ThresholdIndex = 450000 f.cache = c } else { - f.cache = NewLRUCache(50000) + f.cache = NewLRUCache(500000) } // Read cache data from disk. diff --git a/frame.go b/frame.go index 9cdb4c707..1971900e4 100644 --- a/frame.go +++ b/frame.go @@ -38,7 +38,12 @@ type Frame struct { } // NewFrame returns a new instance of frame. -func NewFrame(path, db, name string) *Frame { +func NewFrame(path, db, name string) (*Frame, error) { + err := ValidateName(db) + if err != nil { + return nil, err + } + return &Frame{ path: path, db: db, @@ -50,7 +55,7 @@ func NewFrame(path, db, name string) *Frame { stats: NopStatsClient, LogOutput: ioutil.Discard, - } + }, nil } // Name returns the name the frame was initialized with. diff --git a/pilosa.go b/pilosa.go index dff78ebb1..1a8bba840 100644 --- a/pilosa.go +++ b/pilosa.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/pilosa/pilosa/internal" + "regexp" ) var ( @@ -16,6 +17,9 @@ var ( // ErrFrameRequired is returned when no frame is specified. ErrFrameRequired = errors.New("frame required") + // ErrFrameRequired is returned when no frame is specified. + ErrName = errors.New("name restricted to [a-z0-9_-.]") + // ErrFragmentNotFound is returned when a fragment does not exist. ErrFragmentNotFound = errors.New("fragment not found") @@ -75,3 +79,15 @@ func decodeProfile(pb *internal.Profile) *Profile { // TimeFormat is the go-style time format used to parse string dates. const TimeFormat = "2006-01-02T15:04" + + +// Restrict name using regex +func ValidateName(name string) error { + expr := regexp.MustCompile(`^([a-z0-9._-]{2,64}$)`) + validName := expr.FindStringSubmatchIndex(name) + + if len(validName) == 0 { + return ErrName + } + return nil +} \ No newline at end of file diff --git a/pilosactl/import.go b/pilosactl/import.go index 9d4ad63f4..40a735c61 100644 --- a/pilosactl/import.go +++ b/pilosactl/import.go @@ -15,6 +15,7 @@ import ( "time" "github.com/pilosa/pilosa" + "regexp" ) // ImportCommand represents a command for bulk importing data. @@ -104,7 +105,16 @@ func (cmd *ImportCommand) Run(ctx context.Context) error { } else if len(cmd.Paths) == 0 { return errors.New("path required") } + // Restrict frame name and database name with regex + dbError := pilosa.ValidateName(cmd.Database) + if dbError != nil { + return dbError + } + frameError := pilosa.ValidateName(cmd.Frame) + if frameError != nil { + return frameError + } // Create a client to the server. client, err := pilosa.NewClient(cmd.Host) if err != nil {