encrypt_datasource_passwords_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package datamigrations
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
  6. "github.com/grafana/grafana/pkg/components/securejsondata"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/sqlstore"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestPasswordMigrationCommand(t *testing.T) {
  12. //setup datasources with password, basic_auth and none
  13. sqlstore := sqlstore.InitTestDB(t)
  14. session := sqlstore.NewSession()
  15. defer session.Close()
  16. datasources := []*models.DataSource{
  17. {Type: "influxdb", Name: "influxdb", Password: "foobar"},
  18. {Type: "graphite", Name: "graphite", BasicAuthPassword: "foobar"},
  19. {Type: "prometheus", Name: "prometheus", SecureJsonData: securejsondata.GetEncryptedJsonData(map[string]string{})},
  20. }
  21. // set required default values
  22. for _, ds := range datasources {
  23. ds.Created = time.Now()
  24. ds.Updated = time.Now()
  25. ds.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{})
  26. }
  27. _, err := session.Insert(&datasources)
  28. assert.Nil(t, err)
  29. //run migration
  30. err = EncryptDatasourcePaswords(&commandstest.FakeCommandLine{}, sqlstore)
  31. assert.Nil(t, err)
  32. //verify that no datasources still have password or basic_auth
  33. var dss []*models.DataSource
  34. err = session.SQL("select * from data_source").Find(&dss)
  35. assert.Nil(t, err)
  36. assert.Equal(t, len(dss), 3)
  37. for _, ds := range dss {
  38. sj := ds.SecureJsonData.Decrypt()
  39. if ds.Name == "influxdb" {
  40. assert.Equal(t, ds.Password, "")
  41. v, exist := sj["password"]
  42. assert.True(t, exist)
  43. assert.Equal(t, v, "foobar", "expected password to be moved to securejson")
  44. }
  45. if ds.Name == "graphite" {
  46. assert.Equal(t, ds.BasicAuthPassword, "")
  47. v, exist := sj["basicAuthPassword"]
  48. assert.True(t, exist)
  49. assert.Equal(t, v, "foobar", "expected basic_auth_password to be moved to securejson")
  50. }
  51. if ds.Name == "prometheus" {
  52. assert.Equal(t, len(sj), 0)
  53. }
  54. }
  55. }