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 (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-04-28 16:57:56 -04:00
|
|
|
"net/http"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net/url"
|
2019-07-11 22:53:20 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-07-18 14:22:29 -04:00
|
|
|
"strings"
|
2020-04-28 16:57:56 -04:00
|
|
|
"time"
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2019-07-11 22:53:20 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
"gopkg.in/olivere/elastic.v5"
|
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Elastic constants
|
|
|
|
const (
|
|
|
|
ElasticFormat = "format"
|
|
|
|
ElasticURL = "url"
|
|
|
|
ElasticIndex = "index"
|
|
|
|
ElasticQueueDir = "queue_dir"
|
|
|
|
ElasticQueueLimit = "queue_limit"
|
2020-08-23 12:43:48 -04:00
|
|
|
ElasticUsername = "username"
|
|
|
|
ElasticPassword = "password"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvElasticEnable = "MINIO_NOTIFY_ELASTICSEARCH_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvElasticFormat = "MINIO_NOTIFY_ELASTICSEARCH_FORMAT"
|
|
|
|
EnvElasticURL = "MINIO_NOTIFY_ELASTICSEARCH_URL"
|
|
|
|
EnvElasticIndex = "MINIO_NOTIFY_ELASTICSEARCH_INDEX"
|
|
|
|
EnvElasticQueueDir = "MINIO_NOTIFY_ELASTICSEARCH_QUEUE_DIR"
|
|
|
|
EnvElasticQueueLimit = "MINIO_NOTIFY_ELASTICSEARCH_QUEUE_LIMIT"
|
2020-08-23 12:43:48 -04:00
|
|
|
EnvElasticUsername = "MINIO_NOTIFY_ELASTICSEARCH_USERNAME"
|
|
|
|
EnvElasticPassword = "MINIO_NOTIFY_ELASTICSEARCH_PASSWORD"
|
2019-10-23 01:59:13 -04:00
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// ElasticsearchArgs - Elasticsearch target arguments.
|
|
|
|
type ElasticsearchArgs struct {
|
2020-08-23 12:43:48 -04:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
URL xnet.URL `json:"url"`
|
|
|
|
Index string `json:"index"`
|
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
|
|
|
Transport *http.Transport `json:"-"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate ElasticsearchArgs fields
|
|
|
|
func (a ElasticsearchArgs) Validate() error {
|
|
|
|
if !a.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if a.URL.IsEmpty() {
|
|
|
|
return errors.New("empty URL")
|
|
|
|
}
|
|
|
|
if a.Format != "" {
|
|
|
|
f := strings.ToLower(a.Format)
|
|
|
|
if f != event.NamespaceFormat && f != event.AccessFormat {
|
|
|
|
return errors.New("format value unrecognized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.Index == "" {
|
|
|
|
return errors.New("empty index value")
|
|
|
|
}
|
2020-08-23 12:43:48 -04:00
|
|
|
|
|
|
|
if (a.Username == "" && a.Password != "") || (a.Username != "" && a.Password == "") {
|
|
|
|
return errors.New("username and password should be set in pairs")
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// ElasticsearchTarget - Elasticsearch target.
|
|
|
|
type ElasticsearchTarget struct {
|
2020-04-14 14:19:25 -04:00
|
|
|
id event.TargetID
|
|
|
|
args ElasticsearchArgs
|
|
|
|
client *elastic.Client
|
|
|
|
store Store
|
|
|
|
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *ElasticsearchTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2020-04-21 12:38:32 -04:00
|
|
|
// HasQueueStore - Checks if the queueStore has been configured for the target
|
|
|
|
func (target *ElasticsearchTarget) HasQueueStore() bool {
|
|
|
|
return target.store != nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *ElasticsearchTarget) IsActive() (bool, error) {
|
2020-04-28 16:57:56 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-05-13 09:37:22 -04:00
|
|
|
if target.client == nil {
|
|
|
|
client, err := newClient(target.args)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
target.client = client
|
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
_, code, err := target.client.Ping(target.args.URL.String()).HttpHeadOnly(true).Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err) {
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, errNotConnected
|
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
return false, err
|
2019-12-11 17:27:03 -05:00
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
return !(code >= http.StatusBadRequest), nil
|
2019-12-11 17:27:03 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
// Save - saves the events to the store if queuestore is configured, which will be replayed when the elasticsearch connection is active.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *ElasticsearchTarget) Save(eventData event.Event) error {
|
2019-07-11 22:53:20 -04:00
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
err := target.send(eventData)
|
|
|
|
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err) {
|
|
|
|
return errNotConnected
|
2019-07-11 22:53:20 -04:00
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
return err
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
// send - sends the event to the target.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *ElasticsearchTarget) send(eventData event.Event) error {
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
var key string
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
exists := func() (bool, error) {
|
|
|
|
return target.client.Exists().Index(target.args.Index).Type("event").Id(key).Do(context.Background())
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
remove := func() error {
|
2019-07-11 22:53:20 -04:00
|
|
|
exists, err := exists()
|
|
|
|
if err == nil && exists {
|
|
|
|
_, err = target.client.Delete().Index(target.args.Index).Type("event").Id(key).Do(context.Background())
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
update := func() error {
|
|
|
|
_, err := target.client.Index().Index(target.args.Index).Type("event").BodyJson(map[string]interface{}{"Records": []event.Event{eventData}}).Id(key).Do(context.Background())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
add := func() error {
|
2018-11-07 15:09:03 -05:00
|
|
|
_, err := target.client.Index().Index(target.args.Index).Type("event").BodyJson(map[string]interface{}{"Records": []event.Event{eventData}}).Do(context.Background())
|
2018-03-15 16:03:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.args.Format == event.NamespaceFormat {
|
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
if eventData.EventName == event.ObjectRemovedDelete {
|
|
|
|
err = remove()
|
|
|
|
} else {
|
|
|
|
err = update()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.args.Format == event.AccessFormat {
|
|
|
|
return add()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
// Send - reads an event from store and sends it to Elasticsearch.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *ElasticsearchTarget) Send(eventKey string) error {
|
2019-07-11 22:53:20 -04:00
|
|
|
var err error
|
|
|
|
if target.client == nil {
|
|
|
|
target.client, err = newClient(target.args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 wouldve been already been sent successfully.
|
|
|
|
if os.IsNotExist(eErr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := target.send(eventData); err != nil {
|
2020-04-28 16:57:56 -04:00
|
|
|
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err) {
|
2019-09-19 11:23:43 -04:00
|
|
|
return errNotConnected
|
|
|
|
}
|
2019-07-11 22:53:20 -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 *ElasticsearchTarget) Close() error {
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.client != nil {
|
|
|
|
// Stops the background processes that the client is running.
|
|
|
|
target.client.Stop()
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
// createIndex - creates the index if it does not exist.
|
|
|
|
func createIndex(client *elastic.Client, args ElasticsearchArgs) error {
|
2018-03-15 16:03:41 -04:00
|
|
|
exists, err := client.IndexExists(args.Index).Do(context.Background())
|
|
|
|
if err != nil {
|
2019-07-11 22:53:20 -04:00
|
|
|
return err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
var createIndex *elastic.IndicesCreateResult
|
|
|
|
if createIndex, err = client.CreateIndex(args.Index).Do(context.Background()); err != nil {
|
2019-07-11 22:53:20 -04:00
|
|
|
return err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !createIndex.Acknowledged {
|
2019-07-11 22:53:20 -04:00
|
|
|
return fmt.Errorf("index %v not created", args.Index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newClient - creates a new elastic client with args provided.
|
|
|
|
func newClient(args ElasticsearchArgs) (*elastic.Client, error) {
|
2020-08-23 12:43:48 -04:00
|
|
|
// Client options
|
|
|
|
options := []elastic.ClientOptionFunc{elastic.SetURL(args.URL.String()),
|
|
|
|
elastic.SetMaxRetries(10),
|
|
|
|
elastic.SetSniff(false),
|
|
|
|
elastic.SetHttpClient(&http.Client{Transport: args.Transport})}
|
|
|
|
// Set basic auth
|
|
|
|
if args.Username != "" && args.Password != "" {
|
|
|
|
options = append(options, elastic.SetBasicAuth(args.Username, args.Password))
|
|
|
|
}
|
|
|
|
// Create a client
|
|
|
|
client, err := elastic.NewClient(options...)
|
2020-04-28 16:57:56 -04:00
|
|
|
if err != nil {
|
|
|
|
// https://github.com/olivere/elastic/wiki/Connection-Errors
|
|
|
|
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err) {
|
|
|
|
return nil, errNotConnected
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2020-04-28 16:57:56 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = createIndex(client, args); err != nil {
|
|
|
|
return nil, err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-07-11 22:53:20 -04:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewElasticsearchTarget - creates new Elasticsearch target.
|
2020-01-09 09:15:44 -05:00
|
|
|
func NewElasticsearchTarget(id string, args ElasticsearchArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*ElasticsearchTarget, error) {
|
2020-04-14 14:19:25 -04:00
|
|
|
target := &ElasticsearchTarget{
|
|
|
|
id: event.TargetID{ID: id, Name: "elasticsearch"},
|
|
|
|
args: args,
|
|
|
|
loggerOnce: loggerOnce,
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:53:20 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-elasticsearch-"+id)
|
2020-04-28 16:57:56 -04:00
|
|
|
target.store = NewQueueStore(queueDir, args.QueueLimit)
|
|
|
|
if err := target.store.Open(); err != nil {
|
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
|
|
|
return target, err
|
2019-07-11 22:53:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:57:56 -04:00
|
|
|
var err error
|
|
|
|
target.client, err = newClient(args)
|
|
|
|
if err != nil {
|
|
|
|
if target.store == nil || err != errNotConnected {
|
2020-04-14 14:19:25 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
|
|
|
return target, err
|
2019-07-11 22:53:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.store != nil && !test {
|
2019-07-11 22:53:20 -04:00
|
|
|
// Replays the events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
2019-07-11 22:53:20 -04:00
|
|
|
// Start replaying events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
2019-07-11 22:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|