| 1 |
/* |
| 2 |
Copyright 2016 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 |
"strings" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/spf13/pflag" |
| 26 |
|
| 27 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 |
"k8s.io/apimachinery/pkg/util/wait" |
| 29 |
"k8s.io/apiserver/pkg/apis/apiserver" |
| 30 |
"k8s.io/apiserver/pkg/authentication/authenticatorfactory" |
| 31 |
"k8s.io/apiserver/pkg/authentication/request/headerrequest" |
| 32 |
"k8s.io/apiserver/pkg/features" |
| 33 |
"k8s.io/apiserver/pkg/server" |
| 34 |
"k8s.io/apiserver/pkg/server/dynamiccertificates" |
| 35 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 36 |
"k8s.io/client-go/kubernetes" |
| 37 |
"k8s.io/client-go/rest" |
| 38 |
"k8s.io/client-go/tools/clientcmd" |
| 39 |
"k8s.io/client-go/transport" |
| 40 |
"k8s.io/klog/v2" |
| 41 |
openapicommon "k8s.io/kube-openapi/pkg/common" |
| 42 |
) |
| 43 |
|
| 44 |
// DefaultAuthWebhookRetryBackoff is the default backoff parameters for |
| 45 |
// both authentication and authorization webhook used by the apiserver. |
| 46 |
func DefaultAuthWebhookRetryBackoff() *wait.Backoff { |
| 47 |
return &wait.Backoff{ |
| 48 |
Duration: 500 * time.Millisecond, |
| 49 |
Factor: 1.5, |
| 50 |
Jitter: 0.2, |
| 51 |
Steps: 5, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
type RequestHeaderAuthenticationOptions struct { |
| 56 |
// ClientCAFile is the root certificate bundle to verify client certificates on incoming requests |
| 57 |
// before trusting usernames in headers. |
| 58 |
ClientCAFile string |
| 59 |
|
| 60 |
UsernameHeaders []string |
| 61 |
UIDHeaders []string |
| 62 |
GroupHeaders []string |
| 63 |
ExtraHeaderPrefixes []string |
| 64 |
AllowedNames []string |
| 65 |
} |
| 66 |
|
| 67 |
func (s *RequestHeaderAuthenticationOptions) Validate() []error { |
| 68 |
allErrors := []error{} |
| 69 |
|
| 70 |
if err := checkForWhiteSpaceOnly("requestheader-username-headers", s.UsernameHeaders...); err != nil { |
| 71 |
allErrors = append(allErrors, err) |
| 72 |
} |
| 73 |
if err := checkForWhiteSpaceOnly("requestheader-group-headers", s.GroupHeaders...); err != nil { |
| 74 |
allErrors = append(allErrors, err) |
| 75 |
} |
| 76 |
if err := checkForWhiteSpaceOnly("requestheader-extra-headers-prefix", s.ExtraHeaderPrefixes...); err != nil { |
| 77 |
allErrors = append(allErrors, err) |
| 78 |
} |
| 79 |
if err := checkForWhiteSpaceOnly("requestheader-allowed-names", s.AllowedNames...); err != nil { |
| 80 |
allErrors = append(allErrors, err) |
| 81 |
} |
| 82 |
|
| 83 |
if len(s.UsernameHeaders) > 0 && !caseInsensitiveHas(s.UsernameHeaders, "X-Remote-User") { |
| 84 |
klog.Warningf("--requestheader-username-headers is set without specifying the standard X-Remote-User header - API aggregation will not work") |
| 85 |
} |
| 86 |
if len(s.GroupHeaders) > 0 && !caseInsensitiveHas(s.GroupHeaders, "X-Remote-Group") { |
| 87 |
klog.Warningf("--requestheader-group-headers is set without specifying the standard X-Remote-Group header - API aggregation will not work") |
| 88 |
} |
| 89 |
if len(s.ExtraHeaderPrefixes) > 0 && !caseInsensitiveHas(s.ExtraHeaderPrefixes, "X-Remote-Extra-") { |
| 90 |
klog.Warningf("--requestheader-extra-headers-prefix is set without specifying the standard X-Remote-Extra- header prefix - API aggregation will not work") |
| 91 |
} |
| 92 |
|
| 93 |
if !utilfeature.DefaultFeatureGate.Enabled(features.RemoteRequestHeaderUID) { |
| 94 |
if len(s.UIDHeaders) > 0 { |
| 95 |
allErrors = append(allErrors, fmt.Errorf("--requestheader-uid-headers requires the %q feature to be enabled", features.RemoteRequestHeaderUID)) |
| 96 |
} |
| 97 |
} else { |
| 98 |
if err := checkForWhiteSpaceOnly("requestheader-uid-headers", s.UIDHeaders...); err != nil { |
| 99 |
allErrors = append(allErrors, err) |
| 100 |
} |
| 101 |
if len(s.UIDHeaders) > 0 && !caseInsensitiveHas(s.UIDHeaders, "X-Remote-Uid") { |
| 102 |
// this was added later and so we are able to error out |
| 103 |
allErrors = append(allErrors, fmt.Errorf("--requestheader-uid-headers is set without specifying the standard X-Remote-Uid header - API aggregation will not work")) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return allErrors |
| 108 |
} |
| 109 |
|
| 110 |
func checkForWhiteSpaceOnly(flag string, headerNames ...string) error { |
| 111 |
for _, headerName := range headerNames { |
| 112 |
if len(strings.TrimSpace(headerName)) == 0 { |
| 113 |
return fmt.Errorf("empty value in %q", flag) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
func caseInsensitiveHas(headers []string, header string) bool { |
| 121 |
for _, h := range headers { |
| 122 |
if strings.EqualFold(h, header) { |
| 123 |
return true |
| 124 |
} |
| 125 |
} |
| 126 |
return false |
| 127 |
} |
| 128 |
|
| 129 |
func (s *RequestHeaderAuthenticationOptions) AddFlags(fs *pflag.FlagSet) { |
| 130 |
if s == nil { |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
fs.StringSliceVar(&s.UsernameHeaders, "requestheader-username-headers", s.UsernameHeaders, ""+ |
| 135 |
"List of request headers to inspect for usernames. X-Remote-User is common.") |
| 136 |
|
| 137 |
fs.StringSliceVar(&s.UIDHeaders, "requestheader-uid-headers", s.UIDHeaders, ""+ |
| 138 |
"List of request headers to inspect for UIDs. X-Remote-Uid is suggested. Requires the RemoteRequestHeaderUID feature to be enabled.") |
| 139 |
|
| 140 |
fs.StringSliceVar(&s.GroupHeaders, "requestheader-group-headers", s.GroupHeaders, ""+ |
| 141 |
"List of request headers to inspect for groups. X-Remote-Group is suggested.") |
| 142 |
|
| 143 |
fs.StringSliceVar(&s.ExtraHeaderPrefixes, "requestheader-extra-headers-prefix", s.ExtraHeaderPrefixes, ""+ |
| 144 |
"List of request header prefixes to inspect. X-Remote-Extra- is suggested.") |
| 145 |
|
| 146 |
fs.StringVar(&s.ClientCAFile, "requestheader-client-ca-file", s.ClientCAFile, ""+ |
| 147 |
"Root certificate bundle to use to verify client certificates on incoming requests "+ |
| 148 |
"before trusting usernames in headers specified by --requestheader-username-headers. "+ |
| 149 |
"WARNING: generally do not depend on authorization being already done for incoming requests.") |
| 150 |
|
| 151 |
fs.StringSliceVar(&s.AllowedNames, "requestheader-allowed-names", s.AllowedNames, ""+ |
| 152 |
"List of client certificate common names to allow to provide usernames in headers "+ |
| 153 |
"specified by --requestheader-username-headers. If empty, any client certificate validated "+ |
| 154 |
"by the authorities in --requestheader-client-ca-file is allowed.") |
| 155 |
} |
| 156 |
|
| 157 |
// ToAuthenticationRequestHeaderConfig returns a RequestHeaderConfig config object for these options |
| 158 |
// if necessary, nil otherwise. |
| 159 |
func (s *RequestHeaderAuthenticationOptions) ToAuthenticationRequestHeaderConfig() (*authenticatorfactory.RequestHeaderConfig, error) { |
| 160 |
if len(s.ClientCAFile) == 0 { |
| 161 |
return nil, nil |
| 162 |
} |
| 163 |
|
| 164 |
caBundleProvider, err := dynamiccertificates.NewDynamicCAContentFromFile("request-header", s.ClientCAFile) |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
|
| 169 |
return &authenticatorfactory.RequestHeaderConfig{ |
| 170 |
UsernameHeaders: headerrequest.StaticStringSlice(s.UsernameHeaders), |
| 171 |
UIDHeaders: headerrequest.StaticStringSlice(s.UIDHeaders), |
| 172 |
GroupHeaders: headerrequest.StaticStringSlice(s.GroupHeaders), |
| 173 |
ExtraHeaderPrefixes: headerrequest.StaticStringSlice(s.ExtraHeaderPrefixes), |
| 174 |
CAContentProvider: caBundleProvider, |
| 175 |
AllowedClientNames: headerrequest.StaticStringSlice(s.AllowedNames), |
| 176 |
}, nil |
| 177 |
} |
| 178 |
|
| 179 |
// ClientCertAuthenticationOptions provides different options for client cert auth. You should use `GetClientVerifyOptionFn` to |
| 180 |
// get the verify options for your authenticator. |
| 181 |
type ClientCertAuthenticationOptions struct { |
| 182 |
// ClientCA is the certificate bundle for all the signers that you'll recognize for incoming client certificates |
| 183 |
ClientCA string |
| 184 |
|
| 185 |
// CAContentProvider are the options for verifying incoming connections using mTLS and directly assigning to users. |
| 186 |
// Generally this is the CA bundle file used to authenticate client certificates |
| 187 |
// If non-nil, this takes priority over the ClientCA file. |
| 188 |
CAContentProvider dynamiccertificates.CAContentProvider |
| 189 |
} |
| 190 |
|
| 191 |
// GetClientVerifyOptionFn provides verify options for your authenticator while respecting the preferred order of verifiers. |
| 192 |
func (s *ClientCertAuthenticationOptions) GetClientCAContentProvider() (dynamiccertificates.CAContentProvider, error) { |
| 193 |
if s.CAContentProvider != nil { |
| 194 |
return s.CAContentProvider, nil |
| 195 |
} |
| 196 |
|
| 197 |
if len(s.ClientCA) == 0 { |
| 198 |
return nil, nil |
| 199 |
} |
| 200 |
|
| 201 |
return dynamiccertificates.NewDynamicCAContentFromFile("client-ca-bundle", s.ClientCA) |
| 202 |
} |
| 203 |
|
| 204 |
func (s *ClientCertAuthenticationOptions) AddFlags(fs *pflag.FlagSet) { |
| 205 |
fs.StringVar(&s.ClientCA, "client-ca-file", s.ClientCA, ""+ |
| 206 |
"If set, any request presenting a client certificate signed by one of "+ |
| 207 |
"the authorities in the client-ca-file is authenticated with an identity "+ |
| 208 |
"corresponding to the CommonName of the client certificate.") |
| 209 |
} |
| 210 |
|
| 211 |
// DelegatingAuthenticationOptions provides an easy way for composing API servers to delegate their authentication to |
| 212 |
// the root kube API server. The API federator will act as |
| 213 |
// a front proxy and direction connections will be able to delegate to the core kube API server |
| 214 |
type DelegatingAuthenticationOptions struct { |
| 215 |
// RemoteKubeConfigFile is the file to use to connect to a "normal" kube API server which hosts the |
| 216 |
// TokenAccessReview.authentication.k8s.io endpoint for checking tokens. |
| 217 |
RemoteKubeConfigFile string |
| 218 |
// RemoteKubeConfigFileOptional is specifying whether not specifying the kubeconfig or |
| 219 |
// a missing in-cluster config will be fatal. |
| 220 |
RemoteKubeConfigFileOptional bool |
| 221 |
|
| 222 |
// CacheTTL is the length of time that a token authentication answer will be cached. |
| 223 |
CacheTTL time.Duration |
| 224 |
|
| 225 |
ClientCert ClientCertAuthenticationOptions |
| 226 |
RequestHeader RequestHeaderAuthenticationOptions |
| 227 |
|
| 228 |
// SkipInClusterLookup indicates missing authentication configuration should not be retrieved from the cluster configmap |
| 229 |
SkipInClusterLookup bool |
| 230 |
|
| 231 |
// TolerateInClusterLookupFailure indicates failures to look up authentication configuration from the cluster configmap should not be fatal. |
| 232 |
// Setting this can result in an authenticator that will reject all requests. |
| 233 |
TolerateInClusterLookupFailure bool |
| 234 |
|
| 235 |
// WebhookRetryBackoff specifies the backoff parameters for the authentication webhook retry logic. |
| 236 |
// This allows us to configure the sleep time at each iteration and the maximum number of retries allowed |
| 237 |
// before we fail the webhook call in order to limit the fan out that ensues when the system is degraded. |
| 238 |
WebhookRetryBackoff *wait.Backoff |
| 239 |
|
| 240 |
// TokenRequestTimeout specifies a time limit for requests made by the authorization webhook client. |
| 241 |
// The default value is set to 10 seconds. |
| 242 |
TokenRequestTimeout time.Duration |
| 243 |
|
| 244 |
// CustomRoundTripperFn allows for specifying a middleware function for custom HTTP behaviour for the authentication webhook client. |
| 245 |
CustomRoundTripperFn transport.WrapperFunc |
| 246 |
|
| 247 |
// Anonymous gives user an option to enable/disable Anonymous authentication. |
| 248 |
Anonymous *apiserver.AnonymousAuthConfig |
| 249 |
} |
| 250 |
|
| 251 |
func NewDelegatingAuthenticationOptions() *DelegatingAuthenticationOptions { |
| 252 |
return &DelegatingAuthenticationOptions{ |
| 253 |
// very low for responsiveness, but high enough to handle storms |
| 254 |
CacheTTL: 10 * time.Second, |
| 255 |
ClientCert: ClientCertAuthenticationOptions{}, |
| 256 |
RequestHeader: RequestHeaderAuthenticationOptions{ |
| 257 |
UsernameHeaders: []string{"x-remote-user"}, |
| 258 |
// we specifically don't default UID headers as these were introduced |
| 259 |
// later (kube 1.32) and we don't want 3rd parties to be trusting the default headers |
| 260 |
// before we can safely say that all KAS instances know they should |
| 261 |
// remove them from an incoming request in its WithAuthentication handler. |
| 262 |
// The defaulting will be enabled in a future (1.33+) version. |
| 263 |
UIDHeaders: nil, |
| 264 |
GroupHeaders: []string{"x-remote-group"}, |
| 265 |
ExtraHeaderPrefixes: []string{"x-remote-extra-"}, |
| 266 |
}, |
| 267 |
WebhookRetryBackoff: DefaultAuthWebhookRetryBackoff(), |
| 268 |
TokenRequestTimeout: 10 * time.Second, |
| 269 |
Anonymous: &apiserver.AnonymousAuthConfig{Enabled: true}, |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// WithCustomRetryBackoff sets the custom backoff parameters for the authentication webhook retry logic. |
| 274 |
func (s *DelegatingAuthenticationOptions) WithCustomRetryBackoff(backoff wait.Backoff) { |
| 275 |
s.WebhookRetryBackoff = &backoff |
| 276 |
} |
| 277 |
|
| 278 |
// WithRequestTimeout sets the given timeout for requests made by the authentication webhook client. |
| 279 |
func (s *DelegatingAuthenticationOptions) WithRequestTimeout(timeout time.Duration) { |
| 280 |
s.TokenRequestTimeout = timeout |
| 281 |
} |
| 282 |
|
| 283 |
// WithCustomRoundTripper allows for specifying a middleware function for custom HTTP behaviour for the authentication webhook client. |
| 284 |
func (s *DelegatingAuthenticationOptions) WithCustomRoundTripper(rt transport.WrapperFunc) { |
| 285 |
s.CustomRoundTripperFn = rt |
| 286 |
} |
| 287 |
|
| 288 |
func (s *DelegatingAuthenticationOptions) Validate() []error { |
| 289 |
if s == nil { |
| 290 |
return nil |
| 291 |
} |
| 292 |
|
| 293 |
allErrors := []error{} |
| 294 |
allErrors = append(allErrors, s.RequestHeader.Validate()...) |
| 295 |
|
| 296 |
if s.WebhookRetryBackoff != nil && s.WebhookRetryBackoff.Steps <= 0 { |
| 297 |
allErrors = append(allErrors, fmt.Errorf("number of webhook retry attempts must be greater than 1, but is: %d", s.WebhookRetryBackoff.Steps)) |
| 298 |
} |
| 299 |
|
| 300 |
return allErrors |
| 301 |
} |
| 302 |
|
| 303 |
func (s *DelegatingAuthenticationOptions) AddFlags(fs *pflag.FlagSet) { |
| 304 |
if s == nil { |
| 305 |
return |
| 306 |
} |
| 307 |
|
| 308 |
var optionalKubeConfigSentence string |
| 309 |
if s.RemoteKubeConfigFileOptional { |
| 310 |
optionalKubeConfigSentence = " This is optional. If empty, all token requests are considered to be anonymous and no client CA is looked up in the cluster." |
| 311 |
} |
| 312 |
fs.StringVar(&s.RemoteKubeConfigFile, "authentication-kubeconfig", s.RemoteKubeConfigFile, ""+ |
| 313 |
"kubeconfig file pointing at the 'core' kubernetes server with enough rights to create "+ |
| 314 |
"tokenreviews.authentication.k8s.io."+optionalKubeConfigSentence) |
| 315 |
|
| 316 |
fs.DurationVar(&s.CacheTTL, "authentication-token-webhook-cache-ttl", s.CacheTTL, |
| 317 |
"The duration to cache responses from the webhook token authenticator.") |
| 318 |
|
| 319 |
s.ClientCert.AddFlags(fs) |
| 320 |
s.RequestHeader.AddFlags(fs) |
| 321 |
|
| 322 |
fs.BoolVar(&s.SkipInClusterLookup, "authentication-skip-lookup", s.SkipInClusterLookup, ""+ |
| 323 |
"If false, the authentication-kubeconfig will be used to lookup missing authentication "+ |
| 324 |
"configuration from the cluster.") |
| 325 |
fs.BoolVar(&s.TolerateInClusterLookupFailure, "authentication-tolerate-lookup-failure", s.TolerateInClusterLookupFailure, ""+ |
| 326 |
"If true, failures to look up missing authentication configuration from the cluster are not considered fatal. "+ |
| 327 |
"Note that this can result in authentication that treats all requests as anonymous.") |
| 328 |
} |
| 329 |
|
| 330 |
func (s *DelegatingAuthenticationOptions) ApplyTo(authenticationInfo *server.AuthenticationInfo, servingInfo *server.SecureServingInfo, openAPIConfig *openapicommon.Config) error { |
| 331 |
if s == nil { |
| 332 |
authenticationInfo.Authenticator = nil |
| 333 |
return nil |
| 334 |
} |
| 335 |
|
| 336 |
cfg := authenticatorfactory.DelegatingAuthenticatorConfig{ |
| 337 |
Anonymous: &apiserver.AnonymousAuthConfig{Enabled: true}, |
| 338 |
CacheTTL: s.CacheTTL, |
| 339 |
WebhookRetryBackoff: s.WebhookRetryBackoff, |
| 340 |
TokenAccessReviewTimeout: s.TokenRequestTimeout, |
| 341 |
} |
| 342 |
|
| 343 |
client, err := s.getClient() |
| 344 |
if err != nil { |
| 345 |
return fmt.Errorf("failed to get delegated authentication kubeconfig: %v", err) |
| 346 |
} |
| 347 |
|
| 348 |
// configure token review |
| 349 |
if client != nil { |
| 350 |
cfg.TokenAccessReviewClient = client.AuthenticationV1() |
| 351 |
} |
| 352 |
|
| 353 |
// get the clientCA information |
| 354 |
clientCASpecified := s.ClientCert != ClientCertAuthenticationOptions{} |
| 355 |
var clientCAProvider dynamiccertificates.CAContentProvider |
| 356 |
if clientCASpecified { |
| 357 |
clientCAProvider, err = s.ClientCert.GetClientCAContentProvider() |
| 358 |
if err != nil { |
| 359 |
return fmt.Errorf("unable to load client CA provider: %v", err) |
| 360 |
} |
| 361 |
cfg.ClientCertificateCAContentProvider = clientCAProvider |
| 362 |
if err = authenticationInfo.ApplyClientCert(cfg.ClientCertificateCAContentProvider, servingInfo); err != nil { |
| 363 |
return fmt.Errorf("unable to assign client CA provider: %v", err) |
| 364 |
} |
| 365 |
|
| 366 |
} else if !s.SkipInClusterLookup { |
| 367 |
if client == nil { |
| 368 |
klog.Warningf("No authentication-kubeconfig provided in order to lookup client-ca-file in configmap/%s in %s, so client certificate authentication won't work.", authenticationConfigMapName, authenticationConfigMapNamespace) |
| 369 |
} else { |
| 370 |
clientCAProvider, err = dynamiccertificates.NewDynamicCAFromConfigMapController("client-ca", authenticationConfigMapNamespace, authenticationConfigMapName, "client-ca-file", client) |
| 371 |
if err != nil { |
| 372 |
return fmt.Errorf("unable to load configmap based client CA file: %v", err) |
| 373 |
} |
| 374 |
cfg.ClientCertificateCAContentProvider = clientCAProvider |
| 375 |
if err = authenticationInfo.ApplyClientCert(cfg.ClientCertificateCAContentProvider, servingInfo); err != nil { |
| 376 |
return fmt.Errorf("unable to assign configmap based client CA file: %v", err) |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
requestHeaderCAFileSpecified := len(s.RequestHeader.ClientCAFile) > 0 |
| 383 |
var requestHeaderConfig *authenticatorfactory.RequestHeaderConfig |
| 384 |
if requestHeaderCAFileSpecified { |
| 385 |
requestHeaderConfig, err = s.RequestHeader.ToAuthenticationRequestHeaderConfig() |
| 386 |
if err != nil { |
| 387 |
return fmt.Errorf("unable to create request header authentication config: %v", err) |
| 388 |
} |
| 389 |
|
| 390 |
} else if !s.SkipInClusterLookup { |
| 391 |
if client == nil { |
| 392 |
klog.Warningf("No authentication-kubeconfig provided in order to lookup requestheader-client-ca-file in configmap/%s in %s, so request-header client certificate authentication won't work.", authenticationConfigMapName, authenticationConfigMapNamespace) |
| 393 |
} else { |
| 394 |
requestHeaderConfig, err = s.createRequestHeaderConfig(client) |
| 395 |
if err != nil { |
| 396 |
if s.TolerateInClusterLookupFailure { |
| 397 |
klog.Warningf("Error looking up in-cluster authentication configuration: %v", err) |
| 398 |
klog.Warning("Continuing without authentication configuration. This may treat all requests as anonymous.") |
| 399 |
klog.Warning("To require authentication configuration lookup to succeed, set --authentication-tolerate-lookup-failure=false") |
| 400 |
} else { |
| 401 |
return fmt.Errorf("unable to load configmap based request-header-client-ca-file: %v", err) |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
if requestHeaderConfig != nil { |
| 407 |
cfg.RequestHeaderConfig = requestHeaderConfig |
| 408 |
authenticationInfo.RequestHeaderConfig = requestHeaderConfig |
| 409 |
if err = authenticationInfo.ApplyClientCert(cfg.RequestHeaderConfig.CAContentProvider, servingInfo); err != nil { |
| 410 |
return fmt.Errorf("unable to load request-header-client-ca-file: %v", err) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// create authenticator |
| 415 |
authenticator, securityDefinitions, err := cfg.New() |
| 416 |
if err != nil { |
| 417 |
return err |
| 418 |
} |
| 419 |
authenticationInfo.Authenticator = authenticator |
| 420 |
if openAPIConfig != nil { |
| 421 |
openAPIConfig.SecurityDefinitions = securityDefinitions |
| 422 |
} |
| 423 |
|
| 424 |
return nil |
| 425 |
} |
| 426 |
|
| 427 |
const ( |
| 428 |
authenticationConfigMapNamespace = metav1.NamespaceSystem |
| 429 |
// authenticationConfigMapName is the name of ConfigMap in the kube-system namespace holding the root certificate |
| 430 |
// bundle to use to verify client certificates on incoming requests before trusting usernames in headers specified |
| 431 |
// by --requestheader-username-headers. This is created in the cluster by the kube-apiserver. |
| 432 |
// "WARNING: generally do not depend on authorization being already done for incoming requests.") |
| 433 |
authenticationConfigMapName = "extension-apiserver-authentication" |
| 434 |
) |
| 435 |
|
| 436 |
func (s *DelegatingAuthenticationOptions) createRequestHeaderConfig(client kubernetes.Interface) (*authenticatorfactory.RequestHeaderConfig, error) { |
| 437 |
dynamicRequestHeaderProvider, err := newDynamicRequestHeaderController(client) |
| 438 |
if err != nil { |
| 439 |
return nil, fmt.Errorf("unable to create request header authentication config: %v", err) |
| 440 |
} |
| 441 |
|
| 442 |
// look up authentication configuration in the cluster and in case of an err defer to authentication-tolerate-lookup-failure flag |
| 443 |
// We are passing the context to ProxyCerts.RunOnce as it needs to implement RunOnce(ctx) however the |
| 444 |
// context is not used at all. So passing a empty context shouldn't be a problem |
| 445 |
ctx := context.TODO() |
| 446 |
if err := dynamicRequestHeaderProvider.RunOnce(ctx); err != nil { |
| 447 |
return nil, err |
| 448 |
} |
| 449 |
|
| 450 |
return &authenticatorfactory.RequestHeaderConfig{ |
| 451 |
CAContentProvider: dynamicRequestHeaderProvider, |
| 452 |
UsernameHeaders: headerrequest.StringSliceProvider(headerrequest.StringSliceProviderFunc(dynamicRequestHeaderProvider.UsernameHeaders)), |
| 453 |
UIDHeaders: headerrequest.StringSliceProvider(headerrequest.StringSliceProviderFunc(dynamicRequestHeaderProvider.UIDHeaders)), |
| 454 |
GroupHeaders: headerrequest.StringSliceProvider(headerrequest.StringSliceProviderFunc(dynamicRequestHeaderProvider.GroupHeaders)), |
| 455 |
ExtraHeaderPrefixes: headerrequest.StringSliceProvider(headerrequest.StringSliceProviderFunc(dynamicRequestHeaderProvider.ExtraHeaderPrefixes)), |
| 456 |
AllowedClientNames: headerrequest.StringSliceProvider(headerrequest.StringSliceProviderFunc(dynamicRequestHeaderProvider.AllowedClientNames)), |
| 457 |
}, nil |
| 458 |
} |
| 459 |
|
| 460 |
// getClient returns a Kubernetes clientset. If s.RemoteKubeConfigFileOptional is true, nil will be returned |
| 461 |
// if no kubeconfig is specified by the user and the in-cluster config is not found. |
| 462 |
func (s *DelegatingAuthenticationOptions) getClient() (kubernetes.Interface, error) { |
| 463 |
var clientConfig *rest.Config |
| 464 |
var err error |
| 465 |
if len(s.RemoteKubeConfigFile) > 0 { |
| 466 |
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.RemoteKubeConfigFile} |
| 467 |
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) |
| 468 |
|
| 469 |
clientConfig, err = loader.ClientConfig() |
| 470 |
} else { |
| 471 |
// without the remote kubeconfig file, try to use the in-cluster config. Most addon API servers will |
| 472 |
// use this path. If it is optional, ignore errors. |
| 473 |
clientConfig, err = rest.InClusterConfig() |
| 474 |
if err != nil && s.RemoteKubeConfigFileOptional { |
| 475 |
if err != rest.ErrNotInCluster { |
| 476 |
klog.Warningf("failed to read in-cluster kubeconfig for delegated authentication: %v", err) |
| 477 |
} |
| 478 |
return nil, nil |
| 479 |
} |
| 480 |
} |
| 481 |
if err != nil { |
| 482 |
return nil, fmt.Errorf("failed to get delegated authentication kubeconfig: %v", err) |
| 483 |
} |
| 484 |
|
| 485 |
// set high qps/burst limits since this will effectively limit API server responsiveness |
| 486 |
clientConfig.QPS = 200 |
| 487 |
clientConfig.Burst = 400 |
| 488 |
// do not set a timeout on the http client, instead use context for cancellation |
| 489 |
// if multiple timeouts were set, the request will pick the smaller timeout to be applied, leaving other useless. |
| 490 |
// |
| 491 |
// see https://github.com/golang/go/blob/a937729c2c2f6950a32bc5cd0f5b88700882f078/src/net/http/client.go#L364 |
| 492 |
if s.CustomRoundTripperFn != nil { |
| 493 |
clientConfig.Wrap(s.CustomRoundTripperFn) |
| 494 |
} |
| 495 |
|
| 496 |
return kubernetes.NewForConfig(clientConfig) |
| 497 |
} |