Skip to content

Commit c30c3ff

Browse files
Merge pull request #9 from HatsuneMiku3939/feature/proxy-auth-support
add proxy, basic_auth support
2 parents fd79dc8 + b0481af commit c30c3ff

File tree

7 files changed

+293
-6
lines changed

7 files changed

+293
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ If status_only is true, body is not parsed.
139139

140140
The timeout of each request.
141141

142+
### proxy (string) (optional, default: nil)
143+
144+
The HTTP proxy URL to use for each requests
145+
146+
### user (string) (optional, default: nil)
147+
148+
The user for basic auth
149+
150+
### password (string) (optional, default: nil)
151+
152+
The password for basic auth
153+
142154
## In case of remote error
143155

144156
### Can receive response from remote

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Rake::TestTask.new(:test) do |t|
1010
FileUtils.rm "stub_server.log"
1111
end
1212

13+
if File.exists? "stub_proxy.log"
14+
puts "clear stub_proxy.log"
15+
FileUtils.rm "stub_proxy.log"
16+
end
17+
1318
t.libs.push("lib", "test")
1419
t.test_files = FileList["test/**/test_*.rb"]
1520
t.verbose = true

lib/fluent/plugin/in_http_pull.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ def initialize
3535
config_param :interval, :time
3636
desc 'status_only'
3737
config_param :status_only, :bool, default: false
38-
desc 'timeout second of each request'
38+
desc 'The timeout stime of each request'
3939
config_param :timeout, :time, default: 10
40+
desc 'The HTTP proxy URL to use for each requests'
41+
config_param :proxy, :string, default: nil
42+
43+
desc 'user of basic auth'
44+
config_param :user, :string, default: nil
45+
desc 'password of basic auth'
46+
config_param :password, :string, default: nil
4047

4148
def configure(conf)
4249
compat_parameters_convert(conf, :parser)
@@ -55,9 +62,13 @@ def on_timer
5562
record = { "url" => @url }
5663

5764
begin
58-
res = RestClient::Request.execute(method: :get,
59-
url: @url,
60-
timeout: @timeout)
65+
request_options = { method: :get, url: @url, timeout: @timeout }
66+
67+
request_options[:proxy] = @proxy if @proxy
68+
request_options[:user] = @user if @user
69+
request_options[:password] = @password if @password
70+
71+
res = RestClient::Request.execute request_options
6172
record["status"] = res.code
6273
record["body"] = res.body
6374
rescue StandardError => err

test/helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
# require stub_server
2525
require "test/helper/stub_server"
26+
require "test/helper/stub_proxy"
2627

2728
Test::Unit::TestCase.include(Fluent::Test::Helpers)
2829
Test::Unit::TestCase.extend(Fluent::Test::Helpers)

test/helper/stub_proxy.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'webrick'
2+
require 'webrick/httpproxy'
3+
4+
class StubProxy
5+
def initialize
6+
create_proxy
7+
end
8+
9+
def start
10+
@thread = Thread.new { @proxy.start }
11+
end
12+
13+
def shutdown
14+
@proxy.shutdown
15+
16+
# wait until webrick was shutting down
17+
while true
18+
break if @thread.status == false
19+
20+
# issue webrick shutdown once more
21+
@proxy.shutdown
22+
sleep 1
23+
end
24+
25+
# then exit thread
26+
@thread.exit
27+
end
28+
29+
private
30+
def create_proxy
31+
@log_file = File.open("stub_proxy.log", "a+")
32+
@log = WEBrick::Log.new @log_file
33+
@access_log = [
34+
[@log_file, WEBrick::AccessLog::COMBINED_LOG_FORMAT],
35+
]
36+
37+
@proxy = WEBrick::HTTPProxyServer.new :Port => 4040, :Logger => @log, :AccessLog => @access_log
38+
end
39+
end

test/helper/stub_server.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize
1010
@server.mount_proc '/timeout', &method(:timeout)
1111
@server.mount_proc '/internal_error', &method(:internal_error)
1212
@server.mount_proc '/redirect', &method(:redirect)
13+
@server.mount_proc '/protected', &method(:protected)
1314
end
1415

