Skip to content

Commit

Permalink
Fix mapping of basic auth settings for DataSourceInstanceSettings (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
marefr authored Jul 8, 2021
1 parent 3bbb5c5 commit f94df4a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
10 changes: 10 additions & 0 deletions backend/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
64 changes: 64 additions & 0 deletions backend/common_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
6 changes: 4 additions & 2 deletions backend/http_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f94df4a

Please sign in to comment.