-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
395 lines (339 loc) · 12.7 KB
/
client.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package policies
import (
"context"
"fmt"
"net/url"
"slices"
policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
"github.com/rs/zerolog/log"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// A client to get Kubewarden policies from the Kubernetes cluster
type Client struct {
// client is a controller-runtime client extended with the Kubewarden CRDs
client client.Client
// Namespace where the Kubewarden components (e.g. policy server) are installed
// This is the namespace used to get the policy server resources
kubewardenNamespace string
// FQDN of the policy server to query. If not empty, it will query on port 3000.
// Useful for out-of-cluster debugging
policyServerURL string
}
// Policies represents a collection of auditable policies
type Policies struct {
// PoliciesByGVR a map of policies grouped by GVR
PoliciesByGVR map[schema.GroupVersionResource][]*Policy
// PolicyNum represents the number of policies
PolicyNum int
// SkippedNum represents the number of skipped policies
SkippedNum int
}
// Policy represents a policy and the URL of the policy server where it is running
type Policy struct {
policiesv1.Policy
PolicyServer *url.URL
}
// NewClient returns a policy Client
func NewClient(client client.Client, kubewardenNamespace string, policyServerURL string) (*Client, error) {
if policyServerURL != "" {
log.Info().Msg(fmt.Sprintf("querying PolicyServers at %s for debugging purposes. Don't forget to start `kubectl port-forward` if needed", policyServerURL))
}
return &Client{
client: client,
kubewardenNamespace: kubewardenNamespace,
policyServerURL: policyServerURL,
}, nil
}
// GetPoliciesForANamespace gets all the auditable policies for a given namespace
func (f *Client) GetPoliciesForANamespace(ctx context.Context, namespace string) (*Policies, error) {
namespacePolicies, err := f.findNamespacesForAllClusterAdmissionPolicies(ctx)
if err != nil {
return nil, fmt.Errorf("can't get ClusterAdmissionPolicies: %w", err)
}
admissionPolicies, err := f.getAdmissionPolicies(ctx, namespace)
if err != nil {
return nil, fmt.Errorf("can't get AdmissionPolicies: %w", err)
}
for _, policy := range admissionPolicies {
policy := policy
namespacePolicies[namespace] = append(namespacePolicies[namespace], &policy)
}
return f.groupPoliciesByGVR(ctx, namespacePolicies[namespace], true)
}
func (f *Client) getClusterAdmissionPolicies(ctx context.Context) ([]policiesv1.ClusterAdmissionPolicy, error) {
policies := &policiesv1.ClusterAdmissionPolicyList{}
err := f.client.List(ctx, policies)
if err != nil {
return []policiesv1.ClusterAdmissionPolicy{}, err
}
return policies.Items, nil
}
// GetClusterWidePolicies returns all the auditable cluster-wide policies
func (f *Client) GetClusterWidePolicies(ctx context.Context) (*Policies, error) {
clusterAdmissionPolicies, err := f.getClusterAdmissionPolicies(ctx)
if err != nil {
return nil, err
}
policies := []policiesv1.Policy{}
for _, policy := range clusterAdmissionPolicies {
policy := policy
policies = append(policies, &policy)
}
return f.groupPoliciesByGVR(ctx, policies, false)
}
// initializes map with an entry for all namespaces with an empty policies array as value
func (f *Client) initNamespacePoliciesMap(ctx context.Context) (map[string][]policiesv1.Policy, error) {
namespacePolicies := make(map[string][]policiesv1.Policy)
namespaceList := &corev1.NamespaceList{}
err := f.client.List(ctx, namespaceList, &client.ListOptions{})
if err != nil {
return nil, fmt.Errorf("can't list namespaces: %w", err)
}
for _, namespace := range namespaceList.Items {
namespacePolicies[namespace.Name] = []policiesv1.Policy{}
}
return namespacePolicies, nil
}
// returns a map with an entry per each namespace. Key is the namespace name, and value is an array of ClusterAdmissionPolicies
// that will evaluate resources within this namespace.
func (f *Client) findNamespacesForAllClusterAdmissionPolicies(ctx context.Context) (map[string][]policiesv1.Policy, error) {
namespacePolicies, err := f.initNamespacePoliciesMap(ctx)
if err != nil {
return nil, err
}
policies := &policiesv1.ClusterAdmissionPolicyList{}
err = f.client.List(ctx, policies, &client.ListOptions{})
if err != nil {
return nil, fmt.Errorf("can't list ClusterAdmissionPolicies: %w", err)
}
for _, policy := range policies.Items {
policy := policy
namespaces, err := f.findNamespacesForClusterAdmissionPolicy(ctx, policy)
if err != nil {
return nil, fmt.Errorf("can't find namespaces for ClusterAdmissionPolicy %s: %w", policy.Name, err)
}
for _, namespace := range namespaces {
namespacePolicies[namespace.Name] = append(namespacePolicies[namespace.Name], &policy)
}
}
return namespacePolicies, nil
}
// finds all namespaces where this ClusterAdmissionPolicy will evaluate resources. It uses the namespaceSelector field to filter the namespaces.
func (f *Client) findNamespacesForClusterAdmissionPolicy(ctx context.Context, policy policiesv1.ClusterAdmissionPolicy) ([]corev1.Namespace, error) {
namespaceList := &corev1.NamespaceList{}
labelSelector, err := metav1.LabelSelectorAsSelector(policy.GetUpdatedNamespaceSelector(f.kubewardenNamespace))
if err != nil {
return nil, err
}
opts := client.ListOptions{
LabelSelector: labelSelector,
}
err = f.client.List(ctx, namespaceList, &opts)
if err != nil {
return nil, err
}
return namespaceList.Items, nil
}
func (f *Client) getAdmissionPolicies(ctx context.Context, namespace string) ([]policiesv1.AdmissionPolicy, error) {
policies := &policiesv1.AdmissionPolicyList{}
err := f.client.List(ctx, policies, &client.ListOptions{Namespace: namespace})
if err != nil {
return nil, err
}
return policies.Items, nil
}
// groupPoliciesByGVRAndLabelSelectorg groups policies by GVR.
// If namespaced is true, it will skip cluster-wide resources, otherwise it will skip namespaced resources.
func (f *Client) groupPoliciesByGVR(ctx context.Context, policies []policiesv1.Policy, namespaced bool) (*Policies, error) { //nolint:funlen
policiesByGVR := make(map[schema.GroupVersionResource][]*Policy)
auditablePolicies := map[string]struct{}{}
skippedPolicies := map[string]struct{}{}
for _, policy := range policies {
rules := filterWildcardRules(policy.GetRules())
if len(rules) == 0 {
skippedPolicies[policy.GetUniqueName()] = struct{}{}
log.
Debug().
Str("policy", policy.GetUniqueName()).
Msg("the policy targets only wildcard resources, skipping...")
continue
}
rules = filterNonCreateOperations(rules)
if len(rules) == 0 {
skippedPolicies[policy.GetUniqueName()] = struct{}{}
log.
Debug().
Str("policy", policy.GetUniqueName()).
Msg("the policy does not have rules with a CREATE operation, skipping...")
continue
}
groupVersionResources, err := f.getGroupVersionResources(rules, namespaced)
if err != nil {
return nil, err
}
if len(groupVersionResources) == 0 {
log.
Debug().
Str("policy", policy.GetUniqueName()).
Bool("namespaced", namespaced).
Msg("the policy does not target resources with the selected scope")
continue
}
if !policy.GetBackgroundAudit() {
skippedPolicies[policy.GetUniqueName()] = struct{}{}
log.Debug().Str("policy", policy.GetUniqueName()).Msg("the policy has backgroundAudit set to false, skipping...")
continue
}
if policy.GetStatus().PolicyStatus != policiesv1.PolicyStatusActive {
skippedPolicies[policy.GetUniqueName()] = struct{}{}
log.Debug().Str("policy", policy.GetUniqueName()).Msg("the policy is not active, skipping...")
continue
}
url, err := f.getPolicyServerURLRunningPolicy(ctx, policy)
if err != nil {
return nil, err
}
auditablePolicies[policy.GetUniqueName()] = struct{}{}
policy := &Policy{
Policy: policy,
PolicyServer: url,
}
for _, gvr := range groupVersionResources {
addPolicyToMap(policiesByGVR, gvr, policy)
}
}
return &Policies{
PoliciesByGVR: policiesByGVR,
PolicyNum: len(auditablePolicies),
SkippedNum: len(skippedPolicies),
}, nil
}
func addPolicyToMap(policiesByGVR map[schema.GroupVersionResource][]*Policy, gvr schema.GroupVersionResource, policy *Policy) {
value, found := policiesByGVR[gvr]
if !found {
policiesByGVR[gvr] = []*Policy{policy}
} else {
policiesByGVR[gvr] = append(value, policy)
}
}
// getGroupVersionResources returns a list of GroupVersionResource from a list of policies.
// if namespaced is true, it will skip cluster-wide resources, otherwise it will skip namespaced resources.
func (f *Client) getGroupVersionResources(rules []admissionregistrationv1.RuleWithOperations, namespaced bool) ([]schema.GroupVersionResource, error) {
var groupVersionResources []schema.GroupVersionResource
for _, rule := range rules {
for _, resource := range rule.Resources {
for _, version := range rule.APIVersions {
for _, group := range rule.APIGroups {
gvr := schema.GroupVersionResource{
Group: group,
Version: version,
Resource: resource,
}
isNamespaced, err := f.isNamespacedResource(gvr)
if err != nil {
return nil, err
}
if namespaced && !isNamespaced {
// continue if resource is clusterwide
continue
}
if !namespaced && isNamespaced {
// continue if resource is namespaced
continue
}
groupVersionResources = append(groupVersionResources, gvr)
}
}
}
}
return groupVersionResources, nil
}
// isNamespacedResource checks if the given resource is namespaced or not.
func (f *Client) isNamespacedResource(gvr schema.GroupVersionResource) (bool, error) {
gvk, err := f.client.RESTMapper().KindFor(gvr)
if err != nil {
return false, err
}
mapping, err := f.client.RESTMapper().RESTMapping(gvk.GroupKind(), gvr.Version)
if err != nil {
return false, err
}
return mapping.Scope.Name() == meta.RESTScopeNameNamespace, nil
}
func (f *Client) getPolicyServerURLRunningPolicy(ctx context.Context, policy policiesv1.Policy) (*url.URL, error) {
policyServer, err := f.getPolicyServerByName(ctx, policy.GetPolicyServer())
if err != nil {
return nil, err
}
service, err := f.getServiceByAppLabel(ctx, policyServer.AppLabel(), f.kubewardenNamespace)
if err != nil {
return nil, err
}
if len(service.Spec.Ports) < 1 {
return nil, fmt.Errorf("policy server service does not have a port")
}
var urlStr string
if f.policyServerURL != "" {
url, err := url.Parse(f.policyServerURL)
if err != nil {
log.Fatal().Msg("incorrect URL for policy-server")
}
urlStr = fmt.Sprintf("%s/audit/%s", url, policy.GetUniqueName())
} else {
urlStr = fmt.Sprintf("https://%s.%s.svc:%d/audit/%s", service.Name, f.kubewardenNamespace, service.Spec.Ports[0].Port, policy.GetUniqueName())
}
url, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
return url, nil
}
func (f *Client) getPolicyServerByName(ctx context.Context, policyServerName string) (*policiesv1.PolicyServer, error) {
var policyServer policiesv1.PolicyServer
err := f.client.Get(ctx, client.ObjectKey{Name: policyServerName}, &policyServer)
if err != nil {
return nil, err
}
return &policyServer, nil
}
func (f *Client) getServiceByAppLabel(ctx context.Context, appLabel string, namespace string) (*corev1.Service, error) {
serviceList := corev1.ServiceList{}
err := f.client.List(ctx, &serviceList, &client.ListOptions{Namespace: namespace}, &client.MatchingLabels{"app": appLabel})
if err != nil {
return nil, err
}
if len(serviceList.Items) != 1 {
return nil, fmt.Errorf("could not find a single service for the given policy server app label")
}
return &serviceList.Items[0], nil
}
// filterWildcardRules filters out rules that contain a wildcard in the APIGroups, APIVersions or Resources fields
func filterWildcardRules(rules []admissionregistrationv1.RuleWithOperations) []admissionregistrationv1.RuleWithOperations {
filteredRules := []admissionregistrationv1.RuleWithOperations{}
for _, rule := range rules {
if slices.Contains(rule.APIGroups, "*") ||
slices.Contains(rule.APIVersions, "*") ||
slices.Contains(rule.Resources, "*") {
continue
}
filteredRules = append(filteredRules, rule)
}
return filteredRules
}
// filterNonCreateOperations filters out rules that do not contain a CREATE operation
func filterNonCreateOperations(rules []admissionregistrationv1.RuleWithOperations) []admissionregistrationv1.RuleWithOperations {
filteredRules := []admissionregistrationv1.RuleWithOperations{}
for _, rule := range rules {
for _, operation := range rule.Operations {
if operation == admissionregistrationv1.Create {
filteredRules = append(filteredRules, rule)
}
}
}
return filteredRules
}