Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable schema validation for validate policies #1869

Open
realshuting opened this issue May 3, 2021 · 7 comments
Open

Enable schema validation for validate policies #1869

realshuting opened this issue May 3, 2021 · 7 comments
Labels
enhancement New feature or request validation Issues pertaining to the validate ability.

Comments

@realshuting
Copy link
Member

Is your feature request related to a problem? Please describe.
With Kyverno v1.3.6-rc1, the schema validation(of the resource) is only enabled for the mutate policy, not validate.

If I create the mutate policy with the following overlay, Kyverno denies it as it's invalid.

$ k apply -f mutate.yaml
Error from server: error when creating "test.yaml": admission webhook "validate-policy.kyverno.svc" denied the request: ValidationError(io.k8s.api.apps.v1.Deployment.spec.template): unknown field "data" in io.k8s.api.core.v1.PodTemplateSpec
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: check-root-user
spec:
  background: true
  rules:
  - exclude:
      resources:
        namespaces:
        - test
    match:
      resources:
        kinds:
        - Pod
    name: check-root-user
    mutate:
      patchStrategicMerge:
        data:
          securityContext:
            runAsNonRoot: true

But the following validate policy can be created successfully:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: check-root-user
spec:
  background: true
  rules:
  - exclude:
      resources:
        namespaces:
        - test
    match:
      resources:
        kinds:
        - Pod
    name: check-root-user
    validate:
      pattern:
        data:
          securityContext:
            runAsNonRoot: true
      message: Root user is not allowed. Set runAsNonRoot to true.
  validationFailureAction: audit

Describe the solution you'd like
The logic is there to check the mutate policy, we just need to extend it for the validate policy.

func (o *Controller) ValidatePolicyMutation(policy v1.ClusterPolicy) error {
var kindToRules = make(map[string][]v1.Rule)
for _, rule := range policy.Spec.Rules {
if rule.HasMutate() {
for _, kind := range rule.MatchResources.Kinds {
kindToRules[kind] = append(kindToRules[kind], rule)
}
}
}

@anishagg17
Copy link

Hi @realshuting, I would like to work on this one.

@chipzoller
Copy link
Member

See also relevant comment here.

@anishagg17
Copy link

See also relevant comment here.

Alright, I got your point that we need to adapt validation according to the case. I will consider all those points within the PR(if applicable) and you can then look up if there's something missing.

@realshuting
Copy link
Member Author

An update about the issue - after v1.3.6-rc3, this schema validation for strategicMergePatch is temporarily missing, as we reverted one change #1898 due to #1896. That means such invalid strategicMergePatch polices won't be blocked during creation. But this has nothing to do with validation policy.

@chipzoller
Copy link
Member

@anishagg17 are you still working on this issue?

@harshitasao
Copy link

Hi @realshuting, I would like to work on this issue. Could you please provide some pointers, resources to start with.

@realshuting
Copy link
Member Author

@harshitasao - did you check the issue description #1869 (comment)?
Is anything unclear to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request validation Issues pertaining to the validate ability.
Projects
None yet
Development

No branches or pull requests

5 participants