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

pkg/server/api: Return 405s for non-GET requests #240

Merged

Conversation

wking
Copy link
Member

@wking wking commented Dec 16, 2018

We don't want folks POSTing, etc. to this server. With this pull-request, we'll return the appropriate response status code if they do.

@openshift-ci-robot openshift-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 16, 2018
@abhinavdahiya
Copy link
Contributor

Can you add tests to api_test.go

@wking
Copy link
Member Author

wking commented Dec 16, 2018

I'd have to extend scenario. Narrowly to add a method property? Broadly to add a request property (which would also allow testing of #239)?

@abhinavdahiya
Copy link
Contributor

/hold

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 16, 2018
wking added a commit to wking/machine-config-operator that referenced this pull request Dec 17, 2018
Each sub-handler would be switching on Path anyway, and we don't have
so many paths for the muxer's extra level of path switching to help
much.  And it's nice to only have to put things like [1]'s method
guard in one spot (vs. repeating in each of the muxer's sub-handlers).

[1]: openshift#240
@wking wking force-pushed the server-405-for-non-get-requests branch from a9e8294 to 98d3fa2 Compare December 17, 2018 05:01
@openshift-ci-robot openshift-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 17, 2018
@wking
Copy link
Member Author

wking commented Dec 17, 2018

Rebased onto #242 and added a test with a9e8294 -> 98d3fa2.

Copy link
Member

@ashcrow ashcrow left a comment

Choose a reason for hiding this comment

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

👍

@wking wking force-pushed the server-405-for-non-get-requests branch from 98d3fa2 to 2dc59a3 Compare December 17, 2018 19:00
@wking
Copy link
Member Author

wking commented Dec 17, 2018

With #242 closed, I've rebased this onto master again and added a unit test based on Requests with 98d3fa2 -> 2dc59a3.

pkg/server/api.go Outdated Show resolved Hide resolved
@abhinavdahiya
Copy link
Contributor

/hold cancel

@openshift-ci-robot openshift-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 17, 2018
@wking
Copy link
Member Author

wking commented Dec 17, 2018

e2e-aws:

level=error msg="1 error occurred:"
level=error msg="\t* module.vpc.aws_vpc_endpoint.s3: 1 error occurred:"
level=error msg="\t* aws_vpc_endpoint.s3: Error creating VPC Endpoint: VpcEndpointLimitExceeded: The maximum number of VPC endpoints has been reached."
level=error msg="\tstatus code: 400, request id: e86bbbe2-9fc6-4177-ba9c-ad2a98f8030b"

James is working on getting that limit bumped, but until then it's just luck ;). I'll wait until the queue calms down tonight before retesting, since this is a fairly minor change.

@wking
Copy link
Member Author

wking commented Dec 17, 2018

Our limit has been bumped.

/retest

@wking wking force-pushed the server-405-for-non-get-requests branch from 2dc59a3 to 22d0c45 Compare December 17, 2018 22:37
@openshift-ci-robot openshift-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 Dec 17, 2018
@@ -80,8 +80,14 @@ func NewServerAPIHandler(s Server) *APIHandler {
// ServeHTTP handles the requests for the machine config server
// API handler.
func (sh *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Content-Length", "0")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you point to to a reason why we need to set this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you point to to a reason why we need to set this?

If people start HEADing this server, it seems like they might be interested in getting valid Content-Length back. I just started setting it everywhere to make it easier to test that we get it right when we actually care about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

So looking at 5f0a6a7

The defaultHandler gives us our usual empty 404s for requests that
don't match our other handlers.  Without it, we'd have returned Go's
default:

  404 page not found

body, and had unit-test failures like:

  --- FAIL: TestHandler (0.00s)
      --- FAIL: TestHandler/GET_/does-not-exist (0.00s)
          api_test.go:93: response body length 19 does not match Content-Length -1

I makes me go back to to the original thing that we should not setting/expecting Content-Length when we only care about the StatusCode.

So maybe can we remove all the w.Header().Set("Content-Length", "0") and only set it when we care, ie HEAD/GET success result.

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes me go back to to the original thing that we should not setting/expecting Content-Length when we only care about the StatusCode.

If we want to return 404 page not found\n (with Content-Type: text/plain; charset=utf-8?) for all of our 404s, I'm ok with that (although I don't see a need to do it). But I would like to be consistent among our 404s, and not have some that return with no body and some that return with the text/plain body.

Copy link
Contributor

Choose a reason for hiding this comment

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

lets keep it as is for now..

@wking
Copy link
Member Author

wking commented Dec 17, 2018

images failure was openshift/release#2070.

/retest

@wking wking force-pushed the server-405-for-non-get-requests branch 3 times, most recently from 0eebfed to 124fd90 Compare December 18, 2018 02:27
We don't want folks POSTing, etc. to this server.  If they do, this
commit will return the appropriate response status code.

Also add support for HEAD requests and start setting Content-Length
and Content-Type ourselves instead of leaning on Go's autodetection
[1].  The application/json media type is from [2].

[1]: https://golang.org/pkg/net/http/#ResponseWriter
[2]: https://tools.ietf.org/html/rfc8259#section-1.2
@wking wking force-pushed the server-405-for-non-get-requests branch from 124fd90 to 01d22e1 Compare December 18, 2018 18:18
@abhinavdahiya
Copy link
Contributor

/lgtm

/retest

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 18, 2018
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: abhinavdahiya, wking

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:
  • OWNERS [abhinavdahiya,wking]

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

@ashcrow
Copy link
Member

ashcrow commented Dec 18, 2018

/test e2e-aws

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit 1268da7 into openshift:master Dec 19, 2018
@wking wking deleted the server-405-for-non-get-requests branch December 19, 2018 07:17
osherdp pushed a commit to osherdp/machine-config-operator that referenced this pull request Apr 13, 2021
…streams-from-progressing

Bug 1811143: purge removed imagestreams as a part of upgrade from progessing/impor…
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. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants