2018-03-15 16:03:41 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-03-15 16:03:41 -04:00
|
|
|
*
|
|
|
|
* 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 target
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-11 20:46:03 -04:00
|
|
|
"context"
|
2018-03-15 16:03:41 -04:00
|
|
|
"encoding/json"
|
2018-07-18 14:22:29 -04:00
|
|
|
"errors"
|
2018-03-15 16:03:41 -04:00
|
|
|
"fmt"
|
2018-09-05 19:47:14 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-07-05 05:51:41 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Webhook constants
|
|
|
|
const (
|
|
|
|
WebhookEndpoint = "endpoint"
|
|
|
|
WebhookAuthToken = "auth_token"
|
|
|
|
WebhookQueueDir = "queue_dir"
|
|
|
|
WebhookQueueLimit = "queue_limit"
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvWebhookEnable = "MINIO_NOTIFY_WEBHOOK_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvWebhookEndpoint = "MINIO_NOTIFY_WEBHOOK_ENDPOINT"
|
|
|
|
EnvWebhookAuthToken = "MINIO_NOTIFY_WEBHOOK_AUTH_TOKEN"
|
|
|
|
EnvWebhookQueueDir = "MINIO_NOTIFY_WEBHOOK_QUEUE_DIR"
|
|
|
|
EnvWebhookQueueLimit = "MINIO_NOTIFY_WEBHOOK_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// WebhookArgs - Webhook target arguments.
|
|
|
|
type WebhookArgs struct {
|
2019-12-12 09:53:50 -05:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Endpoint xnet.URL `json:"endpoint"`
|
|
|
|
AuthToken string `json:"authToken"`
|
|
|
|
Transport *http.Transport `json:"-"`
|
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate WebhookArgs fields
|
|
|
|
func (w WebhookArgs) Validate() error {
|
|
|
|
if !w.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if w.Endpoint.IsEmpty() {
|
|
|
|
return errors.New("endpoint empty")
|
|
|
|
}
|
2019-07-05 05:51:41 -04:00
|
|
|
if w.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(w.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if w.QueueLimit > maxLimit {
|
|
|
|
return errors.New("queueLimit should not exceed 10000")
|
|
|
|
}
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// WebhookTarget - Webhook target.
|
|
|
|
type WebhookTarget struct {
|
|
|
|
id event.TargetID
|
|
|
|
args WebhookArgs
|
|
|
|
httpClient *http.Client
|
2019-07-05 05:51:41 -04:00
|
|
|
store Store
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target WebhookTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *WebhookTarget) IsActive() (bool, error) {
|
2019-10-31 02:39:09 -04:00
|
|
|
u, pErr := xnet.ParseHTTPURL(target.args.Endpoint.String())
|
2019-07-05 05:51:41 -04:00
|
|
|
if pErr != nil {
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, pErr
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
2019-08-13 07:13:11 -04:00
|
|
|
if dErr := u.DialHTTP(); dErr != nil {
|
2019-09-12 19:44:51 -04:00
|
|
|
if xnet.IsNetworkOrHostDown(dErr) {
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, errNotConnected
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, dErr
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save - saves the events to the store if queuestore is configured, which will be replayed when the wenhook connection is active.
|
|
|
|
func (target *WebhookTarget) Save(eventData event.Event) error {
|
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
2019-04-10 08:46:01 -04:00
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
2019-07-05 05:51:41 -04:00
|
|
|
// send - sends an event to the webhook.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *WebhookTarget) send(eventData event.Event) error {
|
2018-03-15 16:03:41 -04:00
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
2018-11-14 13:23:44 -05:00
|
|
|
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
2018-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", target.args.Endpoint.String(), bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if target.args.AuthToken != "" {
|
|
|
|
req.Header.Set("Authorization", "Bearer "+target.args.AuthToken)
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := target.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:53:50 -05:00
|
|
|
defer resp.Body.Close()
|
2018-09-05 19:47:14 -04:00
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2018-04-10 20:45:54 -04:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
2019-12-12 09:53:50 -05:00
|
|
|
// close any idle connections upon any error.
|
|
|
|
target.httpClient.CloseIdleConnections()
|
2018-03-15 16:03:41 -04:00
|
|
|
return fmt.Errorf("sending event failed with %v", resp.Status)
|
|
|
|
}
|
2018-04-10 20:45:54 -04:00
|
|
|
|
|
|
|
return nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2019-07-05 05:51:41 -04:00
|
|
|
// Send - reads an event from store and sends it to webhook.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *WebhookTarget) Send(eventKey string) error {
|
2019-12-11 17:27:03 -05:00
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
|
|
|
eventData, eErr := target.store.Get(eventKey)
|
|
|
|
if eErr != nil {
|
|
|
|
// The last event key in a successful batch will be sent in the channel atmost once by the replayEvents()
|
|
|
|
// Such events will not exist and would've been already been sent successfully.
|
|
|
|
if os.IsNotExist(eErr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := target.send(eventData); err != nil {
|
2019-09-19 11:23:43 -04:00
|
|
|
if xnet.IsNetworkOrHostDown(err) {
|
|
|
|
return errNotConnected
|
|
|
|
}
|
2019-07-05 05:51:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the event from store.
|
|
|
|
return target.store.Del(eventKey)
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// Close - does nothing and available for interface compatibility.
|
|
|
|
func (target *WebhookTarget) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebhookTarget - creates new Webhook target.
|
2019-12-12 09:53:50 -05:00
|
|
|
func NewWebhookTarget(id string, args WebhookArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), transport *http.Transport) (*WebhookTarget, error) {
|
2019-07-05 05:51:41 -04:00
|
|
|
|
|
|
|
var store Store
|
|
|
|
|
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-webhook-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
2019-10-23 01:59:13 -04:00
|
|
|
if err := store.Open(); err != nil {
|
|
|
|
return nil, err
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target := &WebhookTarget{
|
2018-11-14 13:23:44 -05:00
|
|
|
id: event.TargetID{ID: id, Name: "webhook"},
|
2018-03-15 16:03:41 -04:00
|
|
|
args: args,
|
|
|
|
httpClient: &http.Client{
|
2019-12-12 09:53:50 -05:00
|
|
|
Transport: transport,
|
2018-03-15 16:03:41 -04:00
|
|
|
},
|
2019-07-05 05:51:41 -04:00
|
|
|
store: store,
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-07-05 05:51:41 -04:00
|
|
|
|
|
|
|
if target.store != nil {
|
|
|
|
// Replays the events from the store.
|
2019-10-11 20:46:03 -04:00
|
|
|
eventKeyCh := replayEvents(target.store, doneCh, loggerOnce, target.ID())
|
2019-07-05 05:51:41 -04:00
|
|
|
// Start replaying events from the store.
|
2019-10-11 20:46:03 -04:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, loggerOnce)
|
2019-07-05 05:51:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|