Skip to content

Commit c9436d7

Browse files
committed
Fix: driver loading when file not accessible
resolves logstash-plugins#369 (also related logstash-plugins#362)
1 parent ae2523b commit c9436d7

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

lib/logstash/inputs/jdbc.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def set_value_tracker(instance)
270270
end
271271

272272
def run(queue)
273+
load_driver
273274
if @schedule
274275
@scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
275276
@scheduler.cron @schedule do

lib/logstash/plugin_mixins/jdbc/jdbc.rb

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -140,47 +140,58 @@ def jdbc_connect
140140

141141
private
142142

143+
def load_driver
144+
if @drivers_loaded.false?
145+
require "java"
146+
require "sequel"
147+
require "sequel/adapters/jdbc"
148+
149+
load_driver_jars
150+
begin
151+
Sequel::JDBC.load_driver(@jdbc_driver_class)
152+
rescue Sequel::AdapterNotFound => e # Sequel::AdapterNotFound, "#{@jdbc_driver_class} not loaded"
153+
# fix this !!!
154+
message = if jdbc_driver_library_set?
155+
"Are you sure you've included the correct jdbc driver in :jdbc_driver_library?"
156+
else
157+
":jdbc_driver_library is not set, are you sure you included " +
158+
"the proper driver client libraries in your classpath?"
159+
end
160+
raise LogStash::PluginLoadingError, "#{e}. #{message}"
161+
end
162+
@drivers_loaded.make_true
163+
end
164+
end
165+
143166
def load_driver_jars
144-
unless @jdbc_driver_library.nil? || @jdbc_driver_library.empty?
167+
if jdbc_driver_library_set?
145168
@jdbc_driver_library.split(",").each do |driver_jar|
169+
@logger.debug("loading #{driver_jar}")
170+
# load 'driver.jar' is different than load 'some.rb' as it only causes the file to be added to
171+
# JRuby's class-loader lookup (class) path - won't raise a LoadError when file is not readable
172+
unless FileTest.readable?(driver_jar)
173+
raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, " +
174+
"file not readable (please check user and group permissions for the path)"
175+
end
146176
begin
147-
@logger.debug("loading #{driver_jar}")
148-
# Use https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-jar-files to make classes from jar
149-
# available
150177
require driver_jar
151178
rescue LoadError => e
152179
raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e.message}"
180+
rescue StandardError => e
181+
raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e}"
153182
end
154183
end
155184
end
156185
end
157186

158-
private
159-
def open_jdbc_connection
160-
require "java"
161-
require "sequel"
162-
require "sequel/adapters/jdbc"
187+
def jdbc_driver_library_set?
188+
!@jdbc_driver_library.nil? && !@jdbc_driver_library.empty?
189+
end
163190

191+
def open_jdbc_connection
192+
# at this point driver is already loaded
164193
Sequel.application_timezone = @plugin_timezone.to_sym
165-
if @drivers_loaded.false?
166-
begin
167-
load_driver_jars
168-
Sequel::JDBC.load_driver(@jdbc_driver_class)
169-
rescue LogStash::Error => e
170-
# raised in load_drivers, e.cause should be the caught Java exceptions
171-
raise LogStash::PluginLoadingError, "#{e.message} and #{e.cause.message}"
172-
rescue Sequel::AdapterNotFound => e
173-
# fix this !!!
174-
message = if @jdbc_driver_library.nil?
175-
":jdbc_driver_library is not set, are you sure you included
176-
the proper driver client libraries in your classpath?"
177-
else
178-
"Are you sure you've included the correct jdbc driver in :jdbc_driver_library?"
179-
end
180-
raise LogStash::PluginLoadingError, "#{e}. #{message}"
181-
end
182-
@drivers_loaded.make_true
183-
end
194+
184195
@database = jdbc_connect()
185196
@database.extension(:pagination)
186197
if @jdbc_default_timezone

spec/inputs/jdbc_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,38 @@
12911291
end
12921292
end
12931293

1294+
context "when an unreadable jdbc_driver_path entry is present" do
1295+
let(:driver_jar_path) do
1296+
jar_file = $CLASSPATH.find { |name| name.index(Jdbc::Derby.driver_jar) }
1297+
raise "derby jar not found on class-path" unless jar_file
1298+
jar_file.sub('file:', '')
1299+
end
1300+
1301+
let(:invalid_driver_jar_path) do
1302+
path = File.join(Dir.mktmpdir, File.basename(driver_jar_path))
1303+
FileUtils.cp driver_jar_path, path
1304+
FileUtils.chmod "u=x,go=", path
1305+
path
1306+
end
1307+
1308+
let(:settings) do
1309+
{ "statement" => "SELECT * from types_table", "jdbc_driver_library" => invalid_driver_jar_path }
1310+
end
1311+
1312+
before do
1313+
plugin.register
1314+
end
1315+
1316+
after do
1317+
plugin.stop
1318+
end
1319+
1320+
it "raise a loading error" do
1321+
expect { plugin.run(queue) }.
1322+
to raise_error(LogStash::PluginLoadingError, /unable to load .*? from :jdbc_driver_library, file not readable/)
1323+
end
1324+
end
1325+
12941326
context "when using prepared statements" do
12951327
let(:last_run_value) { 250 }
12961328
let(:expected_queue_size) { 100 }

0 commit comments

Comments
 (0)