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.
195 lines
4.0 KiB
195 lines
4.0 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package drivers
|
||
10 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io"
|
||
10 years ago
|
"strings"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// User - canonical
|
||
10 years ago
|
type User struct {
|
||
10 years ago
|
AWS string
|
||
|
}
|
||
|
|
||
10 years ago
|
// Statement - minio policy statement
|
||
10 years ago
|
type Statement struct {
|
||
10 years ago
|
Sid string
|
||
|
Effect string
|
||
10 years ago
|
Principal User
|
||
10 years ago
|
Action []string
|
||
|
Resource []string
|
||
10 years ago
|
// add Condition struct/var TODO - fix it in future if necessary
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// BucketPolicy - minio policy collection
|
||
10 years ago
|
type BucketPolicy struct {
|
||
|
Version string // date in 0000-00-00 format
|
||
10 years ago
|
Statement []Statement
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Resource delimiter
|
||
10 years ago
|
const (
|
||
10 years ago
|
MinioResource = "minio:::"
|
||
10 years ago
|
)
|
||
|
|
||
|
// TODO support canonical user
|
||
10 years ago
|
// Principal delimiter
|
||
10 years ago
|
const (
|
||
10 years ago
|
MinioPrincipal = "minio::"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// Action map
|
||
10 years ago
|
var SupportedActionMap = map[string]bool{
|
||
10 years ago
|
"*": true,
|
||
|
"minio:GetObject": true,
|
||
|
"minio:ListBucket": true,
|
||
|
"minio:PutObject": true,
|
||
|
"minio:CreateBucket": true,
|
||
|
"minio:GetBucketPolicy": true,
|
||
|
"minio:DeleteBucketPolicy": true,
|
||
|
"minio:ListAllMyBuckets": true,
|
||
|
"minio:PutBucketPolicy": true,
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Effect map
|
||
10 years ago
|
var SupportedEffectMap = map[string]bool{
|
||
10 years ago
|
"Allow": true,
|
||
|
"Deny": true,
|
||
|
}
|
||
|
|
||
|
func isValidAction(action []string) bool {
|
||
|
for _, a := range action {
|
||
10 years ago
|
if !SupportedActionMap[a] {
|
||
10 years ago
|
return false
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
return true
|
||
10 years ago
|
}
|
||
|
|
||
|
func isValidEffect(effect string) bool {
|
||
10 years ago
|
if SupportedEffectMap[effect] {
|
||
10 years ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func isValidResource(resources []string) bool {
|
||
10 years ago
|
var ok bool
|
||
10 years ago
|
for _, resource := range resources {
|
||
|
switch true {
|
||
10 years ago
|
case strings.HasPrefix(resource, AwsResource):
|
||
|
bucket := strings.SplitAfter(resource, AwsResource)[1]
|
||
10 years ago
|
ok = true
|
||
|
if len(bucket) == 0 {
|
||
|
ok = false
|
||
|
}
|
||
10 years ago
|
case strings.HasPrefix(resource, MinioResource):
|
||
|
bucket := strings.SplitAfter(resource, MinioResource)[1]
|
||
10 years ago
|
ok = true
|
||
|
if len(bucket) == 0 {
|
||
|
ok = false
|
||
|
}
|
||
|
default:
|
||
|
ok = false
|
||
|
}
|
||
|
}
|
||
|
return ok
|
||
|
}
|
||
|
|
||
|
func isValidPrincipal(principal string) bool {
|
||
10 years ago
|
var ok bool
|
||
10 years ago
|
if principal == "*" {
|
||
|
return true
|
||
|
}
|
||
|
switch true {
|
||
10 years ago
|
case strings.HasPrefix(principal, AwsPrincipal):
|
||
|
username := strings.SplitAfter(principal, AwsPrincipal)[1]
|
||
10 years ago
|
ok = true
|
||
|
if len(username) == 0 {
|
||
|
ok = false
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
case strings.HasPrefix(principal, MinioPrincipal):
|
||
|
username := strings.SplitAfter(principal, MinioPrincipal)[1]
|
||
10 years ago
|
ok = true
|
||
|
if len(username) == 0 {
|
||
|
ok = false
|
||
|
}
|
||
|
default:
|
||
|
ok = false
|
||
|
}
|
||
|
return ok
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// Parsepolicy - validate request body is proper JSON and in accordance with policy standards
|
||
10 years ago
|
func Parsepolicy(data io.Reader) (BucketPolicy, bool) {
|
||
|
var policy BucketPolicy
|
||
|
decoder := json.NewDecoder(data)
|
||
|
err := decoder.Decode(&policy)
|
||
|
if err != nil {
|
||
|
goto error
|
||
|
}
|
||
|
if len(policy.Version) == 0 {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
_, err = parseDate(policy.Version)
|
||
10 years ago
|
if err != nil {
|
||
|
goto error
|
||
|
}
|
||
|
if len(policy.Statement) == 0 {
|
||
|
goto error
|
||
|
}
|
||
|
|
||
|
for _, statement := range policy.Statement {
|
||
|
if len(statement.Sid) == 0 {
|
||
|
goto error
|
||
|
}
|
||
|
if len(statement.Effect) == 0 {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
if !isValidEffect(statement.Effect) {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if len(statement.Principal.AWS) == 0 {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
if !isValidPrincipal(statement.Principal.AWS) {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
if len(statement.Action) == 0 {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
if !isValidAction(statement.Action) && !isValidActionS3(statement.Action) {
|
||
10 years ago
|
goto error
|
||
|
}
|
||
10 years ago
|
if len(statement.Resource) == 0 {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if !isValidResource(statement.Resource) {
|
||
|
goto error
|
||
|
}
|
||
10 years ago
|
}
|
||
|
return policy, true
|
||
|
|
||
|
error:
|
||
|
return BucketPolicy{}, false
|
||
|
}
|