new(nok8s): json schema validation for tracingpolicies - #5322
Conversation
42edb96 to
964380b
Compare
So, this is failing because in the
For now, i implement 1, but am open to switch to 2 if requested. EDIT: in the end, i chose to follow what we already do for crds: |
1554289 to
24206d3
Compare
✅ Deploy Preview for tetragon ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
24206d3 to
0d2788f
Compare
|
Example output from feeding a wrong syntax policy: In main, same policy: Policy file (note apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "protection-policy-cve-2024-21626"
spec:
lsmhoooks:
- hook: "bprm_check_security"
args:
- index: 0 # struct linux_binprm *bprm
type: "path"
resolve: "mm.owner.fs.pwd"
selectors:
- matchArgs:
- index: 0
operator: "Prefix"
values:
- "/sys/fs/cgroup"
matchActions:
- action: Post
message: cve-2024-21626 |
0d2788f to
76dae24
Compare
There was a problem hiding this comment.
I confirmed that this came from the vendor's repo, and it's not something that we created by accident. (I guess our CI would catch it, if it came from us).
|
It might be worth doing an explicit validation for drift between the JSON schema and the CRDs in CI. |
This should never happen since |
76dae24 to
0834ad5
Compare
kkourt
left a comment
There was a problem hiding this comment.
Note: x-kubernetes-validations cel expressions are not supported; we would need to extend the json schema validation and manually support them; it's not super easy to do, but it would allow us to cleanup some code.
Is this something that we plan to do?
Because if we can't do it, then I think the best option would be to do just duplicate the validation logic in go since we are using CEL expressions extensively in our spec.
I think we might want to commit to do that in the future; unfortunately, it is a bit complex and would take quite a bit of time.
I am not sure, it quickly becomes risky to rely upon the double validation (sooner or later we would forget to add it) and also why pay the perf penalty (for validation) in k8s builds when k8s already validates for us? |
| github.com/prometheus/client_model v0.6.2 | ||
| github.com/prometheus/common v0.70.0 | ||
| github.com/prometheus/procfs v0.21.1 | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 |
There was a problem hiding this comment.
how much do we trust this new dep?
There was a problem hiding this comment.
Seems widely used and updated. But can't really say :/
| kubeconform: ## Validate Helm chart using kubeconform | ||
| kubeconform: | ||
| mkdir -p $(JSON_SCHEMAS)/ $(SCHEMAS_CACHE)/ | ||
| $(PYTHON) /bin/bash -c "pip install pyyaml && python /code/install/kubernetes/openapi2jsonschema.py /code/$(CRDS_RELATIVE_DIR)/*" | ||
| mv $(ROOT_DIR)/install/kubernetes/*-cilium.io.json $(JSON_SCHEMAS)/ | ||
| kubeconform: $(TETRAGON_CHART)/schemas ## Validate Helm chart using kubeconform |
There was a problem hiding this comment.
could you explain a little bit in the git commit log the why of these changes? I'm not super familiar on why we were doing all this before
There was a problem hiding this comment.
I'm not super familiar on why we were doing all this before
I'm not either :) i just split the schemas generation code from being part of the kubeconform target to its own.
(I just restored the SCHEMAS_CACHE thing because it's still useful).
I expanded the commit message to be more precise :)
Also, note that i tested make kubeconform and it works fine.
0834ad5 to
ec01176
Compare
What's the alternative? Note that my statement is qualified by "if we can't do it" (i.e., translate the CEL validation to a json schema).
I would, also, expect that performance overhead of this is small. Indeed, I would expect that it's negligible compared to how long it takes to load a policy. |
I think we should be able to do it, but it's a bit complex; i can give it a shot and keep this PR on hold until i find the right way to achieve that if you prefer. |
OK, good enough for me :)
IMO, no need to put the PR on hold. Let's just go over the review process as normal and merge it (assuming there are on blockers). Once merged, please create an issue to track the followup work. |
2c2e738 to
a3f1776
Compare
|
@kkourt ooops i think i am really near to make CEL validation work without many changes 🚀 ; pushed the changes in their own commit to keep them separated with their own commit message. It was easier than i thought, i just needed to dig a bit in the jsonschema library. |
a3f1776 to
f76b9ad
Compare
|
Moving to draft to fix CEL stuff. |
6033959 to
a6b9a67
Compare
|
Fixed CEL stuff. |
50a4218 to
9d1c7d1
Compare
We already generated schemas as part of the `kubeconform` make target; move the schemas generation to their own target, and let `kubeconform` depend upon the new target. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Also, run `make generate` to generate them. Next commit will use the schemas through `go:embed` to validate policies. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Prior to this change, feeding a wrong-syntax policy spec to nok8s build would've accepted it just fine. After this change, syntax errors are caught and correctly thrown. Note that `x-kubernetes-validations` CEL expressions validation will come in the follow up commit. New dependency: github.com/santhosh-tekuri/jsonschema/v6. Impact of the change: * Binary size: * main: `49532K` * HEAD: `50752K` * Running memory: * main: `70252K` * HEAD: `73168` Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
We register a `vocabulary` for the `jsonschema.Compiler` that basically maps each `"x-kubernetes-validations"` entry to a function that loads the CEL `rule` and `message`, compiles the rule and store the resulting CEL program. The resulting `celSchema` will then validate all stored CEL programs during the jsonschema validation. `k8s_xvalidation_test` tests are not `k8s` builds specific anymore. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Before this change, we would've tried to evaluate `!return` even when the rule did not include any `return` statement, leading to a warning in the new nok8s `celSchema.Validate()`. Consequently, bumped `CustomResourceDefinitionSchemaVersion`. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
The case is already covered by `tracingpolicy/k8s_xvalidation_test` now. Also, cleaned up unused `build.K8sEnabled()` helper. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
9d1c7d1 to
262eca7
Compare
Description
Since in nok8s builds we can't rely upon kubernetes validation, we were lacking any sort of validation for the tracing policy yaml object.
This patch introduces a validation based upon the json schema of the crds that were previously generated and used only in the
kubeconformmake target.New dependency: github.com/santhosh-tekuri/jsonschema/v6; only used in nok8s build.
Size change:
~800K.
Most of the line changes come from:
pkg/tracingpolicy/schemas/json schemasChangelog