Skip to content
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

Issues with in_http plugin while using CORS #1701

Closed
gma-git-admin opened this issue Sep 26, 2017 · 11 comments
Closed

Issues with in_http plugin while using CORS #1701

gma-git-admin opened this issue Sep 26, 2017 · 11 comments
Labels
bug Something isn't working v1

Comments

@gma-git-admin
Copy link

gma-git-admin commented Sep 26, 2017

  • 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>
@repeatedly
Copy link
Member

repeatedly commented Oct 4, 2017

Does your application send Origin header to in_http?

@gma-git-admin
Copy link
Author

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.

@repeatedly
Copy link
Member

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

@tjratch
Copy link

tjratch commented Aug 22, 2018

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

@wilesna25
Copy link

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

@repeatedly
Copy link
Member

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

@repeatedly repeatedly added bug Something isn't working v1 labels Aug 30, 2018
@repeatedly
Copy link
Member

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|

@fujimotos
Copy link
Member

@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.

@tratch
Copy link

tratch commented Jul 9, 2019

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

@ganmacs
Copy link
Member

ganmacs commented Dec 23, 2019

seems to be solved. closed

@ganmacs ganmacs closed this as completed Dec 23, 2019
@fujimotos
Copy link
Member

@ganmacs Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v1
Projects
None yet
Development

No branches or pull requests

7 participants