You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -43,17 +43,18 @@ In order to ease keeping track of those changes, Preflight packages have a versi
43
43
44
44
### The minimal _policy manifest_
45
45
46
-
Let's just write the minimal _policy manifest_possible.
46
+
Let's write a minimal _policy manifest_to get started.
47
47
48
48
First, create a directory for this new package. We are going to create this new package under the `examples.jetstack.io` namespace, and we are going to name it `podsbestpractices`.
49
49
50
50
Then create the `policy-manifest.yaml` file. The following fields are mandatory:
51
51
52
-
-`schema-version`: indicates which schema is being used for the _policy manifest_. For the moment, there is only version `1.0.0`.
52
+
-`schema-version`: indicates which schema is being used for the _policy manifest_. For now, there is only version: `1.0.0`.
53
+
-`namespace`, `id`, and `package-version`: these properties identify the package. `namespace` must be a FQDN and it is encouraged that `package-version` uses [semver](https://semver.org).
54
+
-`root-query`: Name of the Rego package containing the rules backing the
55
+
package (see below).
53
56
54
-
-`namespace`, `id`, and `package-version`: these properties identify the package. `namespace` must be a FQDN and it is encouraged that `package-version` uses semver.
55
-
56
-
Then, you should also declare the _data-gatherers_ that your rules are going to need. For this example, let's just use `k8s/pods`.
57
+
Then, you should also declare the _data-gatherers_ that your rules are going to need. For this example, we only need one, `k8s/pods`.
57
58
58
59
Finally, it's time to declare the rules for the policy. Rules are organized into sections. Every section has an ID, a name, and a description. Also, every rule has its own ID, name, and description. Additionally, rules can have other metadata like a remediation advice or a set of related links.
59
60
@@ -70,18 +71,18 @@ root-query: "data.pods" # the concept of `root-query` is explained later in this
70
71
data-gatherers:
71
72
- k8s/pods
72
73
sections:
73
-
- id: images
74
-
name: Images
75
-
description: "Restrictions over the images."
76
-
rules:
77
-
- id: tag_not_latest
78
-
name: "Tag is not latest"
79
-
description: >
80
-
Avoid using "latest" as tag for the image since.
81
-
remediation: >
82
-
Change your manifest and edit the Pod template so the image is pinned to a certain tag.
@@ -90,142 +91,107 @@ In the previous section, we created the _policy manifest_, which contains a huma
90
91
91
92
### The Rego package
92
93
93
-
Preflight relies on Open Policy Agent as the policy engine. Rego is OPA's language to define policies. You can find a comprenhensive[documentation](https://www.openpolicyagent.org/docs/latest/policy-language/).
94
+
Preflight relies on Open Policy Agent as the policy engine. Rego is OPA's language to define policies. You can find their comprehensive[documentation here](https://www.openpolicyagent.org/docs/latest/policy-language/).
94
95
95
96
You can have multiple Rego files inside the directory of a Preflight package. All the Rego rules corresponding to the _policy manifest_ rules must be in the same Rego package, and that package must be indicated in the _policy manifest_ using the `root-query` property.
96
97
97
98
For instance, this snippet shows an arbitrary Rego rule in a package named `podsbestpractices`:
98
99
99
100
```
100
-
package pods
101
+
package podsbestpractices
101
102
102
103
import input["k8s/pods"] as pods
103
104
104
-
preflight_tag_not_latest {
105
+
preflight_tag_not_latest[message] {
105
106
true
107
+
message := "true was found to be true"
106
108
}
107
109
```
108
110
109
111
As you can identify, the Rego package for that policy is `pods`. In this case, OPA's `root-query` is `data.pods`, and that is why in the previous section, `policy-manifest.yaml` contains `root-query: "data.pods"`.
110
112
111
113
### Writing Rego rules
112
114
113
-
Rego can be challenging at the beginning because it does not behaves like a traditional programming language. It is strongly recommended to read ["The Basics"](https://www.openpolicyagent.org/docs/latest/policy-language/#the-basics). Also, it is useful to have the [language refence](https://www.openpolicyagent.org/docs/latest/policy-reference/) at hand.
114
-
115
-
You will get faster as you write more Rego rules. In order to speed up this process, it's best to write tests for your rules, even if you think they are not needed. It means you can iterate fast while writing rules and make sure the rules are doing what you intended. It is conventional to name the test files for `policy.rego` as `policy_test.rego`.
115
+
Rego is a declarative language and has a bit of a learning curve. It is strongly recommended to read ["The Basics"](https://www.openpolicyagent.org/docs/latest/policy-language/#the-basics). Also, it is useful to have the [language reference](https://www.openpolicyagent.org/docs/latest/policy-reference/) to hand.
116
116
117
+
In order to speed up the process of writing Rego rules, it's best to write tests. It means you can iterate fast while writing rules and make sure the rules are doing what you intended. It is conventional to name the test files for `policy.rego` as `policy_test.rego`.
117
118
118
119
This example contains the definition for the `tag_no_latest` rule. As you can see, there is the convention within Preflight to add `preflight_` as prefix to the rule ID when that is written in Rego (related issue #27).
message := sprintf("container '%s' in pod '%s' in namespace '%s' is missing an explicit image tag", [container.name, pod.metadata.name, pod.metadata.namespace])
155
138
}
156
139
```
157
140
158
141
### Testing Rego
159
142
160
-
As mentioned before, it is very useful to [write tests for the Rego rules](https://www.openpolicyagent.org/docs/latest/policy-testing/).
143
+
As mentioned before, it is very useful to [write tests for your Rego rules](https://www.openpolicyagent.org/docs/latest/policy-testing/).
161
144
162
-
This snippet contains a testsuite for the previous Rego code.
145
+
This snippet contains a test case for the previous Rego code.
0 commit comments