Skip to content

feat(validation): add pre-execution validation layer#764

Open
nader-ziada wants to merge 2 commits intocontainers:mainfrom
nader-ziada:validation
Open

feat(validation): add pre-execution validation layer#764
nader-ziada wants to merge 2 commits intocontainers:mainfrom
nader-ziada:validation

Conversation

@nader-ziada
Copy link
Collaborator

@nader-ziada nader-ziada commented Feb 10, 2026

Add validation middleware that catches errors before they reach the Kubernetes API.

Changes:

  • Resource validation: catches typos in resource types (GVK)
  • Schema validation: validates manifests against OpenAPI schema
  • RBAC validation: pre-checks permissions using SelfSubjectAccessReview
  • All validators are enabled by default and can be configured via environment variables (MCP_VALIDATION_*) or TOML config.

Closes #775

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Cali0707 just wondering (un releated) some sort of validation of "larger cluster" access would be nice for multi-cluster/acm use-case. I think we currently list all "managed clusters", w/o some sort of "validation"

Copy link
Member

@manusa manusa Feb 11, 2026

Choose a reason for hiding this comment

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

Note that we already have a canIUser method defined here:

func (c *Core) canIUse(ctx context.Context, gvr *schema.GroupVersionResource, namespace, verb string) bool {
accessReviews := c.AuthorizationV1().SelfSubjectAccessReviews()
response, err := accessReviews.Create(ctx, &authv1.SelfSubjectAccessReview{
Spec: authv1.SelfSubjectAccessReviewSpec{ResourceAttributes: &authv1.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Group: gvr.Group,
Version: gvr.Version,
Resource: gvr.Resource,
}},
}, metav1.CreateOptions{})
if err != nil {
// TODO: maybe return the error too
return false
}
return response.Status.Allowed
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

both methods are unexposed (private methods) and serve different purposes in different contexts
I prefer to have them in different packages with clear separation of concerns.

but let me know if you prefer to combine them somehow and I can do that

Copy link
Member

Choose a reason for hiding this comment

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

Just noting in the context of the downstream issue that relates to this fix.
I'm not sure that if in that context, we might just want to put this in the AccessControlRoundTripper.
Here, we're already checking for denied resources in the configuration (for example)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

my understanding is that AccessControlRoundTripper checks configured denials (checks against a static deny list that the server operator configures. This is separate from Kubernetes RBAC) while the RBACValidator checks actual RBAC (what the cluster says about the current user's permissions)

Are you saying we don't need the RBACValidator since we will get the AccessControlRoundTripper check anyways? but the error message and logging would be different?

Copy link
Member

Choose a reason for hiding this comment

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

No, I was saying that maybe (if we're just tackling the downstream issue -it's unclear to me the scope of it since it's not properly described-) we can just add RBAC validation to the roundrtipper (simple solution: KISS, YAGNI)

In any case, I'll review everything else in the issue.

What I'd also suggest is to make this opt-in for a couple of vesions at least upstream, since it might have side-effects we haven't accounted for.

Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

Thx for putting this together.
I gave it an initial look and added a few comments.

Add validation middleware that catches errors before they reach the
Kubernetes API.

Signed-off-by: Nader Ziada <nziada@redhat.com>
@nader-ziada
Copy link
Collaborator Author

made a refactor of how this works, sorry to the reviewers, to make work from the kubernetes pkg instead of the mcp pkg, now more dynamic and flexible. also disabled by default.

@manusa manusa self-requested a review February 16, 2026 13:34
Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

I think that the decoupling from the mcp layer is going great, thx.
I added some more comments regarding the new approach.

Comment on lines 15 to 18
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall we support config overrides through environment variables at the moment, we do support those through CLI flags.

Is there any reason why we need to be able to override this through an environment variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

Copy link
Member

Choose a reason for hiding this comment

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

Are we planning to add more config options to validation?
If so, I understand the nesting of the config into a different struct.
If not, why is the nesting needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i originally had different flags for each validator, but decided its too much complexity, will remove struct

Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a substitute to the AccessControlRoundTripper, but the former is still retained (which is confusing).

I'm not sure if we prefer to make the composable and compose the when wiring together in kubernetes.go.
Or merging both into this one.
Whatever works better, but we should trim and clean up the redundant bits.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

merged the validation login into the AccessControlRoundTripper

Signed-off-by: Nader Ziada <nziada@redhat.com>
@nader-ziada nader-ziada requested a review from manusa February 18, 2026 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments