From 3527740ea1f436760e2675f4f7816d465ca124c5 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 19 Aug 2020 23:39:27 -0700 Subject: [PATCH] Use bitbucket.org/creachadair/shell to parse filter and query --- go.mod | 1 + go.sum | 2 ++ pkg/redis-custom.go | 19 ++++++++++++++----- pkg/redis-time-series.go | 18 +++++++++++++++--- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 2c2ec51..433ba8b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/redistimeseries/grafana-redis-datasource go 1.14 require ( + bitbucket.org/creachadair/shell v0.0.6 github.com/grafana/grafana-plugin-sdk-go v0.75.0 github.com/magefile/mage v1.10.0 // indirect github.com/mediocregopher/radix/v3 v3.5.2 diff --git a/go.sum b/go.sum index 1fb95d6..e82e35b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +bitbucket.org/creachadair/shell v0.0.6 h1:reJflDbKqnlnqb4Oo2pQ1/BqmY/eCWcNGHrIUO8qIzc= +bitbucket.org/creachadair/shell v0.0.6/go.mod h1:8Qqi/cYk7vPnsOePHroKXDJYmb5x7ENhtiFtfZq8K+M= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/pkg/redis-custom.go b/pkg/redis-custom.go index 4298fe5..0b13e51 100644 --- a/pkg/redis-custom.go +++ b/pkg/redis-custom.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" + "bitbucket.org/creachadair/shell" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/data" @@ -16,13 +17,21 @@ import ( * Can PANIC if command is wrong */ func (ds *redisDatasource) executeCustomQuery(qm queryModel, client *radix.Pool) (interface{}, error) { - // Split query and parse command - query := strings.Fields(qm.Query) - command, params := query[0], query[1:] - var result interface{} var err error + // Split query + query, ok := shell.Split(qm.Query) + + // Check if query is valid + if !ok { + err = fmt.Errorf("Query is not valid") + return result, err + } + + // Separate command from params + command, params := query[0], query[1:] + // Handle Panic from custom command to catch "should never get here" defer func() { if err := recover(); err != nil { @@ -36,7 +45,7 @@ func (ds *redisDatasource) executeCustomQuery(qm queryModel, client *radix.Pool) return result, err } - // Extract key or 1st paremeter as required for FlatCmd + // Extract key or 1st parameter as required for FlatCmd key, params := params[0], params[1:] err = client.Do(radix.FlatCmd(&result, command, key, params)) diff --git a/pkg/redis-time-series.go b/pkg/redis-time-series.go index 8abf0aa..214521d 100644 --- a/pkg/redis-time-series.go +++ b/pkg/redis-time-series.go @@ -3,9 +3,9 @@ package main import ( "fmt" "strconv" - "strings" "time" + "bitbucket.org/creachadair/shell" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/data" @@ -73,7 +73,13 @@ func (ds *redisDatasource) queryTsMRange(from int64, to int64, qm queryModel, cl var err error // Split Filter to array - filter := strings.Fields(qm.Filter) + filter, ok := shell.Split(qm.Filter) + + // Check if filter is valid + if !ok { + response.Error = fmt.Errorf("Filter is not valid") + return response + } // Execute command if qm.Aggregation != "" { @@ -283,7 +289,13 @@ func (ds *redisDatasource) queryTsQueryIndex(qm queryModel, client *radix.Pool) response := backend.DataResponse{} // Split Filter to array - filter := strings.Fields(qm.Filter) + filter, ok := shell.Split(qm.Filter) + + // Check if filter is valid + if !ok { + response.Error = fmt.Errorf("Filter is not valid") + return response + } // Execute command var values []string