/
helpers.go
233 lines (209 loc) · 6.86 KB
/
helpers.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
package helpers
import (
"strings"
"github.com/jinzhu/copier"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
)
/////////////////////////////////////////////////
// CLUSTER INTERACTION
/////////////////////////////////////////////////
// DiscoverClusterResources returns a list of all known resources and groups known to this API server
func DiscoverClusterResources(config *rest.Config) (apiGroupList []*metav1.APIGroup, apiResourceList []*metav1.APIResourceList, err error) {
if err != nil {
return nil, nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, nil, err
}
groups, resources, err := discoveryClient.ServerGroupsAndResources()
if err != nil {
return nil, nil, err
}
return groups, resources, nil
}
/////////////////////////////////////////////////
// POLICY RULE MANIPULATION
/////////////////////////////////////////////////
// EnumeratePolicyRules takes a list of rules with wildcards and returns a list of policy rules with resources explicitly enumerated
func EnumeratePolicyRules(inputRules []rbacv1.PolicyRule, config *rest.Config) ([]rbacv1.PolicyRule, error) {
rules := []rbacv1.PolicyRule{}
_, apiResourceList, err := DiscoverClusterResources(config)
if err != nil {
return nil, err
}
allPossibleRules := APIResourcesToExpandedRules(apiResourceList)
for _, rule := range inputRules {
if ruleHasGroupWildcard(&rule) && ruleHasResourceWildcard(&rule) {
var relevantRules []rbacv1.PolicyRule
copier.Copy(&relevantRules, &allPossibleRules)
if !stringInSlice(rule.Verbs, "*") {
for index := range relevantRules {
relevantRules[index].Verbs = rule.Verbs
}
}
rules = append(rules, relevantRules...)
} else if ruleHasGroupWildcard(&rule) {
for _, resource := range rule.Resources {
for _, matchedRule := range allPossibleRules {
if stringInSlice(matchedRule.Resources, resource) {
var tmpRule rbacv1.PolicyRule
copier.Copy(&tmpRule, &matchedRule)
copier.Copy(&tmpRule.Verbs, &rule.Verbs)
rules = append(rules, tmpRule)
}
}
}
} else if ruleHasResourceWildcard(&rule) {
for _, group := range rule.APIGroups {
for _, matchedRule := range allPossibleRules {
if stringInSlice(matchedRule.APIGroups, group) {
var tmpRule rbacv1.PolicyRule
copier.Copy(&tmpRule, &matchedRule)
copier.Copy(&tmpRule.Verbs, &rule.Verbs)
rules = append(rules, tmpRule)
}
}
}
} else {
rules = append(rules, rule)
}
}
// fmt.Printf("%+v\n", rules)
return rules, nil
}
// ExpandPolicyRules ensures that multiple resources with the same verbs are not grouped together in the same rule definition (makes it easier to edit individual verbs later)
func ExpandPolicyRules(inputRules []rbacv1.PolicyRule) []rbacv1.PolicyRule {
rules := []rbacv1.PolicyRule{}
for _, rule := range inputRules {
if len(rule.Resources) > 1 {
for _, resource := range rule.Resources {
var newVerbs []string
var newAPIGroups []string
copier.Copy(&newVerbs, &rule.Verbs)
copier.Copy(&newAPIGroups, &rule.APIGroups)
newRule := rbacv1.PolicyRule{
APIGroups: newAPIGroups,
Resources: []string{resource},
Verbs: newVerbs,
}
rules = append(rules, newRule)
}
} else {
var tmpRule rbacv1.PolicyRule
copier.Copy(&tmpRule, &rule)
rules = append(rules, tmpRule)
}
}
return rules
}
// APIResourcesToExpandedRules converts an APIResourceList into a list of PolicyRules with all verbs allowed
func APIResourcesToExpandedRules(resourceLists []*metav1.APIResourceList) []rbacv1.PolicyRule {
policyRules := make([]rbacv1.PolicyRule, 0, 100)
for _, resourceList := range resourceLists {
for _, resource := range resourceList.APIResources {
group := strings.Split(resourceList.GroupVersion, "/")[0]
if group == "v1" {
group = "" // an extremely cool thing to have to do...
}
verbs := []string{"*"}
if len(resource.Verbs) > 0 {
copier.Copy(&verbs, &resource.Verbs)
}
policyRules = append(policyRules, rbacv1.PolicyRule{
APIGroups: []string{group},
Resources: []string{resource.Name},
Verbs: verbs,
})
}
}
return policyRules
}
// TODO: Literally all of this
// func ApplyAllowRulesToExpandedRuleset(fullRuleSet []rbacv1.PolicyRule, allowRules []rbacv1.PolicyRule, config *rest.Config) ([]rbacv1.PolicyRule, error) {
// outputRules := []rbacv1.PolicyRule{}
// enumeratedRules, err := EnumeratePolicyRules(fullRuleSet, config)
// if err != nil {
// return nil, err
// }
// expandedRules := ExpandPolicyRules(enumeratedRules)
// for _, allowRule := range allowRules {
// handled := false
// for _, rule := range fullRuleSet {
// }
// }
// return outputRules, nil
// }
func ApplyDenyRulesToExpandedRuleset(fullRuleSet []rbacv1.PolicyRule, denyRules []rbacv1.PolicyRule) []rbacv1.PolicyRule {
outputRules := []rbacv1.PolicyRule{}
for _, rule := range fullRuleSet {
for _, denyRule := range denyRules {
var newVerbs []string
denyRuleApplies := false
if ruleHasGroupWildcard(&denyRule) && ruleHasResourceWildcard(&denyRule) {
denyRuleApplies = true
} else if ruleHasGroupWildcard(&denyRule) && slicesIntersect(denyRule.Resources, rule.Resources) {
denyRuleApplies = true
} else if ruleHasResourceWildcard(&denyRule) && slicesIntersect(denyRule.APIGroups, rule.APIGroups) {
denyRuleApplies = true
} else if slicesIntersect(denyRule.APIGroups, rule.APIGroups) && slicesIntersect(denyRule.Resources, rule.Resources) {
denyRuleApplies = true
}
if denyRuleApplies {
if !stringInSlice(denyRule.Verbs, "*") {
newVerbs = subtractStringSlices(rule.Verbs, denyRule.Verbs)
if len(newVerbs) > 0 {
var tmpRule rbacv1.PolicyRule
copier.Copy(&tmpRule, &rule)
tmpRule.Verbs = newVerbs
outputRules = append(outputRules, tmpRule)
}
}
} else {
outputRules = append(outputRules, rule)
}
}
}
return outputRules
}
/////////////////////////////////////////////////
// GOLANG HELPER STUFF
/////////////////////////////////////////////////
func stringInSlice(slice []string, str string) bool {
for _, item := range slice {
if item == str {
return true
}
}
return false
}
func slicesIntersect(s1, s2 []string) bool {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
if hash[e] {
return true
}
}
return false
}
func ruleHasGroupWildcard(rule *rbacv1.PolicyRule) bool {
return stringInSlice(rule.APIGroups, "*")
}
func ruleHasResourceWildcard(rule *rbacv1.PolicyRule) bool {
return stringInSlice(rule.Resources, "*")
}
func subtractStringSlices(allElements []string, elementsToRemove []string) []string {
newList := []string{}
for _, element := range allElements {
if !stringInSlice(elementsToRemove, element) {
newList = append(newList, element)
}
}
return newList
}