forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
504 lines (422 loc) · 21 KB
/
app.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package app
import (
"context"
"crypto/x509"
"log"
"time"
"bytes"
"encoding/pem"
"github.com/pkg/errors"
managementController "github.com/rancher/cluster-controller/controller"
"github.com/rancher/rancher/server"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/cert"
)
type Config struct {
HTTPOnly bool
ACMEDomains []string
KubeConfig string
HTTPListenPort int
HTTPSListenPort int
InteralListenPort int
K8sMode string
AddLocal bool
Debug bool
ListenConfig *v3.ListenConfig
}
var defaultAdminLabel = map[string]string{"authz.management.cattle.io/bootstrapping": "admin-user"}
func Run(ctx context.Context, kubeConfig rest.Config, cfg *Config) error {
management, err := config.NewManagementContext(kubeConfig)
if err != nil {
return err
}
management.LocalConfig = &kubeConfig
if err := ReadTLSConfig(cfg); err != nil {
return err
}
for {
_, err := management.K8sClient.Discovery().ServerVersion()
if err == nil {
break
}
logrus.Infof("Waiting for server to become available: %v", err)
time.Sleep(2 * time.Second)
}
if err := server.New(ctx, cfg.HTTPListenPort, cfg.HTTPSListenPort, management); err != nil {
return err
}
managementController.Register(ctx, management)
if err := management.Start(ctx); err != nil {
return err
}
if err := addData(management, *cfg); err != nil {
return err
}
<-ctx.Done()
if ctx.Err() != nil {
log.Fatal(ctx.Err())
}
return ctx.Err()
}
func addData(management *config.ManagementContext, cfg Config) error {
if err := addListenConfig(management, cfg); err != nil {
return err
}
if err := addRoles(management, cfg.AddLocal); err != nil {
return err
}
return addMachineDrivers(management)
}
func addListenConfig(management *config.ManagementContext, cfg Config) error {
existing, err := management.Management.ListenConfigs("").Get(cfg.ListenConfig.Name, v1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if apierrors.IsNotFound(err) {
existing = nil
}
if existing != nil {
if cfg.ListenConfig.CACerts == "" {
cfg.ListenConfig.CACerts = existing.CACerts
}
if cfg.ListenConfig.Key == "" {
cfg.ListenConfig.Key = existing.Key
}
if cfg.ListenConfig.Cert == "" {
cfg.ListenConfig.Cert = existing.Cert
}
if cfg.ListenConfig.CAKey == "" {
cfg.ListenConfig.CAKey = existing.CAKey
}
if cfg.ListenConfig.CACert == "" {
cfg.ListenConfig.CACert = existing.CACert
}
if len(cfg.ListenConfig.KnownIPs) == 0 {
cfg.ListenConfig.KnownIPs = existing.KnownIPs
}
}
if (cfg.ListenConfig.Key == "" || cfg.ListenConfig.Cert == "") && cfg.ListenConfig.CACert == "" && cfg.ListenConfig.Mode != "acme" {
caKey, err := cert.NewPrivateKey()
if err != nil {
return err
}
caCert, err := cert.NewSelfSignedCACert(cert.Config{
CommonName: "cattle-ca",
Organization: []string{"the-rancher"},
}, caKey)
if err != nil {
return err
}
caCertBuffer := bytes.Buffer{}
if err := pem.Encode(&caCertBuffer, &pem.Block{
Type: cert.CertificateBlockType,
Bytes: caCert.Raw,
}); err != nil {
return err
}
caKeyBuffer := bytes.Buffer{}
if err := pem.Encode(&caKeyBuffer, &pem.Block{
Type: cert.RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(caKey),
}); err != nil {
return err
}
cfg.ListenConfig.CACert = string(caCertBuffer.Bytes())
cfg.ListenConfig.CACerts = cfg.ListenConfig.CACert
cfg.ListenConfig.CAKey = string(caKeyBuffer.Bytes())
}
if cfg.ListenConfig.Mode == "acme" {
cfg.ListenConfig.CACerts = ""
}
if existing == nil {
_, err := management.Management.ListenConfigs("").Create(cfg.ListenConfig)
return err
}
cfg.ListenConfig.ResourceVersion = existing.ResourceVersion
_, err = management.Management.ListenConfigs("").Update(cfg.ListenConfig)
return err
}
func addRoles(management *config.ManagementContext, local bool) error {
rb := newRoleBuilder()
rb.addRole("Create Clusters", "create-clusters").addRule().apiGroups("management.cattle.io").resources("clusters").verbs("create")
rb.addRole("Manage All Clusters", "manage-clusters").addRule().apiGroups("management.cattle.io").resources("clusters").verbs("*")
rb.addRole("Manage Node Drivers", "manage-node-drivers").addRule().apiGroups("management.cattle.io").resources("machinedrivers").verbs("*")
rb.addRole("Manage Catalogs", "manage-catalogs").addRule().apiGroups("management.cattle.io").resources("catalogs", "templates", "templateversions").verbs("*")
rb.addRole("Use Catalog Templates", "use-catalogs").addRule().apiGroups("management.cattle.io").resources("templates", "templateversions").verbs("get", "list", "watch")
rb.addRole("Manage Users", "manage-users").addRule().apiGroups("management.cattle.io").resources("users", "globalroles", "globalrolebindings").verbs("*")
rb.addRole("Manage Roles", "manage-roles").addRule().apiGroups("management.cattle.io").resources("roletemplates").verbs("*")
rb.addRole("Manage Authentication", "manage-authn").addRule().apiGroups("management.cattle.io").resources("authconfigs").verbs("get", "list", "watch", "update")
rb.addRole("Manage Node Templates", "manage-node-templates").addRule().apiGroups("management.cattle.io").resources("machinetemplates").verbs("*")
rb.addRole("Use Node Templates", "use-node-templates").addRule().apiGroups("management.cattle.io").resources("machinedrivers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("machinetemplates").verbs("*")
rb.addRole("Manage Settings", "manage-settings").addRule().apiGroups("management.cattle.io").resources("settings").verbs("*")
rb.addRole("Admin", "admin").addRule().apiGroups("*").resources("*").verbs("*").
addRule().apiGroups().nonResourceURLs("*").verbs("*")
rb.addRole("User", "user").addRule().apiGroups("management.cattle.io").resources("principals", "roletemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("users").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("preferences").verbs("*").
addRule().apiGroups("management.cattle.io").resources("clusters").verbs("create").
addRule().apiGroups("management.cattle.io").resources("templates", "templateversions").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("machinedrivers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("machinetemplates").verbs("*").
addRule().apiGroups("management.cattle.io").resources("settings").verbs("get", "list", "watch")
rb.addRole("User Base", "user-base").addRule().apiGroups("management.cattle.io").resources("principals", "roletemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("users").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("preferences").verbs("*").
addRule().apiGroups("management.cattle.io").resources("settings").verbs("get", "list", "watch")
// TODO user should be dynamically authorized to only see herself
// TODO Need "self-service" for machinetemplates such that a user can create them, but only RUD their own
// TODO enable when groups are "in". they need to be self-service
if err := rb.reconcileGlobalRoles(management); err != nil {
return errors.Wrap(err, "problem reconciling globl roles")
}
// RoleTemplates to be used inside of clusters
rb = newRoleBuilder()
// K8s default roles
rb.addRoleTemplate("Kubernetes cluster-admin", "cluster-admin", "cluster", true, true, true)
rb.addRoleTemplate("Kubernetes admin", "admin", "project", true, true, true)
rb.addRoleTemplate("Kubernetes edit", "edit", "project", true, true, true)
rb.addRoleTemplate("Kubernetes view", "view", "project", true, true, true)
// Cluster roles
rb.addRoleTemplate("Cluster Owner", "cluster-owner", "cluster", true, false, false).
addRule().apiGroups("*").resources("*").verbs("*").
addRule().apiGroups().nonResourceURLs("*").verbs("*")
rb.addRoleTemplate("Cluster Member", "cluster-member", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projects").verbs("create").
addRule().apiGroups("management.cattle.io").resources("machines").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("nodes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Projects", "manage-projects", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projects").verbs("*")
rb.addRoleTemplate("Create Projects", "create-projects", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projects").verbs("create")
rb.addRoleTemplate("View Projects", "view-projects", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projects").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Nodes", "manage-nodes", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("machines").verbs("*").
addRule().apiGroups("*").resources("nodes").verbs("*")
rb.addRoleTemplate("View Nodes", "view-nodes", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("machines").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("nodes").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Volumes", "manage-volumes", "cluster", true, false, false).
addRule().apiGroups("*").resources("persistentvolumes").verbs("*")
rb.addRoleTemplate("View Volumes", "view-volumes", "cluster", true, false, false).
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Cluster Members", "manage-cluster-members", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("*")
rb.addRoleTemplate("View Cluster Members", "view-cluster-members", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("get", "list", "watch")
// Project roles
rb.addRoleTemplate("Project Owner", "project-owner", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("*").
addRule().apiGroups("project.cattle.io").resources("worklods").verbs("*").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
addRule().apiGroups("").resources("namespaces").verbs("create").
setRoleTemplateNames("admin")
rb.addRoleTemplate("Project Member", "project-member", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("worklods").verbs("*").
addRule().apiGroups("").resources("namespaces").verbs("create").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
setRoleTemplateNames("edit")
rb.addRoleTemplate("Read-only", "read-only", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("worklods").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
setRoleTemplateNames("view")
rb.addRoleTemplate("Create Namespaces", "create-ns", "project", true, false, true).
addRule().apiGroups("").resources("namespaces").verbs("create")
rb.addRoleTemplate("Manage Workloads", "manage-workloads", "project", true, false, true).
addRule().apiGroups("*").resources("pods", "pods/attach", "pods/exec", "pods/portforward", "pods/proxy", "replicationcontrollers",
"replicationcontrollers/scale", "daemonsets", "deployments", "deployments/rollback", "deployments/scale", "replicasets",
"replicasets/scale", "statefulsets", "cronjobs", "jobs", "daemonsets", "deployments", "deployments/rollback", "deployments/scale",
"replicasets", "replicasets/scale", "replicationcontrollers/scale", "horizontalpodautoscalers").verbs("*").
addRule().apiGroups("*").resources("limitranges", "pods/log", "pods/status", "replicationcontrollers/status", "resourcequotas", "resourcequotas/status", "bindings").verbs("get", "list", "watch")
rb.addRoleTemplate("View Workloads", "view-workloads", "project", true, false, true).
addRule().apiGroups("*").resources("pods", "pods/attach", "pods/exec", "pods/portforward", "pods/proxy", "replicationcontrollers",
"replicationcontrollers/scale", "daemonsets", "deployments", "deployments/rollback", "deployments/scale", "replicasets",
"replicasets/scale", "statefulsets", "cronjobs", "jobs", "daemonsets", "deployments", "deployments/rollback", "deployments/scale",
"replicasets", "replicasets/scale", "replicationcontrollers/scale", "horizontalpodautoscalers").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("limitranges", "pods/log", "pods/status", "replicationcontrollers/status", "resourcequotas", "resourcequotas/status", "bindings").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Ingress", "manage-ingress", "project", true, false, true).
addRule().apiGroups("*").resources("ingresses").verbs("*")
rb.addRoleTemplate("View Ingress", "view-ingress", "project", true, false, true).
addRule().apiGroups("*").resources("ingresses").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Service", "manage-service", "project", true, false, true).
addRule().apiGroups("*").resources("services", "endpoints").verbs("*")
rb.addRoleTemplate("View Services", "view-services", "project", true, false, true).
addRule().apiGroups("*").resources("services", "endpoints").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Secrets", "manage-secrets", "project", true, false, true).
addRule().apiGroups("*").resources("secrets").verbs("*")
rb.addRoleTemplate("View Secrets", "view-secrets", "project", true, false, true).
addRule().apiGroups("*").resources("secrets").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Config Maps", "manage-configmaps", "project", true, false, true).
addRule().apiGroups("*").resources("configmaps").verbs("*")
rb.addRoleTemplate("View Config Maps", "view-configmaps", "project", true, false, true).
addRule().apiGroups("*").resources("configmaps").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Volumes", "manage-persistentvolumeclaims", "project", true, false, true).
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("*")
rb.addRoleTemplate("View Volumes", "view-persistentvolumeclaims", "project", true, false, true).
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Service Accounts", "manage-serviceaccounts", "project", true, false, true).
addRule().apiGroups("*").resources("serviceaccounts").verbs("*")
rb.addRoleTemplate("View Service Accounts", "view-serviceaccounts", "project", true, false, true).
addRule().apiGroups("*").resources("serviceaccounts").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Project Members", "manage-project-members", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("*")
rb.addRoleTemplate("View Project Members", "view-project-members", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch")
// Not specific to project or cluster
// TODO When clusterevents is replaced with events, remove clusterevents
rb.addRoleTemplate("View Events", "view-events", "", true, false, false).
addRule().apiGroups("*").resources("events").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch")
if err := rb.reconcileRoleTemplates(management); err != nil {
return errors.Wrap(err, "problem reconciling role templates")
}
hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
set := labels.Set(defaultAdminLabel)
admins, err := management.Management.Users("").List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return err
}
// TODO This logic is going to be a problem in an HA setup because a race will cause more than one admin user to be created
var admin *v3.User
if len(admins.Items) == 0 {
admin, err = management.Management.Users("").Create(&v3.User{
ObjectMeta: v1.ObjectMeta{
GenerateName: "user-",
Labels: defaultAdminLabel,
},
DisplayName: "Default Admin",
Username: "admin",
Password: string(hash),
MustChangePassword: true,
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrap(err, "can not ensure admin user exists")
}
} else {
admin = &admins.Items[0]
}
if len(admin.PrincipalIDs) == 0 {
admin.PrincipalIDs = []string{"local://" + admin.Name}
management.Management.Users("").Update(admin)
}
bindings, err := management.Management.GlobalRoleBindings("").List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return err
}
if len(bindings.Items) == 0 {
management.Management.GlobalRoleBindings("").Create(
&v3.GlobalRoleBinding{
ObjectMeta: v1.ObjectMeta{
GenerateName: "globalrolebinding-",
Labels: defaultAdminLabel,
},
Subject: rbacv1.Subject{
Kind: "User",
Name: admin.Name,
},
GlobalRoleName: "admin",
})
}
if local {
// TODO If user delets the local cluster, this will recreate it on restart. Need to fix that
management.Management.Clusters("").Create(&v3.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "local",
Annotations: map[string]string{
"field.cattle.io/creatorId": admin.Name,
},
},
Spec: v3.ClusterSpec{
DisplayName: "local",
Internal: true,
},
})
}
return nil
}
func addMachineDrivers(management *config.ManagementContext) error {
if err := addMachineDriver("amazonec2", "local://", "", true, true, management); err != nil {
return err
}
if err := addMachineDriver("azure", "local://", "", true, true, management); err != nil {
return err
}
if err := addMachineDriver("digitalocean", "local://", "", true, true, management); err != nil {
return err
}
if err := addMachineDriver("exoscale", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("generic", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("google", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("openstack", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("otc", "https://obs.otc.t-systems.com/dockermachinedriver/docker-machine-driver-otc",
"e98f246f625ca46f5e037dc29bdf00fe", false, false, management); err != nil {
return err
}
if err := addMachineDriver("packet", "https://github.com/packethost/docker-machine-driver-packet/releases/download/v0.1.2/docker-machine-driver-packet_linux-amd64.zip",
"cd610cd7d962dfdf88a811ec026bcdcf", true, false, management); err != nil {
return err
}
if err := addMachineDriver("rackspace", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("softlayer", "local://", "", false, true, management); err != nil {
return err
}
if err := addMachineDriver("vmwarevcloudair", "local://", "", false, true, management); err != nil {
return err
}
return addMachineDriver("vmwarevsphere", "local://", "", true, true, management)
}
func addMachineDriver(name, url, checksum string, active, builtin bool, management *config.ManagementContext) error {
lister := management.Management.MachineDrivers("").Controller().Lister()
cli := management.Management.MachineDrivers("")
m, _ := lister.Get("", name)
if m != nil {
if m.Spec.Builtin != builtin || m.Spec.URL != url || m.Spec.Checksum != checksum || m.Spec.DisplayName != name {
logrus.Infof("Updating machine driver %v", name)
m.Spec.Builtin = builtin
m.Spec.URL = url
m.Spec.Checksum = checksum
m.Spec.DisplayName = name
_, err := cli.Update(m)
return err
}
return nil
}
logrus.Infof("Creating machine driver %v", name)
_, err := cli.Create(&v3.MachineDriver{
ObjectMeta: v1.ObjectMeta{
Name: name,
},
Spec: v3.MachineDriverSpec{
Active: active,
Builtin: builtin,
URL: url,
DisplayName: name,
Checksum: checksum,
},
})
return err
}