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
|
|
|
|
|
|
|
|
import (
|
2021-11-15 17:14:22 -05:00
|
|
|
"context"
|
2016-12-27 11:28:10 -05:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-11-05 15:20:08 -04:00
|
|
|
jwtgo "github.com/golang-jwt/jwt/v4"
|
|
|
|
jwtreq "github.com/golang-jwt/jwt/v4/request"
|
2021-11-23 12:51:53 -05:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
|
|
|
xjwt "github.com/minio/minio/internal/jwt"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-08-20 14:32:01 -04:00
|
|
|
iampolicy "github.com/minio/pkg/iam/policy"
|
2016-12-27 11:28:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
jwtAlgorithm = "Bearer"
|
|
|
|
|
|
|
|
// Default JWT token for web handlers is one day.
|
|
|
|
defaultJWTExpiry = 24 * time.Hour
|
|
|
|
|
2020-01-30 21:59:22 -05:00
|
|
|
// Inter-node JWT token expiry is 15 minutes.
|
|
|
|
defaultInterNodeJWTExpiry = 15 * time.Minute
|
2017-07-24 15:46:37 -04:00
|
|
|
|
|
|
|
// URL JWT token expiry is one minute (might be exposed).
|
|
|
|
defaultURLJWTExpiry = time.Minute
|
2016-12-27 11:28:10 -05:00
|
|
|
)
|
|
|
|
|
2017-02-07 15:51:43 -05:00
|
|
|
var (
|
2021-06-17 23:27:04 -04:00
|
|
|
errInvalidAccessKeyID = errors.New("The access key ID you provided does not exist in our records")
|
|
|
|
errAuthentication = errors.New("Authentication failed, check your access credentials")
|
|
|
|
errNoAuthToken = errors.New("JWT token missing")
|
2017-02-07 15:51:43 -05:00
|
|
|
)
|
2016-12-27 11:28:10 -05:00
|
|
|
|
2018-10-16 21:44:58 -04:00
|
|
|
func authenticateJWTUsers(accessKey, secretKey string, expiry time.Duration) (string, error) {
|
|
|
|
passedCredential, err := auth.CreateCredentials(accessKey, secretKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
expiresAt := UTCNow().Add(expiry)
|
|
|
|
return authenticateJWTUsersWithCredentials(passedCredential, expiresAt)
|
|
|
|
}
|
2018-10-16 21:44:58 -04:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
func authenticateJWTUsersWithCredentials(credentials auth.Credentials, expiresAt time.Time) (string, error) {
|
|
|
|
serverCred := globalActiveCred
|
|
|
|
if serverCred.AccessKey != credentials.AccessKey {
|
2022-07-01 16:19:13 -04:00
|
|
|
u, ok := globalIAMSys.GetUser(context.TODO(), credentials.AccessKey)
|
2018-10-16 21:44:58 -04:00
|
|
|
if !ok {
|
|
|
|
return "", errInvalidAccessKeyID
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
serverCred = u.Credentials
|
2018-10-16 21:44:58 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if !serverCred.Equal(credentials) {
|
2018-10-16 21:44:58 -04:00
|
|
|
return "", errAuthentication
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:59:22 -05:00
|
|
|
claims := xjwt.NewMapClaims()
|
|
|
|
claims.SetExpiry(expiresAt)
|
|
|
|
claims.SetAccessKey(credentials.AccessKey)
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
jwt := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, claims)
|
2018-10-16 21:44:58 -04:00
|
|
|
return jwt.SignedString([]byte(serverCred.SecretKey))
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:51:53 -05:00
|
|
|
// cachedAuthenticateNode will cache authenticateNode results for given values up to ttl.
|
|
|
|
func cachedAuthenticateNode(ttl time.Duration) func(accessKey, secretKey, audience string) (string, error) {
|
|
|
|
type key struct {
|
|
|
|
accessKey, secretKey, audience string
|
|
|
|
}
|
|
|
|
type value struct {
|
|
|
|
created time.Time
|
|
|
|
res string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
cache, err := lru.NewARC(100)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(GlobalContext, err)
|
|
|
|
return authenticateNode
|
|
|
|
}
|
|
|
|
return func(accessKey, secretKey, audience string) (string, error) {
|
|
|
|
k := key{accessKey: accessKey, secretKey: secretKey, audience: audience}
|
|
|
|
v, ok := cache.Get(k)
|
|
|
|
if ok {
|
|
|
|
if val, ok := v.(*value); ok && time.Since(val.created) < ttl {
|
|
|
|
return val.res, val.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s, err := authenticateNode(accessKey, secretKey, audience)
|
|
|
|
cache.Add(k, &value{created: time.Now(), res: s, err: err})
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:59:22 -05:00
|
|
|
func authenticateNode(accessKey, secretKey, audience string) (string, error) {
|
|
|
|
claims := xjwt.NewStandardClaims()
|
|
|
|
claims.SetExpiry(UTCNow().Add(defaultInterNodeJWTExpiry))
|
|
|
|
claims.SetAccessKey(accessKey)
|
|
|
|
claims.SetAudience(audience)
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
jwt := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, claims)
|
2020-01-30 21:59:22 -05:00
|
|
|
return jwt.SignedString([]byte(secretKey))
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func authenticateWeb(accessKey, secretKey string) (string, error) {
|
2018-10-16 21:44:58 -04:00
|
|
|
return authenticateJWTUsers(accessKey, secretKey, defaultJWTExpiry)
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
2017-07-24 15:46:37 -04:00
|
|
|
func authenticateURL(accessKey, secretKey string) (string, error) {
|
2018-10-16 21:44:58 -04:00
|
|
|
return authenticateJWTUsers(accessKey, secretKey, defaultURLJWTExpiry)
|
2017-07-24 15:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-01-11 16:26:42 -05:00
|
|
|
// Check if the request is authenticated.
|
|
|
|
// Returns nil if the request is authenticated. errNoAuthToken if token missing.
|
|
|
|
// Returns errAuthentication for all other errors.
|
2022-03-14 12:09:22 -04:00
|
|
|
func metricsRequestAuthenticate(req *http.Request) (*xjwt.MapClaims, []string, bool, error) {
|
2020-01-30 21:59:22 -05:00
|
|
|
token, err := jwtreq.AuthorizationHeaderExtractor.ExtractToken(req)
|
2016-12-27 11:28:10 -05:00
|
|
|
if err != nil {
|
2017-01-11 16:26:42 -05:00
|
|
|
if err == jwtreq.ErrNoTokenInRequest {
|
2022-01-27 00:53:36 -05:00
|
|
|
return nil, nil, false, errNoAuthToken
|
2017-01-11 16:26:42 -05:00
|
|
|
}
|
2022-01-27 00:53:36 -05:00
|
|
|
return nil, nil, false, err
|
2017-09-19 15:37:56 -04:00
|
|
|
}
|
2020-01-30 21:59:22 -05:00
|
|
|
claims := xjwt.NewMapClaims()
|
2021-08-20 14:32:01 -04:00
|
|
|
if err := xjwt.ParseWithClaims(token, claims, func(claims *xjwt.MapClaims) ([]byte, error) {
|
|
|
|
if claims.AccessKey == globalActiveCred.AccessKey {
|
|
|
|
return []byte(globalActiveCred.SecretKey), nil
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
u, ok := globalIAMSys.GetUser(req.Context(), claims.AccessKey)
|
2021-08-20 14:32:01 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidAccessKeyID
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
cred := u.Credentials
|
2021-08-20 14:32:01 -04:00
|
|
|
return []byte(cred.SecretKey), nil
|
|
|
|
}); err != nil {
|
2022-01-27 00:53:36 -05:00
|
|
|
return claims, nil, false, errAuthentication
|
2017-01-11 16:26:42 -05:00
|
|
|
}
|
2021-08-20 14:32:01 -04:00
|
|
|
owner := true
|
2022-01-27 00:53:36 -05:00
|
|
|
var groups []string
|
2021-08-20 14:32:01 -04:00
|
|
|
if globalActiveCred.AccessKey != claims.AccessKey {
|
|
|
|
// Check if the access key is part of users credentials.
|
2022-07-01 16:19:13 -04:00
|
|
|
u, ok := globalIAMSys.GetUser(req.Context(), claims.AccessKey)
|
2021-08-20 14:32:01 -04:00
|
|
|
if !ok {
|
2022-01-27 00:53:36 -05:00
|
|
|
return nil, nil, false, errInvalidAccessKeyID
|
2021-08-20 14:32:01 -04:00
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
ucred := u.Credentials
|
2021-08-20 14:32:01 -04:00
|
|
|
// get embedded claims
|
|
|
|
eclaims, s3Err := checkClaimsFromToken(req, ucred)
|
|
|
|
if s3Err != ErrNone {
|
2022-01-27 00:53:36 -05:00
|
|
|
return nil, nil, false, errAuthentication
|
2021-08-20 14:32:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range eclaims {
|
|
|
|
claims.MapClaims[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
if _, ok = eclaims[iampolicy.SessionPolicyName]; ok {
|
|
|
|
owner = false
|
|
|
|
} else {
|
|
|
|
owner = globalActiveCred.AccessKey == ucred.ParentUser
|
|
|
|
}
|
2022-01-27 00:53:36 -05:00
|
|
|
|
|
|
|
groups = ucred.Groups
|
2021-08-20 14:32:01 -04:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:53:36 -05:00
|
|
|
return claims, groups, owner, nil
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
2021-11-23 12:51:53 -05:00
|
|
|
// newCachedAuthToken returns a token that is cached up to 15 seconds.
|
|
|
|
// If globalActiveCred is updated it is reflected at once.
|
|
|
|
func newCachedAuthToken() func(audience string) string {
|
|
|
|
fn := cachedAuthenticateNode(15 * time.Second)
|
|
|
|
return func(audience string) string {
|
|
|
|
cred := globalActiveCred
|
|
|
|
token, err := fn(cred.AccessKey, cred.SecretKey, audience)
|
|
|
|
logger.CriticalIf(GlobalContext, err)
|
|
|
|
return token
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|