2016-07-24 01:51:12 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-07-24 01:51:12 -04:00
|
|
|
|
|
|
|
import (
|
2016-09-20 19:36:18 -04:00
|
|
|
"encoding/hex"
|
2016-07-24 01:51:12 -04:00
|
|
|
"errors"
|
2016-08-05 01:01:58 -04:00
|
|
|
"io/ioutil"
|
2016-07-24 01:51:12 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-20 19:36:18 -04:00
|
|
|
"github.com/minio/sha256-simd"
|
2016-07-24 01:51:12 -04:00
|
|
|
"gopkg.in/olivere/elastic.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// elasticQueue is a elasticsearch event notification queue.
|
2016-08-05 01:01:58 -04:00
|
|
|
type elasticSearchNotify struct {
|
2016-07-24 01:51:12 -04:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Index string `json:"index"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type elasticClient struct {
|
|
|
|
*elastic.Client
|
2016-08-05 01:01:58 -04:00
|
|
|
params elasticSearchNotify
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connects to elastic search instance at URL.
|
2016-08-05 01:01:58 -04:00
|
|
|
func dialElastic(esNotify elasticSearchNotify) (*elastic.Client, error) {
|
|
|
|
if !esNotify.Enable {
|
|
|
|
return nil, errNotifyNotEnabled
|
2016-07-25 20:53:55 -04:00
|
|
|
}
|
2016-09-20 19:36:18 -04:00
|
|
|
client, err := elastic.NewClient(
|
|
|
|
elastic.SetURL(esNotify.URL),
|
|
|
|
elastic.SetSniff(false),
|
|
|
|
elastic.SetMaxRetries(10),
|
|
|
|
)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
func newElasticNotify(accountID string) (*logrus.Logger, error) {
|
2017-02-09 18:20:54 -05:00
|
|
|
esNotify := serverConfig.Notify.GetElasticSearchByID(accountID)
|
2016-07-25 20:53:55 -04:00
|
|
|
|
|
|
|
// Dial to elastic search.
|
2016-08-05 01:01:58 -04:00
|
|
|
client, err := dialElastic(esNotify)
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the IndexExists service to check if a specified index exists.
|
2016-08-05 01:01:58 -04:00
|
|
|
exists, err := client.IndexExists(esNotify.Index).Do()
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
// Index does not exist, attempt to create it.
|
|
|
|
if !exists {
|
|
|
|
var createIndex *elastic.IndicesCreateResult
|
2016-08-05 01:01:58 -04:00
|
|
|
createIndex, err = client.CreateIndex(esNotify.Index).Do()
|
2016-07-24 01:51:12 -04:00
|
|
|
if err != nil {
|
2016-08-05 01:01:58 -04:00
|
|
|
return nil, err
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
if !createIndex.Acknowledged {
|
2016-11-15 21:14:23 -05:00
|
|
|
return nil, errors.New("Index not created")
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elasticCl := elasticClient{
|
|
|
|
Client: client,
|
2016-08-05 01:01:58 -04:00
|
|
|
params: esNotify,
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
elasticSearchLog := logrus.New()
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Disable writing to console.
|
|
|
|
elasticSearchLog.Out = ioutil.Discard
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Add a elasticSearch hook.
|
|
|
|
elasticSearchLog.Hooks.Add(elasticCl)
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Set default JSON formatter.
|
|
|
|
elasticSearchLog.Formatter = new(logrus.JSONFormatter)
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2016-08-05 01:01:58 -04:00
|
|
|
// Success, elastic search successfully initialized.
|
|
|
|
return elasticSearchLog, nil
|
2016-07-24 01:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fire is required to implement logrus hook
|
|
|
|
func (q elasticClient) Fire(entry *logrus.Entry) error {
|
2016-09-20 05:11:17 -04:00
|
|
|
// Reflect on eventType and Key on their native type.
|
|
|
|
entryStr, ok := entry.Data["EventType"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
keyStr, ok := entry.Data["Key"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-24 01:51:12 -04:00
|
|
|
|
2016-09-20 19:36:18 -04:00
|
|
|
// Calculate a unique key id. Choosing sha256 here.
|
|
|
|
shaKey := sha256.Sum256([]byte(keyStr))
|
|
|
|
keyStr = hex.EncodeToString(shaKey[:])
|
|
|
|
|
2016-09-20 05:11:17 -04:00
|
|
|
// If event matches as delete, we purge the previous index.
|
|
|
|
if eventMatch(entryStr, []string{"s3:ObjectRemoved:*"}) {
|
2016-09-20 19:36:18 -04:00
|
|
|
_, err := q.Client.Delete().Index(q.params.Index).
|
|
|
|
Type("event").Id(keyStr).Do()
|
2016-09-20 05:11:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} // else we update elastic index or create a new one.
|
2016-09-20 19:36:18 -04:00
|
|
|
_, err := q.Client.Index().Index(q.params.Index).
|
2016-09-20 05:11:17 -04:00
|
|
|
Type("event").
|
|
|
|
BodyJson(map[string]interface{}{
|
|
|
|
"Records": entry.Data["Records"],
|
2016-09-20 19:36:18 -04:00
|
|
|
}).Id(keyStr).Do()
|
2016-07-24 01:51:12 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required for logrus hook implementation
|
|
|
|
func (q elasticClient) Levels() []logrus.Level {
|
|
|
|
return []logrus.Level{
|
|
|
|
logrus.InfoLevel,
|
|
|
|
}
|
|
|
|
}
|