Skip to content

Commit 7c78018

Browse files
authored
Merge pull request kubernetes#125473 from liggitt/serviceaccount-cleanup
Clean up service account options completion and fallback
2 parents 8c508c5 + b8be627 commit 7c78018

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

pkg/controlplane/apiserver/options/options.go

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
netutil "k8s.io/utils/net"
3838

3939
_ "k8s.io/kubernetes/pkg/features"
40-
kubeauthenticator "k8s.io/kubernetes/pkg/kubeapiserver/authenticator"
4140
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
4241
"k8s.io/kubernetes/pkg/serviceaccount"
4342
)
@@ -230,49 +229,36 @@ func (o *Options) Complete(alternateDNS []string, alternateIPs []net.IP) (Comple
230229
// adjust authentication for completed authorization
231230
completed.Authentication.ApplyAuthorization(completed.Authorization)
232231

233-
// Use (ServiceAccountSigningKeyFile != "") as a proxy to the user enabling
234-
// TokenRequest functionality. This defaulting was convenient, but messed up
235-
// a lot of people when they rotated their serving cert with no idea it was
236-
// connected to their service account keys. We are taking this opportunity to
237-
// remove this problematic defaulting.
238-
if completed.ServiceAccountSigningKeyFile == "" {
239-
// Default to the private server key for service account token signing
240-
if len(completed.Authentication.ServiceAccounts.KeyFiles) == 0 && completed.SecureServing.ServerCert.CertKey.KeyFile != "" {
241-
if kubeauthenticator.IsValidServiceAccountKeyFile(completed.SecureServing.ServerCert.CertKey.KeyFile) {
242-
completed.Authentication.ServiceAccounts.KeyFiles = []string{completed.SecureServing.ServerCert.CertKey.KeyFile}
243-
} else {
244-
klog.Warning("No TLS key provided, service account token authentication disabled")
232+
// verify and adjust ServiceAccountTokenMaxExpiration
233+
if completed.Authentication.ServiceAccounts.MaxExpiration != 0 {
234+
lowBound := time.Hour
235+
upBound := time.Duration(1<<32) * time.Second
236+
if completed.Authentication.ServiceAccounts.MaxExpiration < lowBound ||
237+
completed.Authentication.ServiceAccounts.MaxExpiration > upBound {
238+
return CompletedOptions{}, fmt.Errorf("the service-account-max-token-expiration must be between 1 hour and 2^32 seconds")
239+
}
240+
if completed.Authentication.ServiceAccounts.ExtendExpiration {
241+
if completed.Authentication.ServiceAccounts.MaxExpiration < serviceaccount.WarnOnlyBoundTokenExpirationSeconds*time.Second {
242+
klog.Warningf("service-account-extend-token-expiration is true, in order to correctly trigger safe transition logic, service-account-max-token-expiration must be set longer than %d seconds (currently %s)", serviceaccount.WarnOnlyBoundTokenExpirationSeconds, completed.Authentication.ServiceAccounts.MaxExpiration)
243+
}
244+
if completed.Authentication.ServiceAccounts.MaxExpiration < serviceaccount.ExpirationExtensionSeconds*time.Second {
245+
klog.Warningf("service-account-extend-token-expiration is true, enabling tokens valid up to %d seconds, which is longer than service-account-max-token-expiration set to %s seconds", serviceaccount.ExpirationExtensionSeconds, completed.Authentication.ServiceAccounts.MaxExpiration)
245246
}
246247
}
247248
}
249+
completed.ServiceAccountTokenMaxExpiration = completed.Authentication.ServiceAccounts.MaxExpiration
248250

249-
if completed.ServiceAccountSigningKeyFile != "" && len(completed.Authentication.ServiceAccounts.Issuers) != 0 && completed.Authentication.ServiceAccounts.Issuers[0] != "" {
250-
sk, err := keyutil.PrivateKeyFromFile(completed.ServiceAccountSigningKeyFile)
251-
if err != nil {
252-
return CompletedOptions{}, fmt.Errorf("failed to parse service-account-issuer-key-file: %v", err)
253-
}
254-
if completed.Authentication.ServiceAccounts.MaxExpiration != 0 {
255-
lowBound := time.Hour
256-
upBound := time.Duration(1<<32) * time.Second
257-
if completed.Authentication.ServiceAccounts.MaxExpiration < lowBound ||
258-
completed.Authentication.ServiceAccounts.MaxExpiration > upBound {
259-
return CompletedOptions{}, fmt.Errorf("the service-account-max-token-expiration must be between 1 hour and 2^32 seconds")
251+
if len(completed.Authentication.ServiceAccounts.Issuers) != 0 && completed.Authentication.ServiceAccounts.Issuers[0] != "" {
252+
if completed.ServiceAccountSigningKeyFile != "" {
253+
sk, err := keyutil.PrivateKeyFromFile(completed.ServiceAccountSigningKeyFile)
254+
if err != nil {
255+
return CompletedOptions{}, fmt.Errorf("failed to parse service-account-issuer-key-file: %w", err)
260256
}
261-
if completed.Authentication.ServiceAccounts.ExtendExpiration {
262-
if completed.Authentication.ServiceAccounts.MaxExpiration < serviceaccount.WarnOnlyBoundTokenExpirationSeconds*time.Second {
263-
klog.Warningf("service-account-extend-token-expiration is true, in order to correctly trigger safe transition logic, service-account-max-token-expiration must be set longer than %d seconds (currently %s)", serviceaccount.WarnOnlyBoundTokenExpirationSeconds, completed.Authentication.ServiceAccounts.MaxExpiration)
264-
}
265-
if completed.Authentication.ServiceAccounts.MaxExpiration < serviceaccount.ExpirationExtensionSeconds*time.Second {
266-
klog.Warningf("service-account-extend-token-expiration is true, enabling tokens valid up to %d seconds, which is longer than service-account-max-token-expiration set to %s seconds", serviceaccount.ExpirationExtensionSeconds, completed.Authentication.ServiceAccounts.MaxExpiration)
267-
}
257+
completed.ServiceAccountIssuer, err = serviceaccount.JWTTokenGenerator(completed.Authentication.ServiceAccounts.Issuers[0], sk)
258+
if err != nil {
259+
return CompletedOptions{}, fmt.Errorf("failed to build token generator: %w", err)
268260
}
269261
}
270-
271-
completed.ServiceAccountIssuer, err = serviceaccount.JWTTokenGenerator(completed.Authentication.ServiceAccounts.Issuers[0], sk)
272-
if err != nil {
273-
return CompletedOptions{}, fmt.Errorf("failed to build token generator: %v", err)
274-
}
275-
completed.ServiceAccountTokenMaxExpiration = completed.Authentication.ServiceAccounts.MaxExpiration
276262
}
277263

278264
for key, value := range completed.APIEnablement.RuntimeConfig {

0 commit comments

Comments
 (0)