inspec/docs/resources/mysql_session.md.erb

76 lines
1.7 KiB
Text
Raw Normal View History

2016-09-22 14:43:57 +02:00
---
title: About the mysql_session Resource
---
# mysql_session
Use the `mysql_session` InSpec audit resource to test SQL commands run against a MySQL database.
## Syntax
2016-09-22 14:43:57 +02:00
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 eq('') }
end
where
* `mysql_session` declares a username and password with permission to run the query
* `query('QUERY')` contains the query to be run
* `its('output') { should eq('') }` compares the results of the query against the expected result in the test
## Matchers
2016-09-22 14:43:57 +02:00
This InSpec audit resource has the following matchers:
### be
2016-09-22 14:43:57 +02:00
<%= partial "/shared/matcher_be" %>
### cmp
2016-09-22 14:43:57 +02:00
<%= partial "/shared/matcher_cmp" %>
### eq
2016-09-22 14:43:57 +02:00
<%= partial "/shared/matcher_eq" %>
### include
2016-09-22 14:43:57 +02:00
<%= partial "/shared/matcher_include" %>
### match
2016-09-22 14:43:57 +02:00
<%= partial "/shared/matcher_match" %>
### output
2016-09-22 14:43:57 +02:00
The `output` matcher tests the results of the query:
its('output') { should eq(/^0/) }
## Examples
2016-09-22 14:43:57 +02:00
The following examples show how to use this InSpec audit resource.
### Test for matching databases
2016-09-22 14:43:57 +02:00
sql = mysql_session('my_user','password')
describe sql.query('show databases like \'test\';') do
its('stdout') { should_not match(/test/) }
end
### Alternate Connection: Different Host
sql = mysql_session('my_user','password','db.example.com')
### Alternate Connection: Different Port
sql = mysql_seesion('my_user','password','localhost',3307)
### Alternate Connection: Using a socket
sql = mysql_session('my_user','password', nil, nil, '/var/lib/mysql-default/mysqld.sock')