Skip to content

Commit 796b6ea

Browse files
authored
Merge pull request #121 from mmussomele/allow-auth-mech
Allow the authentication mechanism to be specified in configuration
2 parents ccd9065 + f140e3e commit 796b6ea

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

lib/fluent/plugin/mongo_auth.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ def self.included(klass)
88
config_param :password, :string, default: nil, secret: true
99
desc "MongoDB authentication database"
1010
config_param :auth_source, :string, default: nil
11+
desc "MongoDB authentication mechanism"
12+
config_param :auth_mech, :string, default: nil
1113
}
1214
end
1315
end
1416

1517
module MongoAuth
1618
def authenticate(client)
17-
unless @user.nil? || @password.nil?
18-
begin
19-
if @auth_source.nil?
20-
client = client.with(user: @user, password: @password)
21-
else
22-
client = client.with(user: @user, password: @password, auth_source: @auth_source)
23-
end
24-
rescue Mongo::Auth::Unauthorized => e
25-
log.fatal e
26-
exit!
19+
begin
20+
if [@user, @password, @auth_source].all?
21+
client = client.with(user: @user, password: @password, auth_source: @auth_source)
22+
elsif [@user, @password].all?
23+
client = client.with(user: @user, password: @password)
24+
elsif [@user, @auth_source, @auth_mech].all?
25+
client = client.with(user: @user, auth_source: @auth_source, auth_mech: @auth_mech.to_sym)
2726
end
27+
rescue Mongo::Auth::Unauthorized => e
28+
log.fatal e
29+
exit!
2830
end
2931
client
3032
end

lib/fluent/plugin/out_mongo.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def configure(conf)
108108

109109
super
110110

111+
if @auth_mech && !Mongo::Auth::SOURCES.has_key?(@auth_mech.to_sym)
112+
raise Fluent::ConfigError, Mongo::Auth::InvalidMechanism.new(@auth_mech.to_sym)
113+
end
114+
111115
if @connection_string.nil? && @database.nil?
112116
raise Fluent::ConfigError, "connection_string or database parameter is required"
113117
end

test/plugin/test_out_mongo.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ def test_configure_without_connection_string_or_database
9292
end
9393
end
9494

95+
def test_configure_auth_mechanism
96+
Mongo::Auth::SOURCES.each do |key, value|
97+
conf = default_config + %[
98+
auth_mech #{key}
99+
]
100+
d = create_driver(conf)
101+
assert_equal(key.to_s, d.instance.auth_mech)
102+
end
103+
assert_raise Fluent::ConfigError do
104+
conf = default_config + %[
105+
auth_mech invalid
106+
]
107+
d = create_driver(conf)
108+
end
109+
end
110+
95111
def test_configure_with_ssl
96112
conf = default_config + %[
97113
ssl true

0 commit comments

Comments
 (0)