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.



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

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"
    @port = opts[:port]
  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.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def db_name
  @db_name
end

#hostObject (readonly)

Returns the value of attribute host.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def host
  @host
end

#instanceObject (readonly)

Returns the value of attribute instance.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def instance
  @instance
end

#local_modeObject (readonly)

Returns the value of attribute local_mode.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def local_mode
  @local_mode
end

#passwordObject (readonly)

Returns the value of attribute password.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



35
36
37
# File 'lib/inspec/resources/mssql_session.rb', line 35

def user
  @user
end

Instance Method Details

#query(q) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



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

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.stdout))
  end
end

#resource_idObject



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

def resource_id
  "mssql_session:User:#{@user}:Host:#{@host}:Database:#{@db_name}:Instance:#{@instance}"
end

#to_sObject



87
88
89
# File 'lib/inspec/resources/mssql_session.rb', line 87

def to_s
  "MSSQL session"
end