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 (
|
2022-10-14 06:08:40 -04:00
|
|
|
"context"
|
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
|
|
|
|
2021-11-05 15:20:08 -04:00
|
|
|
jwtgo "github.com/golang-jwt/jwt/v4"
|
2021-06-01 17:59:40 -04:00
|
|
|
xjwt "github.com/minio/minio/internal/jwt"
|
2017-08-12 22:25:43 -04:00
|
|
|
)
|
2016-12-27 11:28:10 -05:00
|
|
|
|
2021-06-17 23:27:04 -04:00
|
|
|
func getTokenString(accessKey, secretKey string) (string, error) {
|
|
|
|
claims := xjwt.NewMapClaims()
|
|
|
|
claims.SetExpiry(UTCNow().Add(defaultJWTExpiry))
|
|
|
|
claims.SetAccessKey(accessKey)
|
|
|
|
token := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, claims)
|
|
|
|
return token.SignedString([]byte(secretKey))
|
|
|
|
}
|
|
|
|
|
2021-04-29 22:01:43 -04:00
|
|
|
// Tests web request authenticator.
|
|
|
|
func TestWebRequestAuthenticate(t *testing.T) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
obj, fsDir, err := prepareFS(ctx)
|
2021-04-29 22:01:43 -04:00
|
|
|
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 {
|
2022-03-14 12:09:22 -04:00
|
|
|
_, _, _, gotErr := metricsRequestAuthenticate(testCase.req)
|
2021-04-29 22:01:43 -04:00
|
|
|
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) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
obj, fsDir, err := prepareFS(ctx)
|
2020-01-30 21:59:22 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := globalActiveCred
|
2024-07-22 03:04:48 -04:00
|
|
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey)
|
2020-01-30 21:59:22 -05:00
|
|
|
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) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
obj, fsDir, err := prepareFS(ctx)
|
2020-01-30 21:59:22 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(fsDir)
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := globalActiveCred
|
2024-07-22 03:04:48 -04:00
|
|
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey)
|
2020-01-30 21:59:22 -05:00
|
|
|
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) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
obj, fsDir, err := prepareFS(ctx)
|
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
|
2021-11-23 12:51:53 -05:00
|
|
|
b.Run("uncached", func(b *testing.B) {
|
|
|
|
fn := authenticateNode
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-07-22 03:04:48 -04:00
|
|
|
fn(creds.AccessKey, creds.SecretKey)
|
2021-11-23 12:51:53 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("cached", func(b *testing.B) {
|
2024-04-19 12:45:14 -04:00
|
|
|
fn := newCachedAuthToken()
|
2021-11-23 12:51:53 -05:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-07-22 03:04:48 -04:00
|
|
|
fn()
|
2021-11-23 12:51:53 -05:00
|
|
|
}
|
|
|
|
})
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|