-
Notifications
You must be signed in to change notification settings - Fork 44
/
lister.go
205 lines (171 loc) · 5.33 KB
/
lister.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
// Copyright 2018 FairwindsOps Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lookup
import (
"context"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"google.golang.org/api/cloudresourcemanager/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
// Required for GKE Auth
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
type lister struct {
clientset kubernetes.Interface
filter string
gkeParsedProjectName string
subjectKind string
rbacSubjectsByScope map[string]rbacSubject
}
func (l *lister) loadAll() error {
rbErr := l.loadRoleBindings()
if rbErr != nil {
return rbErr
}
crbErr := l.loadClusterRoleBindings()
if crbErr != nil {
return crbErr
}
if l.gkeParsedProjectName != "" {
policy, gkeErr := loadGkeIAMPolicy(l.gkeParsedProjectName)
if gkeErr != nil {
return gkeErr
}
l.loadGkeIamPolicy(policy)
}
return nil
}
func (l *lister) printRbacBindings(outputFormat string) {
if len(l.rbacSubjectsByScope) < 1 {
fmt.Println("No RBAC Bindings found")
return
}
names := make([]string, 0, len(l.rbacSubjectsByScope))
for name := range l.rbacSubjectsByScope {
names = append(names, name)
}
sort.Strings(names)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)
if outputFormat == "wide" {
fmt.Fprintln(w, "SUBJECT\t SCOPE\t ROLE\t SOURCE")
} else {
fmt.Fprintln(w, "SUBJECT\t SCOPE\t ROLE")
}
for _, subjectName := range names {
rbacSubject := l.rbacSubjectsByScope[subjectName]
for scope, simpleRoles := range rbacSubject.RolesByScope {
for _, simpleRole := range simpleRoles {
if outputFormat == "wide" {
fmt.Fprintf(w, "%s/%s \t %s\t %s/%s\t %s/%s\n", rbacSubject.Kind, subjectName, scope, simpleRole.Kind, simpleRole.Name, simpleRole.Source.Kind, simpleRole.Source.Name)
} else {
fmt.Fprintf(w, "%s \t %s\t %s/%s\n", subjectName, scope, simpleRole.Kind, simpleRole.Name)
}
}
}
}
w.Flush()
}
func (l *lister) loadRoleBindings() error {
roleBindings, err := l.clientset.RbacV1().RoleBindings("").List(context.Background(), metav1.ListOptions{})
if err != nil {
fmt.Println("Error loading role bindings")
return err
}
for _, roleBinding := range roleBindings.Items {
for _, subject := range roleBinding.Subjects {
if l.nameMatches(subject.Name) && l.kindMatches(subject.Kind) {
if rbacSubj, exist := l.rbacSubjectsByScope[subject.Name]; exist {
rbacSubj.addRoleBinding(&roleBinding)
} else {
rbacSubj := rbacSubject{
Kind: subject.Kind,
RolesByScope: make(map[string][]simpleRole),
}
rbacSubj.addRoleBinding(&roleBinding)
subjectKey := subject.Name
if rbacSubj.Kind == "ServiceAccount" {
subjectKey = fmt.Sprintf("%s:%s", subject.Namespace, subject.Name)
}
l.rbacSubjectsByScope[subjectKey] = rbacSubj
}
}
}
}
return nil
}
func (l *lister) loadClusterRoleBindings() error {
clusterRoleBindings, err := l.clientset.RbacV1().ClusterRoleBindings().List(context.Background(), metav1.ListOptions{})
if err != nil {
fmt.Println("Error loading cluster role bindings")
return err
}
for _, clusterRoleBinding := range clusterRoleBindings.Items {
for _, subject := range clusterRoleBinding.Subjects {
if l.nameMatches(subject.Name) && l.kindMatches(subject.Kind) {
if rbacSubj, exist := l.rbacSubjectsByScope[subject.Name]; exist {
rbacSubj.addClusterRoleBinding(&clusterRoleBinding)
} else {
rbacSubj := rbacSubject{
Kind: subject.Kind,
RolesByScope: make(map[string][]simpleRole),
}
rbacSubj.addClusterRoleBinding(&clusterRoleBinding)
subjectKey := subject.Name
if rbacSubj.Kind == "ServiceAccount" {
subjectKey = fmt.Sprintf("%s:%s", subject.Namespace, subject.Name)
}
l.rbacSubjectsByScope[subjectKey] = rbacSubj
}
}
}
}
return nil
}
func (l *lister) loadGkeIamPolicy(policy *cloudresourcemanager.Policy) {
for _, binding := range policy.Bindings {
if sr, ok := gkeIamRoles[binding.Role]; ok {
for _, member := range binding.Members {
s := strings.Split(member, ":")
memberKind := strings.Title(s[0])
memberName := s[1]
if l.nameMatches(memberName) && l.kindMatches(memberKind) {
rbacSubj, exist := l.rbacSubjectsByScope[memberName]
if !exist {
rbacSubj = rbacSubject{
Kind: memberKind,
RolesByScope: make(map[string][]simpleRole),
}
}
rbacSubj.RolesByScope[gkeIamScope] = append(rbacSubj.RolesByScope[gkeIamScope], sr)
l.rbacSubjectsByScope[memberName] = rbacSubj
}
}
}
}
}
func (l *lister) nameMatches(name string) bool {
return l.filter == "" || strings.Contains(name, l.filter)
}
func (l *lister) kindMatches(kind string) bool {
if l.subjectKind == "" {
return true
}
lowerKind := strings.ToLower(kind)
return lowerKind == l.subjectKind
}