@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"net/url"
24
24
"os"
25
+ "reflect"
25
26
"strings"
26
27
"sync"
27
28
"time"
@@ -32,6 +33,7 @@ import (
32
33
"k8s.io/apimachinery/pkg/runtime"
33
34
"k8s.io/apimachinery/pkg/runtime/serializer"
34
35
"k8s.io/apimachinery/pkg/util/sets"
36
+ "k8s.io/apimachinery/pkg/util/validation/field"
35
37
"k8s.io/apimachinery/pkg/util/wait"
36
38
"k8s.io/apiserver/pkg/apis/apiserver"
37
39
"k8s.io/apiserver/pkg/apis/apiserver/install"
@@ -95,7 +97,8 @@ type BuiltInAuthenticationOptions struct {
95
97
96
98
// AnonymousAuthenticationOptions contains anonymous authentication options for API Server
97
99
type AnonymousAuthenticationOptions struct {
98
- Allow bool
100
+ Allow bool
101
+ areFlagsSet func () bool
99
102
}
100
103
101
104
// BootstrapTokenAuthenticationOptions contains bootstrap token authentication options for API Server
@@ -169,7 +172,10 @@ func (o *BuiltInAuthenticationOptions) WithAll() *BuiltInAuthenticationOptions {
169
172
170
173
// WithAnonymous set default value for anonymous authentication
171
174
func (o * BuiltInAuthenticationOptions ) WithAnonymous () * BuiltInAuthenticationOptions {
172
- o .Anonymous = & AnonymousAuthenticationOptions {Allow : true }
175
+ o .Anonymous = & AnonymousAuthenticationOptions {
176
+ Allow : true ,
177
+ areFlagsSet : func () bool { return false },
178
+ }
173
179
return o
174
180
}
175
181
@@ -294,6 +300,14 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
294
300
return
295
301
}
296
302
303
+ fs .StringVar (& o .AuthenticationConfigFile , "authentication-config" , o .AuthenticationConfigFile , "" +
304
+ "File with Authentication Configuration to configure the JWT Token authenticator or the anonymous authenticator. " +
305
+ "Note: This feature is in Alpha since v1.29." +
306
+ "--feature-gate=StructuredAuthenticationConfiguration=true needs to be set for enabling this feature." +
307
+ "This feature is mutually exclusive with the oidc-* flags." +
308
+ "To configure anonymous authenticator you need to enable --feature-gate=AnonymousAuthConfigurableEndpoints." +
309
+ "When you configure anonymous authenticator in the authentication config you cannot use the --anonymous-auth flag." )
310
+
297
311
fs .StringSliceVar (& o .APIAudiences , "api-audiences" , o .APIAudiences , "" +
298
312
"Identifiers of the API. The service account token authenticator will validate that " +
299
313
"tokens used against the API are bound to at least one of these audiences. If the " +
@@ -305,6 +319,10 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
305
319
"Enables anonymous requests to the secure port of the API server. " +
306
320
"Requests that are not rejected by another authentication method are treated as anonymous requests. " +
307
321
"Anonymous requests have a username of system:anonymous, and a group name of system:unauthenticated." )
322
+
323
+ o .Anonymous .areFlagsSet = func () bool {
324
+ return fs .Changed ("anonymous-auth" )
325
+ }
308
326
}
309
327
310
328
if o .BootstrapToken != nil {
@@ -358,12 +376,6 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
358
376
"If set, the claim is verified to be present in the ID Token with a matching value. " +
359
377
"Repeat this flag to specify multiple claims." )
360
378
361
- fs .StringVar (& o .AuthenticationConfigFile , "authentication-config" , o .AuthenticationConfigFile , "" +
362
- "File with Authentication Configuration to configure the JWT Token authenticator. " +
363
- "Note: This feature is in Alpha since v1.29." +
364
- "--feature-gate=StructuredAuthenticationConfiguration=true needs to be set for enabling this feature." +
365
- "This feature is mutually exclusive with the oidc-* flags." )
366
-
367
379
o .OIDC .areFlagsConfigured = func () bool {
368
380
return fs .Changed (oidcIssuerURLFlag ) ||
369
381
fs .Changed (oidcClientIDFlag ) ||
@@ -452,10 +464,6 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
452
464
TokenFailureCacheTTL : o .TokenFailureCacheTTL ,
453
465
}
454
466
455
- if o .Anonymous != nil {
456
- ret .Anonymous = o .Anonymous .Allow
457
- }
458
-
459
467
if o .BootstrapToken != nil {
460
468
ret .BootstrapToken = o .BootstrapToken .Enable
461
469
}
@@ -469,12 +477,18 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
469
477
}
470
478
471
479
// When the StructuredAuthenticationConfiguration feature is enabled and the authentication config file is provided,
472
- // load the authentication config from the file.
480
+ // load the authentication config from the file, otherwise set up an empty configuration .
473
481
if len (o .AuthenticationConfigFile ) > 0 {
474
482
var err error
475
483
if ret .AuthenticationConfig , ret .AuthenticationConfigData , err = loadAuthenticationConfig (o .AuthenticationConfigFile ); err != nil {
476
484
return kubeauthenticator.Config {}, err
477
485
}
486
+ } else {
487
+ ret .AuthenticationConfig = & apiserver.AuthenticationConfiguration {}
488
+ }
489
+
490
+ // Set up JWT authenticators from config file or from flags
491
+ if len (o .AuthenticationConfigFile ) > 0 {
478
492
// all known signing algs are allowed when using authentication config
479
493
// TODO: what we really want to express is 'any alg is fine as long it matches a public key'
480
494
ret .OIDCSigningAlgs = oidc .AllValidSigningAlgorithms ()
@@ -532,20 +546,30 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
532
546
jwtAuthenticator .ClaimValidationRules = claimValidationRules
533
547
}
534
548
535
- authConfig := & apiserver.AuthenticationConfiguration {
536
- JWT : []apiserver.JWTAuthenticator {jwtAuthenticator },
537
- }
549
+ ret .AuthenticationConfig .JWT = []apiserver.JWTAuthenticator {jwtAuthenticator }
538
550
539
- ret .AuthenticationConfig = authConfig
540
551
ret .OIDCSigningAlgs = o .OIDC .SigningAlgs
541
552
}
542
553
543
- if ret .AuthenticationConfig != nil {
544
- if err := apiservervalidation .ValidateAuthenticationConfiguration (ret .AuthenticationConfig , ret .ServiceAccountIssuers ).ToAggregate (); err != nil {
545
- return kubeauthenticator.Config {}, err
554
+ // Set up anonymous authenticator from config file or flags
555
+ if o .Anonymous != nil {
556
+ switch {
557
+ case ret .AuthenticationConfig .Anonymous != nil && o .Anonymous .areFlagsSet ():
558
+ // Flags and config file are mutually exclusive
559
+ return kubeauthenticator.Config {}, field .Forbidden (field .NewPath ("anonymous" ), "--anonynous-auth flag cannot be set when anonymous field is configured in authentication configuration file" )
560
+ case ret .AuthenticationConfig .Anonymous != nil :
561
+ // Use the config-file-specified values
562
+ ret .Anonymous = * ret .AuthenticationConfig .Anonymous
563
+ default :
564
+ // Use the flag-specified values
565
+ ret .Anonymous = apiserver.AnonymousAuthConfig {Enabled : o .Anonymous .Allow }
546
566
}
547
567
}
548
568
569
+ if err := apiservervalidation .ValidateAuthenticationConfiguration (ret .AuthenticationConfig , ret .ServiceAccountIssuers ).ToAggregate (); err != nil {
570
+ return kubeauthenticator.Config {}, err
571
+ }
572
+
549
573
if o .RequestHeader != nil {
550
574
var err error
551
575
ret .RequestHeaderConfig , err = o .RequestHeader .ToAuthenticationRequestHeaderConfig ()
@@ -667,6 +691,10 @@ func (o *BuiltInAuthenticationOptions) ApplyTo(
667
691
authenticationconfigmetrics .RegisterMetrics ()
668
692
trackedAuthenticationConfigData := authenticatorConfig .AuthenticationConfigData
669
693
var mu sync.Mutex
694
+
695
+ // ensure anonymous config doesn't change on reload
696
+ originalFileAnonymousConfig := authenticatorConfig .AuthenticationConfig .DeepCopy ().Anonymous
697
+
670
698
go filesystem .WatchUntil (
671
699
ctx ,
672
700
time .Minute ,
@@ -700,7 +728,11 @@ func (o *BuiltInAuthenticationOptions) ApplyTo(
700
728
return
701
729
}
702
730
703
- if err := apiservervalidation .ValidateAuthenticationConfiguration (authConfig , authenticatorConfig .ServiceAccountIssuers ).ToAggregate (); err != nil {
731
+ validationErrs := apiservervalidation .ValidateAuthenticationConfiguration (authConfig , authenticatorConfig .ServiceAccountIssuers )
732
+ if ! reflect .DeepEqual (originalFileAnonymousConfig , authConfig .Anonymous ) {
733
+ validationErrs = append (validationErrs , field .Forbidden (field .NewPath ("anonymous" ), "changed from initial configuration file" ))
734
+ }
735
+ if err := validationErrs .ToAggregate (); err != nil {
704
736
klog .ErrorS (err , "failed to validate authentication config" )
705
737
authenticationconfigmetrics .RecordAuthenticationConfigAutomaticReloadFailure (apiServerID )
706
738
// this config is not semantically valid and never will be, update the tracker so we stop retrying
0 commit comments