-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceaccount.go
324 lines (279 loc) · 10.8 KB
/
serviceaccount.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
package podsecuritypolicy
import (
"fmt"
v13 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/extensions/v1beta1"
"github.com/rancher/types/apis/management.cattle.io/v3"
v12 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
)
const psptpbByTargetProjectNameAnnotationIndex = "podsecuritypolicy.rbac.user.cattle.io/psptpb-by-project-id"
const roleBindingByServiceAccountIndex = "podsecuritypolicy.rbac.user.cattle.io/role-binding-by-service-account"
const psptpbRoleBindingAnnotation = "podsecuritypolicy.rbac.user.cattle.io/psptpb-role-binding"
// RegisterServiceAccount ensures that:
// 1. Each namespace has a pod security policy assigned to a role if:
// a. its project has a PSPT assigned to it
// OR
// b. its cluster has a default PSPT assigned to it
// 2. PSPs are bound to their associated service accounts via a cluster role binding
func RegisterServiceAccount(context *config.UserContext) {
logrus.Infof("registering podsecuritypolicy serviceaccount handler for cluster %v", context.ClusterName)
psptpbInformer := context.Management.Management.PodSecurityPolicyTemplateProjectBindings("").Controller().Informer()
psptpbIndexers := map[string]cache.IndexFunc{
psptpbByTargetProjectNameAnnotationIndex: psptpbByTargetProjectName,
}
psptpbInformer.AddIndexers(psptpbIndexers)
roleBindingInformer := context.RBAC.RoleBindings("").Controller().Informer()
roleBindingIndexers := map[string]cache.IndexFunc{
roleBindingByServiceAccountIndex: roleBindingByServiceAccount,
}
roleBindingInformer.AddIndexers(roleBindingIndexers)
m := &serviceAccountManager{
clusterName: context.ClusterName,
clusters: context.Management.Management.Clusters(""),
pspts: context.Management.Management.PodSecurityPolicyTemplates(""),
roleBindings: context.RBAC.RoleBindings(""),
roleBindingIndexer: roleBindingInformer.GetIndexer(),
policies: context.Extensions.PodSecurityPolicies(""),
psptpbIndexer: psptpbInformer.GetIndexer(),
clusterLister: context.Management.Management.Clusters("").Controller().Lister(),
psptLister: context.Management.Management.PodSecurityPolicyTemplates("").Controller().Lister(),
templateLister: context.Management.Management.PodSecurityPolicyTemplates("").Controller().Lister(),
policyLister: context.Extensions.PodSecurityPolicies("").Controller().Lister(),
roleBindingLister: context.RBAC.RoleBindings("").Controller().Lister(),
roleLister: context.RBAC.ClusterRoles("").Controller().Lister(),
namespaceLister: context.Core.Namespaces("").Controller().Lister(),
projectLister: context.Management.Management.Projects("").Controller().Lister(),
psptpbLister: context.Management.Management.PodSecurityPolicyTemplateProjectBindings("").
Controller().Lister(),
}
context.Core.ServiceAccounts("").AddHandler("ServiceAccountLifecycleHandler", m.sync)
}
func psptpbByTargetProjectName(obj interface{}) ([]string, error) {
psptpb, ok := obj.(*v3.PodSecurityPolicyTemplateProjectBinding)
if !ok || psptpb.TargetProjectName == "" {
return []string{}, nil
}
return []string{psptpb.TargetProjectName}, nil
}
func roleBindingByServiceAccount(obj interface{}) ([]string, error) {
roleBinding, ok := obj.(*rbac.RoleBinding)
if !ok || len(roleBinding.Subjects) != 1 ||
roleBinding.Subjects[0].Name == "" ||
roleBinding.Subjects[0].Namespace == "" {
return []string{}, nil
}
subject := roleBinding.Subjects[0]
return []string{subject.Namespace + "-" + subject.Name}, nil
}
type serviceAccountManager struct {
clusterName string
clusterLister v3.ClusterLister
clusters v3.ClusterInterface
pspts v3.PodSecurityPolicyTemplateInterface
psptLister v3.PodSecurityPolicyTemplateLister
psptpbIndexer cache.Indexer
templateLister v3.PodSecurityPolicyTemplateLister
policyLister v1beta1.PodSecurityPolicyLister
roleBindingLister v12.RoleBindingLister
roleBindings v12.RoleBindingInterface
roleBindingIndexer cache.Indexer
policies v1beta1.PodSecurityPolicyInterface
roleLister v12.ClusterRoleLister
namespaceLister v13.NamespaceLister
projectLister v3.ProjectLister
psptpbLister v3.PodSecurityPolicyTemplateProjectBindingLister
}
func (m *serviceAccountManager) sync(key string, obj *v1.ServiceAccount) error {
if obj == nil {
// do nothing
return nil
}
namespace, err := m.namespaceLister.Get("", obj.Namespace)
if err != nil {
return fmt.Errorf("error getting projects: %v", err)
}
if namespace.Annotations[projectIDAnnotation] == "" {
return nil
}
psptpbs, err := m.psptpbIndexer.ByIndex(psptpbByTargetProjectNameAnnotationIndex, namespace.Annotations[projectIDAnnotation])
if err != nil {
return fmt.Errorf("error getting psptpbs: %v", err)
}
onePSPTPBExists := false
desiredBindings := map[string]*v3.PodSecurityPolicyTemplateProjectBinding{}
for _, rawPSPTPB := range psptpbs {
psptpb, ok := rawPSPTPB.(*v3.PodSecurityPolicyTemplateProjectBinding)
if !ok {
return fmt.Errorf("could not convert to *v3.PodSecurityPolicyTemplateProjectBinding: %v", rawPSPTPB)
}
if psptpb.DeletionTimestamp != nil {
continue
}
onePSPTPBExists = true
key := getClusterRoleName(psptpb.PodSecurityPolicyTemplateName)
desiredBindings[key] = psptpb
}
originalDesiredBindingsLen := len(desiredBindings)
roleBindings, err := m.roleBindingIndexer.ByIndex(roleBindingByServiceAccountIndex, obj.Namespace+"-"+obj.Name)
if err != nil {
return fmt.Errorf("error getting role bindings: %v", err)
}
cluster, err := m.clusterLister.Get("", m.clusterName)
if err != nil {
return fmt.Errorf("error getting cluster: %v", err)
}
for _, rawRoleBinding := range roleBindings {
roleBinding, ok := rawRoleBinding.(*rbac.RoleBinding)
if !ok {
return fmt.Errorf("could not convert to *rbac2.RoleBinding: %v", rawRoleBinding)
}
key := roleBinding.RoleRef.Name
if desiredBindings[key] == nil && okToDelete(obj, roleBinding, cluster, originalDesiredBindingsLen) {
err = m.roleBindings.DeleteNamespaced(roleBinding.Namespace, roleBinding.Name, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("error deleting role binding: %v", err)
}
} else {
delete(desiredBindings, key)
}
}
for clusterRoleName, desiredBinding := range desiredBindings {
roleBindingName := getRoleBindingName(obj, clusterRoleName)
_, err = m.roleBindings.Create(&rbac.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: roleBindingName,
Namespace: obj.Namespace,
Annotations: map[string]string{
podSecurityPolicyTemplateParentAnnotation: desiredBinding.PodSecurityPolicyTemplateName,
psptpbRoleBindingAnnotation: "true",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Name: obj.Name,
Kind: "ServiceAccount",
UID: obj.UID,
},
},
},
TypeMeta: metav1.TypeMeta{
Kind: "RoleBinding",
},
RoleRef: rbac.RoleRef{
APIGroup: apiGroup,
Name: clusterRoleName,
Kind: "ClusterRole",
},
Subjects: []rbac.Subject{
{
Kind: "ServiceAccount",
Name: obj.Name,
Namespace: obj.Namespace,
},
},
})
if err != nil {
return fmt.Errorf("error creating binding: %v", err)
}
}
if !onePSPTPBExists {
// create default pspt role binding if it is set
clusterRoleName := getClusterRoleName(cluster.Spec.DefaultPodSecurityPolicyTemplateName)
roleBindingName := getDefaultRoleBindingName(obj, clusterRoleName)
if cluster.Spec.DefaultPodSecurityPolicyTemplateName != "" {
_, err := m.roleBindingLister.Get(obj.Namespace, roleBindingName)
if err != nil {
if errors.IsNotFound(err) {
_, err = m.roleBindings.Create(&rbac.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: roleBindingName,
Namespace: obj.Namespace,
Annotations: map[string]string{
podSecurityPolicyTemplateParentAnnotation: cluster.Spec.DefaultPodSecurityPolicyTemplateName,
psptpbRoleBindingAnnotation: "true",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Name: obj.Name,
Kind: "ServiceAccount",
UID: obj.UID,
},
},
},
TypeMeta: metav1.TypeMeta{
Kind: "RoleBinding",
},
RoleRef: rbac.RoleRef{
APIGroup: apiGroup,
Name: clusterRoleName,
Kind: "ClusterRole",
},
Subjects: []rbac.Subject{
{
Kind: "ServiceAccount",
Name: obj.Name,
Namespace: obj.Namespace,
},
},
})
if err != nil {
return fmt.Errorf("error creating role binding: %v", err)
}
} else {
return fmt.Errorf("error getting role binding %v: %v", roleBindingName, err)
}
}
}
}
return nil
}
func okToDelete(svcAct *v1.ServiceAccount, rb *rbac.RoleBinding, cluster *v3.Cluster,
originalDesiredBindingsLen int) bool {
// This is not a role binding this logic should manage so exit immediately
if rb.Annotations[psptpbRoleBindingAnnotation] == "" {
return false
}
// No default PSPT is set so its ok to delete this if its a normal rolebinding or a leftover default PSPT binding
if cluster.Spec.DefaultPodSecurityPolicyTemplateName == "" {
return true
}
// at least one PSPTPB exists so we need to delete all default PSPT bindings
if originalDesiredBindingsLen > 0 {
return true
}
// the default PSPT has changed so we need to clean it up before creating the new one
if getDefaultRoleBindingName(svcAct,
getClusterRoleName(cluster.Spec.DefaultPodSecurityPolicyTemplateName)) != rb.Name {
return true
}
return false
}
func getRoleBindingName(obj *v1.ServiceAccount, clusterRoleName string) string {
return fmt.Sprintf("%v-%v-%v-binding", obj.Name, obj.Namespace, clusterRoleName)
}
func getDefaultRoleBindingName(obj *v1.ServiceAccount, clusterRoleName string) string {
return fmt.Sprintf("default-%v-%v-%v-binding", obj.Name, obj.Namespace, clusterRoleName)
}
func getClusterRoleName(podSecurityPolicyTemplateName string) string {
return fmt.Sprintf("%v-clusterrole", podSecurityPolicyTemplateName)
}
func resyncServiceAccounts(serviceAccountLister v13.ServiceAccountLister,
serviceAccountController v13.ServiceAccountController, namespace string) error {
serviceAccounts, err := serviceAccountLister.List(namespace, labels.Everything())
if err != nil {
return fmt.Errorf("error getting service accounts: %v", err)
}
for _, account := range serviceAccounts {
serviceAccountController.Enqueue(account.Namespace, account.Name)
}
return nil
}