Skip to content

new(nok8s): json schema validation for tracingpolicies - #5322

Open
FedeDP wants to merge 6 commits into
mainfrom
new/nok8s-jsonschema-validation
Open

new(nok8s): json schema validation for tracingpolicies#5322
FedeDP wants to merge 6 commits into
mainfrom
new/nok8s-jsonschema-validation

Conversation

@FedeDP

@FedeDP FedeDP commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

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 kubeconform make target.

New dependency: github.com/santhosh-tekuri/jsonschema/v6; only used in nok8s build.
Size change:

  • main: 49532
  • HEAD: 50752

~800K.

Most of the line changes come from:

  • the new pkg/tracingpolicy/schemas/ json schemas
  • the new vendored dependency

Changelog

new(nok8s): json schema + CEL validation for tracingpolicies

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch 4 times, most recently from 42edb96 to 964380b Compare July 22, 2026 14:22
@FedeDP FedeDP added release-note/minor This PR introduces a minor user-visible change kind/feature This introduces a new functionality labels Jul 22, 2026
@FedeDP

FedeDP commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Run static checks / build-nok8s (pull_request)Failing after 1m

So, this is failing because in the tetragon-nok8s target i clean up the pkg/tracingpolicy/schemas/ folder, but then tetra-nok8s fails to build because go:embed can't find the file.
2 possibilities:

  • we keep the install/kubernetes tetragon/schemas/ folder alive (and we re-generate it in generate target); then both tetragon-nok8s and tetra-nok8s will rely upon a pkg/tracingpolicy/schemas/ target that just cp -r install/kubernetes/tetragon/schemas/ pkg/tracingpolicy/schemas/; finally, we gitignore pkg/tracingpolicy/schemas/.
  • both tetra-nok8s and tetragon-nok8s will depend upon pkg/tracingpolicy/schemas/ target that generates json schemas from crds, and both targets clean pkg/tracingpolicy/schemas/ folder up once they are done

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: make generate will re-generate the files directly in the desired folder (pkg/tracingpolicy/schemas/) and they are then versioned. This allows go test -tags nok8s or just go build -tags nok8s to work like a charm since the required files are correctly there.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch 3 times, most recently from 1554289 to 24206d3 Compare July 23, 2026 07:14
@netlify

netlify Bot commented Jul 23, 2026

Copy link
Copy Markdown

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 66da2ba
🔍 Latest deploy log https://app.netlify.com/projects/tetragon/deploys/6a677bf77e1d9b0007a5fa6d
😎 Deploy Preview https://deploy-preview-5322--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from 24206d3 to 0d2788f Compare July 23, 2026 07:21
@FedeDP

FedeDP commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Example output from feeding a wrong syntax policy:

level=error msg="Failed to execute tetragon" error="failed to validate TracingPolicy: jsonschema validation failed with 'file:///tracingpolicy#'\n- at '/spec': additional properties 'lsmhoooks' not allowed"

In main, same policy:

level=info msg="Added TracingPolicy with success" TracingPolicy=/home/fdipierr/pol-lsm-wrong.yaml metadata.namespace="" metadata.name=protection-policy-cve-2024-21626

Policy file (note lsmhoooks :D )

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

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from 0d2788f to 76dae24 Compare July 23, 2026 10:23
@FedeDP
FedeDP marked this pull request as ready for review July 23, 2026 12:10
@FedeDP
FedeDP requested a review from a team as a code owner July 23, 2026 12:10
@FedeDP
FedeDP requested review from andrewstrohman and kkourt July 23, 2026 12:10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread pkg/tracingpolicy/nok8s.go Outdated
Comment thread pkg/tracingpolicy/nok8s.go Outdated
@andrewstrohman

Copy link
Copy Markdown
Contributor

It might be worth doing an explicit validation for drift between the JSON schema and the CRDs in CI.

@FedeDP

FedeDP commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

It might be worth doing an explicit validation for drift between the JSON schema and the CRDs in CI.

This should never happen since make generate takes care of rebuilding both.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from 76dae24 to 0834ad5 Compare July 24, 2026 07:50
@FedeDP
FedeDP requested a review from mtardy July 24, 2026 07:50

@kkourt kkourt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/tracingpolicy/nok8s.go
@FedeDP

FedeDP commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Is this something that we plan to do?

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.

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 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?

Comment thread pkg/tracingpolicy/nok8s.go Outdated
Comment thread go.mod
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how much do we trust this new dep?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems widely used and updated. But can't really say :/

Comment on lines -116 to +119
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/tracingpolicy/nok8s.go
@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from 0834ad5 to ec01176 Compare July 27, 2026 13:46
@kkourt

kkourt commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Is this something that we plan to do?

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.

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 am not sure, it quickly becomes risky to rely upon the double validation (sooner or later we would forget to add it)

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).

and also why pay the perf penalty (for validation) in k8s builds when k8s already validates for us?

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.

@FedeDP

FedeDP commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Note that my statement is qualified by "if we can't do it" (i.e., translate the CEL validation to a json schema).

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.

@kkourt

kkourt commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Note that my statement is qualified by "if we can't do it" (i.e., translate the CEL validation to a json schema).

I think we should be able to do it, but it's a bit complex

OK, good enough for me :)

i can give it a shot and keep this PR on hold until i find the right way to achieve that if you prefer.

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.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch 2 times, most recently from 2c2e738 to a3f1776 Compare July 27, 2026 15:27
@FedeDP

FedeDP commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from a3f1776 to f76b9ad Compare July 27, 2026 15:35
@FedeDP
FedeDP marked this pull request as draft July 27, 2026 15:37
@FedeDP

FedeDP commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Moving to draft to fix CEL stuff.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch 3 times, most recently from 6033959 to a6b9a67 Compare July 27, 2026 15:54
@FedeDP
FedeDP marked this pull request as ready for review July 27, 2026 16:56
@FedeDP
FedeDP requested a review from a team as a code owner July 28, 2026 06:59
@FedeDP
FedeDP requested a review from olsajiri July 28, 2026 06:59
@FedeDP

FedeDP commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Fixed CEL stuff.

@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch 2 times, most recently from 50a4218 to 9d1c7d1 Compare July 28, 2026 07:58
Comment thread install/kubernetes/Makefile Outdated
FedeDP added 6 commits July 29, 2026 09:14
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>
@FedeDP
FedeDP force-pushed the new/nok8s-jsonschema-validation branch from 9d1c7d1 to 262eca7 Compare July 29, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature This introduces a new functionality release-note/minor This PR introduces a minor user-visible change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants