Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider supporting groups instead of single users only and create their tests #351

Open
wants to merge 3 commits into
base: sig-auth-acceptance
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/authorization/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,33 @@ func (saConfig StaticAuthorizationConfig) Matches(a authorizer.Attributes) bool
isAllowed := func(staticConf string, requestVal string) bool {
if staticConf == "" {
return true
} else {
return staticConf == requestVal
}
return staticConf == requestVal
}
isGroupAllowed := func(configGroups, requestGroups []string) bool {
if len(configGroups) == 0 {
return true
}
// O(n^2) is fine here as the groups are small. Optimize if n grows large.
ShazaAldawamneh marked this conversation as resolved.
Show resolved Hide resolved
for _, configGroup := range configGroups {
for _, requestGroup := range requestGroups {
ShazaAldawamneh marked this conversation as resolved.
Show resolved Hide resolved
if configGroup == requestGroup {
return true
}
}
}
return false
}

userName := ""
userGroups := []string{}
if a.GetUser() != nil {
userName = a.GetUser().GetName()
userGroups = a.GetUser().GetGroups()
}

if isAllowed(saConfig.User.Name, userName) &&
isGroupAllowed(saConfig.User.Groups, userGroups) &&
stlaz marked this conversation as resolved.
Show resolved Hide resolved
isAllowed(saConfig.Verb, a.GetVerb()) &&
isAllowed(saConfig.Namespace, a.GetNamespace()) &&
isAllowed(saConfig.APIGroup, a.GetAPIGroup()) &&
Expand Down
49 changes: 49 additions & 0 deletions pkg/authorization/static/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,55 @@ func TestStaticAuthorizer(t *testing.T) {
authorizer.AttributesRecord{Verb: "get", Resource: "services", ResourceRequest: true},
},
},
{
name: "groupMatch",
config: []StaticAuthorizationConfig{
{User: UserConfig{Groups: []string{"admin", "editors"}}, Verb: "get", Resource: "namespaces", ResourceRequest: true},
ShazaAldawamneh marked this conversation as resolved.
Show resolved Hide resolved
},
shouldPass: []authorizer.Attributes{
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user1", Groups: []string{"admin"}}, Verb: "get", Resource: "namespaces", ResourceRequest: true},
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user2", Groups: []string{"editors"}}, Verb: "get", Resource: "namespaces", ResourceRequest: true},
},
shouldNoOpinion: []authorizer.Attributes{
// User with non-matching group
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user3", Groups: []string{"viewers"}}, Verb: "get", Resource: "namespaces", ResourceRequest: true},
// User with no groups
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user4"}, Verb: "get", Resource: "namespaces", ResourceRequest: true},
},
},
{
name: "groupAndUserMatch",
config: []StaticAuthorizationConfig{
{User: UserConfig{Name: "system:foo", Groups: []string{"admin"}}, Verb: "get", Resource: "pods", ResourceRequest: true},
},
shouldPass: []authorizer.Attributes{
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "system:foo", Groups: []string{"admin", "dev"}}, Verb: "get", Resource: "pods", ResourceRequest: true},
},
shouldNoOpinion: []authorizer.Attributes{
// User name matches, but group does not
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "system:foo", Groups: []string{"viewers"}}, Verb: "get", Resource: "pods", ResourceRequest: true},
// Group matches, but user name does not
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "system:bar", Groups: []string{"admin"}}, Verb: "get", Resource: "pods", ResourceRequest: true},
// Neither user name nor group matches
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "system:baz", Groups: []string{"viewers"}}, Verb: "get", Resource: "pods", ResourceRequest: true},
},
},
{
name: "groupWildcard",
config: []StaticAuthorizationConfig{
{User: UserConfig{Groups: []string{}}, Verb: "get", Resource: "services", ResourceRequest: true},
},
shouldPass: []authorizer.Attributes{
// Any group should be allowed as no groups are specified
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user1", Groups: []string{"viewers"}}, Verb: "get", Resource: "services", ResourceRequest: true},
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user2", Groups: []string{"admins", "viewers"}}, Verb: "get", Resource: "services", ResourceRequest: true},
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user3"}, Verb: "get", Resource: "services", ResourceRequest: true},
},
shouldNoOpinion: []authorizer.Attributes{
// Wrong verb
authorizer.AttributesRecord{User: &user.DefaultInfo{Name: "user1", Groups: []string{"viewers"}}, Verb: "update", Resource: "services", ResourceRequest: true},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading