Use the mongodb_session Chef InSpec audit resource to run MongoDB command against a MongoDB Database.
This resource is distributed along with Chef InSpec itself. You can use it automatically.
A mongodb_session resource block declares the user, password, and database to use for the session and then the command to be run:
describe mongodb_session(user: “username”, password: “password”, database: “test”).query(key: value) do its(“params”) { should match(/expected-result/) } end
where
mongodb_session declares a user, password, and database, connecting locally, with permission to run the query.query contains the query to be run.its("params") { should eq(/expected-result/) } compares the results of the query against the expected result in the testThe mongodb_session InSpec resource accepts user, password, host, port, auth_source, auth_mech, ssl, ssl_cert, ssl_ca_cert, and auth_mech_properties parameters.
In Particular:
hostThe server host IP address. Default value: 127.0.0.1.
portThe server port. Default value: 27017.
auth_mechThe authentication mechanism. The available options are: :scram, :scram256, :mongodb_x509, and :aws. Default value: :scram.
See the MongoDB documentation on Ruby driver authentication for more information.
auth_sourceThe database where the user’s authentication credentials are stored. The default value is the database name that is passed as a parameter to the resource.
sslWhether to use the SSL security protocol or not. Set to true to use SSL transport, default value: false. See the MongoDB documentation on Ruby Driver authentication for more information.
Path to the SSL certificate file.
ssl_ca_certPath to the SSL Certificate Authority (CA) certificate file.
ssl_keyPath to SSL key file.
auth_mech_propertiesA hash of the authentication mechanism properties. This option is generally used with the AWS authentication mechanism. See the MongoDB documentation on Ruby Driver authentication using AWS for more information.
This resource uses the MongoDB Ruby Driver to fetch the data.
The following examples show how to use this Chef InSpec audit resource.
rolesInfo command in MongoDBdescribe mongodb_session(user: "foo", password: "bar", database: "test").query(rolesInfo: "dbAdmin").params["roles"].first do
its(["role"]) { should eq "dbAdmin" }
end
describe mongodb_session(user: "foo", password: "bar", database: "test").query(usersInfo: "foo").params["users"].first["roles"].first do
its(["role"]) { should eq "readWrite" }
end
describe mongodb_session(user: "foo", password: "bar", database: "test").query(rolesInfo: "dbAdmin") do
its("params") { should_not be_empty }
its("params") { should include "roles" }
end
For a full list of available matchers, please visit our matchers page.
The params contains all the query data.
© 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/mongodb_session/