fluentd icon indicating copy to clipboard operation
fluentd copied to clipboard

Issues with in_http plugin while using CORS

Open gma-git-admin opened this issue 9 years ago • 11 comments

  • td-agent 0.12.
  • Ubuntu 16.04.1 LTS
  • Configuration in td-agent.conf
<match app.**>
  @type stdout
</match>
<source>
  @type http
  port 8282
  bind 0.0.0.0
  body_size_limit 32m
  keepalive_timeout 10s
  add_remote_addr true
  cors_allow_origins ["http://dev.myapp.com"]
  format json
</source>
  • I try to send log using HTTP POST from AngularJS service with json data on this url: http://dev.myapp.com:8282/app.RouteChange This throws following error: OPTIONS http://dev.myapp.com:8282/app.RouteChange 400 (Bad Request) XMLHttpRequest cannot load http://dev.myapp.com:8282/app.RouteChange. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.myapp.com' is therefore not allowed access. The response had HTTP status code 400.

gma-git-admin avatar Sep 26 '17 16:09 gma-git-admin

Does your application send Origin header to in_http?

repeatedly avatar Oct 04 '17 09:10 repeatedly

I got it working following in_logtank_http plugin [https://github.com/logTank/fluent-plugin-http/blob/master/lib/fluent/plugin/in_logtank_http.rb]

Following lines were important to get the CORS issue resolved:

        header['Access-Control-Allow-Origin'] ||= '*'
        (header['Access-Control-Allow-Methods'] ||= @requestMethod) if @requestMethod
        (header['Access-Control-Allow-Headers'] ||= @requestHeaders) if @requestHeaders

Earlier I tried sending headers

Access-Control-Allow-Origin': ['http://dev.myapp.com:8282', 'http://dev.myapp.com'] But it didn't help much.

gma-git-admin avatar Oct 06 '17 21:10 gma-git-admin

So porting above code from logtank's fluent-plugin-http resolve this issue, right?

repeatedly avatar Oct 20 '17 21:10 repeatedly

This issue is still present in the latest td-agent.

tjratch avatar Aug 22 '18 18:08 tjratch

Actually i´m facing the same issue, In the client side i´m using XMLHttpRequest() and i have this code :

var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', url, true);
xmlHttp.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); //json 
 xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*'); //CORS
 xmlHttp.send(JSON.stringify(data));

On the fluentd server side i have (fluentd.conf) :

screen shot 2018-08-22 at 4 51 57 pm

But when i run the web app, i get this error :
screen shot 2018-08-22 at 4 54 08 pm

it´s supposed that with the Client and Server config it will work.. https://docs.fluentd.org/v0.12/articles/in_http (section "cors_allow_origins")

Thanks in advanced

wilesna25 avatar Aug 22 '18 21:08 wilesna25

Does anyone check in_logtank_http? If no problem with your JavaScript code, could you send a patch to in_http?

repeatedly avatar Aug 30 '18 09:08 repeatedly

I checked logtank http and diff is small, so I simply apply changes to in_http. I need feedback this patch works or not.

diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb
index a9d0bf69..c268e544 100644
--- a/lib/fluent/plugin/in_http.rb
+++ b/lib/fluent/plugin/in_http.rb
@@ -342,6 +342,10 @@ module Fluent::Plugin
             # For multiple X-Forwarded-For headers. Use first header value.
             v = v.first if v.is_a?(Array)
             @remote_addr = v.split(",").first
+          when /Access-Control-Request-Method/i
+            @request_method = v
+          when /Access-Control-Request-Headers/i
+            @request_headers = v
           end
         }
         if expect
@@ -422,7 +426,6 @@ module Fluent::Plugin
         code, header, body = *@callback.call(path_info, params)
         body = body.to_s
 
-        header['Access-Control-Allow-Origin'] = @origin if !@cors_allow_origins.nil? && @cors_allow_origins.include?(@origin)
         if @keep_alive
           header['Connection'] = 'Keep-Alive'
           send_response(code, header, body)
@@ -447,6 +450,15 @@ module Fluent::Plugin
       def send_response(code, header, body)
         header['Content-Length'] ||= body.bytesize
         header['Content-Type'] ||= 'text/plain'
+        if !@cors_allow_origins.nil?
+          if @cors_allow_origins.first == '*'
+            header['Access-Control-Allow-Origin'] = '*'
+          else
+            header['Access-Control-Allow-Origin'] = @origin if @cors_allow_origins.include?(@origin)
+          end
+          header['Access-Control-Allow-Methods'] ||= @request_method if @request_method
+          header['Access-Control-Allow-Headers'] ||= @request_headers if @request_headers
+        end
 
         data = %[HTTP/1.1 #{code}\r\n]
         header.each_pair {|k,v|

repeatedly avatar Aug 31 '18 06:08 repeatedly

@gma-git-admin @tjratch @wilesna25 This issue should be resolved in Fluentd v1.3.0. Try the version and please report back here if the issue still persists.

fujimotos avatar Nov 15 '18 08:11 fujimotos

@fujimotos , I am running td-agent 1.4.2 (3.4.1) and can confirm this fix works! Thank you!

tratch avatar Jul 09 '19 19:07 tratch

seems to be solved. closed

ganmacs avatar Dec 23 '19 02:12 ganmacs

@ganmacs Thank you!

fujimotos avatar Dec 23 '19 03:12 fujimotos