datasource.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/grafana/grafana/pkg/components/securejsondata"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. )
  8. const (
  9. DS_GRAPHITE = "graphite"
  10. DS_INFLUXDB = "influxdb"
  11. DS_INFLUXDB_08 = "influxdb_08"
  12. DS_ES = "elasticsearch"
  13. DS_OPENTSDB = "opentsdb"
  14. DS_CLOUDWATCH = "cloudwatch"
  15. DS_KAIROSDB = "kairosdb"
  16. DS_PROMETHEUS = "prometheus"
  17. DS_POSTGRES = "postgres"
  18. DS_MYSQL = "mysql"
  19. DS_MSSQL = "mssql"
  20. DS_ACCESS_DIRECT = "direct"
  21. DS_ACCESS_PROXY = "proxy"
  22. DS_STACKDRIVER = "stackdriver"
  23. DS_AZURE_MONITOR = "grafana-azure-monitor-datasource"
  24. )
  25. var (
  26. ErrDataSourceNotFound = errors.New("Data source not found")
  27. ErrDataSourceNameExists = errors.New("Data source with same name already exists")
  28. ErrDataSourceUpdatingOldVersion = errors.New("Trying to update old version of datasource")
  29. ErrDatasourceIsReadOnly = errors.New("Data source is readonly. Can only be updated from configuration")
  30. ErrDataSourceAccessDenied = errors.New("Data source access denied")
  31. )
  32. type DsAccess string
  33. type DataSource struct {
  34. Id int64
  35. OrgId int64
  36. Version int
  37. Name string
  38. Type string
  39. Access DsAccess
  40. Url string
  41. Password string
  42. User string
  43. Database string
  44. BasicAuth bool
  45. BasicAuthUser string
  46. BasicAuthPassword string
  47. WithCredentials bool
  48. IsDefault bool
  49. JsonData *simplejson.Json
  50. SecureJsonData securejsondata.SecureJsonData
  51. ReadOnly bool
  52. Created time.Time
  53. Updated time.Time
  54. }
  55. // DecryptedBasicAuthPassword returns data source basic auth password in plain text. It uses either deprecated
  56. // basic_auth_password field or encrypted secure_json_data[basicAuthPassword] variable.
  57. func (ds *DataSource) DecryptedBasicAuthPassword() string {
  58. return ds.decryptedValue("basicAuthPassword", ds.BasicAuthPassword)
  59. }
  60. // DecryptedPassword returns data source password in plain text. It uses either deprecated password field
  61. // or encrypted secure_json_data[password] variable.
  62. func (ds *DataSource) DecryptedPassword() string {
  63. return ds.decryptedValue("password", ds.Password)
  64. }
  65. // decryptedValue returns decrypted value from secureJsonData
  66. func (ds *DataSource) decryptedValue(field string, fallback string) string {
  67. if value, ok := ds.SecureJsonData.DecryptedValue(field); ok {
  68. return value
  69. }
  70. return fallback
  71. }
  72. var knownDatasourcePlugins = map[string]bool{
  73. DS_ES: true,
  74. DS_GRAPHITE: true,
  75. DS_INFLUXDB: true,
  76. DS_INFLUXDB_08: true,
  77. DS_KAIROSDB: true,
  78. DS_CLOUDWATCH: true,
  79. DS_PROMETHEUS: true,
  80. DS_OPENTSDB: true,
  81. DS_POSTGRES: true,
  82. DS_MYSQL: true,
  83. DS_MSSQL: true,
  84. DS_STACKDRIVER: true,
  85. DS_AZURE_MONITOR: true,
  86. "opennms": true,
  87. "abhisant-druid-datasource": true,
  88. "dalmatinerdb-datasource": true,
  89. "gnocci": true,
  90. "zabbix": true,
  91. "newrelic-app": true,
  92. "grafana-datadog-datasource": true,
  93. "grafana-simple-json": true,
  94. "grafana-splunk-datasource": true,
  95. "udoprog-heroic-datasource": true,
  96. "grafana-openfalcon-datasource": true,
  97. "opennms-datasource": true,
  98. "rackerlabs-blueflood-datasource": true,
  99. "crate-datasource": true,
  100. "ayoungprogrammer-finance-datasource": true,
  101. "monasca-datasource": true,
  102. "vertamedia-clickhouse-datasource": true,
  103. "alexanderzobnin-zabbix-datasource": true,
  104. }
  105. func IsKnownDataSourcePlugin(dsType string) bool {
  106. _, exists := knownDatasourcePlugins[dsType]
  107. return exists
  108. }
  109. // ----------------------
  110. // COMMANDS
  111. // Also acts as api DTO
  112. type AddDataSourceCommand struct {
  113. Name string `json:"name" binding:"Required"`
  114. Type string `json:"type" binding:"Required"`
  115. Access DsAccess `json:"access" binding:"Required"`
  116. Url string `json:"url"`
  117. Password string `json:"password"`
  118. Database string `json:"database"`
  119. User string `json:"user"`
  120. BasicAuth bool `json:"basicAuth"`
  121. BasicAuthUser string `json:"basicAuthUser"`
  122. BasicAuthPassword string `json:"basicAuthPassword"`
  123. WithCredentials bool `json:"withCredentials"`
  124. IsDefault bool `json:"isDefault"`
  125. JsonData *simplejson.Json `json:"jsonData"`
  126. SecureJsonData map[string]string `json:"secureJsonData"`
  127. ReadOnly bool `json:"readOnly"`
  128. OrgId int64 `json:"-"`
  129. Result *DataSource
  130. }
  131. // Also acts as api DTO
  132. type UpdateDataSourceCommand struct {
  133. Name string `json:"name" binding:"Required"`
  134. Type string `json:"type" binding:"Required"`
  135. Access DsAccess `json:"access" binding:"Required"`
  136. Url string `json:"url"`
  137. Password string `json:"password"`
  138. User string `json:"user"`
  139. Database string `json:"database"`
  140. BasicAuth bool `json:"basicAuth"`
  141. BasicAuthUser string `json:"basicAuthUser"`
  142. BasicAuthPassword string `json:"basicAuthPassword"`
  143. WithCredentials bool `json:"withCredentials"`
  144. IsDefault bool `json:"isDefault"`
  145. JsonData *simplejson.Json `json:"jsonData"`
  146. SecureJsonData map[string]string `json:"secureJsonData"`
  147. Version int `json:"version"`
  148. ReadOnly bool `json:"readOnly"`
  149. OrgId int64 `json:"-"`
  150. Id int64 `json:"-"`
  151. Result *DataSource
  152. }
  153. type DeleteDataSourceByIdCommand struct {
  154. Id int64
  155. OrgId int64
  156. DeletedDatasourcesCount int64
  157. }
  158. type DeleteDataSourceByNameCommand struct {
  159. Name string
  160. OrgId int64
  161. DeletedDatasourcesCount int64
  162. }
  163. // ---------------------
  164. // QUERIES
  165. type GetDataSourcesQuery struct {
  166. OrgId int64
  167. User *SignedInUser
  168. Result []*DataSource
  169. }
  170. type GetAllDataSourcesQuery struct {
  171. Result []*DataSource
  172. }
  173. type GetDataSourceByIdQuery struct {
  174. Id int64
  175. OrgId int64
  176. Result *DataSource
  177. }
  178. type GetDataSourceByNameQuery struct {
  179. Name string
  180. OrgId int64
  181. Result *DataSource
  182. }
  183. // ---------------------
  184. // Permissions
  185. // ---------------------
  186. type DsPermissionType int
  187. const (
  188. DsPermissionNoAccess DsPermissionType = iota
  189. DsPermissionQuery
  190. )
  191. func (p DsPermissionType) String() string {
  192. names := map[int]string{
  193. int(DsPermissionQuery): "Query",
  194. int(DsPermissionNoAccess): "No Access",
  195. }
  196. return names[int(p)]
  197. }
  198. type DatasourcesPermissionFilterQuery struct {
  199. User *SignedInUser
  200. Datasources []*DataSource
  201. Result []*DataSource
  202. }