2017-01-09 17:22:10 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-04-08 04:13:55 -04:00
|
|
|
"bytes"
|
2017-03-29 16:42:55 -04:00
|
|
|
"crypto/tls"
|
2017-01-09 17:22:10 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-04-08 04:13:55 -04:00
|
|
|
"strings"
|
2017-01-09 17:22:10 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webhookNotify struct {
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:30:34 -04:00
|
|
|
func (w *webhookNotify) Validate() error {
|
|
|
|
if !w.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-16 14:44:01 -04:00
|
|
|
if _, err := checkURL(w.Endpoint); err != nil {
|
2017-03-15 19:30:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-09 17:22:10 -05:00
|
|
|
type httpConn struct {
|
|
|
|
*http.Client
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
2017-04-08 04:13:55 -04:00
|
|
|
// List of success status.
|
|
|
|
var successStatus = []int{
|
|
|
|
http.StatusOK,
|
|
|
|
http.StatusAccepted,
|
|
|
|
http.StatusContinue,
|
|
|
|
http.StatusNoContent,
|
|
|
|
http.StatusPartialContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNetErrorIgnored - is network error ignored.
|
|
|
|
func isNetErrorIgnored(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "Client.Timeout exceeded while awaiting headers") {
|
|
|
|
return true
|
2017-01-09 17:22:10 -05:00
|
|
|
}
|
2017-04-08 04:13:55 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case net.Error:
|
2017-05-04 16:43:54 -04:00
|
|
|
switch e := err.(type) {
|
2017-04-08 04:13:55 -04:00
|
|
|
case *net.DNSError, *net.OpError, net.UnknownNetworkError:
|
|
|
|
return true
|
|
|
|
case *url.Error:
|
2017-05-04 16:43:54 -04:00
|
|
|
// Fixes https://github.com/minio/minio/issues/4050
|
|
|
|
switch e.Err.(type) {
|
|
|
|
case *net.DNSError, *net.OpError, net.UnknownNetworkError:
|
|
|
|
return true
|
|
|
|
}
|
2017-04-08 04:13:55 -04:00
|
|
|
// For a URL error, where it replies back "connection closed"
|
|
|
|
// retry again.
|
|
|
|
if strings.Contains(err.Error(), "Connection closed by foreign host") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if strings.Contains(err.Error(), "net/http: TLS handshake timeout") {
|
|
|
|
// If error is - tlsHandshakeTimeoutError, retry.
|
|
|
|
return true
|
|
|
|
} else if strings.Contains(err.Error(), "i/o timeout") {
|
|
|
|
// If error is - tcp timeoutError, retry.
|
|
|
|
return true
|
|
|
|
} else if strings.Contains(err.Error(), "connection timed out") {
|
|
|
|
// If err is a net.Dial timeout, retry.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup endpoint address by successfully POSTting
|
|
|
|
// a JSON which would send out minio release.
|
|
|
|
func lookupEndpoint(urlStr string) error {
|
|
|
|
minioRelease := bytes.NewReader([]byte(ReleaseTag))
|
|
|
|
req, err := http.NewRequest("POST", urlStr, minioRelease)
|
2017-01-09 17:22:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-08 04:13:55 -04:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 1 * time.Second,
|
|
|
|
Transport: &http.Transport{
|
2017-09-22 17:03:31 -04:00
|
|
|
// Need to close connection after usage.
|
2017-04-08 04:13:55 -04:00
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set content-type.
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// Set proper server user-agent.
|
|
|
|
req.Header.Set("User-Agent", globalServerUserAgent)
|
|
|
|
|
2017-09-22 17:03:31 -04:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
if isNetErrorIgnored(err) {
|
|
|
|
errorIf(err, "Unable to lookup webhook endpoint %s", urlStr)
|
|
|
|
return nil
|
2017-04-08 04:13:55 -04:00
|
|
|
}
|
2017-09-22 17:03:31 -04:00
|
|
|
return err
|
2017-04-08 04:13:55 -04:00
|
|
|
}
|
2017-09-22 17:03:31 -04:00
|
|
|
defer resp.Body.Close()
|
|
|
|
// HTTP status OK/NoContent.
|
|
|
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
|
|
|
return fmt.Errorf("Unable to lookup webhook endpoint %s response(%s)", urlStr, resp.Status)
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-09 17:22:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes new webhook logrus notifier.
|
|
|
|
func newWebhookNotify(accountID string) (*logrus.Logger, error) {
|
2017-02-09 18:20:54 -05:00
|
|
|
rNotify := serverConfig.Notify.GetWebhookByID(accountID)
|
2017-01-09 17:22:10 -05:00
|
|
|
if rNotify.Endpoint == "" {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2017-04-08 04:13:55 -04:00
|
|
|
if err := lookupEndpoint(rNotify.Endpoint); err != nil {
|
2017-01-09 17:22:10 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := httpConn{
|
|
|
|
// Configure aggressive timeouts for client posts.
|
|
|
|
Client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2017-03-29 16:42:55 -04:00
|
|
|
TLSClientConfig: &tls.Config{RootCAs: globalRootCAs},
|
2017-01-09 17:22:10 -05:00
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
KeepAlive: 5 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
TLSHandshakeTimeout: 3 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 3 * time.Second,
|
|
|
|
ExpectContinueTimeout: 2 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Endpoint: rNotify.Endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyLog := logrus.New()
|
|
|
|
notifyLog.Out = ioutil.Discard
|
|
|
|
|
|
|
|
// Set default JSON formatter.
|
|
|
|
notifyLog.Formatter = new(logrus.JSONFormatter)
|
|
|
|
|
|
|
|
notifyLog.Hooks.Add(conn)
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return notifyLog, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire is called when an event should be sent to the message broker.
|
|
|
|
func (n httpConn) Fire(entry *logrus.Entry) error {
|
|
|
|
body, err := entry.Reader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", n.Endpoint, body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set content-type.
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// Set proper server user-agent.
|
|
|
|
req.Header.Set("User-Agent", globalServerUserAgent)
|
|
|
|
|
|
|
|
// Initiate the http request.
|
|
|
|
resp, err := n.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-27 12:50:08 -05:00
|
|
|
// Make sure to close the response body so the connection can be re-used.
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-01-09 17:22:10 -05:00
|
|
|
if resp.StatusCode != http.StatusOK &&
|
|
|
|
resp.StatusCode != http.StatusAccepted &&
|
|
|
|
resp.StatusCode != http.StatusContinue {
|
|
|
|
return fmt.Errorf("Unable to send event %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Levels are Required for logrus hook implementation
|
|
|
|
func (httpConn) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
|
|
|
logrus.InfoLevel,
|
|
|
|
}
|
|
|
|
}
|