Class: Inspec::Resources::MysqlSession

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

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, pass = nil) ⇒ MysqlSession

Returns a new instance of MysqlSession.



18
19
20
21
22
23
# File 'lib/resources/mysql_session.rb', line 18

def initialize(user = nil, pass = nil)
  @user = user
  @pass = pass
  init_fallback if user.nil? or pass.nil?
  skip_resource("Can't run MySQL SQL checks without authentication") if @user.nil? or @pass.nil?
end

Instance Method Details

#query(q, db = '') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/resources/mysql_session.rb', line 25

def query(q, db = '')
  # TODO: simple escape, must be handled by a library
  # that does this securely
  escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$')

  # run the query
  cmd = inspec.command("mysql -u#{@user} -p#{@pass} #{db} -s -e \"#{escaped_query}\"")
  out = cmd.stdout + "\n" + cmd.stderr
  if out =~ /Can't connect to .* MySQL server/ or
     out.downcase =~ /^error/
    # skip this test if the server can't run the query
    skip_resource("Can't connect to MySQL instance for SQL checks.")
  end

  # return the raw command output
  cmd
end

#to_sObject



43
44
45
# File 'lib/resources/mysql_session.rb', line 43

def to_s
  'MySQL Session'
end