addressed reviewer's feedback

This commit is contained in:
Souhaila Noor 2021-12-17 16:46:07 -06:00
parent f05f1d0de2
commit a606bd030a
4 changed files with 15 additions and 48 deletions

View file

@ -70,7 +70,7 @@ func (p *GroupPermissions) ReadPermissionsFile(permsFile io.Reader) (err error)
return fmt.Errorf("unmarshalling permissions failed with error: %s", err)
}
return nil
return
}
func (p *GroupPermissions) GetPermissions(groups []Group, index string) (permission string, errors error) {
@ -91,7 +91,7 @@ func (p *GroupPermissions) GetPermissions(groups []Group, index string) (permiss
if perm, ok := p.Permissions[group.GroupID][index]; ok {
allPermissions[perm] = true
} else {
return "", fmt.Errorf("User %s does not have permission to index %s", group.UserID, index)
return "", fmt.Errorf("user %s does not have permission to index %s", group.UserID, index)
}
} else {
groupsDenied = append(groupsDenied, group.GroupID)
@ -133,6 +133,10 @@ func (p *GroupPermissions) GetAuthorizedIndexList(groups []Group, desiredPermiss
for index, permission := range p.Permissions[group.GroupID] {
if permission == desiredPermission {
indexList = append(indexList, index)
} else if permission == "admin" {
indexList = append(indexList, index)
} else if permission == "write" && desiredPermission == "read" {
indexList = append(indexList, index)
}
}
}

View file

@ -16,6 +16,7 @@ package authz_test
import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
@ -227,7 +228,7 @@ func TestAuth_GetAuthorizedIndexList(t *testing.T) {
"dca35310-ecda-4f23-86cd-876aee55906b": {
"test1": "admin",
"test2": "read",
"test3": "read",
"test3": "write",
},
}}
@ -239,7 +240,7 @@ func TestAuth_GetAuthorizedIndexList(t *testing.T) {
{
group,
"read",
[]string{"test2", "test3"},
[]string{"test1", "test2", "test3"},
},
{
group,
@ -249,7 +250,7 @@ func TestAuth_GetAuthorizedIndexList(t *testing.T) {
{
group,
"write",
nil,
[]string{"test1", "test3"},
},
}
@ -257,6 +258,7 @@ func TestAuth_GetAuthorizedIndexList(t *testing.T) {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
indexList := p.GetAuthorizedIndexList(test.groups, test.permission)
sort.Strings(indexList)
if !reflect.DeepEqual(indexList, test.output) {
t.Errorf("expected %s, but got %s", test.output, indexList)

View file

@ -601,7 +601,7 @@ func lookupAddr(ctx context.Context, resolver *net.Resolver, host string) (strin
func (c *Config) ValidateAuth() (errors []error) {
if !c.Auth.Enable {
return errors
return
}
authConfig := map[string]string{
"ClientId": c.Auth.ClientId,
@ -626,11 +626,7 @@ func (c *Config) ValidateAuth() (errors []error) {
}
}
}
if len(errors) > 0 {
return errors
}
return nil
return errors
}
func (c *Config) ValidatePermissions(permsFile io.Reader) (errors []error) {
@ -667,11 +663,7 @@ func (c *Config) ValidatePermissions(permsFile io.Reader) (errors []error) {
}
}
}
if len(errors) > 0 {
return errors
}
return nil
return errors
}
func (c *Config) ValidatePermissionsFile() (err error) {
@ -684,7 +676,7 @@ func (c *Config) ValidatePermissionsFile() (err error) {
if (fileExt != ".yaml") && (fileExt != ".yml") {
return fmt.Errorf("invalid file extension for auth config permissions file: %s", c.Auth.PermissionsFile)
}
return nil
return
}
func (c *Config) MustValidateAuth() {

View file

@ -237,37 +237,6 @@ func (m *Command) Start() (err error) {
if err = p.ReadPermissionsFile(permsFile); err != nil {
return err
}
groups := []authz.Group{
{
UserID: "user-id",
GroupID: "dca35310-ecda-4f23-86cd-876aee55906b",
GroupName: "group-name",
},
// {
// UserID: "user-id",
// GroupID: "dca35310-ecda-4f23-86cd-876aee559900",
// GroupName: "group-name",
// },
}
index := "test"
perm, err := p.GetPermissions(groups, index)
fmt.Printf("\nuser has %s access to index %s\n", perm, index)
if err != nil {
fmt.Printf("\np: %s, err: %s\n", perm, err.Error())
}
adminAccess := p.IsAdmin(groups)
fmt.Printf("\nAdminAccess: %t\n", adminAccess)
accessList := []string{"read", "write", "admin"}
for _, a := range accessList {
indexList := p.GetAuthorizedIndexList(groups, a)
fmt.Printf("\nPermission requested: %s, Index List: %s\n", a, indexList)
}
}
// Initialize server.