Fluentd: Issues with in_http plugin while using CORS

Created on 26 Sep 2017  路  11Comments  路  Source: fluent/fluentd

  • 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.
bug v1

Most helpful comment

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

All 11 comments

Does your application send Origin header to in_http?

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.

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

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

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

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

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|

@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 , I am running td-agent 1.4.2 (3.4.1) and can confirm this fix works! Thank you!

seems to be solved. closed

@ganmacs Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kakalo24 picture kakalo24  路  3Comments

khteh picture khteh  路  4Comments

geek876 picture geek876  路  6Comments

JayTeli picture JayTeli  路  4Comments

sradco picture sradco  路  3Comments