|
| 1 | +--- |
| 2 | +title: Validating arbitrary data |
| 3 | +date: 2026-02-11T17:38:38-05:00 |
| 4 | +author: Simon Baird |
| 5 | +--- |
| 6 | +In this tutorial we'll introduce some basic Conforma concepts and |
| 7 | +look at examples where Conforma is used to apply policy checks against |
| 8 | +arbitrary input data. |
| 9 | + |
| 10 | +We often use the `ec validate image` command, which fetches and |
| 11 | +verifies an image's SLSA provenance attestations, then applies policy checks |
| 12 | +against them. But Conforma can work just as well with any kind of input using |
| 13 | +the `ec validate input` command, and in fact that is a useful way to |
| 14 | +demonstrate some Conforma ideas and techniques. |
| 15 | + |
| 16 | +## ec validate input |
| 17 | + |
| 18 | +Conforma can perform policy checks on arbitrary data with `ec |
| 19 | +validate input`. Let's try an example. |
| 20 | + |
| 21 | + |
| 22 | +A simple data file: |
| 23 | + |
| 24 | +```yaml |
| 25 | +# file: input.yaml |
| 26 | + |
| 27 | +animals: |
| 28 | + - name: Charlie |
| 29 | + species: dog |
| 30 | + - name: Luna |
| 31 | + species: cat |
| 32 | +``` |
| 33 | +
|
| 34 | +
|
| 35 | +A minimal Conforma policy defined in Rego: |
| 36 | +
|
| 37 | +```rego |
| 38 | +# file: no-cats/main.rego |
| 39 | + |
| 40 | +package main |
| 41 | + |
| 42 | +# METADATA |
| 43 | +# title: No cats |
| 44 | +# description: Disallow felines. |
| 45 | +# custom: |
| 46 | +# short_name: no_cats |
| 47 | +# solution: Ensure no cats are present in the animal list! |
| 48 | +# |
| 49 | +deny contains result if { |
| 50 | + some animal in input.animals |
| 51 | + animal.species == "cat" |
| 52 | + result := {"code": "main.no_cats", "msg": "No cats allowed!"} |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +To use that policy, Conforma needs a policy.yaml file specifying a source: |
| 57 | + |
| 58 | +```yaml |
| 59 | +# file: policy.yaml |
| 60 | + |
| 61 | +sources: |
| 62 | + - policy: |
| 63 | + - ./no-cats |
| 64 | +``` |
| 65 | +
|
| 66 | +
|
| 67 | +Now we can run Conforma like this: |
| 68 | +
|
| 69 | +```ec |
| 70 | +$ ec validate input --file input.yaml --policy policy.yaml |
| 71 | +Success: false |
| 72 | +Result: FAILURE |
| 73 | +Violations: 1, Warnings: 0, Successes: 0 |
| 74 | +Input File: input.yaml |
| 75 | + |
| 76 | +Results: |
| 77 | +✕ [Violation] main.no_cats |
| 78 | + FilePath: input.yaml |
| 79 | + Reason: No cats allowed! |
| 80 | + |
| 81 | +Error: success criteria not met |
| 82 | + |
| 83 | +``` |
| 84 | +
|
| 85 | +
|
| 86 | +## Using --info for more detailed output |
| 87 | +
|
| 88 | +The metadata associated with the policy rule is important for |
| 89 | +Conforma. Adding the `--info` flag will use the metadata to show more |
| 90 | +details about the violation: |
| 91 | + |
| 92 | +```ec |
| 93 | +$ ec validate input --file input.yaml --policy policy.yaml --info |
| 94 | +Success: false |
| 95 | +Result: FAILURE |
| 96 | +Violations: 1, Warnings: 0, Successes: 0 |
| 97 | +Input File: input.yaml |
| 98 | +
|
| 99 | +Results: |
| 100 | +✕ [Violation] main.no_cats |
| 101 | + FilePath: input.yaml |
| 102 | + Reason: No cats allowed! |
| 103 | + Title: No cats |
| 104 | + Description: Disallow felines. To exclude this rule add "main.no_cats" to the `exclude` section of the policy configuration. |
| 105 | + Solution: Ensure no cats are present in the animal list! |
| 106 | + |
| 107 | +Error: success criteria not met |
| 108 | + |
| 109 | +``` |
| 110 | +
|
| 111 | +
|
| 112 | +## Using --show-successes to show passing checks |
| 113 | +
|
| 114 | +Let's "fix" the violation and run it again: |
| 115 | +
|
| 116 | +```ec |
| 117 | +$ sed -i "s/cat/rabbit/" input.yaml |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +```yaml |
| 122 | +# file: input.yaml |
| 123 | + |
| 124 | +animals: |
| 125 | + - name: Charlie |
| 126 | + species: dog |
| 127 | + - name: Luna |
| 128 | + species: rabbit |
| 129 | +``` |
| 130 | +
|
| 131 | +```ec |
| 132 | +$ ec validate input --file input.yaml --policy policy.yaml --info |
| 133 | +Success: true |
| 134 | +Result: SUCCESS |
| 135 | +Violations: 0, Warnings: 0, Successes: 1 |
| 136 | +Input File: input.yaml |
| 137 | + |
| 138 | + |
| 139 | +``` |
| 140 | +
|
| 141 | +
|
| 142 | +By default there's not much output on success, but we can add |
| 143 | +the `--show-successes` flag to change that: |
| 144 | + |
| 145 | +```ec |
| 146 | +$ ec validate input --file input.yaml --policy policy.yaml --info --show-successes |
| 147 | +Success: true |
| 148 | +Result: SUCCESS |
| 149 | +Violations: 0, Warnings: 0, Successes: 1 |
| 150 | +Input File: input.yaml |
| 151 | +
|
| 152 | +Results: |
| 153 | +✓ [Success] main.no_cats |
| 154 | + FilePath: input.yaml |
| 155 | + Title: No cats |
| 156 | + Description: Disallow felines. |
| 157 | +
|
| 158 | +
|
| 159 | +``` |
| 160 | + |
| 161 | + |
| 162 | +(Turn rabbits back into cats for the next step): |
| 163 | + |
| 164 | +```ec |
| 165 | +$ sed -i "s/rabbit/cat/" input.yaml |
| 166 | +
|
| 167 | +``` |
| 168 | + |
| 169 | + |
| 170 | +## Warnings |
| 171 | + |
| 172 | +We can use "warn" to produce a warning instead of a violation: |
| 173 | + |
| 174 | +(We'll append to the existing file here.) |
| 175 | + |
| 176 | +```rego |
| 177 | +# file: no-cats/main.rego |
| 178 | +# from-line: 16 |
| 179 | +
|
| 180 | +# METADATA |
| 181 | +# title: Charlie warning |
| 182 | +# description: Charlie is a troublemaker! |
| 183 | +# custom: |
| 184 | +# short_name: charlie_watch |
| 185 | +# solution: Keep a close eye on Charlie. |
| 186 | +# |
| 187 | +warn contains result if { |
| 188 | + some animal in input.animals |
| 189 | + animal.name == "Charlie" |
| 190 | + result := {"code": "main.charlie_watch", "msg": "Charlie is here"} |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +Notice we now see the warning in the output: |
| 195 | + |
| 196 | +```ec |
| 197 | +$ ec validate input --file input.yaml --policy policy.yaml --info --show-successes |
| 198 | +Success: false |
| 199 | +Result: FAILURE |
| 200 | +Violations: 1, Warnings: 1, Successes: 0 |
| 201 | +Input File: input.yaml |
| 202 | +
|
| 203 | +Results: |
| 204 | +✕ [Violation] main.no_cats |
| 205 | + FilePath: input.yaml |
| 206 | + Reason: No cats allowed! |
| 207 | + Title: No cats |
| 208 | + Description: Disallow felines. To exclude this rule add "main.no_cats" to the `exclude` section of the policy configuration. |
| 209 | + Solution: Ensure no cats are present in the animal list! |
| 210 | + |
| 211 | +› [Warning] main.charlie_watch |
| 212 | + FilePath: input.yaml |
| 213 | + Reason: Charlie is here |
| 214 | + Title: Charlie warning |
| 215 | + Description: Charlie is a troublemaker! |
| 216 | + Solution: Keep a close eye on Charlie. |
| 217 | + |
| 218 | +Error: success criteria not met |
| 219 | + |
| 220 | +``` |
| 221 | +
|
| 222 | +Warnings are considered non-blocking. |
| 223 | +
|
| 224 | +
|
| 225 | +## Adding more detail to the violation reason |
| 226 | +
|
| 227 | +Rego is an expressive and capable language so we can easily add more detail to the violation |
| 228 | +reason. For example: |
| 229 | +
|
| 230 | +```rego |
| 231 | +# file: no-cats/main.rego |
| 232 | +# from-line: 10 |
| 233 | + |
| 234 | +deny contains result if { |
| 235 | + some animal in input.animals |
| 236 | + animal.species == "cat" |
| 237 | + result := {"code": "main.no_cats", "msg": sprintf("A cat named %s was found!", [animal.name])} |
| 238 | +} |
| 239 | +``` |
| 240 | +```ec |
| 241 | +$ ec validate input --file input.yaml --policy policy.yaml |
| 242 | +Success: false |
| 243 | +Result: FAILURE |
| 244 | +Violations: 1, Warnings: 1, Successes: 0 |
| 245 | +Input File: input.yaml |
| 246 | +
|
| 247 | +Results: |
| 248 | +✕ [Violation] main.no_cats |
| 249 | + FilePath: input.yaml |
| 250 | + Reason: A cat named Luna was found! |
| 251 | +
|
| 252 | +› [Warning] main.charlie_watch |
| 253 | + FilePath: input.yaml |
| 254 | + Reason: Charlie is here |
| 255 | +
|
| 256 | +Error: success criteria not met |
| 257 | +
|
| 258 | +``` |
| 259 | + |
| 260 | + |
| 261 | +If there are multiple cats, we now get multiple different violations: |
| 262 | + |
| 263 | +```yaml |
| 264 | +# file: input.yaml |
| 265 | + |
| 266 | +animals: |
| 267 | + - name: Charlie |
| 268 | + species: dog |
| 269 | + - name: Luna |
| 270 | + species: cat |
| 271 | + - name: Fluffy |
| 272 | + species: cat |
| 273 | +``` |
| 274 | +
|
| 275 | +```ec |
| 276 | +$ ec validate input --file input.yaml --policy policy.yaml |
| 277 | +Success: false |
| 278 | +Result: FAILURE |
| 279 | +Violations: 2, Warnings: 1, Successes: 0 |
| 280 | +Input File: input.yaml |
| 281 | + |
| 282 | +Results: |
| 283 | +✕ [Violation] main.no_cats |
| 284 | + FilePath: input.yaml |
| 285 | + Reason: A cat named Fluffy was found! |
| 286 | + |
| 287 | +✕ [Violation] main.no_cats |
| 288 | + FilePath: input.yaml |
| 289 | + Reason: A cat named Luna was found! |
| 290 | + |
| 291 | +› [Warning] main.charlie_watch |
| 292 | + FilePath: input.yaml |
| 293 | + Reason: Charlie is here |
| 294 | + |
| 295 | +Error: success criteria not met |
| 296 | + |
| 297 | +``` |
| 298 | +
|
| 299 | +That's about it for this lesson. Hopefully you now have a better |
| 300 | +idea of what Conforma policies look like, and what kind of output Conforma |
| 301 | +produces. |
| 302 | +
|
| 303 | +Before we wrap this up let's look at two extra tips which should be |
| 304 | +useful when integrating these kind of policy checks into a CI or build system: |
| 305 | +
|
| 306 | +
|
| 307 | +## Machine readable output |
| 308 | +
|
| 309 | +Text output is the default, but you can also output json or yaml, |
| 310 | +which includes some additional information not included in the text output: |
| 311 | +
|
| 312 | +```ec |
| 313 | +$ ec validate input --file input.yaml --policy policy.yaml --info --output json | fold -s -w 400 |
| 314 | +Error: success criteria not met |
| 315 | +{"success":false,"filepaths":[{"filepath":"input.yaml","violations":[{"msg":"A cat named Fluffy was found!","metadata":{"code":"main.no_cats","description":"Disallow felines. To exclude this rule add \"main.no_cats\" to the `exclude` section of the policy configuration.","solution":"Ensure no cats are present in the animal list!","title":"No cats"}},{"msg":"A cat named Luna was |
| 316 | +found!","metadata":{"code":"main.no_cats","description":"Disallow felines. To exclude this rule add \"main.no_cats\" to the `exclude` section of the policy configuration.","solution":"Ensure no cats are present in the animal list!","title":"No cats"}}],"warnings":[{"msg":"Charlie is here","metadata":{"code":"main.charlie_watch","description":"Charlie is a troublemaker!","solution":"Keep a close |
| 317 | +eye on Charlie.","title":"Charlie warning"}}],"successes":null,"success":false,"success-count":0}],"policy":{"sources":[{"policy":["./no-cats"]}]},"ec-version":"v0.8.97","effective-time":"2026-02-11T22:38:39.207175414Z"} |
| 318 | + |
| 319 | +``` |
| 320 | + |
| 321 | + |
| 322 | +## "Strict" vs "non-strict" |
| 323 | + |
| 324 | +By default we produce a non-zero exit code if there are any |
| 325 | +violations, which is useful to interrupt a script or a CI task. You can change |
| 326 | +that behavior if you need to with `--strict=false:` |
| 327 | + |
| 328 | +```ec |
| 329 | +$ ec validate input --file input.yaml --policy policy.yaml > output.txt; echo "Exit code: $?"; head -3 output.txt |
| 330 | +Error: success criteria not met |
| 331 | +Exit code: 1 |
| 332 | +Success: false |
| 333 | +Result: FAILURE |
| 334 | +Violations: 2, Warnings: 1, Successes: 0 |
| 335 | +
|
| 336 | +``` |
| 337 | + |
| 338 | +```ec |
| 339 | +$ ec validate input --file input.yaml --policy policy.yaml --strict=false > output.txt; echo "Exit code: $?"; head -3 output.txt |
| 340 | +Exit code: 0 |
| 341 | +Success: false |
| 342 | +Result: FAILURE |
| 343 | +Violations: 2, Warnings: 1, Successes: 0 |
| 344 | +
|
| 345 | +``` |
| 346 | + |
0 commit comments