Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.6.x: Add short TTL cache to token authentication #15662

Merged
merged 5 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/cmd/server/kubernetes/master/master_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func TestAPIServerDefaults(t *testing.T) {
ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{},
TokenFile: &kubeoptions.TokenFileAuthenticationOptions{},
WebHook: &kubeoptions.WebHookAuthenticationOptions{CacheTTL: 2 * time.Minute},

TokenSuccessCacheTTL: 10 * time.Second,
TokenFailureCacheTTL: 0,
},
Authorization: &kubeoptions.BuiltInAuthorizationOptions{
Mode: "AlwaysAllow",
Expand Down
33 changes: 18 additions & 15 deletions pkg/cmd/server/origin/master_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"k8s.io/apiserver/pkg/authentication/request/union"
"k8s.io/apiserver/pkg/authentication/request/websocket"
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
tokencache "k8s.io/apiserver/pkg/authentication/token/cache"
tokenunion "k8s.io/apiserver/pkg/authentication/token/union"
"k8s.io/apiserver/pkg/authentication/user"
kauthorizer "k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
Expand Down Expand Up @@ -924,7 +926,7 @@ func newServiceAccountTokenGetter(options configapi.MasterConfig) (serviceaccoun

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

// ServiceAccount token
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
Expand All @@ -937,12 +939,7 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
publicKeys = append(publicKeys, readPublicKeys...)
}
serviceAccountTokenAuthenticator := serviceaccount.JWTTokenAuthenticator(publicKeys, true, tokenGetter)
tokenAuthenticators = append(
tokenAuthenticators,
bearertoken.New(serviceAccountTokenAuthenticator),
websocket.NewProtocolAuthenticator(serviceAccountTokenAuthenticator),
paramtoken.New("access_token", serviceAccountTokenAuthenticator, true),
)
tokenAuthenticators = append(tokenAuthenticators, serviceAccountTokenAuthenticator)
}

// OAuth token
Expand All @@ -951,20 +948,26 @@ func newAuthenticator(config configapi.MasterConfig, restOptionsGetter restoptio
if err != nil {
return nil, fmt.Errorf("Error building OAuth token authenticator: %v", err)
}
oauthTokenRequestAuthenticators := []authenticator.Request{
bearertoken.New(oauthTokenAuthenticator),
websocket.NewProtocolAuthenticator(oauthTokenAuthenticator),
paramtoken.New("access_token", oauthTokenAuthenticator, true),
}

tokenAuthenticators = append(tokenAuthenticators,
// if you have a bearer token, you're a human (usually)
// if you change this, have a look at the impersonationFilter where we attach groups to the impersonated user
group.NewGroupAdder(union.New(oauthTokenRequestAuthenticators...), []string{bootstrappolicy.AuthenticatedOAuthGroup}))
group.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
}

if len(tokenAuthenticators) > 0 {
authenticators = append(authenticators, union.New(tokenAuthenticators...))
// Combine all token authenticators
tokenAuth := tokenunion.New(tokenAuthenticators...)

// wrap with short cache on success.
// this means a revoked service account token or access token will be valid for up to 10 seconds.
// it also means group membership changes on users may take up to 10 seconds to become effective.
tokenAuth = tokencache.New(tokenAuth, 10*time.Second, 0)

authenticators = append(authenticators,
bearertoken.New(tokenAuth),
websocket.NewProtocolAuthenticator(tokenAuth),
paramtoken.New("access_token", tokenAuth, true),
)
}

if configapi.UseTLS(config.ServingInfo.ServingInfo) {
Expand Down
34 changes: 20 additions & 14 deletions vendor/k8s.io/kubernetes/pkg/kubeapiserver/authenticator/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading