Skip to content

Conversation

adfost
Copy link
Contributor

@adfost adfost commented May 27, 2021

No description provided.

@adfost adfost changed the title User policies Bucket users test case May 27, 2021
@dvaldivia dvaldivia assigned dvaldivia and adfost and unassigned dvaldivia May 27, 2021
}
]
}`
thirdPolicy := `{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to policy4 to keep things consistant

policyData := &iampolicy.Policy{}
json.Unmarshal([]byte(parsedPolicy.Policy), policyData)
if err2 == nil && !deny[k] {
switch policyAllowsAndMatchesBucket(parsedPolicy, bucket) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of passing parsedPolicy pass policy so that you don't need to unmarshal it again inside of this function

return listUsersWithAccessToBucket(ctx, adminClient, bucket)
}

func policyAllowsAndMatchesBucket(policy *models.Policy, bucket string) int {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change models.Policy to iampolicy.Policy you don't need to unmarshal twice per policy

if resources.Match(bucket, map[string][]string{}) {
if effect.IsValid() {
if effect.IsAllowed(true) {
return 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you return constants it would be more clear, for example:

AccessAllow = 1
AccessDeny = -1
AccessNone = 0

return nil, prepareError(err)
}
var retval []string
seen := make(map[string]bool)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think akHasAccess (access key has access) would be a more fitting name for this variable, as only the accessKeys with value true are actually allowed

if err2 == nil && !deny[k] {
switch policyAllowsAndMatchesBucket(parsedPolicy, bucket) {
case 1:
if !seen[k] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would flip a deny wouldn't it? if seen['accessKey'] is already false and seen in a previous policy

seen := make(map[string]bool)
for i := 0; i < len(users); i++ {
for _, policyName := range users[i].Policy {
deny := make(map[string]bool)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more fitting name for this variable would be akIsDenied (access key is denied)

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, _ := listUsersWithAccessToBucket(ctx, adminClient, tt.args.bucket); !stringArrayCompare(got, tt.want) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe assert.Equal will compare the two arrays, initialize assert via assert := asrt.New(t) and then simply assert.Equal(tt.want, got)

This assert will fail for the empty case as you return an uninitialized array thus []string{} != []string(nil) so you should change what you expect in your test to want: []string(nil),

minioGetPolicyMock = func(name string) (*iampolicy.Policy, error) {
var policy string
if name == "consoleAdmin" {
policy = policy1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where there is repetition, there can be abstraction, if you put all the policies inside a map, where the key is the policy name, there's no need for a large if/else block and allows for an easy way to add more policies

seen[info.Members[j]] = true
if err2 == nil && !deny[info.Members[j]] {
switch policyAllowsAndMatchesBucket(parsedPolicy, bucket) {
case 1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is where the constants mentioned above make the switch case even more readable

swtich res {
case AccessAllow:
case AccessDeny:
}

@dvaldivia dvaldivia merged commit 5782b9d into minio:master May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants