2016-07-19 02:56:27 -04:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-07-19 02:56:27 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-07-19 02:56:27 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-09-16 18:17:49 -04:00
|
|
|
"net/url"
|
2017-08-12 22:25:43 -04:00
|
|
|
"os"
|
2016-07-19 02:56:27 -04:00
|
|
|
"testing"
|
2017-10-31 14:54:32 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
2016-07-19 02:56:27 -04:00
|
|
|
)
|
|
|
|
|
2016-09-16 18:17:49 -04:00
|
|
|
// Test get request auth type.
|
|
|
|
func TestGetRequestAuthType(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
req *http.Request
|
|
|
|
authT authType
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
|
|
|
// Test case - 1
|
|
|
|
// Check for generic signature v4 header.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
URL: &url.URL{
|
2017-02-10 02:26:44 -05:00
|
|
|
Host: "127.0.0.1:9000",
|
2017-01-18 15:24:34 -05:00
|
|
|
Scheme: httpScheme,
|
2016-09-16 18:17:49 -04:00
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
Header: http.Header{
|
|
|
|
"Authorization": []string{"AWS4-HMAC-SHA256 <cred_string>"},
|
|
|
|
"X-Amz-Content-Sha256": []string{streamingContentSHA256},
|
2017-02-20 15:07:03 -05:00
|
|
|
"Content-Encoding": []string{streamingContentEncoding},
|
2016-09-16 18:17:49 -04:00
|
|
|
},
|
|
|
|
Method: "PUT",
|
|
|
|
},
|
|
|
|
authT: authTypeStreamingSigned,
|
|
|
|
},
|
|
|
|
// Test case - 2
|
|
|
|
// Check for JWT header.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
URL: &url.URL{
|
2017-02-10 02:26:44 -05:00
|
|
|
Host: "127.0.0.1:9000",
|
2017-01-18 15:24:34 -05:00
|
|
|
Scheme: httpScheme,
|
2016-09-16 18:17:49 -04:00
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
Header: http.Header{
|
|
|
|
"Authorization": []string{"Bearer 12313123"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
authT: authTypeJWT,
|
|
|
|
},
|
|
|
|
// Test case - 3
|
|
|
|
// Empty authorization header.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
URL: &url.URL{
|
2017-02-10 02:26:44 -05:00
|
|
|
Host: "127.0.0.1:9000",
|
2017-01-18 15:24:34 -05:00
|
|
|
Scheme: httpScheme,
|
2016-09-16 18:17:49 -04:00
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
Header: http.Header{
|
|
|
|
"Authorization": []string{""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
authT: authTypeUnknown,
|
|
|
|
},
|
|
|
|
// Test case - 4
|
|
|
|
// Check for presigned.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
URL: &url.URL{
|
2017-02-10 02:26:44 -05:00
|
|
|
Host: "127.0.0.1:9000",
|
2017-01-18 15:24:34 -05:00
|
|
|
Scheme: httpScheme,
|
2016-09-16 18:17:49 -04:00
|
|
|
Path: "/",
|
|
|
|
RawQuery: "X-Amz-Credential=EXAMPLEINVALIDEXAMPL%2Fs3%2F20160314%2Fus-east-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
authT: authTypePresigned,
|
|
|
|
},
|
|
|
|
// Test case - 5
|
|
|
|
// Check for post policy.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
URL: &url.URL{
|
2017-02-10 02:26:44 -05:00
|
|
|
Host: "127.0.0.1:9000",
|
2017-01-18 15:24:34 -05:00
|
|
|
Scheme: httpScheme,
|
2016-09-16 18:17:49 -04:00
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
Header: http.Header{
|
|
|
|
"Content-Type": []string{"multipart/form-data"},
|
|
|
|
},
|
|
|
|
Method: "POST",
|
|
|
|
},
|
|
|
|
authT: authTypePostPolicy,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// .. Tests all request auth type.
|
|
|
|
for i, testc := range testCases {
|
|
|
|
authT := getRequestAuthType(testc.req)
|
|
|
|
if authT != testc.authT {
|
|
|
|
t.Errorf("Test %d: Expected %d, got %d", i+1, testc.authT, authT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
// Test all s3 supported auth types.
|
|
|
|
func TestS3SupportedAuthType(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
authT authType
|
|
|
|
pass bool
|
|
|
|
}
|
|
|
|
// List of all valid and invalid test cases.
|
|
|
|
testCases := []testCase{
|
|
|
|
// Test 1 - supported s3 type anonymous.
|
|
|
|
{
|
|
|
|
authT: authTypeAnonymous,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 2 - supported s3 type presigned.
|
|
|
|
{
|
|
|
|
authT: authTypePresigned,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 3 - supported s3 type signed.
|
|
|
|
{
|
|
|
|
authT: authTypeSigned,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 4 - supported s3 type with post policy.
|
|
|
|
{
|
|
|
|
authT: authTypePostPolicy,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 5 - supported s3 type with streaming signed.
|
|
|
|
{
|
|
|
|
authT: authTypeStreamingSigned,
|
|
|
|
pass: true,
|
|
|
|
},
|
2016-09-30 17:32:13 -04:00
|
|
|
// Test 6 - supported s3 type with signature v2.
|
|
|
|
{
|
|
|
|
authT: authTypeSignedV2,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 7 - supported s3 type with presign v2.
|
|
|
|
{
|
|
|
|
authT: authTypePresignedV2,
|
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
// Test 8 - JWT is not supported s3 type.
|
2016-08-08 23:56:29 -04:00
|
|
|
{
|
|
|
|
authT: authTypeJWT,
|
|
|
|
pass: false,
|
|
|
|
},
|
2016-09-30 17:32:13 -04:00
|
|
|
// Test 9 - unknown auth header is not supported s3 type.
|
2016-08-08 23:56:29 -04:00
|
|
|
{
|
|
|
|
authT: authTypeUnknown,
|
|
|
|
pass: false,
|
|
|
|
},
|
2016-09-30 17:32:13 -04:00
|
|
|
// Test 10 - some new auth type is not supported s3 type.
|
2016-08-08 23:56:29 -04:00
|
|
|
{
|
2016-09-30 17:32:13 -04:00
|
|
|
authT: authType(9),
|
2016-08-08 23:56:29 -04:00
|
|
|
pass: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Validate all the test cases.
|
|
|
|
for i, tt := range testCases {
|
|
|
|
ok := isSupportedS3AuthType(tt.authT)
|
|
|
|
if ok != tt.pass {
|
|
|
|
t.Errorf("Test %d:, Expected %t, got %t", i+1, tt.pass, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:54:11 -04:00
|
|
|
// TestIsRequestUnsignedPayload - Test validates the Unsigned payload detection logic.
|
|
|
|
func TestIsRequestUnsignedPayload(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
inputAmzContentHeader string
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
// Test case - 1.
|
|
|
|
// Test case with "X-Amz-Content-Sha256" header set to empty value.
|
|
|
|
{"", false},
|
|
|
|
// Test case - 2.
|
|
|
|
// Test case with "X-Amz-Content-Sha256" header set to "UNSIGNED-PAYLOAD"
|
|
|
|
// The payload is flagged as unsigned When "X-Amz-Content-Sha256" header is set to "UNSIGNED-PAYLOAD".
|
2016-11-21 16:51:05 -05:00
|
|
|
{unsignedPayload, true},
|
2016-08-01 23:54:11 -04:00
|
|
|
// Test case - 3.
|
|
|
|
// set to a random value.
|
|
|
|
{"abcd", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating an input HTTP request.
|
|
|
|
// Only the headers are relevant for this particular test.
|
|
|
|
inputReq, err := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error initializing input HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
inputReq.Header.Set("X-Amz-Content-Sha256", testCase.inputAmzContentHeader)
|
|
|
|
actualResult := isRequestUnsignedPayload(inputReq)
|
|
|
|
if testCase.expectedResult != actualResult {
|
|
|
|
t.Errorf("Test %d: Expected the result to `%v`, but instead got `%v`", i+1, testCase.expectedResult, actualResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 17:32:13 -04:00
|
|
|
func TestIsRequestPresignedSignatureV2(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
inputQueryKey string
|
|
|
|
inputQueryValue string
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
// Test case - 1.
|
|
|
|
// Test case with query key "AWSAccessKeyId" set.
|
|
|
|
{"", "", false},
|
|
|
|
// Test case - 2.
|
|
|
|
{"AWSAccessKeyId", "", true},
|
|
|
|
// Test case - 3.
|
|
|
|
{"X-Amz-Content-Sha256", "", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// creating an input HTTP request.
|
|
|
|
// Only the query parameters are relevant for this particular test.
|
|
|
|
inputReq, err := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error initializing input HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
q := inputReq.URL.Query()
|
|
|
|
q.Add(testCase.inputQueryKey, testCase.inputQueryValue)
|
|
|
|
inputReq.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
actualResult := isRequestPresignedSignatureV2(inputReq)
|
|
|
|
if testCase.expectedResult != actualResult {
|
|
|
|
t.Errorf("Test %d: Expected the result to `%v`, but instead got `%v`", i+1, testCase.expectedResult, actualResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:54:11 -04:00
|
|
|
// TestIsRequestPresignedSignatureV4 - Test validates the logic for presign signature verision v4 detection.
|
|
|
|
func TestIsRequestPresignedSignatureV4(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
inputQueryKey string
|
|
|
|
inputQueryValue string
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
// Test case - 1.
|
|
|
|
// Test case with query key ""X-Amz-Credential" set.
|
|
|
|
{"", "", false},
|
|
|
|
// Test case - 2.
|
|
|
|
{"X-Amz-Credential", "", true},
|
|
|
|
// Test case - 3.
|
|
|
|
{"X-Amz-Content-Sha256", "", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
// creating an input HTTP request.
|
|
|
|
// Only the query parameters are relevant for this particular test.
|
|
|
|
inputReq, err := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error initializing input HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
q := inputReq.URL.Query()
|
|
|
|
q.Add(testCase.inputQueryKey, testCase.inputQueryValue)
|
|
|
|
inputReq.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
actualResult := isRequestPresignedSignatureV4(inputReq)
|
|
|
|
if testCase.expectedResult != actualResult {
|
|
|
|
t.Errorf("Test %d: Expected the result to `%v`, but instead got `%v`", i+1, testCase.expectedResult, actualResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 02:56:27 -04:00
|
|
|
// Provides a fully populated http request instance, fails otherwise.
|
|
|
|
func mustNewRequest(method string, urlStr string, contentLength int64, body io.ReadSeeker, t *testing.T) *http.Request {
|
|
|
|
req, err := newTestRequest(method, urlStr, contentLength, body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize new http request %s", err)
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is similar to mustNewRequest but additionally the request
|
|
|
|
// is signed with AWS Signature V4, fails if not able to do so.
|
|
|
|
func mustNewSignedRequest(method string, urlStr string, contentLength int64, body io.ReadSeeker, t *testing.T) *http.Request {
|
|
|
|
req := mustNewRequest(method, urlStr, contentLength, body, t)
|
2017-11-29 16:12:47 -05:00
|
|
|
cred := globalServerConfig.GetCredential()
|
2016-12-26 13:21:23 -05:00
|
|
|
if err := signRequestV4(req, cred.AccessKey, cred.SecretKey); err != nil {
|
2016-07-19 02:56:27 -04:00
|
|
|
t.Fatalf("Unable to inititalized new signed http request %s", err)
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2017-04-10 12:58:08 -04:00
|
|
|
func mustNewSignedBadMD5Request(method string, urlStr string, contentLength int64, body io.ReadSeeker, t *testing.T) *http.Request {
|
|
|
|
req := mustNewRequest(method, urlStr, contentLength, body, t)
|
|
|
|
req.Header.Set("Content-Md5", "YWFhYWFhYWFhYWFhYWFhCg==")
|
2017-11-29 16:12:47 -05:00
|
|
|
cred := globalServerConfig.GetCredential()
|
2017-04-10 12:58:08 -04:00
|
|
|
if err := signRequestV4(req, cred.AccessKey, cred.SecretKey); err != nil {
|
|
|
|
t.Fatalf("Unable to initialized new signed http request %s", err)
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2016-07-19 02:56:27 -04:00
|
|
|
// Tests is requested authenticated function, tests replies for s3 errors.
|
|
|
|
func TestIsReqAuthenticated(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
path, err := newTestConfig(globalMinioDefaultRegion)
|
2016-07-19 02:56:27 -04:00
|
|
|
if err != nil {
|
2016-07-26 03:01:35 -04:00
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
2016-07-19 02:56:27 -04:00
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(path)
|
2016-07-19 02:56:27 -04:00
|
|
|
|
2017-10-31 14:54:32 -04:00
|
|
|
creds, err := auth.CreateCredentials("myuser", "mypassword")
|
2017-03-16 03:16:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable create credential, %s", err)
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:12:47 -05:00
|
|
|
globalServerConfig.SetCredential(creds)
|
2016-07-19 02:56:27 -04:00
|
|
|
|
|
|
|
// List of test cases for validating http request authentication.
|
|
|
|
testCases := []struct {
|
|
|
|
req *http.Request
|
|
|
|
s3Error APIErrorCode
|
|
|
|
}{
|
|
|
|
// When request is nil, internal error is returned.
|
|
|
|
{nil, ErrInternalError},
|
|
|
|
// When request is unsigned, access denied is returned.
|
2017-02-10 02:26:44 -05:00
|
|
|
{mustNewRequest("GET", "http://127.0.0.1:9000", 0, nil, t), ErrAccessDenied},
|
2016-07-19 02:56:27 -04:00
|
|
|
// When request is properly signed, but has bad Content-MD5 header.
|
2017-04-10 12:58:08 -04:00
|
|
|
{mustNewSignedBadMD5Request("PUT", "http://127.0.0.1:9000/", 5, bytes.NewReader([]byte("hello")), t), ErrBadDigest},
|
2016-07-19 02:56:27 -04:00
|
|
|
// When request is properly signed, error is none.
|
2017-02-10 02:26:44 -05:00
|
|
|
{mustNewSignedRequest("GET", "http://127.0.0.1:9000", 0, nil, t), ErrNone},
|
2016-07-19 02:56:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validates all testcases.
|
|
|
|
for _, testCase := range testCases {
|
2017-11-29 16:12:47 -05:00
|
|
|
if s3Error := isReqAuthenticated(testCase.req, globalServerConfig.GetRegion()); s3Error != testCase.s3Error {
|
2016-07-19 02:56:27 -04:00
|
|
|
t.Fatalf("Unexpected s3error returned wanted %d, got %d", testCase.s3Error, s3Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|