2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultVersion - default policy version as per AWS S3 specification.
|
|
|
|
const DefaultVersion = "2012-10-17"
|
|
|
|
|
|
|
|
// Args - arguments to policy to check whether it is allowed
|
|
|
|
type Args struct {
|
2018-10-09 17:00:01 -04:00
|
|
|
AccountName string `json:"account"`
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups []string `json:"groups"`
|
2018-10-09 17:00:01 -04:00
|
|
|
Action Action `json:"action"`
|
|
|
|
BucketName string `json:"bucket"`
|
|
|
|
ConditionValues map[string][]string `json:"conditions"`
|
|
|
|
IsOwner bool `json:"owner"`
|
|
|
|
ObjectName string `json:"object"`
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Policy - bucket policy.
|
|
|
|
type Policy struct {
|
|
|
|
ID ID `json:"ID,omitempty"`
|
|
|
|
Version string
|
|
|
|
Statements []Statement `json:"Statement"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAllowed - checks given policy args is allowed to continue the Rest API.
|
|
|
|
func (policy Policy) IsAllowed(args Args) bool {
|
|
|
|
// Check all deny statements. If any one statement denies, return false.
|
|
|
|
for _, statement := range policy.Statements {
|
|
|
|
if statement.Effect == Deny {
|
|
|
|
if !statement.IsAllowed(args) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For owner, its allowed by default.
|
|
|
|
if args.IsOwner {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check all allow statements. If any one statement allows, return true.
|
|
|
|
for _, statement := range policy.Statements {
|
|
|
|
if statement.Effect == Allow {
|
|
|
|
if statement.IsAllowed(args) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty - returns whether policy is empty or not.
|
|
|
|
func (policy Policy) IsEmpty() bool {
|
|
|
|
return len(policy.Statements) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValid - checks if Policy is valid or not.
|
|
|
|
func (policy Policy) isValid() error {
|
2018-04-27 18:02:54 -04:00
|
|
|
if policy.Version != DefaultVersion && policy.Version != "" {
|
2020-01-03 14:28:52 -05:00
|
|
|
return Errorf("invalid version '%v'", policy.Version)
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, statement := range policy.Statements {
|
|
|
|
if err := statement.isValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 00:26:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON - encodes Policy to JSON data.
|
|
|
|
func (policy Policy) MarshalJSON() ([]byte, error) {
|
|
|
|
if err := policy.isValid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtype to avoid recursive call to MarshalJSON()
|
|
|
|
type subPolicy Policy
|
|
|
|
return json.Marshal(subPolicy(policy))
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:50:36 -04:00
|
|
|
// Merge merges two policies documents and drop
|
|
|
|
// duplicate statements if any.
|
|
|
|
func (policy Policy) Merge(input Policy) Policy {
|
|
|
|
var mergedPolicy Policy
|
|
|
|
if policy.Version != "" {
|
|
|
|
mergedPolicy.Version = policy.Version
|
|
|
|
} else {
|
|
|
|
mergedPolicy.Version = input.Version
|
|
|
|
}
|
|
|
|
for _, st := range policy.Statements {
|
|
|
|
mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone())
|
|
|
|
}
|
|
|
|
for _, st := range input.Statements {
|
|
|
|
mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone())
|
|
|
|
}
|
|
|
|
mergedPolicy.dropDuplicateStatements()
|
|
|
|
return mergedPolicy
|
|
|
|
}
|
|
|
|
|
2020-04-18 00:26:42 -04:00
|
|
|
func (policy *Policy) dropDuplicateStatements() {
|
|
|
|
redo:
|
2018-04-24 18:53:30 -04:00
|
|
|
for i := range policy.Statements {
|
2020-04-18 00:26:42 -04:00
|
|
|
for j, statement := range policy.Statements[i+1:] {
|
2021-03-16 11:50:36 -04:00
|
|
|
if !policy.Statements[i].Equals(statement) {
|
2018-04-24 18:53:30 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-04-18 00:26:42 -04:00
|
|
|
policy.Statements = append(policy.Statements[:j], policy.Statements[j+1:]...)
|
|
|
|
goto redo
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - decodes JSON data to Policy.
|
|
|
|
func (policy *Policy) UnmarshalJSON(data []byte) error {
|
|
|
|
// subtype to avoid recursive call to UnmarshalJSON()
|
|
|
|
type subPolicy Policy
|
|
|
|
var sp subPolicy
|
|
|
|
if err := json.Unmarshal(data, &sp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := Policy(sp)
|
|
|
|
if err := p.isValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-18 00:26:42 -04:00
|
|
|
p.dropDuplicateStatements()
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
*policy = p
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate - validates all statements are for given bucket or not.
|
|
|
|
func (policy Policy) Validate(bucketName string) error {
|
|
|
|
if err := policy.isValid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, statement := range policy.Statements {
|
|
|
|
if err := statement.Validate(bucketName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig - parses data in given reader to Policy.
|
|
|
|
func ParseConfig(reader io.Reader, bucketName string) (*Policy, error) {
|
|
|
|
var policy Policy
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(reader)
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
if err := decoder.Decode(&policy); err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return nil, Errorf("%w", err)
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := policy.Validate(bucketName)
|
|
|
|
return &policy, err
|
|
|
|
}
|