Class: Inspec::Resources::MssqlSession

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/resources/mssql_session.rb', line 33

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] || '1433'
  @instance = opts[:instance]

  # 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

#hostObject (readonly)

Returns the value of attribute host.



32
33
34
# File 'lib/resources/mssql_session.rb', line 32

def host
  @host
end

#instanceObject (readonly)

Returns the value of attribute instance.



32
33
34
# File 'lib/resources/mssql_session.rb', line 32

def instance
  @instance
end

#passwordObject (readonly)

Returns the value of attribute password.



32
33
34
# File 'lib/resources/mssql_session.rb', line 32

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



32
33
34
# File 'lib/resources/mssql_session.rb', line 32

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



32
33
34
# File 'lib/resources/mssql_session.rb', line 32

def user
  @user
end

Instance Method Details

#query(q) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/resources/mssql_session.rb', line 49

def query(q)
  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?
  if @instance.nil?
    cmd_string += " -S '#{@host},#{@port}'"
  else
    cmd_string += " -S '#{@host},#{@port}\\#{@instance}'"
  end
  cmd = inspec.command(cmd_string)
  out = cmd.stdout + "\n" + cmd.stderr
  if cmd.exit_status != 0 || out =~ /Sqlcmd: 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({}))
  else
    DatabaseHelper::SQLQueryResult.new(cmd, parse_csv_result(cmd))
  end
end

#to_sObject



71
72
73
# File 'lib/resources/mssql_session.rb', line 71

def to_s
  'MSSQL session'
end