Skip to content

Commit 4aaca6a

Browse files
Merge pull request #15662 from liggitt/token-cache-3.6.1
Automatic merge from submit-queue 3.6.x: Add short TTL cache to token authentication pick of #14916
2 parents 387485d + 35fd03b commit 4aaca6a

File tree

15 files changed

+853
-31
lines changed

15 files changed

+853
-31
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func TestAPIServerDefaults(t *testing.T) {
126126
ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{},
127127
TokenFile: &kubeoptions.TokenFileAuthenticationOptions{},
128128
WebHook: &kubeoptions.WebHookAuthenticationOptions{CacheTTL: 2 * time.Minute},
129+
130+
TokenSuccessCacheTTL: 10 * time.Second,
131+
TokenFailureCacheTTL: 0,
129132
},
130133
Authorization: &kubeoptions.BuiltInAuthorizationOptions{
131134
Mode: "AlwaysAllow",

pkg/cmd/server/origin/master_config.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"k8s.io/apiserver/pkg/authentication/request/union"
3030
"k8s.io/apiserver/pkg/authentication/request/websocket"
3131
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
32+
tokencache "k8s.io/apiserver/pkg/authentication/token/cache"
33+
tokenunion "k8s.io/apiserver/pkg/authentication/token/union"
3234
"k8s.io/apiserver/pkg/authentication/user"
3335
kauthorizer "k8s.io/apiserver/pkg/authorization/authorizer"
3436
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
@@ -924,7 +926,7 @@ func newServiceAccountTokenGetter(options configapi.MasterConfig) (serviceaccoun
924926

925927
func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptions.Getter, tokenGetter serviceaccount.ServiceAccountTokenGetter, apiClientCAs *x509.CertPool, groupMapper identitymapper.UserToGroupMapper) (authenticator.Request, error) {
926928
authenticators := []authenticator.Request{}
927-
tokenAuthenticators := []authenticator.Request{}
929+
tokenAuthenticators := []authenticator.Token{}
928930

929931
// ServiceAccount token
930932
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
@@ -937,12 +939,7 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
937939
publicKeys = append(publicKeys, readPublicKeys...)
938940
}
939941
serviceAccountTokenAuthenticator := serviceaccount.JWTTokenAuthenticator(publicKeys, true, tokenGetter)
940-
tokenAuthenticators = append(
941-
tokenAuthenticators,
942-
bearertoken.New(serviceAccountTokenAuthenticator),
943-
websocket.NewProtocolAuthenticator(serviceAccountTokenAuthenticator),
944-
paramtoken.New("access_token", serviceAccountTokenAuthenticator, true),
945-
)
942+
tokenAuthenticators = append(tokenAuthenticators, serviceAccountTokenAuthenticator)
946943
}
947944

948945
// OAuth token
@@ -951,20 +948,26 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
951948
if err != nil {
952949
return nil, fmt.Errorf("Error building OAuth token authenticator: %v", err)
953950
}
954-
oauthTokenRequestAuthenticators := []authenticator.Request{
955-
bearertoken.New(oauthTokenAuthenticator),
956-
websocket.NewProtocolAuthenticator(oauthTokenAuthenticator),
957-
paramtoken.New("access_token", oauthTokenAuthenticator, true),
958-
}
959-
960951
tokenAuthenticators = append(tokenAuthenticators,
961952
// if you have a bearer token, you're a human (usually)
962953
// if you change this, have a look at the impersonationFilter where we attach groups to the impersonated user
963-
group.NewGroupAdder(union.New(oauthTokenRequestAuthenticators...), []string{bootstrappolicy.AuthenticatedOAuthGroup}))
954+
group.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
964955
}
965956

966957
if len(tokenAuthenticators) > 0 {
967-
authenticators = append(authenticators, union.New(tokenAuthenticators...))
958+
// Combine all token authenticators
959+
tokenAuth := tokenunion.New(tokenAuthenticators...)
960+
961+
// wrap with short cache on success.
962+
// this means a revoked service account token or access token will be valid for up to 10 seconds.
963+
// it also means group membership changes on users may take up to 10 seconds to become effective.
964+
tokenAuth = tokencache.New(tokenAuth, 10*time.Second, 0)
965+
966+
authenticators = append(authenticators,
967+
bearertoken.New(tokenAuth),
968+
websocket.NewProtocolAuthenticator(tokenAuth),
969+
paramtoken.New("access_token", tokenAuth, true),
970+
)
968971
}
969972

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

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

+20-14
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

+11-2
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

+48
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

+41
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

+54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)