[v10.0.x] DataSourceProxy: Fix url validation error handling (#73321)

* DataSourceProxy: Fix url validation error handling (#73087)

* DataSourceProxy: Fix url validation error handling

* DataSourceProxy: Add unit test for proxyDatasourceRequest

* DataSourceProxy: Fix lint error

(cherry picked from commit 8ec4c1bdc8)

* fix test

---------

Co-authored-by: Ricardo Christmann <80476005+ricci2511@users.noreply.github.com>
Co-authored-by: Will Browne <will.browne@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2023-08-16 18:26:51 +03:00
committed by GitHub
parent 305b3aff15
commit 7d4e370cae
2 changed files with 83 additions and 1 deletions
@@ -1,9 +1,21 @@
package datasourceproxy
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/web"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDataProxy(t *testing.T) {
@@ -52,3 +64,72 @@ func TestDataProxy(t *testing.T) {
}
})
}
// Tests request to datasource proxy service
func TestDatasourceProxy_proxyDatasourceRequest(t *testing.T) {
tcs := []struct {
name string
dsURL string
expectedErrorMsg string
}{
{
name: "Empty datasource URL will return a 400 HTTP status code",
dsURL: "",
expectedErrorMsg: "validation of data source URL \"\" failed: empty URL string",
},
{
name: "Invalid datasource URL will return a 400 HTTP status code",
dsURL: "://host/path",
expectedErrorMsg: "validation of data source URL \"://host/path\" failed: parse \"://host/path\": missing protocol scheme",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
pluginID := datasources.DS_PROMETHEUS
pluginStore := &plugins.FakePluginStore{PluginList: []plugins.PluginDTO{
{JSONData: plugins.JSONData{ID: pluginID}},
}}
p := DataSourceProxyService{
PluginRequestValidator: &fakePluginRequestValidator{},
pluginStore: pluginStore,
}
responseRecorder := httptest.NewRecorder()
c := &contextmodel.ReqContext{
Context: &web.Context{
Req: &http.Request{URL: &url.URL{}},
Resp: web.NewResponseWriter("GET", responseRecorder),
},
Logger: log.NewNopLogger(),
}
p.proxyDatasourceRequest(c, &datasources.DataSource{
Type: pluginID,
URL: tc.dsURL,
})
resp := responseRecorder.Result()
body := resp.Body
b, err := io.ReadAll(body)
require.NoError(t, err)
require.NoError(t, body.Close())
jsonBody := make(map[string]string)
err = json.Unmarshal(b, &jsonBody)
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
require.Equal(t, fmt.Sprintf("Invalid data source URL: %q", tc.dsURL), jsonBody["message"])
require.Equal(t, tc.expectedErrorMsg, jsonBody["error"])
})
}
}
type fakePluginRequestValidator struct{}
func (rv *fakePluginRequestValidator) Validate(_ string, _ *http.Request) error {
return nil
}