Skip to content

Commit

Permalink
Merge pull request #7 from Enapter/rnovatorov/handle-anonymous-users-…
Browse files Browse the repository at this point in the history
…correctly

Handle anonymous users correctly
  • Loading branch information
rnovatorov authored Jul 4, 2022
2 parents d120b93 + 702ee2f commit c94519e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.2.2

- Handle anonymous users correctly.

## v1.2.1

- Return more user-friendly error message if the metric data type is not
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ You will need the following tools to develop the plugin:
- `gzip`
- `docker`

To build the plugin distribution from source run `make dist`.
To build the plugin distribution from source run:

```bash
make dist
```

To start a local Grafana instance with the plugin installed run:

```bash
make grafana-build grafana-run
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "enapter-telemetry",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
1 change: 0 additions & 1 deletion pkg/plugin/internal/queryhandler/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import "errors"
var (
errEmptyQueryText = errors.New("empty query text")
ErrUnsupportedTimeseriesDataType = errors.New("unsupported timeseries data type")
ErrMissingUserInfo = errors.New("missing user info")
)
7 changes: 4 additions & 3 deletions pkg/plugin/internal/queryhandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func (h *QueryHandler) timeseriesToDataFrame(timeseries *telemetryapi.Timeseries
func (h *QueryHandler) HandleQuery(
ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery,
) (data.Frames, error) {
if pCtx.User == nil {
return nil, ErrMissingUserInfo
user := ""
if pCtx.User != nil {
user = pCtx.User.Email
}

queryText, err := h.parseRawQuery(query)
Expand All @@ -85,7 +86,7 @@ func (h *QueryHandler) HandleQuery(
}

timeseries, err := h.telemetryAPIClient.Timeseries(ctx, telemetryapi.TimeseriesParams{
User: pCtx.User.Email,
User: user,
Query: queryText,
From: query.TimeRange.From,
To: query.TimeRange.To,
Expand Down
46 changes: 35 additions & 11 deletions pkg/plugin/internal/queryhandler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ func (s *QueryHandlerSuite) SetupSuite() {
s.queryHandler = queryhandler.New(s.mockTelemetryAPIClient)
}

func (s *QueryHandlerSuite) TestNoUserInfo() {
pCtx := backend.PluginContext{}
dataQuery := backend.DataQuery{}
frames, err := s.queryHandler.HandleQuery(s.ctx, pCtx, dataQuery)
s.Require().ErrorIs(err, queryhandler.ErrMissingUserInfo)
s.Require().Nil(frames)
}

var errFake = errors.New("fake error")

func (s *QueryHandlerSuite) TestTelemetryAPIError() {
Expand Down Expand Up @@ -163,6 +155,33 @@ func (s *QueryHandlerSuite) TestString() {
s.Require().Equal("bar", *dataFields[0].At(1).(*string))
}

func (s *QueryHandlerSuite) TestNoUserInfo() {
q := s.randomDataQuery()
q.user = ""
timeseries := &telemetryapi.Timeseries{
TimeField: []time.Time{
time.Unix(1, 0),
time.Unix(2, 0),
},
DataFields: []*telemetryapi.TimeseriesDataField{{
Type: telemetryapi.TimeseriesDataTypeString,
Values: []interface{}{
newString("foo"),
newString("bar"),
},
}},
}
s.expectGetAndReturnTimeseries(q, timeseries)
frames, err := s.handleQuery(q)
s.Require().Nil(err)
timestampField, dataFields := s.extractTimeseriesFields(frames)
s.Require().Len(dataFields, 1)
s.Require().Equal(int64(1), timestampField.At(0).(time.Time).Unix())
s.Require().Equal(int64(2), timestampField.At(1).(time.Time).Unix())
s.Require().Equal("foo", *dataFields[0].At(0).(*string))
s.Require().Equal("bar", *dataFields[0].At(1).(*string))
}

func (s *QueryHandlerSuite) TestStringArrayIsUnsupported() {
q := s.randomDataQuery()
timeseries := &telemetryapi.Timeseries{
Expand Down Expand Up @@ -333,10 +352,15 @@ func (s *QueryHandlerSuite) randomDataQuery() dataQuery {
}

func (s *QueryHandlerSuite) handleQuery(q dataQuery) (data.Frames, error) {
pCtx := backend.PluginContext{
User: &backend.User{
var user *backend.User
if q.user != "" {
user = &backend.User{
Email: q.user,
},
}
}

pCtx := backend.PluginContext{
User: user,
}
dataQuery := backend.DataQuery{
TimeRange: backend.TimeRange{From: q.from, To: q.to},
Expand Down
10 changes: 4 additions & 6 deletions pkg/telemetryapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ func (c *client) Timeseries(ctx context.Context, p TimeseriesParams) (*Timeserie
}

func (c *client) newTimeseriesRequest(ctx context.Context, p TimeseriesParams) (*http.Request, error) {
if p.User == "" {
return nil, ErrEmptyUser
}

q := make(url.Values)

q.Set("from", p.From.UTC().Format(time.RFC3339))
Expand All @@ -132,8 +128,10 @@ func (c *client) newTimeseriesRequest(ctx context.Context, p TimeseriesParams) (
return nil, err
}

const userField = "X-Enapter-Auth-User"
req.Header[userField] = []string{p.User}
if p.User != "" {
const userField = "X-Enapter-Auth-User"
req.Header[userField] = []string{p.User}
}

const tokenField = "X-Enapter-Auth-Token" //nolint: gosec // false positive
req.Header[tokenField] = []string{c.token}
Expand Down
10 changes: 8 additions & 2 deletions pkg/telemetryapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ func (s *ClientSuite) TestGetZeroContentLength() {
func (s *ClientSuite) TestGetEmptyUser() {
p := s.randomGetParams()
p.User = ""
_, err := s.client.Timeseries(s.ctx, p)
s.Require().ErrorIs(err, telemetryapi.ErrEmptyUser)
s.server.ExpectTimeseriesRequestAndReturnData([]string{"bool"}, `
ts,k=v
1,true
`)
timeseries, err := s.client.Timeseries(s.ctx, s.randomGetParams())
s.Require().NoError(err)
s.Require().Equal(timeseries.TimeField[0].Unix(), int64(1))
s.Require().Equal(*timeseries.DataFields[0].Values[0].(*bool), true)
}

func (s *ClientSuite) TestGetInvalidContentType() {
Expand Down
1 change: 0 additions & 1 deletion pkg/telemetryapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package telemetryapi
import "errors"

var (
ErrEmptyUser = errors.New("empty user")
errEmptyHeaderField = errors.New("empty header field")
errUnexpectedStatus = errors.New("unexpected status")
errUnexpectedAbsenceOfError = errors.New("unexpected absence of error")
Expand Down

0 comments on commit c94519e

Please sign in to comment.