-
Notifications
You must be signed in to change notification settings - Fork 4
/
starClusterRoleAndBindings.go
70 lines (64 loc) · 1.49 KB
/
starClusterRoleAndBindings.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
// OPR-R13-RBAC - ClusterRole has full permissions over ClusterRoles and ClusterRoleBindings
package rules
import (
"encoding/json"
rbacv1 "k8s.io/api/rbac/v1"
)
func StarClusterRoleAndBindings(input []byte) int {
rbac := 0
var foundCR, foundCRB bool
clusterRole := &rbacv1.ClusterRole{}
err := json.Unmarshal(input, clusterRole)
if err != nil {
return 0
}
for _, rule := range clusterRole.Rules {
if contains("rbac.authorization.k8s.io", rule.APIGroups) &&
containsAll([]string{"clusterroles", "clusterrolebindings"}, rule.Resources) &&
(contains("*", rule.Verbs) || containsAll([]string{
"get",
"create",
"update",
"list",
"patch",
"watch",
"delete",
"deletecollection",
}, rule.Verbs)) {
rbac++
} else if contains("rbac.authorization.k8s.io", rule.APIGroups) &&
contains("clusterroles", rule.Resources) &&
(contains("*", rule.Verbs) || containsAll([]string{
"get",
"create",
"update",
"list",
"patch",
"watch",
"delete",
"deletecollection",
}, rule.Verbs)) {
foundCR = true
if foundCR && foundCRB {
rbac++
}
} else if contains("rbac.authorization.k8s.io", rule.APIGroups) &&
contains("clusterrolebindings", rule.Resources) &&
(contains("*", rule.Verbs) || containsAll([]string{
"get",
"create",
"update",
"list",
"patch",
"watch",
"delete",
"deletecollection",
}, rule.Verbs)) {
foundCRB = true
if foundCR && foundCRB {
rbac++
}
}
}
return rbac
}