mirror of
https://github.com/pgsty/minio.git
synced 2026-07-20 12:40:24 +03:00
rename all remaining packages to internal/ (#12418)
This is to ensure that there are no projects that try to import `minio/minio/pkg` into their own repo. Any such common packages should go to `https://github.com/minio/pkg`
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ARN - SQS resource name representation.
|
||||
type ARN struct {
|
||||
TargetID
|
||||
region string
|
||||
}
|
||||
|
||||
// String - returns string representation.
|
||||
func (arn ARN) String() string {
|
||||
if arn.TargetID.ID == "" && arn.TargetID.Name == "" && arn.region == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return "arn:minio:sqs:" + arn.region + ":" + arn.TargetID.String()
|
||||
}
|
||||
|
||||
// MarshalXML - encodes to XML data.
|
||||
func (arn ARN) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
return e.EncodeElement(arn.String(), start)
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (arn *ARN) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var s string
|
||||
if err := d.DecodeElement(&s, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsedARN, err := parseARN(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*arn = *parsedARN
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseARN - parses string to ARN.
|
||||
func parseARN(s string) (*ARN, error) {
|
||||
// ARN must be in the format of arn:minio:sqs:<REGION>:<ID>:<TYPE>
|
||||
if !strings.HasPrefix(s, "arn:minio:sqs:") {
|
||||
return nil, &ErrInvalidARN{s}
|
||||
}
|
||||
|
||||
tokens := strings.Split(s, ":")
|
||||
if len(tokens) != 6 {
|
||||
return nil, &ErrInvalidARN{s}
|
||||
}
|
||||
|
||||
if tokens[4] == "" || tokens[5] == "" {
|
||||
return nil, &ErrInvalidARN{s}
|
||||
}
|
||||
|
||||
return &ARN{
|
||||
region: tokens[3],
|
||||
TargetID: TargetID{
|
||||
ID: tokens[4],
|
||||
Name: tokens[5],
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestARNString(t *testing.T) {
|
||||
testCases := []struct {
|
||||
arn ARN
|
||||
expectedResult string
|
||||
}{
|
||||
{ARN{}, ""},
|
||||
{ARN{TargetID{"1", "webhook"}, ""}, "arn:minio:sqs::1:webhook"},
|
||||
{ARN{TargetID{"1", "webhook"}, "us-east-1"}, "arn:minio:sqs:us-east-1:1:webhook"},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.arn.String()
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestARNMarshalXML(t *testing.T) {
|
||||
testCases := []struct {
|
||||
arn ARN
|
||||
expectedData []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{ARN{}, []byte("<ARN></ARN>"), false},
|
||||
{ARN{TargetID{"1", "webhook"}, ""}, []byte("<ARN>arn:minio:sqs::1:webhook</ARN>"), false},
|
||||
{ARN{TargetID{"1", "webhook"}, "us-east-1"}, []byte("<ARN>arn:minio:sqs:us-east-1:1:webhook</ARN>"), false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
data, err := xml.Marshal(testCase.arn)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(data, testCase.expectedData) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestARNUnmarshalXML(t *testing.T) {
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedARN *ARN
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte("<ARN></ARN>"), nil, true},
|
||||
{[]byte("<ARN>arn:minio:sqs:::</ARN>"), nil, true},
|
||||
{[]byte("<ARN>arn:minio:sqs::1:webhook</ARN>"), &ARN{TargetID{"1", "webhook"}, ""}, false},
|
||||
{[]byte("<ARN>arn:minio:sqs:us-east-1:1:webhook</ARN>"), &ARN{TargetID{"1", "webhook"}, "us-east-1"}, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
arn := &ARN{}
|
||||
err := xml.Unmarshal(testCase.data, &arn)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if *arn != *testCase.expectedARN {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseARN(t *testing.T) {
|
||||
testCases := []struct {
|
||||
s string
|
||||
expectedARN *ARN
|
||||
expectErr bool
|
||||
}{
|
||||
{"", nil, true},
|
||||
{"arn:minio:sqs:::", nil, true},
|
||||
{"arn:minio:sqs::1:webhook:remote", nil, true},
|
||||
{"arn:aws:sqs::1:webhook", nil, true},
|
||||
{"arn:minio:sns::1:webhook", nil, true},
|
||||
{"arn:minio:sqs::1:webhook", &ARN{TargetID{"1", "webhook"}, ""}, false},
|
||||
{"arn:minio:sqs:us-east-1:1:webhook", &ARN{TargetID{"1", "webhook"}, "us-east-1"}, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
arn, err := parseARN(testCase.s)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if *arn != *testCase.expectedARN {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,326 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/minio/minio-go/v7/pkg/set"
|
||||
)
|
||||
|
||||
// ValidateFilterRuleValue - checks if given value is filter rule value or not.
|
||||
func ValidateFilterRuleValue(value string) error {
|
||||
for _, segment := range strings.Split(value, "/") {
|
||||
if segment == "." || segment == ".." {
|
||||
return &ErrInvalidFilterValue{value}
|
||||
}
|
||||
}
|
||||
|
||||
if len(value) <= 1024 && utf8.ValidString(value) && !strings.Contains(value, `\`) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ErrInvalidFilterValue{value}
|
||||
}
|
||||
|
||||
// FilterRule - represents elements inside <FilterRule>...</FilterRule>
|
||||
type FilterRule struct {
|
||||
Name string `xml:"Name"`
|
||||
Value string `xml:"Value"`
|
||||
}
|
||||
|
||||
func (filter FilterRule) isEmpty() bool {
|
||||
return filter.Name == "" && filter.Value == ""
|
||||
}
|
||||
|
||||
// MarshalXML implements a custom marshaller to support `omitempty` feature.
|
||||
func (filter FilterRule) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
if filter.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
type filterRuleWrapper FilterRule
|
||||
return e.EncodeElement(filterRuleWrapper(filter), start)
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (filter *FilterRule) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
// Make subtype to avoid recursive UnmarshalXML().
|
||||
type filterRule FilterRule
|
||||
rule := filterRule{}
|
||||
if err := d.DecodeElement(&rule, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rule.Name != "prefix" && rule.Name != "suffix" {
|
||||
return &ErrInvalidFilterName{rule.Name}
|
||||
}
|
||||
|
||||
if err := ValidateFilterRuleValue(filter.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*filter = FilterRule(rule)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterRuleList - represents multiple <FilterRule>...</FilterRule>
|
||||
type FilterRuleList struct {
|
||||
Rules []FilterRule `xml:"FilterRule,omitempty"`
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (ruleList *FilterRuleList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
// Make subtype to avoid recursive UnmarshalXML().
|
||||
type filterRuleList FilterRuleList
|
||||
rules := filterRuleList{}
|
||||
if err := d.DecodeElement(&rules, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// FilterRuleList must have only one prefix and/or suffix.
|
||||
nameSet := set.NewStringSet()
|
||||
for _, rule := range rules.Rules {
|
||||
if nameSet.Contains(rule.Name) {
|
||||
if rule.Name == "prefix" {
|
||||
return &ErrFilterNamePrefix{}
|
||||
}
|
||||
|
||||
return &ErrFilterNameSuffix{}
|
||||
}
|
||||
|
||||
nameSet.Add(rule.Name)
|
||||
}
|
||||
|
||||
*ruleList = FilterRuleList(rules)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ruleList FilterRuleList) isEmpty() bool {
|
||||
return len(ruleList.Rules) == 0
|
||||
}
|
||||
|
||||
// Pattern - returns pattern using prefix and suffix values.
|
||||
func (ruleList FilterRuleList) Pattern() string {
|
||||
var prefix string
|
||||
var suffix string
|
||||
|
||||
for _, rule := range ruleList.Rules {
|
||||
switch rule.Name {
|
||||
case "prefix":
|
||||
prefix = rule.Value
|
||||
case "suffix":
|
||||
suffix = rule.Value
|
||||
}
|
||||
}
|
||||
|
||||
return NewPattern(prefix, suffix)
|
||||
}
|
||||
|
||||
// S3Key - represents elements inside <S3Key>...</S3Key>
|
||||
type S3Key struct {
|
||||
RuleList FilterRuleList `xml:"S3Key,omitempty" json:"S3Key,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalXML implements a custom marshaller to support `omitempty` feature.
|
||||
func (s3Key S3Key) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
if s3Key.RuleList.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
type s3KeyWrapper S3Key
|
||||
return e.EncodeElement(s3KeyWrapper(s3Key), start)
|
||||
}
|
||||
|
||||
// common - represents common elements inside <QueueConfiguration>, <CloudFunctionConfiguration>
|
||||
// and <TopicConfiguration>
|
||||
type common struct {
|
||||
ID string `xml:"Id" json:"Id"`
|
||||
Filter S3Key `xml:"Filter" json:"Filter"`
|
||||
Events []Name `xml:"Event" json:"Event"`
|
||||
}
|
||||
|
||||
// Queue - represents elements inside <QueueConfiguration>
|
||||
type Queue struct {
|
||||
common
|
||||
ARN ARN `xml:"Queue"`
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (q *Queue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
// Make subtype to avoid recursive UnmarshalXML().
|
||||
type queue Queue
|
||||
parsedQueue := queue{}
|
||||
if err := d.DecodeElement(&parsedQueue, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(parsedQueue.Events) == 0 {
|
||||
return errors.New("missing event name(s)")
|
||||
}
|
||||
|
||||
eventStringSet := set.NewStringSet()
|
||||
for _, eventName := range parsedQueue.Events {
|
||||
if eventStringSet.Contains(eventName.String()) {
|
||||
return &ErrDuplicateEventName{eventName}
|
||||
}
|
||||
|
||||
eventStringSet.Add(eventName.String())
|
||||
}
|
||||
|
||||
*q = Queue(parsedQueue)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate - checks whether queue has valid values or not.
|
||||
func (q Queue) Validate(region string, targetList *TargetList) error {
|
||||
if q.ARN.region == "" {
|
||||
if !targetList.Exists(q.ARN.TargetID) {
|
||||
return &ErrARNNotFound{q.ARN}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if region != "" && q.ARN.region != region {
|
||||
return &ErrUnknownRegion{q.ARN.region}
|
||||
}
|
||||
|
||||
if !targetList.Exists(q.ARN.TargetID) {
|
||||
return &ErrARNNotFound{q.ARN}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRegion - sets region value to queue's ARN.
|
||||
func (q *Queue) SetRegion(region string) {
|
||||
q.ARN.region = region
|
||||
}
|
||||
|
||||
// ToRulesMap - converts Queue to RulesMap
|
||||
func (q Queue) ToRulesMap() RulesMap {
|
||||
pattern := q.Filter.RuleList.Pattern()
|
||||
return NewRulesMap(q.Events, pattern, q.ARN.TargetID)
|
||||
}
|
||||
|
||||
// Unused. Available for completion.
|
||||
type lambda struct {
|
||||
ARN string `xml:"CloudFunction"`
|
||||
}
|
||||
|
||||
// Unused. Available for completion.
|
||||
type topic struct {
|
||||
ARN string `xml:"Topic" json:"Topic"`
|
||||
}
|
||||
|
||||
// Config - notification configuration described in
|
||||
// http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
|
||||
type Config struct {
|
||||
XMLNS string `xml:"xmlns,attr,omitempty"`
|
||||
XMLName xml.Name `xml:"NotificationConfiguration"`
|
||||
QueueList []Queue `xml:"QueueConfiguration,omitempty"`
|
||||
LambdaList []lambda `xml:"CloudFunctionConfiguration,omitempty"`
|
||||
TopicList []topic `xml:"TopicConfiguration,omitempty"`
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (conf *Config) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
// Make subtype to avoid recursive UnmarshalXML().
|
||||
type config Config
|
||||
parsedConfig := config{}
|
||||
if err := d.DecodeElement(&parsedConfig, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Empty queue list means user wants to delete the notification configuration.
|
||||
if len(parsedConfig.QueueList) > 0 {
|
||||
for i, q1 := range parsedConfig.QueueList[:len(parsedConfig.QueueList)-1] {
|
||||
for _, q2 := range parsedConfig.QueueList[i+1:] {
|
||||
// Removes the region from ARN if server region is not set
|
||||
if q2.ARN.region != "" && q1.ARN.region == "" {
|
||||
q2.ARN.region = ""
|
||||
}
|
||||
if reflect.DeepEqual(q1, q2) {
|
||||
return &ErrDuplicateQueueConfiguration{q1}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(parsedConfig.LambdaList) > 0 || len(parsedConfig.TopicList) > 0 {
|
||||
return &ErrUnsupportedConfiguration{}
|
||||
}
|
||||
|
||||
*conf = Config(parsedConfig)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate - checks whether config has valid values or not.
|
||||
func (conf Config) Validate(region string, targetList *TargetList) error {
|
||||
for _, queue := range conf.QueueList {
|
||||
if err := queue.Validate(region, targetList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRegion - sets region to all queue configuration.
|
||||
func (conf *Config) SetRegion(region string) {
|
||||
for i := range conf.QueueList {
|
||||
conf.QueueList[i].SetRegion(region)
|
||||
}
|
||||
}
|
||||
|
||||
// ToRulesMap - converts all queue configuration to RulesMap.
|
||||
func (conf *Config) ToRulesMap() RulesMap {
|
||||
rulesMap := make(RulesMap)
|
||||
|
||||
for _, queue := range conf.QueueList {
|
||||
rulesMap.Add(queue.ToRulesMap())
|
||||
}
|
||||
|
||||
return rulesMap
|
||||
}
|
||||
|
||||
// ParseConfig - parses data in reader to notification configuration.
|
||||
func ParseConfig(reader io.Reader, region string, targetList *TargetList) (*Config, error) {
|
||||
var config Config
|
||||
|
||||
if err := xml.NewDecoder(reader).Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := config.Validate(region, targetList); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.SetRegion(region)
|
||||
//If xml namespace is empty, set a default value before returning.
|
||||
if config.XMLNS == "" {
|
||||
config.XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/"
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
@@ -0,0 +1,961 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateFilterRuleValue(t *testing.T) {
|
||||
testCases := []struct {
|
||||
value string
|
||||
expectErr bool
|
||||
}{
|
||||
{"foo/.", true},
|
||||
{"../foo", true},
|
||||
{`foo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/bazfoo/bar/baz`, true},
|
||||
{string([]byte{0xff, 0xfe, 0xfd}), true},
|
||||
{`foo\bar`, true},
|
||||
{"Hello/世界", false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := ValidateFilterRuleValue(testCase.value)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterRuleUnmarshalXML(t *testing.T) {
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedResult *FilterRule
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte(`<FilterRule></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Name></Name></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Value></Value></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Name></Name><Value></Value></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Name>Prefix</Name><Value>Hello/世界</Value></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Name>ends</Name><Value>foo/bar</Value></FilterRule>`), nil, true},
|
||||
{[]byte(`<FilterRule><Name>prefix</Name><Value>Hello/世界</Value></FilterRule>`), &FilterRule{"prefix", "Hello/世界"}, false},
|
||||
{[]byte(`<FilterRule><Name>suffix</Name><Value>foo/bar</Value></FilterRule>`), &FilterRule{"suffix", "foo/bar"}, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := &FilterRule{}
|
||||
err := xml.Unmarshal(testCase.data, result)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterRuleListUnmarshalXML(t *testing.T) {
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedResult *FilterRuleList
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte(`<S3Key><FilterRule><Name>suffix</Name><Value>Hello/世界</Value></FilterRule><FilterRule><Name>suffix</Name><Value>foo/bar</Value></FilterRule></S3Key>`), nil, true},
|
||||
{[]byte(`<S3Key><FilterRule><Name>prefix</Name><Value>Hello/世界</Value></FilterRule><FilterRule><Name>prefix</Name><Value>foo/bar</Value></FilterRule></S3Key>`), nil, true},
|
||||
{[]byte(`<S3Key><FilterRule><Name>prefix</Name><Value>Hello/世界</Value></FilterRule></S3Key>`), &FilterRuleList{[]FilterRule{{"prefix", "Hello/世界"}}}, false},
|
||||
{[]byte(`<S3Key><FilterRule><Name>suffix</Name><Value>foo/bar</Value></FilterRule></S3Key>`), &FilterRuleList{[]FilterRule{{"suffix", "foo/bar"}}}, false},
|
||||
{[]byte(`<S3Key><FilterRule><Name>prefix</Name><Value>Hello/世界</Value></FilterRule><FilterRule><Name>suffix</Name><Value>foo/bar</Value></FilterRule></S3Key>`), &FilterRuleList{[]FilterRule{{"prefix", "Hello/世界"}, {"suffix", "foo/bar"}}}, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := &FilterRuleList{}
|
||||
err := xml.Unmarshal(testCase.data, result)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterRuleListPattern(t *testing.T) {
|
||||
testCases := []struct {
|
||||
filterRuleList FilterRuleList
|
||||
expectedResult string
|
||||
}{
|
||||
{FilterRuleList{}, ""},
|
||||
{FilterRuleList{[]FilterRule{{"prefix", "Hello/世界"}}}, "Hello/世界*"},
|
||||
{FilterRuleList{[]FilterRule{{"suffix", "foo/bar"}}}, "*foo/bar"},
|
||||
{FilterRuleList{[]FilterRule{{"prefix", "Hello/世界"}, {"suffix", "foo/bar"}}}, "Hello/世界*foo/bar"},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.filterRuleList.Pattern()
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueUnmarshalXML(t *testing.T) {
|
||||
dataCase1 := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>`)
|
||||
|
||||
dataCase2 := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>`)
|
||||
|
||||
dataCase3 := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>`)
|
||||
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{dataCase1, false},
|
||||
{dataCase2, false},
|
||||
{dataCase3, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := xml.Unmarshal(testCase.data, &Queue{})
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueValidate(t *testing.T) {
|
||||
data := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>`)
|
||||
queue1 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queue1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>`)
|
||||
queue2 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queue2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:eu-west-2:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>`)
|
||||
queue3 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queue3); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetList1 := NewTargetList()
|
||||
|
||||
targetList2 := NewTargetList()
|
||||
if err := targetList2.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
queue *Queue
|
||||
region string
|
||||
targetList *TargetList
|
||||
expectErr bool
|
||||
}{
|
||||
{queue1, "eu-west-1", nil, true},
|
||||
{queue2, "us-east-1", targetList1, true},
|
||||
{queue3, "", targetList2, false},
|
||||
{queue2, "us-east-1", targetList2, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := testCase.queue.Validate(testCase.region, testCase.targetList)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueSetRegion(t *testing.T) {
|
||||
data := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>`)
|
||||
queue1 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queue1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs::1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>`)
|
||||
queue2 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queue2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
queue *Queue
|
||||
region string
|
||||
expectedResult ARN
|
||||
}{
|
||||
{queue1, "eu-west-1", ARN{TargetID{"1", "webhook"}, "eu-west-1"}},
|
||||
{queue1, "", ARN{TargetID{"1", "webhook"}, ""}},
|
||||
{queue2, "us-east-1", ARN{TargetID{"1", "webhook"}, "us-east-1"}},
|
||||
{queue2, "", ARN{TargetID{"1", "webhook"}, ""}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
testCase.queue.SetRegion(testCase.region)
|
||||
result := testCase.queue.ARN
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueToRulesMap(t *testing.T) {
|
||||
data := []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>`)
|
||||
queueCase1 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queueCase1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>`)
|
||||
queueCase2 := &Queue{}
|
||||
if err := xml.Unmarshal(data, queueCase2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rulesMapCase1 := NewRulesMap([]Name{ObjectAccessedAll, ObjectCreatedAll, ObjectRemovedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedPut}, "images/*jpg", TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
queue *Queue
|
||||
expectedResult RulesMap
|
||||
}{
|
||||
{queueCase1, rulesMapCase1},
|
||||
{queueCase2, rulesMapCase2},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.queue.ToRulesMap()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigUnmarshalXML(t *testing.T) {
|
||||
dataCase1 := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
dataCase2 := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
dataCase3 := []byte(`
|
||||
<NotificationConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>2</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
dataCase4 := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<CloudFunctionConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>.jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Cloudcode>arn:aws:lambda:us-west-2:444455556666:cloud-function-A</Cloudcode>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</CloudFunctionConfiguration>
|
||||
<TopicConfiguration>
|
||||
<Topic>arn:aws:sns:us-west-2:444455556666:sns-notification-one</Topic>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
</TopicConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
dataCase5 := []byte(`<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/" ></NotificationConfiguration>`)
|
||||
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{dataCase1, false},
|
||||
{dataCase2, false},
|
||||
{dataCase3, false},
|
||||
{dataCase4, true},
|
||||
// make sure we don't fail when queue is empty.
|
||||
{dataCase5, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := xml.Unmarshal(testCase.data, &Config{})
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate(t *testing.T) {
|
||||
data := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config1 := &Config{}
|
||||
if err := xml.Unmarshal(data, config1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config2 := &Config{}
|
||||
if err := xml.Unmarshal(data, config2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>2</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config3 := &Config{}
|
||||
if err := xml.Unmarshal(data, config3); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetList1 := NewTargetList()
|
||||
|
||||
targetList2 := NewTargetList()
|
||||
if err := targetList2.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
config *Config
|
||||
region string
|
||||
targetList *TargetList
|
||||
expectErr bool
|
||||
}{
|
||||
{config1, "eu-west-1", nil, true},
|
||||
{config2, "us-east-1", targetList1, true},
|
||||
{config3, "", targetList2, false},
|
||||
{config2, "us-east-1", targetList2, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := testCase.config.Validate(testCase.region, testCase.targetList)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigSetRegion(t *testing.T) {
|
||||
data := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config1 := &Config{}
|
||||
if err := xml.Unmarshal(data, config1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs::1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config2 := &Config{}
|
||||
if err := xml.Unmarshal(data, config2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>2</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:2:amqp</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config3 := &Config{}
|
||||
if err := xml.Unmarshal(data, config3); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
config *Config
|
||||
region string
|
||||
expectedResult []ARN
|
||||
}{
|
||||
{config1, "eu-west-1", []ARN{{TargetID{"1", "webhook"}, "eu-west-1"}}},
|
||||
{config1, "", []ARN{{TargetID{"1", "webhook"}, ""}}},
|
||||
{config2, "us-east-1", []ARN{{TargetID{"1", "webhook"}, "us-east-1"}}},
|
||||
{config2, "", []ARN{{TargetID{"1", "webhook"}, ""}}},
|
||||
{config3, "us-east-1", []ARN{{TargetID{"1", "webhook"}, "us-east-1"}, {TargetID{"2", "amqp"}, "us-east-1"}}},
|
||||
{config3, "", []ARN{{TargetID{"1", "webhook"}, ""}, {TargetID{"2", "amqp"}, ""}}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
testCase.config.SetRegion(testCase.region)
|
||||
result := []ARN{}
|
||||
for _, queue := range testCase.config.QueueList {
|
||||
result = append(result, queue.ARN)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigToRulesMap(t *testing.T) {
|
||||
data := []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config1 := &Config{}
|
||||
if err := xml.Unmarshal(data, config1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs::1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config2 := &Config{}
|
||||
if err := xml.Unmarshal(data, config2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data = []byte(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>2</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:2:amqp</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
config3 := &Config{}
|
||||
if err := xml.Unmarshal(data, config3); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rulesMapCase1 := NewRulesMap([]Name{ObjectAccessedAll, ObjectCreatedAll, ObjectRemovedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedPut}, "images/*jpg", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase3 := NewRulesMap([]Name{ObjectAccessedAll, ObjectCreatedAll, ObjectRemovedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapCase3.add([]Name{ObjectCreatedPut}, "images/*jpg", TargetID{"2", "amqp"})
|
||||
|
||||
testCases := []struct {
|
||||
config *Config
|
||||
expectedResult RulesMap
|
||||
}{
|
||||
{config1, rulesMapCase1},
|
||||
{config2, rulesMapCase2},
|
||||
{config3, rulesMapCase3},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.config.ToRulesMap()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
reader1 := strings.NewReader(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
reader2 := strings.NewReader(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
reader3 := strings.NewReader(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<QueueConfiguration>
|
||||
<Id>2</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>prefix</Name>
|
||||
<Value>images/</Value>
|
||||
</FilterRule>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</QueueConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
reader4 := strings.NewReader(`
|
||||
<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<QueueConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter></Filter>
|
||||
<Queue>arn:minio:sqs:us-east-1:1:webhook</Queue>
|
||||
<Event>s3:ObjectAccessed:*</Event>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
<Event>s3:ObjectRemoved:*</Event>
|
||||
</QueueConfiguration>
|
||||
<CloudFunctionConfiguration>
|
||||
<Id>1</Id>
|
||||
<Filter>
|
||||
<S3Key>
|
||||
<FilterRule>
|
||||
<Name>suffix</Name>
|
||||
<Value>.jpg</Value>
|
||||
</FilterRule>
|
||||
</S3Key>
|
||||
</Filter>
|
||||
<Cloudcode>arn:aws:lambda:us-west-2:444455556666:cloud-function-A</Cloudcode>
|
||||
<Event>s3:ObjectCreated:Put</Event>
|
||||
</CloudFunctionConfiguration>
|
||||
<TopicConfiguration>
|
||||
<Topic>arn:aws:sns:us-west-2:444455556666:sns-notification-one</Topic>
|
||||
<Event>s3:ObjectCreated:*</Event>
|
||||
</TopicConfiguration>
|
||||
</NotificationConfiguration>
|
||||
`)
|
||||
|
||||
targetList1 := NewTargetList()
|
||||
|
||||
targetList2 := NewTargetList()
|
||||
if err := targetList2.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
reader *strings.Reader
|
||||
region string
|
||||
targetList *TargetList
|
||||
expectErr bool
|
||||
}{
|
||||
{reader1, "eu-west-1", nil, true},
|
||||
{reader2, "us-east-1", targetList1, true},
|
||||
{reader4, "us-east-1", targetList1, true},
|
||||
{reader3, "", targetList2, false},
|
||||
{reader2, "us-east-1", targetList2, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
if _, err := testCase.reader.Seek(0, 0); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err := ParseConfig(testCase.reader, testCase.region, testCase.targetList)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// IsEventError - checks whether given error is event error or not.
|
||||
func IsEventError(err error) bool {
|
||||
switch err.(type) {
|
||||
case ErrInvalidFilterName, *ErrInvalidFilterName:
|
||||
return true
|
||||
case ErrFilterNamePrefix, *ErrFilterNamePrefix:
|
||||
return true
|
||||
case ErrFilterNameSuffix, *ErrFilterNameSuffix:
|
||||
return true
|
||||
case ErrInvalidFilterValue, *ErrInvalidFilterValue:
|
||||
return true
|
||||
case ErrDuplicateEventName, *ErrDuplicateEventName:
|
||||
return true
|
||||
case ErrUnsupportedConfiguration, *ErrUnsupportedConfiguration:
|
||||
return true
|
||||
case ErrDuplicateQueueConfiguration, *ErrDuplicateQueueConfiguration:
|
||||
return true
|
||||
case ErrUnknownRegion, *ErrUnknownRegion:
|
||||
return true
|
||||
case ErrARNNotFound, *ErrARNNotFound:
|
||||
return true
|
||||
case ErrInvalidARN, *ErrInvalidARN:
|
||||
return true
|
||||
case ErrInvalidEventName, *ErrInvalidEventName:
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ErrInvalidFilterName - invalid filter name error.
|
||||
type ErrInvalidFilterName struct {
|
||||
FilterName string
|
||||
}
|
||||
|
||||
func (err ErrInvalidFilterName) Error() string {
|
||||
return fmt.Sprintf("invalid filter name '%v'", err.FilterName)
|
||||
}
|
||||
|
||||
// ErrFilterNamePrefix - more than one prefix usage error.
|
||||
type ErrFilterNamePrefix struct{}
|
||||
|
||||
func (err ErrFilterNamePrefix) Error() string {
|
||||
return "more than one prefix in filter rule"
|
||||
}
|
||||
|
||||
// ErrFilterNameSuffix - more than one suffix usage error.
|
||||
type ErrFilterNameSuffix struct{}
|
||||
|
||||
func (err ErrFilterNameSuffix) Error() string {
|
||||
return "more than one suffix in filter rule"
|
||||
}
|
||||
|
||||
// ErrInvalidFilterValue - invalid filter value error.
|
||||
type ErrInvalidFilterValue struct {
|
||||
FilterValue string
|
||||
}
|
||||
|
||||
func (err ErrInvalidFilterValue) Error() string {
|
||||
return fmt.Sprintf("invalid filter value '%v'", err.FilterValue)
|
||||
}
|
||||
|
||||
// ErrDuplicateEventName - duplicate event name error.
|
||||
type ErrDuplicateEventName struct {
|
||||
EventName Name
|
||||
}
|
||||
|
||||
func (err ErrDuplicateEventName) Error() string {
|
||||
return fmt.Sprintf("duplicate event name '%v' found", err.EventName)
|
||||
}
|
||||
|
||||
// ErrUnsupportedConfiguration - unsupported configuration error.
|
||||
type ErrUnsupportedConfiguration struct{}
|
||||
|
||||
func (err ErrUnsupportedConfiguration) Error() string {
|
||||
return "topic or cloud function configuration is not supported"
|
||||
}
|
||||
|
||||
// ErrDuplicateQueueConfiguration - duplicate queue configuration error.
|
||||
type ErrDuplicateQueueConfiguration struct {
|
||||
Queue Queue
|
||||
}
|
||||
|
||||
func (err ErrDuplicateQueueConfiguration) Error() string {
|
||||
var message string
|
||||
if data, xerr := xml.Marshal(err.Queue); xerr != nil {
|
||||
message = fmt.Sprintf("%+v", err.Queue)
|
||||
} else {
|
||||
message = string(data)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("duplicate queue configuration %v", message)
|
||||
}
|
||||
|
||||
// ErrUnknownRegion - unknown region error.
|
||||
type ErrUnknownRegion struct {
|
||||
Region string
|
||||
}
|
||||
|
||||
func (err ErrUnknownRegion) Error() string {
|
||||
return fmt.Sprintf("unknown region '%v'", err.Region)
|
||||
}
|
||||
|
||||
// ErrARNNotFound - ARN not found error.
|
||||
type ErrARNNotFound struct {
|
||||
ARN ARN
|
||||
}
|
||||
|
||||
func (err ErrARNNotFound) Error() string {
|
||||
return fmt.Sprintf("ARN '%v' not found", err.ARN)
|
||||
}
|
||||
|
||||
// ErrInvalidARN - invalid ARN error.
|
||||
type ErrInvalidARN struct {
|
||||
ARN string
|
||||
}
|
||||
|
||||
func (err ErrInvalidARN) Error() string {
|
||||
return fmt.Sprintf("invalid ARN '%v'", err.ARN)
|
||||
}
|
||||
|
||||
// ErrInvalidEventName - invalid event name error.
|
||||
type ErrInvalidEventName struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (err ErrInvalidEventName) Error() string {
|
||||
return fmt.Sprintf("invalid event name '%v'", err.Name)
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
const (
|
||||
// NamespaceFormat - namespace log format used in some event targets.
|
||||
NamespaceFormat = "namespace"
|
||||
|
||||
// AccessFormat - access log format used in some event targets.
|
||||
AccessFormat = "access"
|
||||
|
||||
// AMZTimeFormat - event time format.
|
||||
AMZTimeFormat = "2006-01-02T15:04:05.000Z"
|
||||
)
|
||||
|
||||
// Identity represents access key who caused the event.
|
||||
type Identity struct {
|
||||
PrincipalID string `json:"principalId"`
|
||||
}
|
||||
|
||||
// Bucket represents bucket metadata of the event.
|
||||
type Bucket struct {
|
||||
Name string `json:"name"`
|
||||
OwnerIdentity Identity `json:"ownerIdentity"`
|
||||
ARN string `json:"arn"`
|
||||
}
|
||||
|
||||
// Object represents object metadata of the event.
|
||||
type Object struct {
|
||||
Key string `json:"key"`
|
||||
Size int64 `json:"size,omitempty"`
|
||||
ETag string `json:"eTag,omitempty"`
|
||||
ContentType string `json:"contentType,omitempty"`
|
||||
UserMetadata map[string]string `json:"userMetadata,omitempty"`
|
||||
VersionID string `json:"versionId,omitempty"`
|
||||
Sequencer string `json:"sequencer"`
|
||||
}
|
||||
|
||||
// Metadata represents event metadata.
|
||||
type Metadata struct {
|
||||
SchemaVersion string `json:"s3SchemaVersion"`
|
||||
ConfigurationID string `json:"configurationId"`
|
||||
Bucket Bucket `json:"bucket"`
|
||||
Object Object `json:"object"`
|
||||
}
|
||||
|
||||
// Source represents client information who triggered the event.
|
||||
type Source struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
UserAgent string `json:"userAgent"`
|
||||
}
|
||||
|
||||
// Event represents event notification information defined in
|
||||
// http://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html.
|
||||
type Event struct {
|
||||
EventVersion string `json:"eventVersion"`
|
||||
EventSource string `json:"eventSource"`
|
||||
AwsRegion string `json:"awsRegion"`
|
||||
EventTime string `json:"eventTime"`
|
||||
EventName Name `json:"eventName"`
|
||||
UserIdentity Identity `json:"userIdentity"`
|
||||
RequestParameters map[string]string `json:"requestParameters"`
|
||||
ResponseElements map[string]string `json:"responseElements"`
|
||||
S3 Metadata `json:"s3"`
|
||||
Source Source `json:"source"`
|
||||
}
|
||||
|
||||
// Log represents event information for some event targets.
|
||||
type Log struct {
|
||||
EventName Name
|
||||
Key string
|
||||
Records []Event
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
// Name - event type enum.
|
||||
// Refer http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#notification-how-to-event-types-and-destinations
|
||||
// for most basic values we have since extend this and its not really much applicable other than a reference point.
|
||||
// "s3:Replication:OperationCompletedReplication" is a MinIO extension.
|
||||
type Name int
|
||||
|
||||
// Values of event Name
|
||||
const (
|
||||
ObjectAccessedAll Name = 1 + iota
|
||||
ObjectAccessedGet
|
||||
ObjectAccessedGetRetention
|
||||
ObjectAccessedGetLegalHold
|
||||
ObjectAccessedHead
|
||||
ObjectCreatedAll
|
||||
ObjectCreatedCompleteMultipartUpload
|
||||
ObjectCreatedCopy
|
||||
ObjectCreatedPost
|
||||
ObjectCreatedPut
|
||||
ObjectCreatedPutRetention
|
||||
ObjectCreatedPutLegalHold
|
||||
ObjectCreatedPutTagging
|
||||
ObjectCreatedDeleteTagging
|
||||
ObjectRemovedAll
|
||||
ObjectRemovedDelete
|
||||
ObjectRemovedDeleteMarkerCreated
|
||||
BucketCreated
|
||||
BucketRemoved
|
||||
ObjectReplicationAll
|
||||
ObjectReplicationFailed
|
||||
ObjectReplicationComplete
|
||||
ObjectReplicationMissedThreshold
|
||||
ObjectReplicationReplicatedAfterThreshold
|
||||
ObjectReplicationNotTracked
|
||||
ObjectRestorePostInitiated
|
||||
ObjectRestorePostCompleted
|
||||
ObjectRestorePostAll
|
||||
ObjectTransitionAll
|
||||
ObjectTransitionFailed
|
||||
ObjectTransitionComplete
|
||||
)
|
||||
|
||||
// Expand - returns expanded values of abbreviated event type.
|
||||
func (name Name) Expand() []Name {
|
||||
switch name {
|
||||
case BucketCreated:
|
||||
return []Name{BucketCreated}
|
||||
case BucketRemoved:
|
||||
return []Name{BucketRemoved}
|
||||
case ObjectAccessedAll:
|
||||
return []Name{
|
||||
ObjectAccessedGet, ObjectAccessedHead,
|
||||
ObjectAccessedGetRetention, ObjectAccessedGetLegalHold,
|
||||
}
|
||||
case ObjectCreatedAll:
|
||||
return []Name{
|
||||
ObjectCreatedCompleteMultipartUpload, ObjectCreatedCopy,
|
||||
ObjectCreatedPost, ObjectCreatedPut,
|
||||
ObjectCreatedPutRetention, ObjectCreatedPutLegalHold,
|
||||
ObjectCreatedPutTagging, ObjectCreatedDeleteTagging,
|
||||
}
|
||||
case ObjectRemovedAll:
|
||||
return []Name{
|
||||
ObjectRemovedDelete,
|
||||
ObjectRemovedDeleteMarkerCreated,
|
||||
}
|
||||
case ObjectReplicationAll:
|
||||
return []Name{
|
||||
ObjectReplicationFailed,
|
||||
ObjectReplicationComplete,
|
||||
ObjectReplicationNotTracked,
|
||||
ObjectReplicationMissedThreshold,
|
||||
ObjectReplicationReplicatedAfterThreshold,
|
||||
}
|
||||
case ObjectRestorePostAll:
|
||||
return []Name{
|
||||
ObjectRestorePostInitiated,
|
||||
ObjectRestorePostCompleted,
|
||||
}
|
||||
case ObjectTransitionAll:
|
||||
return []Name{
|
||||
ObjectTransitionFailed,
|
||||
ObjectTransitionComplete,
|
||||
}
|
||||
default:
|
||||
return []Name{name}
|
||||
}
|
||||
}
|
||||
|
||||
// String - returns string representation of event type.
|
||||
func (name Name) String() string {
|
||||
switch name {
|
||||
case BucketCreated:
|
||||
return "s3:BucketCreated:*"
|
||||
case BucketRemoved:
|
||||
return "s3:BucketRemoved:*"
|
||||
case ObjectAccessedAll:
|
||||
return "s3:ObjectAccessed:*"
|
||||
case ObjectAccessedGet:
|
||||
return "s3:ObjectAccessed:Get"
|
||||
case ObjectAccessedGetRetention:
|
||||
return "s3:ObjectAccessed:GetRetention"
|
||||
case ObjectAccessedGetLegalHold:
|
||||
return "s3:ObjectAccessed:GetLegalHold"
|
||||
case ObjectAccessedHead:
|
||||
return "s3:ObjectAccessed:Head"
|
||||
case ObjectCreatedAll:
|
||||
return "s3:ObjectCreated:*"
|
||||
case ObjectCreatedCompleteMultipartUpload:
|
||||
return "s3:ObjectCreated:CompleteMultipartUpload"
|
||||
case ObjectCreatedCopy:
|
||||
return "s3:ObjectCreated:Copy"
|
||||
case ObjectCreatedPost:
|
||||
return "s3:ObjectCreated:Post"
|
||||
case ObjectCreatedPut:
|
||||
return "s3:ObjectCreated:Put"
|
||||
case ObjectCreatedPutTagging:
|
||||
return "s3:ObjectCreated:PutTagging"
|
||||
case ObjectCreatedDeleteTagging:
|
||||
return "s3:ObjectCreated:DeleteTagging"
|
||||
case ObjectCreatedPutRetention:
|
||||
return "s3:ObjectCreated:PutRetention"
|
||||
case ObjectCreatedPutLegalHold:
|
||||
return "s3:ObjectCreated:PutLegalHold"
|
||||
case ObjectRemovedAll:
|
||||
return "s3:ObjectRemoved:*"
|
||||
case ObjectRemovedDelete:
|
||||
return "s3:ObjectRemoved:Delete"
|
||||
case ObjectRemovedDeleteMarkerCreated:
|
||||
return "s3:ObjectRemoved:DeleteMarkerCreated"
|
||||
case ObjectReplicationAll:
|
||||
return "s3:Replication:*"
|
||||
case ObjectReplicationFailed:
|
||||
return "s3:Replication:OperationFailedReplication"
|
||||
case ObjectReplicationComplete:
|
||||
return "s3:Replication:OperationCompletedReplication"
|
||||
case ObjectReplicationNotTracked:
|
||||
return "s3:Replication:OperationNotTracked"
|
||||
case ObjectReplicationMissedThreshold:
|
||||
return "s3:Replication:OperationMissedThreshold"
|
||||
case ObjectReplicationReplicatedAfterThreshold:
|
||||
return "s3:Replication:OperationReplicatedAfterThreshold"
|
||||
case ObjectRestorePostInitiated:
|
||||
return "s3:ObjectRestore:Post"
|
||||
case ObjectRestorePostCompleted:
|
||||
return "s3:ObjectRestore:Completed"
|
||||
case ObjectTransitionAll:
|
||||
return "s3:ObjectTransition:*"
|
||||
case ObjectTransitionFailed:
|
||||
return "s3:ObjectTransition:Failed"
|
||||
case ObjectTransitionComplete:
|
||||
return "s3:ObjectTransition:Complete"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// MarshalXML - encodes to XML data.
|
||||
func (name Name) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
return e.EncodeElement(name.String(), start)
|
||||
}
|
||||
|
||||
// UnmarshalXML - decodes XML data.
|
||||
func (name *Name) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var s string
|
||||
if err := d.DecodeElement(&s, &start); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventName, err := ParseName(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*name = eventName
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON - encodes to JSON data.
|
||||
func (name Name) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(name.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON - decodes JSON data.
|
||||
func (name *Name) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventName, err := ParseName(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*name = eventName
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseName - parses string to Name.
|
||||
func ParseName(s string) (Name, error) {
|
||||
switch s {
|
||||
case "s3:BucketCreated:*":
|
||||
return BucketCreated, nil
|
||||
case "s3:BucketRemoved:*":
|
||||
return BucketRemoved, nil
|
||||
case "s3:ObjectAccessed:*":
|
||||
return ObjectAccessedAll, nil
|
||||
case "s3:ObjectAccessed:Get":
|
||||
return ObjectAccessedGet, nil
|
||||
case "s3:ObjectAccessed:GetRetention":
|
||||
return ObjectAccessedGetRetention, nil
|
||||
case "s3:ObjectAccessed:GetLegalHold":
|
||||
return ObjectAccessedGetLegalHold, nil
|
||||
case "s3:ObjectAccessed:Head":
|
||||
return ObjectAccessedHead, nil
|
||||
case "s3:ObjectCreated:*":
|
||||
return ObjectCreatedAll, nil
|
||||
case "s3:ObjectCreated:CompleteMultipartUpload":
|
||||
return ObjectCreatedCompleteMultipartUpload, nil
|
||||
case "s3:ObjectCreated:Copy":
|
||||
return ObjectCreatedCopy, nil
|
||||
case "s3:ObjectCreated:Post":
|
||||
return ObjectCreatedPost, nil
|
||||
case "s3:ObjectCreated:Put":
|
||||
return ObjectCreatedPut, nil
|
||||
case "s3:ObjectCreated:PutRetention":
|
||||
return ObjectCreatedPutRetention, nil
|
||||
case "s3:ObjectCreated:PutLegalHold":
|
||||
return ObjectCreatedPutLegalHold, nil
|
||||
case "s3:ObjectCreated:PutTagging":
|
||||
return ObjectCreatedPutTagging, nil
|
||||
case "s3:ObjectCreated:DeleteTagging":
|
||||
return ObjectCreatedDeleteTagging, nil
|
||||
case "s3:ObjectRemoved:*":
|
||||
return ObjectRemovedAll, nil
|
||||
case "s3:ObjectRemoved:Delete":
|
||||
return ObjectRemovedDelete, nil
|
||||
case "s3:ObjectRemoved:DeleteMarkerCreated":
|
||||
return ObjectRemovedDeleteMarkerCreated, nil
|
||||
case "s3:Replication:*":
|
||||
return ObjectReplicationAll, nil
|
||||
case "s3:Replication:OperationFailedReplication":
|
||||
return ObjectReplicationFailed, nil
|
||||
case "s3:Replication:OperationCompletedReplication":
|
||||
return ObjectReplicationComplete, nil
|
||||
case "s3:Replication:OperationMissedThreshold":
|
||||
return ObjectReplicationMissedThreshold, nil
|
||||
case "s3:Replication:OperationReplicatedAfterThreshold":
|
||||
return ObjectReplicationReplicatedAfterThreshold, nil
|
||||
case "s3:Replication:OperationNotTracked":
|
||||
return ObjectReplicationNotTracked, nil
|
||||
case "s3:ObjectRestore:*":
|
||||
return ObjectRestorePostAll, nil
|
||||
case "s3:ObjectRestore:Post":
|
||||
return ObjectRestorePostInitiated, nil
|
||||
case "s3:ObjectRestore:Completed":
|
||||
return ObjectRestorePostCompleted, nil
|
||||
case "s3:ObjectTransition:Failed":
|
||||
return ObjectTransitionFailed, nil
|
||||
case "s3:ObjectTransition:Complete":
|
||||
return ObjectTransitionComplete, nil
|
||||
case "s3:ObjectTransition:*":
|
||||
return ObjectTransitionAll, nil
|
||||
default:
|
||||
return 0, &ErrInvalidEventName{s}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNameExpand(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name Name
|
||||
expectedResult []Name
|
||||
}{
|
||||
{BucketCreated, []Name{BucketCreated}},
|
||||
{BucketRemoved, []Name{BucketRemoved}},
|
||||
{ObjectAccessedAll, []Name{ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold}},
|
||||
{ObjectCreatedAll, []Name{ObjectCreatedCompleteMultipartUpload, ObjectCreatedCopy, ObjectCreatedPost, ObjectCreatedPut,
|
||||
ObjectCreatedPutRetention, ObjectCreatedPutLegalHold, ObjectCreatedPutTagging, ObjectCreatedDeleteTagging}},
|
||||
{ObjectRemovedAll, []Name{ObjectRemovedDelete, ObjectRemovedDeleteMarkerCreated}},
|
||||
{ObjectAccessedHead, []Name{ObjectAccessedHead}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.name.Expand()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Errorf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameString(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
name Name
|
||||
expectedResult string
|
||||
}{
|
||||
{BucketCreated, "s3:BucketCreated:*"},
|
||||
{BucketRemoved, "s3:BucketRemoved:*"},
|
||||
{ObjectAccessedAll, "s3:ObjectAccessed:*"},
|
||||
{ObjectAccessedGet, "s3:ObjectAccessed:Get"},
|
||||
{ObjectAccessedHead, "s3:ObjectAccessed:Head"},
|
||||
{ObjectCreatedAll, "s3:ObjectCreated:*"},
|
||||
{ObjectCreatedCompleteMultipartUpload, "s3:ObjectCreated:CompleteMultipartUpload"},
|
||||
{ObjectCreatedCopy, "s3:ObjectCreated:Copy"},
|
||||
{ObjectCreatedPost, "s3:ObjectCreated:Post"},
|
||||
{ObjectCreatedPut, "s3:ObjectCreated:Put"},
|
||||
{ObjectRemovedAll, "s3:ObjectRemoved:*"},
|
||||
{ObjectRemovedDelete, "s3:ObjectRemoved:Delete"},
|
||||
{ObjectCreatedPutRetention, "s3:ObjectCreated:PutRetention"},
|
||||
{ObjectCreatedPutLegalHold, "s3:ObjectCreated:PutLegalHold"},
|
||||
{ObjectAccessedGetRetention, "s3:ObjectAccessed:GetRetention"},
|
||||
{ObjectAccessedGetLegalHold, "s3:ObjectAccessed:GetLegalHold"},
|
||||
|
||||
{blankName, ""},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.name.String()
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameMarshalXML(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
name Name
|
||||
expectedData []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{ObjectAccessedAll, []byte("<Name>s3:ObjectAccessed:*</Name>"), false},
|
||||
{ObjectRemovedDelete, []byte("<Name>s3:ObjectRemoved:Delete</Name>"), false},
|
||||
{blankName, []byte("<Name></Name>"), false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
data, err := xml.Marshal(testCase.name)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(data, testCase.expectedData) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameUnmarshalXML(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedName Name
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte("<Name>s3:ObjectAccessed:*</Name>"), ObjectAccessedAll, false},
|
||||
{[]byte("<Name>s3:ObjectRemoved:Delete</Name>"), ObjectRemovedDelete, false},
|
||||
{[]byte("<Name></Name>"), blankName, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
var name Name
|
||||
err := xml.Unmarshal(testCase.data, &name)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(name, testCase.expectedName) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameMarshalJSON(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
name Name
|
||||
expectedData []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{ObjectAccessedAll, []byte(`"s3:ObjectAccessed:*"`), false},
|
||||
{ObjectRemovedDelete, []byte(`"s3:ObjectRemoved:Delete"`), false},
|
||||
{blankName, []byte(`""`), false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
data, err := json.Marshal(testCase.name)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(data, testCase.expectedData) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameUnmarshalJSON(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedName Name
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte(`"s3:ObjectAccessed:*"`), ObjectAccessedAll, false},
|
||||
{[]byte(`"s3:ObjectRemoved:Delete"`), ObjectRemovedDelete, false},
|
||||
{[]byte(`""`), blankName, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
var name Name
|
||||
err := json.Unmarshal(testCase.data, &name)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(name, testCase.expectedName) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseName(t *testing.T) {
|
||||
var blankName Name
|
||||
|
||||
testCases := []struct {
|
||||
s string
|
||||
expectedName Name
|
||||
expectErr bool
|
||||
}{
|
||||
{"s3:ObjectAccessed:*", ObjectAccessedAll, false},
|
||||
{"s3:ObjectRemoved:Delete", ObjectRemovedDelete, false},
|
||||
{"", blankName, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
name, err := ParseName(testCase.s)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(name, testCase.expectedName) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/minio/pkg/wildcard"
|
||||
)
|
||||
|
||||
// NewPattern - create new pattern for prefix/suffix.
|
||||
func NewPattern(prefix, suffix string) (pattern string) {
|
||||
if prefix != "" {
|
||||
if !strings.HasSuffix(prefix, "*") {
|
||||
prefix += "*"
|
||||
}
|
||||
|
||||
pattern = prefix
|
||||
}
|
||||
|
||||
if suffix != "" {
|
||||
if !strings.HasPrefix(suffix, "*") {
|
||||
suffix = "*" + suffix
|
||||
}
|
||||
|
||||
pattern += suffix
|
||||
}
|
||||
|
||||
pattern = strings.Replace(pattern, "**", "*", -1)
|
||||
|
||||
return pattern
|
||||
}
|
||||
|
||||
// Rules - event rules
|
||||
type Rules map[string]TargetIDSet
|
||||
|
||||
// Add - adds pattern and target ID.
|
||||
func (rules Rules) Add(pattern string, targetID TargetID) {
|
||||
rules[pattern] = NewTargetIDSet(targetID).Union(rules[pattern])
|
||||
}
|
||||
|
||||
// MatchSimple - returns true one of the matching object name in rules.
|
||||
func (rules Rules) MatchSimple(objectName string) bool {
|
||||
for pattern := range rules {
|
||||
if wildcard.MatchSimple(pattern, objectName) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Match - returns TargetIDSet matching object name in rules.
|
||||
func (rules Rules) Match(objectName string) TargetIDSet {
|
||||
targetIDs := NewTargetIDSet()
|
||||
|
||||
for pattern, targetIDSet := range rules {
|
||||
if wildcard.MatchSimple(pattern, objectName) {
|
||||
targetIDs = targetIDs.Union(targetIDSet)
|
||||
}
|
||||
}
|
||||
|
||||
return targetIDs
|
||||
}
|
||||
|
||||
// Clone - returns copy of this rules.
|
||||
func (rules Rules) Clone() Rules {
|
||||
rulesCopy := make(Rules)
|
||||
|
||||
for pattern, targetIDSet := range rules {
|
||||
rulesCopy[pattern] = targetIDSet.Clone()
|
||||
}
|
||||
|
||||
return rulesCopy
|
||||
}
|
||||
|
||||
// Union - returns union with given rules as new rules.
|
||||
func (rules Rules) Union(rules2 Rules) Rules {
|
||||
nrules := rules.Clone()
|
||||
|
||||
for pattern, targetIDSet := range rules2 {
|
||||
nrules[pattern] = nrules[pattern].Union(targetIDSet)
|
||||
}
|
||||
|
||||
return nrules
|
||||
}
|
||||
|
||||
// Difference - returns diffrence with given rules as new rules.
|
||||
func (rules Rules) Difference(rules2 Rules) Rules {
|
||||
nrules := make(Rules)
|
||||
|
||||
for pattern, targetIDSet := range rules {
|
||||
if nv := targetIDSet.Difference(rules2[pattern]); len(nv) > 0 {
|
||||
nrules[pattern] = nv
|
||||
}
|
||||
}
|
||||
|
||||
return nrules
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewPattern(t *testing.T) {
|
||||
testCases := []struct {
|
||||
prefix string
|
||||
suffix string
|
||||
expectedResult string
|
||||
}{
|
||||
{"", "", ""},
|
||||
{"*", "", "*"},
|
||||
{"", "*", "*"},
|
||||
{"images/", "", "images/*"},
|
||||
{"images/*", "", "images/*"},
|
||||
{"", "jpg", "*jpg"},
|
||||
{"", "*jpg", "*jpg"},
|
||||
{"images/", "jpg", "images/*jpg"},
|
||||
{"images/*", "jpg", "images/*jpg"},
|
||||
{"images/", "*jpg", "images/*jpg"},
|
||||
{"images/*", "*jpg", "images/*jpg"},
|
||||
{"201*/images/", "jpg", "201*/images/*jpg"},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := NewPattern(testCase.prefix, testCase.suffix)
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesAdd(t *testing.T) {
|
||||
rulesCase1 := make(Rules)
|
||||
|
||||
rulesCase2 := make(Rules)
|
||||
rulesCase2.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase3 := make(Rules)
|
||||
rulesCase3.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase4 := make(Rules)
|
||||
rulesCase4.Add(NewPattern("", "*.jpg"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase5 := make(Rules)
|
||||
|
||||
rulesCase6 := make(Rules)
|
||||
rulesCase6.Add(NewPattern("", "*.jpg"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase7 := make(Rules)
|
||||
rulesCase7.Add(NewPattern("", "*.jpg"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase8 := make(Rules)
|
||||
rulesCase8.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rules Rules
|
||||
pattern string
|
||||
targetID TargetID
|
||||
expectedResult int
|
||||
}{
|
||||
{rulesCase1, NewPattern("*", ""), TargetID{"1", "webhook"}, 1},
|
||||
{rulesCase2, NewPattern("*", ""), TargetID{"2", "amqp"}, 2},
|
||||
{rulesCase3, NewPattern("2010*", ""), TargetID{"1", "webhook"}, 1},
|
||||
{rulesCase4, NewPattern("*", ""), TargetID{"1", "webhook"}, 2},
|
||||
{rulesCase5, NewPattern("", "*.jpg"), TargetID{"1", "webhook"}, 1},
|
||||
{rulesCase6, NewPattern("", "*"), TargetID{"2", "amqp"}, 2},
|
||||
{rulesCase7, NewPattern("", "*.jpg"), TargetID{"1", "webhook"}, 1},
|
||||
{rulesCase8, NewPattern("", "*.jpg"), TargetID{"1", "webhook"}, 2},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
testCase.rules.Add(testCase.pattern, testCase.targetID)
|
||||
result := len(testCase.rules)
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesMatch(t *testing.T) {
|
||||
rulesCase1 := make(Rules)
|
||||
|
||||
rulesCase2 := make(Rules)
|
||||
rulesCase2.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase3 := make(Rules)
|
||||
rulesCase3.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
rulesCase3.Add(NewPattern("", "*.png"), TargetID{"2", "amqp"})
|
||||
|
||||
rulesCase4 := make(Rules)
|
||||
rulesCase4.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rules Rules
|
||||
objectName string
|
||||
expectedResult TargetIDSet
|
||||
}{
|
||||
{rulesCase1, "photos.jpg", NewTargetIDSet()},
|
||||
{rulesCase2, "photos.jpg", NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{rulesCase3, "2010/photos.jpg", NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{rulesCase4, "2000/photos.jpg", NewTargetIDSet()},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rules.Match(testCase.objectName)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesClone(t *testing.T) {
|
||||
rulesCase1 := make(Rules)
|
||||
|
||||
rulesCase2 := make(Rules)
|
||||
rulesCase2.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase3 := make(Rules)
|
||||
rulesCase3.Add(NewPattern("", "*.jpg"), TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rules Rules
|
||||
prefix string
|
||||
targetID TargetID
|
||||
}{
|
||||
{rulesCase1, "2010*", TargetID{"1", "webhook"}},
|
||||
{rulesCase2, "2000*", TargetID{"2", "amqp"}},
|
||||
{rulesCase3, "2010*", TargetID{"1", "webhook"}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rules.Clone()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.rules) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.rules, result)
|
||||
}
|
||||
|
||||
result.Add(NewPattern(testCase.prefix, ""), testCase.targetID)
|
||||
if reflect.DeepEqual(result, testCase.rules) {
|
||||
t.Fatalf("test %v: result: expected: not equal, got: equal", i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesUnion(t *testing.T) {
|
||||
rulesCase1 := make(Rules)
|
||||
rules2Case1 := make(Rules)
|
||||
expectedResultCase1 := make(Rules)
|
||||
|
||||
rulesCase2 := make(Rules)
|
||||
rules2Case2 := make(Rules)
|
||||
rules2Case2.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
expectedResultCase2 := make(Rules)
|
||||
expectedResultCase2.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase3 := make(Rules)
|
||||
rulesCase3.Add(NewPattern("", "*"), TargetID{"1", "webhook"})
|
||||
rules2Case3 := make(Rules)
|
||||
expectedResultCase3 := make(Rules)
|
||||
expectedResultCase3.Add(NewPattern("", "*"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase4 := make(Rules)
|
||||
rulesCase4.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
rules2Case4 := make(Rules)
|
||||
rules2Case4.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
expectedResultCase4 := make(Rules)
|
||||
expectedResultCase4.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase5 := make(Rules)
|
||||
rulesCase5.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
rulesCase5.Add(NewPattern("", "*.png"), TargetID{"2", "amqp"})
|
||||
rules2Case5 := make(Rules)
|
||||
rules2Case5.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
expectedResultCase5 := make(Rules)
|
||||
expectedResultCase5.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
expectedResultCase5.Add(NewPattern("", "*.png"), TargetID{"2", "amqp"})
|
||||
expectedResultCase5.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rules Rules
|
||||
rules2 Rules
|
||||
expectedResult Rules
|
||||
}{
|
||||
{rulesCase1, rules2Case1, expectedResultCase1},
|
||||
{rulesCase2, rules2Case2, expectedResultCase2},
|
||||
{rulesCase3, rules2Case3, expectedResultCase3},
|
||||
{rulesCase4, rules2Case4, expectedResultCase4},
|
||||
{rulesCase5, rules2Case5, expectedResultCase5},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rules.Union(testCase.rules2)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesDifference(t *testing.T) {
|
||||
rulesCase1 := make(Rules)
|
||||
rules2Case1 := make(Rules)
|
||||
expectedResultCase1 := make(Rules)
|
||||
|
||||
rulesCase2 := make(Rules)
|
||||
rules2Case2 := make(Rules)
|
||||
rules2Case2.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
expectedResultCase2 := make(Rules)
|
||||
|
||||
rulesCase3 := make(Rules)
|
||||
rulesCase3.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
rules2Case3 := make(Rules)
|
||||
expectedResultCase3 := make(Rules)
|
||||
expectedResultCase3.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase4 := make(Rules)
|
||||
rulesCase4.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
rules2Case4 := make(Rules)
|
||||
rules2Case4.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
rules2Case4.Add(NewPattern("", "*.png"), TargetID{"2", "amqp"})
|
||||
expectedResultCase4 := make(Rules)
|
||||
expectedResultCase4.Add(NewPattern("*", "*"), TargetID{"1", "webhook"})
|
||||
|
||||
rulesCase5 := make(Rules)
|
||||
rulesCase5.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
rulesCase5.Add(NewPattern("", "*"), TargetID{"2", "amqp"})
|
||||
rules2Case5 := make(Rules)
|
||||
rules2Case5.Add(NewPattern("2010*", ""), TargetID{"1", "webhook"})
|
||||
rules2Case5.Add(NewPattern("", "*"), TargetID{"2", "amqp"})
|
||||
expectedResultCase5 := make(Rules)
|
||||
expectedResultCase5.Add(NewPattern("*", ""), TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rules Rules
|
||||
rules2 Rules
|
||||
expectedResult Rules
|
||||
}{
|
||||
{rulesCase1, rules2Case1, expectedResultCase1},
|
||||
{rulesCase2, rules2Case2, expectedResultCase2},
|
||||
{rulesCase3, rules2Case3, expectedResultCase3},
|
||||
{rulesCase4, rules2Case4, expectedResultCase4},
|
||||
{rulesCase5, rules2Case5, expectedResultCase5},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rules.Difference(testCase.rules2)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
// RulesMap - map of rules for every event name.
|
||||
type RulesMap map[Name]Rules
|
||||
|
||||
// add - adds event names, prefixes, suffixes and target ID to rules map.
|
||||
func (rulesMap RulesMap) add(eventNames []Name, pattern string, targetID TargetID) {
|
||||
rules := make(Rules)
|
||||
rules.Add(pattern, targetID)
|
||||
|
||||
for _, eventName := range eventNames {
|
||||
for _, name := range eventName.Expand() {
|
||||
rulesMap[name] = rulesMap[name].Union(rules)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clone - returns copy of this rules map.
|
||||
func (rulesMap RulesMap) Clone() RulesMap {
|
||||
rulesMapCopy := make(RulesMap)
|
||||
|
||||
for eventName, rules := range rulesMap {
|
||||
rulesMapCopy[eventName] = rules.Clone()
|
||||
}
|
||||
|
||||
return rulesMapCopy
|
||||
}
|
||||
|
||||
// Add - adds given rules map.
|
||||
func (rulesMap RulesMap) Add(rulesMap2 RulesMap) {
|
||||
for eventName, rules := range rulesMap2 {
|
||||
rulesMap[eventName] = rules.Union(rulesMap[eventName])
|
||||
}
|
||||
}
|
||||
|
||||
// Remove - removes given rules map.
|
||||
func (rulesMap RulesMap) Remove(rulesMap2 RulesMap) {
|
||||
for eventName, rules := range rulesMap {
|
||||
if nr := rules.Difference(rulesMap2[eventName]); len(nr) != 0 {
|
||||
rulesMap[eventName] = nr
|
||||
} else {
|
||||
delete(rulesMap, eventName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MatchSimple - returns true if matching object name and event name in rules map.
|
||||
func (rulesMap RulesMap) MatchSimple(eventName Name, objectName string) bool {
|
||||
return rulesMap[eventName].MatchSimple(objectName)
|
||||
}
|
||||
|
||||
// Match - returns TargetIDSet matching object name and event name in rules map.
|
||||
func (rulesMap RulesMap) Match(eventName Name, objectName string) TargetIDSet {
|
||||
return rulesMap[eventName].Match(objectName)
|
||||
}
|
||||
|
||||
// NewRulesMap - creates new rules map with given values.
|
||||
func NewRulesMap(eventNames []Name, pattern string, targetID TargetID) RulesMap {
|
||||
// If pattern is empty, add '*' wildcard to match all.
|
||||
if pattern == "" {
|
||||
pattern = "*"
|
||||
}
|
||||
|
||||
rulesMap := make(RulesMap)
|
||||
rulesMap.add(eventNames, pattern, targetID)
|
||||
return rulesMap
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRulesMapClone(t *testing.T) {
|
||||
rulesMapCase1 := make(RulesMap)
|
||||
rulesMapToAddCase1 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rulesMap RulesMap
|
||||
rulesMapToAdd RulesMap
|
||||
}{
|
||||
{rulesMapCase1, rulesMapToAddCase1},
|
||||
{rulesMapCase2, rulesMapToAddCase2},
|
||||
{rulesMapCase3, rulesMapToAddCase3},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rulesMap.Clone()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.rulesMap) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.rulesMap, result)
|
||||
}
|
||||
|
||||
result.Add(testCase.rulesMapToAdd)
|
||||
if reflect.DeepEqual(result, testCase.rulesMap) {
|
||||
t.Fatalf("test %v: result: expected: not equal, got: equal", i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesMapAdd(t *testing.T) {
|
||||
rulesMapCase1 := make(RulesMap)
|
||||
rulesMapToAddCase1 := make(RulesMap)
|
||||
expectedResultCase1 := make(RulesMap)
|
||||
|
||||
rulesMapCase2 := make(RulesMap)
|
||||
rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
expectedResultCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
expectedResultCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rulesMap RulesMap
|
||||
rulesMapToAdd RulesMap
|
||||
expectedResult RulesMap
|
||||
}{
|
||||
{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
|
||||
{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
|
||||
{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
testCase.rulesMap.Add(testCase.rulesMapToAdd)
|
||||
|
||||
if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesMapRemove(t *testing.T) {
|
||||
rulesMapCase1 := make(RulesMap)
|
||||
rulesMapToAddCase1 := make(RulesMap)
|
||||
expectedResultCase1 := make(RulesMap)
|
||||
|
||||
rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapToAddCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
expectedResultCase2 := make(RulesMap)
|
||||
|
||||
rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
rulesMapCase3.add([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
rulesMapToAddCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
expectedResultCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
rulesMap RulesMap
|
||||
rulesMapToAdd RulesMap
|
||||
expectedResult RulesMap
|
||||
}{
|
||||
{rulesMapCase1, rulesMapToAddCase1, expectedResultCase1},
|
||||
{rulesMapCase2, rulesMapToAddCase2, expectedResultCase2},
|
||||
{rulesMapCase3, rulesMapToAddCase3, expectedResultCase3},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
testCase.rulesMap.Remove(testCase.rulesMapToAdd)
|
||||
|
||||
if !reflect.DeepEqual(testCase.rulesMap, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.rulesMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesMapMatch(t *testing.T) {
|
||||
rulesMapCase1 := make(RulesMap)
|
||||
|
||||
rulesMapCase2 := NewRulesMap([]Name{ObjectCreatedAll}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase3 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase4 := NewRulesMap([]Name{ObjectCreatedAll}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
rulesMapCase4.add([]Name{ObjectCreatedAll}, "*", TargetID{"2", "amqp"})
|
||||
|
||||
testCases := []struct {
|
||||
rulesMap RulesMap
|
||||
eventName Name
|
||||
objectName string
|
||||
expectedResult TargetIDSet
|
||||
}{
|
||||
{rulesMapCase1, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet()},
|
||||
{rulesMapCase2, ObjectCreatedPut, "2010/photo.jpg", NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{rulesMapCase3, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet()},
|
||||
{rulesMapCase4, ObjectCreatedPut, "2000/photo.png", NewTargetIDSet(TargetID{"2", "amqp"})},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.rulesMap.Match(testCase.eventName, testCase.objectName)
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRulesMap(t *testing.T) {
|
||||
rulesMapCase1 := make(RulesMap)
|
||||
rulesMapCase1.add([]Name{ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold},
|
||||
"*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase2 := make(RulesMap)
|
||||
rulesMapCase2.add([]Name{ObjectAccessedGet, ObjectAccessedHead,
|
||||
ObjectCreatedPut, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold}, "*", TargetID{"1", "webhook"})
|
||||
|
||||
rulesMapCase3 := make(RulesMap)
|
||||
rulesMapCase3.add([]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"})
|
||||
|
||||
testCases := []struct {
|
||||
eventNames []Name
|
||||
pattern string
|
||||
targetID TargetID
|
||||
expectedResult RulesMap
|
||||
}{
|
||||
{[]Name{ObjectAccessedAll}, "", TargetID{"1", "webhook"}, rulesMapCase1},
|
||||
{[]Name{ObjectAccessedAll, ObjectCreatedPut}, "", TargetID{"1", "webhook"}, rulesMapCase2},
|
||||
{[]Name{ObjectRemovedDelete}, "2010*.jpg", TargetID{"1", "webhook"}, rulesMapCase3},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := NewRulesMap(testCase.eventNames, testCase.pattern, testCase.targetID)
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
||||
t.Errorf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
// AMQPArgs - AMQP target arguments.
|
||||
type AMQPArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
URL xnet.URL `json:"url"`
|
||||
Exchange string `json:"exchange"`
|
||||
RoutingKey string `json:"routingKey"`
|
||||
ExchangeType string `json:"exchangeType"`
|
||||
DeliveryMode uint8 `json:"deliveryMode"`
|
||||
Mandatory bool `json:"mandatory"`
|
||||
Immediate bool `json:"immediate"`
|
||||
Durable bool `json:"durable"`
|
||||
Internal bool `json:"internal"`
|
||||
NoWait bool `json:"noWait"`
|
||||
AutoDeleted bool `json:"autoDeleted"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
}
|
||||
|
||||
//lint:file-ignore ST1003 We cannot change these exported names.
|
||||
|
||||
// AMQP input constants.
|
||||
const (
|
||||
AmqpQueueDir = "queue_dir"
|
||||
AmqpQueueLimit = "queue_limit"
|
||||
|
||||
AmqpURL = "url"
|
||||
AmqpExchange = "exchange"
|
||||
AmqpRoutingKey = "routing_key"
|
||||
AmqpExchangeType = "exchange_type"
|
||||
AmqpDeliveryMode = "delivery_mode"
|
||||
AmqpMandatory = "mandatory"
|
||||
AmqpImmediate = "immediate"
|
||||
AmqpDurable = "durable"
|
||||
AmqpInternal = "internal"
|
||||
AmqpNoWait = "no_wait"
|
||||
AmqpAutoDeleted = "auto_deleted"
|
||||
AmqpArguments = "arguments"
|
||||
AmqpPublishingHeaders = "publishing_headers"
|
||||
|
||||
EnvAMQPEnable = "MINIO_NOTIFY_AMQP_ENABLE"
|
||||
EnvAMQPURL = "MINIO_NOTIFY_AMQP_URL"
|
||||
EnvAMQPExchange = "MINIO_NOTIFY_AMQP_EXCHANGE"
|
||||
EnvAMQPRoutingKey = "MINIO_NOTIFY_AMQP_ROUTING_KEY"
|
||||
EnvAMQPExchangeType = "MINIO_NOTIFY_AMQP_EXCHANGE_TYPE"
|
||||
EnvAMQPDeliveryMode = "MINIO_NOTIFY_AMQP_DELIVERY_MODE"
|
||||
EnvAMQPMandatory = "MINIO_NOTIFY_AMQP_MANDATORY"
|
||||
EnvAMQPImmediate = "MINIO_NOTIFY_AMQP_IMMEDIATE"
|
||||
EnvAMQPDurable = "MINIO_NOTIFY_AMQP_DURABLE"
|
||||
EnvAMQPInternal = "MINIO_NOTIFY_AMQP_INTERNAL"
|
||||
EnvAMQPNoWait = "MINIO_NOTIFY_AMQP_NO_WAIT"
|
||||
EnvAMQPAutoDeleted = "MINIO_NOTIFY_AMQP_AUTO_DELETED"
|
||||
EnvAMQPArguments = "MINIO_NOTIFY_AMQP_ARGUMENTS"
|
||||
EnvAMQPPublishingHeaders = "MINIO_NOTIFY_AMQP_PUBLISHING_HEADERS"
|
||||
EnvAMQPQueueDir = "MINIO_NOTIFY_AMQP_QUEUE_DIR"
|
||||
EnvAMQPQueueLimit = "MINIO_NOTIFY_AMQP_QUEUE_LIMIT"
|
||||
)
|
||||
|
||||
// Validate AMQP arguments
|
||||
func (a *AMQPArgs) Validate() error {
|
||||
if !a.Enable {
|
||||
return nil
|
||||
}
|
||||
if _, err := amqp.ParseURI(a.URL.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
if a.QueueDir != "" {
|
||||
if !filepath.IsAbs(a.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AMQPTarget - AMQP target
|
||||
type AMQPTarget struct {
|
||||
id event.TargetID
|
||||
args AMQPArgs
|
||||
conn *amqp.Connection
|
||||
connMutex sync.Mutex
|
||||
store Store
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns TargetID.
|
||||
func (target *AMQPTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *AMQPTarget) IsActive() (bool, error) {
|
||||
ch, err := target.channel()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() {
|
||||
ch.Close()
|
||||
}()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *AMQPTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
func (target *AMQPTarget) channel() (*amqp.Channel, error) {
|
||||
var err error
|
||||
var conn *amqp.Connection
|
||||
var ch *amqp.Channel
|
||||
|
||||
isAMQPClosedErr := func(err error) bool {
|
||||
if err == amqp.ErrClosed {
|
||||
return true
|
||||
}
|
||||
|
||||
if nerr, ok := err.(*net.OpError); ok {
|
||||
return (nerr.Err.Error() == "use of closed network connection")
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
target.connMutex.Lock()
|
||||
defer target.connMutex.Unlock()
|
||||
|
||||
if target.conn != nil {
|
||||
ch, err = target.conn.Channel()
|
||||
if err == nil {
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
if !isAMQPClosedErr(err) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
conn, err = amqp.Dial(target.args.URL.String())
|
||||
if err != nil {
|
||||
if IsConnRefusedErr(err) {
|
||||
return nil, errNotConnected
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ch, err = conn.Channel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
target.conn = conn
|
||||
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// send - sends an event to the AMQP.
|
||||
func (target *AMQPTarget) send(eventData event.Event, ch *amqp.Channel) error {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = ch.ExchangeDeclare(target.args.Exchange, target.args.ExchangeType, target.args.Durable,
|
||||
target.args.AutoDeleted, target.args.Internal, target.args.NoWait, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ch.Publish(target.args.Exchange, target.args.RoutingKey, target.args.Mandatory,
|
||||
target.args.Immediate, amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: target.args.DeliveryMode,
|
||||
Body: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Save - saves the events to the store which will be replayed when the amqp connection is active.
|
||||
func (target *AMQPTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
ch, err := target.channel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
cErr := ch.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
|
||||
return target.send(eventData, ch)
|
||||
}
|
||||
|
||||
// Send - sends event to AMQP.
|
||||
func (target *AMQPTarget) Send(eventKey string) error {
|
||||
ch, err := target.channel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
cErr := ch.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
|
||||
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, ch); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - does nothing and available for interface compatibility.
|
||||
func (target *AMQPTarget) Close() error {
|
||||
if target.conn != nil {
|
||||
return target.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewAMQPTarget - creates new AMQP target.
|
||||
func NewAMQPTarget(id string, args AMQPArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{}), test bool) (*AMQPTarget, error) {
|
||||
var conn *amqp.Connection
|
||||
var err error
|
||||
|
||||
var store Store
|
||||
|
||||
target := &AMQPTarget{
|
||||
id: event.TargetID{ID: id, Name: "amqp"},
|
||||
args: args,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-amqp-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
conn, err = amqp.Dial(args.URL.String())
|
||||
if err != nil {
|
||||
if store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
target.conn = conn
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
func getNewUUID() (string, error) {
|
||||
u, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,649 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
elasticsearch7 "github.com/elastic/go-elasticsearch/v7"
|
||||
"github.com/minio/highwayhash"
|
||||
"github.com/olivere/elastic/v7"
|
||||
)
|
||||
|
||||
// Elastic constants
|
||||
const (
|
||||
ElasticFormat = "format"
|
||||
ElasticURL = "url"
|
||||
ElasticIndex = "index"
|
||||
ElasticQueueDir = "queue_dir"
|
||||
ElasticQueueLimit = "queue_limit"
|
||||
ElasticUsername = "username"
|
||||
ElasticPassword = "password"
|
||||
|
||||
EnvElasticEnable = "MINIO_NOTIFY_ELASTICSEARCH_ENABLE"
|
||||
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"
|
||||
EnvElasticUsername = "MINIO_NOTIFY_ELASTICSEARCH_USERNAME"
|
||||
EnvElasticPassword = "MINIO_NOTIFY_ELASTICSEARCH_PASSWORD"
|
||||
)
|
||||
|
||||
// ESSupportStatus is a typed string representing the support status for
|
||||
// Elasticsearch
|
||||
type ESSupportStatus string
|
||||
|
||||
const (
|
||||
// ESSUnknown is default value
|
||||
ESSUnknown ESSupportStatus = "ESSUnknown"
|
||||
// ESSDeprecated -> support will be removed in future
|
||||
ESSDeprecated ESSupportStatus = "ESSDeprecated"
|
||||
// ESSUnsupported -> we wont work with this ES server
|
||||
ESSUnsupported ESSupportStatus = "ESSUnsupported"
|
||||
// ESSSupported -> all good!
|
||||
ESSSupported ESSupportStatus = "ESSSupported"
|
||||
)
|
||||
|
||||
func getESVersionSupportStatus(version string) (res ESSupportStatus, err error) {
|
||||
parts := strings.Split(version, ".")
|
||||
if len(parts) < 1 {
|
||||
err = fmt.Errorf("bad ES version string: %s", version)
|
||||
return
|
||||
}
|
||||
|
||||
majorVersion, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
err = fmt.Errorf("bad ES version string: %s", version)
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case majorVersion <= 4:
|
||||
res = ESSUnsupported
|
||||
case majorVersion <= 6:
|
||||
res = ESSDeprecated
|
||||
default:
|
||||
res = ESSSupported
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// magic HH-256 key as HH-256 hash of the first 100 decimals of π as utf-8 string with a zero key.
|
||||
var magicHighwayHash256Key = []byte("\x4b\xe7\x34\xfa\x8e\x23\x8a\xcd\x26\x3e\x83\xe6\xbb\x96\x85\x52\x04\x0f\x93\x5d\xa3\x9f\x44\x14\x97\xe0\x9d\x13\x22\xde\x36\xa0")
|
||||
|
||||
// Interface for elasticsearch client objects
|
||||
type esClient interface {
|
||||
isAtleastV7() bool
|
||||
createIndex(ElasticsearchArgs) error
|
||||
ping(context.Context, ElasticsearchArgs) (bool, error)
|
||||
stop()
|
||||
|
||||
entryExists(context.Context, string, string) (bool, error)
|
||||
removeEntry(context.Context, string, string) error
|
||||
updateEntry(context.Context, string, string, event.Event) error
|
||||
addEntry(context.Context, string, event.Event) error
|
||||
}
|
||||
|
||||
// ElasticsearchArgs - Elasticsearch target arguments.
|
||||
type ElasticsearchArgs struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
if (a.Username == "" && a.Password != "") || (a.Username != "" && a.Password == "") {
|
||||
return errors.New("username and password should be set in pairs")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ElasticsearchTarget - Elasticsearch target.
|
||||
type ElasticsearchTarget struct {
|
||||
id event.TargetID
|
||||
args ElasticsearchArgs
|
||||
client esClient
|
||||
store Store
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *ElasticsearchTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *ElasticsearchTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *ElasticsearchTarget) IsActive() (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := target.checkAndInitClient(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return target.client.ping(ctx, target.args)
|
||||
}
|
||||
|
||||
// Save - saves the events to the store if queuestore is configured, which will be replayed when the elasticsearch connection is active.
|
||||
func (target *ElasticsearchTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
err := target.send(eventData)
|
||||
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err, false) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// send - sends the event to the target.
|
||||
func (target *ElasticsearchTarget) send(eventData event.Event) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if target.args.Format == event.NamespaceFormat {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Calculate a hash of the key for the id of the ES document.
|
||||
// Id's are limited to 512 bytes in V7+, so we need to do this.
|
||||
var keyHash string
|
||||
{
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
if target.client.isAtleastV7() {
|
||||
hh, _ := highwayhash.New(magicHighwayHash256Key) // New will never return error since key is 256 bit
|
||||
hh.Write([]byte(key))
|
||||
hashBytes := hh.Sum(nil)
|
||||
keyHash = base64.URLEncoding.EncodeToString(hashBytes)
|
||||
} else {
|
||||
keyHash = key
|
||||
}
|
||||
}
|
||||
|
||||
if eventData.EventName == event.ObjectRemovedDelete {
|
||||
err = target.client.removeEntry(ctx, target.args.Index, keyHash)
|
||||
} else {
|
||||
err = target.client.updateEntry(ctx, target.args.Index, keyHash, eventData)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if target.args.Format == event.AccessFormat {
|
||||
return target.client.addEntry(ctx, target.args.Index, eventData)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to Elasticsearch.
|
||||
func (target *ElasticsearchTarget) Send(eventKey string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := target.checkAndInitClient(ctx)
|
||||
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 {
|
||||
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err, false) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - does nothing and available for interface compatibility.
|
||||
func (target *ElasticsearchTarget) Close() error {
|
||||
if target.client != nil {
|
||||
// Stops the background processes that the client is running.
|
||||
target.client.stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (target *ElasticsearchTarget) checkAndInitClient(ctx context.Context) error {
|
||||
if target.client != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
clientV7, err := newClientV7(target.args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check es version to confirm if it is supported.
|
||||
serverSupportStatus, version, err := clientV7.getServerSupportStatus(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch serverSupportStatus {
|
||||
case ESSUnknown:
|
||||
return errors.New("unable to determine support status of ES (should not happen)")
|
||||
|
||||
case ESSDeprecated:
|
||||
fmt.Printf("DEPRECATION WARNING: Support for Elasticsearch version '%s' will be dropped in a future release. Please upgrade to a version >= 7.x.", version)
|
||||
target.client, err = newClientV56(target.args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case ESSSupported:
|
||||
target.client = clientV7
|
||||
|
||||
default:
|
||||
// ESSUnsupported case
|
||||
return fmt.Errorf("Elasticsearch version '%s' is not supported! Please use at least version 7.x.", version)
|
||||
}
|
||||
|
||||
target.client.createIndex(target.args)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewElasticsearchTarget - creates new Elasticsearch target.
|
||||
func NewElasticsearchTarget(id string, args ElasticsearchArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*ElasticsearchTarget, error) {
|
||||
target := &ElasticsearchTarget{
|
||||
id: event.TargetID{ID: id, Name: "elasticsearch"},
|
||||
args: args,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-elasticsearch-"+id)
|
||||
target.store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if err := target.store.Open(); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := target.checkAndInitClient(ctx)
|
||||
if err != nil {
|
||||
if target.store == nil || err != errNotConnected {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
|
||||
// ES Client definitions and methods
|
||||
|
||||
type esClientV7 struct {
|
||||
*elasticsearch7.Client
|
||||
}
|
||||
|
||||
func newClientV7(args ElasticsearchArgs) (*esClientV7, error) {
|
||||
// Client options
|
||||
elasticConfig := elasticsearch7.Config{
|
||||
Addresses: []string{args.URL.String()},
|
||||
Transport: args.Transport,
|
||||
MaxRetries: 10,
|
||||
}
|
||||
// Set basic auth
|
||||
if args.Username != "" && args.Password != "" {
|
||||
elasticConfig.Username = args.Username
|
||||
elasticConfig.Password = args.Password
|
||||
}
|
||||
// Create a client
|
||||
client, err := elasticsearch7.NewClient(elasticConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clientV7 := &esClientV7{client}
|
||||
return clientV7, nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) getServerSupportStatus(ctx context.Context) (ESSupportStatus, string, error) {
|
||||
resp, err := c.Info(
|
||||
c.Info.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return ESSUnknown, "", errNotConnected
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
m := make(map[string]interface{})
|
||||
err = json.NewDecoder(resp.Body).Decode(&m)
|
||||
if err != nil {
|
||||
return ESSUnknown, "", fmt.Errorf("unable to get ES Server version - json parse error: %v", err)
|
||||
}
|
||||
|
||||
if v, ok := m["version"].(map[string]interface{}); ok {
|
||||
if ver, ok := v["number"].(string); ok {
|
||||
status, err := getESVersionSupportStatus(ver)
|
||||
return status, ver, err
|
||||
}
|
||||
}
|
||||
return ESSUnknown, "", fmt.Errorf("Unable to get ES Server Version - got INFO response: %v", m)
|
||||
|
||||
}
|
||||
|
||||
func (c *esClientV7) isAtleastV7() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// createIndex - creates the index if it does not exist.
|
||||
func (c *esClientV7) createIndex(args ElasticsearchArgs) error {
|
||||
res, err := c.Indices.ResolveIndex([]string{args.Index})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var v map[string]interface{}
|
||||
found := false
|
||||
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
|
||||
return fmt.Errorf("Error parsing response body: %v", err)
|
||||
}
|
||||
|
||||
indices := v["indices"].([]interface{})
|
||||
for _, index := range indices {
|
||||
name := index.(map[string]interface{})["name"]
|
||||
if name == args.Index {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
resp, err := c.Indices.Create(args.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.IsError() {
|
||||
err := fmt.Errorf("Create index err: %s", res.String())
|
||||
return err
|
||||
}
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) ping(ctx context.Context, _ ElasticsearchArgs) (bool, error) {
|
||||
resp, err := c.Ping(
|
||||
c.Ping.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return false, errNotConnected
|
||||
}
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
return !resp.IsError(), nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) entryExists(ctx context.Context, index string, key string) (bool, error) {
|
||||
res, err := c.Exists(
|
||||
index,
|
||||
key,
|
||||
c.Exists.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
io.Copy(ioutil.Discard, res.Body)
|
||||
res.Body.Close()
|
||||
return !res.IsError(), nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) removeEntry(ctx context.Context, index string, key string) error {
|
||||
exists, err := c.entryExists(ctx, index, key)
|
||||
if err == nil && exists {
|
||||
res, err := c.Delete(
|
||||
index,
|
||||
key,
|
||||
c.Delete.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.IsError() {
|
||||
err := fmt.Errorf("Delete err: %s", res.String())
|
||||
return err
|
||||
}
|
||||
io.Copy(ioutil.Discard, res.Body)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *esClientV7) updateEntry(ctx context.Context, index string, key string, eventData event.Event) error {
|
||||
doc := map[string]interface{}{
|
||||
"Records": []event.Event{eventData},
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
err := enc.Encode(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := c.Index(
|
||||
index,
|
||||
&buf,
|
||||
c.Index.WithDocumentID(key),
|
||||
c.Index.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.IsError() {
|
||||
err := fmt.Errorf("Update err: %s", res.String())
|
||||
return err
|
||||
}
|
||||
io.Copy(ioutil.Discard, res.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) addEntry(ctx context.Context, index string, eventData event.Event) error {
|
||||
doc := map[string]interface{}{
|
||||
"Records": []event.Event{eventData},
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
err := enc.Encode(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := c.Index(
|
||||
index,
|
||||
&buf,
|
||||
c.Index.WithContext(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.IsError() {
|
||||
err := fmt.Errorf("Add err: %s", res.String())
|
||||
return err
|
||||
}
|
||||
io.Copy(ioutil.Discard, res.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *esClientV7) stop() {
|
||||
}
|
||||
|
||||
// For versions under 7
|
||||
type esClientV56 struct {
|
||||
*elastic.Client
|
||||
}
|
||||
|
||||
func newClientV56(args ElasticsearchArgs) (*esClientV56, error) {
|
||||
// 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...)
|
||||
if err != nil {
|
||||
// https://github.com/olivere/elastic/wiki/Connection-Errors
|
||||
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err, false) {
|
||||
return nil, errNotConnected
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &esClientV56{client}, nil
|
||||
}
|
||||
|
||||
func (c *esClientV56) isAtleastV7() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// createIndex - creates the index if it does not exist.
|
||||
func (c *esClientV56) createIndex(args ElasticsearchArgs) error {
|
||||
exists, err := c.IndexExists(args.Index).Do(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
var createIndex *elastic.IndicesCreateResult
|
||||
if createIndex, err = c.CreateIndex(args.Index).Do(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !createIndex.Acknowledged {
|
||||
return fmt.Errorf("index %v not created", args.Index)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *esClientV56) ping(ctx context.Context, args ElasticsearchArgs) (bool, error) {
|
||||
_, code, err := c.Ping(args.URL.String()).HttpHeadOnly(true).Do(ctx)
|
||||
if err != nil {
|
||||
if elastic.IsConnErr(err) || elastic.IsContextErr(err) || xnet.IsNetworkOrHostDown(err, false) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return !(code >= http.StatusBadRequest), nil
|
||||
|
||||
}
|
||||
|
||||
func (c *esClientV56) entryExists(ctx context.Context, index string, key string) (bool, error) {
|
||||
return c.Exists().Index(index).Type("event").Id(key).Do(ctx)
|
||||
}
|
||||
|
||||
func (c *esClientV56) removeEntry(ctx context.Context, index string, key string) error {
|
||||
exists, err := c.entryExists(ctx, index, key)
|
||||
if err == nil && exists {
|
||||
_, err = c.Delete().Index(index).Type("event").Id(key).Do(ctx)
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (c *esClientV56) updateEntry(ctx context.Context, index string, key string, eventData event.Event) error {
|
||||
_, err := c.Index().Index(index).Type("event").BodyJson(map[string]interface{}{"Records": []event.Event{eventData}}).Id(key).Do(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *esClientV56) addEntry(ctx context.Context, index string, eventData event.Event) error {
|
||||
_, err := c.Index().Index(index).Type("event").BodyJson(map[string]interface{}{"Records": []event.Event{eventData}}).Do(ctx)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (c *esClientV56) stop() {
|
||||
c.Stop()
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
|
||||
sarama "github.com/Shopify/sarama"
|
||||
saramatls "github.com/Shopify/sarama/tools/tls"
|
||||
)
|
||||
|
||||
// Kafka input constants
|
||||
const (
|
||||
KafkaBrokers = "brokers"
|
||||
KafkaTopic = "topic"
|
||||
KafkaQueueDir = "queue_dir"
|
||||
KafkaQueueLimit = "queue_limit"
|
||||
KafkaTLS = "tls"
|
||||
KafkaTLSSkipVerify = "tls_skip_verify"
|
||||
KafkaTLSClientAuth = "tls_client_auth"
|
||||
KafkaSASL = "sasl"
|
||||
KafkaSASLUsername = "sasl_username"
|
||||
KafkaSASLPassword = "sasl_password"
|
||||
KafkaSASLMechanism = "sasl_mechanism"
|
||||
KafkaClientTLSCert = "client_tls_cert"
|
||||
KafkaClientTLSKey = "client_tls_key"
|
||||
KafkaVersion = "version"
|
||||
|
||||
EnvKafkaEnable = "MINIO_NOTIFY_KAFKA_ENABLE"
|
||||
EnvKafkaBrokers = "MINIO_NOTIFY_KAFKA_BROKERS"
|
||||
EnvKafkaTopic = "MINIO_NOTIFY_KAFKA_TOPIC"
|
||||
EnvKafkaQueueDir = "MINIO_NOTIFY_KAFKA_QUEUE_DIR"
|
||||
EnvKafkaQueueLimit = "MINIO_NOTIFY_KAFKA_QUEUE_LIMIT"
|
||||
EnvKafkaTLS = "MINIO_NOTIFY_KAFKA_TLS"
|
||||
EnvKafkaTLSSkipVerify = "MINIO_NOTIFY_KAFKA_TLS_SKIP_VERIFY"
|
||||
EnvKafkaTLSClientAuth = "MINIO_NOTIFY_KAFKA_TLS_CLIENT_AUTH"
|
||||
EnvKafkaSASLEnable = "MINIO_NOTIFY_KAFKA_SASL"
|
||||
EnvKafkaSASLUsername = "MINIO_NOTIFY_KAFKA_SASL_USERNAME"
|
||||
EnvKafkaSASLPassword = "MINIO_NOTIFY_KAFKA_SASL_PASSWORD"
|
||||
EnvKafkaSASLMechanism = "MINIO_NOTIFY_KAFKA_SASL_MECHANISM"
|
||||
EnvKafkaClientTLSCert = "MINIO_NOTIFY_KAFKA_CLIENT_TLS_CERT"
|
||||
EnvKafkaClientTLSKey = "MINIO_NOTIFY_KAFKA_CLIENT_TLS_KEY"
|
||||
EnvKafkaVersion = "MINIO_NOTIFY_KAFKA_VERSION"
|
||||
)
|
||||
|
||||
// KafkaArgs - Kafka target arguments.
|
||||
type KafkaArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Brokers []xnet.Host `json:"brokers"`
|
||||
Topic string `json:"topic"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
Version string `json:"version"`
|
||||
TLS struct {
|
||||
Enable bool `json:"enable"`
|
||||
RootCAs *x509.CertPool `json:"-"`
|
||||
SkipVerify bool `json:"skipVerify"`
|
||||
ClientAuth tls.ClientAuthType `json:"clientAuth"`
|
||||
ClientTLSCert string `json:"clientTLSCert"`
|
||||
ClientTLSKey string `json:"clientTLSKey"`
|
||||
} `json:"tls"`
|
||||
SASL struct {
|
||||
Enable bool `json:"enable"`
|
||||
User string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Mechanism string `json:"mechanism"`
|
||||
} `json:"sasl"`
|
||||
}
|
||||
|
||||
// Validate KafkaArgs fields
|
||||
func (k KafkaArgs) Validate() error {
|
||||
if !k.Enable {
|
||||
return nil
|
||||
}
|
||||
if len(k.Brokers) == 0 {
|
||||
return errors.New("no broker address found")
|
||||
}
|
||||
for _, b := range k.Brokers {
|
||||
if _, err := xnet.ParseHost(b.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if k.QueueDir != "" {
|
||||
if !filepath.IsAbs(k.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
if k.Version != "" {
|
||||
if _, err := sarama.ParseKafkaVersion(k.Version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// KafkaTarget - Kafka target.
|
||||
type KafkaTarget struct {
|
||||
id event.TargetID
|
||||
args KafkaArgs
|
||||
producer sarama.SyncProducer
|
||||
config *sarama.Config
|
||||
store Store
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *KafkaTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *KafkaTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *KafkaTarget) IsActive() (bool, error) {
|
||||
if !target.args.pingBrokers() {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store which will be replayed when the Kafka connection is active.
|
||||
func (target *KafkaTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// send - sends an event to the kafka.
|
||||
func (target *KafkaTarget) send(eventData event.Event) error {
|
||||
if target.producer == nil {
|
||||
return errNotConnected
|
||||
}
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := sarama.ProducerMessage{
|
||||
Topic: target.args.Topic,
|
||||
Key: sarama.StringEncoder(key),
|
||||
Value: sarama.ByteEncoder(data),
|
||||
}
|
||||
|
||||
_, _, err = target.producer.SendMessage(&msg)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to Kafka.
|
||||
func (target *KafkaTarget) Send(eventKey string) error {
|
||||
var err error
|
||||
_, err = target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if target.producer == nil {
|
||||
brokers := []string{}
|
||||
for _, broker := range target.args.Brokers {
|
||||
brokers = append(brokers, broker.String())
|
||||
}
|
||||
target.producer, err = sarama.NewSyncProducer(brokers, target.config)
|
||||
if err != nil {
|
||||
if err != sarama.ErrOutOfBrokers {
|
||||
return err
|
||||
}
|
||||
return errNotConnected
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
err = target.send(eventData)
|
||||
if err != nil {
|
||||
// Sarama opens the ciruit breaker after 3 consecutive connection failures.
|
||||
if err == sarama.ErrLeaderNotAvailable || err.Error() == "circuit breaker is open" {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - closes underneath kafka connection.
|
||||
func (target *KafkaTarget) Close() error {
|
||||
if target.producer != nil {
|
||||
return target.producer.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if atleast one broker in cluster is active
|
||||
func (k KafkaArgs) pingBrokers() bool {
|
||||
|
||||
for _, broker := range k.Brokers {
|
||||
_, dErr := net.Dial("tcp", broker.String())
|
||||
if dErr == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NewKafkaTarget - creates new Kafka target with auth credentials.
|
||||
func NewKafkaTarget(id string, args KafkaArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*KafkaTarget, error) {
|
||||
config := sarama.NewConfig()
|
||||
|
||||
target := &KafkaTarget{
|
||||
id: event.TargetID{ID: id, Name: "kafka"},
|
||||
args: args,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.Version != "" {
|
||||
kafkaVersion, err := sarama.ParseKafkaVersion(args.Version)
|
||||
if err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
config.Version = kafkaVersion
|
||||
}
|
||||
|
||||
config.Net.SASL.User = args.SASL.User
|
||||
config.Net.SASL.Password = args.SASL.Password
|
||||
initScramClient(args, config) // initializes configured scram client.
|
||||
config.Net.SASL.Enable = args.SASL.Enable
|
||||
|
||||
tlsConfig, err := saramatls.NewConfig(args.TLS.ClientTLSCert, args.TLS.ClientTLSKey)
|
||||
|
||||
if err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
|
||||
config.Net.TLS.Enable = args.TLS.Enable
|
||||
config.Net.TLS.Config = tlsConfig
|
||||
config.Net.TLS.Config.InsecureSkipVerify = args.TLS.SkipVerify
|
||||
config.Net.TLS.Config.ClientAuth = args.TLS.ClientAuth
|
||||
config.Net.TLS.Config.RootCAs = args.TLS.RootCAs
|
||||
|
||||
config.Producer.RequiredAcks = sarama.WaitForAll
|
||||
config.Producer.Retry.Max = 10
|
||||
config.Producer.Return.Successes = true
|
||||
|
||||
target.config = config
|
||||
|
||||
brokers := []string{}
|
||||
for _, broker := range args.Brokers {
|
||||
brokers = append(brokers, broker.String())
|
||||
}
|
||||
|
||||
var store Store
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-kafka-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
producer, err := sarama.NewSyncProducer(brokers, config)
|
||||
if err != nil {
|
||||
if store == nil || err != sarama.ErrOutOfBrokers {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
target.producer = producer
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* MinIO Object Storage (c) 2021 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 target
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
|
||||
"github.com/Shopify/sarama"
|
||||
"github.com/xdg/scram"
|
||||
)
|
||||
|
||||
func initScramClient(args KafkaArgs, config *sarama.Config) {
|
||||
if args.SASL.Mechanism == "sha512" {
|
||||
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: KafkaSHA512} }
|
||||
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA512)
|
||||
} else if args.SASL.Mechanism == "sha256" {
|
||||
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: KafkaSHA256} }
|
||||
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256)
|
||||
} else {
|
||||
// default to PLAIN
|
||||
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypePlaintext)
|
||||
}
|
||||
}
|
||||
|
||||
// KafkaSHA256 is a function that returns a crypto/sha256 hasher and should be used
|
||||
// to create Client objects configured for SHA-256 hashing.
|
||||
var KafkaSHA256 scram.HashGeneratorFcn = sha256.New
|
||||
|
||||
// KafkaSHA512 is a function that returns a crypto/sha512 hasher and should be used
|
||||
// to create Client objects configured for SHA-512 hashing.
|
||||
var KafkaSHA512 scram.HashGeneratorFcn = sha512.New
|
||||
|
||||
// XDGSCRAMClient implements the client-side of an authentication
|
||||
// conversation with a server. A new conversation must be created for
|
||||
// each authentication attempt.
|
||||
type XDGSCRAMClient struct {
|
||||
*scram.Client
|
||||
*scram.ClientConversation
|
||||
scram.HashGeneratorFcn
|
||||
}
|
||||
|
||||
// Begin constructs a SCRAM client component based on a given hash.Hash
|
||||
// factory receiver. This constructor will normalize the username, password
|
||||
// and authzID via the SASLprep algorithm, as recommended by RFC-5802. If
|
||||
// SASLprep fails, the method returns an error.
|
||||
func (x *XDGSCRAMClient) Begin(userName, password, authzID string) (err error) {
|
||||
x.Client, err = x.HashGeneratorFcn.NewClient(userName, password, authzID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
x.ClientConversation = x.Client.NewConversation()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Step takes a string provided from a server (or just an empty string for the
|
||||
// very first conversation step) and attempts to move the authentication
|
||||
// conversation forward. It returns a string to be sent to the server or an
|
||||
// error if the server message is invalid. Calling Step after a conversation
|
||||
// completes is also an error.
|
||||
func (x *XDGSCRAMClient) Step(challenge string) (response string, err error) {
|
||||
response, err = x.ClientConversation.Step(challenge)
|
||||
return
|
||||
}
|
||||
|
||||
// Done returns true if the conversation is completed or has errored.
|
||||
func (x *XDGSCRAMClient) Done() bool {
|
||||
return x.ClientConversation.Done()
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
const (
|
||||
reconnectInterval = 5 // In Seconds
|
||||
storePrefix = "minio"
|
||||
)
|
||||
|
||||
// MQTT input constants
|
||||
const (
|
||||
MqttBroker = "broker"
|
||||
MqttTopic = "topic"
|
||||
MqttQoS = "qos"
|
||||
MqttUsername = "username"
|
||||
MqttPassword = "password"
|
||||
MqttReconnectInterval = "reconnect_interval"
|
||||
MqttKeepAliveInterval = "keep_alive_interval"
|
||||
MqttQueueDir = "queue_dir"
|
||||
MqttQueueLimit = "queue_limit"
|
||||
|
||||
EnvMQTTEnable = "MINIO_NOTIFY_MQTT_ENABLE"
|
||||
EnvMQTTBroker = "MINIO_NOTIFY_MQTT_BROKER"
|
||||
EnvMQTTTopic = "MINIO_NOTIFY_MQTT_TOPIC"
|
||||
EnvMQTTQoS = "MINIO_NOTIFY_MQTT_QOS"
|
||||
EnvMQTTUsername = "MINIO_NOTIFY_MQTT_USERNAME"
|
||||
EnvMQTTPassword = "MINIO_NOTIFY_MQTT_PASSWORD"
|
||||
EnvMQTTReconnectInterval = "MINIO_NOTIFY_MQTT_RECONNECT_INTERVAL"
|
||||
EnvMQTTKeepAliveInterval = "MINIO_NOTIFY_MQTT_KEEP_ALIVE_INTERVAL"
|
||||
EnvMQTTQueueDir = "MINIO_NOTIFY_MQTT_QUEUE_DIR"
|
||||
EnvMQTTQueueLimit = "MINIO_NOTIFY_MQTT_QUEUE_LIMIT"
|
||||
)
|
||||
|
||||
// MQTTArgs - MQTT target arguments.
|
||||
type MQTTArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Broker xnet.URL `json:"broker"`
|
||||
Topic string `json:"topic"`
|
||||
QoS byte `json:"qos"`
|
||||
User string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
MaxReconnectInterval time.Duration `json:"reconnectInterval"`
|
||||
KeepAlive time.Duration `json:"keepAliveInterval"`
|
||||
RootCAs *x509.CertPool `json:"-"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
}
|
||||
|
||||
// Validate MQTTArgs fields
|
||||
func (m MQTTArgs) Validate() error {
|
||||
if !m.Enable {
|
||||
return nil
|
||||
}
|
||||
u, err := xnet.ParseURL(m.Broker.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "ws", "wss", "tcp", "ssl", "tls", "tcps":
|
||||
default:
|
||||
return errors.New("unknown protocol in broker address")
|
||||
}
|
||||
if m.QueueDir != "" {
|
||||
if !filepath.IsAbs(m.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
if m.QoS == 0 {
|
||||
return errors.New("qos should be set to 1 or 2 if queueDir is set")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MQTTTarget - MQTT target.
|
||||
type MQTTTarget struct {
|
||||
id event.TargetID
|
||||
args MQTTArgs
|
||||
client mqtt.Client
|
||||
store Store
|
||||
quitCh chan struct{}
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *MQTTTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *MQTTTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *MQTTTarget) IsActive() (bool, error) {
|
||||
if !target.client.IsConnectionOpen() {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// send - sends an event to the mqtt.
|
||||
func (target *MQTTTarget) send(eventData event.Event) error {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
token := target.client.Publish(target.args.Topic, target.args.QoS, false, string(data))
|
||||
if !token.WaitTimeout(reconnectInterval * time.Second) {
|
||||
return errNotConnected
|
||||
}
|
||||
return token.Error()
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to MQTT.
|
||||
func (target *MQTTTarget) Send(eventKey string) error {
|
||||
// Do not send if the connection is not active.
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventData, err := target.store.Get(eventKey)
|
||||
if err != 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(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err = target.send(eventData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Save - saves the events to the store if queuestore is configured, which will
|
||||
// be replayed when the mqtt connection is active.
|
||||
func (target *MQTTTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
|
||||
// Do not send if the connection is not active.
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// Close - does nothing and available for interface compatibility.
|
||||
func (target *MQTTTarget) Close() error {
|
||||
target.client.Disconnect(100)
|
||||
close(target.quitCh)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewMQTTTarget - creates new MQTT target.
|
||||
func NewMQTTTarget(id string, args MQTTArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*MQTTTarget, error) {
|
||||
if args.MaxReconnectInterval == 0 {
|
||||
// Default interval
|
||||
// https://github.com/eclipse/paho.mqtt.golang/blob/master/options.go#L115
|
||||
args.MaxReconnectInterval = 10 * time.Minute
|
||||
}
|
||||
|
||||
options := mqtt.NewClientOptions().
|
||||
SetClientID("").
|
||||
SetCleanSession(true).
|
||||
SetUsername(args.User).
|
||||
SetPassword(args.Password).
|
||||
SetMaxReconnectInterval(args.MaxReconnectInterval).
|
||||
SetKeepAlive(args.KeepAlive).
|
||||
SetTLSConfig(&tls.Config{RootCAs: args.RootCAs}).
|
||||
AddBroker(args.Broker.String())
|
||||
|
||||
client := mqtt.NewClient(options)
|
||||
|
||||
target := &MQTTTarget{
|
||||
id: event.TargetID{ID: id, Name: "mqtt"},
|
||||
args: args,
|
||||
client: client,
|
||||
quitCh: make(chan struct{}),
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
token := client.Connect()
|
||||
retryRegister := func() {
|
||||
for {
|
||||
retry:
|
||||
select {
|
||||
case <-doneCh:
|
||||
return
|
||||
case <-target.quitCh:
|
||||
return
|
||||
default:
|
||||
ok := token.WaitTimeout(reconnectInterval * time.Second)
|
||||
if ok && token.Error() != nil {
|
||||
target.loggerOnce(context.Background(),
|
||||
fmt.Errorf("Previous connect failed with %w attempting a reconnect",
|
||||
token.Error()),
|
||||
target.ID())
|
||||
time.Sleep(reconnectInterval * time.Second)
|
||||
token = client.Connect()
|
||||
goto retry
|
||||
}
|
||||
if ok {
|
||||
// Successfully connected.
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-mqtt-"+id)
|
||||
target.store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if err := target.store.Open(); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
|
||||
if !test {
|
||||
go retryRegister()
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
} else {
|
||||
if token.Wait() && token.Error() != nil {
|
||||
return target, token.Error()
|
||||
}
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
const (
|
||||
mysqlTableExists = `SELECT 1 FROM %s;`
|
||||
mysqlCreateNamespaceTable = `CREATE TABLE %s (key_name VARCHAR(2048), value JSON, PRIMARY KEY (key_name));`
|
||||
mysqlCreateAccessTable = `CREATE TABLE %s (event_time DATETIME NOT NULL, event_data JSON);`
|
||||
|
||||
mysqlUpdateRow = `INSERT INTO %s (key_name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value);`
|
||||
mysqlDeleteRow = `DELETE FROM %s WHERE key_name = ?;`
|
||||
mysqlInsertRow = `INSERT INTO %s (event_time, event_data) VALUES (?, ?);`
|
||||
)
|
||||
|
||||
// MySQL related constants
|
||||
const (
|
||||
MySQLFormat = "format"
|
||||
MySQLDSNString = "dsn_string"
|
||||
MySQLTable = "table"
|
||||
MySQLHost = "host"
|
||||
MySQLPort = "port"
|
||||
MySQLUsername = "username"
|
||||
MySQLPassword = "password"
|
||||
MySQLDatabase = "database"
|
||||
MySQLQueueLimit = "queue_limit"
|
||||
MySQLQueueDir = "queue_dir"
|
||||
MySQLMaxOpenConnections = "max_open_connections"
|
||||
|
||||
EnvMySQLEnable = "MINIO_NOTIFY_MYSQL_ENABLE"
|
||||
EnvMySQLFormat = "MINIO_NOTIFY_MYSQL_FORMAT"
|
||||
EnvMySQLDSNString = "MINIO_NOTIFY_MYSQL_DSN_STRING"
|
||||
EnvMySQLTable = "MINIO_NOTIFY_MYSQL_TABLE"
|
||||
EnvMySQLHost = "MINIO_NOTIFY_MYSQL_HOST"
|
||||
EnvMySQLPort = "MINIO_NOTIFY_MYSQL_PORT"
|
||||
EnvMySQLUsername = "MINIO_NOTIFY_MYSQL_USERNAME"
|
||||
EnvMySQLPassword = "MINIO_NOTIFY_MYSQL_PASSWORD"
|
||||
EnvMySQLDatabase = "MINIO_NOTIFY_MYSQL_DATABASE"
|
||||
EnvMySQLQueueLimit = "MINIO_NOTIFY_MYSQL_QUEUE_LIMIT"
|
||||
EnvMySQLQueueDir = "MINIO_NOTIFY_MYSQL_QUEUE_DIR"
|
||||
EnvMySQLMaxOpenConnections = "MINIO_NOTIFY_MYSQL_MAX_OPEN_CONNECTIONS"
|
||||
)
|
||||
|
||||
// MySQLArgs - MySQL target arguments.
|
||||
type MySQLArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Format string `json:"format"`
|
||||
DSN string `json:"dsnString"`
|
||||
Table string `json:"table"`
|
||||
Host xnet.URL `json:"host"`
|
||||
Port string `json:"port"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
MaxOpenConnections int `json:"maxOpenConnections"`
|
||||
}
|
||||
|
||||
// Validate MySQLArgs fields
|
||||
func (m MySQLArgs) Validate() error {
|
||||
if !m.Enable {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m.Format != "" {
|
||||
f := strings.ToLower(m.Format)
|
||||
if f != event.NamespaceFormat && f != event.AccessFormat {
|
||||
return fmt.Errorf("unrecognized format")
|
||||
}
|
||||
}
|
||||
|
||||
if m.Table == "" {
|
||||
return fmt.Errorf("table unspecified")
|
||||
}
|
||||
|
||||
if m.DSN != "" {
|
||||
if _, err := mysql.ParseDSN(m.DSN); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Some fields need to be specified when DSN is unspecified
|
||||
if m.Port == "" {
|
||||
return fmt.Errorf("unspecified port")
|
||||
}
|
||||
if _, err := strconv.Atoi(m.Port); err != nil {
|
||||
return fmt.Errorf("invalid port")
|
||||
}
|
||||
if m.Database == "" {
|
||||
return fmt.Errorf("database unspecified")
|
||||
}
|
||||
}
|
||||
|
||||
if m.QueueDir != "" {
|
||||
if !filepath.IsAbs(m.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
if m.MaxOpenConnections < 0 {
|
||||
return errors.New("maxOpenConnections cannot be less than zero")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MySQLTarget - MySQL target.
|
||||
type MySQLTarget struct {
|
||||
id event.TargetID
|
||||
args MySQLArgs
|
||||
updateStmt *sql.Stmt
|
||||
deleteStmt *sql.Stmt
|
||||
insertStmt *sql.Stmt
|
||||
db *sql.DB
|
||||
store Store
|
||||
firstPing bool
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *MySQLTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *MySQLTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *MySQLTarget) IsActive() (bool, error) {
|
||||
if target.db == nil {
|
||||
db, sErr := sql.Open("mysql", target.args.DSN)
|
||||
if sErr != nil {
|
||||
return false, sErr
|
||||
}
|
||||
target.db = db
|
||||
if target.args.MaxOpenConnections > 0 {
|
||||
// Set the maximum connections limit
|
||||
target.db.SetMaxOpenConns(target.args.MaxOpenConnections)
|
||||
}
|
||||
}
|
||||
if err := target.db.Ping(); err != nil {
|
||||
if IsConnErr(err) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store which will be replayed when the SQL connection is active.
|
||||
func (target *MySQLTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// send - sends an event to the mysql.
|
||||
func (target *MySQLTarget) send(eventData event.Event) error {
|
||||
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 = target.deleteStmt.Exec(key)
|
||||
} else {
|
||||
var data []byte
|
||||
if data, err = json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = target.updateStmt.Exec(key, data)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if target.args.Format == event.AccessFormat {
|
||||
eventTime, err := time.Parse(event.AMZTimeFormat, eventData.EventTime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = target.insertStmt.Exec(eventTime, data)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to MySQL.
|
||||
func (target *MySQLTarget) Send(eventKey string) error {
|
||||
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !target.firstPing {
|
||||
if err := target.executeStmts(); err != nil {
|
||||
if IsConnErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
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 {
|
||||
if IsConnErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - closes underneath connections to MySQL database.
|
||||
func (target *MySQLTarget) Close() error {
|
||||
if target.updateStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.updateStmt.Close()
|
||||
}
|
||||
|
||||
if target.deleteStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.deleteStmt.Close()
|
||||
}
|
||||
|
||||
if target.insertStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.insertStmt.Close()
|
||||
}
|
||||
|
||||
return target.db.Close()
|
||||
}
|
||||
|
||||
// Executes the table creation statements.
|
||||
func (target *MySQLTarget) executeStmts() error {
|
||||
|
||||
_, err := target.db.Exec(fmt.Sprintf(mysqlTableExists, target.args.Table))
|
||||
if err != nil {
|
||||
createStmt := mysqlCreateNamespaceTable
|
||||
if target.args.Format == event.AccessFormat {
|
||||
createStmt = mysqlCreateAccessTable
|
||||
}
|
||||
|
||||
if _, dbErr := target.db.Exec(fmt.Sprintf(createStmt, target.args.Table)); dbErr != nil {
|
||||
return dbErr
|
||||
}
|
||||
}
|
||||
|
||||
switch target.args.Format {
|
||||
case event.NamespaceFormat:
|
||||
// insert or update statement
|
||||
if target.updateStmt, err = target.db.Prepare(fmt.Sprintf(mysqlUpdateRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
// delete statement
|
||||
if target.deleteStmt, err = target.db.Prepare(fmt.Sprintf(mysqlDeleteRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
case event.AccessFormat:
|
||||
// insert statement
|
||||
if target.insertStmt, err = target.db.Prepare(fmt.Sprintf(mysqlInsertRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// NewMySQLTarget - creates new MySQL target.
|
||||
func NewMySQLTarget(id string, args MySQLArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*MySQLTarget, error) {
|
||||
if args.DSN == "" {
|
||||
config := mysql.Config{
|
||||
User: args.User,
|
||||
Passwd: args.Password,
|
||||
Net: "tcp",
|
||||
Addr: args.Host.String() + ":" + args.Port,
|
||||
DBName: args.Database,
|
||||
AllowNativePasswords: true,
|
||||
CheckConnLiveness: true,
|
||||
}
|
||||
|
||||
args.DSN = config.FormatDSN()
|
||||
}
|
||||
|
||||
target := &MySQLTarget{
|
||||
id: event.TargetID{ID: id, Name: "mysql"},
|
||||
args: args,
|
||||
firstPing: false,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
db, err := sql.Open("mysql", args.DSN)
|
||||
if err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.db = db
|
||||
|
||||
if args.MaxOpenConnections > 0 {
|
||||
// Set the maximum connections limit
|
||||
target.db.SetMaxOpenConns(args.MaxOpenConnections)
|
||||
}
|
||||
|
||||
var store Store
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-mysql-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
err = target.db.Ping()
|
||||
if err != nil {
|
||||
if target.store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
} else {
|
||||
if err = target.executeStmts(); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.firstPing = true
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPostgreSQLRegistration checks if sql driver
|
||||
// is registered and fails otherwise.
|
||||
func TestMySQLRegistration(t *testing.T) {
|
||||
var found bool
|
||||
for _, drv := range sql.Drivers() {
|
||||
if drv == "mysql" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("mysql driver not registered")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/stan.go"
|
||||
)
|
||||
|
||||
// NATS related constants
|
||||
const (
|
||||
NATSAddress = "address"
|
||||
NATSSubject = "subject"
|
||||
NATSUsername = "username"
|
||||
NATSPassword = "password"
|
||||
NATSToken = "token"
|
||||
NATSTLS = "tls"
|
||||
NATSTLSSkipVerify = "tls_skip_verify"
|
||||
NATSPingInterval = "ping_interval"
|
||||
NATSQueueDir = "queue_dir"
|
||||
NATSQueueLimit = "queue_limit"
|
||||
NATSCertAuthority = "cert_authority"
|
||||
NATSClientCert = "client_cert"
|
||||
NATSClientKey = "client_key"
|
||||
|
||||
// Streaming constants
|
||||
NATSStreaming = "streaming"
|
||||
NATSStreamingClusterID = "streaming_cluster_id"
|
||||
NATSStreamingAsync = "streaming_async"
|
||||
NATSStreamingMaxPubAcksInFlight = "streaming_max_pub_acks_in_flight"
|
||||
|
||||
EnvNATSEnable = "MINIO_NOTIFY_NATS_ENABLE"
|
||||
EnvNATSAddress = "MINIO_NOTIFY_NATS_ADDRESS"
|
||||
EnvNATSSubject = "MINIO_NOTIFY_NATS_SUBJECT"
|
||||
EnvNATSUsername = "MINIO_NOTIFY_NATS_USERNAME"
|
||||
EnvNATSPassword = "MINIO_NOTIFY_NATS_PASSWORD"
|
||||
EnvNATSToken = "MINIO_NOTIFY_NATS_TOKEN"
|
||||
EnvNATSTLS = "MINIO_NOTIFY_NATS_TLS"
|
||||
EnvNATSTLSSkipVerify = "MINIO_NOTIFY_NATS_TLS_SKIP_VERIFY"
|
||||
EnvNATSPingInterval = "MINIO_NOTIFY_NATS_PING_INTERVAL"
|
||||
EnvNATSQueueDir = "MINIO_NOTIFY_NATS_QUEUE_DIR"
|
||||
EnvNATSQueueLimit = "MINIO_NOTIFY_NATS_QUEUE_LIMIT"
|
||||
EnvNATSCertAuthority = "MINIO_NOTIFY_NATS_CERT_AUTHORITY"
|
||||
EnvNATSClientCert = "MINIO_NOTIFY_NATS_CLIENT_CERT"
|
||||
EnvNATSClientKey = "MINIO_NOTIFY_NATS_CLIENT_KEY"
|
||||
|
||||
// Streaming constants
|
||||
EnvNATSStreaming = "MINIO_NOTIFY_NATS_STREAMING"
|
||||
EnvNATSStreamingClusterID = "MINIO_NOTIFY_NATS_STREAMING_CLUSTER_ID"
|
||||
EnvNATSStreamingAsync = "MINIO_NOTIFY_NATS_STREAMING_ASYNC"
|
||||
EnvNATSStreamingMaxPubAcksInFlight = "MINIO_NOTIFY_NATS_STREAMING_MAX_PUB_ACKS_IN_FLIGHT"
|
||||
)
|
||||
|
||||
// NATSArgs - NATS target arguments.
|
||||
type NATSArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Address xnet.Host `json:"address"`
|
||||
Subject string `json:"subject"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Token string `json:"token"`
|
||||
TLS bool `json:"tls"`
|
||||
TLSSkipVerify bool `json:"tlsSkipVerify"`
|
||||
Secure bool `json:"secure"`
|
||||
CertAuthority string `json:"certAuthority"`
|
||||
ClientCert string `json:"clientCert"`
|
||||
ClientKey string `json:"clientKey"`
|
||||
PingInterval int64 `json:"pingInterval"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
Streaming struct {
|
||||
Enable bool `json:"enable"`
|
||||
ClusterID string `json:"clusterID"`
|
||||
Async bool `json:"async"`
|
||||
MaxPubAcksInflight int `json:"maxPubAcksInflight"`
|
||||
} `json:"streaming"`
|
||||
|
||||
RootCAs *x509.CertPool `json:"-"`
|
||||
}
|
||||
|
||||
// Validate NATSArgs fields
|
||||
func (n NATSArgs) Validate() error {
|
||||
if !n.Enable {
|
||||
return nil
|
||||
}
|
||||
|
||||
if n.Address.IsEmpty() {
|
||||
return errors.New("empty address")
|
||||
}
|
||||
|
||||
if n.Subject == "" {
|
||||
return errors.New("empty subject")
|
||||
}
|
||||
|
||||
if n.ClientCert != "" && n.ClientKey == "" || n.ClientCert == "" && n.ClientKey != "" {
|
||||
return errors.New("cert and key must be specified as a pair")
|
||||
}
|
||||
|
||||
if n.Username != "" && n.Password == "" || n.Username == "" && n.Password != "" {
|
||||
return errors.New("username and password must be specified as a pair")
|
||||
}
|
||||
|
||||
if n.Streaming.Enable {
|
||||
if n.Streaming.ClusterID == "" {
|
||||
return errors.New("empty cluster id")
|
||||
}
|
||||
}
|
||||
|
||||
if n.QueueDir != "" {
|
||||
if !filepath.IsAbs(n.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// To obtain a nats connection from args.
|
||||
func (n NATSArgs) connectNats() (*nats.Conn, error) {
|
||||
connOpts := []nats.Option{nats.Name("Minio Notification")}
|
||||
if n.Username != "" && n.Password != "" {
|
||||
connOpts = append(connOpts, nats.UserInfo(n.Username, n.Password))
|
||||
}
|
||||
if n.Token != "" {
|
||||
connOpts = append(connOpts, nats.Token(n.Token))
|
||||
}
|
||||
if n.Secure || n.TLS && n.TLSSkipVerify {
|
||||
connOpts = append(connOpts, nats.Secure(nil))
|
||||
} else if n.TLS {
|
||||
connOpts = append(connOpts, nats.Secure(&tls.Config{RootCAs: n.RootCAs}))
|
||||
}
|
||||
if n.CertAuthority != "" {
|
||||
connOpts = append(connOpts, nats.RootCAs(n.CertAuthority))
|
||||
}
|
||||
if n.ClientCert != "" && n.ClientKey != "" {
|
||||
connOpts = append(connOpts, nats.ClientCert(n.ClientCert, n.ClientKey))
|
||||
}
|
||||
return nats.Connect(n.Address.String(), connOpts...)
|
||||
}
|
||||
|
||||
// To obtain a streaming connection from args.
|
||||
func (n NATSArgs) connectStan() (stan.Conn, error) {
|
||||
scheme := "nats"
|
||||
if n.Secure {
|
||||
scheme = "tls"
|
||||
}
|
||||
|
||||
var addressURL string
|
||||
if n.Username != "" && n.Password != "" {
|
||||
addressURL = scheme + "://" + n.Username + ":" + n.Password + "@" + n.Address.String()
|
||||
} else if n.Token != "" {
|
||||
addressURL = scheme + "://" + n.Token + "@" + n.Address.String()
|
||||
} else {
|
||||
addressURL = scheme + "://" + n.Address.String()
|
||||
}
|
||||
|
||||
clientID, err := getNewUUID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
connOpts := []stan.Option{stan.NatsURL(addressURL)}
|
||||
if n.Streaming.MaxPubAcksInflight > 0 {
|
||||
connOpts = append(connOpts, stan.MaxPubAcksInflight(n.Streaming.MaxPubAcksInflight))
|
||||
}
|
||||
|
||||
return stan.Connect(n.Streaming.ClusterID, clientID, connOpts...)
|
||||
}
|
||||
|
||||
// NATSTarget - NATS target.
|
||||
type NATSTarget struct {
|
||||
id event.TargetID
|
||||
args NATSArgs
|
||||
natsConn *nats.Conn
|
||||
stanConn stan.Conn
|
||||
store Store
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *NATSTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *NATSTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *NATSTarget) IsActive() (bool, error) {
|
||||
var connErr error
|
||||
if target.args.Streaming.Enable {
|
||||
if target.stanConn == nil || target.stanConn.NatsConn() == nil {
|
||||
target.stanConn, connErr = target.args.connectStan()
|
||||
} else {
|
||||
if !target.stanConn.NatsConn().IsConnected() {
|
||||
return false, errNotConnected
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if target.natsConn == nil {
|
||||
target.natsConn, connErr = target.args.connectNats()
|
||||
} else {
|
||||
if !target.natsConn.IsConnected() {
|
||||
return false, errNotConnected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if connErr != nil {
|
||||
if connErr.Error() == nats.ErrNoServers.Error() {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, connErr
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store which will be replayed when the Nats connection is active.
|
||||
func (target *NATSTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// send - sends an event to the Nats.
|
||||
func (target *NATSTarget) send(eventData event.Event) error {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if target.stanConn != nil {
|
||||
if target.args.Streaming.Async {
|
||||
_, err = target.stanConn.PublishAsync(target.args.Subject, data, nil)
|
||||
} else {
|
||||
err = target.stanConn.Publish(target.args.Subject, data)
|
||||
}
|
||||
} else {
|
||||
err = target.natsConn.Publish(target.args.Subject, data)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Send - sends event to Nats.
|
||||
func (target *NATSTarget) Send(eventKey string) error {
|
||||
_, err := target.IsActive()
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - closes underneath connections to NATS server.
|
||||
func (target *NATSTarget) Close() (err error) {
|
||||
if target.stanConn != nil {
|
||||
// closing the streaming connection does not close the provided NATS connection.
|
||||
if target.stanConn.NatsConn() != nil {
|
||||
target.stanConn.NatsConn().Close()
|
||||
}
|
||||
err = target.stanConn.Close()
|
||||
}
|
||||
|
||||
if target.natsConn != nil {
|
||||
target.natsConn.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// NewNATSTarget - creates new NATS target.
|
||||
func NewNATSTarget(id string, args NATSArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*NATSTarget, error) {
|
||||
var natsConn *nats.Conn
|
||||
var stanConn stan.Conn
|
||||
|
||||
var err error
|
||||
|
||||
var store Store
|
||||
|
||||
target := &NATSTarget{
|
||||
id: event.TargetID{ID: id, Name: "nats"},
|
||||
args: args,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nats-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
if args.Streaming.Enable {
|
||||
stanConn, err = args.connectStan()
|
||||
target.stanConn = stanConn
|
||||
} else {
|
||||
natsConn, err = args.connectNats()
|
||||
target.natsConn = natsConn
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if store == nil || err.Error() != nats.ErrNoServers.Error() {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* MinIO Object Storage (c) 2021 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 target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
natsserver "github.com/nats-io/nats-server/v2/test"
|
||||
)
|
||||
|
||||
func TestNatsConnPlain(t *testing.T) {
|
||||
opts := natsserver.DefaultTestOptions
|
||||
opts.Port = 14222
|
||||
s := natsserver.RunServer(&opts)
|
||||
defer s.Shutdown()
|
||||
|
||||
clientConfig := &NATSArgs{
|
||||
Enable: true,
|
||||
Address: xnet.Host{Name: "localhost",
|
||||
Port: (xnet.Port(opts.Port)),
|
||||
IsPortSet: true},
|
||||
Subject: "test",
|
||||
}
|
||||
con, err := clientConfig.connectNats()
|
||||
if err != nil {
|
||||
t.Errorf("Could not connect to nats: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
}
|
||||
|
||||
func TestNatsConnUserPass(t *testing.T) {
|
||||
opts := natsserver.DefaultTestOptions
|
||||
opts.Port = 14223
|
||||
opts.Username = "testminio"
|
||||
opts.Password = "miniotest"
|
||||
s := natsserver.RunServer(&opts)
|
||||
defer s.Shutdown()
|
||||
|
||||
clientConfig := &NATSArgs{
|
||||
Enable: true,
|
||||
Address: xnet.Host{Name: "localhost",
|
||||
Port: (xnet.Port(opts.Port)),
|
||||
IsPortSet: true},
|
||||
Subject: "test",
|
||||
Username: opts.Username,
|
||||
Password: opts.Password,
|
||||
}
|
||||
|
||||
con, err := clientConfig.connectNats()
|
||||
if err != nil {
|
||||
t.Errorf("Could not connect to nats: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
}
|
||||
|
||||
func TestNatsConnToken(t *testing.T) {
|
||||
opts := natsserver.DefaultTestOptions
|
||||
opts.Port = 14223
|
||||
opts.Authorization = "s3cr3t"
|
||||
s := natsserver.RunServer(&opts)
|
||||
defer s.Shutdown()
|
||||
|
||||
clientConfig := &NATSArgs{
|
||||
Enable: true,
|
||||
Address: xnet.Host{Name: "localhost",
|
||||
Port: (xnet.Port(opts.Port)),
|
||||
IsPortSet: true},
|
||||
Subject: "test",
|
||||
Token: opts.Authorization,
|
||||
}
|
||||
|
||||
con, err := clientConfig.connectNats()
|
||||
if err != nil {
|
||||
t.Errorf("Could not connect to nats: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* MinIO Object Storage (c) 2021 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 target
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
natsserver "github.com/nats-io/nats-server/v2/test"
|
||||
)
|
||||
|
||||
func TestNatsConnTLSCustomCA(t *testing.T) {
|
||||
s, opts := natsserver.RunServerWithConfig(filepath.Join("testdata", "contrib", "nats_tls.conf"))
|
||||
defer s.Shutdown()
|
||||
|
||||
clientConfig := &NATSArgs{
|
||||
Enable: true,
|
||||
Address: xnet.Host{Name: "localhost",
|
||||
Port: (xnet.Port(opts.Port)),
|
||||
IsPortSet: true},
|
||||
Subject: "test",
|
||||
Secure: true,
|
||||
CertAuthority: path.Join("testdata", "contrib", "certs", "root_ca_cert.pem"),
|
||||
}
|
||||
|
||||
con, err := clientConfig.connectNats()
|
||||
if err != nil {
|
||||
t.Errorf("Could not connect to nats: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
}
|
||||
|
||||
func TestNatsConnTLSClientAuthorization(t *testing.T) {
|
||||
s, opts := natsserver.RunServerWithConfig(filepath.Join("testdata", "contrib", "nats_tls_client_cert.conf"))
|
||||
defer s.Shutdown()
|
||||
|
||||
clientConfig := &NATSArgs{
|
||||
Enable: true,
|
||||
Address: xnet.Host{Name: "localhost",
|
||||
Port: (xnet.Port(opts.Port)),
|
||||
IsPortSet: true},
|
||||
Subject: "test",
|
||||
Secure: true,
|
||||
CertAuthority: path.Join("testdata", "contrib", "certs", "root_ca_cert.pem"),
|
||||
ClientCert: path.Join("testdata", "contrib", "certs", "nats_client_cert.pem"),
|
||||
ClientKey: path.Join("testdata", "contrib", "certs", "nats_client_key.pem"),
|
||||
}
|
||||
|
||||
con, err := clientConfig.connectNats()
|
||||
if err != nil {
|
||||
t.Errorf("Could not connect to nats: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/nsqio/go-nsq"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
// NSQ constants
|
||||
const (
|
||||
NSQAddress = "nsqd_address"
|
||||
NSQTopic = "topic"
|
||||
NSQTLS = "tls"
|
||||
NSQTLSSkipVerify = "tls_skip_verify"
|
||||
NSQQueueDir = "queue_dir"
|
||||
NSQQueueLimit = "queue_limit"
|
||||
|
||||
EnvNSQEnable = "MINIO_NOTIFY_NSQ_ENABLE"
|
||||
EnvNSQAddress = "MINIO_NOTIFY_NSQ_NSQD_ADDRESS"
|
||||
EnvNSQTopic = "MINIO_NOTIFY_NSQ_TOPIC"
|
||||
EnvNSQTLS = "MINIO_NOTIFY_NSQ_TLS"
|
||||
EnvNSQTLSSkipVerify = "MINIO_NOTIFY_NSQ_TLS_SKIP_VERIFY"
|
||||
EnvNSQQueueDir = "MINIO_NOTIFY_NSQ_QUEUE_DIR"
|
||||
EnvNSQQueueLimit = "MINIO_NOTIFY_NSQ_QUEUE_LIMIT"
|
||||
)
|
||||
|
||||
// NSQArgs - NSQ target arguments.
|
||||
type NSQArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
NSQDAddress xnet.Host `json:"nsqdAddress"`
|
||||
Topic string `json:"topic"`
|
||||
TLS struct {
|
||||
Enable bool `json:"enable"`
|
||||
SkipVerify bool `json:"skipVerify"`
|
||||
} `json:"tls"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
}
|
||||
|
||||
// Validate NSQArgs fields
|
||||
func (n NSQArgs) Validate() error {
|
||||
if !n.Enable {
|
||||
return nil
|
||||
}
|
||||
|
||||
if n.NSQDAddress.IsEmpty() {
|
||||
return errors.New("empty nsqdAddress")
|
||||
}
|
||||
|
||||
if n.Topic == "" {
|
||||
return errors.New("empty topic")
|
||||
}
|
||||
if n.QueueDir != "" {
|
||||
if !filepath.IsAbs(n.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NSQTarget - NSQ target.
|
||||
type NSQTarget struct {
|
||||
id event.TargetID
|
||||
args NSQArgs
|
||||
producer *nsq.Producer
|
||||
store Store
|
||||
config *nsq.Config
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *NSQTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *NSQTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *NSQTarget) IsActive() (bool, error) {
|
||||
if target.producer == nil {
|
||||
producer, err := nsq.NewProducer(target.args.NSQDAddress.String(), target.config)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
target.producer = producer
|
||||
}
|
||||
|
||||
if err := target.producer.Ping(); err != nil {
|
||||
// To treat "connection refused" errors as errNotConnected.
|
||||
if IsConnRefusedErr(err) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store which will be replayed when the nsq connection is active.
|
||||
func (target *NSQTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// send - sends an event to the NSQ.
|
||||
func (target *NSQTarget) send(eventData event.Event) error {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return target.producer.Publish(target.args.Topic, data)
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to NSQ.
|
||||
func (target *NSQTarget) Send(eventKey string) error {
|
||||
_, err := target.IsActive()
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - closes underneath connections to NSQD server.
|
||||
func (target *NSQTarget) Close() (err error) {
|
||||
if target.producer != nil {
|
||||
// this blocks until complete:
|
||||
target.producer.Stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewNSQTarget - creates new NSQ target.
|
||||
func NewNSQTarget(id string, args NSQArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*NSQTarget, error) {
|
||||
config := nsq.NewConfig()
|
||||
if args.TLS.Enable {
|
||||
config.TlsV1 = true
|
||||
config.TlsConfig = &tls.Config{
|
||||
InsecureSkipVerify: args.TLS.SkipVerify,
|
||||
}
|
||||
}
|
||||
|
||||
var store Store
|
||||
|
||||
target := &NSQTarget{
|
||||
id: event.TargetID{ID: id, Name: "nsq"},
|
||||
args: args,
|
||||
config: config,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nsq-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
producer, err := nsq.NewProducer(args.NSQDAddress.String(), config)
|
||||
if err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.producer = producer
|
||||
|
||||
if err := target.producer.Ping(); err != nil {
|
||||
// To treat "connection refused" errors as errNotConnected.
|
||||
if target.store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
func TestNSQArgs_Validate(t *testing.T) {
|
||||
type fields struct {
|
||||
Enable bool
|
||||
NSQDAddress xnet.Host
|
||||
Topic string
|
||||
TLS struct {
|
||||
Enable bool
|
||||
SkipVerify bool
|
||||
}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1_missing_topic",
|
||||
fields: fields{
|
||||
Enable: true,
|
||||
NSQDAddress: xnet.Host{
|
||||
Name: "127.0.0.1",
|
||||
Port: 4150,
|
||||
IsPortSet: true,
|
||||
},
|
||||
Topic: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "test2_disabled",
|
||||
fields: fields{
|
||||
Enable: false,
|
||||
NSQDAddress: xnet.Host{},
|
||||
Topic: "topic",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test3_OK",
|
||||
fields: fields{
|
||||
Enable: true,
|
||||
NSQDAddress: xnet.Host{
|
||||
Name: "127.0.0.1",
|
||||
Port: 4150,
|
||||
IsPortSet: true,
|
||||
},
|
||||
Topic: "topic",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test4_emptynsqdaddr",
|
||||
fields: fields{
|
||||
Enable: true,
|
||||
NSQDAddress: xnet.Host{},
|
||||
Topic: "topic",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := NSQArgs{
|
||||
Enable: tt.fields.Enable,
|
||||
NSQDAddress: tt.fields.NSQDAddress,
|
||||
Topic: tt.fields.Topic,
|
||||
}
|
||||
if err := n.Validate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("NSQArgs.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq" // Register postgres driver
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
const (
|
||||
psqlTableExists = `SELECT 1 FROM %s;`
|
||||
psqlCreateNamespaceTable = `CREATE TABLE %s (key VARCHAR PRIMARY KEY, value JSONB);`
|
||||
psqlCreateAccessTable = `CREATE TABLE %s (event_time TIMESTAMP WITH TIME ZONE NOT NULL, event_data JSONB);`
|
||||
|
||||
psqlUpdateRow = `INSERT INTO %s (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;`
|
||||
psqlDeleteRow = `DELETE FROM %s WHERE key = $1;`
|
||||
psqlInsertRow = `INSERT INTO %s (event_time, event_data) VALUES ($1, $2);`
|
||||
)
|
||||
|
||||
// Postgres constants
|
||||
const (
|
||||
PostgresFormat = "format"
|
||||
PostgresConnectionString = "connection_string"
|
||||
PostgresTable = "table"
|
||||
PostgresHost = "host"
|
||||
PostgresPort = "port"
|
||||
PostgresUsername = "username"
|
||||
PostgresPassword = "password"
|
||||
PostgresDatabase = "database"
|
||||
PostgresQueueDir = "queue_dir"
|
||||
PostgresQueueLimit = "queue_limit"
|
||||
PostgresMaxOpenConnections = "max_open_connections"
|
||||
|
||||
EnvPostgresEnable = "MINIO_NOTIFY_POSTGRES_ENABLE"
|
||||
EnvPostgresFormat = "MINIO_NOTIFY_POSTGRES_FORMAT"
|
||||
EnvPostgresConnectionString = "MINIO_NOTIFY_POSTGRES_CONNECTION_STRING"
|
||||
EnvPostgresTable = "MINIO_NOTIFY_POSTGRES_TABLE"
|
||||
EnvPostgresHost = "MINIO_NOTIFY_POSTGRES_HOST"
|
||||
EnvPostgresPort = "MINIO_NOTIFY_POSTGRES_PORT"
|
||||
EnvPostgresUsername = "MINIO_NOTIFY_POSTGRES_USERNAME"
|
||||
EnvPostgresPassword = "MINIO_NOTIFY_POSTGRES_PASSWORD"
|
||||
EnvPostgresDatabase = "MINIO_NOTIFY_POSTGRES_DATABASE"
|
||||
EnvPostgresQueueDir = "MINIO_NOTIFY_POSTGRES_QUEUE_DIR"
|
||||
EnvPostgresQueueLimit = "MINIO_NOTIFY_POSTGRES_QUEUE_LIMIT"
|
||||
EnvPostgresMaxOpenConnections = "MINIO_NOTIFY_POSTGRES_MAX_OPEN_CONNECTIONS"
|
||||
)
|
||||
|
||||
// PostgreSQLArgs - PostgreSQL target arguments.
|
||||
type PostgreSQLArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Format string `json:"format"`
|
||||
ConnectionString string `json:"connectionString"`
|
||||
Table string `json:"table"`
|
||||
Host xnet.Host `json:"host"` // default: localhost
|
||||
Port string `json:"port"` // default: 5432
|
||||
Username string `json:"username"` // default: user running minio
|
||||
Password string `json:"password"` // default: no password
|
||||
Database string `json:"database"` // default: same as user
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
MaxOpenConnections int `json:"maxOpenConnections"`
|
||||
}
|
||||
|
||||
// Validate PostgreSQLArgs fields
|
||||
func (p PostgreSQLArgs) Validate() error {
|
||||
if !p.Enable {
|
||||
return nil
|
||||
}
|
||||
if p.Table == "" {
|
||||
return fmt.Errorf("empty table name")
|
||||
}
|
||||
if p.Format != "" {
|
||||
f := strings.ToLower(p.Format)
|
||||
if f != event.NamespaceFormat && f != event.AccessFormat {
|
||||
return fmt.Errorf("unrecognized format value")
|
||||
}
|
||||
}
|
||||
|
||||
if p.ConnectionString != "" {
|
||||
// No pq API doesn't help to validate connection string
|
||||
// prior connection, so no validation for now.
|
||||
} else {
|
||||
// Some fields need to be specified when ConnectionString is unspecified
|
||||
if p.Port == "" {
|
||||
return fmt.Errorf("unspecified port")
|
||||
}
|
||||
if _, err := strconv.Atoi(p.Port); err != nil {
|
||||
return fmt.Errorf("invalid port")
|
||||
}
|
||||
if p.Database == "" {
|
||||
return fmt.Errorf("database unspecified")
|
||||
}
|
||||
}
|
||||
|
||||
if p.QueueDir != "" {
|
||||
if !filepath.IsAbs(p.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
if p.MaxOpenConnections < 0 {
|
||||
return errors.New("maxOpenConnections cannot be less than zero")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostgreSQLTarget - PostgreSQL target.
|
||||
type PostgreSQLTarget struct {
|
||||
id event.TargetID
|
||||
args PostgreSQLArgs
|
||||
updateStmt *sql.Stmt
|
||||
deleteStmt *sql.Stmt
|
||||
insertStmt *sql.Stmt
|
||||
db *sql.DB
|
||||
store Store
|
||||
firstPing bool
|
||||
connString string
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *PostgreSQLTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *PostgreSQLTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *PostgreSQLTarget) IsActive() (bool, error) {
|
||||
if target.db == nil {
|
||||
db, err := sql.Open("postgres", target.connString)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
target.db = db
|
||||
if target.args.MaxOpenConnections > 0 {
|
||||
// Set the maximum connections limit
|
||||
target.db.SetMaxOpenConns(target.args.MaxOpenConnections)
|
||||
}
|
||||
}
|
||||
if err := target.db.Ping(); err != nil {
|
||||
if IsConnErr(err) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store if questore is configured, which will be replayed when the PostgreSQL connection is active.
|
||||
func (target *PostgreSQLTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// IsConnErr - To detect a connection error.
|
||||
func IsConnErr(err error) bool {
|
||||
return IsConnRefusedErr(err) || err.Error() == "sql: database is closed" || err.Error() == "sql: statement is closed" || err.Error() == "invalid connection"
|
||||
}
|
||||
|
||||
// send - sends an event to the PostgreSQL.
|
||||
func (target *PostgreSQLTarget) send(eventData event.Event) error {
|
||||
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 = target.deleteStmt.Exec(key)
|
||||
} else {
|
||||
var data []byte
|
||||
if data, err = json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = target.updateStmt.Exec(key, data)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if target.args.Format == event.AccessFormat {
|
||||
eventTime, err := time.Parse(event.AMZTimeFormat, eventData.EventTime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = target.insertStmt.Exec(eventTime, data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to PostgreSQL.
|
||||
func (target *PostgreSQLTarget) Send(eventKey string) error {
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !target.firstPing {
|
||||
if err := target.executeStmts(); err != nil {
|
||||
if IsConnErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
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 {
|
||||
if IsConnErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - closes underneath connections to PostgreSQL database.
|
||||
func (target *PostgreSQLTarget) Close() error {
|
||||
if target.updateStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.updateStmt.Close()
|
||||
}
|
||||
|
||||
if target.deleteStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.deleteStmt.Close()
|
||||
}
|
||||
|
||||
if target.insertStmt != nil {
|
||||
// FIXME: log returned error. ignore time being.
|
||||
_ = target.insertStmt.Close()
|
||||
}
|
||||
|
||||
return target.db.Close()
|
||||
}
|
||||
|
||||
// Executes the table creation statements.
|
||||
func (target *PostgreSQLTarget) executeStmts() error {
|
||||
|
||||
_, err := target.db.Exec(fmt.Sprintf(psqlTableExists, target.args.Table))
|
||||
if err != nil {
|
||||
createStmt := psqlCreateNamespaceTable
|
||||
if target.args.Format == event.AccessFormat {
|
||||
createStmt = psqlCreateAccessTable
|
||||
}
|
||||
|
||||
if _, dbErr := target.db.Exec(fmt.Sprintf(createStmt, target.args.Table)); dbErr != nil {
|
||||
return dbErr
|
||||
}
|
||||
}
|
||||
|
||||
switch target.args.Format {
|
||||
case event.NamespaceFormat:
|
||||
// insert or update statement
|
||||
if target.updateStmt, err = target.db.Prepare(fmt.Sprintf(psqlUpdateRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
// delete statement
|
||||
if target.deleteStmt, err = target.db.Prepare(fmt.Sprintf(psqlDeleteRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
case event.AccessFormat:
|
||||
// insert statement
|
||||
if target.insertStmt, err = target.db.Prepare(fmt.Sprintf(psqlInsertRow, target.args.Table)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewPostgreSQLTarget - creates new PostgreSQL target.
|
||||
func NewPostgreSQLTarget(id string, args PostgreSQLArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*PostgreSQLTarget, error) {
|
||||
params := []string{args.ConnectionString}
|
||||
if args.ConnectionString == "" {
|
||||
params = []string{}
|
||||
if !args.Host.IsEmpty() {
|
||||
params = append(params, "host="+args.Host.String())
|
||||
}
|
||||
if args.Port != "" {
|
||||
params = append(params, "port="+args.Port)
|
||||
}
|
||||
if args.Username != "" {
|
||||
params = append(params, "username="+args.Username)
|
||||
}
|
||||
if args.Password != "" {
|
||||
params = append(params, "password="+args.Password)
|
||||
}
|
||||
if args.Database != "" {
|
||||
params = append(params, "dbname="+args.Database)
|
||||
}
|
||||
}
|
||||
connStr := strings.Join(params, " ")
|
||||
|
||||
target := &PostgreSQLTarget{
|
||||
id: event.TargetID{ID: id, Name: "postgresql"},
|
||||
args: args,
|
||||
firstPing: false,
|
||||
connString: connStr,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
return target, err
|
||||
}
|
||||
target.db = db
|
||||
|
||||
if args.MaxOpenConnections > 0 {
|
||||
// Set the maximum connections limit
|
||||
target.db.SetMaxOpenConns(args.MaxOpenConnections)
|
||||
}
|
||||
|
||||
var store Store
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-postgresql-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
err = target.db.Ping()
|
||||
if err != nil {
|
||||
if target.store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
} else {
|
||||
if err = target.executeStmts(); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.firstPing = true
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPostgreSQLRegistration checks if postgres driver
|
||||
// is registered and fails otherwise.
|
||||
func TestPostgreSQLRegistration(t *testing.T) {
|
||||
var found bool
|
||||
for _, drv := range sql.Drivers() {
|
||||
if drv == "postgres" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("postgres driver not registered")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
"github.com/minio/pkg/sys"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultLimit = 100000 // Default store limit.
|
||||
eventExt = ".event"
|
||||
)
|
||||
|
||||
// QueueStore - Filestore for persisting events.
|
||||
type QueueStore struct {
|
||||
sync.RWMutex
|
||||
currentEntries uint64
|
||||
entryLimit uint64
|
||||
directory string
|
||||
}
|
||||
|
||||
// NewQueueStore - Creates an instance for QueueStore.
|
||||
func NewQueueStore(directory string, limit uint64) Store {
|
||||
if limit == 0 {
|
||||
limit = defaultLimit
|
||||
_, maxRLimit, err := sys.GetMaxOpenFileLimit()
|
||||
if err == nil {
|
||||
// Limit the maximum number of entries
|
||||
// to maximum open file limit
|
||||
if maxRLimit < limit {
|
||||
limit = maxRLimit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &QueueStore{
|
||||
directory: directory,
|
||||
entryLimit: limit,
|
||||
}
|
||||
}
|
||||
|
||||
// Open - Creates the directory if not present.
|
||||
func (store *QueueStore) Open() error {
|
||||
store.Lock()
|
||||
defer store.Unlock()
|
||||
|
||||
if err := os.MkdirAll(store.directory, os.FileMode(0770)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
names, err := store.list()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currentEntries := uint64(len(names))
|
||||
if currentEntries >= store.entryLimit {
|
||||
return errLimitExceeded
|
||||
}
|
||||
|
||||
store.currentEntries = currentEntries
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// write - writes event to the directory.
|
||||
func (store *QueueStore) write(key string, e event.Event) error {
|
||||
|
||||
// Marshalls the event.
|
||||
eventData, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path := filepath.Join(store.directory, key+eventExt)
|
||||
if err := ioutil.WriteFile(path, eventData, os.FileMode(0770)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Increment the event count.
|
||||
store.currentEntries++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Put - puts a event to the store.
|
||||
func (store *QueueStore) Put(e event.Event) error {
|
||||
store.Lock()
|
||||
defer store.Unlock()
|
||||
if store.currentEntries >= store.entryLimit {
|
||||
return errLimitExceeded
|
||||
}
|
||||
key, err := getNewUUID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return store.write(key, e)
|
||||
}
|
||||
|
||||
// Get - gets a event from the store.
|
||||
func (store *QueueStore) Get(key string) (event event.Event, err error) {
|
||||
store.RLock()
|
||||
|
||||
defer func(store *QueueStore) {
|
||||
store.RUnlock()
|
||||
if err != nil {
|
||||
// Upon error we remove the entry.
|
||||
store.Del(key)
|
||||
}
|
||||
}(store)
|
||||
|
||||
var eventData []byte
|
||||
eventData, err = ioutil.ReadFile(filepath.Join(store.directory, key+eventExt))
|
||||
if err != nil {
|
||||
return event, err
|
||||
}
|
||||
|
||||
if len(eventData) == 0 {
|
||||
return event, os.ErrNotExist
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(eventData, &event); err != nil {
|
||||
return event, err
|
||||
}
|
||||
|
||||
return event, nil
|
||||
}
|
||||
|
||||
// Del - Deletes an entry from the store.
|
||||
func (store *QueueStore) Del(key string) error {
|
||||
store.Lock()
|
||||
defer store.Unlock()
|
||||
return store.del(key)
|
||||
}
|
||||
|
||||
// lockless call
|
||||
func (store *QueueStore) del(key string) error {
|
||||
if err := os.Remove(filepath.Join(store.directory, key+eventExt)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Decrement the current entries count.
|
||||
store.currentEntries--
|
||||
|
||||
// Current entries can underflow, when multiple
|
||||
// events are being pushed in parallel, this code
|
||||
// is needed to ensure that we don't underflow.
|
||||
//
|
||||
// queueStore replayEvents is not serialized,
|
||||
// this code is needed to protect us under
|
||||
// such situations.
|
||||
if store.currentEntries == math.MaxUint64 {
|
||||
store.currentEntries = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// List - lists all files from the directory.
|
||||
func (store *QueueStore) List() ([]string, error) {
|
||||
store.RLock()
|
||||
defer store.RUnlock()
|
||||
return store.list()
|
||||
}
|
||||
|
||||
// list lock less.
|
||||
func (store *QueueStore) list() ([]string, error) {
|
||||
var names []string
|
||||
files, err := ioutil.ReadDir(store.directory)
|
||||
if err != nil {
|
||||
return names, err
|
||||
}
|
||||
|
||||
// Sort the dentries.
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].ModTime().Before(files[j].ModTime())
|
||||
})
|
||||
|
||||
for _, file := range files {
|
||||
names = append(names, file.Name())
|
||||
}
|
||||
|
||||
return names, nil
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
)
|
||||
|
||||
// TestDir
|
||||
var queueDir = filepath.Join(os.TempDir(), "minio_test")
|
||||
|
||||
// Sample test event.
|
||||
var testEvent = event.Event{EventVersion: "1.0", EventSource: "test_source", AwsRegion: "test_region", EventTime: "test_time", EventName: event.ObjectAccessedGet}
|
||||
|
||||
// Initialize the store.
|
||||
func setUpStore(directory string, limit uint64) (Store, error) {
|
||||
store := NewQueueStore(queueDir, limit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
return nil, oErr
|
||||
}
|
||||
return store, nil
|
||||
}
|
||||
|
||||
// Tear down store
|
||||
func tearDownStore() error {
|
||||
return os.RemoveAll(queueDir)
|
||||
}
|
||||
|
||||
// TestQueueStorePut - tests for store.Put
|
||||
func TestQueueStorePut(t *testing.T) {
|
||||
defer func() {
|
||||
if err := tearDownStore(); err != nil {
|
||||
t.Fatal("Failed to tear down store ", err)
|
||||
}
|
||||
}()
|
||||
store, err := setUpStore(queueDir, 100)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create a queue store ", err)
|
||||
|
||||
}
|
||||
// Put 100 events.
|
||||
for i := 0; i < 100; i++ {
|
||||
if err := store.Put(testEvent); err != nil {
|
||||
t.Fatal("Failed to put to queue store ", err)
|
||||
}
|
||||
}
|
||||
// Count the events.
|
||||
names, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(names) != 100 {
|
||||
t.Fatalf("List() Expected: 100, got %d", len(names))
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueueStoreGet - tests for store.Get
|
||||
func TestQueueStoreGet(t *testing.T) {
|
||||
defer func() {
|
||||
if err := tearDownStore(); err != nil {
|
||||
t.Fatal("Failed to tear down store ", err)
|
||||
}
|
||||
}()
|
||||
store, err := setUpStore(queueDir, 10)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create a queue store ", err)
|
||||
}
|
||||
// Put 10 events
|
||||
for i := 0; i < 10; i++ {
|
||||
if err := store.Put(testEvent); err != nil {
|
||||
t.Fatal("Failed to put to queue store ", err)
|
||||
}
|
||||
}
|
||||
eventKeys, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Get 10 events.
|
||||
if len(eventKeys) == 10 {
|
||||
for _, key := range eventKeys {
|
||||
event, eErr := store.Get(strings.TrimSuffix(key, eventExt))
|
||||
if eErr != nil {
|
||||
t.Fatal("Failed to Get the event from the queue store ", eErr)
|
||||
}
|
||||
if !reflect.DeepEqual(testEvent, event) {
|
||||
t.Fatalf("Failed to read the event: error: expected = %v, got = %v", testEvent, event)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("List() Expected: 10, got %d", len(eventKeys))
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueueStoreDel - tests for store.Del
|
||||
func TestQueueStoreDel(t *testing.T) {
|
||||
defer func() {
|
||||
if err := tearDownStore(); err != nil {
|
||||
t.Fatal("Failed to tear down store ", err)
|
||||
}
|
||||
}()
|
||||
store, err := setUpStore(queueDir, 20)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create a queue store ", err)
|
||||
}
|
||||
// Put 20 events.
|
||||
for i := 0; i < 20; i++ {
|
||||
if err := store.Put(testEvent); err != nil {
|
||||
t.Fatal("Failed to put to queue store ", err)
|
||||
}
|
||||
}
|
||||
eventKeys, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Remove all the events.
|
||||
if len(eventKeys) == 20 {
|
||||
for _, key := range eventKeys {
|
||||
err := store.Del(strings.TrimSuffix(key, eventExt))
|
||||
if err != nil {
|
||||
t.Fatal("queue store Del failed with ", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("List() Expected: 20, got %d", len(eventKeys))
|
||||
}
|
||||
|
||||
names, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(names) != 0 {
|
||||
t.Fatalf("List() Expected: 0, got %d", len(names))
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueueStoreLimit - tests the event limit for the store.
|
||||
func TestQueueStoreLimit(t *testing.T) {
|
||||
defer func() {
|
||||
if err := tearDownStore(); err != nil {
|
||||
t.Fatal("Failed to tear down store ", err)
|
||||
}
|
||||
}()
|
||||
// The max limit is set to 5.
|
||||
store, err := setUpStore(queueDir, 5)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create a queue store ", err)
|
||||
}
|
||||
for i := 0; i < 5; i++ {
|
||||
if err := store.Put(testEvent); err != nil {
|
||||
t.Fatal("Failed to put to queue store ", err)
|
||||
}
|
||||
}
|
||||
// Should not allow 6th Put.
|
||||
if err := store.Put(testEvent); err == nil {
|
||||
t.Fatalf("Expected to fail with %s, but passes", errLimitExceeded)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueueStoreLimit - tests for store.LimitN.
|
||||
func TestQueueStoreListN(t *testing.T) {
|
||||
defer func() {
|
||||
if err := tearDownStore(); err != nil {
|
||||
t.Fatal("Failed to tear down store ", err)
|
||||
}
|
||||
}()
|
||||
store, err := setUpStore(queueDir, 10)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create a queue store ", err)
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
if err := store.Put(testEvent); err != nil {
|
||||
t.Fatal("Failed to put to queue store ", err)
|
||||
}
|
||||
}
|
||||
// Should return all the event keys in the store.
|
||||
names, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(names) != 10 {
|
||||
t.Fatalf("List() Expected: 10, got %d", len(names))
|
||||
}
|
||||
|
||||
if err = os.RemoveAll(queueDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = store.List()
|
||||
if !os.IsNotExist(err) {
|
||||
t.Fatalf("Expected List() to fail with os.ErrNotExist, %s", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
)
|
||||
|
||||
// Redis constants
|
||||
const (
|
||||
RedisFormat = "format"
|
||||
RedisAddress = "address"
|
||||
RedisPassword = "password"
|
||||
RedisKey = "key"
|
||||
RedisQueueDir = "queue_dir"
|
||||
RedisQueueLimit = "queue_limit"
|
||||
|
||||
EnvRedisEnable = "MINIO_NOTIFY_REDIS_ENABLE"
|
||||
EnvRedisFormat = "MINIO_NOTIFY_REDIS_FORMAT"
|
||||
EnvRedisAddress = "MINIO_NOTIFY_REDIS_ADDRESS"
|
||||
EnvRedisPassword = "MINIO_NOTIFY_REDIS_PASSWORD"
|
||||
EnvRedisKey = "MINIO_NOTIFY_REDIS_KEY"
|
||||
EnvRedisQueueDir = "MINIO_NOTIFY_REDIS_QUEUE_DIR"
|
||||
EnvRedisQueueLimit = "MINIO_NOTIFY_REDIS_QUEUE_LIMIT"
|
||||
)
|
||||
|
||||
// RedisArgs - Redis target arguments.
|
||||
type RedisArgs struct {
|
||||
Enable bool `json:"enable"`
|
||||
Format string `json:"format"`
|
||||
Addr xnet.Host `json:"address"`
|
||||
Password string `json:"password"`
|
||||
Key string `json:"key"`
|
||||
QueueDir string `json:"queueDir"`
|
||||
QueueLimit uint64 `json:"queueLimit"`
|
||||
}
|
||||
|
||||
// RedisAccessEvent holds event log data and timestamp
|
||||
type RedisAccessEvent struct {
|
||||
Event []event.Event
|
||||
EventTime string
|
||||
}
|
||||
|
||||
// Validate RedisArgs fields
|
||||
func (r RedisArgs) Validate() error {
|
||||
if !r.Enable {
|
||||
return nil
|
||||
}
|
||||
|
||||
if r.Format != "" {
|
||||
f := strings.ToLower(r.Format)
|
||||
if f != event.NamespaceFormat && f != event.AccessFormat {
|
||||
return fmt.Errorf("unrecognized format")
|
||||
}
|
||||
}
|
||||
|
||||
if r.Key == "" {
|
||||
return fmt.Errorf("empty key")
|
||||
}
|
||||
|
||||
if r.QueueDir != "" {
|
||||
if !filepath.IsAbs(r.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RedisArgs) validateFormat(c redis.Conn) error {
|
||||
typeAvailable, err := redis.String(c.Do("TYPE", r.Key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if typeAvailable != "none" {
|
||||
expectedType := "hash"
|
||||
if r.Format == event.AccessFormat {
|
||||
expectedType = "list"
|
||||
}
|
||||
|
||||
if typeAvailable != expectedType {
|
||||
return fmt.Errorf("expected type %v does not match with available type %v", expectedType, typeAvailable)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RedisTarget - Redis target.
|
||||
type RedisTarget struct {
|
||||
id event.TargetID
|
||||
args RedisArgs
|
||||
pool *redis.Pool
|
||||
store Store
|
||||
firstPing bool
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target *RedisTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *RedisTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *RedisTarget) IsActive() (bool, error) {
|
||||
conn := target.pool.Get()
|
||||
defer func() {
|
||||
cErr := conn.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
_, pingErr := conn.Do("PING")
|
||||
if pingErr != nil {
|
||||
if IsConnRefusedErr(pingErr) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, pingErr
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Save - saves the events to the store if questore is configured, which will be replayed when the redis connection is active.
|
||||
func (target *RedisTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
// send - sends an event to the redis.
|
||||
func (target *RedisTarget) send(eventData event.Event) error {
|
||||
conn := target.pool.Get()
|
||||
defer func() {
|
||||
cErr := conn.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
|
||||
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 = conn.Do("HDEL", target.args.Key, key)
|
||||
} else {
|
||||
var data []byte
|
||||
if data, err = json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = conn.Do("HSET", target.args.Key, key, data)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if target.args.Format == event.AccessFormat {
|
||||
data, err := json.Marshal([]RedisAccessEvent{{Event: []event.Event{eventData}, EventTime: eventData.EventTime}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := conn.Do("RPUSH", target.args.Key, data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to redis.
|
||||
func (target *RedisTarget) Send(eventKey string) error {
|
||||
conn := target.pool.Get()
|
||||
defer func() {
|
||||
cErr := conn.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
_, pingErr := conn.Do("PING")
|
||||
if pingErr != nil {
|
||||
if IsConnRefusedErr(pingErr) {
|
||||
return errNotConnected
|
||||
}
|
||||
return pingErr
|
||||
}
|
||||
|
||||
if !target.firstPing {
|
||||
if err := target.args.validateFormat(conn); err != nil {
|
||||
if IsConnRefusedErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
target.firstPing = true
|
||||
}
|
||||
|
||||
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 {
|
||||
if IsConnRefusedErr(err) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - releases the resources used by the pool.
|
||||
func (target *RedisTarget) Close() error {
|
||||
return target.pool.Close()
|
||||
}
|
||||
|
||||
// NewRedisTarget - creates new Redis target.
|
||||
func NewRedisTarget(id string, args RedisArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{}), test bool) (*RedisTarget, error) {
|
||||
pool := &redis.Pool{
|
||||
MaxIdle: 3,
|
||||
IdleTimeout: 2 * 60 * time.Second,
|
||||
Dial: func() (redis.Conn, error) {
|
||||
conn, err := redis.Dial("tcp", args.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if args.Password != "" {
|
||||
if _, err = conn.Do("AUTH", args.Password); err != nil {
|
||||
cErr := conn.Close()
|
||||
targetID := event.TargetID{ID: id, Name: "redis"}
|
||||
loggerOnce(context.Background(), cErr, targetID)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Must be done after AUTH
|
||||
if _, err = conn.Do("CLIENT", "SETNAME", "MinIO"); err != nil {
|
||||
cErr := conn.Close()
|
||||
targetID := event.TargetID{ID: id, Name: "redis"}
|
||||
loggerOnce(context.Background(), cErr, targetID)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
},
|
||||
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
||||
_, err := c.Do("PING")
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
var store Store
|
||||
|
||||
target := &RedisTarget{
|
||||
id: event.TargetID{ID: id, Name: "redis"},
|
||||
args: args,
|
||||
pool: pool,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-redis-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if oErr := store.Open(); oErr != nil {
|
||||
target.loggerOnce(context.Background(), oErr, target.ID())
|
||||
return target, oErr
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
conn := target.pool.Get()
|
||||
defer func() {
|
||||
cErr := conn.Close()
|
||||
target.loggerOnce(context.Background(), cErr, target.ID())
|
||||
}()
|
||||
|
||||
_, pingErr := conn.Do("PING")
|
||||
if pingErr != nil {
|
||||
if target.store == nil || !(IsConnRefusedErr(pingErr) || IsConnResetErr(pingErr)) {
|
||||
target.loggerOnce(context.Background(), pingErr, target.ID())
|
||||
return target, pingErr
|
||||
}
|
||||
} else {
|
||||
if err := target.args.validateFormat(conn); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.firstPing = true
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
)
|
||||
|
||||
const retryInterval = 3 * time.Second
|
||||
|
||||
// errNotConnected - indicates that the target connection is not active.
|
||||
var errNotConnected = errors.New("not connected to target server/service")
|
||||
|
||||
// errLimitExceeded error is sent when the maximum limit is reached.
|
||||
var errLimitExceeded = errors.New("the maximum store limit reached")
|
||||
|
||||
// Store - To persist the events.
|
||||
type Store interface {
|
||||
Put(event event.Event) error
|
||||
Get(key string) (event.Event, error)
|
||||
List() ([]string, error)
|
||||
Del(key string) error
|
||||
Open() error
|
||||
}
|
||||
|
||||
// replayEvents - Reads the events from the store and replays.
|
||||
func replayEvents(store Store, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), id event.TargetID) <-chan string {
|
||||
eventKeyCh := make(chan string)
|
||||
|
||||
go func() {
|
||||
retryTicker := time.NewTicker(retryInterval)
|
||||
defer retryTicker.Stop()
|
||||
defer close(eventKeyCh)
|
||||
for {
|
||||
names, err := store.List()
|
||||
if err == nil {
|
||||
for _, name := range names {
|
||||
select {
|
||||
case eventKeyCh <- strings.TrimSuffix(name, eventExt):
|
||||
// Get next key.
|
||||
case <-doneCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(names) < 2 {
|
||||
select {
|
||||
case <-retryTicker.C:
|
||||
if err != nil {
|
||||
loggerOnce(context.Background(),
|
||||
fmt.Errorf("store.List() failed '%w'", err), id)
|
||||
}
|
||||
case <-doneCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return eventKeyCh
|
||||
}
|
||||
|
||||
// IsConnRefusedErr - To check fot "connection refused" error.
|
||||
func IsConnRefusedErr(err error) bool {
|
||||
return errors.Is(err, syscall.ECONNREFUSED)
|
||||
}
|
||||
|
||||
// IsConnResetErr - Checks for connection reset errors.
|
||||
func IsConnResetErr(err error) bool {
|
||||
if strings.Contains(err.Error(), "connection reset by peer") {
|
||||
return true
|
||||
}
|
||||
// incase if error message is wrapped.
|
||||
return errors.Is(err, syscall.ECONNRESET)
|
||||
}
|
||||
|
||||
// sendEvents - Reads events from the store and re-plays.
|
||||
func sendEvents(target event.Target, eventKeyCh <-chan string, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{})) {
|
||||
retryTicker := time.NewTicker(retryInterval)
|
||||
defer retryTicker.Stop()
|
||||
|
||||
send := func(eventKey string) bool {
|
||||
for {
|
||||
err := target.Send(eventKey)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if err != errNotConnected && !IsConnResetErr(err) {
|
||||
loggerOnce(context.Background(),
|
||||
fmt.Errorf("target.Send() failed with '%w'", err),
|
||||
target.ID())
|
||||
}
|
||||
|
||||
// Retrying after 3secs back-off
|
||||
|
||||
select {
|
||||
case <-retryTicker.C:
|
||||
case <-doneCh:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case eventKey, ok := <-eventKeyCh:
|
||||
if !ok {
|
||||
// closed channel.
|
||||
return
|
||||
}
|
||||
|
||||
if !send(eventKey) {
|
||||
return
|
||||
}
|
||||
case <-doneCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICCjCCAbGgAwIBAgIUKLFyLD0Ze9gR3A2aBxgEiT6MgZUwCgYIKoZIzj0EAwIw
|
||||
GDEWMBQGA1UEAwwNTWluaW8gUm9vdCBDQTAeFw0yMDA5MTQxMzI0MzNaFw0zMDA5
|
||||
MTIxMzI0MzNaMEIxCzAJBgNVBAYTAkNBMQ4wDAYDVQQKDAVNaW5JTzEPMA0GA1UE
|
||||
CwwGQ2xpZW50MRIwEAYDVQQDDAlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggqhkjO
|
||||
PQMBBwNCAARAhYrQXYbzeKyVSw8nf57gBphwFP1o5S7CjxoGKCfghzdhExKiEmbi
|
||||
sK+FSS2YtltU7cM7L7AduLIbuEnGHHYQo4GuMIGrMAkGA1UdEwQCMAAwUwYDVR0j
|
||||
BEwwSoAUWN6Fr30E5vvvNOBkuGGkqGzA3SihHKQaMBgxFjAUBgNVBAMMDU1pbmlv
|
||||
IFJvb3QgQ0GCFHiTsAON45VvwFb0MxHEdLPeWi95MA4GA1UdDwEB/wQEAwIFoDAd
|
||||
BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGgYDVR0RBBMwEYcEfwAAAYIJ
|
||||
bG9jYWxob3N0MAoGCCqGSM49BAMCA0cAMEQCIC7MHOEf0C/zqw/ZOaCffeJIMeFm
|
||||
iT8ugBfhFbgGkd5YAiBz9FEfV4JMZQ4N29WLmvxxDSxkL8g5e3fnIK8Aa4excw==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,5 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIBluB2BuspJcz1e58rnXpQEx48/ZwNmygNw06NbdTZDroAoGCCqGSM49
|
||||
AwEHoUQDQgAEQIWK0F2G83islUsPJ3+e4AaYcBT9aOUuwo8aBign4Ic3YRMSohJm
|
||||
4rCvhUktmLZbVO3DOy+wHbiyG7hJxhx2EA==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -0,0 +1,12 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIByTCCAW+gAwIBAgIUdAg80BTm1El7s5ZZezgjsls9BwkwCgYIKoZIzj0EAwIw
|
||||
GDEWMBQGA1UEAwwNTWluaW8gUm9vdCBDQTAeFw0yMDA5MTQxMjQzMjNaFw0zMDA5
|
||||
MTIxMjQzMjNaMAAwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASolKUI7FVSA2Ts
|
||||
+GSW/DHDKNczDNjfccI2GLETso6ie8buveOODj1JIL9ff5pRDN+U6QvwwlDmXEqh
|
||||
1a6XBI4Ho4GuMIGrMAkGA1UdEwQCMAAwUwYDVR0jBEwwSoAUWN6Fr30E5vvvNOBk
|
||||
uGGkqGzA3SihHKQaMBgxFjAUBgNVBAMMDU1pbmlvIFJvb3QgQ0GCFHiTsAON45Vv
|
||||
wFb0MxHEdLPeWi95MA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcD
|
||||
AQYIKwYBBQUHAwIwGgYDVR0RBBMwEYcEfwAAAYIJbG9jYWxob3N0MAoGCCqGSM49
|
||||
BAMCA0gAMEUCIB7WXnQAkmjw2QE6A3uOscOIctJnlVNREfm4V9CrF6UGAiEA734B
|
||||
vKlhMk8H459BRoIp8GpOuUWqLqocSmMM1febvcg=
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,5 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEILFuMS2xvsc/CsuqtSv3S2iSCcc28rZsg1wpR2kirXFloAoGCCqGSM49
|
||||
AwEHoUQDQgAEqJSlCOxVUgNk7PhklvwxwyjXMwzY33HCNhixE7KOonvG7r3jjg49
|
||||
SSC/X3+aUQzflOkL8MJQ5lxKodWulwSOBw==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -0,0 +1,11 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIBlTCCATygAwIBAgIUeJOwA43jlW/AVvQzEcR0s95aL3kwCgYIKoZIzj0EAwIw
|
||||
GDEWMBQGA1UEAwwNTWluaW8gUm9vdCBDQTAeFw0yMDA5MTQxMjMwMDJaFw0zMDA5
|
||||
MTIxMjMwMDJaMBgxFjAUBgNVBAMMDU1pbmlvIFJvb3QgQ0EwWTATBgcqhkjOPQIB
|
||||
BggqhkjOPQMBBwNCAARK9fVNGHc1h5B5fpOMyEdyhh18xNNcNUGQ5iGLO97Z0KtK
|
||||
5vRlDeeE1I0SaJgqppm9OEHw32JU0HMi4FBZi2Rso2QwYjAdBgNVHQ4EFgQUWN6F
|
||||
r30E5vvvNOBkuGGkqGzA3SgwHwYDVR0jBBgwFoAUWN6Fr30E5vvvNOBkuGGkqGzA
|
||||
3SgwDwYDVR0TAQH/BAUwAwEB/zAPBgNVHREECDAGhwR/AAABMAoGCCqGSM49BAMC
|
||||
A0cAMEQCIDPOiks2Vs3RmuJZl5HHjuqaFSOAp1g7pZpMb3Qrh9YDAiAtjO2xOpkS
|
||||
WynK8P7EfyQP/IUa7GxJIoHk6/H/TCsYvQ==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,5 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIB8tAGuc9FP4XbYqMP67TKgjL7OTrACGgEmTf+zMvYRhoAoGCCqGSM49
|
||||
AwEHoUQDQgAESvX1TRh3NYeQeX6TjMhHcoYdfMTTXDVBkOYhizve2dCrSub0ZQ3n
|
||||
hNSNEmiYKqaZvThB8N9iVNBzIuBQWYtkbA==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -0,0 +1,7 @@
|
||||
port: 14225
|
||||
net: localhost
|
||||
|
||||
tls {
|
||||
cert_file: "./testdata/contrib/certs/nats_server_cert.pem"
|
||||
key_file: "./testdata/contrib/certs/nats_server_key.pem"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
port: 14226
|
||||
net: localhost
|
||||
|
||||
tls {
|
||||
cert_file: "./testdata/contrib/certs/nats_server_cert.pem"
|
||||
key_file: "./testdata/contrib/certs/nats_server_key.pem"
|
||||
ca_file: "./testdata/contrib/certs/root_ca_cert.pem"
|
||||
verify_and_map: true
|
||||
}
|
||||
authorization {
|
||||
ADMIN = {
|
||||
publish = ">"
|
||||
subscribe = ">"
|
||||
}
|
||||
users = [
|
||||
{user: "CN=localhost,OU=Client,O=MinIO,C=CA", permissions: $ADMIN}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/internal/event"
|
||||
xnet "github.com/minio/minio/internal/net"
|
||||
"github.com/minio/pkg/certs"
|
||||
)
|
||||
|
||||
// Webhook constants
|
||||
const (
|
||||
WebhookEndpoint = "endpoint"
|
||||
WebhookAuthToken = "auth_token"
|
||||
WebhookQueueDir = "queue_dir"
|
||||
WebhookQueueLimit = "queue_limit"
|
||||
WebhookClientCert = "client_cert"
|
||||
WebhookClientKey = "client_key"
|
||||
|
||||
EnvWebhookEnable = "MINIO_NOTIFY_WEBHOOK_ENABLE"
|
||||
EnvWebhookEndpoint = "MINIO_NOTIFY_WEBHOOK_ENDPOINT"
|
||||
EnvWebhookAuthToken = "MINIO_NOTIFY_WEBHOOK_AUTH_TOKEN"
|
||||
EnvWebhookQueueDir = "MINIO_NOTIFY_WEBHOOK_QUEUE_DIR"
|
||||
EnvWebhookQueueLimit = "MINIO_NOTIFY_WEBHOOK_QUEUE_LIMIT"
|
||||
EnvWebhookClientCert = "MINIO_NOTIFY_WEBHOOK_CLIENT_CERT"
|
||||
EnvWebhookClientKey = "MINIO_NOTIFY_WEBHOOK_CLIENT_KEY"
|
||||
)
|
||||
|
||||
// WebhookArgs - Webhook target arguments.
|
||||
type WebhookArgs struct {
|
||||
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"`
|
||||
ClientCert string `json:"clientCert"`
|
||||
ClientKey string `json:"clientKey"`
|
||||
}
|
||||
|
||||
// Validate WebhookArgs fields
|
||||
func (w WebhookArgs) Validate() error {
|
||||
if !w.Enable {
|
||||
return nil
|
||||
}
|
||||
if w.Endpoint.IsEmpty() {
|
||||
return errors.New("endpoint empty")
|
||||
}
|
||||
if w.QueueDir != "" {
|
||||
if !filepath.IsAbs(w.QueueDir) {
|
||||
return errors.New("queueDir path should be absolute")
|
||||
}
|
||||
}
|
||||
if w.ClientCert != "" && w.ClientKey == "" || w.ClientCert == "" && w.ClientKey != "" {
|
||||
return errors.New("cert and key must be specified as a pair")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WebhookTarget - Webhook target.
|
||||
type WebhookTarget struct {
|
||||
id event.TargetID
|
||||
args WebhookArgs
|
||||
httpClient *http.Client
|
||||
store Store
|
||||
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
||||
}
|
||||
|
||||
// ID - returns target ID.
|
||||
func (target WebhookTarget) ID() event.TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// HasQueueStore - Checks if the queueStore has been configured for the target
|
||||
func (target *WebhookTarget) HasQueueStore() bool {
|
||||
return target.store != nil
|
||||
}
|
||||
|
||||
// IsActive - Return true if target is up and active
|
||||
func (target *WebhookTarget) IsActive() (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, target.args.Endpoint.String(), nil)
|
||||
if err != nil {
|
||||
if xnet.IsNetworkOrHostDown(err, false) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := target.httpClient.Do(req)
|
||||
if err != nil {
|
||||
if xnet.IsNetworkOrHostDown(err, false) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return false, errNotConnected
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
// No network failure i.e response from the target means its up
|
||||
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.send(eventData)
|
||||
if err != nil {
|
||||
if xnet.IsNetworkOrHostDown(err, false) {
|
||||
return errNotConnected
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// send - sends an event to the webhook.
|
||||
func (target *WebhookTarget) send(eventData event.Event) error {
|
||||
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := eventData.S3.Bucket.Name + "/" + objectName
|
||||
|
||||
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", target.args.Endpoint.String(), bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if target.args.AuthToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+target.args.AuthToken)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := target.httpClient.Do(req)
|
||||
if err != nil {
|
||||
target.Close()
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
target.Close()
|
||||
return fmt.Errorf("sending event failed with %v", resp.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - reads an event from store and sends it to webhook.
|
||||
func (target *WebhookTarget) Send(eventKey string) error {
|
||||
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 {
|
||||
if xnet.IsNetworkOrHostDown(err, false) {
|
||||
return errNotConnected
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete the event from store.
|
||||
return target.store.Del(eventKey)
|
||||
}
|
||||
|
||||
// Close - does nothing and available for interface compatibility.
|
||||
func (target *WebhookTarget) Close() error {
|
||||
// Close idle connection with "keep-alive" states
|
||||
target.httpClient.CloseIdleConnections()
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewWebhookTarget - creates new Webhook target.
|
||||
func NewWebhookTarget(ctx context.Context, id string, args WebhookArgs, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), transport *http.Transport, test bool) (*WebhookTarget, error) {
|
||||
var store Store
|
||||
target := &WebhookTarget{
|
||||
id: event.TargetID{ID: id, Name: "webhook"},
|
||||
args: args,
|
||||
loggerOnce: loggerOnce,
|
||||
}
|
||||
|
||||
if target.args.ClientCert != "" && target.args.ClientKey != "" {
|
||||
manager, err := certs.NewManager(ctx, target.args.ClientCert, target.args.ClientKey, tls.LoadX509KeyPair)
|
||||
if err != nil {
|
||||
return target, err
|
||||
}
|
||||
transport.TLSClientConfig.GetClientCertificate = manager.GetClientCertificate
|
||||
}
|
||||
target.httpClient = &http.Client{Transport: transport}
|
||||
|
||||
if args.QueueDir != "" {
|
||||
queueDir := filepath.Join(args.QueueDir, storePrefix+"-webhook-"+id)
|
||||
store = NewQueueStore(queueDir, args.QueueLimit)
|
||||
if err := store.Open(); err != nil {
|
||||
target.loggerOnce(context.Background(), err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
target.store = store
|
||||
}
|
||||
|
||||
_, err := target.IsActive()
|
||||
if err != nil {
|
||||
if target.store == nil || err != errNotConnected {
|
||||
target.loggerOnce(ctx, err, target.ID())
|
||||
return target, err
|
||||
}
|
||||
}
|
||||
|
||||
if target.store != nil && !test {
|
||||
// Replays the events from the store.
|
||||
eventKeyCh := replayEvents(target.store, ctx.Done(), target.loggerOnce, target.ID())
|
||||
// Start replaying events from the store.
|
||||
go sendEvents(target, eventKeyCh, ctx.Done(), target.loggerOnce)
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TargetID - holds identification and name strings of notification target.
|
||||
type TargetID struct {
|
||||
ID string
|
||||
Name string
|
||||
}
|
||||
|
||||
// String - returns string representation.
|
||||
func (tid TargetID) String() string {
|
||||
return tid.ID + ":" + tid.Name
|
||||
}
|
||||
|
||||
// ToARN - converts to ARN.
|
||||
func (tid TargetID) ToARN(region string) ARN {
|
||||
return ARN{TargetID: tid, region: region}
|
||||
}
|
||||
|
||||
// MarshalJSON - encodes to JSON data.
|
||||
func (tid TargetID) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(tid.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON - decodes JSON data.
|
||||
func (tid *TargetID) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetID, err := parseTargetID(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*tid = *targetID
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseTargetID - parses string to TargetID.
|
||||
func parseTargetID(s string) (*TargetID, error) {
|
||||
tokens := strings.Split(s, ":")
|
||||
if len(tokens) != 2 {
|
||||
return nil, fmt.Errorf("invalid TargetID format '%v'", s)
|
||||
}
|
||||
|
||||
return &TargetID{
|
||||
ID: tokens[0],
|
||||
Name: tokens[1],
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTargetDString(t *testing.T) {
|
||||
testCases := []struct {
|
||||
tid TargetID
|
||||
expectedResult string
|
||||
}{
|
||||
{TargetID{}, ":"},
|
||||
{TargetID{"1", "webhook"}, "1:webhook"},
|
||||
{TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, "httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.tid.String()
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetDToARN(t *testing.T) {
|
||||
tid := TargetID{"1", "webhook"}
|
||||
testCases := []struct {
|
||||
tid TargetID
|
||||
region string
|
||||
expectedARN ARN
|
||||
}{
|
||||
{tid, "", ARN{TargetID: tid, region: ""}},
|
||||
{tid, "us-east-1", ARN{TargetID: tid, region: "us-east-1"}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
arn := testCase.tid.ToARN(testCase.region)
|
||||
|
||||
if arn != testCase.expectedARN {
|
||||
t.Fatalf("test %v: ARN: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetDMarshalJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
tid TargetID
|
||||
expectedData []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{TargetID{}, []byte(`":"`), false},
|
||||
{TargetID{"1", "webhook"}, []byte(`"1:webhook"`), false},
|
||||
{TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, []byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
data, err := testCase.tid.MarshalJSON()
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(data, testCase.expectedData) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetDUnmarshalJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
data []byte
|
||||
expectedTargetID *TargetID
|
||||
expectErr bool
|
||||
}{
|
||||
{[]byte(`""`), nil, true},
|
||||
{[]byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), nil, true},
|
||||
{[]byte(`":"`), &TargetID{}, false},
|
||||
{[]byte(`"1:webhook"`), &TargetID{"1", "webhook"}, false},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
targetID := &TargetID{}
|
||||
err := targetID.UnmarshalJSON(testCase.data)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
if *targetID != *testCase.expectedTargetID {
|
||||
t.Fatalf("test %v: TargetID: expected: %v, got: %v", i+1, testCase.expectedTargetID, targetID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
// TargetIDSet - Set representation of TargetIDs.
|
||||
type TargetIDSet map[TargetID]struct{}
|
||||
|
||||
// IsEmpty returns true if the set is empty.
|
||||
func (set TargetIDSet) IsEmpty() bool {
|
||||
return len(set) != 0
|
||||
}
|
||||
|
||||
// Clone - returns copy of this set.
|
||||
func (set TargetIDSet) Clone() TargetIDSet {
|
||||
setCopy := NewTargetIDSet()
|
||||
for k, v := range set {
|
||||
setCopy[k] = v
|
||||
}
|
||||
return setCopy
|
||||
}
|
||||
|
||||
// add - adds TargetID to the set.
|
||||
func (set TargetIDSet) add(targetID TargetID) {
|
||||
set[targetID] = struct{}{}
|
||||
}
|
||||
|
||||
// Union - returns union with given set as new set.
|
||||
func (set TargetIDSet) Union(sset TargetIDSet) TargetIDSet {
|
||||
nset := set.Clone()
|
||||
|
||||
for k := range sset {
|
||||
nset.add(k)
|
||||
}
|
||||
|
||||
return nset
|
||||
}
|
||||
|
||||
// Difference - returns diffrence with given set as new set.
|
||||
func (set TargetIDSet) Difference(sset TargetIDSet) TargetIDSet {
|
||||
nset := NewTargetIDSet()
|
||||
for k := range set {
|
||||
if _, ok := sset[k]; !ok {
|
||||
nset.add(k)
|
||||
}
|
||||
}
|
||||
|
||||
return nset
|
||||
}
|
||||
|
||||
// NewTargetIDSet - creates new TargetID set with given TargetIDs.
|
||||
func NewTargetIDSet(targetIDs ...TargetID) TargetIDSet {
|
||||
set := make(TargetIDSet)
|
||||
for _, targetID := range targetIDs {
|
||||
set.add(targetID)
|
||||
}
|
||||
return set
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTargetIDSetClone(t *testing.T) {
|
||||
testCases := []struct {
|
||||
set TargetIDSet
|
||||
targetIDToAdd TargetID
|
||||
}{
|
||||
{NewTargetIDSet(), TargetID{"1", "webhook"}},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), TargetID{"2", "webhook"}},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}, TargetID{"2", "amqp"}), TargetID{"2", "webhook"}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.set.Clone()
|
||||
|
||||
if !reflect.DeepEqual(result, testCase.set) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.set, result)
|
||||
}
|
||||
|
||||
result.add(testCase.targetIDToAdd)
|
||||
if reflect.DeepEqual(result, testCase.set) {
|
||||
t.Fatalf("test %v: result: expected: not equal, got: equal", i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetIDSetUnion(t *testing.T) {
|
||||
testCases := []struct {
|
||||
set TargetIDSet
|
||||
setToAdd TargetIDSet
|
||||
expectedResult TargetIDSet
|
||||
}{
|
||||
{NewTargetIDSet(), NewTargetIDSet(), NewTargetIDSet()},
|
||||
{NewTargetIDSet(), NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(), NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"2", "amqp"}), NewTargetIDSet(TargetID{"1", "webhook"}, TargetID{"2", "amqp"})},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.set.Union(testCase.setToAdd)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetIDSetDifference(t *testing.T) {
|
||||
testCases := []struct {
|
||||
set TargetIDSet
|
||||
setToRemove TargetIDSet
|
||||
expectedResult TargetIDSet
|
||||
}{
|
||||
{NewTargetIDSet(), NewTargetIDSet(), NewTargetIDSet()},
|
||||
{NewTargetIDSet(), NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet()},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(), NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"2", "amqp"}), NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet(TargetID{"1", "webhook"}), NewTargetIDSet()},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.set.Difference(testCase.setToRemove)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTargetIDSet(t *testing.T) {
|
||||
testCases := []struct {
|
||||
targetIDs []TargetID
|
||||
expectedResult TargetIDSet
|
||||
}{
|
||||
{[]TargetID{}, NewTargetIDSet()},
|
||||
{[]TargetID{{"1", "webhook"}}, NewTargetIDSet(TargetID{"1", "webhook"})},
|
||||
{[]TargetID{{"1", "webhook"}, {"2", "amqp"}}, NewTargetIDSet(TargetID{"1", "webhook"}, TargetID{"2", "amqp"})},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := NewTargetIDSet(testCase.targetIDs...)
|
||||
|
||||
if !reflect.DeepEqual(testCase.expectedResult, result) {
|
||||
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Target - event target interface
|
||||
type Target interface {
|
||||
ID() TargetID
|
||||
IsActive() (bool, error)
|
||||
Save(Event) error
|
||||
Send(string) error
|
||||
Close() error
|
||||
HasQueueStore() bool
|
||||
}
|
||||
|
||||
// TargetList - holds list of targets indexed by target ID.
|
||||
type TargetList struct {
|
||||
sync.RWMutex
|
||||
targets map[TargetID]Target
|
||||
}
|
||||
|
||||
// Add - adds unique target to target list.
|
||||
func (list *TargetList) Add(targets ...Target) error {
|
||||
list.Lock()
|
||||
defer list.Unlock()
|
||||
|
||||
for _, target := range targets {
|
||||
if _, ok := list.targets[target.ID()]; ok {
|
||||
return fmt.Errorf("target %v already exists", target.ID())
|
||||
}
|
||||
list.targets[target.ID()] = target
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exists - checks whether target by target ID exists or not.
|
||||
func (list *TargetList) Exists(id TargetID) bool {
|
||||
list.RLock()
|
||||
defer list.RUnlock()
|
||||
|
||||
_, found := list.targets[id]
|
||||
return found
|
||||
}
|
||||
|
||||
// TargetIDResult returns result of Remove/Send operation, sets err if
|
||||
// any for the associated TargetID
|
||||
type TargetIDResult struct {
|
||||
// ID where the remove or send were initiated.
|
||||
ID TargetID
|
||||
// Stores any error while removing a target or while sending an event.
|
||||
Err error
|
||||
}
|
||||
|
||||
// Remove - closes and removes targets by given target IDs.
|
||||
func (list *TargetList) Remove(targetIDSet TargetIDSet) {
|
||||
list.Lock()
|
||||
defer list.Unlock()
|
||||
|
||||
for id := range targetIDSet {
|
||||
target, ok := list.targets[id]
|
||||
if ok {
|
||||
target.Close()
|
||||
delete(list.targets, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Targets - list all targets
|
||||
func (list *TargetList) Targets() []Target {
|
||||
if list == nil {
|
||||
return []Target{}
|
||||
}
|
||||
|
||||
list.RLock()
|
||||
defer list.RUnlock()
|
||||
|
||||
targets := []Target{}
|
||||
for _, tgt := range list.targets {
|
||||
targets = append(targets, tgt)
|
||||
}
|
||||
|
||||
return targets
|
||||
}
|
||||
|
||||
// List - returns available target IDs.
|
||||
func (list *TargetList) List() []TargetID {
|
||||
list.RLock()
|
||||
defer list.RUnlock()
|
||||
|
||||
keys := []TargetID{}
|
||||
for k := range list.targets {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// TargetMap - returns available targets.
|
||||
func (list *TargetList) TargetMap() map[TargetID]Target {
|
||||
list.RLock()
|
||||
defer list.RUnlock()
|
||||
return list.targets
|
||||
}
|
||||
|
||||
// Send - sends events to targets identified by target IDs.
|
||||
func (list *TargetList) Send(event Event, targetIDset TargetIDSet, resCh chan<- TargetIDResult) {
|
||||
go func() {
|
||||
var wg sync.WaitGroup
|
||||
for id := range targetIDset {
|
||||
list.RLock()
|
||||
target, ok := list.targets[id]
|
||||
list.RUnlock()
|
||||
if ok {
|
||||
wg.Add(1)
|
||||
go func(id TargetID, target Target) {
|
||||
defer wg.Done()
|
||||
tgtRes := TargetIDResult{ID: id}
|
||||
if err := target.Save(event); err != nil {
|
||||
tgtRes.Err = err
|
||||
}
|
||||
resCh <- tgtRes
|
||||
}(id, target)
|
||||
} else {
|
||||
resCh <- TargetIDResult{ID: id}
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
}()
|
||||
}
|
||||
|
||||
// NewTargetList - creates TargetList.
|
||||
func NewTargetList() *TargetList {
|
||||
return &TargetList{targets: make(map[TargetID]Target)}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ExampleTarget struct {
|
||||
id TargetID
|
||||
sendErr bool
|
||||
closeErr bool
|
||||
}
|
||||
|
||||
func (target ExampleTarget) ID() TargetID {
|
||||
return target.id
|
||||
}
|
||||
|
||||
// Save - Sends event directly without persisting.
|
||||
func (target ExampleTarget) Save(eventData Event) error {
|
||||
return target.send(eventData)
|
||||
}
|
||||
|
||||
func (target ExampleTarget) send(eventData Event) error {
|
||||
b := make([]byte, 1)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(b[0]) * time.Millisecond)
|
||||
|
||||
if target.sendErr {
|
||||
return errors.New("send error")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send - interface compatible method does no-op.
|
||||
func (target ExampleTarget) Send(eventKey string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (target ExampleTarget) Close() error {
|
||||
if target.closeErr {
|
||||
return errors.New("close error")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (target ExampleTarget) IsActive() (bool, error) {
|
||||
return false, errors.New("not connected to target server/service")
|
||||
}
|
||||
|
||||
// HasQueueStore - No-Op. Added for interface compatibility
|
||||
func (target ExampleTarget) HasQueueStore() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func TestTargetListAdd(t *testing.T) {
|
||||
targetListCase1 := NewTargetList()
|
||||
|
||||
targetListCase2 := NewTargetList()
|
||||
if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetListCase3 := NewTargetList()
|
||||
if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
targetList *TargetList
|
||||
target Target
|
||||
expectedResult []TargetID
|
||||
expectErr bool
|
||||
}{
|
||||
{targetListCase1, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"1", "webhook"}}, false},
|
||||
{targetListCase2, &ExampleTarget{TargetID{"1", "webhook"}, false, false}, []TargetID{{"2", "testcase"}, {"1", "webhook"}}, false},
|
||||
{targetListCase3, &ExampleTarget{TargetID{"3", "testcase"}, false, false}, nil, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
err := testCase.targetList.Add(testCase.target)
|
||||
expectErr := (err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
|
||||
if !testCase.expectErr {
|
||||
result := testCase.targetList.List()
|
||||
|
||||
if len(result) != len(testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
|
||||
for _, targetID1 := range result {
|
||||
var found bool
|
||||
for _, targetID2 := range testCase.expectedResult {
|
||||
if reflect.DeepEqual(targetID1, targetID2) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetListExists(t *testing.T) {
|
||||
targetListCase1 := NewTargetList()
|
||||
|
||||
targetListCase2 := NewTargetList()
|
||||
if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetListCase3 := NewTargetList()
|
||||
if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
targetList *TargetList
|
||||
targetID TargetID
|
||||
expectedResult bool
|
||||
}{
|
||||
{targetListCase1, TargetID{"1", "webhook"}, false},
|
||||
{targetListCase2, TargetID{"1", "webhook"}, false},
|
||||
{targetListCase3, TargetID{"3", "testcase"}, true},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.targetList.Exists(testCase.targetID)
|
||||
|
||||
if result != testCase.expectedResult {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetListList(t *testing.T) {
|
||||
targetListCase1 := NewTargetList()
|
||||
|
||||
targetListCase2 := NewTargetList()
|
||||
if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetListCase3 := NewTargetList()
|
||||
if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := targetListCase3.Add(&ExampleTarget{TargetID{"1", "webhook"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
targetList *TargetList
|
||||
expectedResult []TargetID
|
||||
}{
|
||||
{targetListCase1, []TargetID{}},
|
||||
{targetListCase2, []TargetID{{"2", "testcase"}}},
|
||||
{targetListCase3, []TargetID{{"3", "testcase"}, {"1", "webhook"}}},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
result := testCase.targetList.List()
|
||||
|
||||
if len(result) != len(testCase.expectedResult) {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
|
||||
for _, targetID1 := range result {
|
||||
var found bool
|
||||
for _, targetID2 := range testCase.expectedResult {
|
||||
if reflect.DeepEqual(targetID1, targetID2) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetListSend(t *testing.T) {
|
||||
targetListCase1 := NewTargetList()
|
||||
|
||||
targetListCase2 := NewTargetList()
|
||||
if err := targetListCase2.Add(&ExampleTarget{TargetID{"2", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetListCase3 := NewTargetList()
|
||||
if err := targetListCase3.Add(&ExampleTarget{TargetID{"3", "testcase"}, false, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
targetListCase4 := NewTargetList()
|
||||
if err := targetListCase4.Add(&ExampleTarget{TargetID{"4", "testcase"}, true, false}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
targetList *TargetList
|
||||
targetID TargetID
|
||||
expectErr bool
|
||||
}{
|
||||
{targetListCase1, TargetID{"1", "webhook"}, false},
|
||||
{targetListCase2, TargetID{"1", "non-existent"}, false},
|
||||
{targetListCase3, TargetID{"3", "testcase"}, false},
|
||||
{targetListCase4, TargetID{"4", "testcase"}, true},
|
||||
}
|
||||
|
||||
resCh := make(chan TargetIDResult)
|
||||
for i, testCase := range testCases {
|
||||
testCase.targetList.Send(Event{}, map[TargetID]struct{}{
|
||||
testCase.targetID: {},
|
||||
}, resCh)
|
||||
res := <-resCh
|
||||
expectErr := (res.Err != nil)
|
||||
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTargetList(t *testing.T) {
|
||||
if result := NewTargetList(); result == nil {
|
||||
t.Fatalf("test: result: expected: <non-nil>, got: <nil>")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user