Custom validators #42
Replies: 3 comments 6 replies
-
Thanks @WitoDelnat - i'm trying to understand what the actual workflow/steps would be for both a creator of a plugin and a consumer.. could you provide some example? i.e. "Step by step instructions to create a new validator that can be used by others, "Step by step instructions to use a custom validator when validating resources with monokle cli"? Also - as an additional layer of security on the node side - could we consider using https://github.com/patriksimek/vm2 for sandboxing the actual plugin (we already use vm2 in Monokle for sandboxing template code)? or does that not play nicely with your proposed approach!? |
Beta Was this translation helpful? Give feedback.
-
I'm creating a separate thread for security 👇
|
Beta Was this translation helpful? Give feedback.
-
Would it make sense to add the optional possibility for validators to provide "quick-fixes" for the errors they find? Most (all?) current OPA validations could be fixed automatically by adding/setting properties/etc - which would be easy to add and extremely helpful in scenarios like code-scanning (where we could generate PRs based on quick-fixes) and in editors where we could have keyboard-actions for applying quick-fixes!? |
Beta Was this translation helpful? Give feedback.
-
This describes (extensively) how we could approach custom validators.
Requirements
Proposal
1. Create plugin: custom validation API
This is a temporary poc repository: https://github.com/WitoDelnat/bookish-octo-bassoon
There is a working POC with following interface:
Besides metadata, you will see a validate function that has
RuleContext
andRuleApi
parameters:note: I think we should not expose our Resource directly but instead
resource = { $id: resource.id, ...resource.content}
.Important to note is that we the plugin should be the default export for this plugin module.
2. Create bundle: compile typescript and rollup ESM bundler
The following configuration is used - it's quite basic.
3. Serve the plugin
See below how this would eventually be done but for this poc it's done simple..
3. Load the plugin
Different implementation is needed for NodeJs and Browser. This is because NodeJs does not yet support importing ESM modules using HTTP (docs).
Browser
NodeJs
There are two options to work around the restriction:
4. Configure plugins and validate resources
No change needed. Current validation configuration is powerful enough.
Miscellaneous
Codegen types from Kubernetes (core + CRDs)
I've written a script that generates types and typeguards to go from
resource.content: any
to a fully typed object. This will give intellisense to anyone trying to write a plugin which is a drastic DX improvement.CLI to manage plugin lifecycle
This would put all pieces together. Below is a basic concept but it has to be iterated on.
Maybe this is not needed for now as some npm scripts and a submit form would do the trick as well?
Security
For now a combination of manual review and signature should be sufficient? I doubt there will be so many submissions that manual review will be a bottleneck and it's the simplest solution. The signature can be a shasum of the bundle which we then use together with a HTTP Signature header.
Maybe shasum is not needed when we upload manually to our own infra?
Tasks to be done
Plugins [5-6 days]
--validator
and later can be extended with a flag--template
. For now this repository can include the rollup config, codegen script and everything just plainly put in it. Let's get some usage before we do a touch-up of internals. [POC repo available - 1 day]npx create monokle-plugin --validator
,npm run codegen
andnpm run build
- let's avoid CLI for now.npm run preview
can run the local server which should be sufficient to test. [POC available - 0.5 day]At this point it works with CLI or by adding it to the monokle.validation.yaml.
Plugins configurable in browser [5-8 days]
Plugins gallery [5-8 days]
Beta Was this translation helpful? Give feedback.
All reactions