k8s.io/apiserver@v0.31.1/pkg/authorization/authorizer/interfaces.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package authorizer 18 19 import ( 20 "context" 21 "net/http" 22 23 "k8s.io/apimachinery/pkg/fields" 24 "k8s.io/apimachinery/pkg/labels" 25 "k8s.io/apiserver/pkg/authentication/user" 26 ) 27 28 // Attributes is an interface used by an Authorizer to get information about a request 29 // that is used to make an authorization decision. 30 type Attributes interface { 31 // GetUser returns the user.Info object to authorize 32 GetUser() user.Info 33 34 // GetVerb returns the kube verb associated with API requests (this includes get, list, watch, create, update, patch, delete, deletecollection, and proxy), 35 // or the lowercased HTTP verb associated with non-API requests (this includes get, put, post, patch, and delete) 36 GetVerb() string 37 38 // When IsReadOnly() == true, the request has no side effects, other than 39 // caching, logging, and other incidentals. 40 IsReadOnly() bool 41 42 // The namespace of the object, if a request is for a REST object. 43 GetNamespace() string 44 45 // The kind of object, if a request is for a REST object. 46 GetResource() string 47 48 // GetSubresource returns the subresource being requested, if present 49 GetSubresource() string 50 51 // GetName returns the name of the object as parsed off the request. This will not be present for all request types, but 52 // will be present for: get, update, delete 53 GetName() string 54 55 // The group of the resource, if a request is for a REST object. 56 GetAPIGroup() string 57 58 // GetAPIVersion returns the version of the group requested, if a request is for a REST object. 59 GetAPIVersion() string 60 61 // IsResourceRequest returns true for requests to API resources, like /api/v1/nodes, 62 // and false for non-resource endpoints like /api, /healthz 63 IsResourceRequest() bool 64 65 // GetPath returns the path of the request 66 GetPath() string 67 68 // ParseFieldSelector is lazy, thread-safe, and stores the parsed result and error. 69 // It returns an error if the field selector cannot be parsed. 70 // The returned requirements must be treated as readonly and not modified. 71 GetFieldSelector() (fields.Requirements, error) 72 73 // ParseLabelSelector is lazy, thread-safe, and stores the parsed result and error. 74 // It returns an error if the label selector cannot be parsed. 75 // The returned requirements must be treated as readonly and not modified. 76 GetLabelSelector() (labels.Requirements, error) 77 } 78 79 // Authorizer makes an authorization decision based on information gained by making 80 // zero or more calls to methods of the Attributes interface. It returns nil when an action is 81 // authorized, otherwise it returns an error. 82 type Authorizer interface { 83 Authorize(ctx context.Context, a Attributes) (authorized Decision, reason string, err error) 84 } 85 86 type AuthorizerFunc func(ctx context.Context, a Attributes) (Decision, string, error) 87 88 func (f AuthorizerFunc) Authorize(ctx context.Context, a Attributes) (Decision, string, error) { 89 return f(ctx, a) 90 } 91 92 // RuleResolver provides a mechanism for resolving the list of rules that apply to a given user within a namespace. 93 type RuleResolver interface { 94 // RulesFor get the list of cluster wide rules, the list of rules in the specific namespace, incomplete status and errors. 95 RulesFor(user user.Info, namespace string) ([]ResourceRuleInfo, []NonResourceRuleInfo, bool, error) 96 } 97 98 // RequestAttributesGetter provides a function that extracts Attributes from an http.Request 99 type RequestAttributesGetter interface { 100 GetRequestAttributes(user.Info, *http.Request) Attributes 101 } 102 103 // AttributesRecord implements Attributes interface. 104 type AttributesRecord struct { 105 User user.Info 106 Verb string 107 Namespace string 108 APIGroup string 109 APIVersion string 110 Resource string 111 Subresource string 112 Name string 113 ResourceRequest bool 114 Path string 115 116 FieldSelectorRequirements fields.Requirements 117 FieldSelectorParsingErr error 118 LabelSelectorRequirements labels.Requirements 119 LabelSelectorParsingErr error 120 } 121 122 func (a AttributesRecord) GetUser() user.Info { 123 return a.User 124 } 125 126 func (a AttributesRecord) GetVerb() string { 127 return a.Verb 128 } 129 130 func (a AttributesRecord) IsReadOnly() bool { 131 return a.Verb == "get" || a.Verb == "list" || a.Verb == "watch" 132 } 133 134 func (a AttributesRecord) GetNamespace() string { 135 return a.Namespace 136 } 137 138 func (a AttributesRecord) GetResource() string { 139 return a.Resource 140 } 141 142 func (a AttributesRecord) GetSubresource() string { 143 return a.Subresource 144 } 145 146 func (a AttributesRecord) GetName() string { 147 return a.Name 148 } 149 150 func (a AttributesRecord) GetAPIGroup() string { 151 return a.APIGroup 152 } 153 154 func (a AttributesRecord) GetAPIVersion() string { 155 return a.APIVersion 156 } 157 158 func (a AttributesRecord) IsResourceRequest() bool { 159 return a.ResourceRequest 160 } 161 162 func (a AttributesRecord) GetPath() string { 163 return a.Path 164 } 165 166 func (a AttributesRecord) GetFieldSelector() (fields.Requirements, error) { 167 return a.FieldSelectorRequirements, a.FieldSelectorParsingErr 168 } 169 170 func (a AttributesRecord) GetLabelSelector() (labels.Requirements, error) { 171 return a.LabelSelectorRequirements, a.LabelSelectorParsingErr 172 } 173 174 type Decision int 175 176 const ( 177 // DecisionDeny means that an authorizer decided to deny the action. 178 DecisionDeny Decision = iota 179 // DecisionAllow means that an authorizer decided to allow the action. 180 DecisionAllow 181 // DecisionNoOpinion means that an authorizer has no opinion on whether 182 // to allow or deny an action. 183 DecisionNoOpinion 184 )