1516
def start
@@ -70,4 +71,14 @@ def internal_error(req, res)
7071
def redirect(req, res)
7172
res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect, "http://127.0.0.1:3939/"
7273
end
74+
75+
def protected(req, res)
76+
WEBrick::HTTPAuth.basic_auth(req, res, 'protected') do |user, password|
77+
user == 'HatsuneMiku' && password == '3939'
78+
end
79+
80+
res.status = 200
81+
res['Content-Type'] = 'application/json'
82+
res.body = '{ "status": "OK" }'
83+
end
7384
end

test/plugin/test_in_http_pull.rb

Lines changed: 210 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class HttpPullInputTest < Test::Unit::TestCase
77
@stub_server = nil
88

99
setup do
10-
Fluent::Test.setup
11-
1210
@stub_server = StubServer.new
1311
@stub_server.start
1412
end
@@ -39,6 +37,27 @@ class HttpPullInputTest < Test::Unit::TestCase
3937

4038
assert_equal(10, d.instance.timeout)
4139
end
40+
41+
test 'user' do
42+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
43+
assert_equal("test", d.instance.tag)
44+
45+
assert_equal(nil, d.instance.user)
46+
end
47+
48+
test 'password' do
49+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
50+
assert_equal("test", d.instance.tag)
51+
52+
assert_equal(nil, d.instance.password)
53+
end
54+
55+
test 'proxy' do
56+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
57+
assert_equal("test", d.instance.tag)
58+
59+
assert_equal(nil, d.instance.proxy)
60+
end
4261
end
4362

4463
sub_test_case "success case" do
@@ -245,6 +264,195 @@ class HttpPullInputTest < Test::Unit::TestCase
245264
end
246265
end
247266

