-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
manager.go
432 lines (363 loc) · 13.6 KB
/
manager.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"crypto/rand"
"errors"
"log"
"strings"
"github.com/emicklei/go-restful"
"k8s.io/api/authorization/v1"
errorsK8s "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/kubernetes/dashboard/src/app/backend/args"
authApi "github.com/kubernetes/dashboard/src/app/backend/auth/api"
clientapi "github.com/kubernetes/dashboard/src/app/backend/client/api"
kdErrors "github.com/kubernetes/dashboard/src/app/backend/errors"
)
// Dashboard UI default values for client configs.
const (
// High enough QPS to fit all expected use cases. QPS=0 is not set here, because
// client code is overriding it.
DefaultQPS = 1e6
// High enough Burst to fit all expected use cases. Burst=0 is not set here, because
// client code is overriding it.
DefaultBurst = 1e6
// Use kubernetes protobuf as content type by default
DefaultContentType = "application/vnd.kubernetes.protobuf"
// Default cluster/context/auth name to be set in clientcmd config
DefaultCmdConfigName = "kubernetes"
// Header name that contains token used for authorization. See TokenManager for more information.
JWETokenHeader = "jweToken"
// Default http header for user-agent
DefaultUserAgent = "dashboard"
)
// VERSION of this binary
var Version = "UNKNOWN"
// clientManager implements ClientManager interface
type clientManager struct {
// Autogenerated key on backend start used to secure requests from csrf attacks
csrfKey string
// Path to kubeconfig file. If both kubeConfigPath and apiserverHost are empty
// inClusterConfig will be used
kubeConfigPath string
// Address of apiserver host in format 'protocol://address:port'
apiserverHost string
// Initialized on clientManager creation and used if kubeconfigPath and apiserverHost are
// empty
inClusterConfig *rest.Config
// Responsible for decrypting tokens coming in request header. Used for authentication.
tokenManager authApi.TokenManager
// Kubernetes client created without providing auth info. It uses permissions granted to
// service account used by dashboard or kubeconfig file if it was passed during dashboard init.
insecureClient kubernetes.Interface
// Kubernetes client config created without providing auth info. It uses permissions granted
// to service account used by dashboard or kubeconfig file if it was passed during dashboard
// init.
insecureConfig *rest.Config
}
// Client returns a kubernetes client. In case dashboard login is enabled and option to skip
// login page is disabled only secure client will be returned, otherwise insecure client will be
// used.
func (self *clientManager) Client(req *restful.Request) (kubernetes.Interface, error) {
if req == nil {
return nil, errors.New("Request can not be nil!")
}
if self.isSecureModeEnabled(req) {
return self.secureClient(req)
}
return self.InsecureClient(), nil
}
// Config returns a rest config. In case dashboard login is enabled and option to skip
// login page is disabled only secure config will be returned, otherwise insecure config will be
// used.
func (self *clientManager) Config(req *restful.Request) (*rest.Config, error) {
if req == nil {
return nil, errors.New("Request can not be nil!")
}
if self.isSecureModeEnabled(req) {
return self.secureConfig(req)
}
return self.InsecureConfig(), nil
}
// InsecureClient returns kubernetes client that was created without providing auth info. It uses
// permissions granted to service account used by dashboard or kubeconfig file if it was passed
// during dashboard init.
func (self *clientManager) InsecureClient() kubernetes.Interface {
return self.insecureClient
}
// InsecureConfig returns kubernetes client config that used privileges of dashboard service account
// or kubeconfig file if it was passed during dashboard init.
func (self *clientManager) InsecureConfig() *rest.Config {
return self.insecureConfig
}
// CanI returns true when user is allowed to access data provided within SelfSubjectAccessReview, false otherwise.
func (self *clientManager) CanI(req *restful.Request, ssar *v1.SelfSubjectAccessReview) bool {
// In case user is not authenticated (uses skip option) do not allow access.
if info, _ := self.extractAuthInfo(req); info == nil {
return false
}
client, err := self.Client(req)
if err != nil {
log.Println(err)
return false
}
response, err := client.AuthorizationV1().SelfSubjectAccessReviews().Create(ssar)
if err != nil {
log.Println(err)
return false
}
return response.Status.Allowed
}
// ClientCmdConfig creates ClientCmd Config based on authentication information extracted from request.
// Currently request header is only checked for existence of 'Authentication: BearerToken'
func (self *clientManager) ClientCmdConfig(req *restful.Request) (clientcmd.ClientConfig, error) {
authInfo, err := self.extractAuthInfo(req)
if err != nil {
return nil, err
}
cfg, err := self.buildConfigFromFlags(self.apiserverHost, self.kubeConfigPath)
if err != nil {
return nil, err
}
return self.buildCmdConfig(authInfo, cfg), nil
}
// CSRFKey returns key that is generated upon client manager creation
func (self *clientManager) CSRFKey() string {
return self.csrfKey
}
// HasAccess configures K8S api client with provided auth info and executes a basic check against apiserver to see
// if it is valid.
func (self *clientManager) HasAccess(authInfo api.AuthInfo) error {
cfg, err := self.buildConfigFromFlags(self.apiserverHost, self.kubeConfigPath)
if err != nil {
return err
}
clientConfig := self.buildCmdConfig(&authInfo, cfg)
cfg, err = clientConfig.ClientConfig()
if err != nil {
return err
}
client, err := kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
_, err = client.ServerVersion()
return err
}
// VerberClient returns new verber client based on authentication information extracted from request
func (self *clientManager) VerberClient(req *restful.Request) (clientapi.ResourceVerber, error) {
client, err := self.Client(req)
if err != nil {
return nil, err
}
return NewResourceVerber(client.CoreV1().RESTClient(),
client.ExtensionsV1beta1().RESTClient(), client.AppsV1beta2().RESTClient(),
client.BatchV1().RESTClient(), client.BatchV1beta1().RESTClient(), client.AutoscalingV1().RESTClient(),
client.StorageV1().RESTClient()), nil
}
// SetTokenManager sets the token manager that will be used for token decryption.
func (self *clientManager) SetTokenManager(manager authApi.TokenManager) {
self.tokenManager = manager
}
// Initializes config with default values
func (self *clientManager) initConfig(cfg *rest.Config) {
cfg.QPS = DefaultQPS
cfg.Burst = DefaultBurst
cfg.ContentType = DefaultContentType
cfg.UserAgent = DefaultUserAgent + "/" + Version
}
// Returns rest Config based on provided apiserverHost and kubeConfigPath flags. If both are
// empty then in-cluster config will be used and if it is nil the error is returned.
func (self *clientManager) buildConfigFromFlags(apiserverHost, kubeConfigPath string) (
*rest.Config, error) {
if len(kubeConfigPath) > 0 || len(apiserverHost) > 0 {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfigPath},
&clientcmd.ConfigOverrides{ClusterInfo: api.Cluster{Server: apiserverHost}}).ClientConfig()
}
if self.isRunningInCluster() {
return self.inClusterConfig, nil
}
return nil, errors.New("Could not create client config. Check logs for more information")
}
// Based on rest config creates auth info structure.
func (self *clientManager) buildAuthInfoFromConfig(cfg *rest.Config) api.AuthInfo {
return api.AuthInfo{
Token: cfg.BearerToken,
ClientCertificate: cfg.CertFile,
ClientKey: cfg.KeyFile,
ClientCertificateData: cfg.CertData,
ClientKeyData: cfg.KeyData,
Username: cfg.Username,
Password: cfg.Password,
}
}
// Based on auth info and rest config creates client cmd config.
func (self *clientManager) buildCmdConfig(authInfo *api.AuthInfo, cfg *rest.Config) clientcmd.ClientConfig {
cmdCfg := api.NewConfig()
cmdCfg.Clusters[DefaultCmdConfigName] = &api.Cluster{
Server: cfg.Host,
CertificateAuthority: cfg.TLSClientConfig.CAFile,
CertificateAuthorityData: cfg.TLSClientConfig.CAData,
InsecureSkipTLSVerify: cfg.TLSClientConfig.Insecure,
}
cmdCfg.AuthInfos[DefaultCmdConfigName] = authInfo
cmdCfg.Contexts[DefaultCmdConfigName] = &api.Context{
Cluster: DefaultCmdConfigName,
AuthInfo: DefaultCmdConfigName,
}
cmdCfg.CurrentContext = DefaultCmdConfigName
return clientcmd.NewDefaultClientConfig(
*cmdCfg,
&clientcmd.ConfigOverrides{},
)
}
// Extracts authorization information from the request header
func (self *clientManager) extractAuthInfo(req *restful.Request) (*api.AuthInfo, error) {
authHeader := req.HeaderParameter("Authorization")
jweToken := req.HeaderParameter(JWETokenHeader)
// Authorization header will be more important than our token
token := self.extractTokenFromHeader(authHeader)
if len(token) > 0 {
return &api.AuthInfo{Token: token}, nil
}
if self.tokenManager != nil && len(jweToken) > 0 {
return self.tokenManager.Decrypt(jweToken)
}
return nil, errorsK8s.NewUnauthorized(kdErrors.MSG_LOGIN_UNAUTHORIZED_ERROR)
}
func (self *clientManager) extractTokenFromHeader(authHeader string) string {
if strings.HasPrefix(authHeader, "Bearer ") {
return strings.TrimPrefix(authHeader, "Bearer ")
}
return ""
}
func (self *clientManager) isLoginEnabled(req *restful.Request) bool {
return req.Request.TLS != nil || args.Holder.GetEnableInsecureLogin()
}
// Secure mode means that every request to Dashboard has to be authenticated and privileges
// of Dashboard SA can not be used.
func (self *clientManager) isSecureModeEnabled(req *restful.Request) bool {
if self.isLoginEnabled(req) && !args.Holder.GetEnableSkipLogin() {
return true
}
authInfo, _ := self.extractAuthInfo(req)
return self.isLoginEnabled(req) && args.Holder.GetEnableSkipLogin() && authInfo != nil
}
func (self *clientManager) secureClient(req *restful.Request) (kubernetes.Interface, error) {
cfg, err := self.secureConfig(req)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
return client, nil
}
func (self *clientManager) secureConfig(req *restful.Request) (*rest.Config, error) {
cmdConfig, err := self.ClientCmdConfig(req)
if err != nil {
return nil, err
}
cfg, err := cmdConfig.ClientConfig()
if err != nil {
return nil, err
}
self.initConfig(cfg)
return cfg, nil
}
// Initializes client manager
func (self *clientManager) init() {
self.initInClusterConfig()
self.initCSRFKey()
self.initInsecureClient()
}
// Initializes in-cluster config if apiserverHost and kubeConfigPath were not provided.
func (self *clientManager) initInClusterConfig() {
if len(self.apiserverHost) > 0 || len(self.kubeConfigPath) > 0 {
log.Print("Skipping in-cluster config")
return
}
log.Print("Using in-cluster config to connect to apiserver")
cfg, err := rest.InClusterConfig()
if err != nil {
log.Printf("Could not init in cluster config: %s", err.Error())
return
}
self.inClusterConfig = cfg
}
// Initializes csrfKey. If in-cluster config is detected then csrf key is initialized with
// service account token, otherwise it is generated
func (self *clientManager) initCSRFKey() {
if self.inClusterConfig == nil {
// Most likely running for a dev, so no replica issues, just generate a random key
log.Println("Using random key for csrf signing")
self.generateCSRFKey()
return
}
// We run in a cluster, so we should use a signing key that is the same for potential replications
log.Println("Using service account token for csrf signing")
self.csrfKey = self.inClusterConfig.BearerToken
}
func (self *clientManager) initInsecureClient() {
self.initInsecureConfig()
client, err := kubernetes.NewForConfig(self.insecureConfig)
if err != nil {
panic(err)
}
self.insecureClient = client
}
func (self *clientManager) initInsecureConfig() {
cfg, err := self.buildConfigFromFlags(self.apiserverHost, self.kubeConfigPath)
if err != nil {
panic(err)
}
defaultAuthInfo := self.buildAuthInfoFromConfig(cfg)
authInfo := &defaultAuthInfo
cmdConfig := self.buildCmdConfig(authInfo, cfg)
cfg, err = cmdConfig.ClientConfig()
if err != nil {
panic(err)
}
self.initConfig(cfg)
self.insecureConfig = cfg
}
// Generates random csrf key
func (self *clientManager) generateCSRFKey() {
bytes := make([]byte, 256)
_, err := rand.Read(bytes)
if err != nil {
panic("Fatal error. Could not generate csrf key")
}
self.csrfKey = string(bytes)
}
// Returns true if in-cluster config is used
func (self *clientManager) isRunningInCluster() bool {
return self.inClusterConfig != nil
}
// NewClientManager creates client manager based on kubeConfigPath and apiserverHost parameters.
// If both are empty then in-cluster config is used.
func NewClientManager(kubeConfigPath, apiserverHost string) clientapi.ClientManager {
result := &clientManager{
kubeConfigPath: kubeConfigPath,
apiserverHost: apiserverHost,
}
result.init()
return result
}