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

Better support for subtyping #82

Merged
merged 2 commits into from
Feb 1, 2018
Merged
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
16 changes: 11 additions & 5 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,32 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo
if apiRequest.Link == "" {
switch apiRequest.Method {
case http.MethodGet:
if !apiRequest.AccessControl.CanList(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not list "+apiRequest.Schema.Type)
if apiRequest.ID == "" {
if !apiRequest.AccessControl.CanList(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not list "+apiRequest.Schema.ID)
}
} else {
if !apiRequest.AccessControl.CanGet(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not get "+apiRequest.Schema.ID)
}
}
handler = apiRequest.Schema.ListHandler
nextHandler = s.Defaults.ListHandler
case http.MethodPost:
if !apiRequest.AccessControl.CanCreate(apiRequest, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not create "+apiRequest.Schema.Type)
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not create "+apiRequest.Schema.ID)
Copy link
Author

Choose a reason for hiding this comment

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

Schema.Type is "/something/something/schema". ID is what we want

}
handler = apiRequest.Schema.CreateHandler
nextHandler = s.Defaults.CreateHandler
case http.MethodPut:
if !apiRequest.AccessControl.CanUpdate(apiRequest, nil, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not update "+apiRequest.Schema.Type)
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not update "+apiRequest.Schema.ID)
}
handler = apiRequest.Schema.UpdateHandler
nextHandler = s.Defaults.UpdateHandler
case http.MethodDelete:
if !apiRequest.AccessControl.CanDelete(apiRequest, nil, apiRequest.Schema) {
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not delete "+apiRequest.Schema.Type)
return apiRequest, httperror.NewAPIError(httperror.PermissionDenied, "Can not delete "+apiRequest.Schema.ID)
}
handler = apiRequest.Schema.DeleteHandler
nextHandler = s.Defaults.DeleteHandler
Expand Down
4 changes: 4 additions & 0 deletions authorization/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (*AllAccess) CanCreate(apiContext *types.APIContext, schema *types.Schema)
return slice.ContainsString(schema.CollectionMethods, http.MethodPost)
}

func (*AllAccess) CanGet(apiContext *types.APIContext, schema *types.Schema) bool {
return slice.ContainsString(schema.ResourceMethods, http.MethodGet)
}

func (*AllAccess) CanList(apiContext *types.APIContext, schema *types.Schema) bool {
return slice.ContainsString(schema.CollectionMethods, http.MethodGet)
}
Expand Down
7 changes: 6 additions & 1 deletion store/crd/crd_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crd

import (
"context"
"strings"

"github.com/rancher/norman/store/proxy"
"github.com/rancher/norman/types"
Expand Down Expand Up @@ -49,6 +50,10 @@ func NewCRDStoreFromClients(apiExtClientSet apiextclientset.Interface, k8sClient
}

func key(schema *types.Schema) string {
if !strings.EqualFold(schema.BaseType, schema.ID) {
return schema.Version.Path + "/" + schema.BaseType
}

return schema.Version.Path + "/" + schema.ID
}

Expand Down Expand Up @@ -105,7 +110,7 @@ func (c *Store) AddSchemas(ctx context.Context, schemas ...*types.Schema) error
var allSchemas []*types.Schema

for _, schema := range schemas {
if schema.Store != nil || !schema.CanList(nil) {
if schema.Store != nil || !schema.CanList(nil) || !strings.EqualFold(schema.BaseType, schema.ID) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion store/schema/schema_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *ty
continue
}

if schema.CanList(apiContext) {
if schema.CanList(apiContext) || schema.CanGet(apiContext) {
schemas = s.addSchema(apiContext, schema, schemaMap, schemas, included)
}
}
Expand Down
7 changes: 7 additions & 0 deletions types/schema_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func (s *Schema) CanList(context *APIContext) bool {
return context.AccessControl.CanList(context, s)
}

func (s *Schema) CanGet(context *APIContext) bool {
if context == nil {
return slice.ContainsString(s.ResourceMethods, http.MethodGet)
}
return context.AccessControl.CanGet(context, s)
}

func (s *Schema) CanCreate(context *APIContext) bool {
if context == nil {
return slice.ContainsString(s.CollectionMethods, http.MethodPost)
Expand Down
1 change: 1 addition & 0 deletions types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type ResponseWriter interface {
type AccessControl interface {
CanCreate(apiContext *APIContext, schema *Schema) bool
CanList(apiContext *APIContext, schema *Schema) bool
CanGet(apiContext *APIContext, schema *Schema) bool
CanUpdate(apiContext *APIContext, obj map[string]interface{}, schema *Schema) bool
CanDelete(apiContext *APIContext, obj map[string]interface{}, schema *Schema) bool

Expand Down