Loki: Change how push API checks for contentType (#4443)

* Change how push API checks for contentType

- Currently, the ContentType is expected to be exactly
  "application/json". That's a problem because although a
"application/json; charset=utf-8" is considered a valid JSON content
type, it will be seen as an error.

* Use mime.ParseMediaType to parse contentType.

- mime.ParseMediaType is more reliable than strings.Contains because if
  a wrong contentType (such as "application/jsonnn") is given, we still
accept it
- mime.ParseMediaType has support for contentType parameters. This way,
  valid contentTypes such as "application/json; charset=utf-8" will not
be rejected

* Add changelog entry.

* Change body in test to avoid lint issues.

- Without this change, the lint fails because gzipString would be called
  with always the same input. Full error:
```
`gzipString` - `source` always receives ``{"streams": [{ "stream": {
"foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ]
}]}` ("{\"streams\": [{ \"stream\": { \"foo\": \"bar2\" }, \"values\": [
[ ...)
` (unparam)
```
pull/4338/head
Dylan Guedes 4 years ago committed by GitHub
parent 4c5b084525
commit ab10bc69d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 7
      pkg/loghttp/push/push.go
  3. 21
      pkg/loghttp/push/push_test.go

@ -1,5 +1,6 @@
## Main
* [4400](https://github.com/grafana/loki/pull/4400) **trevorwhitney**: Config: automatically apply memberlist config too all rings when provided
* [4443](https://github.com/grafana/loki/pull/4443) **DylanGuedes**: Loki: Change how push API checks for contentType
# 2.3.0 (2021/08/06)

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math"
"mime"
"net/http"
"time"
@ -78,8 +79,14 @@ func ParseRequest(logger log.Logger, userID string, r *http.Request, tenantsRete
req logproto.PushRequest
)
contentType, _ /* params */, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
switch contentType {
case applicationJSON:
var err error
// todo once https://github.com/weaveworks/common/commit/73225442af7da93ec8f6a6e2f7c8aafaee3f8840 is in Loki.

@ -72,6 +72,27 @@ func TestParseRequest(t *testing.T) {
contentEncoding: `snappy`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json; charset=utf-8`,
contentEncoding: `gzip`,
valid: true,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/jsonn; charset=utf-8`,
contentEncoding: `gzip`,
valid: false,
},
{
path: `/loki/api/v1/push`,
body: gzipString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`),
contentType: `application/json; charsetutf-8`,
contentEncoding: `gzip`,
valid: false,
},
}
// Testing input array

Loading…
Cancel
Save