forked from openshift/origin
/
authenticator.go
177 lines (157 loc) · 7.94 KB
/
authenticator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package origin
import (
"crypto/x509"
"fmt"
"time"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/group"
"k8s.io/apiserver/pkg/authentication/request/anonymous"
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
"k8s.io/apiserver/pkg/authentication/request/headerrequest"
"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"
genericapiserver "k8s.io/apiserver/pkg/server"
webhooktoken "k8s.io/apiserver/plugin/pkg/authenticator/token/webhook"
kclientsetexternal "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/cert"
sacontroller "k8s.io/kubernetes/pkg/controller/serviceaccount"
"k8s.io/kubernetes/pkg/serviceaccount"
userclient "github.com/openshift/client-go/user/clientset/versioned"
usertypedclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
"github.com/openshift/origin/pkg/apiserver/authentication/internaloauth"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
oauthvalidation "github.com/openshift/origin/pkg/oauth/apis/oauth/validation"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
oauthclientlister "github.com/openshift/origin/pkg/oauth/generated/listers/oauth/internalversion"
"github.com/openshift/origin/pkg/oauthserver/authenticator/request/paramtoken"
usercache "github.com/openshift/origin/pkg/user/cache"
)
func NewAuthenticator(
options configapi.MasterConfig,
privilegedLoopbackConfig *rest.Config,
informers InformerAccess,
) (authenticator.Request, map[string]genericapiserver.PostStartHookFunc, error) {
kubeExternalClient, err := kclientsetexternal.NewForConfig(privilegedLoopbackConfig)
if err != nil {
return nil, nil, err
}
oauthClient, err := oauthclient.NewForConfig(privilegedLoopbackConfig)
if err != nil {
return nil, nil, err
}
userClient, err := userclient.NewForConfig(privilegedLoopbackConfig)
if err != nil {
return nil, nil, err
}
// this is safe because the server does a quorum read and we're hitting a "magic" authorizer to get permissions based on system:masters
// once the cache is added, we won't be paying a double hop cost to etcd on each request, so the simplification will help.
serviceAccountTokenGetter := sacontroller.NewGetterFromClient(kubeExternalClient)
apiClientCAs, err := configapi.GetAPIClientCertCAPool(options)
if err != nil {
return nil, nil, err
}
return newAuthenticator(
options,
oauthClient.OAuthAccessTokens(),
informers.GetOauthInformers().Oauth().InternalVersion().OAuthClients().Lister(),
serviceAccountTokenGetter,
userClient.User().Users(),
apiClientCAs,
usercache.NewGroupCache(informers.GetUserInformers().User().V1().Groups()),
)
}
func newAuthenticator(config configapi.MasterConfig, accessTokenGetter oauthclient.OAuthAccessTokenInterface, oauthClientLister oauthclientlister.OAuthClientLister, tokenGetter serviceaccount.ServiceAccountTokenGetter, userGetter usertypedclient.UserInterface, apiClientCAs *x509.CertPool, groupMapper internaloauth.UserToGroupMapper) (authenticator.Request, map[string]genericapiserver.PostStartHookFunc, error) {
postStartHooks := map[string]genericapiserver.PostStartHookFunc{}
authenticators := []authenticator.Request{}
tokenAuthenticators := []authenticator.Token{}
// ServiceAccount token
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
publicKeys := []interface{}{}
for _, keyFile := range config.ServiceAccountConfig.PublicKeyFiles {
readPublicKeys, err := cert.PublicKeysFromFile(keyFile)
if err != nil {
return nil, nil, fmt.Errorf("Error reading service account key file %s: %v", keyFile, err)
}
publicKeys = append(publicKeys, readPublicKeys...)
}
serviceAccountTokenAuthenticator := serviceaccount.JWTTokenAuthenticator(
serviceaccount.LegacyIssuer,
publicKeys,
serviceaccount.NewLegacyValidator(true, tokenGetter),
)
tokenAuthenticators = append(tokenAuthenticators, serviceAccountTokenAuthenticator)
}
// OAuth token
if config.OAuthConfig != nil {
validators := []internaloauth.OAuthTokenValidator{internaloauth.NewExpirationValidator(), internaloauth.NewUIDValidator()}
if inactivityTimeout := config.OAuthConfig.TokenConfig.AccessTokenInactivityTimeoutSeconds; inactivityTimeout != nil {
timeoutValidator := internaloauth.NewTimeoutValidator(accessTokenGetter, oauthClientLister, *inactivityTimeout, oauthvalidation.MinimumInactivityTimeoutSeconds)
validators = append(validators, timeoutValidator)
postStartHooks["openshift.io-TokenTimeoutUpdater"] = func(context genericapiserver.PostStartHookContext) error {
go timeoutValidator.Run(context.StopCh)
return nil
}
}
oauthTokenAuthenticator := internaloauth.NewTokenAuthenticator(accessTokenGetter, userGetter, groupMapper, validators...)
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.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
}
for _, wta := range config.AuthConfig.WebhookTokenAuthenticators {
ttl, err := time.ParseDuration(wta.CacheTTL)
if err != nil {
return nil, nil, fmt.Errorf("Error parsing CacheTTL=%q: %v", wta.CacheTTL, err)
}
webhookTokenAuthenticator, err := webhooktoken.New(wta.ConfigFile, ttl)
if err != nil {
return nil, nil, fmt.Errorf("Failed to create webhook token authenticator for ConfigFile=%q: %v", wta.ConfigFile, err)
}
tokenAuthenticators = append(tokenAuthenticators, webhookTokenAuthenticator)
}
if len(tokenAuthenticators) > 0 {
// 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),
)
}
// build cert authenticator
// TODO: add "system:" prefix in authenticator, limit cert to username
// TODO: add "system:" prefix to groups in authenticator, limit cert to group name
opts := x509request.DefaultVerifyOptions()
opts.Roots = apiClientCAs
certauth := x509request.New(opts, x509request.CommonNameUserConversion)
authenticators = append(authenticators, certauth)
resultingAuthenticator := union.NewFailOnError(authenticators...)
topLevelAuthenticators := []authenticator.Request{}
// if we have a front proxy providing authentication configuration, wire it up and it should come first
if config.AuthConfig.RequestHeader != nil {
requestHeaderAuthenticator, err := headerrequest.NewSecure(
config.AuthConfig.RequestHeader.ClientCA,
config.AuthConfig.RequestHeader.ClientCommonNames,
config.AuthConfig.RequestHeader.UsernameHeaders,
config.AuthConfig.RequestHeader.GroupHeaders,
config.AuthConfig.RequestHeader.ExtraHeaderPrefixes,
)
if err != nil {
return nil, nil, fmt.Errorf("Error building front proxy auth config: %v", err)
}
topLevelAuthenticators = append(topLevelAuthenticators, union.New(requestHeaderAuthenticator, resultingAuthenticator))
} else {
topLevelAuthenticators = append(topLevelAuthenticators, resultingAuthenticator)
}
topLevelAuthenticators = append(topLevelAuthenticators, anonymous.NewAuthenticator())
return group.NewAuthenticatedGroupAdder(union.NewFailOnError(topLevelAuthenticators...)), postStartHooks, nil
}