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

Namespace schema does not correctly show permitted collectionMethods #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion pkg/schema/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ func newSchemas() (*types.APISchemas, error) {
return apiSchemas, nil
}

// Returns the new duplicate free slice.
func uniq(s []string) []string {
length := len(s)
found := make(map[string]struct{})
result := make([]string, 0, length)

for i := 0; i < length; i++ {
v := s[i]
if _, ok := found[v]; !ok {
found[v] = struct{}{}
result = append(result, v)
}
}

return result
}

func (c *Collection) Schemas(user user.Info) (*types.APISchemas, error) {
access := c.as.AccessFor(user)
c.removeOldRecords(access, user)
Expand Down Expand Up @@ -128,7 +145,8 @@ func (c *Collection) schemasForSubject(access *accesscontrol.AccessSet) (*types.
verbAccess["get"] = accessList
verbAccess["watch"] = accessList
if len(accessList) == 0 {
// always allow list
// always allow list. note, this can lead to duplicates
// which will be removed later.
s.CollectionMethods = append(s.CollectionMethods, http.MethodGet)
}
}
Expand Down Expand Up @@ -162,6 +180,8 @@ func (c *Collection) schemasForSubject(access *accesscontrol.AccessSet) (*types.
continue
}

s.CollectionMethods = uniq(s.CollectionMethods)
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this call can be moved to https://github.com/votdev/steve/blob/7a749b127d4fa57af225a7c209ee0d0bb7beed73/pkg/schema/factory.go#L166, but i'm not sure how it is possible to get 3 GET entries (see here) in the slice in that case (i assume only 2).


if err := result.AddSchema(*s); err != nil {
return nil, err
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/schema/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestSchemas(t *testing.T) {
})
}
}

func TestSchemaCache(t *testing.T) {
// Schemas are a frequently used resource. It's important that the cache doesn't have a leak given size/frequency of resource
tests := []struct {
Expand Down Expand Up @@ -120,6 +121,43 @@ func TestSchemaCache(t *testing.T) {
}
}

func TestUniq(t *testing.T) {
tests := []struct {
name string
param []string
desiredResult []string
}{
{
name: "remove duplicates [1/4]",
param: []string{"GET", "POST", "PUT"},
desiredResult: []string{"GET", "POST", "PUT"},
},
{
name: "remove duplicates [2/4]",
param: []string{"GET", "GET", "POST"},
desiredResult: []string{"GET", "POST"},
},
{
name: "remove duplicates [3/4]",
param: []string{"DELETE"},
desiredResult: []string{"DELETE"},
},
{
name: "remove duplicates [4/4]",
param: []string{},
desiredResult: []string{},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result := uniq(test.param)
assert.Equal(t, result, test.desiredResult)
})
}
}

func runSchemaTest(t *testing.T, config schemaTestConfig, lookup *mockAccessSetLookup, collection *Collection, testUser user.Info) {
for _, verb := range config.permissionVerbs {
lookup.AddAccessForUser(testUser, verb, k8sSchema.GroupResource{Group: testGroup, Resource: "testCRD"}, "*", "*")
Expand Down