Class: Inspec::Resources::MysqlSession

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

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, pass = nil, host = "localhost", port = nil, socket = nil) ⇒ MysqlSession

Returns a new instance of MysqlSession.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inspec/resources/mysql_session.rb', line 40

def initialize(user = nil, pass = nil, host = "localhost", port = nil, socket = nil)
  @user = user
  @pass = pass
  @host = host
  @port = port
  @socket = socket
  @db = nil
  init_fallback if user.nil? || pass.nil?
  raise Inspec::Exceptions::ResourceFailed, "Can't run MySQL SQL checks without authentication." if @user.nil? || @pass.nil?

  test_connection
end

Instance Method Details

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inspec/resources/mysql_session.rb', line 53

def query(q, db = "")
  raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?

  @db = db
  mysql_cmd = create_mysql_cmd(q, @db)

  cmd = if !@pass.nil?
          inspec.command(mysql_cmd, redact_regex: /(mysql -u\w+ -p).+(\s-(h|S).*)/)
        else
          inspec.command(mysql_cmd)
        end
  out = cmd.stdout + "\n" + cmd.stderr
  if cmd.exit_status != 0 || out =~ /Can't connect to .* MySQL server/ || out.downcase =~ /^error:.*/
    raise Inspec::Exceptions::ResourceFailed, "MySQL query with errors: #{out}"
  else
    Lines.new(cmd.stdout.strip, "MySQL query: #{q}", cmd.exit_status)
  end
end

#resource_idObject



72
73
74
# File 'lib/inspec/resources/mysql_session.rb', line 72

def resource_id
  "mysql_session:User:#{@user}:Host:#{@host}:Database:#{@db}"
end

#to_sObject



76
77
78
# File 'lib/inspec/resources/mysql_session.rb', line 76

def to_s
  "MySQL Session"
end