Class: Inspec::Resources::OracledbSession
- Inherits:
-
Object
- Object
- Inspec::Resources::OracledbSession
- Defined in:
- lib/resources/oracledb_session.rb
Overview
STABILITY: Experimental This resource needs further testing and refinement
Instance Attribute Summary collapse
-
#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
A new instance of OracledbSession.
- #query(q) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ OracledbSession
Returns a new instance of OracledbSession.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/resources/oracledb_session.rb', line 27 def initialize(opts = {}) @user = opts[:user] @password = opts[:password] || opts[:pass] if opts[:pass] warn '[DEPRECATED] use `password` option to supply password instead of `pass`' end @host = opts[:host] || 'localhost' @port = opts[:port] || '1521' @service = opts[:service] # we prefer sqlci although it is way slower than sqlplus, but it understands csv properly @sqlcl_bin = 'sql' @sqlplus_bin = opts[:sqlplus_bin] || 'sqlplus' return skip_resource "Can't run Oracle checks without authentication" if @user.nil? || @password.nil? return skip_resource 'You must provide a service name for the session' if @service.nil? end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
26 27 28 |
# File 'lib/resources/oracledb_session.rb', line 26 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
26 27 28 |
# File 'lib/resources/oracledb_session.rb', line 26 def password @password end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
26 27 28 |
# File 'lib/resources/oracledb_session.rb', line 26 def service @service end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
26 27 28 |
# File 'lib/resources/oracledb_session.rb', line 26 def user @user end |
Instance Method Details
#query(q) ⇒ Object
46 47 48 49 50 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 |
# File 'lib/resources/oracledb_session.rb', line 46 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 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 FEEDBACK OFF" p = :parse_html_result end query = verify_query(escaped_query) query += ';' unless query.end_with?(';') command = %{echo "#{opts}\n#{query}\nEXIT" | #{bin} "#{@user}"/"#{@password}"@#{@host}:#{@port}/#{@service}} 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
78 79 80 |
# File 'lib/resources/oracledb_session.rb', line 78 def to_s 'Oracle Session' end |