A workflow for hardening a MongoDB container against a STIG using Packer and Ansible, including a scanning step and a threshold validation step to verify compliance.
- Docker - Container engine.
- Packer - A container image builder tool.
- Ansible - An orchestration tool that can target containers. Used as the provisioner to STIG-harden the container under Packer's direction.
- Progress Chef's InSpec testing framework.
- SAF CLI for validating the results of the InSpec scan against a defined threshold (e.g. "95% of tests pass", or "no high-severity failures")
To ensure the profile runs correctly in your specific environment, you need to configure the inputs in the inputs.yml. This will OVERRIDE THE VALUE SET in inspec.yml located here. A template file named inputs_template.yml is provided to help you get started. More information about InSpec inputs can be found in the InSpec Profile Documentation.
Ensure that the Ansible variables are correctly set under spec/ansible/roles/mongo-stig/defaults/main.yml
# The username for the MongoDB administrative account.
mongo_dba: "root"
# The password for the MongoDB administrative account.
mongo_dba_password: "root"
# The hostname or IP address of the MongoDB server.
mongo_host: "localhost"
# The port number on which the MongoDB server is listening.
mongo_port: "27017"
# The database to authenticate against.
mongo_auth_source: "admin"
# The path to the Certificate Authority (CA) bundle file for SSL/TLS connections.
ca_file: "/etc/ssl/CA_bundle.pem"
# The path to the MongoDB SSL/TLS certificate key file.
certificate_key_file: "/etc/ssl/mongodb.pem"
# Adding a user to 'mongo_superusers'
mongo_superusers:
- "admin.root"
- "test.myTester"-
Clone the Repository
Start by cloning the
mongo-hardeningrepository from GitHub to your local machine:git clone https://github.com/mitre/mongodb-enterprise-advanced-4-stig-baseline-hardening.git cd mongo-hardening -
Create MongoDB Certificates
Follow the
README.mdunder thecertificatesdirectory to download the DoD Certificates PKI Bundle and to create any other necessary certificates. -
Update the Ansible Role's Default Values
Visit the defaults/main.yml in the
mongo-stigrole repository to see the available variables. Review these variables and decide which ones, if any, you want to update for your environment.If you decide to update any variables, modify the
varssection of the playbook located atspec/ansible/mongo-stig-hardening-playbook.yml -
Create and Update
inputs.ymlfor InspecExecute the following command to create the
inputs.ymlfile underspec/mongo-inspec-profileby copyinginputs_template.ymland renaming it toinputs.yml. Update this file with your values. Refer to other values here.cp spec/mongo-inspec-profile/inputs_template.yml spec/mongo-inspec-profile/inputs.yml
-
Check Configuration Flags
If you want to disable the
fips_modeorenterprise_editionflags, ensure they are disabled inspec/ansible/mongo-stig-hardening-playbook.yml. -
Initialize Packer
Initialize Packer to install the required Ansible and Docker plugins:
packer init . -
Update
variables.pkrvar.hclfor Packer VariablesUpdate the
variables.pkrvar.hclwith your variables formongo-validate.pkr.hclExecute the following command to create the
variables.pkrvar.hclfile by copyingvariables_template.pkrvar.hcland renaming it tovariables.pkrvar.hcl. Update this file with your values.cp variables_template.pkrvar.hcl variables.pkrvar.hcl
7.1 Optional: Update the
attestation_template.jsonNow if Using a STIG ViewerIf you have a STIG Viewer available, you can update the
attestation_template.jsonnow to avoid rerunning the validation Packer file. This allows you to look up the control IDs beforehand and check for compliance in advance.Follow the instructions here to proceed, and then return to this step once done.
-
Build the Hardened Image
Execute the following command to build and save the hardened Mongo image:
packer build mongo-hardening.pkr.hcl
-
Validate the Hardened Image
Execute the following command to test the hardened Mongo image:
packer build -var-file="variables.pkrvar.hcl" mongo-validate.pkr.hcl -
Run the Hardened Image
Execute the following command to run the hardened Mongo image:
docker run -d \ --name mongo-hardened \ -p 27017:27017 \ -v mongodb_configdb:/data/configdb \ -v mongodb_db:/data/db \ -e PATH="/usr/local/src/openssl-3.1.0/apps:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \ -e LD_LIBRARY_PATH="/usr/local/src/openssl-3.1.0:" \ mongo-hardened \ mongod --config /etc/mongod.conf
-
Cleanup Test Users and Roles
Once the hardened Mongo image is up and running, ensure you review and clean up any test users, roles, and databases that may have been created during the validation process.
-
Databases:
products
-
Users:
test.myTesterproducts.myRoleTestUser
-
Roles:
products.myTestRole
For a full check of what could have been created, visit the inspec repository and review the users and roles listed there.
-
After running the hardening and validation packer files, a report will be generated in reports/mongo_inspec_results.json.
-
Upload the Results:
Upload the
mongo_inspec_results.jsonfile to Heimdall. There should be 19 Not Reviewed controls. These controls need to be attested to. -
Edit the Attestation Template:
Execute the following command to create the
attestation.jsonfile underreportsby copyingattestation_template.jsonand renaming it toattestation.json.cp spec/mongo-inspec-profile/inputs_template.yml spec/mongo-inspec-profile/inputs.yml
Alternatively, you can also have the SAF CLI guide you through the creation of the attestation file. For instructions on how to use the CLI to create an attestation, please refer to the SAF CLI documentation.
-
Review and Provide Explanations:
Manually review each control and provide an explanation on whether it
passedorfailed. -
Update
variables.pkrvar.hcl:Update
variables.pkrvar.hclwith your new attestation file values. -
Re-run the Packer Validation to Apply Your Attestations
Execute the following command to test the hardened Mongo image, it should now produce a docker image tagged with
passed:packer build -var-file="variables.pkrvar.hcl" mongo-validate.pkr.hcl -
Re-upload to Heimdall:
Upload the new
mongo_inspec_results.jsonfile back into Heimdall see your compliance level.
For the full README, refer to the certificates folder.
- If you encounter connection errors, ensure you have the latest OpenSSL version (last tested with OpenSSL 3.3.0).
Full repository here.
-
Running InSpec Checks
- Remove the
--controlsflag to run all inspec checks at once.
inspec exec spec/mongo-inspec-profile/ -t docker://mongo-hardened --controls=SV-252134 --input-file=spec/mongo-inspec-profile/inputs.yml --reporter cli json:reports/mongo_inspec_results.json --no-create-lockfile --enhanced-outcomes - Remove the
-
Deeper Testing with InSpec Shell
inspec shell -t docker://mongo-hardened --depends=spec/mongo-inspec-profile/ --input-file=spec/mongo-inspec-profile/inputs.yml
-
You can add additional types of scanning beyond InSpec (or get InSpec to run more than one testing profile) by modifying the
scripts/scan.shfile. See the MITRE SAF(c) Validation Library for more InSpec profiles, or use your favorite image scanning tool.
Full repository here.
- Sean Chacon Cai - seanlongcc
- Will Dower - wdower