Skip to content

Commit 61eb559

Browse files
committed
Add short ttl cache to token authenticator on success
1 parent 4a137e5 commit 61eb559

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Diff for: pkg/cmd/server/origin/master_config.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"k8s.io/apiserver/pkg/authentication/request/union"
3131
"k8s.io/apiserver/pkg/authentication/request/websocket"
3232
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
33+
tokencache "k8s.io/apiserver/pkg/authentication/token/cache"
34+
tokenunion "k8s.io/apiserver/pkg/authentication/token/union"
3335
"k8s.io/apiserver/pkg/authentication/user"
3436
kauthorizer "k8s.io/apiserver/pkg/authorization/authorizer"
3537
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
@@ -913,7 +915,7 @@ func newServiceAccountTokenGetter(options configapi.MasterConfig) (serviceaccoun
913915

914916
func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptions.Getter, tokenGetter serviceaccount.ServiceAccountTokenGetter, apiClientCAs *x509.CertPool, groupMapper identitymapper.UserToGroupMapper) (authenticator.Request, error) {
915917
authenticators := []authenticator.Request{}
916-
tokenAuthenticators := []authenticator.Request{}
918+
tokenAuthenticators := []authenticator.Token{}
917919

918920
// ServiceAccount token
919921
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
@@ -926,12 +928,7 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
926928
publicKeys = append(publicKeys, readPublicKeys...)
927929
}
928930
serviceAccountTokenAuthenticator := serviceaccount.JWTTokenAuthenticator(publicKeys, true, tokenGetter)
929-
tokenAuthenticators = append(
930-
tokenAuthenticators,
931-
bearertoken.New(serviceAccountTokenAuthenticator),
932-
websocket.NewProtocolAuthenticator(serviceAccountTokenAuthenticator),
933-
paramtoken.New("access_token", serviceAccountTokenAuthenticator, true),
934-
)
931+
tokenAuthenticators = append(tokenAuthenticators, serviceAccountTokenAuthenticator)
935932
}
936933

937934
// OAuth token
@@ -940,20 +937,26 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
940937
if err != nil {
941938
return nil, fmt.Errorf("Error building OAuth token authenticator: %v", err)
942939
}
943-
oauthTokenRequestAuthenticators := []authenticator.Request{
944-
bearertoken.New(oauthTokenAuthenticator),
945-
websocket.NewProtocolAuthenticator(oauthTokenAuthenticator),
946-
paramtoken.New("access_token", oauthTokenAuthenticator, true),
947-
}
948-
949940
tokenAuthenticators = append(tokenAuthenticators,
950941
// if you have a bearer token, you're a human (usually)
951942
// if you change this, have a look at the impersonationFilter where we attach groups to the impersonated user
952-
group.NewGroupAdder(union.New(oauthTokenRequestAuthenticators...), []string{bootstrappolicy.AuthenticatedOAuthGroup}))
943+
group.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
953944
}
954945

955946
if len(tokenAuthenticators) > 0 {
956-
authenticators = append(authenticators, union.New(tokenAuthenticators...))
947+
// Combine all token authenticators
948+
tokenAuth := tokenunion.New(tokenAuthenticators...)
949+
950+
// wrap with short cache on success.
951+
// this means a revoked service account token or access token will be valid for up to 10 seconds.
952+
// it also means group membership changes on users may take up to 10 seconds to become effective.
953+
tokenAuth = tokencache.New(tokenAuth, 10*time.Second, 0)
954+
955+
authenticators = append(authenticators,
956+
bearertoken.New(tokenAuth),
957+
websocket.NewProtocolAuthenticator(tokenAuth),
958+
paramtoken.New("access_token", tokenAuth, true),
959+
)
957960
}
958961

959962
if configapi.UseTLS(config.ServingInfo.ServingInfo) {

0 commit comments

Comments
 (0)