-
Notifications
You must be signed in to change notification settings - Fork 367
/
policy.go
110 lines (99 loc) · 5.34 KB
/
policy.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
/*
Copyright 2022 The KCP 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 bootstrap
import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rbacv1helpers "k8s.io/kubernetes/pkg/apis/rbac/v1"
rbacrest "k8s.io/kubernetes/pkg/registry/rbac/rest"
"k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy"
"github.com/kcp-dev/kcp/sdk/apis/core"
"github.com/kcp-dev/kcp/sdk/apis/tenancy"
)
const (
// SystemKcpAdminGroup is global admin group. Members of this group have all permissions across all workspaces.
SystemKcpAdminGroup = "system:kcp:admin"
// SystemKcpWorkspaceBootstrapper is the group used to bootstrap resources, both during the root setup, as well
// as when the default APIBinding initializing controller performs its bootstrapping for initializing workspaces.
// We need a separate group (not the privileged system group) for this because system-owned workspaces (e.g. root:users) need
// a workspace owner annotation set, and the owner annotation is skipped/not set for the privileged system group.
SystemKcpWorkspaceBootstrapper = "system:kcp:tenancy:workspace-bootstrapper"
// SystemLogicalClusterAdmin is a group used by the workspace scheduler to create LogicalCluster resources.
// This group allows it to skip the entire authorization stack except the bootstrap policy authorizer.
// Otherwise, the requests would be rejected because the LogicalCluster resource does not exist yet.
SystemLogicalClusterAdmin = "system:kcp:logical-cluster-admin"
// SystemExternalLogicalClusterAdmin is a group used by the workspace controllers to manage LogicalCluster
// resources after creation, using a subset of permissions allowed for the internal logical-cluster-admin.
SystemExternalLogicalClusterAdmin = "system:kcp:external-logical-cluster-admin"
// SystemKcpWorkspaceAccessGroup is a group that gives a user system:authenticated access to a workspace.
SystemKcpWorkspaceAccessGroup = "system:kcp:workspace:access"
)
// ClusterRoleBindings return default rolebindings to the default roles.
func clusterRoleBindings() []rbacv1.ClusterRoleBinding {
return []rbacv1.ClusterRoleBinding{
clusterRoleBindingCustomName(rbacv1helpers.NewClusterBinding("cluster-admin").Groups(SystemKcpAdminGroup).BindingOrDie(), "system:kcp:admin:cluster-admin"),
clusterRoleBindingCustomName(rbacv1helpers.NewClusterBinding(SystemKcpWorkspaceBootstrapper).Groups(SystemKcpWorkspaceBootstrapper, "apis.kcp.io:binding:"+SystemKcpWorkspaceBootstrapper).BindingOrDie(), SystemKcpWorkspaceBootstrapper),
clusterRoleBindingCustomName(rbacv1helpers.NewClusterBinding(SystemLogicalClusterAdmin).Groups(SystemLogicalClusterAdmin).BindingOrDie(), SystemLogicalClusterAdmin),
clusterRoleBindingCustomName(rbacv1helpers.NewClusterBinding(SystemExternalLogicalClusterAdmin).Groups(SystemExternalLogicalClusterAdmin).BindingOrDie(), SystemExternalLogicalClusterAdmin),
}
}
func clusterRoles() []rbacv1.ClusterRole {
return []rbacv1.ClusterRole{
{
ObjectMeta: metav1.ObjectMeta{Name: "system:kcp:tenancy:reader"},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("get", "list", "watch").Groups(tenancy.GroupName).Resources("workspaces").RuleOrDie(),
rbacv1helpers.NewRule(bootstrappolicy.Read...).Groups(tenancy.GroupName).Resources("workspacetypes").RuleOrDie(),
rbacv1helpers.NewRule("get", "list", "watch").Groups(core.GroupName).Resources("logicalclusters").RuleOrDie(),
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: SystemKcpWorkspaceBootstrapper},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("*").Groups("*").Resources("*").RuleOrDie(),
rbacv1helpers.NewRule("*").URLs("*").RuleOrDie(),
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: SystemLogicalClusterAdmin},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("*").Groups(core.GroupName).Resources("logicalclusters", "logicalclusters/status").RuleOrDie(),
rbacv1helpers.NewRule("delete", "update", "get").Groups(tenancy.GroupName).Resources("workspaces").RuleOrDie(),
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: SystemExternalLogicalClusterAdmin},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("delete", "update", "get").Groups(core.GroupName).Resources("logicalclusters", "logicalclusters/status").RuleOrDie(),
rbacv1helpers.NewRule("delete", "update", "get").Groups(tenancy.GroupName).Resources("workspaces").RuleOrDie(),
rbacv1helpers.NewRule("access").URLs("/").RuleOrDie(),
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: SystemKcpWorkspaceAccessGroup},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("access").URLs("/").RuleOrDie(),
},
},
}
}
func clusterRoleBindingCustomName(b rbacv1.ClusterRoleBinding, name string) rbacv1.ClusterRoleBinding {
b.ObjectMeta.Name = name
return b
}
func Policy() *rbacrest.PolicyData {
return &rbacrest.PolicyData{
ClusterRoles: clusterRoles(),
ClusterRoleBindings: clusterRoleBindings(),
}
}