forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles.go
182 lines (168 loc) · 5.79 KB
/
roles.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
package kube
import (
"fmt"
"sort"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// GetTeamRoles returns the roles for the given team dev namespace
func GetTeamRoles(kubeClient kubernetes.Interface, ns string) (map[string]*rbacv1.Role, []string, error) {
m := map[string]*rbacv1.Role{}
names := []string{}
resources, err := kubeClient.RbacV1().Roles(ns).List(metav1.ListOptions{
LabelSelector: LabelKind + "=" + ValueKindEnvironmentRole,
})
if err != nil {
return m, names, err
}
for _, env := range resources.Items {
n := env.Name
copy := env
m[n] = ©
if n != "" {
names = append(names, n)
}
}
sort.Strings(names)
return m, names, nil
}
// GetEnvironmentRoles returns all the environment role binding names and details
func GetEnvironmentRoles(jxClient versioned.Interface, ns string) (map[string]*v1.EnvironmentRoleBinding, []string, error) {
names := []string{}
m := map[string]*v1.EnvironmentRoleBinding{}
envRoleBindingsList, err := jxClient.JenkinsV1().EnvironmentRoleBindings(ns).List(metav1.ListOptions{})
if err != nil {
return m, names, fmt.Errorf("Failed to retrieve EnvironmentRoleBinding list for namespace %s: %s", ns, err)
}
for _, envRoleBinding := range envRoleBindingsList.Items {
copy := envRoleBinding
name := copy.Name
m[name] = ©
names = append(names, name)
}
return m, names, err
}
// UpdateUserRoles updates the EnvironmentRoleBinding values based on the given userRoles
// userKind is "User" or "ServiceAccount"
func GetUserRoles(jxClient versioned.Interface, ns string, userKind string, userName string) ([]string, error) {
envRoles, _, err := GetEnvironmentRoles(jxClient, ns)
currentRoles := userRolesFor(userKind, userName, envRoles)
return currentRoles, err
}
// UpdateUserRoles updates the EnvironmentRoleBinding values based on the given userRoles.
// userKind is "User" or "ServiceAccount"
func UpdateUserRoles(kubeClient kubernetes.Interface, jxClient versioned.Interface, ns string, userKind string, userName string, userRoles []string, roles map[string]*rbacv1.Role) error {
envRoleInterface := jxClient.JenkinsV1().EnvironmentRoleBindings(ns)
envRoles, _, err := GetEnvironmentRoles(jxClient, ns)
for name, _ := range roles {
envRole := envRoles[name]
if envRole == nil {
envRole = &v1.EnvironmentRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
Labels: map[string]string{
LabelKind: ValueKindEnvironmentRole,
},
},
Spec: v1.EnvironmentRoleBindingSpec{
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: name,
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{},
},
}
_, err = envRoleInterface.Create(envRole)
if err != nil {
return errors.Wrapf(err, "Failed to create EnvironmentRoleBinding %s", name)
}
envRoles[name] = envRole
}
}
oldRoles := userRolesFor(userKind, userName, envRoles)
deleteRoles, createRoles := util.DiffSlices(oldRoles, userRoles)
for _, name := range deleteRoles {
envRole := envRoles[name]
if envRole == nil {
log.Warnf("Could not remove user %s kind %s from EnvironmentRoleBinding %s as it does not exist", userName, userKind, name)
} else {
found := false
for idx, subject := range envRole.Spec.Subjects {
if subject.Kind == userKind && subject.Name == userName {
found = true
envRole.Spec.Subjects = append(envRole.Spec.Subjects[0:idx], envRole.Spec.Subjects[idx+1:]...)
_, err = envRoleInterface.Update(envRole)
if err != nil {
return errors.Wrapf(err, "Failed to add User %s kind %s as a Subject of EnvironmentRoleBinding %s: %s", userName, userKind, name, err)
}
}
}
if !found {
log.Warnf("User user %s kind %s is not a Subject of EnvironmentRoleBinding %s", userName, userKind, name)
}
}
}
for _, name := range createRoles {
envRole := envRoles[name]
if envRole == nil {
// TODO lazily create the EnvironmentRoleBinding?
log.Warnf("Could not add user %s to EnvironmentRoleBinding %s as it does not exist!", userName, name)
} else {
found := false
for _, subject := range envRole.Spec.Subjects {
if subject.Kind == userKind && subject.Name == userName {
found = true
}
}
if found {
log.Warnf("User %s kind %s is already a Subject of EnvironmentRoleBinding %s", userName, userKind, name)
} else {
newSubject := rbacv1.Subject{
Name: userName,
Kind: userKind,
Namespace: ns,
}
newEnvRole, err := envRoleInterface.Get(envRole.Name, metav1.GetOptions{})
create := false
if err != nil {
create = true
newEnvRole = envRole
} else {
newEnvRole.Spec = envRole.Spec
}
newEnvRole.Spec.Subjects = append(newEnvRole.Spec.Subjects, newSubject)
if create {
_, err = envRoleInterface.Create(newEnvRole)
if err != nil {
return errors.Wrapf(err, "Failed to create EnvironmentRoleBinding %s with Subject User %s kind %s: %s", name, userName, userKind, err)
}
} else {
_, err = envRoleInterface.Update(newEnvRole)
if err != nil {
return errors.Wrapf(err, "Failed to add User %s kind %s as a Subject of EnvironmentRoleBinding %s: %s", userName, userKind, name, err)
}
}
}
}
}
return nil
}
func userRolesFor(userKind string, userName string, envRoles map[string]*v1.EnvironmentRoleBinding) []string {
answer := []string{}
for _, envRole := range envRoles {
for _, subject := range envRole.Spec.Subjects {
if subject.Kind == userKind && subject.Name == userName {
answer = append(answer, envRole.Name)
}
}
}
return answer
}