Debian Codegraph @

pkg/server/options/authentication_dynamic_request_header.go

raw
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 options
18
19 import (
20 "context"
21 "fmt"
22
23 "k8s.io/apimachinery/pkg/util/errors"
24 "k8s.io/apiserver/pkg/authentication/request/headerrequest"
25 "k8s.io/apiserver/pkg/server/dynamiccertificates"
26 "k8s.io/client-go/kubernetes"
27 )
28
29 var _ dynamiccertificates.ControllerRunner = &DynamicRequestHeaderController{}
30 var _ dynamiccertificates.CAContentProvider = &DynamicRequestHeaderController{}
31
32 var _ headerrequest.RequestHeaderAuthRequestProvider = &DynamicRequestHeaderController{}
33
34 // DynamicRequestHeaderController combines DynamicCAFromConfigMapController and RequestHeaderAuthRequestController
35 // into one controller for dynamically filling RequestHeaderConfig struct
36 type DynamicRequestHeaderController struct {
37 *dynamiccertificates.ConfigMapCAController
38 *headerrequest.RequestHeaderAuthRequestController
39 }
40
41 // newDynamicRequestHeaderController creates a new controller that implements DynamicRequestHeaderController
42 func newDynamicRequestHeaderController(client kubernetes.Interface) (*DynamicRequestHeaderController, error) {
43 requestHeaderCAController, err := dynamiccertificates.NewDynamicCAFromConfigMapController(
44 "client-ca",
45 authenticationConfigMapNamespace,
46 authenticationConfigMapName,
47 "requestheader-client-ca-file",
48 client)
49 if err != nil {
50 return nil, fmt.Errorf("unable to create DynamicCAFromConfigMap controller: %v", err)
51 }
52
53 requestHeaderAuthRequestController := headerrequest.NewRequestHeaderAuthRequestController(
54 authenticationConfigMapName,
55 authenticationConfigMapNamespace,
56 client,
57 "requestheader-username-headers",
58 "requestheader-uid-headers",
59 "requestheader-group-headers",
60 "requestheader-extra-headers-prefix",
61 "requestheader-allowed-names",
62 )
63 return &DynamicRequestHeaderController{
64 ConfigMapCAController: requestHeaderCAController,
65 RequestHeaderAuthRequestController: requestHeaderAuthRequestController,
66 }, nil
67 }
68
69 func (c *DynamicRequestHeaderController) RunOnce(ctx context.Context) error {
70 errs := []error{}
71 errs = append(errs, c.ConfigMapCAController.RunOnce(ctx))
72 errs = append(errs, c.RequestHeaderAuthRequestController.RunOnce(ctx))
73 return errors.NewAggregate(errs)
74 }
75
76 func (c *DynamicRequestHeaderController) Run(ctx context.Context, workers int) {
77 go c.ConfigMapCAController.Run(ctx, workers)
78 go c.RequestHeaderAuthRequestController.Run(ctx, workers)
79 <-ctx.Done()
80 }