I'm already running an elasticsearch instance behind Traefik for some time, now that I wanted to upgrade Traefik from a version previous 2.0.5 the CORS headers aren't working anymore like expected. Whenrequesting the elasticsearch instance with the help of elasticsearch.js this results in a cURL request copied like this from the web developer tools:
curl 'https://search.example.com/' -X OPTIONS -H 'authority: search.example.com' -H 'access-control-request-method: HEAD' -H 'origin: https://doc.example.com' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36' -H 'access-control-request-headers: authorization' -H 'accept: */*' -H 'sec-fetch-site: same-site' -H 'sec-fetch-mode: cors' -H 'referer: https://doc.example.com/' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7' --compressed -v
With Traefik <= 2.0.5 it resulted in a response like this:
> OPTIONS / HTTP/2
> Host: search.example.com
> authority: search.example.com
> access-control-request-method: HEAD
> origin: https://doc.example.com
> user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
> access-control-request-headers: authorization
> accept: */*
> sec-fetch-site: same-site
> sec-fetch-mode: cors
> referer: https://doc.example.com/
> accept-encoding: gzip, deflate, br
> accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 200
< access-control-allow-credentials: true
< access-control-allow-headers: Authorization,Origin,Content-Type,Accept
< access-control-allow-methods: GET,POST,PUT,PATCH,DELETE,OPTIONS
< access-control-allow-origin: *
< access-control-max-age: 0
< content-length: 0
< date: Thu, 09 Jan 2020 12:57:09 GMT
<
* Connection #0 to host search.example.com left intact
* Closing connection 0
Since Traefik >= 2.0.6 (IMHO it's related to https://github.com/containous/traefik/pull/4857) the response changed:
> OPTIONS / HTTP/2
> Host: search.example.com
> authority: search.example.com
> access-control-request-method: HEAD
> origin: https://doc.example.com
> user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
> access-control-request-headers: authorization
> accept: */*
> sec-fetch-site: same-site
> sec-fetch-mode: cors
> referer: https://doc.example.com/
> accept-encoding: gzip, deflate, br
> accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 200
< content-length: 0
< date: Thu, 09 Jan 2020 12:24:32 GMT
<
* Connection #0 to host search.example.com left intact
* Closing connection 0
So it's quite obvious that my browser errors out with something like Access to XMLHttpRequest at 'https://search.example.com/_search?q=con' from origin 'https://doc.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
So far I have defined this middleware, even if it's quite much open, it simply worked:
--label traefik.http.middlewares.elasticsearch.headers.accesscontrolalloworigin=* \
--label traefik.http.middlewares.elasticsearch.headers.accesscontrolallowcredentials=true \
--label traefik.http.middlewares.elasticsearch.headers.accesscontrolallowheaders=Authorization,Origin,Content-Type,Accept \
--label traefik.http.middlewares.elasticsearch.headers.accesscontrolallowmethods=GET,POST,PUT,PATCH,DELETE,OPTIONS \
--label traefik.http.middlewares.elasticsearch.headers.customresponseheaders.Access-Control-Allow-Headers=Authorization,Origin,Content-Type,Accept \
--label traefik.http.middlewares.elasticsearch.headers.customresponseheaders.Access-Control-Allow-Methods=GET,POST,PUT,PATCH,DELETE,OPTIONS \
I'm not sure how to resolve the issue now, I can see that Traefik is loggin the request as an OPTIONS request, but it simply doesn't return the configured CORS headers. What can I do? Is this a regression?