2017-05-01 18:02:15 +00:00
---
2017-05-05 17:11:07 +00:00
title: About the oracledb_session Resource
2018-02-16 00:28:15 +00:00
platform: os
2017-05-01 18:02:15 +00:00
---
2017-05-05 17:11:07 +00:00
# oracledb_session
2017-05-01 18:02:15 +00:00
2019-04-26 18:24:29 +00:00
Use the `oracledb_session` Chef InSpec audit resource to test SQL commands run against a Oracle database.
2017-05-01 18:02:15 +00:00
2017-10-03 21:35:10 +00:00
<br>
2018-08-09 12:34:49 +00:00
## Availability
### Installation
2019-04-26 18:24:29 +00:00
This resource is distributed along with Chef InSpec itself. You can use it automatically.
2018-08-09 12:34:49 +00:00
### Version
This resource first became available in v1.0.0 of InSpec.
2017-05-01 18:02:15 +00:00
## Syntax
2017-05-05 17:11:07 +00:00
A `oracledb_session` resource block declares the username and password to use for the session with an optional service to connect to, and then the command to be run:
2017-05-01 18:02:15 +00:00
2017-06-29 15:01:32 +00:00
describe oracledb_session(user: 'username', password: 'password', service: 'ORCL.localdomain').query('QUERY').row(0).column('result') do
its('value') { should eq('') }
2017-05-01 18:02:15 +00:00
end
where
2017-06-29 15:01:32 +00:00
* `oracledb_session` declares a username and password with permission to run the query (required), and an optional parameters for host (default: `localhost`), SID (default: `nil`, which uses the default SID, and path to the sqlplus binary (default: `sqlplus`).
2018-07-09 17:57:45 +00:00
* it is possible to run queries as sysdba/sysoper by using `as_db_role option`, see examples
2018-11-27 21:56:49 +00:00
* SQLcl can be used in place of sqlplus. Use the `sqlcl_bin` option to set the sqlcl binary path instead of `sqlplus_bin`.
2017-05-01 18:02:15 +00:00
* `query('QUERY')` contains the query to be run
2017-06-29 15:01:32 +00:00
* `its('value') { should eq('') }` compares the results of the query against the expected result in the test
2017-05-01 18:02:15 +00:00
2017-10-03 21:35:10 +00:00
<br>
2017-05-01 18:02:15 +00:00
2018-07-09 17:57:45 +00:00
## oracledb_session(...).query method Properties
* rows the query result as array of hashes
* row(number) selected row from query result, where number is just a row number in the query result
* column(name) array with values from selected column
2017-05-01 18:02:15 +00:00
## Examples
2019-04-26 18:24:29 +00:00
The following examples show how to use this Chef InSpec audit resource.
2017-05-01 18:02:15 +00:00
### Test for matching databases
2017-05-05 17:11:07 +00:00
sql = oracledb_session(user: 'my_user', pass: 'password')
2017-06-29 15:01:32 +00:00
describe sql.query('SELECT NAME AS VALUE FROM v$database;').row(0).column('value') do
its('value') { should cmp 'ORCL' }
2017-05-05 13:29:38 +00:00
end
### Test for matching databases with custom host, SID and sqlplus binary location
2017-05-05 17:11:07 +00:00
sql = oracledb_session(user: 'my_user', pass: 'password', host: 'oraclehost', sid: 'mysid', sqlplus_bin: '/u01/app/oracle/product/12.1.0/dbhome_1/bin/sqlplus')
2017-06-29 15:01:32 +00:00
describe sql.query('SELECT NAME FROM v$database;').row(0).column('name') do
its('value') { should cmp 'ORCL' }
2017-05-01 18:02:15 +00:00
end
2017-10-03 21:35:10 +00:00
2018-07-09 17:57:45 +00:00
### Test for table contains a specified value in any row for the given column name
sql = oracledb_session(user: 'my_user', pass: 'password', service: 'MYSID')
describe sql.query('SELECT * FROM my_table;').column('my_column') do
it { should include 'my_value' }
end
### Test tablespace exists as sysdba
The check will change user (with su) to specified user and run 'sqlplus / as sysdba' (sysoper, sysasm)
sql = oracledb_session(as_os_user: 'oracle', as_db_role: 'sysdba', service: 'MYSID')
describe sql.query('SELECT tablespace_name AS name FROM dba_tablespaces;').column('name') do
it { should include 'MYTABLESPACE' }
end
NOTE: option `as_os_user` available only on unix-like systems and not supported on Windows. Also this option requires that you are running inspec as `root` or with `--sudo`
### Test number of rows in the query result
sql = oracledb_session(user: 'my_user', pass: 'password')
describe sql.query('SELECT * FROM my_table;').rows do
its('count') { should eq 20 }
end
### Use data out of (remote) DB query to build other tests
sql = oracledb_session(user: 'my_user', pass: 'password', host: 'my.remote.db', service: 'MYSID')
sql.query('SELECT * FROM files;').rows.each do |file_row|
describe file(file_row['path']) do
2019-05-25 10:40:36 +00:00
its('owner') { should eq file_row['owner']}
2018-07-09 17:57:45 +00:00
end
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/).