-
Notifications
You must be signed in to change notification settings - Fork 151
/
nsaccess.go
182 lines (154 loc) · 4.91 KB
/
nsaccess.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
package nsaccess
import (
"context"
"fmt"
authorizationv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
typedauth "k8s.io/client-go/kubernetes/typed/authorization/v1"
"k8s.io/client-go/rest"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
// DefautltWegoAppRules is the minimun set of permissions a user will need to use the wego-app in a given namespace
var DefautltWegoAppRules = []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"secrets", "pods", "events"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{"apps"},
Resources: []string{"deployments", "replicasets"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{"kustomize.toolkit.fluxcd.io"},
Resources: []string{"kustomizations"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{"helm.toolkit.fluxcd.io"},
Resources: []string{"helmreleases"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{"source.toolkit.fluxcd.io"},
Resources: []string{"buckets", "helmcharts", "gitrepositories", "helmrepositories"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{""},
Resources: []string{"events"},
Verbs: []string{"get", "list", "watch"},
},
}
// Checker contains methods for validing user access to Kubernetes namespaces, based on a set of PolicyRules
//counterfeiter:generate . Checker
type Checker interface {
// FilterAccessibleNamespaces returns a filtered list of namespaces to which a user has access to
FilterAccessibleNamespaces(ctx context.Context, cfg *rest.Config, namespaces []corev1.Namespace) ([]corev1.Namespace, error)
}
type simpleChecker struct {
rules []rbacv1.PolicyRule
}
func NewChecker(rules []rbacv1.PolicyRule) Checker {
return simpleChecker{rules: rules}
}
func (sc simpleChecker) FilterAccessibleNamespaces(ctx context.Context, cfg *rest.Config, namespaces []corev1.Namespace) ([]corev1.Namespace, error) {
result := []corev1.Namespace{}
for _, ns := range namespaces {
ok, err := userCanUseNamespace(ctx, cfg, ns, sc.rules)
if err != nil {
return nil, fmt.Errorf("user namespace access: %w", err)
}
if ok {
result = append(result, ns)
}
}
return result, nil
}
func userCanUseNamespace(ctx context.Context, cfg *rest.Config, ns corev1.Namespace, rules []rbacv1.PolicyRule) (bool, error) {
auth, err := newAuthClient(cfg)
if err != nil {
return false, err
}
sar := &authorizationv1.SelfSubjectRulesReview{
Spec: authorizationv1.SelfSubjectRulesReviewSpec{
Namespace: ns.Name,
},
}
authRes, err := auth.SelfSubjectRulesReviews().Create(ctx, sar, metav1.CreateOptions{})
if err != nil {
return false, err
}
return hasAllRules(authRes.Status, rules, ns.Name), nil
}
var allK8sVerbs = []string{"create", "get", "list", "watch", "patch", "delete", "deletecollection"}
// hasAll rules determines if a set of SubjectRulesReview rules match a minimum set of policy rules
func hasAllRules(status authorizationv1.SubjectRulesReviewStatus, rules []rbacv1.PolicyRule, ns string) bool {
hasAccess := true
// We need to understand the "sum" of all the rules for a role.
// Convert to a hash lookup to make it easier to tell what a user can do.
// Looks like { "apps": { "deployments": { get: true, list: true } } }
derivedAccess := map[string]map[string]map[string]bool{}
for _, statusRule := range status.ResourceRules {
for _, apiGroup := range statusRule.APIGroups {
if _, ok := derivedAccess[apiGroup]; !ok {
derivedAccess[apiGroup] = map[string]map[string]bool{}
}
for _, resource := range statusRule.Resources {
if _, ok := derivedAccess[apiGroup][resource]; !ok {
derivedAccess[apiGroup][resource] = map[string]bool{}
}
for _, verb := range statusRule.Verbs {
if verb == "*" {
for _, v := range allK8sVerbs {
derivedAccess[apiGroup][resource][v] = true
}
} else {
derivedAccess[apiGroup][resource][verb] = true
}
}
}
}
}
Rules:
for _, rule := range rules {
for _, apiGroup := range rule.APIGroups {
g, ok := derivedAccess[apiGroup]
if !ok {
hasAccess = false
continue
}
Resources:
for _, resource := range rule.Resources {
r, ok := g[resource]
if !ok {
// A resource is not present for this apiGroup.
hasAccess = false
continue Rules
}
for _, verb := range rule.Verbs {
_, ok := r[verb]
if !ok {
// A verb is not present for this resource,
// no need to check the rest of the verbs.
hasAccess = false
continue Resources
}
hasAccess = true
}
}
}
}
return hasAccess
}
func newAuthClient(cfg *rest.Config) (typedauth.AuthorizationV1Interface, error) {
cs, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("making clientset: %w", err)
}
return cs.AuthorizationV1(), nil
}