-
Notifications
You must be signed in to change notification settings - Fork 18
/
permissions.go
153 lines (138 loc) · 5.22 KB
/
permissions.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
package manifesttestutils
import (
. "github.com/onsi/gomega"
"github.com/solo-io/go-utils/installutils/kuberesource"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
)
type ServiceAccountPermissions struct {
ServiceAccount map[string]*NamespacePermissions
}
type NamespacePermissions struct {
Namespace map[string]*ApiGroupPermissions
}
type ApiGroupPermissions struct {
ApiGroup map[string]*ResourcePermissions
}
type ResourcePermissions struct {
Resource map[string]*Verbs
}
type Verbs struct {
Values map[string]bool
}
func (p *ServiceAccountPermissions) AddExpectedPermission(serviceAccount, namespace string, apiGroups, resources, verbs []string) {
if p.ServiceAccount == nil {
p.ServiceAccount = make(map[string]*NamespacePermissions)
}
if _, exists := p.ServiceAccount[serviceAccount]; !exists {
p.ServiceAccount[serviceAccount] = &NamespacePermissions{}
}
p.ServiceAccount[serviceAccount].addExpectedPermission(namespace, apiGroups, resources, verbs)
}
func (p *NamespacePermissions) addExpectedPermission(namespace string, apiGroups, resources, verbs []string) {
if p.Namespace == nil {
p.Namespace = make(map[string]*ApiGroupPermissions)
}
if _, exists := p.Namespace[namespace]; !exists {
p.Namespace[namespace] = &ApiGroupPermissions{}
}
p.Namespace[namespace].addExpectedPermission(apiGroups, resources, verbs)
}
func (p *ApiGroupPermissions) addExpectedPermission(apiGroups, resources, verbs []string) {
if p.ApiGroup == nil {
p.ApiGroup = make(map[string]*ResourcePermissions)
}
for _, g := range apiGroups {
if _, exists := p.ApiGroup[g]; !exists {
p.ApiGroup[g] = &ResourcePermissions{}
}
p.ApiGroup[g].addExpectedPermission(resources, verbs)
}
}
func (p *ResourcePermissions) addExpectedPermission(resources, verbsToAdd []string) {
if p.Resource == nil {
p.Resource = make(map[string]*Verbs)
}
for _, r := range resources {
if _, exists := p.Resource[r]; !exists {
p.Resource[r] = &Verbs{Values: make(map[string]bool)}
}
for _, v := range verbsToAdd {
p.Resource[r].Values[v] = true
}
}
}
func (t *testManifest) mustFindDeploymentsV1Beta1() []*v1beta1.Deployment {
var deployments []*v1beta1.Deployment
for _, resource := range t.resources {
if resource.GetKind() == "Deployment" && resource.GetAPIVersion() == "extensions/v1beta1" {
converted, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred())
Expect(converted).To(BeAssignableToTypeOf(&v1beta1.Deployment{}))
deployments = append(deployments, converted.(*v1beta1.Deployment))
}
}
return deployments
}
func (t *testManifest) mustFindDeploymentsAppsV1() []*appsv1.Deployment {
var deployments []*appsv1.Deployment
for _, resource := range t.resources {
if resource.GetKind() == "Deployment" && resource.GetAPIVersion() == "apps/v1" {
converted, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred())
Expect(converted).To(BeAssignableToTypeOf(&appsv1.Deployment{}))
deployments = append(deployments, converted.(*appsv1.Deployment))
}
}
return deployments
}
func (t *testManifest) mustFindServiceAccounts() []*corev1.ServiceAccount {
var serviceAccounts []*corev1.ServiceAccount
for _, resource := range t.resources {
if resource.GetKind() == "ServiceAccount" {
converted, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred())
Expect(converted).To(BeAssignableToTypeOf(&corev1.ServiceAccount{}))
serviceAccounts = append(serviceAccounts, converted.(*corev1.ServiceAccount))
}
}
return serviceAccounts
}
// ApiGroup is "" for service accounts
func (t *testManifest) mustFindRoleBindings(subjectKind, subjectApiGroup, subjectNamespace, subjectName string) []*rbacv1.RoleBinding {
var roleBindings []*rbacv1.RoleBinding
for _, resource := range t.resources {
if resource.GetKind() == "RoleBinding" {
converted, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred())
Expect(converted).To(BeAssignableToTypeOf(&rbacv1.RoleBinding{}))
roleBinding := converted.(*rbacv1.RoleBinding)
for _, s := range roleBinding.Subjects {
if s.Kind == subjectKind && s.APIGroup == subjectApiGroup && s.Name == subjectName && s.Namespace == subjectNamespace {
roleBindings = append(roleBindings, converted.(*rbacv1.RoleBinding))
}
}
}
}
return roleBindings
}
// ApiGroup is "" for service accounts
func (t *testManifest) mustFindClusterRoleBindings(subjectKind, subjectApiGroup, subjectNamespace, subjectName string) []*rbacv1.ClusterRoleBinding {
var clusterRoleBindings []*rbacv1.ClusterRoleBinding
for _, resource := range t.resources {
if resource.GetKind() == "ClusterRoleBinding" {
converted, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred())
Expect(converted).To(BeAssignableToTypeOf(&rbacv1.ClusterRoleBinding{}))
roleBinding := converted.(*rbacv1.ClusterRoleBinding)
for _, s := range roleBinding.Subjects {
if s.Kind == subjectKind && s.APIGroup == subjectApiGroup && s.Name == subjectName && s.Namespace == subjectNamespace {
clusterRoleBindings = append(clusterRoleBindings, converted.(*rbacv1.ClusterRoleBinding))
}
}
}
}
return clusterRoleBindings
}