Class: Inspec::Resources::MssqlSession
- Inherits:
-
Object
- Object
- Inspec::Resources::MssqlSession
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#local_mode ⇒ Object
readonly
Returns the value of attribute local_mode.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ MssqlSession
constructor
A new instance of MssqlSession.
- #query(q) ⇒ Object
- #to_s ⇒ Object
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 48 49 50 51 52 53 54 |
# 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 @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] # 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
#host ⇒ Object (readonly)
Returns the value of attribute host.
32 33 34 |
# File 'lib/resources/mssql_session.rb', line 32 def host @host end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
32 33 34 |
# File 'lib/resources/mssql_session.rb', line 32 def instance @instance end |
#local_mode ⇒ Object (readonly)
Returns the value of attribute local_mode.
32 33 34 |
# File 'lib/resources/mssql_session.rb', line 32 def local_mode @local_mode end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
32 33 34 |
# File 'lib/resources/mssql_session.rb', line 32 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
32 33 34 |
# File 'lib/resources/mssql_session.rb', line 32 def port @port end |
#user ⇒ Object (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
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/resources/mssql_session.rb', line 56 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? 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_s ⇒ Object
82 83 84 |
# File 'lib/resources/mssql_session.rb', line 82 def to_s 'MSSQL session' end |