sql: better client-side handling and validation

This commit is contained in:
boyska 2018-12-25 03:04:34 +01:00
parent 8bb28d7a7c
commit 89419185ed

View file

@ -21,21 +21,34 @@ import (
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
) )
// TODO: type ExpressionValue type ExprValue struct {
Node expr.Node
}
func (e *ExprValue) String() string {
if e.Node != nil {
return e.Node.String()
} else {
return "<Empty Expression>"
}
}
func (e *ExprValue) Set(value string) error {
ast, err := expr.ParseExpression(value)
if err != nil {
return err
}
e.Node = ast
return nil
}
func main() { func main() {
addr := flag.String("addr", "localhost:9080", "http service address") addr := flag.String("addr", "localhost:9080", "http service address")
querySocket := flag.String("socket", "", "Path to a unix domain socket for the HTTP server") querySocket := flag.String("socket", "", "Path to a unix domain socket for the HTTP server")
backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)") backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)")
filter := flag.String("where", "", "sql-like query to filter logs") var filter ExprValue
flag.Var(&filter, "where", "sql-like query to filter logs")
flag.Parse() flag.Parse()
filterExpr, err := expr.ParseExpression(*filter)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid filter:", err)
os.Exit(2)
}
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
var d *websocket.Dialer var d *websocket.Dialer
@ -85,18 +98,20 @@ func main() {
log.Println("invalid BSON", err) log.Println("invalid BSON", err)
continue continue
} }
if filter.Node != nil {
context := datasource.NewContextSimpleNative(parsed) context := datasource.NewContextSimpleNative(parsed)
val, ok := vm.Eval(context, filterExpr) val, ok := vm.Eval(context, filter.Node)
if !ok || val == nil { // errors when evaluating if !ok || val == nil { // errors when evaluating
continue continue
} }
if val.Type() != value.BoolType { if val.Type() != value.BoolType {
fmt.Fprintln(os.Stderr, "WARNING: The expression doesn't return a boolean") fmt.Fprintln(os.Stderr, "WARNING: The 'where' expression doesn't return a boolean")
continue continue
} }
if val.Value().(bool) != true { if val.Value().(bool) != true {
continue continue
} }
}
if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil { if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil {
log.Println("error printing", err) log.Println("error printing", err)
} }