Add a new config entry moving to version 13. ``` "webhook": { "1": { "enable": true, "address": "http://requestb.in/1i9al7m1" } } ```master
parent
f24753812a
commit
d6a327fbc5
@ -0,0 +1,136 @@ |
||||
/* |
||||
* 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 ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"net" |
||||
"net/http" |
||||
"net/url" |
||||
"time" |
||||
|
||||
"github.com/Sirupsen/logrus" |
||||
) |
||||
|
||||
type webhookNotify struct { |
||||
Enable bool `json:"enable"` |
||||
Endpoint string `json:"endpoint"` |
||||
} |
||||
|
||||
type httpConn struct { |
||||
*http.Client |
||||
Endpoint string |
||||
} |
||||
|
||||
// Lookup host address by dialing.
|
||||
func lookupHost(addr string) error { |
||||
dialer := &net.Dialer{ |
||||
Timeout: 300 * time.Millisecond, |
||||
KeepAlive: 300 * time.Millisecond, |
||||
} |
||||
nconn, err := dialer.Dial("tcp", addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return nconn.Close() |
||||
} |
||||
|
||||
// Initializes new webhook logrus notifier.
|
||||
func newWebhookNotify(accountID string) (*logrus.Logger, error) { |
||||
rNotify := serverConfig.GetWebhookNotifyByID(accountID) |
||||
|
||||
if rNotify.Endpoint == "" { |
||||
return nil, errInvalidArgument |
||||
} |
||||
|
||||
u, err := url.Parse(rNotify.Endpoint) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err = lookupHost(u.Host); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
conn := httpConn{ |
||||
// Configure aggressive timeouts for client posts.
|
||||
Client: &http.Client{ |
||||
Transport: &http.Transport{ |
||||
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 |
||||
} |
||||
|
||||
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, |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 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 ( |
||||
"fmt" |
||||
"io" |
||||
"net/http" |
||||
"net/http/httptest" |
||||
"path" |
||||
"testing" |
||||
|
||||
"github.com/Sirupsen/logrus" |
||||
) |
||||
|
||||
// Custom post handler to handle POST requests.
|
||||
type postHandler struct{} |
||||
|
||||
func (p postHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
||||
if r.Method != "POST" { |
||||
http.Error(w, fmt.Sprintf("Unexpected method %s", r.Method), http.StatusBadRequest) |
||||
return |
||||
} |
||||
io.Copy(w, r.Body) |
||||
} |
||||
|
||||
// Tests web hook initialization.
|
||||
func TestNewWebHookNotify(t *testing.T) { |
||||
root, err := newTestConfig("us-east-1") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer removeAll(root) |
||||
|
||||
_, err = newWebhookNotify("1") |
||||
if err == nil { |
||||
t.Fatal("Unexpected should fail") |
||||
} |
||||
|
||||
serverConfig.SetWebhookNotifyByID("10", webhookNotify{Enable: true, Endpoint: "http://www."}) |
||||
_, err = newWebhookNotify("10") |
||||
if err == nil { |
||||
t.Fatal("Unexpected should fail with lookupHost") |
||||
} |
||||
|
||||
serverConfig.SetWebhookNotifyByID("15", webhookNotify{Enable: true, Endpoint: "http://%"}) |
||||
_, err = newWebhookNotify("15") |
||||
if err == nil { |
||||
t.Fatal("Unexpected should fail with invalid URL escape") |
||||
} |
||||
|
||||
server := httptest.NewServer(postHandler{}) |
||||
defer server.Close() |
||||
|
||||
serverConfig.SetWebhookNotifyByID("20", webhookNotify{Enable: true, Endpoint: server.URL}) |
||||
webhook, err := newWebhookNotify("20") |
||||
if err != nil { |
||||
t.Fatal("Unexpected shouldn't fail", err) |
||||
} |
||||
|
||||
webhook.WithFields(logrus.Fields{ |
||||
"Key": path.Join("bucket", "object"), |
||||
"EventType": "s3:ObjectCreated:Put", |
||||
}).Info() |
||||
} |
Loading…
Reference in new issue