Skip to content

Commit 6903795

Browse files
committed
Reduce linting noise with future keywords
1 parent fdfe524 commit 6903795

File tree

10 files changed

+76
-54
lines changed

10 files changed

+76
-54
lines changed

examples/lib/core.rego

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package lib.core
22

3+
import future.keywords.if
4+
35
default is_gatekeeper := false
46

5-
is_gatekeeper {
7+
is_gatekeeper if {
68
has_field(input, "review")
79
has_field(input.review, "object")
810
}
911

10-
resource := input.review.object {
12+
resource := input.review.object if {
1113
is_gatekeeper
1214
}
1315

14-
resource := input {
16+
resource := input if {
1517
not is_gatekeeper
1618
}
1719

@@ -34,24 +36,24 @@ annotations := resource.metadata.annotations
3436

3537
gv := split(apiVersion, "/")
3638

37-
group := gv[0] {
39+
group := gv[0] if {
3840
contains(apiVersion, "/")
3941
}
4042

41-
group := "core" {
43+
group := "core" if {
4244
not contains(apiVersion, "/")
4345
}
4446

4547
version := gv[count(gv) - 1]
4648

47-
has_field(obj, field) {
49+
has_field(obj, field) if {
4850
not object.get(obj, field, "N_DEFINED") == "N_DEFINED"
4951
}
5052

51-
missing_field(obj, field) {
53+
missing_field(obj, field) if {
5254
obj[field] == ""
5355
}
5456

55-
missing_field(obj, field) {
57+
missing_field(obj, field) if {
5658
not has_field(obj, field)
5759
}

examples/lib/core_test.rego

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package lib.core
22

3-
test_not_gk {
3+
import future.keywords.if
4+
5+
test_not_gk if {
46
not is_gatekeeper with input as {"kind": "test"}
57
}
68

7-
test_is_gk {
9+
test_is_gk if {
810
is_gatekeeper with input as {"review": {"object": {"kind": "test"}}}
911
}
1012

11-
test_has_field_pos {
13+
test_has_field_pos if {
1214
has_field({"kind": "test"}, "kind")
1315
}
1416

15-
test_missing_field {
17+
test_missing_field if {
1618
not has_field({"kind": "test"}, "abc")
1719
}

examples/lib/pods.rego

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
package lib.pods
22

3+
import future.keywords.contains
4+
import future.keywords.if
5+
36
import data.lib.core
47

58
default pod := false
69

7-
pod := core.resource.spec.template {
10+
pod := core.resource.spec.template if {
811
pod_templates := ["daemonset", "deployment", "job", "replicaset", "replicationcontroller", "statefulset"]
912
lower(core.kind) == pod_templates[_]
1013
}
1114

12-
pod := core.resource {
15+
pod := core.resource if {
1316
lower(core.kind) == "pod"
1417
}
1518

16-
pod := core.resource.spec.jobTemplate.spec.template {
19+
pod := core.resource.spec.jobTemplate.spec.template if {
1720
lower(core.kind) == "cronjob"
1821
}
1922

20-
containers[container] {
23+
containers contains container if {
2124
keys := {"containers", "initContainers"}
2225
all_containers := [c | some k; keys[k]; c = pod.spec[k][_]]
2326
container := all_containers[_]
2427
}
2528

26-
volumes[pod.spec.volumes[_]]
29+
volumes contains pod.spec.volumes[_]

examples/lib/pods_test.rego

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package lib.pods
22

3-
test_input_as_other {
3+
import future.keywords.if
4+
5+
test_input_as_other if {
46
resource := pod with input as {
57
"kind": "Other",
68
"spec": {"containers": [{}]},
@@ -9,7 +11,7 @@ test_input_as_other {
911
not resource
1012
}
1113

12-
test_input_as_pod {
14+
test_input_as_pod if {
1315
resource := pod with input as {
1416
"kind": "Pod",
1517
"spec": {"containers": [{}]},
@@ -18,7 +20,7 @@ test_input_as_pod {
1820
resource.spec.containers
1921
}
2022

21-
test_input_as_deployment {
23+
test_input_as_deployment if {
2224
resource := pod with input as {
2325
"kind": "Deployment",
2426
"spec": {"template": {"spec": {"containers": [{}]}}},
@@ -27,7 +29,7 @@ test_input_as_deployment {
2729
resource.spec.containers
2830
}
2931

30-
test_input_as_cronjob {
32+
test_input_as_cronjob if {
3133
resource := pod with input as {
3234
"kind": "CronJob",
3335
"spec": {"jobTemplate": {"spec": {"template": {"spec": {"containers": [{}]}}}}},
@@ -36,7 +38,7 @@ test_input_as_cronjob {
3638
resource.spec.containers
3739
}
3840

39-
test_containers {
41+
test_containers if {
4042
podcontainers := containers with input as {
4143
"kind": "Pod",
4244
"spec": {"containers": [{"name": "container"}]},
@@ -45,7 +47,7 @@ test_containers {
4547
podcontainers[_].name == "container"
4648
}
4749

48-
test_volumes {
50+
test_volumes if {
4951
podvolumes := volumes with input as {
5052
"kind": "Pod",
5153
"spec": {"volumes": [{"name": "volume"}]},

examples/lib/psp.rego

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package lib.psps
22

3+
import future.keywords.contains
4+
import future.keywords.if
5+
import future.keywords.in
6+
37
import data.lib.core
48

59
# PodSecurityPolicies are not namespace scoped, so the default PSPs included
610
# in managed Kubernetes offerings cannot be excluded using the normal
711
# methods in Gatekeeper.
8-
is_exception {
12+
is_exception if {
913
exceptions := {
1014
"gce.privileged", # GKE
1115
"gce.persistent-volume-binder", # GKE
@@ -16,10 +20,10 @@ is_exception {
1620
"gce.fluentd-gcp", # GKE
1721
}
1822

19-
core.name == exceptions[_]
23+
core.name in exceptions
2024
}
2125

22-
psps[psp] {
26+
psps contains psp if {
2327
lower(core.kind) = "podsecuritypolicy"
2428
not is_exception
2529
psp = core.resource

examples/lib/psp_test.rego

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package lib.psps
22

3-
test_exception_pos {
3+
import future.keywords.if
4+
5+
test_exception_pos if {
46
is_exception with input as {"metadata": {"name": "gce.privileged"}}
57
}
68

7-
test_exception_neg {
9+
test_exception_neg if {
810
not is_exception with input as {"metadata": {"name": "test"}}
911
}

examples/lib/rbac.rego

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package lib.rbac
22

3+
import future.keywords.if
34
import future.keywords.in
45

56
import data.lib.core
67

7-
rule_has_verb(rule, verb) {
8+
rule_has_verb(rule, verb) if {
89
verbs := ["*", lower(verb)]
910
verbs[_] == lower(rule.verbs[_])
1011
}
1112

12-
rule_has_resource_type(rule, type) {
13+
rule_has_resource_type(rule, type) if {
1314
types := ["*", lower(type)]
1415
types[_] == lower(rule.resources[_])
1516
}
1617

17-
rule_has_resource_name(rule, name) {
18+
rule_has_resource_name(rule, name) if {
1819
name in rule.resourceNames
1920
}
2021

21-
rule_has_resource_name(rule, _) {
22+
rule_has_resource_name(rule, _) if {
2223
core.missing_field(rule, "resourceNames")
2324
}

examples/lib/rbac_test.rego

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
package lib.rbac
22

3-
test_rule_has_verb_with_use {
3+
import future.keywords.if
4+
5+
test_rule_has_verb_with_use if {
46
rule_has_verb({"verbs": ["use"]}, "use")
57
}
68

7-
test_rule_has_verb_with_asterisk {
9+
test_rule_has_verb_with_asterisk if {
810
rule_has_verb({"verbs": ["*"]}, "use")
911
}
1012

11-
test_rule_has_verb_with_list {
13+
test_rule_has_verb_with_list if {
1214
not rule_has_verb({"verbs": ["list"]}, "use")
1315
}
1416

15-
test_rule_has_resource_type_with_pod {
17+
test_rule_has_resource_type_with_pod if {
1618
rule_has_resource_type({"resources": ["Pod"]}, "pod")
1719
}
1820

19-
test_rule_has_resource_type_with_resourceall {
21+
test_rule_has_resource_type_with_resourceall if {
2022
rule_has_resource_type({"resources": ["*"]}, "pod")
2123
}
2224

23-
test_rule_has_resource_type_with_container {
25+
test_rule_has_resource_type_with_container if {
2426
not rule_has_resource_type({"resources": ["Container"]}, "pod")
2527
}
2628

27-
test_rule_has_resource_name_match {
29+
test_rule_has_resource_name_match if {
2830
rule_has_resource_name({"resourceNames": ["test"]}, "test")
2931
}
3032

31-
test_rule_has_resource_name_no_match {
33+
test_rule_has_resource_name_no_match if {
3234
not rule_has_resource_name({"resourceNames": ["test"]}, "wrong")
3335
}
3436

35-
test_rule_has_resource_name_null {
37+
test_rule_has_resource_name_null if {
3638
rule_has_resource_name({}, "wrong")
3739
}

examples/lib/security.rego

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package lib.security
22

3-
dropped_capability(container, cap) {
3+
import future.keywords.if
4+
5+
dropped_capability(container, cap) if {
46
lower(container.securityContext.capabilities.drop[_]) == lower(cap)
57
}
68

7-
dropped_capability(psp, cap) {
9+
dropped_capability(psp, cap) if {
810
lower(psp.spec.requiredDropCapabilities[_]) == lower(cap)
911
}
1012

11-
added_capability(container, cap) {
13+
added_capability(container, cap) if {
1214
lower(container.securityContext.capabilities.add[_]) == lower(cap)
1315
}
1416

15-
added_capability(psp, cap) {
17+
added_capability(psp, cap) if {
1618
lower(psp.spec.allowedCapabilities[_]) == lower(cap)
1719
}
1820

19-
added_capability(psp, cap) {
21+
added_capability(psp, cap) if {
2022
lower(psp.spec.defaultAddCapabilities[_]) == lower(cap)
2123
}

examples/lib/security_test.rego

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
package lib.security
22

3-
test_added_capabilities_container_match {
3+
import future.keywords.if
4+
5+
test_added_capabilities_container_match if {
46
added_capability({"securityContext": {"capabilities": {"add": ["CAP_SYS_ADMIN"]}}}, "CAP_SYS_ADMIN")
57
}
68

7-
test_added_capabilities_container_nomatch {
9+
test_added_capabilities_container_nomatch if {
810
not added_capability({"securityContext": {"capabilities": {"add": ["CAP_SYS_ADMIN"]}}}, "test")
911
}
1012

11-
test_added_capabilities_psp_match {
13+
test_added_capabilities_psp_match if {
1214
added_capability({"spec": {"allowedCapabilities": ["CAP_SYS_ADMIN"]}}, "CAP_SYS_ADMIN")
1315
}
1416

15-
test_added_capabilities_psp_nomatch {
17+
test_added_capabilities_psp_nomatch if {
1618
not added_capability({"spec": {"allowedCapabilities": ["CAP_SYS_ADMIN"]}}, "test")
1719
}
1820

19-
test_dropped_capabilities_container_match {
21+
test_dropped_capabilities_container_match if {
2022
dropped_capability({"securityContext": {"capabilities": {"drop": ["CAP_SYS_ADMIN"]}}}, "CAP_SYS_ADMIN")
2123
}
2224

23-
test_dropped_capabilities_container_nomatch {
25+
test_dropped_capabilities_container_nomatch if {
2426
not dropped_capability({"securityContext": {"capabilities": {"drop": ["CAP_SYS_ADMIN"]}}}, "test")
2527
}
2628

27-
test_dropped_capabilities_psp_match {
29+
test_dropped_capabilities_psp_match if {
2830
dropped_capability({"spec": {"requiredDropCapabilities": ["CAP_SYS_ADMIN"]}}, "CAP_SYS_ADMIN")
2931
}
3032

31-
test_dropped_capabilities_psp_nomatch {
33+
test_dropped_capabilities_psp_nomatch if {
3234
not dropped_capability({"spec": {"requiredDropCapabilities": ["CAP_SYS_ADMIN"]}}, "test")
3335
}

0 commit comments

Comments
 (0)