api_client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package services
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "path"
  11. "runtime"
  12. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  13. "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  14. "github.com/grafana/grafana/pkg/util/errutil"
  15. "golang.org/x/xerrors"
  16. )
  17. type GrafanaComClient struct {
  18. retryCount int
  19. }
  20. func (client *GrafanaComClient) GetPlugin(pluginId, repoUrl string) (models.Plugin, error) {
  21. logger.Debugf("getting plugin metadata from: %v pluginId: %v \n", repoUrl, pluginId)
  22. body, err := sendRequest(HttpClient, repoUrl, "repo", pluginId)
  23. if err != nil {
  24. if err == ErrNotFoundError {
  25. return models.Plugin{}, errutil.Wrap("Failed to find requested plugin, check if the plugin_id is correct", err)
  26. }
  27. return models.Plugin{}, errutil.Wrap("Failed to send request", err)
  28. }
  29. var data models.Plugin
  30. err = json.Unmarshal(body, &data)
  31. if err != nil {
  32. logger.Info("Failed to unmarshal plugin repo response error:", err)
  33. return models.Plugin{}, err
  34. }
  35. return data, nil
  36. }
  37. func (client *GrafanaComClient) DownloadFile(pluginName, filePath, url string, checksum string) (content []byte, err error) {
  38. // Try handling url like local file path first
  39. if _, err := os.Stat(url); err == nil {
  40. bytes, err := ioutil.ReadFile(url)
  41. if err != nil {
  42. return nil, errutil.Wrap("Failed to read file", err)
  43. }
  44. return bytes, nil
  45. }
  46. client.retryCount = 0
  47. defer func() {
  48. if r := recover(); r != nil {
  49. client.retryCount++
  50. if client.retryCount < 3 {
  51. logger.Info("Failed downloading. Will retry once.")
  52. content, err = client.DownloadFile(pluginName, filePath, url, checksum)
  53. } else {
  54. client.retryCount = 0
  55. failure := fmt.Sprintf("%v", r)
  56. if failure == "runtime error: makeslice: len out of range" {
  57. err = xerrors.New("Corrupt http response from source. Please try again")
  58. } else {
  59. panic(r)
  60. }
  61. }
  62. }
  63. }()
  64. // TODO: this would be better if it was streamed file by file instead of buffered.
  65. // Using no timeout here as some plugins can be bigger and smaller timeout would prevent to download a plugin on
  66. // slow network. As this is CLI operation hanging is not a big of an issue as user can just abort.
  67. body, err := sendRequest(HttpClientNoTimeout, url)
  68. if err != nil {
  69. return nil, errutil.Wrap("Failed to send request", err)
  70. }
  71. if len(checksum) > 0 && checksum != fmt.Sprintf("%x", md5.Sum(body)) {
  72. return nil, xerrors.New("Expected MD5 checksum does not match the downloaded archive. Please contact security@grafana.com.")
  73. }
  74. return body, nil
  75. }
  76. func (client *GrafanaComClient) ListAllPlugins(repoUrl string) (models.PluginRepo, error) {
  77. body, err := sendRequest(HttpClient, repoUrl, "repo")
  78. if err != nil {
  79. logger.Info("Failed to send request", "error", err)
  80. return models.PluginRepo{}, errutil.Wrap("Failed to send request", err)
  81. }
  82. var data models.PluginRepo
  83. err = json.Unmarshal(body, &data)
  84. if err != nil {
  85. logger.Info("Failed to unmarshal plugin repo response error:", err)
  86. return models.PluginRepo{}, err
  87. }
  88. return data, nil
  89. }
  90. func sendRequest(client http.Client, repoUrl string, subPaths ...string) ([]byte, error) {
  91. u, _ := url.Parse(repoUrl)
  92. for _, v := range subPaths {
  93. u.Path = path.Join(u.Path, v)
  94. }
  95. req, err := http.NewRequest(http.MethodGet, u.String(), nil)
  96. req.Header.Set("grafana-version", grafanaVersion)
  97. req.Header.Set("grafana-os", runtime.GOOS)
  98. req.Header.Set("grafana-arch", runtime.GOARCH)
  99. req.Header.Set("User-Agent", "grafana "+grafanaVersion)
  100. if err != nil {
  101. return []byte{}, err
  102. }
  103. res, err := client.Do(req)
  104. if err != nil {
  105. return []byte{}, err
  106. }
  107. return handleResponse(res)
  108. }
  109. func handleResponse(res *http.Response) ([]byte, error) {
  110. if res.StatusCode == 404 {
  111. return []byte{}, ErrNotFoundError
  112. }
  113. if res.StatusCode/100 != 2 && res.StatusCode/100 != 4 {
  114. return []byte{}, fmt.Errorf("Api returned invalid status: %s", res.Status)
  115. }
  116. body, err := ioutil.ReadAll(res.Body)
  117. defer res.Body.Close()
  118. if res.StatusCode/100 == 4 {
  119. if len(body) == 0 {
  120. return []byte{}, &BadRequestError{Status: res.Status}
  121. }
  122. var message string
  123. var jsonBody map[string]string
  124. err = json.Unmarshal(body, &jsonBody)
  125. if err != nil || len(jsonBody["message"]) == 0 {
  126. message = string(body)
  127. } else {
  128. message = jsonBody["message"]
  129. }
  130. return []byte{}, &BadRequestError{Status: res.Status, Message: message}
  131. }
  132. return body, err
  133. }