2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
title: About the postgres_session Resource
|
2018-02-16 00:28:15 +00:00
|
|
|
platform: os
|
2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# postgres_session
|
|
|
|
|
|
|
|
Use the `postgres_session` InSpec audit resource to test SQL commands run against a PostgreSQL database.
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Syntax
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
A `postgres_session` resource block declares the username and password to use for the session, and then the command to be run:
|
|
|
|
|
2017-04-25 23:33:10 +00:00
|
|
|
# Create a PostgreSQL session:
|
|
|
|
sql = postgres_session('username', 'password', 'host')
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-04-25 23:33:10 +00:00
|
|
|
# default values:
|
|
|
|
# username: 'postgres'
|
|
|
|
# host: 'localhost'
|
|
|
|
|
|
|
|
# Run an SQL query with an optional database to execute
|
|
|
|
sql.query('sql_query', ['database_name'])`
|
|
|
|
|
|
|
|
A full example is:
|
|
|
|
|
|
|
|
sql = postgres_session('username', 'password', 'host')
|
2016-09-22 12:43:57 +00:00
|
|
|
describe sql.query('SELECT * FROM pg_shadow WHERE passwd IS NULL;') do
|
2017-04-25 23:33:10 +00:00
|
|
|
its('output') { should eq '' }
|
2016-09-22 12:43:57 +00:00
|
|
|
end
|
|
|
|
|
2017-04-25 23:33:10 +00:00
|
|
|
where `its('output') { should eq '' }` compares the results of the query against the expected result in the test
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Examples
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
The following examples show how to use this InSpec audit resource.
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### Test the PostgreSQL shadow password
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-04-25 23:33:10 +00:00
|
|
|
sql = postgres_session('my_user', 'password', '192.168.1.2')
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-04-25 23:33:10 +00:00
|
|
|
describe sql.query('SELECT * FROM pg_shadow WHERE passwd IS NULL;', ['testdb']) do
|
2016-09-22 12:43:57 +00:00
|
|
|
its('output') { should eq('') }
|
|
|
|
end
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### Test for risky database entries
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
describe postgres_session('my_user', 'password').query('SELECT count (*)
|
|
|
|
FROM pg_language
|
|
|
|
WHERE lanpltrusted = \'f\'
|
|
|
|
AND lanname!=\'internal\'
|
2017-04-25 23:33:10 +00:00
|
|
|
AND lanname!=\'c\';', ['postgres']) do
|
2016-09-22 12:43:57 +00:00
|
|
|
its('output') { should eq '0' }
|
|
|
|
end
|
2017-10-03 21:35:10 +00:00
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
## Matchers
|
|
|
|
|
2018-02-16 03:07:18 +00:00
|
|
|
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
|
2017-10-03 21:35:10 +00:00
|
|
|
|
|
|
|
### output
|
|
|
|
|
|
|
|
The `output` matcher tests the results of the query:
|
|
|
|
|
|
|
|
its('output') { should eq(/^0/) }
|