Listen bucket notification for multiple prefixes/suffixes (#2911)

* Listen bucket notification for multiple prefixes/suffixes

* After review fixes
master
Mateusz Gajewski 8 years ago committed by Harshavardhana
parent 6199aa0707
commit 73982c8cb6
  1. 16
      cmd/api-resources.go
  2. 36
      cmd/api-resources_test.go
  3. 18
      cmd/bucket-notification-handlers.go
  4. 16
      cmd/bucket-notification-handlers_test.go
  5. 7
      cmd/test-utils_test.go

@ -80,9 +80,19 @@ func getObjectResources(values url.Values) (uploadID string, partNumberMarker, m
}
// Parse listen bucket notification resources.
func getListenBucketNotificationResources(values url.Values) (prefix string, suffix string, events []string) {
prefix = values.Get("prefix")
suffix = values.Get("suffix")
func getListenBucketNotificationResources(values url.Values) (prefix []string, suffix []string, events []string) {
prefix = values["prefix"]
suffix = values["suffix"]
events = values["events"]
return prefix, suffix, events
}
// Validates filter values
func validateFilterValues(values []string) (err APIErrorCode) {
for _, value := range values {
if !IsValidObjectPrefix(value) {
return ErrFilterValueInvalid
}
}
return ErrNone
}

@ -18,6 +18,7 @@ package cmd
import (
"net/url"
"strings"
"testing"
)
@ -188,3 +189,38 @@ func TestGetObjectsResources(t *testing.T) {
}
}
}
// Validates if filter values are correct
func TestValidateFilterValues(t *testing.T) {
testCases := []struct {
values []string
expectedError APIErrorCode
}{
{
values: []string{""},
expectedError: ErrNone,
},
{
values: []string{"", "prefix"},
expectedError: ErrNone,
},
{
values: []string{strings.Repeat("a", 1025)},
expectedError: ErrFilterValueInvalid,
},
{
values: []string{"a\\b"},
expectedError: ErrFilterValueInvalid,
},
{
values: []string{string([]byte{0xff, 0xfe, 0xfd})},
expectedError: ErrFilterValueInvalid,
},
}
for i, testCase := range testCases {
if actualError := validateFilterValues(testCase.values); actualError != testCase.expectedError {
t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.expectedError, actualError)
}
}
}

@ -250,9 +250,15 @@ func (api objectAPIHandlers) ListenBucketNotificationHandler(w http.ResponseWrit
bucket := vars["bucket"]
// Parse listen bucket notification resources.
prefix, suffix, events := getListenBucketNotificationResources(r.URL.Query())
if !IsValidObjectPrefix(prefix) || !IsValidObjectPrefix(suffix) {
writeErrorResponse(w, r, ErrFilterValueInvalid, r.URL.Path)
prefixes, suffixes, events := getListenBucketNotificationResources(r.URL.Query())
if err := validateFilterValues(prefixes); err != ErrNone {
writeErrorResponse(w, r, err, r.URL.Path)
return
}
if err := validateFilterValues(suffixes); err != ErrNone {
writeErrorResponse(w, r, err, r.URL.Path)
return
}
@ -279,13 +285,15 @@ func (api objectAPIHandlers) ListenBucketNotificationHandler(w http.ResponseWrit
globalMinioAddr,
)
var filterRules []filterRule
if prefix != "" {
for _, prefix := range prefixes {
filterRules = append(filterRules, filterRule{
Name: "prefix",
Value: prefix,
})
}
if suffix != "" {
for _, suffix := range suffixes {
filterRules = append(filterRules, filterRule{
Name: "suffix",
Value: suffix,

@ -235,24 +235,24 @@ func testListenBucketNotificationHandler(obj ObjectLayer, instanceType string, t
invalidEvents := []string{"invalidEvent"}
testCases := []struct {
bucketName string
prefix string
suffix string
prefixes []string
suffixes []string
events []string
kind testKind
expectedHTTPCode int
expectedAPIError string
}{
// FIXME: Need to find a way to run valid listen bucket notification test case without blocking the unit test.
{randBucket, "", "", invalidEvents, CheckStatus, signatureMismatchError.HTTPStatusCode, ""},
{randBucket, tooBigPrefix, "", validEvents, CheckStatus, http.StatusBadRequest, ""},
{invalidBucket, "", "", validEvents, CheckStatus, http.StatusBadRequest, ""},
{randBucket, "", "", validEvents, InvalidAuth, signatureMismatchError.HTTPStatusCode, signatureMismatchError.Code},
{randBucket, []string{}, []string{}, invalidEvents, CheckStatus, signatureMismatchError.HTTPStatusCode, ""},
{randBucket, []string{tooBigPrefix}, []string{}, validEvents, CheckStatus, http.StatusBadRequest, ""},
{invalidBucket, []string{}, []string{}, validEvents, CheckStatus, http.StatusBadRequest, ""},
{randBucket, []string{}, []string{}, validEvents, InvalidAuth, signatureMismatchError.HTTPStatusCode, signatureMismatchError.Code},
}
for i, test := range testCases {
testRec = httptest.NewRecorder()
testReq, tErr = newTestSignedRequestV4("GET",
getListenBucketNotificationURL("", test.bucketName, test.prefix, test.suffix, test.events),
getListenBucketNotificationURL("", test.bucketName, test.prefixes, test.suffixes, test.events),
0, nil, credentials.AccessKeyID, credentials.SecretAccessKey)
if tErr != nil {
t.Fatalf("%s: Failed to create HTTP testRequest for ListenBucketNotification: <ERROR> %v", instanceType, tErr)
@ -301,7 +301,7 @@ func testListenBucketNotificationHandler(obj ObjectLayer, instanceType string, t
})
testRec = httptest.NewRecorder()
testReq, tErr = newTestSignedRequestV4("GET",
getListenBucketNotificationURL("", randBucket, "", "*.jpg", []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"}),
getListenBucketNotificationURL("", randBucket, []string{}, []string{"*.jpg"}, []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"}),
0, nil, credentials.AccessKeyID, credentials.SecretAccessKey)
if tErr != nil {
t.Fatalf("%s: Failed to create HTTP testRequest for ListenBucketNotification: <ERROR> %v", instanceType, tErr)

@ -1402,10 +1402,11 @@ func getGetBucketNotificationURL(endPoint, bucketName string) string {
}
// return URL for listen bucket notification.
func getListenBucketNotificationURL(endPoint, bucketName, prefix, suffix string, events []string) string {
func getListenBucketNotificationURL(endPoint, bucketName string, prefixes, suffixes, events []string) string {
queryValue := url.Values{}
queryValue.Set("prefix", prefix)
queryValue.Set("suffix", suffix)
queryValue["prefix"] = prefixes
queryValue["suffix"] = suffixes
queryValue["events"] = events
return makeTestTargetURL(endPoint, bucketName, "", queryValue)
}

Loading…
Cancel
Save