mirror of
https://github.com/minio/minio.git
synced 2025-01-11 23:13:23 -05:00
Cleanup: Moving IsValidLocationContraint to handler utils
This commit is contained in:
parent
a1a667ae5d
commit
8deddb82f4
54
handler-utils.go
Normal file
54
handler-utils.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// validates location constraint from the request body.
|
||||||
|
// the location value in the request body should match the Region in serverConfig.
|
||||||
|
// other values of location are not accepted.
|
||||||
|
// make bucket fails in such cases.
|
||||||
|
func isValidLocationContraint(reqBody io.Reader, serverRegion string) APIErrorCode {
|
||||||
|
var locationContraint createBucketLocationConfiguration
|
||||||
|
var errCode APIErrorCode
|
||||||
|
errCode = ErrNone
|
||||||
|
e := xmlDecoder(reqBody, &locationContraint)
|
||||||
|
if e != nil {
|
||||||
|
if e == io.EOF {
|
||||||
|
// Do nothing.
|
||||||
|
// failed due to empty body. The location will be set to default value from the serverConfig.
|
||||||
|
// this is valid.
|
||||||
|
errCode = ErrNone
|
||||||
|
} else {
|
||||||
|
// Failed due to malformed configuration.
|
||||||
|
errCode = ErrMalformedXML
|
||||||
|
//writeErrorResponse(w, r, ErrMalformedXML, r.URL.Path)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Region obtained from the body.
|
||||||
|
// It should be equal to Region in serverConfig.
|
||||||
|
// Else ErrInvalidRegion returned.
|
||||||
|
// For empty value location will be to set to default value from the serverConfig.
|
||||||
|
if locationContraint.Location != "" && serverRegion != locationContraint.Location {
|
||||||
|
//writeErrorResponse(w, r, ErrInvalidRegion, r.URL.Path)
|
||||||
|
errCode = ErrInvalidRegion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errCode
|
||||||
|
}
|
66
handler-utils_test.go
Normal file
66
handler-utils_test.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tests validate bucket LocationConstraint.
|
||||||
|
func TestIsValidLocationContraint(t *testing.T) {
|
||||||
|
// generates the input request with XML bucket configuration set to the request body.
|
||||||
|
createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) {
|
||||||
|
createBucketConfig := createBucketLocationConfiguration{}
|
||||||
|
createBucketConfig.Location = location
|
||||||
|
var createBucketConfigBytes []byte
|
||||||
|
createBucketConfigBytes, e := xml.Marshal(createBucketConfig)
|
||||||
|
if e != nil {
|
||||||
|
return nil, e
|
||||||
|
}
|
||||||
|
createBucketConfigBuffer := bytes.NewBuffer(createBucketConfigBytes)
|
||||||
|
req.Body = ioutil.NopCloser(createBucketConfigBuffer)
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
locationForInputRequest string
|
||||||
|
serverConfigRegion string
|
||||||
|
expectedCode APIErrorCode
|
||||||
|
}{
|
||||||
|
// Test case - 1.
|
||||||
|
{"us-east-1", "us-east-1", ErrNone},
|
||||||
|
// Test case - 2.
|
||||||
|
// In case of empty request body ErrNone is returned.
|
||||||
|
{"", "us-east-1", ErrNone},
|
||||||
|
// Test case - 3.
|
||||||
|
{"eu-central-1", "us-east-1", ErrInvalidRegion},
|
||||||
|
}
|
||||||
|
for i, testCase := range testCases {
|
||||||
|
inputRequest, e := createExpectedRequest(&http.Request{}, testCase.locationForInputRequest)
|
||||||
|
if e != nil {
|
||||||
|
t.Fatalf("Test %d: Failed to Marshal bucket configuration", i+1)
|
||||||
|
}
|
||||||
|
actualCode := isValidLocationContraint(inputRequest.Body, testCase.serverConfigRegion)
|
||||||
|
if testCase.expectedCode != actualCode {
|
||||||
|
t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,7 +17,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@ -97,36 +96,3 @@ func retainSlash(s string) string {
|
|||||||
func pathJoin(s1 string, s2 string) string {
|
func pathJoin(s1 string, s2 string) string {
|
||||||
return retainSlash(s1) + s2
|
return retainSlash(s1) + s2
|
||||||
}
|
}
|
||||||
|
|
||||||
// validates location constraint from the request body.
|
|
||||||
// the location value in the request body should match the Region in serverConfig.
|
|
||||||
// other values of location are not accepted.
|
|
||||||
// make bucket fails in such cases.
|
|
||||||
func isValidLocationContraint(reqBody io.Reader, serverRegion string) APIErrorCode {
|
|
||||||
var locationContraint createBucketLocationConfiguration
|
|
||||||
var errCode APIErrorCode
|
|
||||||
errCode = ErrNone
|
|
||||||
e := xmlDecoder(reqBody, &locationContraint)
|
|
||||||
if e != nil {
|
|
||||||
if e == io.EOF {
|
|
||||||
// Do nothing.
|
|
||||||
// failed due to empty body. The location will be set to default value from the serverConfig.
|
|
||||||
// this is valid.
|
|
||||||
errCode = ErrNone
|
|
||||||
} else {
|
|
||||||
// Failed due to malformed configuration.
|
|
||||||
errCode = ErrMalformedXML
|
|
||||||
//writeErrorResponse(w, r, ErrMalformedXML, r.URL.Path)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Region obtained from the body.
|
|
||||||
// It should be equal to Region in serverConfig.
|
|
||||||
// Else ErrInvalidRegion returned.
|
|
||||||
// For empty value location will be to set to default value from the serverConfig.
|
|
||||||
if locationContraint.Location != "" && serverRegion != locationContraint.Location {
|
|
||||||
//writeErrorResponse(w, r, ErrInvalidRegion, r.URL.Path)
|
|
||||||
errCode = ErrInvalidRegion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errCode
|
|
||||||
}
|
|
||||||
|
@ -17,10 +17,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/xml"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -107,44 +103,3 @@ func TestIsValidObjectName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests validate bucket LocationConstraint.
|
|
||||||
func TestIsValidLocationContraint(t *testing.T) {
|
|
||||||
// generates the input request with XML bucket configuration set to the request body.
|
|
||||||
createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) {
|
|
||||||
createBucketConfig := createBucketLocationConfiguration{}
|
|
||||||
createBucketConfig.Location = location
|
|
||||||
var createBucketConfigBytes []byte
|
|
||||||
createBucketConfigBytes, e := xml.Marshal(createBucketConfig)
|
|
||||||
if e != nil {
|
|
||||||
return nil, e
|
|
||||||
}
|
|
||||||
createBucketConfigBuffer := bytes.NewBuffer(createBucketConfigBytes)
|
|
||||||
req.Body = ioutil.NopCloser(createBucketConfigBuffer)
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
locationForInputRequest string
|
|
||||||
serverConfigRegion string
|
|
||||||
expectedCode APIErrorCode
|
|
||||||
}{
|
|
||||||
// Test case - 1.
|
|
||||||
{"us-east-1", "us-east-1", ErrNone},
|
|
||||||
// Test case - 2.
|
|
||||||
// In case of empty request body ErrNone is returned.
|
|
||||||
{"", "us-east-1", ErrNone},
|
|
||||||
// Test case - 3.
|
|
||||||
{"eu-central-1", "us-east-1", ErrInvalidRegion},
|
|
||||||
}
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
inputRequest, e := createExpectedRequest(&http.Request{}, testCase.locationForInputRequest)
|
|
||||||
if e != nil {
|
|
||||||
t.Fatalf("Test %d: Failed to Marshal bucket configuration", i+1)
|
|
||||||
}
|
|
||||||
actualCode := isValidLocationContraint(inputRequest.Body, testCase.serverConfigRegion)
|
|
||||||
if testCase.expectedCode != actualCode {
|
|
||||||
t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user