diff --git a/lib/inspec/resources/oracledb_session.rb b/lib/inspec/resources/oracledb_session.rb index 8bdb699b2..d21b516b0 100644 --- a/lib/inspec/resources/oracledb_session.rb +++ b/lib/inspec/resources/oracledb_session.rb @@ -1,7 +1,5 @@ require "inspec/resources/command" require "inspec/utils/database_helpers" -require "htmlentities" -require "rexml/document" require "hashie/mash" require "csv" @@ -48,18 +46,16 @@ module Inspec::Resources if @sqlcl_bin && inspec.command(@sqlcl_bin).exist? @bin = @sqlcl_bin format_options = "set sqlformat csv\nSET FEEDBACK OFF" - parser = :parse_csv_result else @bin = "#{@sqlplus_bin} -S" - format_options = "SET MARKUP HTML ON\nSET PAGESIZE 32000\nSET FEEDBACK OFF" - parser = :parse_html_result + format_options = "SET MARKUP CSV ON\nSET PAGESIZE 32000\nSET FEEDBACK OFF" end command = command_builder(format_options, sql) inspec_cmd = inspec.command(command) DatabaseHelper::SQLQueryResult.new(inspec_cmd, - send(parser, inspec_cmd.stdout)) + send(:parse_csv_result, inspec_cmd.stdout)) end def to_s @@ -95,55 +91,13 @@ module Inspec::Resources query end - def parse_csv_result(stdout) - output = stdout.delete(/\r/) - table = CSV.parse(output, { headers: true }) - - # convert to hash - headers = table.headers - - results = table.map do |row| - res = {} - headers.each do |header| - res[header.downcase] = row[header] - end - Hashie::Mash.new(res) - end - results - end - - def parse_html_result(stdout) - result = stdout - # make oracle html valid html by removing the p tag, it does not include a closing tag - result = result.gsub("

", "").gsub("

", "").gsub("
", "") - doc = REXML::Document.new result - table = doc.elements["table"] - hash = [] - unless table.nil? - rows = table.elements.to_a - headers = rows[0].elements.to_a("th").map { |entry| entry.text.strip } - rows.delete_at(0) - - # iterate over each row, first row is header - hash = [] - if !rows.nil? && !rows.empty? - hash = rows.map do |row| - res = {} - entries = row.elements.to_a("td") - # ignore if we have empty entries, oracle is adding th rows in between - return nil if entries.empty? - - headers.each_with_index do |header, index| - # we need htmlentities since we do not have nokogiri - coder = HTMLEntities.new - val = coder.decode(entries[index].text).strip - res[header.downcase] = val - end - Hashie::Mash.new(res) - end.compact - end - end - hash + def parse_csv_result(stdout, bin) + output = if stdout.respond_to?(:to_str) + stdout.strip + else + stdout.delete(/\r/) + end + CSV.parse(output, headers: true, header_converters: converter).map{ |row| Hashie::Mash.new(row.to_h) } end end end