You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
5.0 KiB
181 lines
5.0 KiB
9 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"bytes"
|
||
8 years ago
|
"errors"
|
||
|
"io"
|
||
|
"path"
|
||
|
"sync"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
// Variable represents bucket policies in memory.
|
||
|
var globalBucketPolicies *bucketPolicies
|
||
|
|
||
|
// Global bucket policies list, policies are enforced on each bucket looking
|
||
|
// through the policies here.
|
||
|
type bucketPolicies struct {
|
||
|
rwMutex *sync.RWMutex
|
||
|
|
||
|
// Collection of 'bucket' policies.
|
||
|
bucketPolicyConfigs map[string]*bucketPolicy
|
||
|
}
|
||
|
|
||
|
// Fetch bucket policy for a given bucket.
|
||
|
func (bp bucketPolicies) GetBucketPolicy(bucket string) *bucketPolicy {
|
||
|
bp.rwMutex.RLock()
|
||
|
defer bp.rwMutex.RUnlock()
|
||
|
return bp.bucketPolicyConfigs[bucket]
|
||
|
}
|
||
|
|
||
|
// Set a new bucket policy for a bucket, this operation will overwrite
|
||
|
// any previous bucketpolicies for the bucket.
|
||
|
func (bp *bucketPolicies) SetBucketPolicy(bucket string, policy *bucketPolicy) error {
|
||
|
bp.rwMutex.Lock()
|
||
|
defer bp.rwMutex.Unlock()
|
||
|
if policy == nil {
|
||
|
return errors.New("invalid argument")
|
||
|
}
|
||
|
bp.bucketPolicyConfigs[bucket] = policy
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Remove bucket policy for a bucket, from in-memory map.
|
||
|
func (bp *bucketPolicies) RemoveBucketPolicy(bucket string) {
|
||
|
bp.rwMutex.Lock()
|
||
|
defer bp.rwMutex.Unlock()
|
||
|
delete(bp.bucketPolicyConfigs, bucket)
|
||
|
}
|
||
|
|
||
|
// Loads all bucket policies from persistent layer.
|
||
|
func loadAllBucketPolicies(objAPI ObjectLayer) (policies map[string]*bucketPolicy, err error) {
|
||
|
// List buckets to proceed loading all notification configuration.
|
||
|
buckets, err := objAPI.ListBuckets()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
policies = make(map[string]*bucketPolicy)
|
||
|
// Loads bucket policy.
|
||
|
for _, bucket := range buckets {
|
||
|
var policy *bucketPolicy
|
||
|
policy, err = readBucketPolicy(bucket.Name, objAPI)
|
||
|
if err != nil {
|
||
|
switch err.(type) {
|
||
|
case BucketPolicyNotFound:
|
||
|
continue
|
||
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
policies[bucket.Name] = policy
|
||
|
}
|
||
|
|
||
|
// Success.
|
||
|
return policies, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
// Intialize all bucket policies.
|
||
|
func initBucketPolicies(objAPI ObjectLayer) error {
|
||
|
if objAPI == nil {
|
||
|
return errInvalidArgument
|
||
|
}
|
||
|
// Read all bucket policies.
|
||
|
policies, err := loadAllBucketPolicies(objAPI)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
globalBucketPolicies = &bucketPolicies{
|
||
|
rwMutex: &sync.RWMutex{},
|
||
|
bucketPolicyConfigs: policies,
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// getOldBucketsConfigPath - get old buckets config path. (Only used for migrating old bucket policies)
|
||
9 years ago
|
func getOldBucketsConfigPath() (string, error) {
|
||
9 years ago
|
configPath, err := getConfigPath()
|
||
|
if err != nil {
|
||
9 years ago
|
return "", err
|
||
9 years ago
|
}
|
||
8 years ago
|
return path.Join(configPath, "buckets"), nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// readBucketPolicy - reads bucket policy for an input bucket, returns BucketPolicyNotFound
|
||
|
// if bucket policy is not found. This function also parses the bucket policy into an object.
|
||
|
func readBucketPolicy(bucket string, objAPI ObjectLayer) (*bucketPolicy, error) {
|
||
9 years ago
|
// Verify bucket is valid.
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return nil, BucketNameInvalid{Bucket: bucket}
|
||
9 years ago
|
}
|
||
9 years ago
|
policyPath := pathJoin(bucketConfigPrefix, bucket, policyJSON)
|
||
8 years ago
|
objInfo, err := objAPI.GetObjectInfo(minioMetaBucket, policyPath)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
if _, ok := err.(ObjectNotFound); ok {
|
||
|
return nil, BucketPolicyNotFound{Bucket: bucket}
|
||
|
}
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
9 years ago
|
var buffer bytes.Buffer
|
||
8 years ago
|
err = objAPI.GetObject(minioMetaBucket, policyPath, 0, objInfo.Size, &buffer)
|
||
9 years ago
|
if err != nil {
|
||
|
if _, ok := err.(ObjectNotFound); ok {
|
||
9 years ago
|
return nil, BucketPolicyNotFound{Bucket: bucket}
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
8 years ago
|
// Parse the saved policy.
|
||
|
var policy = &bucketPolicy{}
|
||
|
err = parseBucketPolicy(&buffer, policy)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
|
||
|
}
|
||
|
return policy, nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// removeBucketPolicy - removes any previously written bucket policy. Returns BucketPolicyNotFound
|
||
|
// if no policies are found.
|
||
8 years ago
|
func removeBucketPolicy(bucket string, objAPI ObjectLayer) error {
|
||
9 years ago
|
// Verify bucket is valid.
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return BucketNameInvalid{Bucket: bucket}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
policyPath := pathJoin(bucketConfigPrefix, bucket, policyJSON)
|
||
8 years ago
|
if err := objAPI.DeleteObject(minioMetaBucket, policyPath); err != nil {
|
||
9 years ago
|
if _, ok := err.(ObjectNotFound); ok {
|
||
9 years ago
|
return BucketPolicyNotFound{Bucket: bucket}
|
||
9 years ago
|
}
|
||
9 years ago
|
return err
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// writeBucketPolicy - save all bucket policies.
|
||
|
func writeBucketPolicy(bucket string, objAPI ObjectLayer, reader io.Reader, size int64) error {
|
||
9 years ago
|
// Verify if bucket path legal
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return BucketNameInvalid{Bucket: bucket}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
policyPath := pathJoin(bucketConfigPrefix, bucket, policyJSON)
|
||
8 years ago
|
_, err := objAPI.PutObject(minioMetaBucket, policyPath, size, reader, nil)
|
||
9 years ago
|
return err
|
||
9 years ago
|
}
|