2018-04-24 18:53:30 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-04-24 18:53:30 -04: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 (
|
2018-10-09 17:00:01 -04:00
|
|
|
"bytes"
|
2018-04-24 18:53:30 -04:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-12-26 20:39:30 -05:00
|
|
|
"fmt"
|
2018-04-24 18:53:30 -04:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2018-10-08 18:47:13 -04:00
|
|
|
"strings"
|
2018-04-24 18:53:30 -04:00
|
|
|
"sync"
|
|
|
|
|
2020-01-08 06:31:43 -05:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2019-05-29 19:35:12 -04:00
|
|
|
miniogopolicy "github.com/minio/minio-go/v6/pkg/policy"
|
2018-04-27 18:02:54 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2019-01-20 23:57:14 -05:00
|
|
|
"github.com/minio/minio/pkg/event"
|
2018-04-24 18:53:30 -04:00
|
|
|
"github.com/minio/minio/pkg/handlers"
|
|
|
|
"github.com/minio/minio/pkg/policy"
|
|
|
|
)
|
|
|
|
|
2018-06-06 15:52:56 -04:00
|
|
|
// PolicySys - policy subsystem.
|
2018-04-24 18:53:30 -04:00
|
|
|
type PolicySys struct {
|
|
|
|
sync.RWMutex
|
|
|
|
bucketPolicyMap map[string]policy.Policy
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set - sets policy to given bucket name. If policy is empty, existing policy is removed.
|
|
|
|
func (sys *PolicySys) Set(bucketName string, policy policy.Policy) {
|
2019-06-03 14:06:13 -04:00
|
|
|
if globalIsGateway {
|
|
|
|
// Set policy is a non-op under gateway mode.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
if policy.IsEmpty() {
|
|
|
|
delete(sys.bucketPolicyMap, bucketName)
|
|
|
|
} else {
|
|
|
|
sys.bucketPolicyMap[bucketName] = policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove - removes policy for given bucket name.
|
|
|
|
func (sys *PolicySys) Remove(bucketName string) {
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
delete(sys.bucketPolicyMap, bucketName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAllowed - checks given policy args is allowed to continue the Rest API.
|
|
|
|
func (sys *PolicySys) IsAllowed(args policy.Args) bool {
|
2019-06-03 14:06:13 -04:00
|
|
|
if globalIsGateway {
|
|
|
|
// When gateway is enabled, no cached value
|
|
|
|
// is used to validate bucket policies.
|
2019-11-09 12:27:23 -05:00
|
|
|
objAPI := newObjectLayerFn()
|
2019-06-03 14:06:13 -04:00
|
|
|
if objAPI != nil {
|
|
|
|
config, err := objAPI.GetBucketPolicy(context.Background(), args.BucketName)
|
|
|
|
if err == nil {
|
|
|
|
return config.IsAllowed(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-15 13:11:10 -04:00
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
2019-06-03 14:06:13 -04:00
|
|
|
// If policy is available for given bucket, check the policy.
|
|
|
|
if p, found := sys.bucketPolicyMap[args.BucketName]; found {
|
|
|
|
return p.IsAllowed(args)
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// As policy is not available for given bucket name, returns IsOwner i.e.
|
|
|
|
// operation is allowed only for owner.
|
|
|
|
return args.IsOwner
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:05:02 -04:00
|
|
|
// Loads policies for all buckets into PolicySys.
|
|
|
|
func (sys *PolicySys) load(buckets []BucketInfo, objAPI ObjectLayer) error {
|
2018-04-24 18:53:30 -04:00
|
|
|
for _, bucket := range buckets {
|
2018-06-27 02:59:48 -04:00
|
|
|
config, err := objAPI.GetBucketPolicy(context.Background(), bucket.Name)
|
2018-04-24 18:53:30 -04:00
|
|
|
if err != nil {
|
2018-06-06 15:52:56 -04:00
|
|
|
if _, ok := err.(BucketPolicyNotFound); ok {
|
|
|
|
sys.Remove(bucket.Name)
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
2018-06-06 15:52:56 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// This part is specifically written to handle migration
|
|
|
|
// when the Version string is empty, this was allowed
|
|
|
|
// in all previous minio releases but we need to migrate
|
|
|
|
// those policies by properly setting the Version string
|
|
|
|
// from now on.
|
|
|
|
if config.Version == "" {
|
|
|
|
logger.Info("Found in-consistent bucket policies, Migrating them for Bucket: (%s)", bucket.Name)
|
|
|
|
config.Version = policy.DefaultVersion
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
if err = savePolicyConfig(context.Background(), objAPI, bucket.Name, config); err != nil {
|
2018-06-06 15:52:56 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
2018-04-27 18:02:54 -04:00
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
2018-06-06 15:52:56 -04:00
|
|
|
sys.Set(bucket.Name, *config)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initializes policy system from policy.json of all buckets.
|
2019-10-01 20:05:02 -04:00
|
|
|
func (sys *PolicySys) Init(buckets []BucketInfo, objAPI ObjectLayer) error {
|
2018-06-06 15:52:56 -04:00
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2019-06-03 14:06:13 -04:00
|
|
|
// In gateway mode, we don't need to load the policies
|
|
|
|
// from the backend.
|
|
|
|
if globalIsGateway {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2018-10-08 18:47:13 -04:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
// Initializing policy needs a retry mechanism for
|
|
|
|
// the following reasons:
|
|
|
|
// - Read quorum is lost just after the initialization
|
|
|
|
// of the object layer.
|
2019-09-05 18:51:27 -04:00
|
|
|
retryTimerCh := newRetryTimerSimple(doneCh)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-retryTimerCh:
|
|
|
|
// Load PolicySys once during boot.
|
2019-10-01 20:05:02 -04:00
|
|
|
if err := sys.load(buckets, objAPI); err != nil {
|
2019-09-05 18:51:27 -04:00
|
|
|
if err == errDiskNotFound ||
|
|
|
|
strings.Contains(err.Error(), InsufficientReadQuorum{}.Error()) ||
|
|
|
|
strings.Contains(err.Error(), InsufficientWriteQuorum{}.Error()) {
|
|
|
|
logger.Info("Waiting for policy subsystem to be initialized..")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
2018-06-06 15:52:56 -04:00
|
|
|
}
|
2019-09-05 18:51:27 -04:00
|
|
|
return nil
|
|
|
|
case <-globalOSSignalCh:
|
|
|
|
return fmt.Errorf("Initializing Policy sub-system gracefully stopped")
|
2018-06-06 15:52:56 -04:00
|
|
|
}
|
2018-10-08 18:47:13 -04:00
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPolicySys - creates new policy system.
|
|
|
|
func NewPolicySys() *PolicySys {
|
|
|
|
return &PolicySys{
|
|
|
|
bucketPolicyMap: make(map[string]policy.Policy),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:59:59 -04:00
|
|
|
func getConditionValues(request *http.Request, locationConstraint string, username string, claims map[string]interface{}) map[string][]string {
|
2019-01-20 23:57:14 -05:00
|
|
|
currTime := UTCNow()
|
|
|
|
principalType := func() string {
|
|
|
|
if username != "" {
|
|
|
|
return "User"
|
|
|
|
}
|
|
|
|
return "Anonymous"
|
|
|
|
}()
|
2018-12-26 20:39:30 -05:00
|
|
|
args := map[string][]string{
|
2019-01-20 23:57:14 -05:00
|
|
|
"CurrenTime": {currTime.Format(event.AMZTimeFormat)},
|
|
|
|
"EpochTime": {fmt.Sprintf("%d", currTime.Unix())},
|
|
|
|
"principaltype": {principalType},
|
2018-12-26 20:39:30 -05:00
|
|
|
"SecureTransport": {fmt.Sprintf("%t", request.TLS != nil)},
|
2019-01-20 23:57:14 -05:00
|
|
|
"SourceIp": {handlers.GetSourceIP(request)},
|
2018-12-26 20:39:30 -05:00
|
|
|
"UserAgent": {request.UserAgent()},
|
|
|
|
"Referer": {request.Referer()},
|
2019-01-20 23:57:14 -05:00
|
|
|
"userid": {username},
|
|
|
|
"username": {username},
|
2018-12-26 20:39:30 -05:00
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
for key, values := range request.Header {
|
|
|
|
if existingValues, found := args[key]; found {
|
|
|
|
args[key] = append(existingValues, values...)
|
|
|
|
} else {
|
|
|
|
args[key] = values
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, values := range request.URL.Query() {
|
|
|
|
if existingValues, found := args[key]; found {
|
|
|
|
args[key] = append(existingValues, values...)
|
|
|
|
} else {
|
|
|
|
args[key] = values
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if locationConstraint != "" {
|
|
|
|
args["LocationConstraint"] = []string{locationConstraint}
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:59:59 -04:00
|
|
|
// JWT specific values
|
|
|
|
for k, v := range claims {
|
|
|
|
vStr, ok := v.(string)
|
|
|
|
if ok {
|
|
|
|
args[k] = []string{vStr}
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2018-06-27 02:59:48 -04:00
|
|
|
// getPolicyConfig - get policy config for given bucket name.
|
|
|
|
func getPolicyConfig(objAPI ObjectLayer, bucketName string) (*policy.Policy, error) {
|
2018-04-24 18:53:30 -04:00
|
|
|
// Construct path to policy.json for the given bucket.
|
|
|
|
configFile := path.Join(bucketConfigPrefix, bucketName, bucketPolicyConfig)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
configData, err := readConfig(context.Background(), objAPI, configFile)
|
2018-04-24 18:53:30 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = BucketPolicyNotFound{Bucket: bucketName}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return policy.ParseConfig(bytes.NewReader(configData), bucketName)
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
func savePolicyConfig(ctx context.Context, objAPI ObjectLayer, bucketName string, bucketPolicy *policy.Policy) error {
|
2018-04-24 18:53:30 -04:00
|
|
|
data, err := json.Marshal(bucketPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct path to policy.json for the given bucket.
|
|
|
|
configFile := path.Join(bucketConfigPrefix, bucketName, bucketPolicyConfig)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return saveConfig(ctx, objAPI, configFile, data)
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func removePolicyConfig(ctx context.Context, objAPI ObjectLayer, bucketName string) error {
|
|
|
|
// Construct path to policy.json for the given bucket.
|
|
|
|
configFile := path.Join(bucketConfigPrefix, bucketName, bucketPolicyConfig)
|
|
|
|
|
|
|
|
if err := objAPI.DeleteObject(ctx, minioMetaBucket, configFile); err != nil {
|
|
|
|
if _, ok := err.(ObjectNotFound); ok {
|
|
|
|
return BucketPolicyNotFound{Bucket: bucketName}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolicyToBucketAccessPolicy - converts policy.Policy to minio-go/policy.BucketAccessPolicy.
|
|
|
|
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 06:31:43 -05:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
2018-04-24 18:53:30 -04: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 06:31:43 -05:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
2018-04-24 18:53:30 -04: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
|
|
|
|
}
|