267+
sub_test_case "remote is prtected by basic auth" do
268+
TEST_AUTH_SUCCESS_CONFIG = %[
269+
tag test
270+
url http://127.0.0.1:3939/protected
271+
timeout 2s
272+
user HatsuneMiku
273+
password 3939
274+
275+
interval 3s
276+
format json
277+
]
278+
279+
TEST_AUTH_FAIL_CONFIG = %[
280+
tag test
281+
url http://127.0.0.1:3939/protected
282+
timeout 2s
283+
user HatsuneMiku
284+
password wrong_password
285+
286+
interval 3s
287+
format json
288+
]
289+
290+
TEST_AUTH_FAIL_NOT_GIVEN_CONFIG = %[
291+
tag test
292+
url http://127.0.0.1:3939/protected
293+
timeout 2s
294+
295+
interval 3s
296+
format json
297+
]
298+
299+
test 'interval 3 with corrent password' do
300+
d = create_driver TEST_AUTH_SUCCESS_CONFIG
301+
assert_equal("test", d.instance.tag)
302+
assert_equal(3, d.instance.interval)
303+
304+
d.run(timeout: 8) do
305+
sleep 7
306+
end
307+
assert_equal(2, d.events.size)
308+
309+
d.events.each do |tag, time, record|
310+
assert_equal("test", tag)
311+
312+
assert_equal({"url"=>"http://127.0.0.1:3939/protected","status"=>200, "message"=>{"status"=>"OK"}}, record)
313+
assert(time.is_a?(Fluent::EventTime))
314+
end
315+
end
316+
317+
test 'interval 3 with wrong password' do
318+
d = create_driver TEST_AUTH_FAIL_CONFIG
319+
assert_equal("test", d.instance.tag)
320+
assert_equal(3, d.instance.interval)
321+
322+
d.run(timeout: 8) do
323+
sleep 7
324+
end
325+
assert_equal(2, d.events.size)
326+
327+
d.events.each do |tag, time, record|
328+
assert_equal("test", tag)
329+
330+
assert_equal("http://127.0.0.1:3939/protected", record["url"])
331+
assert(time.is_a?(Fluent::EventTime))
332+
333+
assert_equal(401, record["status"])
334+
assert_not_nil(record["error"])
335+
end
336+
end
337+
338+
test 'interval 3 without auth info' do
339+
d = create_driver TEST_AUTH_FAIL_CONFIG
340+
assert_equal("test", d.instance.tag)
341+
assert_equal(3, d.instance.interval)
342+
343+
d.run(timeout: 8) do
344+
sleep 7
345+
end
346+
assert_equal(2, d.events.size)
347+
348+
d.events.each do |tag, time, record|
349+
assert_equal("test", tag)
350+
351+
assert_equal("http://127.0.0.1:3939/protected", record["url"])
352+
assert(time.is_a?(Fluent::EventTime))
353+
354+
assert_equal(401, record["status"])
355+
assert_not_nil(record["error"])
356+
end
357+
end
358+
end
359+
360+
sub_test_case "success case behind proxy" do
361+
TEST_INTERVAL_3_PROXY_CONFIG = %[
362+
tag test
363+
url http://127.0.0.1:3939
364+
proxy http://127.0.0.1:4040
365+
366+
interval 3s
367+
format none
368+
status_only true
369+
]
370+
371+
TEST_INTERVAL_3_REDIRECT_PROXY_CONFIG = %[
372+
tag test
373+
url http://127.0.0.1:3939/redirect
374+
proxy http://127.0.0.1:4040
375+
376+
interval 3s
377+
format json
378+
]
379+
380+
TEST_AUTH_SUCCESS_PROXY_CONFIG = %[
381+
tag test
382+
url http://127.0.0.1:3939/protected
383+
proxy http://127.0.0.1:4040
384+
timeout 2s
385+
user HatsuneMiku
386+
password 3939
387+
388+
interval 3s
389+
format json
390+
]
391+
392+
setup do
393+
@proxy_server = StubProxy.new
394+
@proxy_server.start
395+
end
396+
397+
teardown do
398+
@proxy_server.shutdown
399+
end
400+
401+
test 'interval 3 with status_only' do
402+
d = create_driver TEST_INTERVAL_3_PROXY_CONFIG
403+
assert_equal("test", d.instance.tag)
404+
assert_equal(3, d.instance.interval)
405+
406+
d.run(timeout: 8) do
407+
sleep 7
408+
end
409+
assert_equal(2, d.events.size)
410+
411+
d.events.each do |tag, time, record|
412+
assert_equal("test", tag)
413+
414+
assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200}, record)
415+
assert(time.is_a?(Fluent::EventTime))
416+
end
417+
end
418+
419+
test 'interval 3 with redirect' do
420+
d = create_driver TEST_INTERVAL_3_REDIRECT_PROXY_CONFIG
421+
assert_equal("test", d.instance.tag)
422+
assert_equal(3, d.instance.interval)
423+
424+
d.run(timeout: 8) do
425+
sleep 7
426+
end
427+
assert_equal(2, d.events.size)
428+
429+
d.events.each do |tag, time, record|
430+
assert_equal("test", tag)
431+
432+
assert_equal({"url"=>"http://127.0.0.1:3939/redirect","status"=>200, "message"=>{"status"=>"OK"}}, record)
433+
assert(time.is_a?(Fluent::EventTime))
434+
end
435+
end
436+
437+
test 'interval 3 with corrent password' do
438+
d = create_driver TEST_AUTH_SUCCESS_PROXY_CONFIG
439+
assert_equal("test", d.instance.tag)
440+
assert_equal(3, d.instance.interval)
441+
442+
d.run(timeout: 8) do
443+
sleep 7
444+
end
445+
assert_equal(2, d.events.size)
446+
447+
d.events.each do |tag, time, record|
448+
assert_equal("test", tag)
449+
450+
assert_equal({"url"=>"http://127.0.0.1:3939/protected","status"=>200, "message"=>{"status"=>"OK"}}, record)
451+
assert(time.is_a?(Fluent::EventTime))
452+
end
453+
end
454+
end
455+
248456
private
249457

250458
def create_driver(conf)

0 commit comments

Comments
 (0)