Class: Inspec::Resources::OracledbSession
- Inherits:
-
Object
- Object
- Inspec::Resources::OracledbSession
- Defined in:
- lib/inspec/resources/oracledb_session.rb
Overview
STABILITY: Experimental This resource needs further testing and refinement
Instance Attribute Summary collapse
-
#as_db_role ⇒ Object
readonly
Returns the value of attribute as_db_role.
-
#as_os_user ⇒ Object
readonly
Returns the value of attribute as_os_user.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ OracledbSession
constructor
rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity.
- #query(q) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ OracledbSession
rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/inspec/resources/oracledb_session.rb', line 26 def initialize(opts = {}) @user = opts[:user] @password = opts[:password] || opts[:pass] if opts[:pass] Inspec.deprecate(:oracledb_session_pass_option, "The oracledb_session `pass` option is deprecated. Please use `password`.") end @host = opts[:host] || "localhost" @port = opts[:port] || "1521" @service = opts[:service] # connection as sysdba stuff return skip_resource "Option 'as_os_user' not available in Windows" if inspec.os.windows? && opts[:as_os_user] @su_user = opts[:as_os_user] @db_role = opts[:as_db_role] # we prefer sqlci although it is way slower than sqlplus, but it understands csv properly @sqlcl_bin = "sql" unless opts.key?(:sqlplus_bin) # don't use it if user specified sqlplus_bin option @sqlplus_bin = opts[:sqlplus_bin] || "sqlplus" return fail_resource "Can't run Oracle checks without authentication" if @su_user.nil? && (@user.nil? || @password.nil?) return fail_resource "You must provide a service name for the session" if @service.nil? end |
Instance Attribute Details
#as_db_role ⇒ Object (readonly)
Returns the value of attribute as_db_role.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def as_db_role @as_db_role end |
#as_os_user ⇒ Object (readonly)
Returns the value of attribute as_os_user.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def as_os_user @as_os_user end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def password @password end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def service @service end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
24 25 26 |
# File 'lib/inspec/resources/oracledb_session.rb', line 24 def user @user end |
Instance Method Details
#query(q) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/inspec/resources/oracledb_session.rb', line 51 def query(q) escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"') # escape tables with $ escaped_query = escaped_query.gsub("$", '\\$') p = nil # use sqlplus if sqlcl is not available if @sqlcl_bin && inspec.command(@sqlcl_bin).exist? bin = @sqlcl_bin opts = "set sqlformat csv\nSET FEEDBACK OFF" p = :parse_csv_result else bin = @sqlplus_bin opts = "SET MARKUP HTML ON\nSET PAGESIZE 32000\nSET FEEDBACK OFF" p = :parse_html_result end query = verify_query(escaped_query) query += ";" unless query.end_with?(";") if @db_role.nil? command = %{#{bin} "#{@user}"/"#{@password}"@#{@host}:#{@port}/#{@service} <<EOC\n#{opts}\n#{query}\nEXIT\nEOC} elsif @su_user.nil? command = %{#{bin} "#{@user}"/"#{@password}"@#{@host}:#{@port}/#{@service} as #{@db_role} <<EOC\n#{opts}\n#{query}\nEXIT\nEOC} else command = %{su - #{@su_user} -c "env ORACLE_SID=#{@service} #{bin} / as #{@db_role} <<EOC\n#{opts}\n#{query}\nEXIT\nEOC"} end cmd = inspec.command(command) out = cmd.stdout + "\n" + cmd.stderr if out.downcase =~ /^error/ # TODO: we need to throw an exception here # change once https://github.com/chef/inspec/issues/1205 is in warn "Could not execute the sql query #{out}" DatabaseHelper::SQLQueryResult.new(cmd, Hashie::Mash.new({})) end DatabaseHelper::SQLQueryResult.new(cmd, send(p, cmd.stdout)) end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/inspec/resources/oracledb_session.rb', line 89 def to_s "Oracle Session" end |