encrypt_datasource_passwords_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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"},
  20. {Type: "elasticsearch", Name: "elasticsearch", Password: "pwd"},
  21. }
  22. // set required default values
  23. for _, ds := range datasources {
  24. ds.Created = time.Now()
  25. ds.Updated = time.Now()
  26. if ds.Name == "elasticsearch" {
  27. ds.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{
  28. "key": "value",
  29. })
  30. } else {
  31. ds.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{})
  32. }
  33. }
  34. _, err := session.Insert(&datasources)
  35. assert.Nil(t, err)
  36. // force secure_json_data to be null to verify that migration can handle that
  37. _, err = session.Exec("update data_source set secure_json_data = null where name = 'influxdb'")
  38. assert.Nil(t, err)
  39. //run migration
  40. err = EncryptDatasourcePaswords(&commandstest.FakeCommandLine{}, sqlstore)
  41. assert.Nil(t, err)
  42. //verify that no datasources still have password or basic_auth
  43. var dss []*models.DataSource
  44. err = session.SQL("select * from data_source").Find(&dss)
  45. assert.Nil(t, err)
  46. assert.Equal(t, len(dss), 4)
  47. for _, ds := range dss {
  48. sj := ds.SecureJsonData.Decrypt()
  49. if ds.Name == "influxdb" {
  50. assert.Equal(t, ds.Password, "")
  51. v, exist := sj["password"]
  52. assert.True(t, exist)
  53. assert.Equal(t, v, "foobar", "expected password to be moved to securejson")
  54. }
  55. if ds.Name == "graphite" {
  56. assert.Equal(t, ds.BasicAuthPassword, "")
  57. v, exist := sj["basicAuthPassword"]
  58. assert.True(t, exist)
  59. assert.Equal(t, v, "foobar", "expected basic_auth_password to be moved to securejson")
  60. }
  61. if ds.Name == "prometheus" {
  62. assert.Equal(t, len(sj), 0)
  63. }
  64. if ds.Name == "elasticsearch" {
  65. assert.Equal(t, ds.Password, "")
  66. key, exist := sj["key"]
  67. assert.True(t, exist)
  68. password, exist := sj["password"]
  69. assert.True(t, exist)
  70. assert.Equal(t, password, "pwd", "expected password to be moved to securejson")
  71. assert.Equal(t, key, "value", "expected existing key to be kept intact in securejson")
  72. }
  73. }
  74. }