datasources: querier: add user to query (#109917)

This commit is contained in:
Gábor Farkas
2025-09-03 09:29:26 +02:00
committed by GitHub
parent cb77e97996
commit 0bfec936b3
2 changed files with 19 additions and 2 deletions
+12 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
dataapi "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1" dataapi "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
"github.com/grafana/grafana/pkg/apimachinery/identity"
queryapi "github.com/grafana/grafana/pkg/apis/query/v0alpha1" queryapi "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
"github.com/grafana/grafana/pkg/expr" "github.com/grafana/grafana/pkg/expr"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
@@ -45,6 +46,14 @@ func loadTestdataFrames(t *testing.T, filename string) *backend.QueryDataRespons
return result return result
} }
type mockUser struct {
identity.Requester
}
func (mu mockUser) GetOrgID() int64 {
return -1
}
func TestQueryAPI(t *testing.T) { func TestQueryAPI(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
@@ -167,7 +176,9 @@ func TestQueryAPI(t *testing.T) {
legacyDatasourceLookup: &mockLegacyDataSourceLookup{}, legacyDatasourceLookup: &mockLegacyDataSourceLookup{},
} }
req := httptest.NewRequest(http.MethodPost, "/some-path", bytes.NewReader([]byte(tc.queryJSON))) reqCtx := identity.WithRequester(context.Background(), mockUser{})
req := httptest.NewRequestWithContext(reqCtx, http.MethodPost, "/some-path", bytes.NewReader([]byte(tc.queryJSON)))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
// Set optional headers // Set optional headers
+7 -1
View File
@@ -234,7 +234,13 @@ func QueryData(ctx context.Context, log log.Logger, dscache datasources.CacheSer
headers: headers, headers: headers,
concurrentQueryLimit: 16, // TODO: make it configurable concurrentQueryLimit: 16, // TODO: make it configurable
} }
return s.QueryDataNew(ctx, nil, false, reqDTO)
user, err := identity.GetRequester(ctx)
if err != nil {
return nil, err
}
return s.QueryDataNew(ctx, user, false, reqDTO)
} }
// handleExpressions handles queries when there is an expression. // handleExpressions handles queries when there is an expression.