Check for empty URLs when creating/updating a data source (#42837)
* checks for empty URLs added * check for TimeSeriesTypeNot to fix InfluxDB alerts * log a warning when a data frame is ignored * fix: add brittle Prometheus URL input selector needs a proper aria-label or test-data-id selector * test: add URL input aria-label needs to use the grafana/e2e-selectors package * test: run ci * add URL validation for specific data sources, e2e tests * Update pkg/api/datasource/validation.go Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * delete duplicated logs * delete unnecessary leading newline Co-authored-by: gillesdemey <gilles.de.mey@gmail.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
co-authored by
Marcus Efraimsson
gillesdemey
parent
2053049c40
commit
7ed82ac049
@@ -1,17 +1,37 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/mssql"
|
||||
)
|
||||
|
||||
var logger = log.New("datasource")
|
||||
|
||||
// requiredURL contains the set of data sources that require a URL.
|
||||
var requiredURL = map[string]bool{
|
||||
models.DS_GRAPHITE: true,
|
||||
models.DS_INFLUXDB: true,
|
||||
models.DS_INFLUXDB_08: true,
|
||||
models.DS_ES: true,
|
||||
models.DS_PROMETHEUS: true,
|
||||
models.DS_ALERTMANAGER: true,
|
||||
models.DS_JAEGER: true,
|
||||
models.DS_LOKI: true,
|
||||
models.DS_OPENTSDB: true,
|
||||
models.DS_TEMPO: true,
|
||||
models.DS_ZIPKIN: true,
|
||||
models.DS_MYSQL: true,
|
||||
models.DS_POSTGRES: true,
|
||||
models.DS_MSSQL: true,
|
||||
}
|
||||
|
||||
// URLValidationError represents an error from validating a data source URL.
|
||||
type URLValidationError struct {
|
||||
Err error
|
||||
@@ -40,6 +60,11 @@ var reURL = regexp.MustCompile("^[^:]*://")
|
||||
// The data source's type and URL must be provided. If successful, the valid URL object is returned, otherwise an
|
||||
// error is returned.
|
||||
func ValidateURL(typeName, urlStr string) (*url.URL, error) {
|
||||
// Check for empty URLs
|
||||
if _, exists := requiredURL[typeName]; exists && strings.TrimSpace(urlStr) == "" {
|
||||
return nil, URLValidationError{Err: errors.New("empty URL string"), URL: ""}
|
||||
}
|
||||
|
||||
var u *url.URL
|
||||
var err error
|
||||
switch strings.ToLower(typeName) {
|
||||
|
||||
+9
-11
@@ -256,13 +256,9 @@ func (hs *HTTPServer) DeleteDataSourceByName(c *models.ReqContext) response.Resp
|
||||
})
|
||||
}
|
||||
|
||||
func validateURL(tp string, u string) response.Response {
|
||||
if u != "" {
|
||||
if _, err := datasource.ValidateURL(tp, u); err != nil {
|
||||
datasourcesLogger.Error("Received invalid data source URL as part of data source command",
|
||||
"url", u)
|
||||
return response.Error(400, fmt.Sprintf("Validation error, invalid URL: %q", u), err)
|
||||
}
|
||||
func validateURL(cmdType string, url string) response.Response {
|
||||
if _, err := datasource.ValidateURL(cmdType, url); err != nil {
|
||||
return response.Error(400, fmt.Sprintf("Validation error, invalid URL: %q", url), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -274,10 +270,13 @@ func AddDataSource(c *models.ReqContext) response.Response {
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
datasourcesLogger.Debug("Received command to add data source", "url", cmd.Url)
|
||||
cmd.OrgId = c.OrgId
|
||||
if resp := validateURL(cmd.Type, cmd.Url); resp != nil {
|
||||
return resp
|
||||
if cmd.Url != "" {
|
||||
if resp := validateURL(cmd.Type, cmd.Url); resp != nil {
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
@@ -306,8 +305,7 @@ func (hs *HTTPServer) UpdateDataSource(c *models.ReqContext) response.Response {
|
||||
datasourcesLogger.Debug("Received command to update data source", "url", cmd.Url)
|
||||
cmd.OrgId = c.OrgId
|
||||
var err error
|
||||
cmd.Id, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
if cmd.Id, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
if resp := validateURL(cmd.Type, cmd.Url); resp != nil {
|
||||
|
||||
@@ -564,18 +564,18 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
|
||||
|
||||
tests := []*testCase{
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB_08, authTypePassword, authCheckQuery, false),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB_08, authTypePassword, authCheckQuery, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, authTypePassword, authCheckHeader, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, authTypePassword, authCheckHeader, false),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, authTypeBasic, authCheckHeader, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, authTypeBasic, authCheckHeader, false),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB_08, "http://localhost:9090", authTypePassword, authCheckQuery, false),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB_08, "http://localhost:9090", authTypePassword, authCheckQuery, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, "http://localhost:9090", authTypePassword, authCheckHeader, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, "http://localhost:9090", authTypePassword, authCheckHeader, false),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, "http://localhost:9090", authTypeBasic, authCheckHeader, true),
|
||||
createAuthTest(t, secretsService, models.DS_INFLUXDB, "http://localhost:9090", authTypeBasic, authCheckHeader, false),
|
||||
|
||||
// These two should be enough for any other datasource at the moment. Proxy has special handling
|
||||
// only for Influx, others have the same path and only BasicAuth. Non BasicAuth datasources
|
||||
// do not go through proxy but through TSDB API which is not tested here.
|
||||
createAuthTest(t, secretsService, models.DS_ES, authTypeBasic, authCheckHeader, false),
|
||||
createAuthTest(t, secretsService, models.DS_ES, authTypeBasic, authCheckHeader, true),
|
||||
createAuthTest(t, secretsService, models.DS_ES, "http://localhost:9200", authTypeBasic, authCheckHeader, false),
|
||||
createAuthTest(t, secretsService, models.DS_ES, "http://localhost:9200", authTypeBasic, authCheckHeader, true),
|
||||
}
|
||||
for _, test := range tests {
|
||||
runDatasourceAuthTest(t, secretsService, test)
|
||||
@@ -900,7 +900,7 @@ const (
|
||||
authCheckHeader = "header"
|
||||
)
|
||||
|
||||
func createAuthTest(t *testing.T, secretsService secrets.Service, dsType string, authType string, authCheck string, useSecureJsonData bool) *testCase {
|
||||
func createAuthTest(t *testing.T, secretsService secrets.Service, dsType string, url string, authType string, authCheck string, useSecureJsonData bool) *testCase {
|
||||
ctx := context.Background()
|
||||
|
||||
// Basic user:password
|
||||
@@ -911,6 +911,7 @@ func createAuthTest(t *testing.T, secretsService secrets.Service, dsType string,
|
||||
Id: 1,
|
||||
Type: dsType,
|
||||
JsonData: simplejson.New(),
|
||||
Url: url,
|
||||
},
|
||||
}
|
||||
var message string
|
||||
|
||||
Reference in New Issue
Block a user