mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 02:31:50 +08:00
Data sources: Don't fail if URL doesn't specify protocol (#24497)
This commit is contained in:
@ -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{
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user