Class: Inspec::Resources::OracledbSession

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inspec/resources/oracledb_session.rb', line 40

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

  @bin = "sqlplus"
  @host = opts[:host] || "localhost"
  @port = opts[:port] || "1521"
  @service = opts[:service]
  @su_user = opts[:as_os_user]
  @db_role = opts[:as_db_role]
  @sqlcl_bin = opts[:sqlcl_bin] || nil
  @sqlplus_bin = opts[:sqlplus_bin] || "sqlplus"

  # CHEF-28019: Support for TNS alias and environment variables
  @tns_alias = opts[:tns_alias]
  @env_vars = opts[:env] || {}

  skip_resource "Option 'as_os_user' not available in Windows" if inspec.os.windows? && su_user
  fail_resource "Can't run Oracle checks without authentication" unless su_user || (user || password)
end

Instance Attribute Details

#bin ⇒ Object (readonly)

Returns the value of attribute bin.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def bin
  @bin
end

#db_role ⇒ Object (readonly)

Returns the value of attribute db_role.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def db_role
  @db_role
end

#env_vars ⇒ Object (readonly)

Returns the value of attribute env_vars.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def env_vars
  @env_vars
end

#host ⇒ Object (readonly)

Returns the value of attribute host.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def host
  @host
end

#password ⇒ Object (readonly)

Returns the value of attribute password.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def password
  @password
end

#port ⇒ Object (readonly)

Returns the value of attribute port.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def port
  @port
end

#service ⇒ Object (readonly)

Returns the value of attribute service.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def service
  @service
end

#su_user ⇒ Object (readonly)

Returns the value of attribute su_user.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def su_user
  @su_user
end

#tns_alias ⇒ Object (readonly)

Returns the value of attribute tns_alias.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def tns_alias
  @tns_alias
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



37
38
39
# File 'lib/inspec/resources/oracledb_session.rb', line 37

def user
  @user
end

Instance Method Details

#query(sql) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/inspec/resources/oracledb_session.rb', line 64

def query(sql)
  raise Inspec::Exceptions::ResourceSkipped, "#{resource_exception_message}" if resource_skipped?
  raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?

  if @sqlcl_bin && inspec.command(@sqlcl_bin).exist?
    @bin = @sqlcl_bin
    format_options = "set sqlformat csv\nSET FEEDBACK OFF"
  else
    @bin = "#{@sqlplus_bin} -S"
    format_options = "SET PAGESIZE 32000\nSET FEEDBACK OFF\nSET UNDERLINE OFF"
  end

  command = command_builder(format_options, sql)
  inspec_cmd = inspec.command(command)
  out = inspec_cmd.stdout + "\n" + inspec_cmd.stderr

  if inspec_cmd.exit_status != 0 || out.downcase =~ /^error.*/
    raise Inspec::Exceptions::ResourceFailed, "Oracle query with errors: #{out}"
  else
    begin
      unless inspec_cmd.stdout.empty?
        DatabaseHelper::SQLQueryResult.new(inspec_cmd, parse_csv_result(inspec_cmd.stdout))
      else
        inspec_cmd.stdout
      end
    rescue Exception => ex
      raise Inspec::Exceptions::ResourceFailed, "Oracle query with exception: #{ex}"
    end
  end
end

#resource_id ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/inspec/resources/oracledb_session.rb', line 99

def resource_id
  if @tns_alias && !@tns_alias.empty?
    "#{@tns_alias}-#{@user}" # e.g., "XEPDB1_TCPS-USER"
  elsif @user
    "#{@host}-#{@port}-#{@user}" # e.g., "localhost-1521-USER"
  elsif @su_user
    "#{@host}-#{@port}-#{@su_user}"
  else
    ""
  end
end

#to_s ⇒ Object



95
96
97
# File 'lib/inspec/resources/oracledb_session.rb', line 95

def to_s
  "Oracle Session"
end