You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.3 KiB

  1. /*
  2. * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cmd
  17. import (
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "net/http"
  22. "net/url"
  23. "time"
  24. "github.com/Sirupsen/logrus"
  25. )
  26. type webhookNotify struct {
  27. Enable bool `json:"enable"`
  28. Endpoint string `json:"endpoint"`
  29. }
  30. func (w *webhookNotify) Validate() error {
  31. if !w.Enable {
  32. return nil
  33. }
  34. if _, err := checkURL(w.Endpoint); err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. type httpConn struct {
  40. *http.Client
  41. Endpoint string
  42. }
  43. // Lookup endpoint address by successfully dialing.
  44. func lookupEndpoint(u *url.URL) error {
  45. dialer := &net.Dialer{
  46. Timeout: 300 * time.Millisecond,
  47. KeepAlive: 300 * time.Millisecond,
  48. }
  49. nconn, err := dialer.Dial("tcp", canonicalAddr(u))
  50. if err != nil {
  51. return err
  52. }
  53. return nconn.Close()
  54. }
  55. // Initializes new webhook logrus notifier.
  56. func newWebhookNotify(accountID string) (*logrus.Logger, error) {
  57. rNotify := serverConfig.Notify.GetWebhookByID(accountID)
  58. if rNotify.Endpoint == "" {
  59. return nil, errInvalidArgument
  60. }
  61. u, err := url.Parse(rNotify.Endpoint)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if err = lookupEndpoint(u); err != nil {
  66. return nil, err
  67. }
  68. conn := httpConn{
  69. // Configure aggressive timeouts for client posts.
  70. Client: &http.Client{
  71. Transport: &http.Transport{
  72. DialContext: (&net.Dialer{
  73. Timeout: 5 * time.Second,
  74. KeepAlive: 5 * time.Second,
  75. }).DialContext,
  76. TLSHandshakeTimeout: 3 * time.Second,
  77. ResponseHeaderTimeout: 3 * time.Second,
  78. ExpectContinueTimeout: 2 * time.Second,
  79. },
  80. },
  81. Endpoint: rNotify.Endpoint,
  82. }
  83. notifyLog := logrus.New()
  84. notifyLog.Out = ioutil.Discard
  85. // Set default JSON formatter.
  86. notifyLog.Formatter = new(logrus.JSONFormatter)
  87. notifyLog.Hooks.Add(conn)
  88. // Success
  89. return notifyLog, nil
  90. }
  91. // Fire is called when an event should be sent to the message broker.
  92. func (n httpConn) Fire(entry *logrus.Entry) error {
  93. body, err := entry.Reader()
  94. if err != nil {
  95. return err
  96. }
  97. req, err := http.NewRequest("POST", n.Endpoint, body)
  98. if err != nil {
  99. return err
  100. }
  101. // Set content-type.
  102. req.Header.Set("Content-Type", "application/json")
  103. // Set proper server user-agent.
  104. req.Header.Set("User-Agent", globalServerUserAgent)
  105. // Initiate the http request.
  106. resp, err := n.Do(req)
  107. if err != nil {
  108. return err
  109. }
  110. // Make sure to close the response body so the connection can be re-used.
  111. defer resp.Body.Close()
  112. if resp.StatusCode != http.StatusOK &&
  113. resp.StatusCode != http.StatusAccepted &&
  114. resp.StatusCode != http.StatusContinue {
  115. return fmt.Errorf("Unable to send event %s", resp.Status)
  116. }
  117. return nil
  118. }
  119. // Levels are Required for logrus hook implementation
  120. func (httpConn) Levels() []logrus.Level {
  121. return []logrus.Level{
  122. logrus.InfoLevel,
  123. }
  124. }