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

Server Side Strict Field Validation #105916

Merged
merged 1 commit into from Nov 19, 2021

Conversation

kevindelgado
Copy link
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Performs strict server side schema validation via the fieldValidation=[Strict,Warn,Ignore] query parameter.

It introduces the fieldValidation query parameter that when set to Strict causes requests to error when unknown fields are present, when set to Warn succeeds the request but returns a warning, and when set to Ignore it ignores unknown fields.

For create/update requests, we use strict json unmarshalling to determine the unknown fields and error/warn based on those fields.

For JSON Patch requests (i.e. CRDs), we use the prune mechanism in the customresource handler to resolve any unknown fields.

For SMP Patch request, we use the the unstructured converter to resolve unknown fields.

Apply Patch requests already require strict schemas and will error on unknown fields.

Which issue(s) this PR fixes:

First step in solving #39434 and #5889

Does this PR introduce a user-facing change?

Performs strict server side schema validation requests via the `fieldValidation=[Strict,Warn,Ignore]` query parameter.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

KEP-2885

[KEP-2885](https://github.com/kubernetes/enhancements/issues/2885)

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 26, 2021
@k8s-ci-robot k8s-ci-robot added area/apiserver area/test kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/testing Categorizes an issue or PR as relevant to SIG Testing. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 26, 2021
@kevindelgado
Copy link
Contributor Author

/assign @liggitt
/assign @apelisse

Addressed most of the feedback from liggitt#2. Left responses to comments on that PR. Still a couple TODOs around test formatting and defaulting to warning, but wanted to get this up for another round of review ASAP.

@k8s-triage-robot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@caesarxuchao
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. area/dependency Issues or PRs related to dependency changes and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 26, 2021
Copy link
Contributor Author

@kevindelgado kevindelgado left a comment

Choose a reason for hiding this comment

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

Writing tests now, but it's going pretty slowly. Will hopefully add a bunch more tomorrow. Probably wont address the converter stuff until I've made it through all the tests. Feel free to hold off on reviewing until I've added more tests, but if you do want to take a look I made some changes the to jsonPatcher in order to collect decoding errors that arise from the patch itself having invalid json, and started updating a few of the tests to be more inline with your testing comment.

Relavent commits are 9bd487a and dd91910

Copy link
Contributor Author

@kevindelgado kevindelgado left a comment

Choose a reason for hiding this comment

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

I've done a big pass based on previous feedback. In particular, I revamped the integration testing suite based on Jordan's previous comments. It caught a bunch of bugs.

I figured this would be a good time to check in and ask for a review especially around the testing strategy.

In parallel, I have a few isolated issues I'm working on, but I don't want this to block reviewing. These are mainly:
1. kjson.UnmarshalStrict doesn't seem to recognizing duplicate fields when unmarshalling into an unstructured object. I need to dig into this further. (fixed by unmarshalling directly into the object)
2. I still need to implement full field path collection to the converter (and pruning) so that the errors reported contain the full path to the field (i.e /spec/unknownField rather than just unknownField)(Done)
3. The SMP/warn test case is not returning the full list of unknown fields. I need to dig into this further.(Fixed via the new converter implementation)

Also, I've cordoned off the benchmark tests for now. Still working on them, but the ones I had I think lost their utility and were hard to maintain as I was rapidly iterating on the tests. I will rewrite them once the test suite gets reviewed and is a little more stable.

staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@kevindelgado kevindelgado left a comment

Choose a reason for hiding this comment

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

All feedback addressed, benchstats still look good (ignore case is completely inert, but slightly more allocs in the strict case than before):

Ignore

name                                      old time/op    new time/op    delta
FromUnstructuredWithValidation/Ignore-16     149µs ± 1%     148µs ± 1%   ~     (p=0.222 n=5+5)

name                                      old alloc/op   new alloc/op   delta
FromUnstructuredWithValidation/Ignore-16    26.8kB ± 0%    26.8kB ± 0%   ~     (p=0.524 n=5+5)

name                                      old allocs/op  new allocs/op  delta
FromUnstructuredWithValidation/Ignore-16       641 ± 0%       641 ± 0%   ~     (all equal)

Strict

name                                      old time/op    new time/op    delta
FromUnstructuredWithValidation/Strict-16     149µs ± 1%     167µs ± 1%  +11.87%  (p=0.008 n=5+5)

name                                      old alloc/op   new alloc/op   delta
FromUnstructuredWithValidation/Strict-16    26.8kB ± 0%    33.5kB ± 0%  +25.00%  (p=0.008 n=5+5)

name                                      old allocs/op  new allocs/op  delta
FromUnstructuredWithValidation/Strict-16       641 ± 0%       745 ± 0%  +16.22%  (p=0.008 n=5+5)

staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/runtime/converter.go Outdated Show resolved Hide resolved
// alpha: v1.23
//
// Enables server-side field validation.
StrictFieldValidation featuregate.Feature = "StrictFieldValidation"
Copy link
Member

Choose a reason for hiding this comment

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

nit: would ServerSideFieldValidation be a better name, since the feature enables warnings in addition to strict handling? should be an easy find/replace if so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea I agree, done

Copy link
Member

Choose a reason for hiding this comment

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

follow up with a PR updating the KEP and a PR updating website docs

staging/src/k8s.io/apimachinery/pkg/runtime/types.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@kevindelgado kevindelgado left a comment

Choose a reason for hiding this comment

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

all feedback addressed

// alpha: v1.23
//
// Enables server-side field validation.
StrictFieldValidation featuregate.Feature = "StrictFieldValidation"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea I agree, done

staging/src/k8s.io/apimachinery/pkg/runtime/types.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go Outdated Show resolved Hide resolved
staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go Outdated Show resolved Hide resolved
@liggitt
Copy link
Member

liggitt commented Nov 19, 2021

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kevindelgado, liggitt

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 19, 2021
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2021
@leilajal
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2021
@liggitt liggitt moved this from In progress to API review completed, 1.23 in API Reviews Nov 19, 2021
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Nov 19, 2021

@kevindelgado: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-e2e-gce-network-proxy-grpc b3347064b17f0c6eafac8e30136bc03bed9a2782 link false /test pull-kubernetes-e2e-gce-network-proxy-grpc
pull-kubernetes-e2e-gce-network-proxy-http-connect b3347064b17f0c6eafac8e30136bc03bed9a2782 link true /test pull-kubernetes-e2e-gce-network-proxy-http-connect
pull-kubernetes-e2e-gce-large-performance c6954828a21e4f5e4a714ecfdf36bf169a55abfa link true /test pull-kubernetes-e2e-gce-large-performance
pull-kubernetes-e2e-gce-big-performance c6954828a21e4f5e4a714ecfdf36bf169a55abfa link true /test pull-kubernetes-e2e-gce-big-performance

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Implements server side field validation behind the
`ServerSideFieldValidation` feature gate. With the
feature enabled, any create/update/patch request
with the `fieldValidation` query param set to
"Strict" will error if the object in the request
body have unknown fields. A value of "Warn"
(also the default when the feautre is enabled)
will succeed the request with a warning.

When the feature is disabled (or the query param
has a value of "Ignore"), the request will succeed
as it previously had with no indications of any
unknown or duplicate fields.
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2021
@leilajal
Copy link
Contributor

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review Categorizes an issue or PR as actively needing an API review. approved Indicates a PR has been approved by an approver from all required OWNERS files. area/apiserver area/code-generation area/dependency Issues or PRs related to dependency changes area/kubelet area/provider/gcp Issues or PRs related to gcp provider area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: API review completed, 1.23
Development

Successfully merging this pull request may close these issues.

None yet

8 participants