1
16
17 package app
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "reflect"
24
25 "k8s.io/apimachinery/pkg/types"
26 "k8s.io/apiserver/pkg/authentication/authenticator"
27 "k8s.io/apiserver/pkg/authentication/authenticatorfactory"
28 "k8s.io/apiserver/pkg/authorization/authorizer"
29 "k8s.io/apiserver/pkg/authorization/authorizerfactory"
30 "k8s.io/apiserver/pkg/server/dynamiccertificates"
31 genericoptions "k8s.io/apiserver/pkg/server/options"
32 clientset "k8s.io/client-go/kubernetes"
33 authenticationclient "k8s.io/client-go/kubernetes/typed/authentication/v1"
34 authorizationclient "k8s.io/client-go/kubernetes/typed/authorization/v1"
35
36 kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
37 "k8s.io/kubernetes/pkg/kubelet/server"
38 )
39
40
41
42 func BuildAuth(nodeName types.NodeName, client clientset.Interface, config kubeletconfig.KubeletConfiguration) (server.AuthInterface, func(<-chan struct{}), error) {
43
44 var (
45 tokenClient authenticationclient.AuthenticationV1Interface
46 sarClient authorizationclient.AuthorizationV1Interface
47 )
48 if client != nil && !reflect.ValueOf(client).IsNil() {
49 tokenClient = client.AuthenticationV1()
50 sarClient = client.AuthorizationV1()
51 }
52
53 authenticator, runAuthenticatorCAReload, err := BuildAuthn(tokenClient, config.Authentication)
54 if err != nil {
55 return nil, nil, err
56 }
57
58 attributes := server.NewNodeAuthorizerAttributesGetter(nodeName)
59
60 authorizer, err := BuildAuthz(sarClient, config.Authorization)
61 if err != nil {
62 return nil, nil, err
63 }
64
65 return server.NewKubeletAuth(authenticator, attributes, authorizer), runAuthenticatorCAReload, nil
66 }
67
68
69 func BuildAuthn(client authenticationclient.AuthenticationV1Interface, authn kubeletconfig.KubeletAuthentication) (authenticator.Request, func(<-chan struct{}), error) {
70 var dynamicCAContentFromFile *dynamiccertificates.DynamicFileCAContent
71 var err error
72 if len(authn.X509.ClientCAFile) > 0 {
73 dynamicCAContentFromFile, err = dynamiccertificates.NewDynamicCAContentFromFile("client-ca-bundle", authn.X509.ClientCAFile)
74 if err != nil {
75 return nil, nil, err
76 }
77 }
78
79 authenticatorConfig := authenticatorfactory.DelegatingAuthenticatorConfig{
80 Anonymous: authn.Anonymous.Enabled,
81 CacheTTL: authn.Webhook.CacheTTL.Duration,
82 ClientCertificateCAContentProvider: dynamicCAContentFromFile,
83 }
84
85 if authn.Webhook.Enabled {
86 if client == nil {
87 return nil, nil, errors.New("no client provided, cannot use webhook authentication")
88 }
89 authenticatorConfig.WebhookRetryBackoff = genericoptions.DefaultAuthWebhookRetryBackoff()
90 authenticatorConfig.TokenAccessReviewClient = client
91 }
92
93 authenticator, _, err := authenticatorConfig.New()
94 if err != nil {
95 return nil, nil, err
96 }
97
98 return authenticator, func(stopCh <-chan struct{}) {
99
100
101 ctx, cancel := context.WithCancel(context.Background())
102 go func() {
103 select {
104 case <-stopCh:
105 cancel()
106 case <-ctx.Done():
107 }
108 }()
109 if dynamicCAContentFromFile != nil {
110 go dynamicCAContentFromFile.Run(ctx, 1)
111 }
112 }, err
113 }
114
115
116 func BuildAuthz(client authorizationclient.AuthorizationV1Interface, authz kubeletconfig.KubeletAuthorization) (authorizer.Authorizer, error) {
117 switch authz.Mode {
118 case kubeletconfig.KubeletAuthorizationModeAlwaysAllow:
119 return authorizerfactory.NewAlwaysAllowAuthorizer(), nil
120
121 case kubeletconfig.KubeletAuthorizationModeWebhook:
122 if client == nil {
123 return nil, errors.New("no client provided, cannot use webhook authorization")
124 }
125 authorizerConfig := authorizerfactory.DelegatingAuthorizerConfig{
126 SubjectAccessReviewClient: client,
127 AllowCacheTTL: authz.Webhook.CacheAuthorizedTTL.Duration,
128 DenyCacheTTL: authz.Webhook.CacheUnauthorizedTTL.Duration,
129 WebhookRetryBackoff: genericoptions.DefaultAuthWebhookRetryBackoff(),
130 }
131 return authorizerConfig.New()
132
133 case "":
134 return nil, fmt.Errorf("no authorization mode specified")
135
136 default:
137 return nil, fmt.Errorf("unknown authorization mode %s", authz.Mode)
138
139 }
140 }
141
View as plain text