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

return 400 status when invalid json patch passed to apiserver #68346

Merged
merged 1 commit into from
Sep 25, 2018

Conversation

CaoShuFeng
Copy link
Contributor

@CaoShuFeng CaoShuFeng commented Sep 6, 2018

related to : #68202

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:

kube-apiserver would return 400 Bad Request when it couldn't decode a json patch.
kube-apiserver would return 422 Unprocessable Entity when a json patch couldn't be applied to one object.

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 6, 2018
Copy link

@felixfbecker felixfbecker left a comment

Choose a reason for hiding this comment

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

It's important to distinguish between an invalid patch and a failed test operation.

}
patchedJS, err := patchObj.Apply(versionedJS)
if err != nil {
return nil, errors.NewBadRequest(err.Error())

Choose a reason for hiding this comment

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

I assume that this is the branch that gets hit when a test operation fails. Is there a way to check for that error specifically and send a 409 Conflict? A failed test operation is very different from an "invalid" patch (e.g. wrong paths etc) and it needs to be possible to handle it specifically on the client. A failed test operation means a failed precondition and the client likely just wants to update the local state from the server, regenerate the patch and try again. If the patch is invalid it would not try again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree that 400 is not good here.

@fedebongio
Copy link
Contributor

/assign @caesarxuchao

@@ -284,9 +284,13 @@ func (p *jsonPatcher) applyJSPatch(versionedJS []byte) (patchedJS []byte, retErr
case types.JSONPatchType:
patchObj, err := jsonpatch.DecodePatch(p.patchJS)
if err != nil {
return nil, err
return nil, errors.NewBadRequest(err.Error())
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the interpretPatchError(err) like other places in this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interpretPatchError can only deal with mergepatch errors

Copy link
Member

Choose a reason for hiding this comment

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

Got it.

I agree that we should fix upstream to make json-patch return typed errors. Then we can make interpretPatchError interprets json-patch errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
patchedJS, err := patchObj.Apply(versionedJS)
if err != nil {
return nil, errors.NewGenericServerResponse(http.StatusUnprocessableEntity, "", schema.GroupResource{}, "", err.Error(), 0, false)
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@@ -326,6 +327,9 @@ func (tc *patchTestCase) Run(t *testing.T) {
patch := []byte{}
switch patchType {
case types.StrategicMergePatchType:
if len(tc.jsonPatch) > 0 {
continue
}
Copy link
Member

Choose a reason for hiding this comment

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

Are these lines for debugging only? If so please remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tc.startingPod.UID = uid
tc.startingPod.ResourceVersion = "2"
tc.startingPod.APIVersion = examplev1.SchemeGroupVersion.String()
tc.updatePod = tc.startingPod
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you don't need updatePod at all? It's weird to have updatePod: &example.Pod{} and then tc.updatePod = tc.startingPod.

Copy link
Member

Choose a reason for hiding this comment

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

Actually why not using setTcPod like other test cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tc.startingPod.UID = uid
tc.startingPod.ResourceVersion = "2"
tc.startingPod.APIVersion = examplev1.SchemeGroupVersion.String()
tc.updatePod = tc.startingPod
Copy link
Member

Choose a reason for hiding this comment

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

I would have made a utility function to return a pod pointer to avoid duplicate code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 10, 2018
@felixfbecker
Copy link

My comment above got collapsed by GitHub, but this still does not fix the linked issue. It is an improvement, but #68202 is specifically about failed test operations which should return a distinct status code (409 Conflict).

@CaoShuFeng
Copy link
Contributor Author

CaoShuFeng commented Sep 10, 2018

My comment above got collapsed by GitHub, but this still does not fix the linked issue. It is an improvement, but #68202 is specifically about failed test operations which should return a distinct status code (409 Conflict).

I see. Maybe we should fix this in upstream first.
json-patch just return fmt.Errorf for all kinds of errors. So we cant't distinguish the error should be a 409 or 422 here.

https://github.com/evanphx/json-patch/blob/master/patch.go#L536

@felixfbecker
Copy link

@CaoShuFeng got it. In that case please remove the "Fix: #68202" from the PR description so the issue doesn't get closed. Thanks for the improvements in this PR!

@CaoShuFeng
Copy link
Contributor Author

. In that case please remove the "Fix: #68202" from the PR description so the issue doesn't get closed.

Done.

@@ -604,6 +618,52 @@ func TestPatchResourceWithRacingVersionConflict(t *testing.T) {
tc.Run(t)
}

func TestPatchResourceWithInvalidJSONPatch(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems you need to change the patchTestCase framework a bit to make it work for your two test cases.

Instead of making that framework more complicated, how about making a focused test for applyJSPatch? You don't need to fake too much to make it work.

And you can move the tests to right below TestPatchInvalid. (And you can rename TestPatchInvalid to TestStrategicMergePatchInvalid to make is clear).

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 11, 2018
Copy link
Member

@caesarxuchao caesarxuchao left a comment

Choose a reason for hiding this comment

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

lgtm except for one nit.

versionedJS, err := runtime.Encode(codec, pod)
if err != nil {
t.Errorf("%s: unexpected error: %v", test.name, err)
return
Copy link
Member

Choose a reason for hiding this comment

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

use Fatalf?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

replace return with continue, just as other places do.

@caesarxuchao
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 18, 2018
@CaoShuFeng
Copy link
Contributor Author

CaoShuFeng commented Sep 18, 2018

/assign @sttts
for approve
Thanks.

@k8s-ci-robot
Copy link
Contributor

@CaoShuFeng: GitHub didn't allow me to assign the following users: for, approve.

Note that only kubernetes members and repo collaborators can be assigned.
For more information please see the contributor guide

In response to this:

/assign @sttts for approve
Thanks.

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.

@caesarxuchao
Copy link
Member

/assign @lavalamp

for approval and possibly apply milestone.

@lavalamp
Copy link
Member

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: caesarxuchao, CaoShuFeng, lavalamp

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 Sep 19, 2018
@lavalamp
Copy link
Member

Please add a release note explaining the status code change.

@lavalamp
Copy link
Member

(I think this isn't urgent and can wait until master opens back up for merges.)

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Sep 20, 2018
@CaoShuFeng
Copy link
Contributor Author

Please add a release note explaining the status code change.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/apiserver cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants