2018-04-25 00:53:30 +02:00
|
|
|
/*
|
2020-06-13 05:04:01 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2018,2020 MinIO, Inc.
|
2018-04-25 00:53:30 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2020-04-01 09:04:25 +02:00
|
|
|
"net/url"
|
2020-04-06 22:44:16 +02:00
|
|
|
"strconv"
|
2020-04-01 09:04:25 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2018-04-25 00:53:30 +02:00
|
|
|
|
2020-01-08 12:31:43 +01:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-07-14 18:38:05 +02:00
|
|
|
miniogopolicy "github.com/minio/minio-go/v7/pkg/policy"
|
2020-04-01 09:04:25 +02:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2018-04-28 00:02:54 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-01-27 23:12:34 +01:00
|
|
|
"github.com/minio/minio/pkg/bucket/policy"
|
2018-04-25 00:53:30 +02:00
|
|
|
"github.com/minio/minio/pkg/handlers"
|
|
|
|
)
|
|
|
|
|
2018-06-06 21:52:56 +02:00
|
|
|
// PolicySys - policy subsystem.
|
2020-05-20 19:18:15 +02:00
|
|
|
type PolicySys struct{}
|
2018-04-25 00:53:30 +02:00
|
|
|
|
2020-05-19 22:53:54 +02:00
|
|
|
// Get returns stored bucket policy
|
|
|
|
func (sys *PolicySys) Get(bucket string) (*policy.Policy, error) {
|
2020-05-20 19:18:15 +02:00
|
|
|
return globalBucketMetadataSys.GetPolicyConfig(bucket)
|
2018-04-25 00:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAllowed - checks given policy args is allowed to continue the Rest API.
|
|
|
|
func (sys *PolicySys) IsAllowed(args policy.Args) bool {
|
2020-05-20 19:18:15 +02:00
|
|
|
p, err := sys.Get(args.BucketName)
|
|
|
|
if err == nil {
|
2020-05-19 22:53:54 +02:00
|
|
|
return p.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:18:15 +02:00
|
|
|
// Log unhandled errors.
|
|
|
|
if _, ok := err.(BucketPolicyNotFound); !ok {
|
|
|
|
logger.LogIf(GlobalContext, err)
|
2018-04-25 00:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// As policy is not available for given bucket name, returns IsOwner i.e.
|
|
|
|
// operation is allowed only for owner.
|
|
|
|
return args.IsOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPolicySys - creates new policy system.
|
|
|
|
func NewPolicySys() *PolicySys {
|
2020-05-20 19:18:15 +02:00
|
|
|
return &PolicySys{}
|
2018-04-25 00:53:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
func getConditionValues(r *http.Request, lc string, username string, claims map[string]interface{}) map[string][]string {
|
2019-01-21 05:57:14 +01:00
|
|
|
currTime := UTCNow()
|
2020-04-01 09:04:25 +02:00
|
|
|
|
|
|
|
principalType := "Anonymous"
|
|
|
|
if username != "" {
|
|
|
|
principalType = "User"
|
2020-08-18 02:39:55 +02:00
|
|
|
if len(claims) > 0 {
|
|
|
|
principalType = "AssumedRole"
|
|
|
|
}
|
|
|
|
if username == globalActiveCred.AccessKey {
|
|
|
|
principalType = "Account"
|
|
|
|
}
|
2020-04-01 09:04:25 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
vid := r.URL.Query().Get("versionId")
|
|
|
|
if vid == "" {
|
|
|
|
if u, err := url.Parse(r.Header.Get(xhttp.AmzCopySource)); err == nil {
|
|
|
|
vid = u.Query().Get("versionId")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:34:15 +02:00
|
|
|
authType := getRequestAuthType(r)
|
|
|
|
var signatureVersion string
|
|
|
|
switch authType {
|
|
|
|
case authTypeSignedV2, authTypePresignedV2:
|
|
|
|
signatureVersion = signV2Algorithm
|
|
|
|
case authTypeSigned, authTypePresigned, authTypeStreamingSigned, authTypePostPolicy:
|
|
|
|
signatureVersion = signV4Algorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
var authtype string
|
|
|
|
switch authType {
|
|
|
|
case authTypePresignedV2, authTypePresigned:
|
|
|
|
authtype = "REST-QUERY-STRING"
|
|
|
|
case authTypeSignedV2, authTypeSigned, authTypeStreamingSigned:
|
|
|
|
authtype = "REST-HEADER"
|
|
|
|
case authTypePostPolicy:
|
|
|
|
authtype = "POST"
|
|
|
|
}
|
|
|
|
|
2018-12-27 02:39:30 +01:00
|
|
|
args := map[string][]string{
|
2021-04-02 18:34:15 +02:00
|
|
|
"CurrentTime": {currTime.Format(time.RFC3339)},
|
|
|
|
"EpochTime": {strconv.FormatInt(currTime.Unix(), 10)},
|
|
|
|
"SecureTransport": {strconv.FormatBool(r.TLS != nil)},
|
|
|
|
"SourceIp": {handlers.GetSourceIP(r)},
|
|
|
|
"UserAgent": {r.UserAgent()},
|
|
|
|
"Referer": {r.Referer()},
|
|
|
|
"principaltype": {principalType},
|
|
|
|
"userid": {username},
|
|
|
|
"username": {username},
|
|
|
|
"versionid": {vid},
|
|
|
|
"signatureversion": {signatureVersion},
|
|
|
|
"authType": {authtype},
|
2018-12-27 02:39:30 +01:00
|
|
|
}
|
2018-04-25 00:53:30 +02:00
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
if lc != "" {
|
|
|
|
args["LocationConstraint"] = []string{lc}
|
2020-04-01 09:04:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
cloneHeader := r.Header.Clone()
|
2020-04-01 09:04:25 +02:00
|
|
|
|
|
|
|
for _, objLock := range []string{
|
|
|
|
xhttp.AmzObjectLockMode,
|
|
|
|
xhttp.AmzObjectLockLegalHold,
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate,
|
|
|
|
} {
|
|
|
|
if values, ok := cloneHeader[objLock]; ok {
|
|
|
|
args[strings.TrimPrefix(objLock, "X-Amz-")] = values
|
|
|
|
}
|
|
|
|
cloneHeader.Del(objLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, values := range cloneHeader {
|
2018-04-25 00:53:30 +02:00
|
|
|
if existingValues, found := args[key]; found {
|
|
|
|
args[key] = append(existingValues, values...)
|
|
|
|
} else {
|
|
|
|
args[key] = values
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 09:04:25 +02:00
|
|
|
var cloneURLValues = url.Values{}
|
2020-04-06 22:44:16 +02:00
|
|
|
for k, v := range r.URL.Query() {
|
2020-04-01 09:04:25 +02:00
|
|
|
cloneURLValues[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, objLock := range []string{
|
|
|
|
xhttp.AmzObjectLockMode,
|
|
|
|
xhttp.AmzObjectLockLegalHold,
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate,
|
|
|
|
} {
|
|
|
|
if values, ok := cloneURLValues[objLock]; ok {
|
|
|
|
args[strings.TrimPrefix(objLock, "X-Amz-")] = values
|
|
|
|
}
|
|
|
|
cloneURLValues.Del(objLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, values := range cloneURLValues {
|
2018-04-25 00:53:30 +02:00
|
|
|
if existingValues, found := args[key]; found {
|
|
|
|
args[key] = append(existingValues, values...)
|
|
|
|
} else {
|
|
|
|
args[key] = values
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:59:59 +02:00
|
|
|
// JWT specific values
|
|
|
|
for k, v := range claims {
|
|
|
|
vStr, ok := v.(string)
|
|
|
|
if ok {
|
2020-08-18 02:39:55 +02:00
|
|
|
// Special case for AD/LDAP STS users
|
|
|
|
if k == ldapUser {
|
2020-09-01 08:56:22 +02:00
|
|
|
args["user"] = []string{vStr}
|
|
|
|
} else {
|
|
|
|
args[k] = []string{vStr}
|
2020-08-18 02:39:55 +02:00
|
|
|
}
|
2019-10-16 17:59:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 09:04:25 +02:00
|
|
|
|
2018-04-25 00:53:30 +02:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
// PolicyToBucketAccessPolicy converts a MinIO policy into a minio-go policy data structure.
|
2018-04-25 00:53:30 +02:00
|
|
|
func PolicyToBucketAccessPolicy(bucketPolicy *policy.Policy) (*miniogopolicy.BucketAccessPolicy, error) {
|
|
|
|
// Return empty BucketAccessPolicy for empty bucket policy.
|
|
|
|
if bucketPolicy == nil {
|
|
|
|
return &miniogopolicy.BucketAccessPolicy{Version: policy.DefaultVersion}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(bucketPolicy)
|
|
|
|
if err != nil {
|
|
|
|
// This should not happen because bucketPolicy is valid to convert to JSON data.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var policyInfo miniogopolicy.BucketAccessPolicy
|
2020-01-08 12:31:43 +01:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
2018-04-25 00:53:30 +02:00
|
|
|
if err = json.Unmarshal(data, &policyInfo); err != nil {
|
|
|
|
// This should not happen because data is valid to JSON data.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &policyInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketAccessPolicyToPolicy - converts minio-go/policy.BucketAccessPolicy to policy.Policy.
|
|
|
|
func BucketAccessPolicyToPolicy(policyInfo *miniogopolicy.BucketAccessPolicy) (*policy.Policy, error) {
|
|
|
|
data, err := json.Marshal(policyInfo)
|
|
|
|
if err != nil {
|
|
|
|
// This should not happen because policyInfo is valid to convert to JSON data.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var bucketPolicy policy.Policy
|
2020-01-08 12:31:43 +01:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
2018-04-25 00:53:30 +02:00
|
|
|
if err = json.Unmarshal(data, &bucketPolicy); err != nil {
|
|
|
|
// This should not happen because data is valid to JSON data.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bucketPolicy, nil
|
|
|
|
}
|