From f94df4a6f21dbe69dec4745793b71ecb71e07333 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Thu, 8 Jul 2021 10:05:34 +0200 Subject: [PATCH] Fix mapping of basic auth settings for DataSourceInstanceSettings (#379) --- backend/common.go | 10 +++++++ backend/common_test.go | 64 ++++++++++++++++++++++++++++++++++++++++ backend/http_settings.go | 6 ++-- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 backend/common_test.go diff --git a/backend/common.go b/backend/common.go index a19addd73..c2fd69e1a 100644 --- a/backend/common.go +++ b/backend/common.go @@ -91,6 +91,16 @@ func (s *DataSourceInstanceSettings) HTTPClientOptions() (httpclient.Options, er return httpclient.Options{}, err } + if s.BasicAuthEnabled { + httpSettings.BasicAuthEnabled = s.BasicAuthEnabled + httpSettings.BasicAuthUser = s.BasicAuthUser + httpSettings.BasicAuthPassword = s.DecryptedSecureJSONData["basicAuthPassword"] + } else if s.User != "" { + httpSettings.BasicAuthEnabled = true + httpSettings.BasicAuthUser = s.User + httpSettings.BasicAuthPassword = s.DecryptedSecureJSONData["password"] + } + return httpSettings.HTTPClientOptions(), nil } diff --git a/backend/common_test.go b/backend/common_test.go new file mode 100644 index 000000000..ce4440f7a --- /dev/null +++ b/backend/common_test.go @@ -0,0 +1,64 @@ +package backend + +import ( + "testing" + + "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/stretchr/testify/assert" +) + +func TestDataSourceInstanceSettings(t *testing.T) { + t.Run("HTTPClientOptions() should translate basic auth settings as expected", func(t *testing.T) { + tcs := []struct { + instanceSettings *DataSourceInstanceSettings + expectedClientOptions httpclient.Options + }{ + { + instanceSettings: &DataSourceInstanceSettings{}, + expectedClientOptions: httpclient.Options{}, + }, + { + instanceSettings: &DataSourceInstanceSettings{ + User: "user", + JSONData: []byte("{}"), + BasicAuthEnabled: true, + BasicAuthUser: "buser", + DecryptedSecureJSONData: map[string]string{ + "basicAuthPassword": "bpwd", + "password": "pwd", + }, + }, + expectedClientOptions: httpclient.Options{ + BasicAuth: &httpclient.BasicAuthOptions{ + User: "buser", + Password: "bpwd", + }, + }, + }, + { + instanceSettings: &DataSourceInstanceSettings{ + User: "user", + JSONData: []byte("{}"), + BasicAuthEnabled: false, + BasicAuthUser: "buser", + DecryptedSecureJSONData: map[string]string{ + "basicAuthPassword": "bpwd", + "password": "pwd", + }, + }, + expectedClientOptions: httpclient.Options{ + BasicAuth: &httpclient.BasicAuthOptions{ + User: "user", + Password: "pwd", + }, + }, + }, + } + + for _, tc := range tcs { + opts, err := tc.instanceSettings.HTTPClientOptions() + assert.NoError(t, err) + assert.Equal(t, tc.expectedClientOptions.BasicAuth, opts.BasicAuth) + } + }) +} diff --git a/backend/http_settings.go b/backend/http_settings.go index f77d5f372..34706853b 100644 --- a/backend/http_settings.go +++ b/backend/http_settings.go @@ -103,8 +103,10 @@ func parseHTTPSettings(jsonData json.RawMessage, secureJSONData map[string]strin } var dat map[string]interface{} - if err := json.Unmarshal(jsonData, &dat); err != nil { - return nil, err + if jsonData != nil { + if err := json.Unmarshal(jsonData, &dat); err != nil { + return nil, err + } } if v, exists := dat["access"]; exists {