Skip to content

Commit

Permalink
[IND-519] SQL: Add current user clause to every queries
Browse files Browse the repository at this point in the history
  • Loading branch information
maidmaid committed Jan 22, 2025
1 parent 37c9dc3 commit 7680a09
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ require (
github.com/seatgeek/logrus-gelf-formatter v0.0.0-20210414080842-5b05eb8ff761 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/zipkin v1.28.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,8 @@ github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY=
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmvncFJFHJ7Gvn9wZArjbV5/FppcK2fKk/tI=
github.com/yalue/merged_fs v1.2.2 h1:vXHTpJBluJryju7BBpytr3PDIkzsPMpiEknxVGPhN/I=
github.com/yalue/merged_fs v1.2.2/go.mod h1:WqqchfVYQyclV2tnR7wtRhBddzBvLVR83Cjw9BKQw0M=
Expand Down
31 changes: 29 additions & 2 deletions pkg/tsdb/sqleng/sql_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/xwb1989/sqlparser"
"net"
"regexp"
"runtime/debug"
Expand Down Expand Up @@ -183,7 +184,7 @@ func (e *DataSourceHandler) QueryData(ctx context.Context, req *backend.QueryDat
}

wg.Add(1)
go e.executeQuery(query, &wg, ctx, ch, queryjson)
go e.executeQuery(query, &wg, ctx, ch, queryjson, req)
}

wg.Wait()
Expand All @@ -199,7 +200,7 @@ func (e *DataSourceHandler) QueryData(ctx context.Context, req *backend.QueryDat
}

func (e *DataSourceHandler) executeQuery(query backend.DataQuery, wg *sync.WaitGroup, queryContext context.Context,
ch chan DBDataResponse, queryJson QueryJson) {
ch chan DBDataResponse, queryJson QueryJson, req *backend.QueryDataRequest) {
defer wg.Done()
queryResult := DBDataResponse{
dataResponse: backend.DataResponse{},
Expand Down Expand Up @@ -248,6 +249,12 @@ func (e *DataSourceHandler) executeQuery(query backend.DataQuery, wg *sync.WaitG
return
}

interpolatedQuery, err = whereUsernameEquals(interpolatedQuery, req.PluginContext.User.Login)
if err != nil {
errAppendDebug("add current user clause failed", e.TransformQueryError(logger, err), interpolatedQuery)
return
}

rows, err := e.db.QueryContext(queryContext, interpolatedQuery)
if err != nil {
errAppendDebug("db query error", e.TransformQueryError(logger, err), interpolatedQuery)
Expand Down Expand Up @@ -360,6 +367,26 @@ func (e *DataSourceHandler) executeQuery(query backend.DataQuery, wg *sync.WaitG
ch <- queryResult
}

func whereUsernameEquals(query string, username string) (string, error) {
stmt, err := sqlparser.Parse(query)
if err != nil {
return "", err
}

selectStmt, ok := stmt.(*sqlparser.Select)
if !ok {
return query, nil
}

selectStmt.AddWhere(&sqlparser.ComparisonExpr{
Operator: sqlparser.EqualStr,
Left: &sqlparser.ColName{Name: sqlparser.NewColIdent("username")},
Right: sqlparser.NewStrVal([]byte(username)),
})

return sqlparser.String(selectStmt), nil
}

// Interpolate provides global macros/substitutions for all sql datasources.
var Interpolate = func(query backend.DataQuery, timeRange backend.TimeRange, timeInterval string, sql string) string {
interval := query.Interval
Expand Down

0 comments on commit 7680a09

Please sign in to comment.