Class: Inspec::Resources::OracledbSession

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/oracledb_session.rb

Overview

STABILITY: Experimental This resource needs further testing and refinement

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject (readonly)

Returns the value of attribute host.



26
27
28
# File 'lib/resources/oracledb_session.rb', line 26

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



26
27
28
# File 'lib/resources/oracledb_session.rb', line 26

def password
  @password
end

#serviceObject (readonly)

Returns the value of attribute service.



26
27
28
# File 'lib/resources/oracledb_session.rb', line 26

def service
  @service
end

#userObject (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
# 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

  command = "echo \"#{opts}\n#{verify_query(escaped_query)}\nEXIT\" | #{bin} -s #{@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_sObject



76
77
78
# File 'lib/resources/oracledb_session.rb', line 76

def to_s
  'Oracle Session'
end