Class: Inspec::Resources::MssqlSession

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

Overview

STABILITY: Experimental This resource needs further testing and refinement

This requires the ‘sqlcmd` tool available on platform

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MssqlSession

Returns a new instance of MssqlSession.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inspec/resources/mssql_session.rb', line 31

def initialize(opts = {})
  @user = opts[:user]
  @password = opts[:password] || opts[:pass]
  if opts[:pass]
    Inspec.deprecate(:mssql_session_pass_option, "The mssql_session `pass` option is deprecated. Please use `password`.")
  end
  @local_mode = opts[:local_mode]
  unless local_mode?
    @host = opts[:host] || "localhost"
    if opts.key?(:port)
      @port = opts[:port]
    else
      @port = "1433"
    end
  end
  @instance = opts[:instance]
  @db_name = opts[:db_name]

  # check if sqlcmd is available
  raise Inspec::Exceptions::ResourceSkipped, "sqlcmd is missing" unless inspec.command("sqlcmd").exist?
  # check that database is reachable
  raise Inspec::Exceptions::ResourceSkipped, "Can't connect to the MS SQL Server." unless test_connection
end

Instance Attribute Details

#db_nameObject (readonly)

Returns the value of attribute db_name.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def db_name
  @db_name
end

#hostObject (readonly)

Returns the value of attribute host.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def host
  @host
end

#instanceObject (readonly)

Returns the value of attribute instance.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def instance
  @instance
end

#local_modeObject (readonly)

Returns the value of attribute local_mode.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def local_mode
  @local_mode
end

#passwordObject (readonly)

Returns the value of attribute password.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



30
31
32
# File 'lib/inspec/resources/mssql_session.rb', line 30

def user
  @user
end

Instance Method Details

#query(q) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



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
# File 'lib/inspec/resources/mssql_session.rb', line 55

def query(q) # rubocop:disable Metrics/PerceivedComplexity
  escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$')
  # surpress 'x rows affected' in SQLCMD with 'set nocount on;'
  cmd_string = "sqlcmd -Q \"set nocount on; #{escaped_query}\" -W -w 1024 -s ','"
  cmd_string += " -U '#{@user}' -P '#{@password}'" unless @user.nil? || @password.nil?
  cmd_string += " -d '#{@db_name}'" unless @db_name.nil?
  unless local_mode?
    if @port.nil?
      cmd_string += " -S '#{@host}"
    else
      cmd_string += " -S '#{@host},#{@port}"
    end
    if @instance.nil?
      cmd_string += "'"
    else
      cmd_string += "\\#{@instance}'"
    end
  end
  cmd = inspec.command(cmd_string)
  out = cmd.stdout + "\n" + cmd.stderr
  if cmd.exit_status != 0 || out =~ /Sqlcmd: Error/
    raise Inspec::Exceptions::ResourceFailed, "Could not execute the sql query #{out}"
  else
    DatabaseHelper::SQLQueryResult.new(cmd, parse_csv_result(cmd))
  end
end

#to_sObject



82
83
84
# File 'lib/inspec/resources/mssql_session.rb', line 82

def to_s
  "MSSQL session"
end