Skip to content

Fix error source in errors with invalid JWT information #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 18, 2024

Conversation

ivanahuckova
Copy link
Contributor

@ivanahuckova ivanahuckova commented Sep 13, 2024

This PR takes care of false-positive plugin errors identified in https://ops.grafana-ops.net/goto/BHCltU6SR?orgId=1. The main issue is that if users input invalid Google JWT information - for example incorrect email:
image

The returned error was plugin error as we weren't handling that case. This PR fixes it by checking for *oauth2.RetrieveError and based on status code from that error sets the error source. It also removes a logic to check for timeouts, as bumped grafana-plugin-sdk-go takes care of it. We have tests in place for this and they are passing even with removed logic, as sdk correctly handles it.

Now this is fixed to downstream:
image

Also I had to fix some lint issues (see https://drone.grafana.net/grafana/google-sheets-datasource/447/1/8) - I have added a comments to them bellow.

netErr, neErrOk := err.(net.Error)
if neErrOk && netErr.Timeout() || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer have to take care of timeout errors as updated grafana-plugin-sdk-go handles that. You can also see that we are keeping the tests with checking for timeouts and it is correctly passing:
image

@ivanahuckova ivanahuckova marked this pull request as ready for review September 13, 2024 14:42
@ivanahuckova ivanahuckova requested a review from a team as a code owner September 13, 2024 14:42
@ivanahuckova ivanahuckova changed the title WIP Fix error source in errors with invalid JWT information Sep 13, 2024
- name: unexported-return
- name: unexported-return
disabled: true
- name: redundant-import-alias
Copy link
Contributor Author

@ivanahuckova ivanahuckova Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed because of bumped grafana-plugin-sdk-go . It was complaining about Magefile.go and using named import in the code bellow, which is what we do in every data source and comes from create plugin.

//go:build mage
// +build mage

package main

import (
	// mage:import
	build "github.com/grafana/grafana-plugin-sdk-go/build"
)

// Default configures the default target.
var Default = build.BuildAll

@@ -79,7 +81,7 @@ linters:
- dogsled
- dupl
- errcheck
- exportloopref
- copyloopvar
Copy link
Contributor Author

@ivanahuckova ivanahuckova Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was recommended by ci as exportloopref is deprecated.

Comment on lines +97 to +103
if gridData, ok := item.(*sheets.GridData); ok {
return gridData, map[string]any{
"hit": true,
"expires": expires.Unix(),
}, nil
}
return nil, nil, errors.New("invalid cache item not type of *sheets.GridData")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no change in this besides checking if item is actually GridData. But linter was complaining that we weren't checking if item is GridData, so I added it.

Comment on lines +32 to +35
if spreadsheet, ok := args.Get(0).(*sheets.Spreadsheet); ok {
return spreadsheet, args.Error(1)
}
return nil, args.Error(1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another fix for linter complaining that we aren't checking if spreadsheet is actually *sheets.Spreadsheet.

@@ -241,7 +284,8 @@ func TestGooglesheets(t *testing.T) {
})

t.Run("meta warnings field is populated correctly", func(t *testing.T) {
warnings := meta["warnings"].([]string)
warnings, ok := meta["warnings"].([]string)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last linter warning fixed.

@ivanahuckova ivanahuckova merged commit 299efed into main Sep 18, 2024
3 checks passed
@ivanahuckova ivanahuckova deleted the ivana/oauth-error branch September 18, 2024 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants