datasource.go 7.4 KB

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