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/>.
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-06-20 18:28:33 -04:00
|
|
|
"bytes"
|
2019-02-27 20:46:55 -05:00
|
|
|
"context"
|
2021-09-07 22:03:48 -04:00
|
|
|
"crypto/x509"
|
2019-06-20 18:28:33 -04:00
|
|
|
"encoding/base64"
|
2021-04-29 16:01:42 -04:00
|
|
|
"errors"
|
2019-02-05 18:47:11 -05:00
|
|
|
"fmt"
|
2018-10-09 17:00:01 -04:00
|
|
|
"net/http"
|
2020-04-28 15:49:56 -04:00
|
|
|
"strings"
|
2021-09-07 22:03:48 -04:00
|
|
|
"time"
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
|
|
|
"github.com/minio/minio/internal/config/identity/openid"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-30 00:16:42 -04:00
|
|
|
iampolicy "github.com/minio/pkg/iam/policy"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/wildcard"
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// STS API version.
|
2021-09-13 19:22:14 -04:00
|
|
|
stsAPIVersion = "2011-06-15"
|
|
|
|
stsVersion = "Version"
|
|
|
|
stsAction = "Action"
|
|
|
|
stsPolicy = "Policy"
|
|
|
|
stsToken = "Token"
|
|
|
|
stsWebIdentityToken = "WebIdentityToken"
|
|
|
|
stsWebIdentityAccessToken = "WebIdentityAccessToken" // only valid if UserInfo is enabled.
|
|
|
|
stsDurationSeconds = "DurationSeconds"
|
|
|
|
stsLDAPUsername = "LDAPUsername"
|
|
|
|
stsLDAPPassword = "LDAPPassword"
|
2019-02-05 18:47:11 -05:00
|
|
|
|
|
|
|
// STS API action constants
|
2021-09-07 22:03:48 -04:00
|
|
|
clientGrants = "AssumeRoleWithClientGrants"
|
|
|
|
webIdentity = "AssumeRoleWithWebIdentity"
|
|
|
|
ldapIdentity = "AssumeRoleWithLDAPIdentity"
|
|
|
|
clientCertificate = "AssumeRoleWithCertificate"
|
|
|
|
assumeRole = "AssumeRole"
|
2019-08-05 13:06:40 -04:00
|
|
|
|
|
|
|
stsRequestBodyLimit = 10 * (1 << 20) // 10 MiB
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
// JWT claim keys
|
|
|
|
expClaim = "exp"
|
|
|
|
subClaim = "sub"
|
2021-07-26 22:40:15 -04:00
|
|
|
audClaim = "aud"
|
2021-07-27 21:37:51 -04:00
|
|
|
azpClaim = "azp"
|
2021-04-29 16:01:42 -04:00
|
|
|
issClaim = "iss"
|
2019-11-29 08:27:54 -05:00
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// JWT claim to check the parent user
|
|
|
|
parentClaim = "parent"
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// LDAP claim keys
|
2021-07-11 21:38:52 -04:00
|
|
|
ldapUser = "ldapUser"
|
|
|
|
ldapUserN = "ldapUsername"
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
func parseOpenIDParentUser(parentUser string) (userID string, err error) {
|
2021-07-11 20:39:52 -04:00
|
|
|
if strings.HasPrefix(parentUser, "openid:") {
|
|
|
|
tokens := strings.SplitN(strings.TrimPrefix(parentUser, "openid:"), ":", 2)
|
2021-07-09 14:17:21 -04:00
|
|
|
if len(tokens) == 2 {
|
|
|
|
return tokens[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errSkipFile
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// stsAPIHandlers implements and provides http handlers for AWS STS API.
|
|
|
|
type stsAPIHandlers struct{}
|
|
|
|
|
|
|
|
// registerSTSRouter - registers AWS STS compatible APIs.
|
|
|
|
func registerSTSRouter(router *mux.Router) {
|
|
|
|
// Initialize STS.
|
|
|
|
sts := &stsAPIHandlers{}
|
|
|
|
|
|
|
|
// STS Router
|
2019-08-06 15:08:58 -04:00
|
|
|
stsRouter := router.NewRoute().PathPrefix(SlashSeparator).Subrouter()
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
// Assume roles with no JWT, handles AssumeRole.
|
2019-07-03 01:34:32 -04:00
|
|
|
stsRouter.Methods(http.MethodPost).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
|
|
|
|
ctypeOk := wildcard.MatchSimple("application/x-www-form-urlencoded*", r.Header.Get(xhttp.ContentType))
|
|
|
|
authOk := wildcard.MatchSimple(signV4Algorithm+"*", r.Header.Get(xhttp.Authorization))
|
2021-08-08 01:43:01 -04:00
|
|
|
noQueries := len(r.URL.RawQuery) == 0
|
2019-04-23 18:55:41 -04:00
|
|
|
return ctypeOk && authOk && noQueries
|
|
|
|
}).HandlerFunc(httpTraceAll(sts.AssumeRole))
|
2019-02-27 20:46:55 -05:00
|
|
|
|
2019-02-05 18:47:11 -05:00
|
|
|
// Assume roles with JWT handler, handles both ClientGrants and WebIdentity.
|
2019-11-29 08:27:54 -05:00
|
|
|
stsRouter.Methods(http.MethodPost).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
|
2019-07-03 01:34:32 -04:00
|
|
|
ctypeOk := wildcard.MatchSimple("application/x-www-form-urlencoded*", r.Header.Get(xhttp.ContentType))
|
2021-08-08 01:43:01 -04:00
|
|
|
noQueries := len(r.URL.RawQuery) == 0
|
2019-04-23 18:55:41 -04:00
|
|
|
return ctypeOk && noQueries
|
2020-09-12 02:02:32 -04:00
|
|
|
}).HandlerFunc(httpTraceAll(sts.AssumeRoleWithSSO))
|
2019-02-05 18:47:11 -05:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// AssumeRoleWithClientGrants
|
2019-11-29 08:27:54 -05:00
|
|
|
stsRouter.Methods(http.MethodPost).HandlerFunc(httpTraceAll(sts.AssumeRoleWithClientGrants)).
|
|
|
|
Queries(stsAction, clientGrants).
|
|
|
|
Queries(stsVersion, stsAPIVersion).
|
|
|
|
Queries(stsToken, "{Token:.*}")
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-01-04 16:48:12 -05:00
|
|
|
// AssumeRoleWithWebIdentity
|
2019-11-29 08:27:54 -05:00
|
|
|
stsRouter.Methods(http.MethodPost).HandlerFunc(httpTraceAll(sts.AssumeRoleWithWebIdentity)).
|
|
|
|
Queries(stsAction, webIdentity).
|
|
|
|
Queries(stsVersion, stsAPIVersion).
|
|
|
|
Queries(stsWebIdentityToken, "{Token:.*}")
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// AssumeRoleWithLDAPIdentity
|
2019-11-29 08:27:54 -05:00
|
|
|
stsRouter.Methods(http.MethodPost).HandlerFunc(httpTraceAll(sts.AssumeRoleWithLDAPIdentity)).
|
|
|
|
Queries(stsAction, ldapIdentity).
|
|
|
|
Queries(stsVersion, stsAPIVersion).
|
|
|
|
Queries(stsLDAPUsername, "{LDAPUsername:.*}").
|
|
|
|
Queries(stsLDAPPassword, "{LDAPPassword:.*}")
|
2021-09-07 22:03:48 -04:00
|
|
|
|
|
|
|
// AssumeRoleWithCertificate
|
|
|
|
stsRouter.Methods(http.MethodPost).HandlerFunc(httpTraceAll(sts.AssumeRoleWithCertificate)).
|
|
|
|
Queries(stsAction, clientCertificate).
|
|
|
|
Queries(stsVersion, stsAPIVersion)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:09:18 -04:00
|
|
|
func checkAssumeRoleAuth(ctx context.Context, r *http.Request) (user auth.Credentials, isErrCodeSTS bool, stsErr STSErrorCode) {
|
2019-02-27 20:46:55 -05:00
|
|
|
switch getRequestAuthType(r) {
|
|
|
|
default:
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, true, ErrSTSAccessDenied
|
2019-02-27 20:46:55 -05:00
|
|
|
case authTypeSigned:
|
2019-10-23 01:59:13 -04:00
|
|
|
s3Err := isReqAuthenticated(ctx, r, globalServerRegion, serviceSTS)
|
2021-05-24 12:28:19 -04:00
|
|
|
if s3Err != ErrNone {
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, false, STSErrorCode(s3Err)
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
2021-05-04 14:58:19 -04:00
|
|
|
|
|
|
|
user, _, s3Err = getReqAccessKeyV4(r, globalServerRegion, serviceSTS)
|
2021-05-24 12:28:19 -04:00
|
|
|
if s3Err != ErrNone {
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, false, STSErrorCode(s3Err)
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
2021-05-04 14:58:19 -04:00
|
|
|
|
|
|
|
// Temporary credentials or Service accounts cannot generate further temporary credentials.
|
|
|
|
if user.IsTemp() || user.IsServiceAccount() {
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, true, ErrSTSAccessDenied
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session tokens are not allowed in STS AssumeRole requests.
|
|
|
|
if getSessionToken(r) != "" {
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, true, ErrSTSAccessDenied
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:09:18 -04:00
|
|
|
return user, true, ErrSTSNone
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
|
|
|
|
2021-08-08 01:43:01 -04:00
|
|
|
func parseForm(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range r.PostForm {
|
|
|
|
if _, ok := r.Form[k]; !ok {
|
|
|
|
r.Form[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
// AssumeRole - implementation of AWS STS API AssumeRole to get temporary
|
|
|
|
// credentials for regular users on Minio.
|
|
|
|
// https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
|
|
|
|
func (sts *stsAPIHandlers) AssumeRole(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AssumeRole")
|
|
|
|
|
2020-05-21 12:09:18 -04:00
|
|
|
user, isErrCodeSTS, stsErr := checkAssumeRoleAuth(ctx, r)
|
2019-02-27 20:46:55 -05:00
|
|
|
if stsErr != ErrSTSNone {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, isErrCodeSTS, stsErr, nil)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
2021-05-04 14:58:19 -04:00
|
|
|
|
2021-08-08 01:43:01 -04:00
|
|
|
if err := parseForm(r); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
if r.Form.Get(stsVersion) != stsAPIVersion {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, fmt.Errorf("Invalid STS API version %s, expecting %s", r.Form.Get(stsVersion), stsAPIVersion))
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
action := r.Form.Get(stsAction)
|
2019-02-27 20:46:55 -05:00
|
|
|
switch action {
|
|
|
|
case assumeRole:
|
|
|
|
default:
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Unsupported action %s", action))
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = newContext(r, w, action)
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, nil)
|
2019-02-27 20:46:55 -05:00
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
sessionPolicyStr := r.Form.Get(stsPolicy)
|
2019-06-20 18:28:33 -04:00
|
|
|
// https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
|
|
|
|
// The plain text that you use for both inline and managed session
|
|
|
|
// policies shouldn't exceed 2048 characters.
|
|
|
|
if len(sessionPolicyStr) > 2048 {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Session policy shouldn't exceed 2048 characters"))
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sessionPolicyStr) > 0 {
|
|
|
|
sessionPolicy, err := iampolicy.ParseConfig(bytes.NewReader([]byte(sessionPolicyStr)))
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version in policy must not be empty
|
|
|
|
if sessionPolicy.Version == "" {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Version cannot be empty expecting '2012-10-17'"))
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
var err error
|
|
|
|
m := make(map[string]interface{})
|
2019-11-29 08:27:54 -05:00
|
|
|
m[expClaim], err = openid.GetDefaultExpiration(r.Form.Get(stsDurationSeconds))
|
2019-02-27 20:46:55 -05:00
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
policies, err := globalIAMSys.PolicyDBGet(user.AccessKey, false)
|
2019-02-27 20:46:55 -05:00
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
policyName := strings.Join(policies, ",")
|
2019-08-02 17:25:00 -04:00
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
// This policy is the policy associated with the user
|
|
|
|
// requesting for temporary credentials. The temporary
|
|
|
|
// credentials will inherit the same policy requirements.
|
2020-03-23 17:17:18 -04:00
|
|
|
m[iamPolicyClaimNameOpenID()] = policyName
|
2019-06-20 18:28:33 -04:00
|
|
|
|
|
|
|
if len(sessionPolicyStr) > 0 {
|
|
|
|
m[iampolicy.SessionPolicyName] = base64.StdEncoding.EncodeToString([]byte(sessionPolicyStr))
|
|
|
|
}
|
2019-02-27 20:46:55 -05:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
secret := globalActiveCred.SecretKey
|
2019-02-27 20:46:55 -05:00
|
|
|
cred, err := auth.GetNewCredentialsWithMetadata(m, secret)
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
// Set the parent of the temporary access key, this is useful
|
|
|
|
// in obtaining service accounts by this cred.
|
|
|
|
cred.ParentUser = user.AccessKey
|
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
// Set the newly generated credentials.
|
|
|
|
if err = globalIAMSys.SetTempUser(cred.AccessKey, cred, policyName); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2019-02-27 20:46:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// Notify all other MinIO peers to reload temp users
|
2019-06-06 20:46:22 -04:00
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(cred.AccessKey, true) {
|
2019-02-27 20:46:55 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assumeRoleResponse := &AssumeRoleResponse{
|
|
|
|
Result: AssumeRoleResult{
|
|
|
|
Credentials: cred,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-03 01:34:32 -04:00
|
|
|
assumeRoleResponse.ResponseMetadata.RequestID = w.Header().Get(xhttp.AmzRequestID)
|
2019-02-27 20:46:55 -05:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(assumeRoleResponse))
|
|
|
|
}
|
|
|
|
|
2020-09-12 02:02:32 -04:00
|
|
|
func (sts *stsAPIHandlers) AssumeRoleWithSSO(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AssumeRoleSSOCommon")
|
2019-01-04 16:48:12 -05:00
|
|
|
|
2019-02-05 18:47:11 -05:00
|
|
|
// Parse the incoming form data.
|
2021-08-08 01:43:01 -04:00
|
|
|
if err := parseForm(r); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-01-04 16:48:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
if r.Form.Get(stsVersion) != stsAPIVersion {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, fmt.Errorf("Invalid STS API version %s, expecting %s", r.Form.Get("Version"), stsAPIVersion))
|
2019-01-04 16:48:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
action := r.Form.Get(stsAction)
|
2019-02-05 18:47:11 -05:00
|
|
|
switch action {
|
2020-09-12 02:02:32 -04:00
|
|
|
case ldapIdentity:
|
|
|
|
sts.AssumeRoleWithLDAPIdentity(w, r)
|
|
|
|
return
|
2019-02-05 18:47:11 -05:00
|
|
|
case clientGrants, webIdentity:
|
|
|
|
default:
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Unsupported action %s", action))
|
2019-01-04 16:48:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:47:11 -05:00
|
|
|
ctx = newContext(r, w, action)
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, nil)
|
2018-11-21 23:03:24 -05:00
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
if globalOpenIDValidators == nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSNotInitialized, errServerNotInitialized)
|
2018-10-09 17:00:01 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
v, err := globalOpenIDValidators.Get("jwt")
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2018-10-09 17:00:01 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
token := r.Form.Get(stsToken)
|
2019-02-05 18:47:11 -05:00
|
|
|
if token == "" {
|
2019-11-29 08:27:54 -05:00
|
|
|
token = r.Form.Get(stsWebIdentityToken)
|
2019-02-05 18:47:11 -05:00
|
|
|
}
|
|
|
|
|
2021-09-13 19:22:14 -04:00
|
|
|
accessToken := r.Form.Get(stsWebIdentityAccessToken)
|
|
|
|
|
|
|
|
m, err := v.Validate(token, accessToken, r.Form.Get(stsDurationSeconds))
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
2019-10-01 18:07:20 -04:00
|
|
|
case openid.ErrTokenExpired:
|
2019-02-05 18:47:11 -05:00
|
|
|
switch action {
|
|
|
|
case clientGrants:
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSClientGrantsExpiredToken, err)
|
2019-02-05 18:47:11 -05:00
|
|
|
case webIdentity:
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSWebIdentityExpiredToken, err)
|
2019-02-05 18:47:11 -05:00
|
|
|
}
|
|
|
|
return
|
2019-11-29 08:27:54 -05:00
|
|
|
case auth.ErrInvalidDuration:
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-02-05 18:47:11 -05:00
|
|
|
return
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2018-10-09 17:00:01 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:37:51 -04:00
|
|
|
// REQUIRED. Audience(s) that this ID Token is intended for.
|
|
|
|
// It MUST contain the OAuth 2.0 client_id of the Relying Party
|
|
|
|
// as an audience value. It MAY also contain identifiers for
|
|
|
|
// other audiences. In the general case, the aud value is an
|
|
|
|
// array of case sensitive strings. In the common special case
|
|
|
|
// when there is one audience, the aud value MAY be a single
|
|
|
|
// case sensitive
|
|
|
|
audValues, ok := iampolicy.GetValuesFromClaims(m, audClaim)
|
|
|
|
if !ok {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
errors.New("STS JWT Token has `aud` claim invalid, `aud` must match configured OpenID Client ID"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !audValues.Contains(globalOpenIDConfig.ClientID) {
|
|
|
|
// if audience claims is missing, look for "azp" claims.
|
|
|
|
// OPTIONAL. Authorized party - the party to which the ID
|
|
|
|
// Token was issued. If present, it MUST contain the OAuth
|
|
|
|
// 2.0 Client ID of this party. This Claim is only needed
|
|
|
|
// when the ID Token has a single audience value and that
|
|
|
|
// audience is different than the authorized party. It MAY
|
|
|
|
// be included even when the authorized party is the same
|
|
|
|
// as the sole audience. The azp value is a case sensitive
|
|
|
|
// string containing a StringOrURI value
|
|
|
|
azpValues, ok := iampolicy.GetValuesFromClaims(m, azpClaim)
|
|
|
|
if !ok {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
errors.New("STS JWT Token has `aud` claim invalid, `aud` must match configured OpenID Client ID"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !azpValues.Contains(globalOpenIDConfig.ClientID) {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
errors.New("STS JWT Token has `azp` claim invalid, `azp` must match configured OpenID Client ID"))
|
|
|
|
return
|
|
|
|
}
|
2021-07-26 22:40:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-29 16:01:42 -04:00
|
|
|
var subFromToken string
|
|
|
|
if v, ok := m[subClaim]; ok {
|
|
|
|
subFromToken, _ = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subFromToken == "" {
|
2021-07-26 22:40:15 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
errors.New("STS JWT Token has `sub` claim missing, `sub` claim is mandatory"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:01:42 -04:00
|
|
|
var issFromToken string
|
|
|
|
if v, ok := m[issClaim]; ok {
|
|
|
|
issFromToken, _ = v.(string)
|
|
|
|
}
|
|
|
|
|
2020-07-19 18:34:01 -04:00
|
|
|
// JWT has requested a custom claim with policy value set.
|
|
|
|
// This is a MinIO STS API specific value, this value should
|
|
|
|
// be set and configured on your identity provider as part of
|
|
|
|
// JWT custom claims.
|
|
|
|
var policyName string
|
|
|
|
policySet, ok := iampolicy.GetPoliciesFromClaims(m, iamPolicyClaimNameOpenID())
|
2021-06-30 20:11:23 -04:00
|
|
|
policies := strings.Join(policySet.ToSlice(), ",")
|
2020-07-19 18:34:01 -04:00
|
|
|
if ok {
|
2021-06-30 20:11:23 -04:00
|
|
|
policyName = globalIAMSys.CurrentPolicies(policies)
|
2020-07-19 18:34:01 -04:00
|
|
|
}
|
|
|
|
|
2021-06-30 20:11:23 -04:00
|
|
|
if globalPolicyOPA == nil {
|
|
|
|
if !ok {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
fmt.Errorf("%s claim missing from the JWT token, credentials will not be generated", iamPolicyClaimNameOpenID()))
|
|
|
|
return
|
|
|
|
} else if policyName == "" {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
fmt.Errorf("None of the given policies (`%s`) are defined, credentials will not be generated", policies))
|
|
|
|
return
|
|
|
|
}
|
2020-07-19 18:34:01 -04:00
|
|
|
}
|
|
|
|
m[iamPolicyClaimNameOpenID()] = policyName
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
sessionPolicyStr := r.Form.Get(stsPolicy)
|
2019-06-20 18:28:33 -04:00
|
|
|
// https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html
|
|
|
|
// The plain text that you use for both inline and managed session
|
|
|
|
// policies shouldn't exceed 2048 characters.
|
|
|
|
if len(sessionPolicyStr) > 2048 {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Session policy should not exceed 2048 characters"))
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sessionPolicyStr) > 0 {
|
|
|
|
sessionPolicy, err := iampolicy.ParseConfig(bytes.NewReader([]byte(sessionPolicyStr)))
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version in policy must not be empty
|
|
|
|
if sessionPolicy.Version == "" {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Invalid session policy version"))
|
2019-06-20 18:28:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-23 18:21:16 -04:00
|
|
|
m[iampolicy.SessionPolicyName] = base64.StdEncoding.EncodeToString([]byte(sessionPolicyStr))
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
secret := globalActiveCred.SecretKey
|
2018-10-09 17:00:01 -04:00
|
|
|
cred, err := auth.GetNewCredentialsWithMetadata(m, secret)
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2018-10-09 17:00:01 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:01:42 -04:00
|
|
|
// https://openid.net/specs/openid-connect-core-1_0.html#ClaimStability
|
|
|
|
// claim is only considered stable when subject and iss are used together
|
|
|
|
// this is to ensure that ParentUser doesn't change and we get to use
|
|
|
|
// parentUser as per the requirements for service accounts for OpenID
|
|
|
|
// based logins.
|
2021-07-11 20:39:52 -04:00
|
|
|
cred.ParentUser = "openid:" + subFromToken + ":" + issFromToken
|
2019-01-04 16:48:12 -05:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// Set the newly generated credentials.
|
2018-10-29 14:08:59 -04:00
|
|
|
if err = globalIAMSys.SetTempUser(cred.AccessKey, cred, policyName); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2018-10-09 17:00:01 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// Notify all other MinIO peers to reload temp users
|
2019-06-06 20:46:22 -04:00
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(cred.AccessKey, true) {
|
2019-01-14 01:44:20 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
2019-01-04 16:48:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:47:11 -05:00
|
|
|
var encodedSuccessResponse []byte
|
|
|
|
switch action {
|
|
|
|
case clientGrants:
|
2019-02-27 20:46:55 -05:00
|
|
|
clientGrantsResponse := &AssumeRoleWithClientGrantsResponse{
|
2019-02-05 18:47:11 -05:00
|
|
|
Result: ClientGrantsResult{
|
|
|
|
Credentials: cred,
|
|
|
|
SubjectFromToken: subFromToken,
|
|
|
|
},
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
2019-07-03 01:34:32 -04:00
|
|
|
clientGrantsResponse.ResponseMetadata.RequestID = w.Header().Get(xhttp.AmzRequestID)
|
2019-02-27 20:46:55 -05:00
|
|
|
encodedSuccessResponse = encodeResponse(clientGrantsResponse)
|
2019-02-05 18:47:11 -05:00
|
|
|
case webIdentity:
|
2019-02-27 20:46:55 -05:00
|
|
|
webIdentityResponse := &AssumeRoleWithWebIdentityResponse{
|
2019-02-05 18:47:11 -05:00
|
|
|
Result: WebIdentityResult{
|
|
|
|
Credentials: cred,
|
|
|
|
SubjectFromWebIdentityToken: subFromToken,
|
|
|
|
},
|
2019-02-27 20:46:55 -05:00
|
|
|
}
|
2019-07-03 01:34:32 -04:00
|
|
|
webIdentityResponse.ResponseMetadata.RequestID = w.Header().Get(xhttp.AmzRequestID)
|
2019-02-27 20:46:55 -05:00
|
|
|
encodedSuccessResponse = encodeResponse(webIdentityResponse)
|
2019-02-05 18:47:11 -05:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodedSuccessResponse)
|
|
|
|
}
|
2019-02-05 18:47:11 -05:00
|
|
|
|
|
|
|
// AssumeRoleWithWebIdentity - implementation of AWS STS API supporting OAuth2.0
|
|
|
|
// users from web identity provider such as Facebook, Google, or any OpenID
|
|
|
|
// Connect-compatible identity provider.
|
|
|
|
//
|
|
|
|
// Eg:-
|
|
|
|
// $ curl https://minio:9000/?Action=AssumeRoleWithWebIdentity&WebIdentityToken=<jwt>
|
|
|
|
func (sts *stsAPIHandlers) AssumeRoleWithWebIdentity(w http.ResponseWriter, r *http.Request) {
|
2020-09-12 02:02:32 -04:00
|
|
|
sts.AssumeRoleWithSSO(w, r)
|
2019-02-05 18:47:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AssumeRoleWithClientGrants - implementation of AWS STS extension API supporting
|
|
|
|
// OAuth2.0 client credential grants.
|
|
|
|
//
|
|
|
|
// Eg:-
|
|
|
|
// $ curl https://minio:9000/?Action=AssumeRoleWithClientGrants&Token=<jwt>
|
|
|
|
func (sts *stsAPIHandlers) AssumeRoleWithClientGrants(w http.ResponseWriter, r *http.Request) {
|
2020-09-12 02:02:32 -04:00
|
|
|
sts.AssumeRoleWithSSO(w, r)
|
2019-02-05 18:47:11 -05:00
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
|
|
|
// AssumeRoleWithLDAPIdentity - implements user auth against LDAP server
|
|
|
|
func (sts *stsAPIHandlers) AssumeRoleWithLDAPIdentity(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AssumeRoleWithLDAPIdentity")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, nil, stsLDAPPassword)
|
2020-06-05 01:07:55 -04:00
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// Parse the incoming form data.
|
2021-08-08 01:43:01 -04:00
|
|
|
if err := parseForm(r); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
if r.Form.Get(stsVersion) != stsAPIVersion {
|
2020-06-05 01:07:55 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter,
|
|
|
|
fmt.Errorf("Invalid STS API version %s, expecting %s", r.Form.Get("Version"), stsAPIVersion))
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
ldapUsername := r.Form.Get(stsLDAPUsername)
|
|
|
|
ldapPassword := r.Form.Get(stsLDAPPassword)
|
2019-09-09 19:12:29 -04:00
|
|
|
|
|
|
|
if ldapUsername == "" || ldapPassword == "" {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, fmt.Errorf("LDAPUsername and LDAPPassword cannot be empty"))
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-05 01:07:55 -04:00
|
|
|
action := r.Form.Get(stsAction)
|
|
|
|
switch action {
|
|
|
|
case ldapIdentity:
|
|
|
|
default:
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Unsupported action %s", action))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
sessionPolicyStr := r.Form.Get(stsPolicy)
|
2019-09-23 18:21:16 -04:00
|
|
|
// https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
|
|
|
|
// The plain text that you use for both inline and managed session
|
|
|
|
// policies shouldn't exceed 2048 characters.
|
|
|
|
if len(sessionPolicyStr) > 2048 {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Session policy should not exceed 2048 characters"))
|
2019-09-23 18:21:16 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sessionPolicyStr) > 0 {
|
|
|
|
sessionPolicy, err := iampolicy.ParseConfig(bytes.NewReader([]byte(sessionPolicyStr)))
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-09-23 18:21:16 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version in policy must not be empty
|
|
|
|
if sessionPolicy.Version == "" {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, fmt.Errorf("Version needs to be specified in session policy"))
|
2019-09-23 18:21:16 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 19:52:49 -05:00
|
|
|
ldapUserDN, groupDistNames, err := globalLDAPConfig.Bind(ldapUsername, ldapPassword)
|
2019-09-09 19:12:29 -04:00
|
|
|
if err != nil {
|
2021-01-18 00:54:32 -05:00
|
|
|
err = fmt.Errorf("LDAP server error: %w", err)
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-10 19:52:49 -05:00
|
|
|
// Check if this user or their groups have a policy applied.
|
2021-03-25 03:38:15 -04:00
|
|
|
ldapPolicies, _ := globalIAMSys.PolicyDBGet(ldapUserDN, false, groupDistNames...)
|
2021-06-24 15:00:06 -04:00
|
|
|
if len(ldapPolicies) == 0 && globalPolicyOPA == nil {
|
2021-03-23 18:15:51 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue,
|
|
|
|
fmt.Errorf("expecting a policy to be set for user `%s` or one of their groups: `%s` - rejecting this request",
|
|
|
|
ldapUserDN, strings.Join(groupDistNames, "`,`")))
|
2021-02-10 19:52:49 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:13:21 -04:00
|
|
|
expiryDur, err := globalLDAPConfig.GetExpiryDuration(r.Form.Get(stsDurationSeconds))
|
|
|
|
if err != nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
m := map[string]interface{}{
|
2021-07-11 21:38:52 -04:00
|
|
|
expClaim: UTCNow().Add(expiryDur).Unix(),
|
|
|
|
ldapUser: ldapUserDN,
|
|
|
|
ldapUserN: ldapUsername,
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2019-09-23 18:21:16 -04:00
|
|
|
if len(sessionPolicyStr) > 0 {
|
|
|
|
m[iampolicy.SessionPolicyName] = base64.StdEncoding.EncodeToString([]byte(sessionPolicyStr))
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
secret := globalActiveCred.SecretKey
|
2019-09-09 19:12:29 -04:00
|
|
|
cred, err := auth.GetNewCredentialsWithMetadata(m, secret)
|
|
|
|
if err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
// Set the parent of the temporary access key, this is useful
|
|
|
|
// in obtaining service accounts by this cred.
|
2021-01-18 00:54:32 -05:00
|
|
|
cred.ParentUser = ldapUserDN
|
2020-04-28 15:49:56 -04:00
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
// Set this value to LDAP groups, LDAP user can be part
|
|
|
|
// of large number of groups
|
2021-02-10 19:52:49 -05:00
|
|
|
cred.Groups = groupDistNames
|
2020-05-20 14:33:35 -04:00
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
// Set the newly generated credentials, policyName is empty on purpose
|
|
|
|
// LDAP policies are applied automatically using their ldapUser, ldapGroups
|
|
|
|
// mapping.
|
|
|
|
if err = globalIAMSys.SetTempUser(cred.AccessKey, cred, ""); err != nil {
|
2020-05-21 12:09:18 -04:00
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
2019-09-09 19:12:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload temp users
|
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(cred.AccessKey, true) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapIdentityResponse := &AssumeRoleWithLDAPResponse{
|
|
|
|
Result: LDAPIdentityResult{
|
|
|
|
Credentials: cred,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ldapIdentityResponse.ResponseMetadata.RequestID = w.Header().Get(xhttp.AmzRequestID)
|
|
|
|
encodedSuccessResponse := encodeResponse(ldapIdentityResponse)
|
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodedSuccessResponse)
|
|
|
|
}
|
2021-09-07 22:03:48 -04:00
|
|
|
|
|
|
|
// AssumeRoleWithCertificate implements user authentication with client certificates.
|
|
|
|
// It verifies the client-provided X.509 certificate, maps the certificate to an S3 policy
|
|
|
|
// and returns temp. S3 credentials to the client.
|
|
|
|
//
|
|
|
|
// API endpoint: https://minio:9000?Action=AssumeRoleWithCertificate&Version=2011-06-15
|
|
|
|
func (sts *stsAPIHandlers) AssumeRoleWithCertificate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = newContext(r, w, "AssumeRoleWithCertificate")
|
|
|
|
|
|
|
|
if !globalSTSTLSConfig.Enabled {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSNotInitialized, errors.New("STS API 'AssumeRoleWithCertificate' is disabled"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to establish a TLS connection and the
|
|
|
|
// client must provide exactly one client certificate.
|
|
|
|
// Otherwise, we don't have a certificate to verify or
|
|
|
|
// the policy lookup would ambigious.
|
|
|
|
if r.TLS == nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInsecureConnection, errors.New("No TLS connection attempt"))
|
|
|
|
return
|
|
|
|
}
|
2021-09-17 12:37:01 -04:00
|
|
|
|
|
|
|
// A client may send a certificate chain such that we end up
|
|
|
|
// with multiple peer certificates. However, we can only accept
|
|
|
|
// a single client certificate. Otherwise, the certificate to
|
|
|
|
// policy mapping would be ambigious.
|
|
|
|
// However, we can filter all CA certificates and only check
|
|
|
|
// whether they client has sent exactly one (non-CA) leaf certificate.
|
|
|
|
var peerCertificates = make([]*x509.Certificate, 0, len(r.TLS.PeerCertificates))
|
|
|
|
for _, cert := range r.TLS.PeerCertificates {
|
|
|
|
if cert.IsCA {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
peerCertificates = append(peerCertificates, cert)
|
|
|
|
}
|
|
|
|
r.TLS.PeerCertificates = peerCertificates
|
|
|
|
|
|
|
|
// Now, we have to check that the client has provided exactly one leaf
|
|
|
|
// certificate that we can map to a policy.
|
2021-09-07 22:03:48 -04:00
|
|
|
if len(r.TLS.PeerCertificates) == 0 {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, errors.New("No client certificate provided"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(r.TLS.PeerCertificates) > 1 {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidParameterValue, errors.New("More than one client certificate provided"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var certificate = r.TLS.PeerCertificates[0]
|
2021-09-17 12:37:01 -04:00
|
|
|
if !globalSTSTLSConfig.InsecureSkipVerify { // Verify whether the client certificate has been issued by a trusted CA.
|
2021-09-07 22:03:48 -04:00
|
|
|
_, err := certificate.Verify(x509.VerifyOptions{
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
Roots: globalRootCAs,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInvalidClientCertificate, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We map the X.509 subject common name to the policy. So, a client
|
|
|
|
// with the common name "foo" will be associated with the policy "foo".
|
|
|
|
// Other mapping functions - e.g. public-key hash based mapping - are
|
|
|
|
// possible but not implemented.
|
|
|
|
//
|
|
|
|
// Group mapping is not possible with standard X.509 certificates.
|
|
|
|
if certificate.Subject.CommonName == "" {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, errors.New("certificate subject CN cannot be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expiry, err := globalSTSTLSConfig.GetExpiryDuration(r.Form.Get(stsDurationSeconds))
|
|
|
|
if err != nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSMissingParameter, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set the expiry of the temp. credentials to the minimum of the
|
|
|
|
// configured expiry and the duration until the certificate itself
|
|
|
|
// expires.
|
|
|
|
// We must not issue credentials that out-live the certificate.
|
|
|
|
if validUntil := time.Until(certificate.NotAfter); validUntil < expiry {
|
|
|
|
expiry = validUntil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Associate any service accounts to the certificate CN
|
|
|
|
parentUser := "tls:" + certificate.Subject.CommonName
|
|
|
|
|
|
|
|
tmpCredentials, err := auth.GetNewCredentialsWithMetadata(map[string]interface{}{
|
|
|
|
expClaim: time.Now().UTC().Add(expiry).Unix(),
|
|
|
|
parentClaim: parentUser,
|
|
|
|
subClaim: certificate.Subject.CommonName,
|
|
|
|
audClaim: certificate.Subject.Organization,
|
|
|
|
issClaim: certificate.Issuer.CommonName,
|
|
|
|
}, globalActiveCred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpCredentials.ParentUser = parentUser
|
|
|
|
err = globalIAMSys.SetTempUser(tmpCredentials.AccessKey, tmpCredentials, certificate.Subject.CommonName)
|
|
|
|
if err != nil {
|
|
|
|
writeSTSErrorResponse(ctx, w, true, ErrSTSInternalError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = new(AssumeRoleWithCertificateResponse)
|
|
|
|
response.Result.Credentials = tmpCredentials
|
|
|
|
response.Metadata.RequestID = w.Header().Get(xhttp.AmzRequestID)
|
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
|
|
|
}
|