-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Always set the Content-Type header when a body is present #10204
Conversation
This define a custom RoundTripper to add the Content-Type header when it is needed. This commit should be reverted once hashicorp/consul#10204 is merged and released upstream. I did not write a test for this change but tested locally and the header is correctly present for PUT requests but not for GET or DELETE.
This define a custom RoundTripper to add the Content-Type header when it is needed. This commit should be reverted once hashicorp/consul#10204 is merged and released upstream. I did not write a test for this change but tested locally and the header is correctly present for PUT requests but not for GET or DELETE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @remilapeyre this is a great PR.
I think it can probably be merged as-is I just had one Q about those non-JSON exceptions and whether we need to do any more diligence to make sure we didn't miss any other edge cases?
api/operator_license.go
Outdated
@@ -100,6 +100,7 @@ func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseRepl | |||
r := op.c.newRequest("PUT", "/v1/operator/license") | |||
r.setWriteOptions(opts) | |||
r.body = strings.NewReader(license) | |||
r.header.Set("Content-Type", "application/octet-stream") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job finding these exceptions! How did you find them out of interest? Did tests break for these endpoints because of the wrong content type?
I can't think of any other exceptions but then I doubt I'd have remembered this one either so wondering if there is anything else we should do to be sure we caught all the non-json endpoints.
If you already went through exhaustively though then great!
I just remembered those two and looked briefly at the documentation and thought I had found them all. Looking at the output of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice idea!
I tried a few things in the code to pick up possible others:
$ rg -P 'io.Copy' agent/*endpoint.go
agent/operator_license_endpoint.go
37: if _, err := io.Copy(buf, req.Body); err != nil {
agent/kvs_endpoint.go
219: if _, err := io.Copy(buf, req.Body); err != nil {
agent/event_endpoint.go
49: if _, err := io.Copy(&buf, req.Body); err != nil
Which you've already got all of anyway.
I also tried:
rg -P '\.Body' agent/*.go | rg -v 'decodeBody\(|DecodeJSON\(|_test.go'
agent/watch_handler.go: defer resp.Body.Close()
agent/watch_handler.go: io.Copy(output, resp.Body)
agent/txn_endpoint.go: req.Body = http.MaxBytesReader(resp, req.Body, maxTxnLen)
agent/txn_endpoint.go: Body: check.Definition.Body,
agent/snapshot_endpoint.go: if err := s.agent.delegate.SnapshotRPC(&args, req.Body, resp, nil); err != nil {
agent/operator_license_endpoint.go: if _, err := io.Copy(buf, req.Body); err != nil {
agent/namespace_endpoint.go: if req.Body == nil {
agent/kvs_endpoint.go: if _, err := io.Copy(buf, req.Body); err != nil {
agent/event_endpoint.go: if _, err := io.Copy(&buf, req.Body); err != nil {
agent/http.go: if req.Body == nil {
agent/http.go: dec := json.NewDecoder(req.Body)
agent/acl_endpoint.go: policyBytes, err := ioutil.ReadAll(req.Body)
agent/agent.go: Body: chkType.Body,
Which are all false-positives except one: https://www.consul.io/api-docs/acl#translate-rules expects a raw HCL ACL token policy as the body so it probably needs a different content type. So I think we need an update here too:
Lines 896 to 918 in cc5dba1
// RulesTranslate translates the legacy rule syntax into the current syntax. | |
// | |
// Deprecated: Support for the legacy syntax translation will be removed | |
// when legacy ACL support is removed. | |
func (a *ACL) RulesTranslate(rules io.Reader) (string, error) { | |
r := a.c.newRequest("POST", "/v1/acl/rules/translate") | |
r.body = rules | |
rtt, resp, err := requireOK(a.c.doRequest(r)) | |
if err != nil { | |
return "", err | |
} | |
defer resp.Body.Close() | |
qm := &QueryMeta{} | |
parseQueryMeta(resp, qm) | |
qm.RequestTime = rtt | |
ruleBytes, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return "", fmt.Errorf("Failed to read translated rule body: %v", err) | |
} | |
return string(ruleBytes), nil | |
} |
I think with that we can merge!
Thanks for this Remi! Much appreciated.
Hi Paul I added the Content Type for the ACL and fixed the conflicts. I think it should be good now :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
Thanks for getting this in Remi!
GitHub did something strange here when I merged - it errored on my several times but when I went to merge manually I found the merge actually worked and this is already in master: 213524a Despite that GH isn't marking this PR as merged and if I attempt to merge again it just creates another empty commit on master and fails again. So I'll manually close this and go and do the 1.10 backport cherry-pick by hand. |
* Always set the Content-Type header when a body is present Closes #10011 * Add Changelog entry * Add more Content-Type exceptions * Fix tests
* Always set the Content-Type header when a body is present Closes #10011 * Add Changelog entry * Add more Content-Type exceptions * Fix tests
* Always set the Content-Type header when a body is present Closes #10011 * Add Changelog entry * Add more Content-Type exceptions * Fix tests
* Always set the Content-Type header when a body is present Closes #10011 * Add Changelog entry * Add more Content-Type exceptions * Fix tests
* Always set the Content-Type header when a body is present Closes #10011 * Add Changelog entry * Add more Content-Type exceptions * Fix tests
Closes #10011