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

add support of X-Forwarded-For header #1051

Merged
merged 2 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ def on_headers_complete(headers)
end
when /Origin/i
@origin = v
when /X-Forwarded-For/i
@remote_addr = v.split(",").first
end
}
if expect
Expand Down
81 changes: 49 additions & 32 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ def test_multi_json_with_add_remote_addr

end

def test_json_with_add_remote_addr_given_x_forwarded_for
d = create_driver(CONFIG + "add_remote_addr true")
time = Time.parse("2011-01-02 13:14:15 UTC").to_i

d.expect_emit "tag1", time, {"REMOTE_ADDR"=>"129.78.138.66", "a"=>1}
d.expect_emit "tag2", time, {"REMOTE_ADDR"=>"129.78.138.66", "a"=>1}

d.run do
d.expected_emits.each {|tag,_time,record|
res = post("/#{tag}", {"json"=>record.to_json, "time"=>_time.to_s}, {"X-Forwarded-For"=>"129.78.138.66, 127.0.0.1"})
assert_equal "200", res.code
}
end

end

def test_multi_json_with_add_remote_addr_given_x_forwarded_for
d = create_driver(CONFIG + "add_remote_addr true")

time = Time.parse("2011-01-02 13:14:15 UTC").to_i
events = [{"a"=>1},{"a"=>2}]
tag = "tag1"

d.expect_emit "tag1", time, {"REMOTE_ADDR"=>"129.78.138.66", "a"=>1}
d.expect_emit "tag1", time, {"REMOTE_ADDR"=>"129.78.138.66", "a"=>2}

d.run do
res = post("/#{tag}", {"json"=>events.to_json, "time"=>time.to_s}, {"X-Forwarded-For"=>"129.78.138.66, 127.0.0.1"})
assert_equal "200", res.code
end

end

def test_multi_json_with_add_http_headers
d = create_driver(CONFIG + "add_http_headers true")

Expand Down Expand Up @@ -302,50 +335,34 @@ def test_cors_allowed
d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]")
assert_equal ["http://foo.com"], d.instance.cors_allow_origins

test_in_http_cros_allowed = nil
acao = nil
time = Fluent::EventTime.new(Time.parse("2011-01-02 13:14:15 UTC").to_i)

begin
d.run do
Net::HTTP.start("127.0.0.1", PORT) do |http|
req = Net::HTTP::Post.new("/foo/bar", {"Origin" => "http://foo.com", "Content-Type" => "application/octet-stream"})
res = http.request(req)
d.expect_emit "tag1", time, {"a"=>1}
d.expect_emit "tag2", time, {"a"=>1}

acao = res["Access-Control-Allow-Origin"]
end
end
test_in_http_cros_allowed = true
rescue
test_in_http_cros_allowed = false
d.run do
d.expected_emits.each {|tag,_time,record|
res = post("/#{tag}", {"json"=>record.to_json, "time"=>_time.to_s}, {"Origin"=>"http://foo.com"})
assert_equal "200", res.code
assert_equal "http://foo.com", res["Access-Control-Allow-Origin"]
}
end

assert_equal true, test_in_http_cros_allowed
assert_equal "http://foo.com", acao
end

def test_cors_disallowed
d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]")
assert_equal ["http://foo.com"], d.instance.cors_allow_origins

test_in_http_cros_disallowed = nil
response_code = nil
time = Fluent::EventTime.new(Time.parse("2011-01-02 13:14:15 UTC").to_i)

begin
d.run do
Net::HTTP.start("127.0.0.1", PORT) do |http|
req = Net::HTTP::Post.new("/foo/bar", {"Origin" => "http://bar.com", "Content-Type" => "application/octet-stream"})
res = http.request(req)
d.expected_emits_length = 0
d.run do
res = post("/tag1", {"json"=>{"a"=>1}.to_json, "time"=>time.to_s}, {"Origin"=>"http://bar.com"})
assert_equal "403", res.code

response_code = res.code
end
end
test_in_http_cros_disallowed = true
rescue
test_in_http_cros_disallowed = false
res = post("/tag2", {"json"=>{"a"=>1}.to_json, "time"=>time.to_s}, {"Origin"=>"http://bar.com"})
assert_equal "403", res.code
end

assert_equal true, test_in_http_cros_disallowed
assert_equal "403", response_code
end

$test_in_http_connection_object_ids = []
Expand Down