Skip to content

Commit 8a47952

Browse files
Merge pull request #14916 from liggitt/token-cache
Automatic merge from submit-queue Add short TTL cache to token authentication Addresses frequent etcd lookups due to high-volume controllers Fixes authenticator aspect of https://bugzilla.redhat.com/show_bug.cgi?id=1464579
2 parents 72f5bc5 + 49589f4 commit 8a47952

File tree

15 files changed

+854
-31
lines changed

15 files changed

+854
-31
lines changed

pkg/cmd/server/kubernetes/master/master_config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ func TestAPIServerDefaults(t *testing.T) {
140140
},
141141
TokenFile: &kubeoptions.TokenFileAuthenticationOptions{},
142142
WebHook: &kubeoptions.WebHookAuthenticationOptions{CacheTTL: 2 * time.Minute},
143+
144+
TokenSuccessCacheTTL: 10 * time.Second,
145+
TokenFailureCacheTTL: 0,
143146
},
144147
Authorization: &kubeoptions.BuiltInAuthorizationOptions{
145148
Mode: "AlwaysAllow",

pkg/cmd/server/origin/master_config.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"reflect"
99
"strings"
10+
"time"
1011

1112
"github.com/golang/glog"
1213

@@ -27,6 +28,8 @@ import (
2728
"k8s.io/apiserver/pkg/authentication/request/union"
2829
"k8s.io/apiserver/pkg/authentication/request/websocket"
2930
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
31+
tokencache "k8s.io/apiserver/pkg/authentication/token/cache"
32+
tokenunion "k8s.io/apiserver/pkg/authentication/token/union"
3033
"k8s.io/apiserver/pkg/authentication/user"
3134
kauthorizer "k8s.io/apiserver/pkg/authorization/authorizer"
3235
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
@@ -672,7 +675,7 @@ func newAdmissionChain(pluginNames []string, admissionConfigFilename string, plu
672675

673676
func newAuthenticator(config configapi.MasterConfig, accessTokenGetter oauthclient.OAuthAccessTokenInterface, tokenGetter serviceaccount.ServiceAccountTokenGetter, userGetter userclient.UserResourceInterface, apiClientCAs *x509.CertPool, groupMapper identitymapper.UserToGroupMapper) (authenticator.Request, error) {
674677
authenticators := []authenticator.Request{}
675-
tokenAuthenticators := []authenticator.Request{}
678+
tokenAuthenticators := []authenticator.Token{}
676679

677680
// ServiceAccount token
678681
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
@@ -685,31 +688,32 @@ func newAuthenticator(config configapi.MasterConfig, accessTokenGetter oauthclie
685688
publicKeys = append(publicKeys, readPublicKeys...)
686689
}
687690
serviceAccountTokenAuthenticator := serviceaccount.JWTTokenAuthenticator(publicKeys, true, tokenGetter)
688-
tokenAuthenticators = append(
689-
tokenAuthenticators,
690-
bearertoken.New(serviceAccountTokenAuthenticator),
691-
websocket.NewProtocolAuthenticator(serviceAccountTokenAuthenticator),
692-
paramtoken.New("access_token", serviceAccountTokenAuthenticator, true),
693-
)
691+
tokenAuthenticators = append(tokenAuthenticators, serviceAccountTokenAuthenticator)
694692
}
695693

696694
// OAuth token
697695
if config.OAuthConfig != nil {
698696
oauthTokenAuthenticator := authnregistry.NewTokenAuthenticator(accessTokenGetter, userGetter, groupMapper)
699-
oauthTokenRequestAuthenticators := []authenticator.Request{
700-
bearertoken.New(oauthTokenAuthenticator),
701-
websocket.NewProtocolAuthenticator(oauthTokenAuthenticator),
702-
paramtoken.New("access_token", oauthTokenAuthenticator, true),
703-
}
704-
705697
tokenAuthenticators = append(tokenAuthenticators,
706698
// if you have a bearer token, you're a human (usually)
707699
// if you change this, have a look at the impersonationFilter where we attach groups to the impersonated user
708-
group.NewGroupAdder(union.New(oauthTokenRequestAuthenticators...), []string{bootstrappolicy.AuthenticatedOAuthGroup}))
700+
group.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
709701
}
710702

711703
if len(tokenAuthenticators) > 0 {
712-
authenticators = append(authenticators, union.New(tokenAuthenticators...))
704+
// Combine all token authenticators
705+
tokenAuth := tokenunion.New(tokenAuthenticators...)
706+
707+
// wrap with short cache on success.
708+
// this means a revoked service account token or access token will be valid for up to 10 seconds.
709+
// it also means group membership changes on users may take up to 10 seconds to become effective.
710+
tokenAuth = tokencache.New(tokenAuth, 10*time.Second, 0)
711+
712+
authenticators = append(authenticators,
713+
bearertoken.New(tokenAuth),
714+
websocket.NewProtocolAuthenticator(tokenAuth),
715+
paramtoken.New("access_token", tokenAuth, true),
716+
)
713717
}
714718

715719
// build cert authenticator

vendor/k8s.io/kubernetes/pkg/kubeapiserver/authenticator/config.go

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/pkg/kubeapiserver/options/authentication.go

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/staging/src/k8s.io/apiserver/pkg/authentication/group/token_group_adder.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/staging/src/k8s.io/apiserver/pkg/authentication/group/token_group_adder_test.go

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/staging/src/k8s.io/apiserver/pkg/authentication/token/cache/BUILD

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)