2021-04-18 15:41:13 -04:00
|
|
|
// 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/>.
|
2016-12-27 11:28:10 -05:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
import (
|
2021-04-29 22:01:43 -04:00
|
|
|
"net/http"
|
2017-08-12 22:25:43 -04:00
|
|
|
"os"
|
|
|
|
"testing"
|
2017-10-31 14:54:32 -04:00
|
|
|
|
2020-01-30 21:59:22 -05:00
|
|
|
xjwt "github.com/minio/minio/cmd/jwt"
|
2017-10-31 14:54:32 -04:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2017-08-12 22:25:43 -04:00
|
|
|
)
|
2016-12-27 11:28:10 -05:00
|
|
|
|
|
|
|
func testAuthenticate(authType string, t *testing.T) {
|
2018-08-15 00:41:47 -04:00
|
|
|
obj, fsDir, err := prepareFS()
|
2016-12-27 11:28:10 -05:00
|
|
|
if err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
t.Fatal(err)
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
2018-08-15 00:41:47 -04:00
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:24:43 -04:00
|
|
|
cred, err := auth.GetNewCredentials()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error getting new credentials: %s", err)
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
globalActiveCred = cred
|
2016-12-27 11:28:10 -05:00
|
|
|
|
|
|
|
// Define test cases.
|
|
|
|
testCases := []struct {
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
2018-02-01 22:43:30 -05:00
|
|
|
// Access key (less than 3 chrs) too small.
|
|
|
|
{"u1", cred.SecretKey, auth.ErrInvalidAccessKeyLength},
|
2017-08-03 23:03:37 -04:00
|
|
|
// Secret key (less than 8 chrs) too small.
|
2017-10-31 14:54:32 -04:00
|
|
|
{cred.AccessKey, "pass", auth.ErrInvalidSecretKeyLength},
|
2016-12-27 11:28:10 -05:00
|
|
|
// Authentication error.
|
|
|
|
{"myuser", "mypassword", errInvalidAccessKeyID},
|
|
|
|
// Authentication error.
|
2017-08-03 23:03:37 -04:00
|
|
|
{cred.AccessKey, "mypassword", errAuthentication},
|
2016-12-27 11:28:10 -05:00
|
|
|
// Success.
|
2017-08-03 23:03:37 -04:00
|
|
|
{cred.AccessKey, cred.SecretKey, nil},
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run tests.
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
var err error
|
2020-01-30 21:59:22 -05:00
|
|
|
if authType == "web" {
|
2016-12-27 11:28:10 -05:00
|
|
|
_, err = authenticateWeb(testCase.accessKey, testCase.secretKey)
|
2017-07-24 15:46:37 -04:00
|
|
|
} else if authType == "url" {
|
|
|
|
_, err = authenticateURL(testCase.accessKey, testCase.secretKey)
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.expectedErr != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("%+v: expected: %s, got: <nil>", testCase, testCase.expectedErr)
|
|
|
|
}
|
|
|
|
if testCase.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("%+v: expected: %s, got: %s", testCase, testCase.expectedErr, err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("%+v: expected: <nil>, got: %s", testCase, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:51:51 -05:00
|
|
|
func TestAuthenticateWeb(t *testing.T) {
|
2016-12-27 11:28:10 -05:00
|
|
|
testAuthenticate("web", t)
|
|
|
|
}
|
2017-01-26 19:51:51 -05:00
|
|
|
|
2017-07-24 15:46:37 -04:00
|
|
|
func TestAuthenticateURL(t *testing.T) {
|
|
|
|
testAuthenticate("url", t)
|
|
|
|
}
|
|
|
|
|
2021-04-29 22:01:43 -04:00
|
|
|
// Tests web request authenticator.
|
|
|
|
func TestWebRequestAuthenticate(t *testing.T) {
|
|
|
|
obj, fsDir, err := prepareFS()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := globalActiveCred
|
|
|
|
token, err := getTokenString(creds.AccessKey, creds.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable get token %s", err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
req *http.Request
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
// Set valid authorization header.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"Authorization": []string{token},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
// No authorization header.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
Header: http.Header{},
|
|
|
|
},
|
|
|
|
expectedErr: errNoAuthToken,
|
|
|
|
},
|
|
|
|
// Invalid authorization token.
|
|
|
|
{
|
|
|
|
req: &http.Request{
|
|
|
|
Header: http.Header{
|
|
|
|
"Authorization": []string{"invalid-token"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: errAuthentication,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
_, _, gotErr := webRequestAuthenticate(testCase.req)
|
|
|
|
if testCase.expectedErr != gotErr {
|
|
|
|
t.Errorf("Test %d, expected err %s, got %s", i+1, testCase.expectedErr, gotErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:59:22 -05:00
|
|
|
func BenchmarkParseJWTStandardClaims(b *testing.B) {
|
|
|
|
obj, fsDir, err := prepareFS()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := globalActiveCred
|
|
|
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
err = xjwt.ParseWithStandardClaims(token, xjwt.NewStandardClaims(), []byte(creds.SecretKey))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParseJWTMapClaims(b *testing.B) {
|
|
|
|
obj, fsDir, err := prepareFS()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := globalActiveCred
|
|
|
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
err = xjwt.ParseWithClaims(token, xjwt.NewMapClaims(), func(*xjwt.MapClaims) ([]byte, error) {
|
|
|
|
return []byte(creds.SecretKey), nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:51:51 -05:00
|
|
|
func BenchmarkAuthenticateNode(b *testing.B) {
|
2018-08-15 00:41:47 -04:00
|
|
|
obj, fsDir, err := prepareFS()
|
2017-01-26 19:51:51 -05:00
|
|
|
if err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
creds := globalActiveCred
|
2017-01-26 19:51:51 -05:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-30 21:59:22 -05:00
|
|
|
authenticateNode(creds.AccessKey, creds.SecretKey, "")
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAuthenticateWeb(b *testing.B) {
|
2018-08-15 00:41:47 -04:00
|
|
|
obj, fsDir, err := prepareFS()
|
2017-01-26 19:51:51 -05:00
|
|
|
if err != nil {
|
2018-08-15 00:41:47 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
creds := globalActiveCred
|
2017-01-26 19:51:51 -05:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
authenticateWeb(creds.AccessKey, creds.SecretKey)
|
|
|
|
}
|
|
|
|
}
|