mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Provisioning: Validate that datasource access field equals to direct or proxy (#26440)
* Validate that datasource access field equals to allowed value (direct or proxy)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ func (cr *configReader) readConfig(path string) ([]*configs, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = validateDefaultUniqueness(datasources)
|
err = cr.validateDefaultUniqueness(datasources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*c
|
|||||||
return v0.mapToDatasourceFromConfig(apiVersion.APIVersion), nil
|
return v0.mapToDatasourceFromConfig(apiVersion.APIVersion), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateDefaultUniqueness(datasources []*configs) error {
|
func (cr *configReader) validateDefaultUniqueness(datasources []*configs) error {
|
||||||
defaultCount := map[int64]int{}
|
defaultCount := map[int64]int{}
|
||||||
for i := range datasources {
|
for i := range datasources {
|
||||||
if datasources[i].Datasources == nil {
|
if datasources[i].Datasources == nil {
|
||||||
@@ -95,7 +96,12 @@ func validateDefaultUniqueness(datasources []*configs) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ds.Access == "" {
|
if ds.Access == "" {
|
||||||
ds.Access = "proxy"
|
ds.Access = models.DS_ACCESS_PROXY
|
||||||
|
}
|
||||||
|
|
||||||
|
if ds.Access != models.DS_ACCESS_DIRECT && ds.Access != models.DS_ACCESS_PROXY {
|
||||||
|
cr.log.Warn("invalid access value, will use 'proxy' instead", "value", ds.Access)
|
||||||
|
ds.Access = models.DS_ACCESS_PROXY
|
||||||
}
|
}
|
||||||
|
|
||||||
if ds.IsDefault {
|
if ds.IsDefault {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ var (
|
|||||||
brokenYaml = "testdata/broken-yaml"
|
brokenYaml = "testdata/broken-yaml"
|
||||||
multipleOrgsWithDefault = "testdata/multiple-org-default"
|
multipleOrgsWithDefault = "testdata/multiple-org-default"
|
||||||
withoutDefaults = "testdata/appliedDefaults"
|
withoutDefaults = "testdata/appliedDefaults"
|
||||||
|
invalidAccess = "testdata/invalid-access"
|
||||||
|
|
||||||
fakeRepo *fakeRepository
|
fakeRepo *fakeRepository
|
||||||
)
|
)
|
||||||
@@ -149,6 +150,13 @@ func TestDatasourceAsConfig(t *testing.T) {
|
|||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("invalid access should warn about invalid value and return 'proxy'", func() {
|
||||||
|
reader := &configReader{log: logger}
|
||||||
|
configs, err := reader.readConfig(invalidAccess)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(configs[0].Datasources[0].Access, ShouldEqual, models.DS_ACCESS_PROXY)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("skip invalid directory", func() {
|
Convey("skip invalid directory", func() {
|
||||||
cfgProvider := &configReader{log: log.New("test logger")}
|
cfgProvider := &configReader{log: log.New("test logger")}
|
||||||
cfg, err := cfgProvider.readConfig("./invalid-directory")
|
cfg, err := cfgProvider.readConfig("./invalid-directory")
|
||||||
|
|||||||
6
pkg/services/provisioning/datasources/testdata/invalid-access/invalid-access.yaml
vendored
Normal file
6
pkg/services/provisioning/datasources/testdata/invalid-access/invalid-access.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
apiVersion: 1
|
||||||
|
|
||||||
|
datasources:
|
||||||
|
- name: invalid-access
|
||||||
|
type: prometheus
|
||||||
|
access: INVALID_ACCESS
|
||||||
Reference in New Issue
Block a user