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.
289 lines
8.7 KiB
289 lines
8.7 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 (
|
||
|
"bytes"
|
||
8 years ago
|
"fmt"
|
||
9 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
|
||
|
mux "github.com/gorilla/mux"
|
||
9 years ago
|
"github.com/minio/minio/pkg/wildcard"
|
||
9 years ago
|
)
|
||
|
|
||
|
// maximum supported access policy size.
|
||
9 years ago
|
const maxAccessPolicySize = 20 * 1024 // 20KiB.
|
||
9 years ago
|
|
||
|
// Verify if a given action is valid for the url path based on the
|
||
|
// existing bucket access policy.
|
||
9 years ago
|
func bucketPolicyEvalStatements(action string, resource string, conditions map[string]string, statements []policyStatement) bool {
|
||
9 years ago
|
for _, statement := range statements {
|
||
|
if bucketPolicyMatchStatement(action, resource, conditions, statement) {
|
||
|
if statement.Effect == "Allow" {
|
||
|
return true
|
||
|
}
|
||
9 years ago
|
// Do not uncomment kept here for readability.
|
||
9 years ago
|
// else statement.Effect == "Deny"
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
// None match so deny.
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Verify if action, resource and conditions match input policy statement.
|
||
9 years ago
|
func bucketPolicyMatchStatement(action string, resource string, conditions map[string]string, statement policyStatement) bool {
|
||
9 years ago
|
// Verify if action matches.
|
||
|
if bucketPolicyActionMatch(action, statement) {
|
||
|
// Verify if resource matches.
|
||
|
if bucketPolicyResourceMatch(resource, statement) {
|
||
|
// Verify if condition matches.
|
||
|
if bucketPolicyConditionMatch(conditions, statement) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Verify if given action matches with policy statement.
|
||
9 years ago
|
func bucketPolicyActionMatch(action string, statement policyStatement) bool {
|
||
9 years ago
|
for _, policyAction := range statement.Actions {
|
||
9 years ago
|
if matched := actionMatch(policyAction, action); matched {
|
||
9 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
9 years ago
|
// Match function matches wild cards in 'pattern' for resource.
|
||
|
func resourceMatch(pattern, resource string) bool {
|
||
9 years ago
|
return wildcard.MatchExtended(pattern, resource)
|
||
9 years ago
|
}
|
||
|
|
||
|
// Match function matches wild cards in 'pattern' for action.
|
||
|
func actionMatch(pattern, action string) bool {
|
||
9 years ago
|
return wildcard.Match(pattern, action)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Verify if given resource matches with policy statement.
|
||
|
func bucketPolicyResourceMatch(resource string, statement policyStatement) bool {
|
||
|
for _, resourcep := range statement.Resources {
|
||
9 years ago
|
// the resource rule for object could contain "*" wild card.
|
||
|
// the requested object can be given access based on the already set bucket policy if
|
||
|
// the match is successful.
|
||
|
// More info: http://docs.aws.amazon.com/AmazonS3/latest/dev/s3-arn-format.html .
|
||
9 years ago
|
if matched := resourceMatch(resourcep, resource); matched {
|
||
|
return true
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
|
|
||
|
// Verify if given condition matches with policy statement.
|
||
9 years ago
|
func bucketPolicyConditionMatch(conditions map[string]string, statement policyStatement) bool {
|
||
9 years ago
|
// Supports following conditions.
|
||
|
// - StringEquals
|
||
|
// - StringNotEquals
|
||
|
//
|
||
|
// Supported applicable condition keys for each conditions.
|
||
|
// - s3:prefix
|
||
|
// - s3:max-keys
|
||
|
var conditionMatches = true
|
||
|
for condition, conditionKeys := range statement.Conditions {
|
||
|
if condition == "StringEquals" {
|
||
|
if conditionKeys["s3:prefix"] != conditions["prefix"] {
|
||
|
conditionMatches = false
|
||
|
break
|
||
|
}
|
||
|
if conditionKeys["s3:max-keys"] != conditions["max-keys"] {
|
||
|
conditionMatches = false
|
||
|
break
|
||
|
}
|
||
|
} else if condition == "StringNotEquals" {
|
||
|
if conditionKeys["s3:prefix"] == conditions["prefix"] {
|
||
|
conditionMatches = false
|
||
|
break
|
||
|
}
|
||
|
if conditionKeys["s3:max-keys"] == conditions["max-keys"] {
|
||
|
conditionMatches = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return conditionMatches
|
||
|
}
|
||
|
|
||
|
// PutBucketPolicyHandler - PUT Bucket policy
|
||
|
// -----------------
|
||
|
// This implementation of the PUT operation uses the policy
|
||
|
// subresource to add to or replace a policy on a bucket
|
||
9 years ago
|
func (api objectAPIHandlers) PutBucketPolicyHandler(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
vars := mux.Vars(r)
|
||
|
bucket := vars["bucket"]
|
||
9 years ago
|
switch getRequestAuthType(r) {
|
||
|
default:
|
||
|
// For all unknown auth types return error.
|
||
|
writeErrorResponse(w, r, ErrAccessDenied, r.URL.Path)
|
||
|
return
|
||
|
case authTypePresigned, authTypeSigned:
|
||
|
if s3Error := isReqAuthenticated(r); s3Error != ErrNone {
|
||
|
writeErrorResponse(w, r, s3Error, r.URL.Path)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// If Content-Length is unknown or zero, deny the
|
||
|
// request. PutBucketPolicy always needs a Content-Length if
|
||
|
// incoming request is not chunked.
|
||
|
if !contains(r.TransferEncoding, "chunked") {
|
||
|
if r.ContentLength == -1 || r.ContentLength == 0 {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrMissingContentLength, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
// If Content-Length is greater than maximum allowed policy size.
|
||
|
if r.ContentLength > maxAccessPolicySize {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrEntityTooLarge, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Read access policy up to maxAccessPolicySize.
|
||
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/access-policy-language-overview.html
|
||
|
// bucket policies are limited to 20KB in size, using a limit reader.
|
||
8 years ago
|
policyBytes, err := ioutil.ReadAll(io.LimitReader(r.Body, maxAccessPolicySize))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
errorIf(err, "Unable to read from client.")
|
||
|
writeErrorResponse(w, r, toAPIErrorCode(err), r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
// Parse bucket policy.
|
||
8 years ago
|
var policy = &bucketPolicy{}
|
||
|
err = parseBucketPolicy(bytes.NewReader(policyBytes), policy)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
errorIf(err, "Unable to parse bucket policy.")
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInvalidPolicyDocument, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
// Parse check bucket policy.
|
||
8 years ago
|
if s3Error := checkBucketPolicyResources(bucket, policy); s3Error != ErrNone {
|
||
9 years ago
|
writeErrorResponse(w, r, s3Error, r.URL.Path)
|
||
|
return
|
||
9 years ago
|
}
|
||
|
|
||
|
// Save bucket policy.
|
||
8 years ago
|
if err = writeBucketPolicy(bucket, api.ObjectAPI, bytes.NewReader(policyBytes), int64(len(policyBytes))); err != nil {
|
||
9 years ago
|
errorIf(err, "Unable to write bucket policy.")
|
||
9 years ago
|
switch err.(type) {
|
||
9 years ago
|
case BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInvalidBucketName, r.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInternalError, r.URL.Path)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
// Set the bucket policy in memory.
|
||
|
globalBucketPolicies.SetBucketPolicy(bucket, policy)
|
||
|
|
||
|
// Success.
|
||
9 years ago
|
writeSuccessNoContent(w)
|
||
|
}
|
||
|
|
||
|
// DeleteBucketPolicyHandler - DELETE Bucket policy
|
||
|
// -----------------
|
||
|
// This implementation of the DELETE operation uses the policy
|
||
|
// subresource to add to remove a policy on a bucket.
|
||
9 years ago
|
func (api objectAPIHandlers) DeleteBucketPolicyHandler(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
vars := mux.Vars(r)
|
||
|
bucket := vars["bucket"]
|
||
|
|
||
9 years ago
|
switch getRequestAuthType(r) {
|
||
|
default:
|
||
|
// For all unknown auth types return error.
|
||
|
writeErrorResponse(w, r, ErrAccessDenied, r.URL.Path)
|
||
9 years ago
|
return
|
||
9 years ago
|
case authTypePresigned, authTypeSigned:
|
||
|
if s3Error := isReqAuthenticated(r); s3Error != ErrNone {
|
||
|
writeErrorResponse(w, r, s3Error, r.URL.Path)
|
||
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
// Delete bucket access policy.
|
||
8 years ago
|
if err := removeBucketPolicy(bucket, api.ObjectAPI); err != nil {
|
||
9 years ago
|
errorIf(err, "Unable to remove bucket policy.")
|
||
9 years ago
|
switch err.(type) {
|
||
9 years ago
|
case BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInvalidBucketName, r.URL.Path)
|
||
9 years ago
|
case BucketPolicyNotFound:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrNoSuchBucketPolicy, r.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInternalError, r.URL.Path)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
// Remove bucket policy.
|
||
|
globalBucketPolicies.RemoveBucketPolicy(bucket)
|
||
|
|
||
|
// Success.
|
||
9 years ago
|
writeSuccessNoContent(w)
|
||
|
}
|
||
|
|
||
|
// GetBucketPolicyHandler - GET Bucket policy
|
||
|
// -----------------
|
||
|
// This operation uses the policy
|
||
|
// subresource to return the policy of a specified bucket.
|
||
9 years ago
|
func (api objectAPIHandlers) GetBucketPolicyHandler(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
vars := mux.Vars(r)
|
||
|
bucket := vars["bucket"]
|
||
|
|
||
9 years ago
|
switch getRequestAuthType(r) {
|
||
|
default:
|
||
|
// For all unknown auth types return error.
|
||
|
writeErrorResponse(w, r, ErrAccessDenied, r.URL.Path)
|
||
9 years ago
|
return
|
||
9 years ago
|
case authTypePresigned, authTypeSigned:
|
||
|
if s3Error := isReqAuthenticated(r); s3Error != ErrNone {
|
||
|
writeErrorResponse(w, r, s3Error, r.URL.Path)
|
||
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
// Read bucket access policy.
|
||
8 years ago
|
policy, err := readBucketPolicy(bucket, api.ObjectAPI)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
errorIf(err, "Unable to read bucket policy.")
|
||
9 years ago
|
switch err.(type) {
|
||
9 years ago
|
case BucketNameInvalid:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInvalidBucketName, r.URL.Path)
|
||
9 years ago
|
case BucketPolicyNotFound:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrNoSuchBucketPolicy, r.URL.Path)
|
||
9 years ago
|
default:
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInternalError, r.URL.Path)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
// Write to client.
|
||
|
fmt.Fprint(w, policy)
|
||
9 years ago
|
}
|