-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredefined_rules.go
531 lines (486 loc) · 19.7 KB
/
predefined_rules.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package kubelint
import (
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
batchV1 "k8s.io/api/batch/v1"
batchV1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
networkingV1 "k8s.io/api/networking/v1"
)
/*
There are a whole bunch of predefined rules for you to use to make it convenient
to include more generally applicable rules. If you wish to expand the list, please submit a pull request!
Predefined rules relating to resources of type appsv1.Deployment
- An AppsV1Deployment should have a project label: APPSV1_DEPLOYMENT_EXISTS_PROJECT_LABEL
- An AppsV1Deployment should have an app.kubernetes.io/name label: APPSV1_DEPLOYMENT_EXISTS_APP_K8S_LABEL
- An AppsV1Deployment should be within a namespace: APPSV1_DEPLOYMENT_WITHIN_NAMESPACE
- An AppsV1Deployment should specify a liveness endpoint: APPSV1_DEPLOYMENT_CONTAINER_EXISTS_LIVENESS
- An AppsV1Deployment should specify a readiness endpoint: APPSV1_DEPLOYMENT_CONTAINER_EXISTS_READINESS
- An AppsV1Deploument should have liveness and readiness endpoints that aren't the same: APPSV1_DEPLOYMENT_LIVENESS_READINESS_NONMATCHING
Predefined rules relating to resources of type v1.PodSpec
- A V1PodSpec should have a non-nil security context: V1_PODSPEC_NON_NIL_SECURITY_CONTEXT
- A V1PodSpec should specify runAsNonRoot: true: V1_PODSPEC_RUN_AS_NON_ROOT
- A V1PodSpec should have a user and group ID of 44444: V1_PODSPEC_CORRECT_USER_GROUP_ID
- A V1PodSpec should have exactly one container: V1_PODSPEC_EXACTLY_1_CONTAINER
- A V1PodSpec should have a non-zero number of containers: V1_PODSPEC_NON_ZERO_CONTAINERS
Predefined rules relating to resources of type v1.Container
- A V1Container should have a non-null security context: V1_CONTAINER_EXISTS_SECURITY_CONTEXT
- A V1Container should not allow privilege escalation: V1_CONTAINER_ALLOW_PRIVILEGE_ESCALATION_FALSE
- A V1Container's image should come from a set of allowed images defined in isImageAllowed: V1_CONTAINER_VALID_IMAGE
- A V1Container should have privileged set to false: V1_CONTAINER_PRIVILEGED_FALSE
- A V1Container should specify Resource Limits and Requests: V1_CONTAINER_EXISTS_RESOURCE_LIMITS_AND_REQUESTS
- A V1Container should make CPU requests that are less than or equal to 100%: V1_CONTAINER_REQUESTS_CPU_REASONABLE
Predefined rules relating to resources of type batchV1.Job
- A BatchV1Beta1CronJob should be within a namespace
- A BatchV1Beta1CronJob should forbid concurrent operations: BATCHV1_BETA1_CRONJOB_FORBID_CONCURRENT
- A BatchV1Job should be within a namespace: BATCHV1_JOB_WITHIN_NAMESPACE
- A BatchV1Job's restart policy should be set to Never: BATCHV1_JOB_RESTART_NEVER
- A BatchV1Job's Time to Live should be set: BATCHV1_JOB_EXISTS_TTL
Predefined rules relating to resources of type v1.Namespace
- A V1Namespace should have a valid DNS name: V1_NAMESPACE_VALID_DNS
Predefined rules relating to resources of type v1.Service
- A V1Service should be within a namespace: V1_SERVICE_WITHIN_NAMESPACE
- A V1Service name should be a valid DNS: V1_SERVICE_NAME_VALID_DNS
Predefined interdependent rules
- A unit should always contain one namespace: INTERDEPENDENT_ONE_NAMESPACE
- All resources should be under the namespace in the unit: INTERDEPENDENT_MATCHING_NAMESPACE
- The unit should contain a network policy: INTERDEPENDENT_NETWORK_POLICY_REQUIRED
*/
var (
// An AppsV1Deployment should have a project label.
APPSV1_DEPLOYMENT_EXISTS_PROJECT_LABEL = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_EXISTS_PROJECT_LABEL",
Condition: func(deployment *appsv1.Deployment) bool {
_, found := deployment.Spec.Template.Labels["project"]
return found
},
Message: "There should be a project label present under the deployment's spec.template.labels",
Level: log.ErrorLevel,
}
// An AppsV1Deployment should have an app.kubernetes.io/name label.
APPSV1_DEPLOYMENT_EXISTS_APP_K8S_LABEL = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_EXISTS_APP_K8S_LABEL",
Condition: func(deployment *appsv1.Deployment) bool {
_, found := deployment.Spec.Template.Labels["app.kubernetes.io/name"]
return found
},
Message: "There should be an app.kubernetes.io/name label present for the deployment's spec.template",
Level: log.ErrorLevel,
Fix: func(deployment *appsv1.Deployment) bool {
label, found := deployment.Spec.Template.Labels["app"]
if found {
delete(deployment.Spec.Template.Labels, "app")
deployment.Spec.Template.Labels["app.kubernetes.io/name"] = label
return true
}
return false
},
FixDescription: func(deployment *appsv1.Deployment) string {
return fmt.Sprintf("Found app label in deployment %s and used this value to populate the \"app.kubernetes.io/name\" key", deployment.Name)
},
}
// An AppsV1Deployment should be within a namespace
APPSV1_DEPLOYMENT_WITHIN_NAMESPACE = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_WITHIN_NAMESPACE",
Condition: func(deployment *appsv1.Deployment) bool {
return deployment.Namespace != ""
},
Message: "The resource must be within a namespace",
Level: log.ErrorLevel,
}
// An AppsV1Deployment should specify a liveness endpoint
APPSV1_DEPLOYMENT_CONTAINER_EXISTS_LIVENESS = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_CONTAINER_EXISTS_LIVENESS",
Prereqs: []RuleID{"V1_PODSPEC_NON_ZERO_CONTAINERS"},
Condition: func(deployment *appsv1.Deployment) bool {
return deployment.Spec.Template.Spec.Containers[0].LivenessProbe != nil &&
deployment.Spec.Template.Spec.Containers[0].LivenessProbe.Handler.HTTPGet != nil
},
Message: "Expected declaration of liveness probe for the container (livenessProbe)",
Level: log.ErrorLevel,
}
// An AppsV1Deployment should specify a readiness endpoint
APPSV1_DEPLOYMENT_CONTAINER_EXISTS_READINESS = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_CONTAINER_EXISTS_READINESS",
Prereqs: []RuleID{"V1_PODSPEC_NON_ZERO_CONTAINERS"},
Condition: func(deployment *appsv1.Deployment) bool {
return deployment.Spec.Template.Spec.Containers[0].ReadinessProbe != nil &&
deployment.Spec.Template.Spec.Containers[0].ReadinessProbe.Handler.HTTPGet != nil
},
Message: "Expected declaration of readiness probe for the container (readinessProbe)",
Level: log.ErrorLevel,
}
// An AppsV1Deploument should have liveness and readiness endpoints that aren't the same
APPSV1_DEPLOYMENT_LIVENESS_READINESS_NONMATCHING = &AppsV1DeploymentRule{
ID: "APPSV1_DEPLOYMENT_LIVENESS_READINESS_NONMATCHING",
Prereqs: []RuleID{"V1_PODSPEC_NON_ZERO_CONTAINERS", "APPSV1_DEPLOYMENT_CONTAINER_EXISTS_READINESS", "APPSV1_DEPLOYMENT_CONTAINER_EXISTS_LIVENESS"},
Condition: func(deployment *appsv1.Deployment) bool {
container := deployment.Spec.Template.Spec.Containers[0]
return container.LivenessProbe.Handler.HTTPGet.Path != container.ReadinessProbe.Handler.HTTPGet.Path
},
Message: "It's recommended that the readiness and liveness probe endpoints don't match",
Level: log.WarnLevel,
}
// A V1PodSpec should have a non-nil security context
V1_PODSPEC_NON_NIL_SECURITY_CONTEXT = &V1PodSpecRule{
ID: "V1_PODSPEC_NON_NIL_SECURITY_CONTEXT",
Condition: func(podSpec *v1.PodSpec) bool {
return podSpec.SecurityContext != nil
},
Message: "The Security context should be present",
Fix: func(podSpec *v1.PodSpec) bool {
podSpec.SecurityContext = &corev1.PodSecurityContext{}
return true
},
Level: log.ErrorLevel,
FixDescription: func(podSpec *v1.PodSpec) string {
return "Set pod's security context to an empty map"
},
}
// A V1PodSpec should specify runAsNonRoot: true
V1_PODSPEC_RUN_AS_NON_ROOT = &V1PodSpecRule{
ID: "V1_PODSPEC_RUN_AS_NON_ROOT",
Prereqs: []RuleID{"V1_PODSPEC_NON_NIL_SECURITY_CONTEXT"},
Condition: func(podSpec *v1.PodSpec) bool {
return podSpec.SecurityContext.RunAsNonRoot != nil &&
*podSpec.SecurityContext.RunAsNonRoot == true
},
Message: "The Pod Template Spec should enforce that any containers run as non-root users",
Fix: func(podSpec *v1.PodSpec) bool {
runAsNonRoot := true
podSpec.SecurityContext.RunAsNonRoot = &runAsNonRoot
return true
},
Level: log.ErrorLevel,
FixDescription: func(podSpec *v1.PodSpec) string {
return "Set pod's runAsNonRoot key to true"
},
}
// A V1PodSpec should have a user and group ID of 44444
V1_PODSPEC_CORRECT_USER_GROUP_ID = &V1PodSpecRule{
ID: "V1_PODSPEC_CORRECT_USER_GROUP_ID",
Prereqs: []RuleID{"V1_PODSPEC_NON_NIL_SECURITY_CONTEXT"},
Condition: func(podSpec *v1.PodSpec) bool {
return podSpec.SecurityContext.RunAsUser != nil &&
podSpec.SecurityContext.RunAsGroup != nil &&
*podSpec.SecurityContext.RunAsUser == 44444 &&
*podSpec.SecurityContext.RunAsGroup == 44444
},
Message: "The user and group ID of the podspec should be set to 44444",
Fix: func(podSpec *v1.PodSpec) bool {
userId := int64(44444)
groupId := int64(44444)
if podSpec.SecurityContext == nil {
podSpec.SecurityContext = &corev1.PodSecurityContext{}
}
podSpec.SecurityContext.RunAsUser = &userId
podSpec.SecurityContext.RunAsGroup = &groupId
return true
},
Level: log.ErrorLevel,
FixDescription: func(podSpec *v1.PodSpec) string {
return "Set pod's User and Group ID to 44444"
},
}
// A V1PodSpec should have exactly one container
V1_PODSPEC_EXACTLY_1_CONTAINER = &V1PodSpecRule{
ID: "V1_PODSPEC_EXACTLY_1_CONTAINER",
Condition: func(podSpec *v1.PodSpec) bool {
return len(podSpec.Containers) == 1
},
Message: "A Podspec should have exactly 1 container",
Level: log.ErrorLevel,
}
// A V1PodSpec should have a non-zero number of containers
V1_PODSPEC_NON_ZERO_CONTAINERS = &V1PodSpecRule{
ID: "V1_PODSPEC_NON_ZERO_CONTAINERS",
Condition: func(podSpec *v1.PodSpec) bool {
return len(podSpec.Containers) != 0
},
Message: "The Podspec should have at least 1 container defined",
Level: log.ErrorLevel,
}
// A V1Container should have a non-null security context
V1_CONTAINER_EXISTS_SECURITY_CONTEXT = &V1ContainerRule{
ID: "V1_CONTAINER_EXISTS_SECURITY_CONTEXT",
Condition: func(container *v1.Container) bool {
return container.SecurityContext != nil
},
Message: "A v1/Container should have a non-null security context",
Level: log.ErrorLevel,
Fix: func(container *v1.Container) bool {
container.SecurityContext = &v1.SecurityContext{}
return true
},
FixDescription: func(container *v1.Container) string {
return fmt.Sprintf("Set container %s's security context to the empty map", container.Name)
},
}
// A V1Container should not allow privilege escalation
V1_CONTAINER_ALLOW_PRIVILEGE_ESCALATION_FALSE = &V1ContainerRule{
ID: "V1_CONTAINER_ALLOW_PRIVILEGE_ESCALATION_FALSE",
Prereqs: []RuleID{"V1_CONTAINER_EXISTS_SECURITY_CONTEXT"},
Condition: func(container *v1.Container) bool {
return container.SecurityContext.AllowPrivilegeEscalation != nil &&
*container.SecurityContext.AllowPrivilegeEscalation == false
},
Message: "Expected Container's AllowPrivilegeEscalation to be present and set to false",
Level: log.ErrorLevel,
Fix: func(container *v1.Container) bool {
desired := false
container.SecurityContext.AllowPrivilegeEscalation = &desired
return true
},
FixDescription: func(container *v1.Container) string {
return fmt.Sprintf("Set AllowPrivilegeEscalation to false on Container %s", container.Name)
},
}
// A V1Container's image should come from a set of allowed images defined in isImageAllowed
V1_CONTAINER_VALID_IMAGE = &V1ContainerRule{
ID: "V1_CONTAINER_VALID_IMAGE",
Condition: func(container *v1.Container) bool {
return isImageAllowed(container.Image)
},
Message: "The container's image was not from the set of allowed images",
Level: log.ErrorLevel,
}
// A V1Container should have privileged set to false
V1_CONTAINER_PRIVILEGED_FALSE = &V1ContainerRule{
ID: "V1_CONTAINER_PRIVILEGED_FALSE",
Prereqs: []RuleID{"V1_CONTAINER_EXISTS_SECURITY_CONTEXT"},
Condition: func(container *v1.Container) bool {
return container.SecurityContext.Privileged != nil &&
*container.SecurityContext.Privileged == false
},
Message: "Expected Privileged to be present and set to false",
Level: log.ErrorLevel,
Fix: func(container *v1.Container) bool {
privileged := false
container.SecurityContext.Privileged = &privileged
return true
},
FixDescription: func(container *v1.Container) string {
return fmt.Sprintf("Set Privileged key on container %s to false", container.Name)
},
}
// A V1Container should specify Resource Limits and Requests
V1_CONTAINER_EXISTS_RESOURCE_LIMITS_AND_REQUESTS = &V1ContainerRule{
ID: "V1_CONTAINER_EXISTS_RESOURCE_LIMITS_AND_REQUESTS",
Condition: func(container *v1.Container) bool {
return container.Resources.Limits != nil && container.Resources.Requests != nil
},
Message: "Resource limits must be set for the container (resources.requests) and (resources.limits)",
Level: log.ErrorLevel,
}
// A V1Container should make CPU requests that are less than or equal to 100%
V1_CONTAINER_REQUESTS_CPU_REASONABLE = &V1ContainerRule{
ID: "V1_CONTAINER_REQUESTS_CPU_REASONABLE",
Prereqs: []RuleID{"V1_CONTAINER_EXISTS_RESOURCE_LIMITS_AND_REQUESTS"},
Condition: func(container *v1.Container) bool {
// If the container is requesting CPU, it shouldn't be more than 1 unit.
cpuUsage := container.Resources.Requests.Cpu()
return cpuUsage.CmpInt64(1) != 1
},
Message: "You should request less than 1 unit of CPU",
Level: log.ErrorLevel,
}
// A BatchV1Beta1CronJob should be within a namespace
BATCHV1_BETA1_CRONJOB_WITHIN_NAMESPACE = &BatchV1Beta1CronJobRule{
ID: "BATCHV1_BETA1_CRONJOB_WITHIN_NAMESPACE",
Condition: func(job *batchV1beta1.CronJob) bool {
return job.Namespace != ""
},
Message: "The cronjob must be within a namespace",
Level: log.ErrorLevel,
}
// A BatchV1Beta1CronJob should forbid concurrent operations
BATCHV1_BETA1_CRONJOB_FORBID_CONCURRENT = &BatchV1Beta1CronJobRule{
ID: "BATCHV1_BETA1_CRONJOB_FORBID_CONCURRENT",
Condition: func(job *batchV1beta1.CronJob) bool {
return job.Spec.ConcurrencyPolicy == batchV1beta1.ForbidConcurrent
},
Message: "Concurent operations should be forbidden",
Level: log.ErrorLevel,
Fix: func(job *batchV1beta1.CronJob) bool {
job.Spec.ConcurrencyPolicy = batchV1beta1.ForbidConcurrent
return true
},
FixDescription: func(job *batchV1beta1.CronJob) string {
return fmt.Sprintf("Set concurrency policy on cronjob %s to forbid concurrent", job.Name)
},
}
// A BatchV1Job should be within a namespace
BATCHV1_JOB_WITHIN_NAMESPACE = &BatchV1JobRule{
ID: "BATCHV1_JOB_WITHIN_NAMESPACE",
Condition: func(job *batchV1.Job) bool {
return job.Namespace != ""
},
Message: "A Job should have a namespace specified",
Level: log.ErrorLevel,
}
// A BatchV1Job's restart policy should be set to Never
BATCHV1_JOB_RESTART_NEVER = &BatchV1JobRule{
ID: "BATCHV1_JOB_RESTART_NEVER",
Condition: func(job *batchV1.Job) bool {
return len(job.Spec.Template.Spec.RestartPolicy) != 0 &&
job.Spec.Template.Spec.RestartPolicy == "Never"
},
Message: "A Job's restart policy should be set to Never",
Level: log.ErrorLevel,
Fix: func(job *batchV1.Job) bool {
job.Spec.Template.Spec.RestartPolicy = "Never"
return true
},
FixDescription: func(job *batchV1.Job) string {
return fmt.Sprintf("Set job %s's restart policy to Never", job.Name)
},
}
// A BatchV1Job's Time to Live should be set
BATCHV1_JOB_EXISTS_TTL = &BatchV1JobRule{
ID: "BATCHV1_JOB_EXISTS_TTL",
Condition: func(job *batchV1.Job) bool {
return job.Spec.TTLSecondsAfterFinished != nil
},
Message: "A Job should set a TTLSecondsAfterFinished value",
Level: log.ErrorLevel,
}
// A V1Namespace should have a valid DNS name
V1_NAMESPACE_VALID_DNS = &V1NamespaceRule{
ID: "V1_NAMESPACE_VALID_DNS",
Condition: func(namespace *v1.Namespace) bool {
const ACCEPTABLE_DNS = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$`
validDNS := regexp.MustCompile(ACCEPTABLE_DNS)
return validDNS.MatchString(namespace.Name)
},
Message: "A namespace needs to have a valid DNS name",
Level: log.ErrorLevel,
}
// A V1Service should be within a namespace
V1_SERVICE_WITHIN_NAMESPACE = &V1ServiceRule{
ID: "V1_SERVICE_WITHIN_NAMESPACE",
Condition: func(service *v1.Service) bool {
return service.Namespace != ""
},
Message: "A service should have a namespace specified",
Level: log.ErrorLevel,
}
// A V1Service name should be a valid DNS
V1_SERVICE_NAME_VALID_DNS = &V1ServiceRule{
ID: "V1_SERVICE_NAME_VALID_DNS",
Condition: func(service *v1.Service) bool {
const ACCEPTABLE_DNS = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$`
validDNS := regexp.MustCompile(ACCEPTABLE_DNS)
return validDNS.MatchString(service.Name)
},
Level: log.ErrorLevel,
Message: "A service's name needs to be a valid DNS",
}
)
var (
// A unit should contain exactly one namespace.
INTERDEPENDENT_ONE_NAMESPACE = &InterdependentRule{
ID: "INTERDEPENDENT_ONE_NAMESPACE",
Condition: func(resources []*Resource) (bool, []*Resource) {
var namespaces []*Resource
for _, resource := range resources {
if resource.TypeInfo.GetKind() == "Namespace" {
namespaces = append(namespaces, resource)
}
}
return len(namespaces) == 1, namespaces
},
Message: "The unit should contain exactly one namespace",
Level: log.ErrorLevel,
}
// All resources should be under the namespace in the unit
INTERDEPENDENT_MATCHING_NAMESPACE = &InterdependentRule{
ID: "INTERDEPENDENT_MATCHING_NAMESPACE",
Condition: func(resources []*Resource) (bool, []*Resource) {
var namespace *v1.Namespace
for _, resource := range resources {
if ns, ok := resource.Object.(*v1.Namespace); ok {
namespace = ns
break
}
}
if namespace == nil {
return true, nil
}
var wrongNamespaceResources []*Resource
// now test that all ppl are under that namespace
for _, resource := range resources {
if resource.TypeInfo.GetKind() == "Namespace" {
continue
}
if resource.Object.GetNamespace() != namespace.Name {
wrongNamespaceResources = append(wrongNamespaceResources, resource)
}
}
return len(wrongNamespaceResources) == 0, wrongNamespaceResources
},
Message: "All resources must be under the correct namespace",
Level: log.ErrorLevel,
Fix: func(resources []*Resource) bool {
for _, resource := range resources {
if ns, ok := resource.Object.(*v1.Namespace); ok {
name := ns.Name
for _, r := range resources {
if r.Object == ns {
continue
}
r.Object.SetNamespace(name)
}
break
}
}
return true
},
FixDescription: func(resources []*Resource) string {
var namespace string
for _, resource := range resources {
if resource.Object.GetNamespace() != "" {
namespace = resource.Object.GetNamespace()
}
}
return fmt.Sprintf("Set everyone's namespace to %s", namespace)
},
}
// There should be a network policy for the namespace
INTERDEPENDENT_NETWORK_POLICY_REQUIRED = &InterdependentRule{
ID: "INTERDEPENDENT_NETWORK_POLICY_REQUIRED",
Condition: func(resources []*Resource) (bool, []*Resource) {
var namespaces []*v1.Namespace
for _, resource := range resources {
if n, ok := resource.Object.(*v1.Namespace); ok {
namespaces = append(namespaces, n)
}
}
if len(namespaces) != 1 {
return true, nil // cuz test isn't relevant
}
found := false
for _, resource := range resources {
if _, ok := resource.Object.(*networkingV1.NetworkPolicy); ok {
found = true
break
}
}
return found, nil
},
Message: "There must be a network policy defined",
Level: log.ErrorLevel,
}
)
func isImageAllowed(image string) bool {
ALLOWED_DOCKER_REGISTRIES := []string{"277433404353.dkr.ecr.eu-central-1.amazonaws.com"}
for _, r := range ALLOWED_DOCKER_REGISTRIES {
if strings.HasPrefix(image, r) {
return true
}
}
return false
}