@@ -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"
@@ -97,7 +99,8 @@ type BuiltInAuthenticationOptions struct {
97
99
98
100
// AnonymousAuthenticationOptions contains anonymous authentication options for API Server
99
101
type AnonymousAuthenticationOptions struct {
100
- Allow bool
102
+ Allow bool
103
+ areFlagsSet func () bool
101
104
}
102
105
103
106
// BootstrapTokenAuthenticationOptions contains bootstrap token authentication options for API Server
@@ -171,7 +174,10 @@ func (o *BuiltInAuthenticationOptions) WithAll() *BuiltInAuthenticationOptions {
171
174
172
175
// WithAnonymous set default value for anonymous authentication
173
176
func (o * BuiltInAuthenticationOptions ) WithAnonymous () * BuiltInAuthenticationOptions {
174
- o .Anonymous = & AnonymousAuthenticationOptions {Allow : true }
177
+ o .Anonymous = & AnonymousAuthenticationOptions {
178
+ Allow : true ,
179
+ areFlagsSet : func () bool { return false },
180
+ }
175
181
return o
176
182
}
177
183
@@ -296,6 +302,14 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
296
302
return
297
303
}
298
304
305
+ fs .StringVar (& o .AuthenticationConfigFile , "authentication-config" , o .AuthenticationConfigFile , "" +
306
+ "File with Authentication Configuration to configure the JWT Token authenticator or the anonymous authenticator. " +
307
+ "Note: This feature is in Alpha since v1.29." +
308
+ "--feature-gate=StructuredAuthenticationConfiguration=true needs to be set for enabling this feature." +
309
+ "This feature is mutually exclusive with the oidc-* flags." +
310
+ "To configure anonymous authenticator you need to enable --feature-gate=AnonymousAuthConfigurableEndpoints." +
311
+ "When you configure anonymous authenticator in the authentication config you cannot use the --anonymous-auth flag." )
312
+
299
313
fs .StringSliceVar (& o .APIAudiences , "api-audiences" , o .APIAudiences , "" +
300
314
"Identifiers of the API. The service account token authenticator will validate that " +
301
315
"tokens used against the API are bound to at least one of these audiences. If the " +
@@ -307,6 +321,10 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
307
321
"Enables anonymous requests to the secure port of the API server. " +
308
322
"Requests that are not rejected by another authentication method are treated as anonymous requests. " +
309
323
"Anonymous requests have a username of system:anonymous, and a group name of system:unauthenticated." )
324
+
325
+ o .Anonymous .areFlagsSet = func () bool {
326
+ return fs .Changed ("anonymous-auth" )
327
+ }
310
328
}
311
329
312
330
if o .BootstrapToken != nil {
@@ -360,12 +378,6 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
360
378
"If set, the claim is verified to be present in the ID Token with a matching value. " +
361
379
"Repeat this flag to specify multiple claims." )
362
380
363
- fs .StringVar (& o .AuthenticationConfigFile , "authentication-config" , o .AuthenticationConfigFile , "" +
364
- "File with Authentication Configuration to configure the JWT Token authenticator. " +
365
- "Note: This feature is in Alpha since v1.29." +
366
- "--feature-gate=StructuredAuthenticationConfiguration=true needs to be set for enabling this feature." +
367
- "This feature is mutually exclusive with the oidc-* flags." )
368
-
369
381
o .OIDC .areFlagsConfigured = func () bool {
370
382
return fs .Changed (oidcIssuerURLFlag ) ||
371
383
fs .Changed (oidcClientIDFlag ) ||
@@ -454,10 +466,6 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
454
466
TokenFailureCacheTTL : o .TokenFailureCacheTTL ,
455
467
}
456
468
457
- if o .Anonymous != nil {
458
- ret .Anonymous = o .Anonymous .Allow
459
- }
460
-
461
469
if o .BootstrapToken != nil {
462
470
ret .BootstrapToken = o .BootstrapToken .Enable
463
471
}
@@ -471,12 +479,18 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
471
479
}
472
480
473
481
// When the StructuredAuthenticationConfiguration feature is enabled and the authentication config file is provided,
474
- // load the authentication config from the file.
482
+ // load the authentication config from the file, otherwise set up an empty configuration .
475
483
if len (o .AuthenticationConfigFile ) > 0 {
476
484
var err error
477
485
if ret .AuthenticationConfig , ret .AuthenticationConfigData , err = loadAuthenticationConfig (o .AuthenticationConfigFile ); err != nil {
478
486
return kubeauthenticator.Config {}, err
479
487
}
488
+ } else {
489
+ ret .AuthenticationConfig = & apiserver.AuthenticationConfiguration {}
490
+ }
491
+
492
+ // Set up JWT authenticators from config file or from flags
493
+ if len (o .AuthenticationConfigFile ) > 0 {
480
494
// all known signing algs are allowed when using authentication config
481
495
// TODO: what we really want to express is 'any alg is fine as long it matches a public key'
482
496
ret .OIDCSigningAlgs = oidc .AllValidSigningAlgorithms ()
@@ -534,20 +548,30 @@ func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticat
534
548
jwtAuthenticator .ClaimValidationRules = claimValidationRules
535
549
}
536
550
537
- authConfig := & apiserver.AuthenticationConfiguration {
538
- JWT : []apiserver.JWTAuthenticator {jwtAuthenticator },
539
- }
551
+ ret .AuthenticationConfig .JWT = []apiserver.JWTAuthenticator {jwtAuthenticator }
540
552
541
- ret .AuthenticationConfig = authConfig
542
553
ret .OIDCSigningAlgs = o .OIDC .SigningAlgs
543
554
}
544
555
545
- if ret .AuthenticationConfig != nil {
546
- if err := apiservervalidation .ValidateAuthenticationConfiguration (ret .AuthenticationConfig , ret .ServiceAccountIssuers ).ToAggregate (); err != nil {
547
- return kubeauthenticator.Config {}, err
556
+ // Set up anonymous authenticator from config file or flags
557
+ if o .Anonymous != nil {
558
+ switch {
559
+ case ret .AuthenticationConfig .Anonymous != nil && o .Anonymous .areFlagsSet ():
560
+ // Flags and config file are mutually exclusive
561
+ return kubeauthenticator.Config {}, field .Forbidden (field .NewPath ("anonymous" ), "--anonynous-auth flag cannot be set when anonymous field is configured in authentication configuration file" )
562
+ case ret .AuthenticationConfig .Anonymous != nil :
563
+ // Use the config-file-specified values
564
+ ret .Anonymous = * ret .AuthenticationConfig .Anonymous
565
+ default :
566
+ // Use the flag-specified values
567
+ ret .Anonymous = apiserver.AnonymousAuthConfig {Enabled : o .Anonymous .Allow }
548
568
}
549
569
}
550
570
571
+ if err := apiservervalidation .ValidateAuthenticationConfiguration (ret .AuthenticationConfig , ret .ServiceAccountIssuers ).ToAggregate (); err != nil {
572
+ return kubeauthenticator.Config {}, err
573
+ }
574
+
551
575
if o .RequestHeader != nil {
552
576
var err error
553
577
ret .RequestHeaderConfig , err = o .RequestHeader .ToAuthenticationRequestHeaderConfig ()
@@ -683,6 +707,10 @@ func (o *BuiltInAuthenticationOptions) ApplyTo(
683
707
authenticationconfigmetrics .RegisterMetrics ()
684
708
trackedAuthenticationConfigData := authenticatorConfig .AuthenticationConfigData
685
709
var mu sync.Mutex
710
+
711
+ // ensure anonymous config doesn't change on reload
712
+ originalFileAnonymousConfig := authenticatorConfig .AuthenticationConfig .DeepCopy ().Anonymous
713
+
686
714
go filesystem .WatchUntil (
687
715
ctx ,
688
716
time .Minute ,
@@ -716,7 +744,11 @@ func (o *BuiltInAuthenticationOptions) ApplyTo(
716
744
return
717
745
}
718
746
719
- if err := apiservervalidation .ValidateAuthenticationConfiguration (authConfig , authenticatorConfig .ServiceAccountIssuers ).ToAggregate (); err != nil {
747
+ validationErrs := apiservervalidation .ValidateAuthenticationConfiguration (authConfig , authenticatorConfig .ServiceAccountIssuers )
748
+ if ! reflect .DeepEqual (originalFileAnonymousConfig , authConfig .Anonymous ) {
749
+ validationErrs = append (validationErrs , field .Forbidden (field .NewPath ("anonymous" ), "changed from initial configuration file" ))
750
+ }
751
+ if err := validationErrs .ToAggregate (); err != nil {
720
752
klog .ErrorS (err , "failed to validate authentication config" )
721
753
authenticationconfigmetrics .RecordAuthenticationConfigAutomaticReloadFailure (apiServerID )
722
754
// this config is not semantically valid and never will be, update the tracker so we stop retrying
0 commit comments