2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/mysql_session"
|
2020-06-29 22:43:10 +00:00
|
|
|
require "inspec/resources/command"
|
2018-03-09 13:41:21 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::MysqlSession" do
|
|
|
|
it "verify mysql_session escaped login details with single quotes correctly" do
|
|
|
|
resource = load_resource("mysql_session",
|
2019-11-07 23:17:22 +00:00
|
|
|
"root",
|
|
|
|
%q{'%"'"&^*&()'*%})
|
|
|
|
|
|
|
|
_(resource.send(:create_mysql_cmd, "SELECT 1 FROM DUAL;"))
|
|
|
|
.must_equal(%q{mysql -uroot -p\'\%\"\'\"\&\^\*\&\(\)\'\*\% -h localhost -s -e "SELECT 1 FROM DUAL;"})
|
2018-03-09 13:41:21 +00:00
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify mysql_session omits optional username and password" do
|
|
|
|
resource = load_resource("mysql_session")
|
2019-11-07 23:17:22 +00:00
|
|
|
|
|
|
|
_(resource.send(:create_mysql_cmd, "SELECT 1 FROM DUAL;"))
|
|
|
|
.must_equal('mysql -h localhost -s -e "SELECT 1 FROM DUAL;"')
|
2018-03-09 13:41:21 +00:00
|
|
|
end
|
2020-06-29 22:43:10 +00:00
|
|
|
it "verify mysql_session redacts output" do
|
|
|
|
cmd = %q{mysql -uroot -p\'\%\"\'\"\&\^\*\&\(\)\'\*\% -h localhost -s -e "SELECT 1 FROM DUAL;"}
|
|
|
|
options = { redact_regex: /(mysql -u\w+ -p).+(\s-(h|S).*)/ }
|
|
|
|
resource = load_resource("command", cmd, options)
|
|
|
|
|
|
|
|
expected_to_s = %q{Command: `mysql -uroot -pREDACTED -h localhost -s -e "SELECT 1 FROM DUAL;"`}
|
|
|
|
_(resource.to_s).must_equal(expected_to_s)
|
|
|
|
end
|
2021-06-02 10:05:23 +00:00
|
|
|
it "fails when no user, password" do
|
|
|
|
resource = load_resource("mysql_session", nil, nil, "localhost", 3306)
|
|
|
|
_(resource.resource_failed?).must_equal true
|
|
|
|
_(resource.resource_exception_message).must_equal "Can't run MySQL SQL checks without authentication."
|
|
|
|
end
|
2021-06-03 13:32:43 +00:00
|
|
|
it "fails when no connection established" do
|
2021-06-02 10:05:23 +00:00
|
|
|
resource = load_resource("mysql_session", "root", "root", "localhost", 3306)
|
|
|
|
_(resource.resource_failed?).must_equal true
|
|
|
|
_(resource.resource_exception_message).must_include "MySQL query with errors"
|
|
|
|
end
|
|
|
|
|
2018-03-09 13:41:21 +00:00
|
|
|
end
|