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"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2018-04-24 18:53:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ActionSet - set of actions.
|
|
|
|
type ActionSet map[Action]struct{}
|
|
|
|
|
|
|
|
// Add - add action to the set.
|
|
|
|
func (actionSet ActionSet) Add(action Action) {
|
|
|
|
actionSet[action] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains - checks given action exists in the action set.
|
|
|
|
func (actionSet ActionSet) Contains(action Action) bool {
|
|
|
|
_, found := actionSet[action]
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2020-01-09 22:29:57 -05:00
|
|
|
// Equals - checks whether given action set is equal to current action set or not.
|
|
|
|
func (actionSet ActionSet) Equals(sactionSet ActionSet) bool {
|
|
|
|
// If length of set is not equal to length of given set, the
|
|
|
|
// set is not equal to given set.
|
|
|
|
if len(actionSet) != len(sactionSet) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// As both sets are equal in length, check each elements are equal.
|
|
|
|
for k := range actionSet {
|
|
|
|
if _, ok := sactionSet[k]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// Intersection - returns actions available in both ActionSet.
|
|
|
|
func (actionSet ActionSet) Intersection(sset ActionSet) ActionSet {
|
|
|
|
nset := NewActionSet()
|
|
|
|
for k := range actionSet {
|
|
|
|
if _, ok := sset[k]; ok {
|
|
|
|
nset.Add(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nset
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON - encodes ActionSet to JSON data.
|
|
|
|
func (actionSet ActionSet) MarshalJSON() ([]byte, error) {
|
|
|
|
if len(actionSet) == 0 {
|
2020-01-03 14:28:52 -05:00
|
|
|
return nil, Errorf("empty actions not allowed")
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(actionSet.ToSlice())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actionSet ActionSet) String() string {
|
|
|
|
actions := []string{}
|
|
|
|
for action := range actionSet {
|
|
|
|
actions = append(actions, string(action))
|
|
|
|
}
|
|
|
|
sort.Strings(actions)
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v", actions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSlice - returns slice of actions from the action set.
|
|
|
|
func (actionSet ActionSet) ToSlice() []Action {
|
|
|
|
actions := []Action{}
|
|
|
|
for action := range actionSet {
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
return actions
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:50:36 -04:00
|
|
|
// Clone clones ActionSet structure
|
|
|
|
func (actionSet ActionSet) Clone() ActionSet {
|
|
|
|
return NewActionSet(actionSet.ToSlice()...)
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// UnmarshalJSON - decodes JSON data to ActionSet.
|
|
|
|
func (actionSet *ActionSet) UnmarshalJSON(data []byte) error {
|
|
|
|
var sset set.StringSet
|
|
|
|
if err := json.Unmarshal(data, &sset); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sset) == 0 {
|
2020-01-03 14:28:52 -05:00
|
|
|
return Errorf("empty actions not allowed")
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*actionSet = make(ActionSet)
|
|
|
|
for _, s := range sset.ToSlice() {
|
|
|
|
action, err := parseAction(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
actionSet.Add(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewActionSet - creates new action set.
|
|
|
|
func NewActionSet(actions ...Action) ActionSet {
|
|
|
|
actionSet := make(ActionSet)
|
|
|
|
for _, action := range actions {
|
|
|
|
actionSet.Add(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return actionSet
|
|
|
|
}
|