Skip to content

Commit

Permalink
Add validation to active_gate_url and api_token (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Feb 8, 2022
1 parent 7835c57 commit ae06928
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/dynatrace_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Plugin
class DynatraceOutputConstants
# The version of the Dynatrace output plugin
def self.version
'0.1.6'
'0.2.0'
end
end
end
Expand Down
21 changes: 20 additions & 1 deletion lib/fluent/plugin/out_dynatrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def configure(conf)
compat_parameters_convert(conf, :inject)
super

@uri = URI.parse(@active_gate_url)
raise Fluent::ConfigError, 'api_token is empty' if @api_token.empty?

@uri = parse_and_validate_uri(@active_gate_url)

@agent = Net::HTTP.new(@uri.host, @uri.port)

return unless uri.scheme == 'https'
Expand Down Expand Up @@ -173,6 +176,22 @@ def failure_message(res)

"failed to request #{uri} (#{res_summary})"
end

#############################################

private

def parse_and_validate_uri(uri_string)
raise Fluent::ConfigError, 'active_gate_url is empty' if uri_string.empty?

uri = URI.parse(uri_string)
raise Fluent::ConfigError, 'active_gate_url scheme must be http or https' unless
%w[http https].include?(uri.scheme)

raise Fluent::ConfigError, 'active_gate_url must include an authority' if uri.host.nil?

uri
end
end
end
end
64 changes: 64 additions & 0 deletions test/plugin/out_dynatrace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,70 @@ def create_driver(conf = config)
assert_equal true, d.instance.agent.original_agent.use_ssl?
assert_equal OpenSSL::SSL::VERIFY_NONE, d.instance.agent.original_agent.verify_mode
end

test 'should throw if active_gate_url is not set' do
assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url
api_token secret
))
end
end

test 'with IPv6 as host should not throw' do
d = create_driver(%(
active_gate_url http://[::1]//logs
api_token secret
))
assert_equal 'http://[::1]//logs', d.instance.active_gate_url
assert_equal 'secret', d.instance.api_token
end

test 'should throw if active_gate_url scheme is not http or https' do
assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url ./relative/path
api_token secret
))
end

assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url example.dynatrace.com/logs
api_token secret
))
end

assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url example
api_token secret
))
end

assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url ssh://example.dynatrace.com/logs
api_token secret
))
end

assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url http:path/to/logs
api_token secret
))
end
end

test 'should throw if api_token is not set' do
assert_raises Fluent::ConfigError do
create_driver(%(
active_gate_url https://example.dynatrace.com/logs
api_token
))
end
end
end

sub_test_case 'tests for #write' do
Expand Down

0 comments on commit ae06928

Please sign in to comment.