Data sources: Don't fail if URL doesn't specify protocol (#24497)

This commit is contained in:
Arve Knudsen
2020-05-12 13:04:18 +02:00
committed by GitHub
parent 0475f96347
commit 164242f5fd
7 changed files with 193 additions and 25 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/opentracing/opentracing-go"
"golang.org/x/oauth2"
"github.com/grafana/grafana/pkg/api/datasource"
"github.com/grafana/grafana/pkg/bus"
glog "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/login/social"
@ -31,20 +32,6 @@ var (
client = newHTTPClient()
)
type URLValidationError struct {
error
url string
}
func (e URLValidationError) Error() string {
return fmt.Sprintf("Validation of URL %q failed: %s", e.url, e.error.Error())
}
func (e URLValidationError) Unwrap() error {
return e.error
}
type DataSourceProxy struct {
ds *models.DataSource
ctx *models.ReqContext
@ -86,9 +73,9 @@ func (lw *logWrapper) Write(p []byte) (n int, err error) {
// NewDataSourceProxy creates a new Datasource proxy
func NewDataSourceProxy(ds *models.DataSource, plugin *plugins.DataSourcePlugin, ctx *models.ReqContext,
proxyPath string, cfg *setting.Cfg) (*DataSourceProxy, error) {
targetURL, err := url.Parse(ds.Url)
targetURL, err := datasource.ValidateURL(ds.Url)
if err != nil {
return nil, URLValidationError{error: err, url: ds.Url}
return nil, err
}
return &DataSourceProxy{

View File

@ -567,7 +567,25 @@ func TestNewDataSourceProxy_InvalidURL(t *testing.T) {
plugin := plugins.DataSourcePlugin{}
_, err := NewDataSourceProxy(&ds, &plugin, &ctx, "api/method", &cfg)
require.Error(t, err)
assert.True(t, strings.HasPrefix(err.Error(), `Validation of URL "://host/root" failed`))
assert.True(t, strings.HasPrefix(err.Error(), `Validation of data source URL "://host/root" failed`))
}
func TestNewDataSourceProxy_ProtocolLessURL(t *testing.T) {
ctx := models.ReqContext{
Context: &macaron.Context{
Req: macaron.Request{},
},
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR},
}
ds := models.DataSource{
Type: "test",
Url: "127.0.01:5432",
}
cfg := setting.Cfg{}
plugin := plugins.DataSourcePlugin{}
_, err := NewDataSourceProxy(&ds, &plugin, &ctx, "api/method", &cfg)
require.NoError(t, err)
}
type CloseNotifierResponseRecorder struct {