Class: Inspec::Resources::MysqlSession
- Inherits:
-
Object
- Object
- Inspec::Resources::MysqlSession
- Defined in:
- lib/inspec/resources/mysql_session.rb
Instance Method Summary collapse
-
#initialize(user = nil, pass = nil, host = "localhost", port = nil, socket = nil) ⇒ MysqlSession
constructor
A new instance of MysqlSession.
- #query(q, db = "") ⇒ Object
- #to_s ⇒ Object
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 |
# 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 init_fallback if user.nil? || pass.nil? skip_resource("Can't run MySQL SQL checks without authentication") if @user.nil? || @pass.nil? end |
Instance Method Details
#query(q, db = "") ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/inspec/resources/mysql_session.rb', line 50 def query(q, 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:.*/ Lines.new(out, "MySQL query with errors: #{q}", cmd.exit_status) else Lines.new(cmd.stdout.strip, "MySQL query: #{q}", cmd.exit_status) end end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/inspec/resources/mysql_session.rb', line 65 def to_s "MySQL Session" end |