Use the mysql_session Chef InSpec audit resource to test SQL commands run against a MySQL database.
This resource is distributed along with Chef InSpec itself. You can use it automatically.
This resource first became available in v1.0.0 of InSpec.
A mysql_session resource block declares the username and password to use for the session, and then the command to be run:
describe mysql_session('username', 'password').query('QUERY') do
its('output') { should match(/expected-result/) }
end
where
mysql_session declares a username and password, connecting locally, with permission to run the queryquery('QUERY') contains the query to be runits('output') { should eq(/expected-result/) } compares the results of the query against the expected result in the testThe following examples show how to use this Chef InSpec audit resource.
sql = mysql_session('my_user','password')
describe sql.query('show databases like \'test\';') do
its('output') { should_not match(/test/) }
end
sql = mysql_session('my_user','password','db.example.com')
sql = mysql_session('my_user','password','localhost',3307)
sql = mysql_session('my_user','password', nil, nil, '/var/lib/mysql-default/mysqld.sock')
describe mysql_session('my_user','password').query('show tables in existing_database;') do
its('exit_status') { should eq(0) }
end
describe mysql_session('my_user','password').query('show tables in non_existent_database;') do
its('exit_status') { should_not eq(0) }
end
describe mysql_session('my_user','password').query('show tables in non_existent_database;') do
its('stderr') { should match(/Unknown database/) }
end
This Chef InSpec audit resource builds a command object and returns the the result object. For a full list of available matchers, please visit our matchers page.
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/inspec/resources/mysql_session/