| 1 |
/* |
| 2 |
Copyright 2020 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 headerrequest |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"sync/atomic" |
| 24 |
"time" |
| 25 |
|
| 26 |
corev1 "k8s.io/api/core/v1" |
| 27 |
"k8s.io/apimachinery/pkg/api/equality" |
| 28 |
"k8s.io/apimachinery/pkg/api/errors" |
| 29 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 |
"k8s.io/apimachinery/pkg/fields" |
| 31 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 32 |
"k8s.io/apimachinery/pkg/util/wait" |
| 33 |
coreinformers "k8s.io/client-go/informers/core/v1" |
| 34 |
"k8s.io/client-go/kubernetes" |
| 35 |
corev1listers "k8s.io/client-go/listers/core/v1" |
| 36 |
"k8s.io/client-go/tools/cache" |
| 37 |
"k8s.io/client-go/util/workqueue" |
| 38 |
"k8s.io/klog/v2" |
| 39 |
) |
| 40 |
|
| 41 |
const ( |
| 42 |
authenticationRoleName = "extension-apiserver-authentication-reader" |
| 43 |
) |
| 44 |
|
| 45 |
// RequestHeaderAuthRequestProvider a provider that knows how to dynamically fill parts of RequestHeaderConfig struct |
| 46 |
type RequestHeaderAuthRequestProvider interface { |
| 47 |
UsernameHeaders() []string |
| 48 |
UIDHeaders() []string |
| 49 |
GroupHeaders() []string |
| 50 |
ExtraHeaderPrefixes() []string |
| 51 |
AllowedClientNames() []string |
| 52 |
} |
| 53 |
|
| 54 |
var _ RequestHeaderAuthRequestProvider = &RequestHeaderAuthRequestController{} |
| 55 |
|
| 56 |
type requestHeaderBundle struct { |
| 57 |
UsernameHeaders []string |
| 58 |
UIDHeaders []string |
| 59 |
GroupHeaders []string |
| 60 |
ExtraHeaderPrefixes []string |
| 61 |
AllowedClientNames []string |
| 62 |
} |
| 63 |
|
| 64 |
// RequestHeaderAuthRequestController a controller that exposes a set of methods for dynamically filling parts of RequestHeaderConfig struct. |
| 65 |
// The methods are sourced from the config map which is being monitored by this controller. |
| 66 |
// The controller is primed from the server at the construction time for components that don't want to dynamically react to changes |
| 67 |
// in the config map. |
| 68 |
type RequestHeaderAuthRequestController struct { |
| 69 |
name string |
| 70 |
|
| 71 |
configmapName string |
| 72 |
configmapNamespace string |
| 73 |
|
| 74 |
client kubernetes.Interface |
| 75 |
configmapLister corev1listers.ConfigMapNamespaceLister |
| 76 |
configmapInformer cache.SharedIndexInformer |
| 77 |
configmapInformerSynced cache.InformerSynced |
| 78 |
|
| 79 |
queue workqueue.TypedRateLimitingInterface[string] |
| 80 |
|
| 81 |
// exportedRequestHeaderBundle is a requestHeaderBundle that contains the last read, non-zero length content of the configmap |
| 82 |
exportedRequestHeaderBundle atomic.Value |
| 83 |
|
| 84 |
usernameHeadersKey string |
| 85 |
uidHeadersKey string |
| 86 |
groupHeadersKey string |
| 87 |
extraHeaderPrefixesKey string |
| 88 |
allowedClientNamesKey string |
| 89 |
} |
| 90 |
|
| 91 |
// NewRequestHeaderAuthRequestController creates a new controller that implements RequestHeaderAuthRequestController |
| 92 |
func NewRequestHeaderAuthRequestController( |
| 93 |
cmName string, |
| 94 |
cmNamespace string, |
| 95 |
client kubernetes.Interface, |
| 96 |
usernameHeadersKey, uidHeadersKey, groupHeadersKey, extraHeaderPrefixesKey, allowedClientNamesKey string) *RequestHeaderAuthRequestController { |
| 97 |
c := &RequestHeaderAuthRequestController{ |
| 98 |
name: "RequestHeaderAuthRequestController", |
| 99 |
|
| 100 |
client: client, |
| 101 |
|
| 102 |
configmapName: cmName, |
| 103 |
configmapNamespace: cmNamespace, |
| 104 |
|
| 105 |
usernameHeadersKey: usernameHeadersKey, |
| 106 |
uidHeadersKey: uidHeadersKey, |
| 107 |
groupHeadersKey: groupHeadersKey, |
| 108 |
extraHeaderPrefixesKey: extraHeaderPrefixesKey, |
| 109 |
allowedClientNamesKey: allowedClientNamesKey, |
| 110 |
|
| 111 |
queue: workqueue.NewTypedRateLimitingQueueWithConfig( |
| 112 |
workqueue.DefaultTypedControllerRateLimiter[string](), |
| 113 |
workqueue.TypedRateLimitingQueueConfig[string]{Name: "RequestHeaderAuthRequestController"}, |
| 114 |
), |
| 115 |
} |
| 116 |
|
| 117 |
// we construct our own informer because we need such a small subset of the information available. Just one namespace. |
| 118 |
c.configmapInformer = coreinformers.NewFilteredConfigMapInformer(client, c.configmapNamespace, 12*time.Hour, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, func(listOptions *metav1.ListOptions) { |
| 119 |
listOptions.FieldSelector = fields.OneTermEqualSelector("metadata.name", c.configmapName).String() |
| 120 |
}) |
| 121 |
|
| 122 |
c.configmapInformer.AddEventHandler(cache.FilteringResourceEventHandler{ |
| 123 |
FilterFunc: func(obj interface{}) bool { |
| 124 |
if cast, ok := obj.(*corev1.ConfigMap); ok { |
| 125 |
return cast.Name == c.configmapName && cast.Namespace == c.configmapNamespace |
| 126 |
} |
| 127 |
if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok { |
| 128 |
if cast, ok := tombstone.Obj.(*corev1.ConfigMap); ok { |
| 129 |
return cast.Name == c.configmapName && cast.Namespace == c.configmapNamespace |
| 130 |
} |
| 131 |
} |
| 132 |
return true // always return true just in case. The checks are fairly cheap |
| 133 |
}, |
| 134 |
Handler: cache.ResourceEventHandlerFuncs{ |
| 135 |
// we have a filter, so any time we're called, we may as well queue. We only ever check one configmap |
| 136 |
// so we don't have to be choosy about our key. |
| 137 |
AddFunc: func(obj interface{}) { |
| 138 |
c.queue.Add(c.keyFn()) |
| 139 |
}, |
| 140 |
UpdateFunc: func(oldObj, newObj interface{}) { |
| 141 |
c.queue.Add(c.keyFn()) |
| 142 |
}, |
| 143 |
DeleteFunc: func(obj interface{}) { |
| 144 |
c.queue.Add(c.keyFn()) |
| 145 |
}, |
| 146 |
}, |
| 147 |
}) |
| 148 |
|
| 149 |
c.configmapLister = corev1listers.NewConfigMapLister(c.configmapInformer.GetIndexer()).ConfigMaps(c.configmapNamespace) |
| 150 |
c.configmapInformerSynced = c.configmapInformer.HasSynced |
| 151 |
|
| 152 |
return c |
| 153 |
} |
| 154 |
|
| 155 |
func (c *RequestHeaderAuthRequestController) UsernameHeaders() []string { |
| 156 |
return c.loadRequestHeaderFor(c.usernameHeadersKey) |
| 157 |
} |
| 158 |
|
| 159 |
func (c *RequestHeaderAuthRequestController) UIDHeaders() []string { |
| 160 |
return c.loadRequestHeaderFor(c.uidHeadersKey) |
| 161 |
} |
| 162 |
|
| 163 |
func (c *RequestHeaderAuthRequestController) GroupHeaders() []string { |
| 164 |
return c.loadRequestHeaderFor(c.groupHeadersKey) |
| 165 |
} |
| 166 |
|
| 167 |
func (c *RequestHeaderAuthRequestController) ExtraHeaderPrefixes() []string { |
| 168 |
return c.loadRequestHeaderFor(c.extraHeaderPrefixesKey) |
| 169 |
} |
| 170 |
|
| 171 |
func (c *RequestHeaderAuthRequestController) AllowedClientNames() []string { |
| 172 |
return c.loadRequestHeaderFor(c.allowedClientNamesKey) |
| 173 |
} |
| 174 |
|
| 175 |
// Run starts RequestHeaderAuthRequestController controller and blocks until stopCh is closed. |
| 176 |
func (c *RequestHeaderAuthRequestController) Run(ctx context.Context, workers int) { |
| 177 |
defer utilruntime.HandleCrash() |
| 178 |
defer c.queue.ShutDown() |
| 179 |
|
| 180 |
klog.Infof("Starting %s", c.name) |
| 181 |
defer klog.Infof("Shutting down %s", c.name) |
| 182 |
|
| 183 |
go c.configmapInformer.Run(ctx.Done()) |
| 184 |
|
| 185 |
// wait for caches to fill before starting your work |
| 186 |
if !cache.WaitForNamedCacheSync(c.name, ctx.Done(), c.configmapInformerSynced) { |
| 187 |
return |
| 188 |
} |
| 189 |
|
| 190 |
// doesn't matter what workers say, only start one. |
| 191 |
go wait.Until(c.runWorker, time.Second, ctx.Done()) |
| 192 |
|
| 193 |
<-ctx.Done() |
| 194 |
} |
| 195 |
|
| 196 |
// // RunOnce runs a single sync loop |
| 197 |
func (c *RequestHeaderAuthRequestController) RunOnce(ctx context.Context) error { |
| 198 |
configMap, err := c.client.CoreV1().ConfigMaps(c.configmapNamespace).Get(ctx, c.configmapName, metav1.GetOptions{}) |
| 199 |
switch { |
| 200 |
case errors.IsNotFound(err): |
| 201 |
// ignore, authConfigMap is nil now |
| 202 |
return nil |
| 203 |
case errors.IsForbidden(err): |
| 204 |
klog.Warningf("Unable to get configmap/%s in %s. Usually fixed by "+ |
| 205 |
"'kubectl create rolebinding -n %s ROLEBINDING_NAME --role=%s --serviceaccount=YOUR_NS:YOUR_SA'", |
| 206 |
c.configmapName, c.configmapNamespace, c.configmapNamespace, authenticationRoleName) |
| 207 |
return err |
| 208 |
case err != nil: |
| 209 |
return err |
| 210 |
} |
| 211 |
return c.syncConfigMap(configMap) |
| 212 |
} |
| 213 |
|
| 214 |
func (c *RequestHeaderAuthRequestController) runWorker() { |
| 215 |
for c.processNextWorkItem() { |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func (c *RequestHeaderAuthRequestController) processNextWorkItem() bool { |
| 220 |
dsKey, quit := c.queue.Get() |
| 221 |
if quit { |
| 222 |
return false |
| 223 |
} |
| 224 |
defer c.queue.Done(dsKey) |
| 225 |
|
| 226 |
err := c.sync() |
| 227 |
if err == nil { |
| 228 |
c.queue.Forget(dsKey) |
| 229 |
return true |
| 230 |
} |
| 231 |
|
| 232 |
utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err)) |
| 233 |
c.queue.AddRateLimited(dsKey) |
| 234 |
|
| 235 |
return true |
| 236 |
} |
| 237 |
|
| 238 |
// sync reads the config and propagates the changes to exportedRequestHeaderBundle |
| 239 |
// which is exposed by the set of methods that are used to fill RequestHeaderConfig struct |
| 240 |
func (c *RequestHeaderAuthRequestController) sync() error { |
| 241 |
configMap, err := c.configmapLister.Get(c.configmapName) |
| 242 |
if err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
return c.syncConfigMap(configMap) |
| 246 |
} |
| 247 |
|
| 248 |
func (c *RequestHeaderAuthRequestController) syncConfigMap(configMap *corev1.ConfigMap) error { |
| 249 |
hasChanged, newRequestHeaderBundle, err := c.hasRequestHeaderBundleChanged(configMap) |
| 250 |
if err != nil { |
| 251 |
return err |
| 252 |
} |
| 253 |
if hasChanged { |
| 254 |
c.exportedRequestHeaderBundle.Store(newRequestHeaderBundle) |
| 255 |
klog.V(2).Infof("Loaded a new request header values for %v", c.name) |
| 256 |
} |
| 257 |
return nil |
| 258 |
} |
| 259 |
|
| 260 |
func (c *RequestHeaderAuthRequestController) hasRequestHeaderBundleChanged(cm *corev1.ConfigMap) (bool, *requestHeaderBundle, error) { |
| 261 |
currentHeadersBundle, err := c.getRequestHeaderBundleFromConfigMap(cm) |
| 262 |
if err != nil { |
| 263 |
return false, nil, err |
| 264 |
} |
| 265 |
|
| 266 |
rawHeaderBundle := c.exportedRequestHeaderBundle.Load() |
| 267 |
if rawHeaderBundle == nil { |
| 268 |
return true, currentHeadersBundle, nil |
| 269 |
} |
| 270 |
|
| 271 |
// check to see if we have a change. If the values are the same, do nothing. |
| 272 |
loadedHeadersBundle, ok := rawHeaderBundle.(*requestHeaderBundle) |
| 273 |
if !ok { |
| 274 |
return true, currentHeadersBundle, nil |
| 275 |
} |
| 276 |
|
| 277 |
if !equality.Semantic.DeepEqual(loadedHeadersBundle, currentHeadersBundle) { |
| 278 |
return true, currentHeadersBundle, nil |
| 279 |
} |
| 280 |
return false, nil, nil |
| 281 |
} |
| 282 |
|
| 283 |
func (c *RequestHeaderAuthRequestController) getRequestHeaderBundleFromConfigMap(cm *corev1.ConfigMap) (*requestHeaderBundle, error) { |
| 284 |
usernameHeaderCurrentValue, err := deserializeStrings(cm.Data[c.usernameHeadersKey]) |
| 285 |
if err != nil { |
| 286 |
return nil, err |
| 287 |
} |
| 288 |
|
| 289 |
uidHeaderCurrentValue, err := deserializeStrings(cm.Data[c.uidHeadersKey]) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
|
| 294 |
groupHeadersCurrentValue, err := deserializeStrings(cm.Data[c.groupHeadersKey]) |
| 295 |
if err != nil { |
| 296 |
return nil, err |
| 297 |
} |
| 298 |
|
| 299 |
extraHeaderPrefixesCurrentValue, err := deserializeStrings(cm.Data[c.extraHeaderPrefixesKey]) |
| 300 |
if err != nil { |
| 301 |
return nil, err |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
allowedClientNamesCurrentValue, err := deserializeStrings(cm.Data[c.allowedClientNamesKey]) |
| 306 |
if err != nil { |
| 307 |
return nil, err |
| 308 |
} |
| 309 |
|
| 310 |
return &requestHeaderBundle{ |
| 311 |
UsernameHeaders: usernameHeaderCurrentValue, |
| 312 |
UIDHeaders: uidHeaderCurrentValue, |
| 313 |
GroupHeaders: groupHeadersCurrentValue, |
| 314 |
ExtraHeaderPrefixes: extraHeaderPrefixesCurrentValue, |
| 315 |
AllowedClientNames: allowedClientNamesCurrentValue, |
| 316 |
}, nil |
| 317 |
} |
| 318 |
|
| 319 |
func (c *RequestHeaderAuthRequestController) loadRequestHeaderFor(key string) []string { |
| 320 |
rawHeaderBundle := c.exportedRequestHeaderBundle.Load() |
| 321 |
if rawHeaderBundle == nil { |
| 322 |
return nil // this can happen if we've been unable load data from the apiserver for some reason |
| 323 |
} |
| 324 |
headerBundle := rawHeaderBundle.(*requestHeaderBundle) |
| 325 |
|
| 326 |
switch key { |
| 327 |
case c.usernameHeadersKey: |
| 328 |
return headerBundle.UsernameHeaders |
| 329 |
case c.uidHeadersKey: |
| 330 |
return headerBundle.UIDHeaders |
| 331 |
case c.groupHeadersKey: |
| 332 |
return headerBundle.GroupHeaders |
| 333 |
case c.extraHeaderPrefixesKey: |
| 334 |
return headerBundle.ExtraHeaderPrefixes |
| 335 |
case c.allowedClientNamesKey: |
| 336 |
return headerBundle.AllowedClientNames |
| 337 |
default: |
| 338 |
return nil |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func (c *RequestHeaderAuthRequestController) keyFn() string { |
| 343 |
// this format matches DeletionHandlingMetaNamespaceKeyFunc for our single key |
| 344 |
return c.configmapNamespace + "/" + c.configmapName |
| 345 |
} |
| 346 |
|
| 347 |
func deserializeStrings(in string) ([]string, error) { |
| 348 |
if len(in) == 0 { |
| 349 |
return nil, nil |
| 350 |
} |
| 351 |
var ret []string |
| 352 |
if err := json.Unmarshal([]byte(in), &ret); err != nil { |
| 353 |
return nil, err |
| 354 |
} |
| 355 |
return ret, nil |
| 356 |
} |