diff --git a/x-pack/lib/filters/geoip/download_manager.rb b/x-pack/lib/filters/geoip/download_manager.rb index 351a47471..62f4630a6 100644 --- a/x-pack/lib/filters/geoip/download_manager.rb +++ b/x-pack/lib/filters/geoip/download_manager.rb @@ -78,7 +78,8 @@ module LogStash module Filters module Geoip class DownloadManager end end - # extract COPYRIGHT.txt, LICENSE.txt and GeoLite2-{ASN,City}.mmdb from .tgz to temp directory + # extract all files and folders from .tgz to vendor directory + # existing files folders will be replaced def unzip(zip_path) new_database_path = zip_path[0...-(GZ_EXT.length)] + DB_EXT temp_dir = Stud::Temporary.pathname @@ -86,9 +87,15 @@ module LogStash module Filters module Geoip class DownloadManager LogStash::Util::Tar.extract(zip_path, temp_dir) logger.debug("extract database to ", :path => temp_dir) + ::Dir.each_child(temp_dir) do |file| + path = ::File.join(temp_dir, file) - FileUtils.cp(::File.join(temp_dir, database_name_ext), new_database_path) - FileUtils.cp_r(::Dir.glob(::File.join(temp_dir, "{COPYRIGHT,LICENSE}.txt")), @vendor_path) + if !::File.directory?(path) && database_name_ext.eql?(file) + FileUtils.cp(path, new_database_path) + else + FileUtils.cp_r(path, @vendor_path) + end + end new_database_path end diff --git a/x-pack/spec/filters/geoip/download_manager_spec.rb b/x-pack/spec/filters/geoip/download_manager_spec.rb index efd3ac0ed..22e092c1d 100644 --- a/x-pack/spec/filters/geoip/download_manager_spec.rb +++ b/x-pack/spec/filters/geoip/download_manager_spec.rb @@ -3,6 +3,7 @@ # you may not use this file except in compliance with the Elastic License. require_relative 'test_helper' +require 'fileutils' require "filters/geoip/download_manager" describe LogStash::Filters::Geoip do @@ -105,13 +106,17 @@ describe LogStash::Filters::Geoip do let(:copyright_path) { get_file_path('COPYRIGHT.txt') } let(:license_path) { get_file_path('LICENSE.txt') } let(:readme_path) { get_file_path('README.txt') } + let(:folder_path) { get_file_path('inner') } + let(:folder_more_path) { ::File.join(get_file_path('inner'), 'more.txt') } + let(:folder_less_path) { ::File.join(get_file_path('inner'), 'less.txt') } - before do - file_path = ::File.expand_path("./fixtures/sample", ::File.dirname(__FILE__)) + after do + file_path = ::File.expand_path("./fixtures/sample.mmdb", ::File.dirname(__FILE__)) delete_file(file_path, copyright_path, license_path, readme_path) + FileUtils.rm_r folder_path end - it "should extract database and license related files" do + it "should extract all files in tarball" do path = ::File.expand_path("./fixtures/sample.tgz", ::File.dirname(__FILE__)) unzip_db_path = download_manager.send(:unzip, path) @@ -119,9 +124,10 @@ describe LogStash::Filters::Geoip do expect(::File.exist?(unzip_db_path)).to be_truthy expect(::File.exist?(copyright_path)).to be_truthy expect(::File.exist?(license_path)).to be_truthy - expect(::File.exist?(readme_path)).to be_falsey - - delete_file(unzip_db_path, copyright_path, license_path) + expect(::File.exist?(readme_path)).to be_truthy + expect(::File.directory?(folder_path)).to be_truthy + expect(::File.exist?(folder_more_path)).to be_truthy + expect(::File.exist?(folder_less_path)).to be_truthy end end diff --git a/x-pack/spec/filters/geoip/fixtures/sample.tgz b/x-pack/spec/filters/geoip/fixtures/sample.tgz index 22bb93cb7..3336704a4 100644 Binary files a/x-pack/spec/filters/geoip/fixtures/sample.tgz and b/x-pack/spec/filters/geoip/fixtures/sample.tgz differ