feat: Added integration test cases for Put/Get/DeleteBucketPolicy actions. Made some bug fixes in these actions implementations

This commit is contained in:
jonaustin09
2024-03-20 17:31:52 -04:00
parent d469a72213
commit af641e5368
9 changed files with 698 additions and 22 deletions
+5 -2
View File
@@ -45,10 +45,13 @@ type BucketPolicyItem struct {
}
func (bpi *BucketPolicyItem) Validate(bucket string, iam IAMService) error {
if err := bpi.Effect.Validate(); err != nil {
return err
}
if err := bpi.Principals.Validate(iam); err != nil {
return err
}
if err := bpi.Effect.Validate(); err != nil {
if err := bpi.Resources.Validate(bucket); err != nil {
return err
}
@@ -61,7 +64,7 @@ func (bpi *BucketPolicyItem) Validate(bucket string, iam IAMService) error {
return fmt.Errorf("unsupported object action '%v' on the specified resources", action)
}
if !isObjectAction && !containsBucketAction {
return fmt.Errorf("unsupported bucket action '%v', on the specified resources", action)
return fmt.Errorf("unsupported bucket action '%v' on the specified resources", action)
}
}
+18 -6
View File
@@ -133,20 +133,31 @@ var supportedObjectActionList = map[Action]struct{}{
}
// Validates Action: it should either wildcard match with supported actions list or be in it
func (a Action) IsValid() bool {
func (a Action) IsValid() error {
if !strings.HasPrefix(string(a), "s3:") {
return fmt.Errorf("invalid action: %v", a)
}
if a == AllActions {
return nil
}
if a[len(a)-1] == '*' {
pattern := strings.TrimSuffix(string(a), "*")
for act := range supportedActionList {
if strings.HasPrefix(string(act), pattern) {
return true
return nil
}
}
return false
return fmt.Errorf("invalid wildcard usage: %v prefix is not in the supported actions list", pattern)
}
_, found := supportedActionList[a]
return found
if !found {
return fmt.Errorf("unsupported action: %v", a)
}
return nil
}
// Checks if the action is object action
@@ -203,8 +214,9 @@ func (a *Actions) UnmarshalJSON(data []byte) error {
// Validates and adds a new Action to Actions map
func (a Actions) Add(str string) error {
action := Action(str)
if !action.IsValid() {
return fmt.Errorf("invalid action")
err := action.IsValid()
if err != nil {
return err
}
a[action] = struct{}{}
+1 -1
View File
@@ -79,7 +79,7 @@ func (p Principals) Validate(iam IAMService) error {
return err
}
if len(accs) > 0 {
return fmt.Errorf("users doesn't exist: %v", accs)
return fmt.Errorf("user accounts don't exist: %v", accs)
}
return nil
+8 -9
View File
@@ -58,15 +58,14 @@ func (r *Resources) UnmarshalJSON(data []byte) error {
// Adds and validates a new resource to Resources map
func (r Resources) Add(rc string) error {
_, found := r[rc]
if found {
return fmt.Errorf("duplicate resource")
ok, pattern := isValidResource(rc)
if !ok {
return fmt.Errorf("invalid resource: %v", rc)
}
ok, pattern := isValidResource(rc)
if !ok {
return fmt.Errorf("invalid resource")
_, found := r[pattern]
if found {
return fmt.Errorf("duplicate resource: %v", rc)
}
r[pattern] = struct{}{}
@@ -100,7 +99,7 @@ func (r Resources) ContainsBucketPattern() bool {
func (r Resources) Validate(bucket string) error {
for resource := range r {
if !strings.HasPrefix(resource, bucket) {
return fmt.Errorf("invalid bucket resource")
return fmt.Errorf("incorrect bucket name in %v", resource)
}
}
@@ -118,7 +117,7 @@ func isValidResource(rc string) (isValid bool, pattern string) {
return false, ""
}
// The resource can't start with / (bucket name comes first)
if res == "/" {
if strings.HasPrefix(res, "/") {
return false, ""
}