├── .delivery └── project.toml ├── .editorconfig ├── .gitattributes ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── branchcleanup.yml ├── .gitignore ├── .rubocop.yml ├── .travis.yml ├── .vscode └── extensions.json ├── Berksfile ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── TESTING.md ├── TROUBLESHOOTING.md ├── attributes └── default.rb ├── chefignore ├── docs └── supported_configuration.md ├── examples ├── automate_win │ ├── .kitchen.yml │ ├── Berksfile │ ├── README.md │ ├── metadata.rb │ ├── recipes │ │ ├── chef_client_config.rb │ │ └── default.rb │ ├── spec │ │ ├── spec_helper.rb │ │ └── unit │ │ │ └── recipes │ │ │ ├── chef_client_config_spec.rb │ │ │ └── default_spec.rb │ └── templates │ │ └── default │ │ └── automate_ingest.rb.erb ├── chef-server │ ├── README.md │ └── Vagrantfile └── wrapper_audit │ ├── .kitchen.yml │ ├── Berksfile │ ├── README.md │ ├── metadata.rb │ ├── recipes │ └── default.rb │ └── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ └── default_spec.rb ├── files └── default │ ├── handler │ └── audit_report.rb │ └── vendor │ ├── chef-automate │ └── fetcher.rb │ └── chef-server │ └── fetcher.rb ├── kitchen.dokken.yml ├── kitchen.yml ├── libraries ├── helper.rb └── reporters │ ├── audit-enforcer.rb │ ├── automate.rb │ ├── cs_automate.rb │ └── json_file.rb ├── metadata.rb ├── recipes ├── default.rb └── inspec.rb ├── resources └── inspec_gem.rb ├── spec ├── chef-client.pem ├── data │ ├── mock.rb │ └── mock_profile.rb ├── spec_helper.rb └── unit │ ├── libraries │ ├── audit_enforcer_spec.rb │ ├── automate_spec.rb │ ├── cs_automate_spec.rb │ ├── helpers_spec.rb │ └── json_file_spec.rb │ ├── recipes │ └── default_spec.rb │ └── report │ ├── audit_report_spec.rb │ └── fetcher_spec.rb ├── tasks └── changelog.rake └── test ├── cookbooks └── test_helper │ ├── .gitignore │ ├── Berksfile │ ├── files │ └── waivers-fixture │ │ ├── controls │ │ └── waiver-check.rb │ │ ├── inspec.yml │ │ └── waivers │ │ └── waivers.yaml │ ├── metadata.rb │ └── recipes │ ├── create_file.rb │ ├── force_inspec_core.rb │ ├── install_inspec.rb │ ├── setup.rb │ └── setup_waiver_fixture.rb ├── integration ├── chef-node-disabled │ └── default.rb ├── chef-node-enabled │ └── default.rb ├── chef15-compatible-inspec │ └── default.rb ├── default │ └── default.rb ├── gem-install-core-version2 │ └── default.rb ├── gem-install-core-version3 │ └── default.rb ├── gem-install-core-version4 │ └── default.rb ├── inspec-attributes │ └── default.rb ├── skip-inspec-gem-install │ └── default.rb └── waivers │ └── default.rb └── kitchen-automate ├── .kitchen.yml ├── Berksfile ├── README.md ├── libraries └── helper.rb ├── metadata.rb ├── recipes ├── bootstrap_localhost.rb ├── configure_services.rb ├── converge_localhost.rb ├── default.rb ├── harakiri.rb └── upload_profile.rb ├── templates └── default │ └── knife.rb.erb └── test └── integration └── default └── chef-server-visibility_spec.rb /.delivery/project.toml: -------------------------------------------------------------------------------- 1 | remote_file = "https://raw.githubusercontent.com/chef-cookbooks/community_cookbook_tools/master/delivery/project.toml" 2 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root=true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | 11 | # 2 space indentation 12 | indent_style = space 13 | indent_size = 2 14 | 15 | # Avoid issues parsing cookbook files later 16 | charset = utf-8 17 | 18 | # Avoid cookstyle warnings 19 | trim_trailing_whitespace = true 20 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Order is important. The last matching pattern has the most precedence. 2 | 3 | * @chef-cookbooks/audit-cookbook-team 4 | README.md @chef/docs-team 5 | .github/ISSUE_TEMPLATE/** @chef/docs-team 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Cookbook version 2 | [Version of the cookbook where you are encountering the issue] 3 | 4 | ### Chef-client version 5 | [Version of chef-client in your environment] 6 | 7 | ### Platform Details 8 | [Operating system distribution and release version. Cloud provider if running in the cloud] 9 | 10 | ### Scenario: 11 | [What you are trying to achieve and you can't?] 12 | 13 | ### Steps to Reproduce: 14 | [If you are filing an issue what are the things we need to do in order to repro your problem? How are you using this cookbook or any resources it includes?] 15 | 16 | ### Expected Result: 17 | [What are you expecting to happen as the consequence of above reproduction steps?] 18 | 19 | ### Actual Result: 20 | [What actually happens after the reproduction steps? Include the error output or a link to a gist if possible.] 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | [Describe what this change achieves] 4 | 5 | ### Issues Resolved 6 | 7 | [List any existing issues this PR resolves] 8 | 9 | ### Check List 10 | 11 | - [ ] All tests pass. See 12 | - [ ] New functionality includes testing. 13 | - [ ] New functionality has been documented in the README if applicable 14 | - [ ] All commits have been signed for the Developer Certificate of Origin. See 15 | -------------------------------------------------------------------------------- /.github/workflows/branchcleanup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Branch Cleanup 3 | # This workflow is triggered on all closed pull requests. 4 | # However the script does not do anything if a merge was not performed. 5 | "on": 6 | pull_request: 7 | types: [closed] 8 | 9 | env: 10 | NO_BRANCH_DELETED_EXIT_CODE: 0 11 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: jessfraz/branch-cleanup-action@master 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.rbc 2 | .config 3 | InstalledFiles 4 | lib/bundler/man 5 | pkg 6 | test/tmp 7 | test/version_tmp 8 | tmp 9 | _Store 10 | *~ 11 | *# 12 | .#* 13 | \#*# 14 | *.un~ 15 | *.tmp 16 | *.bk 17 | *.bkup 18 | 19 | # editor temp files 20 | .idea 21 | .*.sw[a-z] 22 | 23 | # ruby/bundler files 24 | .ruby-version 25 | .ruby-gemset 26 | .rvmrc 27 | Gemfile.lock 28 | .bundle 29 | *.gem 30 | coverage 31 | spec/reports 32 | 33 | # YARD / rdoc artifacts 34 | .yardoc 35 | _yardoc 36 | doc/ 37 | rdoc 38 | 39 | # chef infra stuff 40 | Berksfile.lock 41 | .kitchen 42 | kitchen.local.yml 43 | vendor/ 44 | .coverage/ 45 | .zero-knife.rb 46 | Policyfile.lock.json 47 | 48 | # vagrant stuff 49 | .vagrant/ 50 | .vagrant.d/ 51 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | Lint/UselessAssignment: 2 | Exclude: 3 | - 'spec/unit/report/audit_report_spec.rb' 4 | - 'test/integration/default/default.rb' 5 | - 'test/kitchen-automate/recipes/upload_profile.rb' 6 | 7 | Naming/AccessorMethodName: 8 | Exclude: 9 | - 'examples/chef-server/Vagrantfile' 10 | - 'spec/unit/report/audit_report_spec.rb' 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | 3 | addons: 4 | apt: 5 | sources: 6 | - chef-current-xenial 7 | packages: 8 | - chef-workstation 9 | 10 | # Don't `bundle install` which takes about 1.5 mins 11 | install: echo "skip bundle install" 12 | 13 | branches: 14 | only: 15 | - master 16 | 17 | services: 18 | - docker 19 | 20 | env: 21 | global: 22 | - CHEF_VERSION=15.8.23 23 | - KITCHEN_YAML=kitchen.dokken.yml 24 | - CHEF_LICENSE="accept-no-persist" 25 | 26 | before_install: 27 | - sudo iptables -L DOCKER || ( echo "DOCKER iptables chain missing" ; sudo iptables -N DOCKER ) 28 | - CHEF_LICENSE="accept-no-persist" sudo chef gem install webmock 29 | - eval "$(chef shell-init bash)" 30 | - chef --version 31 | - kitchen list 32 | 33 | matrix: 34 | include: 35 | # Run the style, unit and chefspec tests 36 | - rvm: 2.6.5 37 | script: rake 38 | # Run integration tests with test-kitchen 39 | - rvm: 2.6.5 40 | script: rake $SUITE 41 | env: SUITE=test:integration OS='default-centos-7' 42 | - rvm: 2.6.5 43 | script: rake $SUITE 44 | env: SUITE=test:integration OS='default-ubuntu-1804' 45 | - rvm: 2.6.5 46 | script: rake $SUITE 47 | env: SUITE=test:integration OS='missing-profile-no-fail-ubuntu-1804' 48 | - rvm: 2.6.5 49 | script: rake $SUITE && exit 1 || echo "OK" 50 | env: SUITE=test:integration OS='missing-profile-fail-ubuntu-1804' 51 | 52 | # Next 5 suites are disabled due to an issue with the gem() inspec resource 53 | # see https://github.com/chef-cookbooks/audit/issues/406 54 | # 55 | # - rvm: 2.6.5 56 | # script: rake $SUITE 57 | # env: SUITE=test:integration OS='chef15-compatible-inspec-ubuntu-1804' 58 | # Next 4 suites test running inspec 3 or 4 on audit cookbook. This is only permitted on chef 14 or older. 59 | # - rvm: 2.6.5 60 | # script: rake $SUITE 61 | # env: SUITE=test:integration OS='gem-install-core-version4-centos-7' CHEF_VERSION=14 62 | # - rvm: 2.6.5 63 | # script: rake $SUITE 64 | # env: SUITE=test:integration OS='gem-install-core-version3-centos-7' CHEF_VERSION=14 65 | # - rvm: 2.6.5 66 | # script: rake $SUITE 67 | # env: SUITE=test:integration OS='gem-install-core-version4-ubuntu-1804' CHEF_VERSION=14 68 | # - rvm: 2.6.5 69 | # script: rake $SUITE 70 | # env: SUITE=test:integration OS='gem-install-core-version3-ubuntu-1804' CHEF_VERSION=14 71 | 72 | - rvm: 2.6.5 73 | script: rake $SUITE 74 | env: SUITE=test:integration OS='inspec-attributes-ubuntu-1804' 75 | - rvm: 2.6.5 76 | script: rake $SUITE 77 | env: SUITE=test:integration OS='chef-node-enabled-ubuntu-1804' 78 | - rvm: 2.6.5 79 | script: rake $SUITE 80 | env: SUITE=test:integration OS='chef-node-disabled-ubuntu-1804' 81 | - rvm: 2.6.5 82 | script: rake $SUITE 83 | env: SUITE=test:integration OS='hash-centos-7' 84 | - rvm: 2.6.5 85 | script: rake $SUITE 86 | env: SUITE=test:integration OS='hash-ubuntu-1804' 87 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "chef-software.chef", 4 | "rebornix.ruby", 5 | "editorconfig.editorconfig" 6 | ] 7 | } -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | 5 | group :integration do 6 | cookbook 'test_helper', path: 'test/cookbooks/test_helper' 7 | end 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | Please refer to the Chef Community Code of Conduct at 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please refer to 2 | https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | group :release do 4 | gem 'community_cookbook_releaser' 5 | gem 'stove' 6 | end 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # audit cookbook 2 | [![Cookbook Version](http://img.shields.io/cookbook/v/audit.svg)][cookbook] [![Build Status](http://img.shields.io/travis/chef-cookbooks/audit.svg)][travis] 3 | 4 | The `audit` cookbook allows you to run InSpec profiles as part of a Chef Client run. It downloads configured profiles from various sources like Chef Automate, Chef Supermarket or Git and reports audit runs to Chef Automate. 5 | 6 | 7 | ## DEPRECATION 8 | 9 | The audit cookbook has been replaced by the Chef Infra Compliance phase which shipped in Chef Infra Client 17. This new phase of execution within the Chef Infra Client includes the functionality of the audit cookbook (plus a lot more) without the need for an external cookbook. This cookbook will continue to work for existing users, but will no longer receive updates. Consider migrating the Compliance Phase for enhanced functionality: 10 | 11 | https://docs.chef.io/chef_compliance_phase/ 12 | 13 | ## Quickstart 14 | 15 | The `audit` cookbook supports a number of different reporters and fetchers which can be confusing. Please see the [supported configurations documentation](https://github.com/chef-cookbooks/audit/blob/master/docs/supported_configuration.md) which has a few copy/paste examples to get you started quickly. 16 | 17 | ## Requirements 18 | 19 | ### Chef 20 | 21 | - Chef Client >=12.20 22 | 23 | ### Support Matrix 24 | 25 | #### Chef Automate 26 | 27 | | Automate version | InSpec version | Audit Cookbook version | 28 | |--------------------|------------------|--------------------------| 29 | | < 0.8.0 | ≤ 1.23.0 | ≤ 3.1.0 | 30 | | ≥ 0.8.0 | ≥ 1.24.0 | ≥ 4.0.0 | 31 | | ≥ 2 | ≥ 2.2.102 | ≥ 7.1.0 | 32 | 33 | #### Chef Infra Client 34 | 35 | | Chef Client | Audit Cookbook version | 36 | |----------------------------|---------------------------| 37 | | >= 15 | >= 8.0.0 | 38 | 39 | Note: 40 | When used with Chef Infra Client 15 and above, the Audit cookbook _must_ be >= 7.7.0. Otherwise, you will see the following failure. Of course, we recommend using the latest available Chef Infra Client and audit cookbook after testing in your non-production environments. Remove the `inspec_version` setting. See more detail in the [Inspec Gem Installation](#inspec-gem-installation) section 41 | 42 | ### Legacy Chef Infra Client Releases (pre-15.x) and Inspec 43 | 44 | Newer Chef Infra Clients, in the 15.x and 16.x ranges ship with an 45 | inspec-core gem that the audit cookbook in the proper version range 46 | listed in the matrix above knows to use. It will not attempt to 47 | download a gem in this case. 48 | 49 | We find that 14.12 has the inspec-core 3.9.0 gem embedded which should 50 | generate reports compatible with A2, but anything before that uses 51 | InSpec gem v2.x which is NOT compatible with A2. To explain further, 52 | Inspec 2.x will send reports to A2, but they will false positive, as 53 | this report format is incompatible with A2 54 | 55 | Chef Infra Client 14.x + plain, non inspec-core InSpec 3.9.3 installed by 56 | the audit cookbook gem installer definitely works. So in Chef Infra 57 | Client 14.x before 14.12, we recommend this usage. 58 | 59 | ## Overview 60 | 61 | ### Component Architecture 62 | ``` 63 | ┌──────────────────────┐ ┌──────────────────────┐ ┌─────────────────────┐ 64 | │ Chef Infra Client │ │ Chef Server Proxy │ │ Chef Automate │ 65 | │ │ │ (optional) │ │ │ 66 | │ ┌──────────────────┐ │ │ │ │ │ 67 | │ │ │◀┼────┼──────────────────────┼────│ Profiles │ 68 | │ │ audit cookbook │ │ │ │ │ │ 69 | │ │ │─┼────┼──────────────────────┼───▶│ Reports │ 70 | │ └──────────────────┘ │ │ │ │ │ 71 | │ │ │ │ │ │ 72 | └──────────────────────┘ └──────────────────────┘ └─────────────────────┘ 73 | ``` 74 | 75 | InSpec Profiles can be hosted from a variety of locations: 76 | 77 | ``` 78 | ┌──────────────────────┐ ┌─────────────────────┐ 79 | │ Chef Infra Client │ ┌───────────────────────┐ │ Chef Automate │ 80 | │ │ ┌──│ Profiles(Supermarket, │ │ │ 81 | │ ┌──────────────────┐ │ │ │ GitHub, local, etc) │ │ │ 82 | │ │ │◀┼──┘ └───────────────────────┘ │ │ 83 | │ │ audit cookbook │◀┼────────────────────────────────│ Profiles │ 84 | │ │ │─┼───────────────────────────────▶│ Reports │ 85 | │ └──────────────────┘ │ │ │ 86 | │ │ │ │ 87 | └──────────────────────┘ └─────────────────────┘ 88 | ``` 89 | 90 | ## Usage 91 | 92 | The audit cookbook needs to be configured for each node where the `chef-client` runs. The `audit` cookbook can be reused for all nodes, all node-specific configuration is done via Chef attributes. 93 | 94 | ### InSpec Gem Installation 95 | 96 | **This section refers to EOL configuration. Starting with Chef Infra Client 15.x, only the embedded InSpec gem can be used and the `inspec_version` attribute will be ignored. Additionally, attempting to continue installing another version of the gem outside the Chef Infra Client inspec-core installation using the `inspec_gem()` resource can result in errors like the following. If you are running a Chef Infra Client release earlier than 15.x, we recommend testing an upgrade to Chef Infra Client 16.x, using the latest available audit cookbook version, and removing any `inspec_version` setting you may have done in your wrapper cookbook** 97 | 98 | ``` 99 | [2020-09-22T10:43:51-04:00] ERROR: Report handler Chef::Handler::AuditReport raised # 100 | [2020-09-22T10:43:51-04:00] ERROR: /opt/chef/embedded/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' 101 | [2020-09-22T10:43:51-04:00] ERROR: /opt/chef/embedded/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' 102 | [2020-09-22T10:43:51-04:00] ERROR: /var/chef/cache/cookbooks/audit/files/default/handler/audit_report.rb:138:in `load_chef_fetcher' 103 | [2020-09-22T10:43:51-04:00] ERROR: /var/chef/cache/cookbooks/audit/files/default/handler/audit_report.rb:62:in `report' 104 | ``` 105 | 106 | Beginning with version 3.x of the `audit` cookbook, the cookbook will first check to see if an InSpec gem is already installed. If it is, it will not attempt to install it. With the release of Chef Infra Client 14.2.x, the Chef omnibus package includes InSpec as a gem `inspec-core`. Recent releases of the audit cookbook use of this bundled component will reduce audit run times and also ensure that Chef users in air-gapped or firewalled environments can still use the `audit` cookbook without requiring gem mirrors, etc. 107 | 108 | To install a different version of the InSpec gem, or to force installation of the gem, set the `node['audit']['inspec_version']` attribute to the version you wish to be installed. 109 | 110 | Beginning with version 3.x of the `audit` cookbook, the default version of the InSpec gem to be installed (if it isn't already installed) is the latest version. Prior versions of the `audit` cookbook were version-locked to `inspec` version 1.15.0. **The use of Chef Infra Client versions 14.x and earlier was EOL as of April 30, 2020** 111 | 112 | Note on AIX Support: 113 | 114 | * InSpec is only supported via the bundled InSpec gem shipped with version >= 14.2.x of the chef-client package. 115 | * Standalone InSpec gem installation or upgrade is not supported. 116 | * The default `nil` value of `node['audit']['inspec_version']` will ensure the above behavior is adhered to. 117 | 118 | ### Configure node 119 | 120 | Once the cookbook is available in Chef Server, you need to add the `audit::default` recipe to the run-list of each node (or, preferably create a wrapper cookbook). The profiles are selected using the `node['audit']['profiles']` attribute. A list of example configurations are documented in [Supported Configurations](docs/supported_configuration.md). Below is another example demonstrating the different locations profiles can be "fetched" from: 121 | 122 | ```ruby 123 | default['audit']['profiles']['linux-baseline'] = { 124 | 'compliance': 'user/linux-baseline', 125 | 'version': '2.1.0' 126 | } 127 | 128 | default['audit']['profiles']['ssh'] = { 129 | 'supermarket': 'hardening/ssh-hardening' 130 | } 131 | 132 | default['audit']['profiles']['brewinc/win2012_audit'] = { 133 | 'path': 'E:/profiles/win2012_audit' 134 | } 135 | 136 | default['audit']['profiles']['ssl'] = { 137 | 'git': 'https://github.com/dev-sec/ssl-benchmark.git' 138 | } 139 | 140 | default['audit']['profiles']['ssh2'] = { 141 | 'url': 'https://github.com/dev-sec/tests-ssh-hardening/archive/master.zip' 142 | } 143 | ``` 144 | 145 | #### Attributes 146 | 147 | You can also pass in [InSpec Attributes](https://www.inspec.io/docs/reference/profiles/) to your audit run. Do this by defining the attributes: 148 | 149 | ```ruby 150 | default['audit']['attributes'] = { 151 | first_attribute: 'some value', 152 | second_attribute: 'another value', 153 | } 154 | ``` 155 | 156 | #### Waivers 157 | 158 | You can use Chef InSpec's [Waiver Feature](https://www.inspec.io/docs/reference/waivers/) to mark individual failing controls as being administratively accepted, either on a temporary or permanent basis. Prepare a waiver YAML file, and use your Chef Infra cookbooks to deliver the file to your converging node (for example, using [cookbook_file](https://docs.chef.io/resource_cookbook_file.html) or [remote_file](https://docs.chef.io/resource_remote_file.html)). Then set the attribute `default['audit']['waiver_file']` to the location of the waiver file on the local node, and Chef InSpec will apply the waivers. 159 | 160 | ### Reporting 161 | 162 | #### Reporting to Chef Automate via Chef Server 163 | 164 | To retrieve compliance profiles and report to Chef Automate through Chef Server, set the `reporter` and `profiles` attributes. 165 | 166 | This requires Chef Client >= 12.16.42, Chef Server version 12.11.1, and Chef Automate 0.6.6 or newer, as well as integration between the Chef Server and Chef Automate. More details [here](https://docs.chef.io/integrate_compliance_chef_automate.html#collector-chef-server-automate). 167 | 168 | To upload profiles, you can use the [Automate API](https://docs.chef.io/api_automate.html) or the `inspec compliance` subcommands (requires InSpec 1.7.2 or newer). 169 | 170 | Attributes example of fetching from Automate, reporting to Automate both via Chef Server: 171 | 172 | ```ruby 173 | default['audit']['reporter'] = 'chef-server-automate' 174 | default['audit']['fetcher'] = 'chef-server' 175 | default['audit']['profiles']['my-profile'] = { 176 | 'compliance': 'john/my-profile' 177 | } 178 | ``` 179 | 180 | #### Direct reporting to Chef Automate 181 | 182 | To report directly to Chef Automate, set the `reporter` attribute to 'chef-automate' and specify where to fetch the `profiles` from. 183 | 184 | * `insecure` - a `true` value will skip the SSL certificate verification. Default value is `false` 185 | 186 | This method sends the report using the `data_collector.server_url` and `data_collector.token` options, defined in `client.rb`. It requires `inspec` version `0.27.1` or greater. Further information is available at Chef Docs: [Configure a Data Collector token in Chef Automate](https://docs.chef.io/ingest_data_chef_automate.html) 187 | 188 | ```ruby 189 | default['audit']['reporter'] = 'chef-automate' 190 | default['audit']['profiles']['tmp_compliance_profile'] = { 191 | 'url': 'https://github.com/nathenharvey/tmp_compliance_profile' 192 | } 193 | ``` 194 | 195 | If you are using a self-signed certificate, please also read [how to add the Chef Automate certificate to the trusted_certs directory](https://docs.chef.io/data_collection_without_server.html#add-chef-automate-certificate-to-trusted-certs-directory) 196 | 197 | Version compatibility matrix: 198 | 199 | | Automate version | InSpec version | Audit Cookbook version | 200 | |--------------------|------------------|--------------------------| 201 | | < 0.8.0 | ≤ 1.23.0 | ≤ 3.1.0 | 202 | | ≥ 0.8.0 | ≥ 1.24.0 | ≥ 4.0.0 | 203 | 204 | 205 | #### Compliance report size limitations 206 | 207 | The size of the report being generated from running the compliance scan is influenced by a few factors like: 208 | * number of controls and tests in a profile 209 | * number of profile failures for the node 210 | * controls metadata (title, description, tags, etc) 211 | * number of resources (users, processes, etc) that are being tested 212 | 213 | Depending on your setup, there are some limits you need to be aware of. A common one is Chef Server default (1MB) request size. Exceeding this limit will reject the report with `ERROR: 413 "Request Entity Too Large"`. For more details about these limits, please refer to [TROUBLESHOOTING.md](TROUBLESHOOTING.md#413-request-entity-too-large). 214 | 215 | #### Write to file on disk 216 | 217 | To write the report to a file on disk, simply set the `reporter` to 'json-file' like so: 218 | 219 | ```ruby 220 | default['audit']['reporter'] = 'json-file' 221 | default['audit']['profiles']['ssh2'] = { 222 | 'path': '/some/base_ssh.tar.gz' 223 | } 224 | ``` 225 | 226 | The resulting file will be written to `node['audit']['json_file']['location']` which defaults to 227 | `/cookbooks/audit/inspec-.json`. The path will also be output to 228 | the Chef log: 229 | 230 | ``` 231 | [2017-08-29T00:22:10+00:00] INFO: Reporting to json-file 232 | [2017-08-29T00:22:10+00:00] INFO: Writing report to /opt/kitchen/cache/cookbooks/audit/inspec-20170829002210.json 233 | [2017-08-29T00:22:10+00:00] INFO: Report handlers complete 234 | ``` 235 | 236 | #### Enforce compliance with executed profiles 237 | 238 | The `audit-enforcer` enables you to enforce compliance with executed profiles. If the system under test is determined to be non-compliant, this reporter will raise an error and fail the Chef Client run. To activate compliance enforcement, set the `reporter` attribute to 'audit-enforcer': 239 | 240 | ```ruby 241 | default['audit']['reporter'] = 'audit-enforcer' 242 | ``` 243 | 244 | Note that detection of non-compliance will immediately terminate the Chef Client run. If you specify [multiple reporters](#multiple-reporters), place the `audit-enforcer` at the end of the list, allowing the other reporters to generate their output prior to run termination. 245 | 246 | #### Multiple Reporters 247 | 248 | To enable multiple reporters, simply define multiple reporters with all the necessary information 249 | for each one. For example, to report to Chef Automate and write to json file on disk: 250 | 251 | ```ruby 252 | default['audit']['reporter'] = ['chef-server-automate', 'json-file'] 253 | default['audit']['profiles']['windows'] = { 254 | 'compliance': 'base/windows' 255 | } 256 | ) 257 | ``` 258 | 259 | ### Profile Fetcher 260 | 261 | #### Fetch profiles from Chef Automate via Chef Server 262 | 263 | To enable reporting to Chef Automate with profiles from Chef Automate, you need to have Chef Server integrated with [Chef Automate](https://docs.chef.io/integrate_compliance_chef_automate.html#collector-chef-server-automate). You can then set the `fetcher` attribute to 'chef-server'. 264 | 265 | This allows the audit cookbook to fetch profiles stored in Chef Automate. For example: 266 | 267 | ```ruby 268 | default['audit']['reporter'] = 'chef-server-automate' 269 | default['audit']['fetcher'] = 'chef-server' 270 | default['audit']['profiles']['ssh'] = { 271 | 'compliance': 'base/ssh' 272 | } 273 | ``` 274 | 275 | #### Fetch profiles directly from Chef Automate 276 | 277 | This method fetches profiles using the `data_collector.server_url` and `data_collector.token` options, in `client.rb`. It requires `inspec` version `0.27.1` or greater. Further information is available at Chef Docs: [Configure a Data Collector token in Chef Automate](https://docs.chef.io/ingest_data_chef_automate.html) 278 | 279 | ```ruby 280 | default['audit']['reporter'] = 'chef-automate' 281 | default['audit']['fetcher'] = 'chef-automate' 282 | default['audit']['profiles']['ssh'] = { 283 | 'name': 'ssh', 284 | } 285 | ``` 286 | 287 | ## Interval Settings 288 | 289 | If you have long running audit profiles that you don't wish to execute on every chef-client run, 290 | you can enable an interval: 291 | 292 | ``` 293 | default['audit']['interval']['enabled'] = true 294 | default['audit']['interval']['time'] = 1440 # once a day, the default value 295 | ``` 296 | 297 | The time attribute is in minutes. 298 | 299 | You can enable the interval and set the interval time, along with your desired profiles, 300 | in an environment or role like this: 301 | 302 | ```json 303 | "audit": { 304 | "profiles": [ 305 | { 306 | "name": "ssh", 307 | "compliance": "base/ssh" 308 | }, 309 | { 310 | "name": "linux", 311 | "compliance": "base/linux" 312 | } 313 | ], 314 | "interval": { 315 | "enabled": true, 316 | "time": 1440 317 | } 318 | } 319 | ``` 320 | 321 | ## Alternate Source Location for `inspec` Gem 322 | 323 | If you are not able or do not wish to pull the `inspec` gem from rubygems.org, 324 | you may specify an alternate source using: 325 | 326 | ``` 327 | # URI to alternate gem source (e.g. http://gems.server.com or filesytem location) 328 | # root of location must host the *specs.4.8.gz source index 329 | default['audit']['inspec_gem_source'] = 'http://internal.gem.server.com/gems' 330 | ``` 331 | 332 | Please note that all dependencies to the `inspec` gem must also be hosted in this location. 333 | 334 | ## Using Chef node data 335 | 336 | While it is recommended that InSpec profiles should be self-contained and not rely on external data unless 337 | necessary, there are valid use cases where a profile's test may exhibit different behavior depending on 338 | aspects of the node under test. 339 | 340 | There are two primary ways to pass Chef data to the InSpec run via the audit cookbook. 341 | 342 | ### Option 1: Explicitly pass necessary data (recommended) 343 | 344 | Any data added to the `node['audit']['attributes']` hash will be passed as individual InSpec attributes. 345 | This provides a clean interface between the Chef run and InSpec profile, allowing for easy assignment 346 | of sane default values in the InSpec profile. This method is especially recommended if the InSpec profile 347 | is expected to be used outside of the context of the audit cookbook so it's extra clear to profile 348 | consumers what attributes are necessary. 349 | 350 | In a wrapper cookbook or similar, set your Chef attributes: 351 | 352 | ```ruby 353 | node.normal['audit']['attributes']['key1'] = 'value1' 354 | node.normal['audit']['attributes']['debug_enabled'] = node['my_cookbook']['debug_enabled'] 355 | node.normal['audit']['attributes']['environment'] = node.chef_environment 356 | ``` 357 | 358 | ... and then use them in your InSpec profile: 359 | 360 | ```ruby 361 | environment = attribute('environment', description: 'The chef environment for the node', default: 'dev') 362 | 363 | control 'debug-disabled-in-production' do 364 | title 'Debug logs disabled in production' 365 | desc 'Debug logs contain potentially sensitive information and should not be on in prod.' 366 | impact 1.0 367 | 368 | describe file('/path/to/my/app/config') do 369 | its('content') { should_not include "debug=true" } 370 | end 371 | 372 | only_if { environment == 'production' } 373 | end 374 | ``` 375 | 376 | ### Option 2: Use the chef node object 377 | 378 | In the event where it is not practical to opt-in to pass certain attributes and data, the audit cookbook will 379 | pass the Chef node object as an InSpec attribute named `chef_node`. 380 | 381 | While this provides the ability to write more flexible profiles, it makes it more difficult to reuse profiles 382 | outside of an audit cookbook run, requiring the profile user to know how to pass in a single attribute containing 383 | Chef-like data. Therefore, it is recommended to use Option 1 whenever possible. 384 | 385 | To use this option, first enable it in a wrapper cookbook or similar: 386 | 387 | ```ruby 388 | node.override['audit']['chef_node_attribute_enabled'] = true 389 | ``` 390 | 391 | ... and then use it in your profile: 392 | 393 | ```ruby 394 | chef_node = attribute('chef_node', description: 'Chef Node') 395 | 396 | control 'no-password-auth-in-prod' do 397 | title 'No Password Authentication in Production' 398 | desc 'Password authentication is allowed in all environments except production' 399 | impact 1.0 400 | 401 | describe sshd_config do 402 | its('PasswordAuthentication') { should cmp 'No' } 403 | end 404 | 405 | only_if { chef_node['chef_environment'] == 'production' } 406 | end 407 | ``` 408 | 409 | ## Using the InSpec Backend Cache 410 | 411 | **Introduced in Audit Cookbook v6.0.0 and InSpec v1.47.0** 412 | 413 | InSpec v1.47.0 provides the ability to cache the result of commands executed on the node being tested. This drastically improves InSpec performance when slower-running commands are run multiple times during execution. 414 | 415 | This feature is **enabled by default** in the audit cookbook. If your profile runs a command multiple times and expects output to be different each time, you may have to disable this feature. To do so, set the `inspec_backend_cache` attribute to `false`: 416 | 417 | ```ruby 418 | node.normal['audit']['inspec_backend_cache'] = false 419 | ``` 420 | 421 | ## Troubleshooting 422 | 423 | Please refer to [TROUBLESHOOTING.md](TROUBLESHOOTING.md). 424 | 425 | Please let us know if you have any [issues](https://github.com/chef-cookbooks/audit/issues), we are happy to help. 426 | 427 | ## Run the tests for this cookbook: 428 | 429 | Install [Chef Development Kit](https://downloads.chef.io/chefdk) on your machine. 430 | 431 | ```bash 432 | # Install webmock gem needed by rspec 433 | chef gem install webmock 434 | 435 | # Run style checks 436 | rake style 437 | 438 | # Run all unit and ChefSpec tests 439 | rspec 440 | 441 | # Run a specific test 442 | rspec ./spec/unit/libraries/automate_spec.rb 443 | ``` 444 | 445 | ## How to release the `audit` cookbook 446 | 447 | * Cookbook source located here: (https://github.com/chef-cookbooks/audit) 448 | * Hosted Chef users("collaborators") that can publish it to supermarket.chef.io: `apop`, `arlimus`, `chris-rock`, `sr`. Add more collaborators from `Supermarket>Manage Cookbook>Add Collaborator` 449 | 450 | Releasing a new cookbook version: 451 | 452 | 1. Install changelog gem: `chef gem install github_changelog_generator` 453 | 2. version bump the metadata.rb and updated changelog (`rake changelog`) 454 | 3. Get your changes merged into master 455 | 4. Go to the `audit` cookbook directory and pull from master 456 | 5. Run `bundle install` 457 | 6. Use stove to publish the cookbook(including git version tag). You must point to the private key of your hosted chef user. For example: 458 | 459 | ``` 460 | stove --username apop --key ~/git/chef-repo/.chef/apop.pem 461 | ``` 462 | 463 | ## License 464 | 465 | | | | 466 | |:---------------------|:-----------------------------------------| 467 | | **Author:** | Stephan Renatus () 468 | | **Author:** | Christoph Hartmann () 469 | | **Copyright:** | Copyright (c) 2015 Chef Software Inc. 470 | | **License:** | Apache License, Version 2.0 471 | 472 | Licensed under the Apache License, Version 2.0 (the "License"); 473 | you may not use this file except in compliance with the License. 474 | You may obtain a copy of the License at 475 | 476 | http://www.apache.org/licenses/LICENSE-2.0 477 | 478 | Unless required by applicable law or agreed to in writing, software 479 | distributed under the License is distributed on an "AS IS" BASIS, 480 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 481 | See the License for the specific language governing permissions and 482 | limitations under the License. 483 | 484 | [cookbook]: https://supermarket.chef.io/cookbooks/audit 485 | [travis]: http://travis-ci.org/chef-cookbooks/audit 486 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | 3 | # Import other external rake tasks 4 | Dir.glob('tasks/*.rake').each { |r| import r } 5 | 6 | # Style tests. cookstyle (rubocop) and Foodcritic 7 | namespace :style do 8 | begin 9 | require 'cookstyle' 10 | require 'rubocop/rake_task' 11 | 12 | desc 'Run Ruby style checks' 13 | RuboCop::RakeTask.new(:ruby) 14 | rescue LoadError => e 15 | puts ">>> Gem load error: #{e}, omitting style:ruby" unless ENV['CI'] 16 | end 17 | 18 | begin 19 | require 'foodcritic' 20 | 21 | desc 'Run Chef style checks' 22 | FoodCritic::Rake::LintTask.new(:chef) do |t| 23 | t.options = { 24 | fail_tags: ['any'], 25 | progress: true, 26 | } 27 | end 28 | rescue LoadError 29 | puts ">>> Gem load error: #{e}, omitting style:chef" unless ENV['CI'] 30 | end 31 | end 32 | 33 | desc 'Run all style checks' 34 | task style: ['style:chef', 'style:ruby'] 35 | task lint: ['style'] 36 | 37 | # ChefSpec 38 | begin 39 | require 'rspec/core/rake_task' 40 | 41 | desc 'Run ChefSpec examples' 42 | RSpec::Core::RakeTask.new(:spec) 43 | rescue LoadError => e 44 | puts ">>> Gem load error: #{e}, omitting spec" unless ENV['CI'] 45 | end 46 | 47 | namespace :test do 48 | task :integration do 49 | concurrency = ENV['CONCURRENCY'] || 1 50 | os = ENV['OS'] || '' 51 | sh('sh', '-c', "kitchen test -c #{concurrency} #{os}") 52 | end 53 | 54 | # call it like this: rake test:kitchen_automate[verify] 55 | task :kitchen_automate, :action do |_t, args| 56 | if %w(list create converge verify destroy test).include?(args[:action]) 57 | sh('sh', '-c', "cd test/kitchen-automate; kitchen #{args[:action]}") 58 | else 59 | puts ">>> Unknown kitchen action: #{args[:action]}" 60 | end 61 | end 62 | end 63 | 64 | namespace :supermarket do 65 | begin 66 | require 'stove/rake_task' 67 | 68 | desc 'Publish cookbook to Supermarket with Stove' 69 | Stove::RakeTask.new 70 | rescue LoadError => e 71 | puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV['CI'] 72 | end 73 | end 74 | 75 | # Default 76 | task default: %w(style spec) 77 | -------------------------------------------------------------------------------- /TESTING.md: -------------------------------------------------------------------------------- 1 | Please refer to 2 | 3 | -------------------------------------------------------------------------------- /TROUBLESHOOTING.md: -------------------------------------------------------------------------------- 1 | # Troubleshooting 2 | 3 | ## HTTP Errors 4 | 5 | ### 401, 403 Unauthorized - bad clock 6 | 7 | Occasionally, the system date/time will drift between client and server. If this drift is greater than a couple of minutes, the Chef Server will throw a 401 unauthorized and the request will not be forwarded to the Compliance server. 8 | 9 | On the Chef Server you can see this in the following logs: 10 | ``` 11 | # chef-server-ctl tail 12 | 13 | ==> /var/log/opscode/nginx/access.log <== 14 | 192.168.200.102 - - [28/Aug/2016:14:57:36 +0000] "GET /organizations/brewinc/nodes/vagrant-c0971990 HTTP/1.1" 401 "0.004" 93 "-" "Chef Client/12.14.38 (ruby-2.3.1-p112; ohai-8.19.2; x86_64-linux; +https://chef.io)" "127.0.0.1:8000" "401" "0.003" "12.14.38" "algorithm=sha1;version=1.1;" "vagrant-c0971990" "2013-09-25T15:00:14Z" "2jmj7l5rSw0yVb/vlWAYkK/YBwk=" 1060 15 | 16 | ==> /var/log/opscode/opscode-erchef/crash.log <== 17 | 2016-08-28 14:57:36 =ERROR REPORT==== 18 | {<<"method=GET; path=/organizations/brewinc/nodes/vagrant-c0971990; status=401; ">>,"Unauthorized"} 19 | 20 | ==> /var/log/opscode/opscode-erchef/erchef.log <== 21 | 2016-08-28 14:57:36.521 [error] {<<"method=GET; path=/organizations/brewinc/nodes/vagrant-c0971990; status=401; ">>,"Unauthorized"} 22 | 23 | ==> /var/log/opscode/opscode-erchef/current <== 24 | 2016-08-28_14:57:36.52659 [error] {<<"method=GET; path=/organizations/brewinc/nodes/vagrant-c0971990; status=401; ">>,"Unauthorized"} 25 | 26 | ==> /var/log/opscode/opscode-erchef/requests.log.1 <== 27 | 2016-08-28T14:57:36Z erchef@127.0.0.1 method=GET; path=/organizations/brewinc/nodes/vagrant-c0971990; status=401; req_id=g3IAA2QAEGVyY2hlZkAxMjcuMC4wLjEBAAOFrgAAAAAAAAAA; org_name=brewinc; msg=bad_clock; couchdb_groups=false; couchdb_organizations=false; couchdb_containers=false; couchdb_acls=false; 503_mode=false; couchdb_associations=false; couchdb_association_requests=false; req_time=1; user=vagrant-c0971990; req_api_version=1; 28 | 29 | ``` 30 | Additionally, the chef_gate log will contain a similar message: 31 | ``` 32 | # /var/log/opscode/chef_gate/current 33 | 2016-08-28_15:01:34.88702 [GIN] 2016/08/28 - 15:01:34 | 401 | 13.650403ms | 192.168.200.102 | POST /compliance/organizations/brewinc/inspec 34 | 2016-08-28_15:01:34.88704 Error #01: Authentication failed. Please check your system's clock. 35 | ``` 36 | 37 | ### 401 Token and Refresh Token Authentication 38 | 39 | In the event of a malformed or unset token, the Chef Compliance server will log the token error: 40 | ``` 41 | ==> /var/log/chef-compliance/core/current <== 42 | 2016-08-28_20:41:46.17496 20:41:46.174 ERR => Token authentication: %!(EXTRA *errors.errorString=malformed JWS, only 1 segments) 43 | 2016-08-28_20:41:46.17498 [GIN] 2016/08/28 - 20:41:46 | 401 | 53.824µs | 192.168.200.102 | GET /owners/base/compliance/linux/tar 44 | 45 | ==> /var/log/chef-compliance/nginx/compliance.access.log <== 46 | 192.168.200.102 - - [28/Aug/2016:21:23:46 +0000] "GET /api/owners/base/compliance/linux/tar HTTP/1.1" 401 0 "-" "Ruby" 47 | ``` 48 | 49 | ### 413 Request Entity Too Large 50 | 51 | If the `audit` cookbook report handler prints this stacktrace and you are using the `chef-server-automate` reporter: 52 | ``` 53 | Running handlers: 54 | [2017-12-21T16:21:15+00:00] WARN: Compliance report size is 1.71 MB. 55 | [2017-12-21T16:21:15+00:00] ERROR: 413 "Request Entity Too Large" (Net::HTTPServerException) 56 | /opt/chef/embedded/lib/ruby/2.4.0/net/http/response.rb:122:in `error!' 57 | /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4/lib/chef/http.rb:152:in `request' 58 | /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.6.4/lib/chef/http.rb:131:in `post' 59 | /var/chef/cache/cookbooks/audit/libraries/reporters/cs_automate.rb:37:in `block in send_report' 60 | ... 61 | ``` 62 | 63 | and the Chef Infra Server Nginx logs confirm the `413` error: 64 | ``` 65 | ==> /var/log/opscode/nginx/access.log <== 66 | 192.168.56.40 - - [21/Dec/2017:11:35:30 +0000] "POST /organizations/eu_org/data-collector HTTP/1.1" 413 "0.803" 64 "-" "Chef Client/13.6.4 (ruby-2.4.2-p198; ohai-13.6.0; x86_64-linux; +https://chef.io)" "-" "-" "-" "13.6.4" "algorithm=sha1;version=1.1;" "bootstrapped-node" "2017-12-21T11:35:31Z" "GR6RyPvKkZDaGyQDYCPfoQGS8G4=" 1793064 67 | ``` 68 | 69 | you most likely hit the `erchef` request size in Chef Infra Server. Prior to Infra Server 13.0, the default was ~1MB. Infra Server 13.0 and later default to ~2MB. 70 | 71 | As an example, to set the limit to ~3MB, add the following line in Chef Server's `/etc/opscode/chef-server.rb`: 72 | ``` 73 | opscode_erchef['max_request_size'] = 3000000 74 | ``` 75 | and run `chef-server-ctl reconfigure` to apply this change. 76 | 77 | ## Chef Automate Backend Errors 78 | 79 | If a Compliance report is not becoming available in the Chef Automate UI or API and this error shows up in the `logstash` logs: 80 | ``` 81 | /var/log/delivery/logstash/current 82 | 2017-12-21_13:59:54.69949 DEBUG: Ruby filter is processing an 'inspec_profile' event 83 | 2017-12-21_14:00:16.51553 java.lang.OutOfMemoryError: Java heap space 84 | 2017-12-21_14:00:16.51556 Dumping heap to /opt/delivery/embedded/logstash/heapdump.hprof ... 85 | 2017-12-21_14:00:16.51556 Unable to create /opt/delivery/embedded/logstash/heapdump.hprof: File exists 86 | 2017-12-21_14:00:18.50676 Error: Your application used more memory than the safety cap of 383M. 87 | 2017-12-21_14:00:18.50694 Specify -J-Xmx####m to increase it (#### = cap size in MB). 88 | 2017-12-21_14:00:18.50703 Specify -w for full OutOfMemoryError stack trace 89 | ``` 90 | 91 | you have reached the java heap size(`-Xmx`) limit of `logstash`. This is automatically set during `automate-ctl reconfigure` to 10% of the system memory. 92 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Stephan Renatus 3 | # Copyright:: (c) 2016-2019, Chef Software Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Controls the inspec gem version to install and execution. Example values: '1.1.0', 'latest' 18 | # Starting with Chef Infra Client 15, only the embedded InSpec gem can be used and this attribute will be ignored 19 | default['audit']['inspec_version'] = nil 20 | 21 | # sets URI to alternate gem source 22 | # example values: nil, 'https://mygem.server.com' 23 | # notes: the root of the URL must host the *specs.4.8.gz source index 24 | default['audit']['inspec_gem_source'] = nil 25 | 26 | # If enabled, a cache is built for all backend calls. This should only be 27 | # disabled if you are expecting unique results from the same backend call. 28 | default['audit']['inspec_backend_cache'] = true 29 | 30 | # controls where inspec scan reports are sent 31 | # possible values: 'chef-server-automate', 'chef-automate', 'json-file' 32 | # notes: 'chef-automate' requires inspec version 0.27.1 or greater 33 | # deprecated: 'chef-visibility' is replaced with 'chef-automate' 34 | # deprecated: 'chef-compliance' is replaced with 'chef-automate' 35 | # deprecated: 'chef-server-visibility' is replaced with 'chef-server-automate' 36 | default['audit']['reporter'] = 'json-file' 37 | 38 | # controls where inspec profiles are fetched from, Chef Automate or via Chef Server 39 | # possible values: nil, 'chef-server', 'chef-automate' 40 | default['audit']['fetcher'] = nil 41 | 42 | # allow for connections to HTTPS endpoints using self-signed ssl certificates 43 | default['audit']['insecure'] = nil 44 | 45 | # Optional for 'chef-server-automate' reporter 46 | # defaults to Chef Server org if not defined 47 | default['audit']['owner'] = nil 48 | 49 | # raise exception if Automate API endpoint is unreachable 50 | # while fetching profiles or posting a report 51 | default['audit']['raise_if_unreachable'] = true 52 | 53 | # fail converge if downloaded profile is not present 54 | # https://github.com/chef-cookbooks/audit/issues/166 55 | default['audit']['fail_if_not_present'] = false 56 | 57 | # control how often inspec scans are run, if not on every node converge 58 | # notes: false value will result in running inspec scan every converge 59 | default['audit']['interval']['enabled'] = false 60 | 61 | # controls how often inspec scans are run (in minutes) 62 | # notes: only used if interval is enabled above 63 | default['audit']['interval']['time'] = 1440 64 | 65 | # controls verbosity of inspec runner 66 | default['audit']['quiet'] = true 67 | 68 | # Chef Inspec Compliance profiles to be used for scan of node 69 | # See README.md for details 70 | default['audit']['profiles'] = {} 71 | 72 | # Attributes used to run the given profiles 73 | default['audit']['attributes'] = {} 74 | 75 | # Set this to false if you don't want ['audit']['attributes'] to be saved in the node object and stored in Chef Server or Automate. Useful if you are passing sensitive data to the inspec profile via the attributes. 76 | default['audit']['attributes_save'] = true 77 | 78 | # If enabled, a hash of the Chef "node" object will be sent to InSpec in an attribute 79 | # named `chef_node` 80 | default['audit']['chef_node_attribute_enabled'] = false 81 | 82 | # Set this to the path of a YAML waiver file you wish to apply 83 | # See https://www.inspec.io/docs/reference/waivers/ 84 | default['audit']['waiver_file'] = nil 85 | 86 | # The location of the json-file output: 87 | # /cookbooks/audit/inspec-.json 88 | default['audit']['json_file']['location'] = 89 | File.expand_path(Time.now.utc.strftime('../../inspec-%Y%m%d%H%M%S.json'), __FILE__) 90 | 91 | # Control results that have a `run_time` below this limit will 92 | # be stripped of the `start_time` and `run_time` fields to 93 | # reduce the size of the reports being sent to Automate 94 | default['audit']['run_time_limit'] = 1.0 95 | 96 | # A control result message that exceeds this character limit will be truncated. 97 | # This helps keep reports to a reasonable size. On rare occasions, we've seen messages exceeding 9 MB in size, 98 | # causing the report to not be ingested in the backend because of the 4 MB report size rpc limitation. 99 | # InSpec will append this text at the end of any truncated messages: `[Truncated to 10000 characters]` 100 | # Requires InSpec 4.18.114 or newer (bundled with Chef Infra Client starting with version 16.0.303) 101 | default['audit']['result_message_limit'] = 10000 102 | 103 | # When an InSpec resource throws an exception (e.g. permission denied), results will contain a short error message and a 104 | # detailed ruby stacktrace of the error. This attribute instructs InSpec not to include the detailed stacktrace in order 105 | # to keep the overall report to a manageable size. 106 | # Requires InSpec 4.18.114 or newer (bundled with Chef Infra Client starting with version 16.0.303) 107 | default['audit']['result_include_backtrace'] = false 108 | 109 | # The array of results per control will be truncated at this limit to avoid large reports that cannot be 110 | # processed by Automate. A summary of removed results will be sent with each impacted control. 111 | default['audit']['control_results_limit'] = 50 112 | -------------------------------------------------------------------------------- /chefignore: -------------------------------------------------------------------------------- 1 | # Put files/directories that should be ignored in this file when uploading 2 | # to a Chef Infra Server or Supermarket. 3 | # Lines that start with '# ' are comments. 4 | 5 | # OS generated files # 6 | ###################### 7 | .DS_Store 8 | ehthumbs.db 9 | Icon? 10 | nohup.out 11 | Thumbs.db 12 | .envrc 13 | 14 | # EDITORS # 15 | ########### 16 | .#* 17 | .project 18 | .settings 19 | *_flymake 20 | *_flymake.* 21 | *.bak 22 | *.sw[a-z] 23 | *.tmproj 24 | *~ 25 | \#* 26 | REVISION 27 | TAGS* 28 | tmtags 29 | .vscode 30 | .editorconfig 31 | 32 | ## COMPILED ## 33 | ############## 34 | *.class 35 | *.com 36 | *.dll 37 | *.exe 38 | *.o 39 | *.pyc 40 | *.so 41 | */rdoc/ 42 | a.out 43 | mkmf.log 44 | 45 | # Testing # 46 | ########### 47 | .circleci/* 48 | .codeclimate.yml 49 | .delivery/* 50 | .foodcritic 51 | .kitchen* 52 | .mdlrc 53 | .overcommit.yml 54 | .rspec 55 | .rubocop.yml 56 | .travis.yml 57 | .watchr 58 | .yamllint 59 | azure-pipelines.yml 60 | Dangerfile 61 | examples/* 62 | features/* 63 | Guardfile 64 | kitchen.yml* 65 | mlc_config.json 66 | Procfile 67 | Rakefile 68 | spec/* 69 | test/* 70 | 71 | # SCM # 72 | ####### 73 | .git 74 | .gitattributes 75 | .gitconfig 76 | .github/* 77 | .gitignore 78 | .gitkeep 79 | .gitmodules 80 | .svn 81 | */.bzr/* 82 | */.git 83 | */.hg/* 84 | */.svn/* 85 | 86 | # Berkshelf # 87 | ############# 88 | Berksfile 89 | Berksfile.lock 90 | cookbooks/* 91 | tmp 92 | 93 | # Bundler # 94 | ########### 95 | vendor/* 96 | Gemfile 97 | Gemfile.lock 98 | 99 | # Policyfile # 100 | ############## 101 | Policyfile.rb 102 | Policyfile.lock.json 103 | 104 | # Documentation # 105 | ############# 106 | CODE_OF_CONDUCT* 107 | CONTRIBUTING* 108 | documentation/* 109 | TESTING* 110 | UPGRADING* 111 | 112 | # Vagrant # 113 | ########### 114 | .vagrant 115 | Vagrantfile 116 | -------------------------------------------------------------------------------- /docs/supported_configuration.md: -------------------------------------------------------------------------------- 1 | ## Supported Configurations 2 | 3 | 4 | 5 | 6 | 7 | 41 | 42 | 43 | 44 | 45 | 46 | 89 | 90 | 91 |
Report to Chef Automate via Chef Server 8 | 9 | when fetching profiles from Chef Automate via Chef Server 10 |
11 | # audit cookbook attributes:
12 | ['audit']['reporter'] = 'chef-server-automate'
13 | ['audit']['fetcher'] = 'chef-server'
14 | ['audit']['profiles']['linux-baseline'] = { 'compliance': 'linux-baseline', 'version': '2.2.2' }
15 |  
16 | # chef-server.rb (Chef Server configuration):
17 | data_collector['root_url'] = 'https://automate-server.test/data-collector/v0/'
18 | profiles['root_url'] = 'https://automate-server.test'
19 |  
20 | # delivery.rb (configuration only for Automate v1):
21 | compliance_profiles["enable"] = true
22 | 
23 | 24 | when fetching URL and GIT profiles 25 |
26 | # audit cookbook attributes:
27 | ['audit']['reporter'] = 'chef-server-automate'
28 | ['audit']['fetcher'] = 'chef-automate'
29 | ['audit']['profiles']['linux-baseline'] = { 'url': 'https://github.com/dev-sec/linux-baseline/archive/2.0.1.tar.gz' }
30 | ['audit']['profiles']['ssl-benchmark'] = { 'git': 'https://github.com/dev-sec/ssl-benchmark.git' }
31 |  
32 | # chef-server.rb (Chef Server configuration):
33 | data_collector['root_url'] = 'https://automate-server.test/data-collector/v0/'
34 | profiles['root_url'] = 'https://automate-server.test'
35 |  
36 | # delivery.rb (configuration only for Automate v1):
37 | compliance_profiles["enable"] = true
38 | 
39 | 40 |
Report directly to Chef Automate 47 | 48 | when fetching profiles from Chef Automate 49 |
50 | # audit cookbook attributes:
51 | ['audit']['reporter'] = 'chef-automate'
52 | ['audit']['fetcher'] = 'chef-automate'
53 | ['audit']['profiles']['linux-baseline'] = { 'compliance': 'linux-baseline' }
54 |  
55 | # client.rb (Chef Client configuration):
56 | data_collector['server_url'] = 'https://automate-server.test/data-collector/v0/'
57 | data_collector['token'] = '...'
58 | 
59 | 60 | 61 | when fetching URL and GIT profiles 62 |
63 | # audit cookbook attributes:
64 | ['audit']['reporter'] = 'chef-automate'
65 | ['audit']['fetcher'] = 'chef-automate'
66 | ['audit']['profiles']['linux-baseline'] = { 'url': 'https://github.com/dev-sec/linux-baseline/archive/2.0.1.tar.gz' }
67 | ['audit']['profiles']['ssl-benchmark'] = { 'git': 'https://github.com/dev-sec/ssl-benchmark.git' }
68 |  
69 | # client.rb (Chef Client configuration):
70 | data_collector['server_url'] = 'https://automate-server.test/data-collector/v0/'
71 | data_collector['token'] = '...'
72 | 
73 | 74 | 75 | when fetching local path and Chef Supermarket profiles 76 |
77 | # audit cookbook attributes:
78 | ['audit']['reporter'] = 'chef-automate'
79 | ['audit']['fetcher'] = 'chef-automate'
80 | ['audit']['profiles']['web-iis'] = { 'path': 'E:/profiles/web-iis' }
81 | ['audit']['profiles']['ssh-baseline'] = { 'supermarket': 'dev-sec/ssh-baseline' }
82 |  
83 | # client.rb (Chef Client configuration):
84 | data_collector['server_url'] = 'https://automate-server.test/data-collector/v0/'
85 | data_collector['token'] = '...'
86 | 
87 | 88 |
92 | -------------------------------------------------------------------------------- /examples/automate_win/.kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: 3 | name: vagrant 4 | 5 | provisioner: 6 | name: chef_zero 7 | 8 | platforms: 9 | - name: windows-2012R2 10 | driver_config: 11 | box: mwrock/Windows2012R2 12 | 13 | suites: 14 | - name: automate_win 15 | run_list: 16 | - recipe[automate_win::chef_client_config] 17 | attributes: 18 | -------------------------------------------------------------------------------- /examples/automate_win/Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | -------------------------------------------------------------------------------- /examples/automate_win/README.md: -------------------------------------------------------------------------------- 1 | # Example: Automate Ingest Cookbook 2 | 3 | This example cookbook demonstrates how to setup chef-client to send converge data to your Chef Automate. In your environment, the content of this cookbook might likely just be added to an existing cookbook that configures chef-client in Windows, or even a cookbook that configures common Windows components on your machines. This cookbook is based on the information in `https://docs.chef.io/ingest_data_chef_automate.html` 4 | 5 | This example cookbook is also consumed in the example wrapper cookbook, named `wrapper_audit` 6 | 7 | ## Requirements 8 | 9 | ### Platforms 10 | 11 | - Windows Server 2012 R2 12 | 13 | ### Chef 14 | 15 | - Chef 12+ 16 | 17 | ### Cookbooks 18 | 19 | - `windows` 20 | 21 | ## Attributes 22 | 23 | No custom. 24 | 25 | ## Usage 26 | 27 | For this example, you would add the following recipes to a node's run list: 28 | - `automate_win::chef_client_config` - Add configuration to chef-client for Chef Automate data ingest. 29 | -------------------------------------------------------------------------------- /examples/automate_win/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'automate_win' 2 | maintainer 'Chef Software, Inc' 3 | maintainer_email 'support@chef.io' 4 | license 'Apache-2.0' 5 | description 'Sample cookbook for configuring Chef Automate direct reporting' 6 | version '0.2.0' 7 | 8 | depends 'chef-client' 9 | -------------------------------------------------------------------------------- /examples/automate_win/recipes/chef_client_config.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: automate_win 3 | # Recipe:: chef_client_config 4 | 5 | # Create client.d directory in the chef client folder 6 | directory 'c:/chef/client.d/' do 7 | recursive true 8 | action :create 9 | end 10 | 11 | # Create the automate_ingest file in the client.d directory which will send 12 | # 'chef-client' data to Chef Automate 13 | template 'c:/chef/client.d/automate_ingest.rb' do 14 | source 'automate_ingest.rb.erb' 15 | variables( 16 | server_url: 'https://automateserver.domain.com/data-collector/v0/', 17 | auth_token: 'some#databag#value' 18 | ) 19 | notifies :create, 'ruby_block[reload_client_config]' 20 | action :create 21 | end 22 | 23 | include_recipe 'chef-client::config' 24 | -------------------------------------------------------------------------------- /examples/automate_win/recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: automate_win 3 | # Recipe:: default 4 | 5 | include_recipe 'automate_win::chef_client_config' 6 | -------------------------------------------------------------------------------- /examples/automate_win/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'chefspec' 2 | require 'chefspec/berkshelf' 3 | ChefSpec::Coverage.start! 4 | # Configure defaults 5 | RSpec.configure do |config| 6 | # Specify the operating platform to mock Ohai data from (default: nil) 7 | config.platform = 'windows' 8 | # Specify the operating version to mock Ohai data from (default: nil) 9 | config.version = '2012R2' 10 | end 11 | -------------------------------------------------------------------------------- /examples/automate_win/spec/unit/recipes/chef_client_config_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: automate_ingest 3 | # Spec:: chef_client_config 4 | 5 | # THIS IS NOT A WORKING EXAMPLE. 6 | # This is more of a stub to conceptualize what to test from 7 | # chef_client_config.rb 8 | require 'spec_helper' 9 | 10 | describe 'automate_ingest::chef_client_config' do 11 | let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) } 12 | 13 | it 'Creates the config.d directory' do 14 | expect(chef_run).to create_directory('c:\chef\config.d') 15 | end 16 | 17 | it 'Creates the automate_ingest.rb cookbook file' do 18 | expect(chef_run).to create_template('c:/chef/client.d/\ 19 | automate_ingest.rb') 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /examples/automate_win/spec/unit/recipes/default_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: automate_win 3 | # Spec:: default 4 | 5 | require 'spec_helper' 6 | 7 | describe 'automate_win::default' do 8 | let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) } 9 | end 10 | -------------------------------------------------------------------------------- /examples/automate_win/templates/default/automate_ingest.rb.erb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 | # FILE IS MANAGED BY CHEF, DON'T EDIT MANUALLY. CHANGES WILL BE OVERWRITTEN! 4 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5 | 6 | # See https://docs.chef.io/ingest_data_chef_automate.html. 7 | 8 | # Your automate server URL would go here 9 | data_collector.server_url '<%= @server_url %>' 10 | # Your token from setting up Chef Automate 11 | data_collector.token '<%= @auth_token %>' 12 | 13 | # If you want to change the default SSL certificate validation 14 | #ssl_verify_mode :verify_none 15 | #verify_api_cert false 16 | -------------------------------------------------------------------------------- /examples/chef-server/README.md: -------------------------------------------------------------------------------- 1 | # Example: Vagrant with Chef Server and Chef Compliance 2 | 3 | This directory contains a simple vagrant setup that expects you have a Chef Server already running. 4 | 5 | 1.) Upload cookbook to Chef Server 6 | 7 | ``` 8 | $ berks vendor -e integration 9 | Resolving cookbook dependencies... 10 | Fetching 'audit' from source at . 11 | Using audit (2.0.0) from source at . 12 | Using compat_resource (12.16.1) 13 | Using chef_handler (2.0.0) 14 | Vendoring audit (2.0.0) to /Users/jmiller/Devel/ChefProject/audit/berks-cookbooks/audit 15 | Vendoring chef_handler (2.0.0) to /Users/jmiller/Devel/ChefProject/audit/berks-cookbooks/chef_handler 16 | Vendoring compat_resource (12.16.1) to /Users/jmiller/Devel/ChefProject/audit/berks-cookbooks/compat_resource 17 | $ knife cookbook upload -a -o berks-cookbooks 18 | Uploading audit [2.0.0] 19 | Uploading chef_handler [2.0.0] 20 | Uploading compat_resource [12.16.1] 21 | Uploaded all cookbooks. 22 | $ 23 | ``` 24 | 25 | Or if you want to upload cookbooks individually, you could use `knife`. This expects that `compat_resource` and `chef_handler` are already uploaded to Chef Server. 26 | 27 | ``` 28 | mkdir cookbooks 29 | cd cookbooks 30 | git clone https://github.com/chef-cookbooks/audit.git 31 | cd .. 32 | chef exec knife cookbook upload audit -o ./cookbooks -c test-chef-server/knife.rb 33 | ``` 34 | 35 | 2.) Adapt the chef Server settings in vagrant file: 36 | 37 | ``` 38 | chef.chef_server_url = 'https://192.168.33.101/organizations/brewinc' 39 | chef.validation_key_path = 'brewinc-validator.pem' 40 | chef.validation_client_name = 'brewinc-validator' 41 | ``` 42 | 43 | 3.) Start node with chef-client 44 | 45 | ``` 46 | vagrant up 47 | # or if you have it already up 48 | vagrant provision 49 | ``` 50 | -------------------------------------------------------------------------------- /examples/chef-server/Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | NODE_SCRIPT = < 39 | token: <%= ENV['COMPLIANCE_ACCESSTOKEN'] %> 40 | refresh_token: <%= ENV['COMPLIANCE_REFRESHTOKEN'] %> 41 | insecure: true 42 | # The owner should show up in the bottom-left of the home page of Compliance. 43 | # If it is integrated with Chef Server, it will be the Chef Server org-name 44 | owner: yourchef-orgname 45 | profiles: 46 | # Example default profile already in Compliance. 47 | base/windows: true 48 | # If you create and import a custom profile you wrote, you can also add it. 49 | yourchef-orgname/customprofilename: true 50 | -------------------------------------------------------------------------------- /examples/wrapper_audit/Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | -------------------------------------------------------------------------------- /examples/wrapper_audit/README.md: -------------------------------------------------------------------------------- 1 | # Example: Wrapper cookbook for `Audit` 2 | 3 | This example cookbook demonstrates how you could wrap the audit cookbook with a customizable internal cookbook. This might be done to easily change default attributes set in the audit cookbook. The wrapper also helps you control the versioning of the audit cookbook in your environment. It can also be useful to help create roles to target where the audit cookbook applies, or even set attributes on nodes to apply Compliance profiles (if you had some you want to run on all machines). 4 | 5 | ## Requirements 6 | 7 | ### Platforms 8 | - Windows Server 2008 R2 9 | - Windows Server 2012 10 | - Windows Server 2012 R2 11 | - Ubuntu 12.04 12 | - Ubuntu 14.04 13 | 14 | ### Chef 15 | 16 | - Chef 12+ 17 | 18 | ### Cookbooks 19 | 20 | - `audit` 21 | - `automate_win` 22 | 23 | ## Attributes 24 | 25 | There are no custom attributes for this cookbook. 26 | 27 | ## Usage 28 | 29 | Include `wrapper_audit::default` in a node's `run_list`. This will also pull down the community audit cookbook, which is used to run Chef Compliance profiles. These profiles will create a report and send it to Chef Compliance or Chef Automate, depending on your `['audit']['reporter']` setting. 30 | 31 | This cookbook also consumes the example cookbook `automate_win`, which demonstrates how to setup chef-client to ingest Chef Automate data. 32 | -------------------------------------------------------------------------------- /examples/wrapper_audit/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'wrapper_audit' 2 | maintainer 'Chef Software, Inc' 3 | maintainer_email 'support@chef.io' 4 | license 'Apache-2.0' 5 | description 'Wrapper cookbook that runs Audit cookbook.' 6 | version '0.2.0' 7 | 8 | # Put whatever operating systems your company supports where you may want 9 | # to use Compliance profiles 10 | supports 'windows' 11 | 12 | depends 'audit', '>= 0.14.1' 13 | depends 'automate_win', '>=0.2.0' 14 | -------------------------------------------------------------------------------- /examples/wrapper_audit/recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: wrapper_audit 3 | # Recipe:: default 4 | 5 | # This includes statement is to include the chef_client_config recipe in the 6 | # automate_win cookbook, which is another sample cookbook under the 7 | # examples directory. The automate_win cookbook provides an example of 8 | # how you would setup chef-client to send converge data to your 9 | # Chef Automate server. 10 | include_recipe 'automate_win::chef_client_config' 11 | 12 | # Set the collector to chef-automate instead of the default chef-server-compliance. 13 | node.default['audit']['collector'] = 'chef-automate' 14 | 15 | # Execute the community audit cookbook with the collector set 16 | include_recipe 'audit::default' 17 | -------------------------------------------------------------------------------- /examples/wrapper_audit/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'chefspec' 2 | require 'chefspec/berkshelf' 3 | ChefSpec::Coverage.start! 4 | -------------------------------------------------------------------------------- /examples/wrapper_audit/spec/unit/recipes/default_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: wrapper_audit 3 | # Spec:: default 4 | 5 | require 'spec_helper' 6 | 7 | describe 'wrapper_audit::default' do 8 | let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) } 9 | end 10 | -------------------------------------------------------------------------------- /files/default/handler/audit_report.rb: -------------------------------------------------------------------------------- 1 | require 'chef/handler' 2 | include ReportHelpers 3 | 4 | class Chef 5 | class Handler 6 | # Creates a compliance audit report 7 | class AuditReport < ::Chef::Handler 8 | MIN_INSPEC_VERSION = '1.25.1'.freeze 9 | MIN_INSPEC_VERSION_AUTOMATE_REPORTER = '2.2.64'.freeze 10 | 11 | def report 12 | # get reporter(s) from attributes as an array 13 | reporters = get_reporters(node['audit']) 14 | 15 | if reporters.include?('chef-visibility') 16 | Chef::Log.warn 'reporter `chef-visibility` is deprecated and removed in audit cookbook 4.0. Please use `chef-automate`.' 17 | end 18 | 19 | if reporters.include?('chef-server-visibility') 20 | Chef::Log.warn 'reporter `chef-server-visibility`is deprecated and removed in audit cookbook 4.0. Please use `chef-server-automate`.' 21 | end 22 | 23 | if reporters.include?('chef-compliance') 24 | Chef::Log.warn 'reporter `chef-compliance` is deprecated and removed in audit cookbook 9.0. Please use `chef-automate`.' 25 | end 26 | 27 | if reporters.include?('chef-server-compliance') 28 | Chef::Log.warn 'reporter `chef-server-compliance` is deprecated and removed in audit cookbook 9.0. Please use `chef-automate`.' 29 | end 30 | 31 | # collect attribute values 32 | server = node['audit']['server'] 33 | interval = node['audit']['interval'] 34 | interval_enabled = node['audit']['interval']['enabled'] 35 | interval_time = node['audit']['interval']['time'] 36 | if node['audit']['profiles'].class.eql?(Chef::Node::ImmutableMash) 37 | profiles = [] 38 | node['audit']['profiles'].keys.each do |p| 39 | h = node['audit']['profiles'][p].to_hash 40 | h['name'] = p 41 | profiles.push(h) 42 | end 43 | else 44 | Chef::Log.warn "Use of a hash array for the node['audit']['profiles'] is deprecated. Please refer to the README and use a hash of hashes." 45 | profiles = node['audit']['profiles'] 46 | end 47 | quiet = node['audit']['quiet'] 48 | fetcher = node['audit']['fetcher'] 49 | 50 | attributes = node.run_state['audit_attributes'].to_h 51 | 52 | # add chef node data as an attribute if enabled 53 | attributes['chef_node'] = chef_node_attribute_data if node['audit']['chef_node_attribute_enabled'] 54 | 55 | # load inspec, supermarket bundle and compliance bundle 56 | load_needed_dependencies 57 | 58 | # confirm our inspec version is valid 59 | validate_inspec_version 60 | 61 | # detect if we run in a chef client with chef server 62 | load_chef_fetcher if reporters.include?('chef-server') || 63 | reporters.include?('chef-server-automate') || 64 | %w(chef-server chef-server-automate).include?(fetcher) 65 | 66 | load_automate_fetcher if fetcher == 'chef-automate' 67 | 68 | # true if profile is due to run (see libraries/helper.rb) 69 | if check_interval_settings(interval, interval_enabled, interval_time) 70 | 71 | # create a file with a timestamp to calculate interval timing 72 | create_timestamp_file if interval_enabled 73 | reporter_format = 'json' 74 | if Gem::Version.new(::Inspec::VERSION) >= Gem::Version.new(MIN_INSPEC_VERSION_AUTOMATE_REPORTER) 75 | reporter_format = 'json-automate' 76 | end 77 | 78 | # return hash of opts to be used by runner 79 | opts = get_opts(reporter_format, quiet, attributes) 80 | 81 | # instantiate inspec runner with given options and run profiles; return report 82 | report = call(opts, profiles) 83 | 84 | # send report to the correct reporter (automate, chef-server) 85 | if !report.empty? 86 | # iterate through reporters 87 | reporters.each do |reporter| 88 | send_report(reporter, server, profiles, report) 89 | end 90 | else 91 | Chef::Log.error 'Audit report was not generated properly, skipped reporting' 92 | end 93 | else 94 | Chef::Log.info 'Audit run skipped due to interval configuration' 95 | end 96 | end 97 | 98 | # overwrite the default run_report_safely to be able to throw exceptions 99 | def run_report_safely(run_status) 100 | run_report_unsafe(run_status) 101 | rescue Exception => e # rubocop:disable Lint/RescueException 102 | Chef::Log.error("Report handler #{self.class.name} raised #{e.inspect}") 103 | Array(e.backtrace).each { |line| Chef::Log.error(line) } 104 | # force a chef-client exception if user requested it 105 | throw e if e.is_a?(Reporter::AuditEnforcer::ControlFailure) || node['audit']['fail_if_not_present'] 106 | ensure 107 | @run_status = nil 108 | end 109 | 110 | def validate_inspec_version 111 | minimum_ver_msg = "This audit cookbook version requires InSpec #{MIN_INSPEC_VERSION} or newer, aborting compliance scan..." 112 | raise minimum_ver_msg if Gem::Version.new(::Inspec::VERSION) < Gem::Version.new(MIN_INSPEC_VERSION) 113 | 114 | # check if we have a valid version for backend caching 115 | backend_cache_msg = 'inspec_backend_cache requires InSpec version >= 1.47.0' 116 | Chef::Log.warn backend_cache_msg if node['audit']['inspec_backend_cache'] && 117 | (Gem::Version.new(::Inspec::VERSION) < Gem::Version.new('1.47.0')) 118 | end 119 | 120 | def load_needed_dependencies 121 | require 'inspec' 122 | # load supermarket plugin, this is part of the inspec gem 123 | require 'bundles/inspec-supermarket/api' 124 | require 'bundles/inspec-supermarket/target' 125 | 126 | # load the compliance plugin 127 | require 'bundles/inspec-compliance/configuration' 128 | require 'bundles/inspec-compliance/support' 129 | require 'bundles/inspec-compliance/http' 130 | require 'bundles/inspec-compliance/api' 131 | require 'bundles/inspec-compliance/target' 132 | end 133 | 134 | # we expect inspec to be loaded before 135 | def load_chef_fetcher 136 | Chef::Log.debug "Load Chef Server fetcher from: #{cookbook_vendor_path}" 137 | $LOAD_PATH.unshift(cookbook_vendor_path) 138 | require 'chef-server/fetcher' 139 | end 140 | 141 | def load_automate_fetcher 142 | Chef::Log.debug "Load Chef Automate fetcher from: #{cookbook_vendor_path}" 143 | $LOAD_PATH.unshift(cookbook_vendor_path) 144 | require 'chef-automate/fetcher' 145 | end 146 | 147 | def get_opts(reporter, quiet, attributes) 148 | output = quiet ? ::File::NULL : $stdout 149 | Chef::Log.debug "Reporter is [#{reporter}]" 150 | # You can pass nil (no waiver files), one file, or an array. InSpec expects an Array regardless. 151 | waivers = Array(node['audit']['waiver_file']).to_a 152 | waivers.delete_if do |file| 153 | unless File.exist?(file) 154 | Chef::Log.error "The specified InSpec waiver file #{file} is missing, skipping it..." 155 | true 156 | end 157 | end 158 | opts = { 159 | 'report' => true, 160 | 'format' => reporter, # For compatibility with older versions of inspec. TODO: Remove this line from Q2 2019 161 | 'reporter' => [reporter], 162 | 'output' => output, 163 | 'logger' => Chef::Log, # Use chef-client log level for inspec run, 164 | backend_cache: node['audit']['inspec_backend_cache'], 165 | attributes: attributes, 166 | waiver_file: waivers, 167 | reporter_message_truncation: node['audit']['result_message_limit'], 168 | reporter_backtrace_inclusion: node['audit']['result_include_backtrace'], 169 | } 170 | opts 171 | end 172 | 173 | # run profiles and return report 174 | def call(opts, profiles) 175 | Chef::Log.info "Using InSpec #{::Inspec::VERSION}" 176 | Chef::Log.debug "Options are set to: #{opts}" 177 | runner = ::Inspec::Runner.new(opts) 178 | 179 | # parse profile hashes for runner, see libraries/helper.rb 180 | tests = tests_for_runner(profiles) 181 | if !tests.empty? 182 | tests.each { |target| runner.add_target(target, opts) } 183 | 184 | Chef::Log.info "Running tests from: #{tests.inspect}" 185 | runner.run 186 | r = runner.report 187 | 188 | # output summary of InSpec Report in Chef Logs 189 | if !r.nil? && opts['format'] == 'json-min' 190 | time = 0 191 | time = r[:statistics][:duration] unless r[:statistics].nil? 192 | passed_controls = r[:controls].select { |c| c[:status] == 'passed' }.size 193 | failed_controls = r[:controls].select { |c| c[:status] == 'failed' }.size 194 | skipped_controls = r[:controls].select { |c| c[:status] == 'skipped' }.size 195 | Chef::Log.info "Summary: #{passed_controls} successful, #{failed_controls} failures, #{skipped_controls} skipped in #{time} s" 196 | end 197 | 198 | Chef::Log.debug "Audit Report #{r}" 199 | r 200 | else 201 | failed_report('No audit tests are defined.') 202 | end 203 | rescue Inspec::FetcherFailure => e 204 | err = "Cannot fetch all profiles: #{tests}. Please make sure you're authenticated and the server is reachable. #{e.message}" 205 | failed_report(err) 206 | rescue => e 207 | failed_report(e.message) 208 | end 209 | 210 | # In case InSpec raises a runtime exception without providing a valid report, 211 | # we make one up and add two new fields to it: `status` and `status_message` 212 | def failed_report(err) 213 | Chef::Log.error "InSpec has raised a runtime exception. Generating a minimal failed report." 214 | Chef::Log.error err 215 | { 216 | "platform": { 217 | "name": "unknown", 218 | "release": "unknown" 219 | }, 220 | "profiles": [], 221 | "statistics": { 222 | "duration": 0.0000001 223 | }, 224 | "version": "4.22.0", 225 | "status": "failed", 226 | "status_message": err 227 | } 228 | end 229 | 230 | # extracts relevant node data 231 | def gather_nodeinfo 232 | n = run_context.node 233 | runlist_roles = n.run_list.select { |item| item.type == :role }.map(&:name) 234 | runlist_recipes = n.run_list.select { |item| item.type == :recipe }.map(&:name) 235 | { 236 | node: n.name, 237 | os: { 238 | release: n['platform_version'], 239 | family: n['platform'], 240 | }, 241 | environment: n.environment, 242 | roles: runlist_roles, 243 | recipes: runlist_recipes, 244 | policy_name: n.policy_name || '', 245 | policy_group: n.policy_group || '', 246 | chef_tags: n.tags, 247 | organization_name: chef_server_uri.path.split('/').last || '', 248 | source_fqdn: chef_server_uri.host || '', 249 | ipaddress: n['ipaddress'], 250 | fqdn: n['fqdn'], 251 | } 252 | end 253 | 254 | # send InSpec report to the reporter (see libraries/reporters.rb) 255 | def send_report(reporter, server, source_location, report) 256 | Chef::Log.info "Reporting to #{reporter}" 257 | 258 | # Set `insecure` here to avoid passing 6 aruguments to `AuditReport#send_report` 259 | # See `cookstyle` Metrics/ParameterLists 260 | insecure = node['audit']['insecure'] 261 | run_time_limit = node['audit']['run_time_limit'] 262 | control_results_limit = node['audit']['control_results_limit'] 263 | 264 | # TODO: harmonize reporter interface 265 | if reporter == 'chef-automate' 266 | # `run_status.entity_uuid` is calling the `entity_uuid` method in libraries/helper.rb 267 | opts = { 268 | entity_uuid: run_status.entity_uuid, 269 | run_id: run_status.run_id, 270 | node_info: gather_nodeinfo, 271 | insecure: insecure, 272 | source_location: source_location, 273 | run_time_limit: run_time_limit, 274 | control_results_limit: control_results_limit, 275 | } 276 | Reporter::ChefAutomate.new(opts).send_report(report) 277 | elsif reporter == 'chef-server-automate' 278 | chef_url = server || base_chef_server_url 279 | chef_org = Chef::Config[:chef_server_url].split('/').last 280 | if chef_url 281 | url = construct_url(chef_url, File.join('organizations', chef_org, 'data-collector')) 282 | # `run_status.entity_uuid` is calling the `entity_uuid` method in libraries/helper.rb 283 | opts = { 284 | entity_uuid: run_status.entity_uuid, 285 | run_id: run_status.run_id, 286 | node_info: gather_nodeinfo, 287 | insecure: insecure, 288 | url: url, 289 | source_location: source_location, 290 | run_time_limit: run_time_limit, 291 | control_results_limit: control_results_limit, 292 | } 293 | Reporter::ChefServerAutomate.new(opts).send_report(report) 294 | else 295 | Chef::Log.warn "unable to determine chef-server url required by inspec report collector '#{reporter}'. Skipping..." 296 | end 297 | elsif reporter == 'json-file' 298 | path = node['audit']['json_file']['location'] 299 | Chef::Log.info "Writing report to #{path}" 300 | Reporter::JsonFile.new(file: path).send_report(report) 301 | elsif reporter == 'audit-enforcer' 302 | Reporter::AuditEnforcer.new.send_report(report) 303 | else 304 | Chef::Log.warn "#{reporter} is not a supported InSpec report collector" 305 | end 306 | end 307 | 308 | # Gather Chef node attributes, etc. for passing to the InSpec run 309 | def chef_node_attribute_data 310 | node_data = node.to_h 311 | node_data['chef_environment'] = node.chef_environment 312 | 313 | node_data 314 | end 315 | end 316 | end 317 | end 318 | -------------------------------------------------------------------------------- /files/default/vendor/chef-automate/fetcher.rb: -------------------------------------------------------------------------------- 1 | module ChefAutomate 2 | class Fetcher < Compliance::Fetcher 3 | name 'chef-automate' 4 | 5 | # it positions itself before `compliance` fetcher 6 | # only load it, if you want to use audit cookbook in Chef Solo with Chef Automate 7 | priority 502 8 | 9 | def self.resolve(target) 10 | uri = if target.is_a?(String) && URI(target).scheme == 'compliance' 11 | URI(target) 12 | elsif target.respond_to?(:key?) && target.key?(:compliance) 13 | URI("compliance://#{target[:compliance]}") 14 | end 15 | 16 | return nil if uri.nil? 17 | 18 | # we have detailed information available in our lockfile, no need to ask the server 19 | if target.respond_to?(:key?) && target.key?(:url) 20 | profile_fetch_url = target[:url] 21 | config = {} 22 | else 23 | # verifies that the target e.g base/ssh exists 24 | profile = sanitize_profile_name(uri) 25 | owner, id = profile.split('/') 26 | profile_path = if target.respond_to?(:key?) && target.key?(:version) 27 | "/compliance/profiles/#{owner}/#{id}/version/#{target[:version]}/tar" 28 | else 29 | "/compliance/profiles/#{owner}/#{id}/tar" 30 | end 31 | dc = Chef::Config[:data_collector] 32 | url = URI(dc[:server_url]) 33 | url.path = profile_path 34 | profile_fetch_url = url.to_s 35 | 36 | raise 'No data-collector token set, which is required by the chef-automate fetcher. ' \ 37 | 'Set the `data_collector.token` configuration parameter in your client.rb ' \ 38 | 'or use the "chef-server-automate" reporter which does not require any ' \ 39 | 'data-collector settings and uses Chef Server to fetch profiles.' if dc[:token].nil? 40 | 41 | config = { 42 | 'token' => dc[:token], 43 | } 44 | end 45 | 46 | new(profile_fetch_url, config) 47 | rescue URI::Error => _e 48 | nil 49 | end 50 | 51 | # returns a parsed url for `admin/profile` or `compliance://admin/profile` 52 | # TODO: remove in future, copied from inspec to support older versions of inspec 53 | def self.sanitize_profile_name(profile) 54 | uri = if URI(profile).scheme == 'compliance' 55 | URI(profile) 56 | else 57 | URI("compliance://#{profile}") 58 | end 59 | uri.to_s.sub(%r{^compliance:\/\/}, '') 60 | end 61 | 62 | def initialize(url, opts) 63 | options = { 64 | 'insecure' => true, 65 | 'token' => opts['token'], 66 | 'server_type' => 'automate', 67 | 'automate' => { 68 | 'ent' => 'default', 69 | 'token_type' => 'dctoken', 70 | }, 71 | } 72 | super(url, options) 73 | end 74 | 75 | def to_s 76 | 'Chef Automate for Chef Solo Fetcher' 77 | end 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /files/default/vendor/chef-server/fetcher.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | # author: Christoph Hartmann 3 | 4 | require 'uri' 5 | 6 | require 'bundles/inspec-compliance/target' 7 | require 'inspec/fetcher' 8 | require 'inspec/errors' 9 | 10 | # This class implements an InSpec fetcher for for Chef Server. The implementation 11 | # is based on the Chef Compliance fetcher and only adapts the calls to redirect 12 | # the requests via Chef Server. 13 | # 14 | # This implementation depends on chef-client runtime, therefore it is only executable 15 | # inside of a chef-client run 16 | module ChefServer 17 | class Fetcher < Compliance::Fetcher 18 | name 'chef-server' 19 | 20 | # it positions itself before `compliance` fetcher 21 | # only load it, if the Chef Server is integrated with Chef Compliance 22 | priority 501 23 | 24 | def self.resolve(target) 25 | uri = if target.is_a?(String) && URI(target).scheme == 'compliance' 26 | URI(target) 27 | elsif target.respond_to?(:key?) && target.key?(:compliance) 28 | URI("compliance://#{target[:compliance]}") 29 | end 30 | 31 | return nil if uri.nil? 32 | 33 | profile = uri.host + uri.path 34 | profile = uri.user + '@' + profile if uri.user 35 | 36 | config = { 37 | 'insecure' => true, 38 | } 39 | 40 | if target.respond_to?(:key?) && target.key?(:version) 41 | new(target_url(profile, config, target[:version]), config) 42 | else 43 | new(target_url(profile, config), config) 44 | end 45 | rescue URI::Error => _e 46 | nil 47 | end 48 | 49 | def self.chef_server_url_base 50 | cs = URI(Chef::Config[:chef_server_url]) 51 | cs.path = '' 52 | cs.to_s 53 | end 54 | 55 | def self.chef_server_org 56 | Chef::Config[:chef_server_url].split('/').last 57 | end 58 | 59 | def self.url_prefix 60 | return '/compliance' if chef_server_reporter? || chef_server_fetcher? 61 | '' 62 | end 63 | 64 | def self.target_url(profile, config, version = nil) 65 | o, p = profile.split('/') 66 | reqpath = if version 67 | "organizations/#{chef_server_org}/owners/#{o}/compliance/#{p}/version/#{version}/tar" 68 | else 69 | "organizations/#{chef_server_org}/owners/#{o}/compliance/#{p}/tar" 70 | end 71 | 72 | if config['insecure'] 73 | Chef::Config[:verify_api_cert] = false 74 | Chef::Config[:ssl_verify_mode] = :verify_none 75 | end 76 | 77 | target_url = construct_url(chef_server_url_base + url_prefix + '/', reqpath) 78 | Chef::Log.info("Fetching profile from: #{target_url}") 79 | target_url 80 | end 81 | 82 | # 83 | # We want to save compliance: in the lockfile rather than url: to 84 | # make sure we go back through the ComplianceAPI handling. 85 | # 86 | def resolved_source 87 | { compliance: chef_server_url } 88 | end 89 | 90 | # Downloads archive to temporary file using a Chef::ServerAPI 91 | # client so that Chef Server's header-based authentication can be 92 | # used. 93 | def download_archive_to_temp 94 | return @temp_archive_path unless @temp_archive_path.nil? 95 | 96 | Chef::Config[:verify_api_cert] = false # FIXME 97 | Chef::Config[:ssl_verify_mode] = :verify_none # FIXME 98 | 99 | rest = Chef::ServerAPI.new(@target, Chef::Config) 100 | archive = with_http_rescue do 101 | rest.streaming_request(@target) 102 | end 103 | @archive_type = '.tar.gz' 104 | raise "Unable to find requested profile on path: '#{target_path}' on the Automate system." if archive.nil? 105 | Inspec::Log.debug("Archive stored at temporary location: #{archive.path}") 106 | @temp_archive_path = archive.path 107 | end 108 | 109 | def to_s 110 | 'Chef Server/Compliance Profile Loader' 111 | end 112 | 113 | # internal class methods 114 | def self.chef_server_reporter? 115 | return false unless defined?(Chef) && defined?(Chef.node) && defined?(Chef.node.attributes) 116 | reporters = get_reporters(Chef.node.attributes['audit']) 117 | # TODO: harmonize with audit_report.rb load_chef_fetcher 118 | Chef.node.attributes['audit'] && ( 119 | reporters.include?('chef-server') || 120 | reporters.include?('chef-server-compliance') || 121 | reporters.include?('chef-server-visibility') || 122 | reporters.include?('chef-server-automate') 123 | ) 124 | end 125 | 126 | def self.chef_server_fetcher? 127 | # TODO: harmonize with audit_report.rb load_chef_fetcher 128 | %w(chef-server chef-server-compliance chef-server-visibility chef-server-automate).include?(Chef.node.attributes['audit']['fetcher']) 129 | end 130 | 131 | private 132 | 133 | def chef_server_url 134 | m = %r{^#{@config['server']}/owners/(?[^/]+)/compliance/(?[^/]+)/tar$}.match(@target) 135 | "#{m[:owner]}/#{m[:id]}" 136 | end 137 | 138 | def target_path 139 | return @target.path if @target.respond_to?(:path) 140 | @target 141 | end 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /kitchen.dokken.yml: -------------------------------------------------------------------------------- 1 | driver: 2 | name: dokken 3 | privileged: true # because Docker and SystemD/Upstart 4 | chef_version: <%= ENV['CHEF_VERSION'] || 14 %> 5 | 6 | transport: 7 | name: dokken 8 | 9 | provisioner: 10 | name: dokken 11 | chef_log_level: info 12 | chef_output_format: minimal 13 | 14 | verifier: 15 | name: inspec 16 | sudo: false 17 | 18 | platforms: 19 | - name: amazonlinux 20 | driver: 21 | image: dokken/amazonlinux 22 | pid_one_command: /sbin/init 23 | 24 | - name: amazonlinux-2 25 | driver: 26 | image: dokken/amazonlinux-2 27 | pid_one_command: /usr/lib/systemd/systemd 28 | 29 | - name: debian-8 30 | driver: 31 | image: dokken/debian-8 32 | pid_one_command: /bin/systemd 33 | intermediate_instructions: 34 | - RUN /usr/bin/apt-get update 35 | 36 | - name: debian-9 37 | driver: 38 | image: dokken/debian-9 39 | pid_one_command: /bin/systemd 40 | intermediate_instructions: 41 | - RUN /usr/bin/apt-get update 42 | 43 | - name: centos-6 44 | driver: 45 | image: dokken/centos-6 46 | pid_one_command: /sbin/init 47 | 48 | - name: centos-7 49 | driver: 50 | image: dokken/centos-7 51 | pid_one_command: /usr/lib/systemd/systemd 52 | 53 | - name: oracle-6 54 | driver: 55 | image: oraclelinux:6 56 | pid_one_command: /sbin/init 57 | 58 | - name: oracle-7 59 | driver: 60 | image: oraclelinux:7 61 | pid_one_command: /usr/lib/systemd/systemd 62 | 63 | - name: ubuntu-14.04 64 | driver: 65 | image: dokken/ubuntu-14.04 66 | pid_one_command: /sbin/init 67 | intermediate_instructions: 68 | - RUN /usr/bin/apt-get update 69 | 70 | - name: ubuntu-16.04 71 | driver: 72 | image: dokken/ubuntu-16.04 73 | pid_one_command: /bin/systemd 74 | intermediate_instructions: 75 | - RUN /usr/bin/apt-get update 76 | 77 | - name: ubuntu-18.04 78 | driver: 79 | image: dokken/ubuntu-18.04 80 | pid_one_command: /bin/systemd 81 | intermediate_instructions: 82 | - RUN /usr/bin/apt-get update 83 | 84 | suites: 85 | - name: default # run inspec locally 86 | run_list: 87 | - recipe[test_helper::setup] 88 | - recipe[audit::default] 89 | attributes: 90 | audit: 91 | reporter: json-file 92 | profiles: 93 | - name: ssh-hardening 94 | url: https://github.com/dev-sec/tests-ssh-hardening/archive/master.zip 95 | - name: ssh-baseline 96 | supermarket: dev-sec/ssh-baseline 97 | - name: inspec-attributes 98 | run_list: 99 | - recipe[test_helper::setup] 100 | - recipe[test_helper::create_file] 101 | - recipe[audit::default] 102 | attributes: 103 | audit: 104 | reporter: json-file 105 | json_file: 106 | location: <%= File.join('/tmp', Time.now.utc.strftime('inspec-%Y%m%d%H%M%S.json')) %> 107 | profiles: 108 | attribute-file-exists-profile: 109 | git: https://github.com/mhedgpeth/attribute-file-exists-profile.git 110 | attributes: 111 | file: /opt/kitchen/cache/attribute-file-exists.test 112 | - name: chef-node-enabled 113 | run_list: 114 | - recipe[audit::default] 115 | attributes: 116 | audit: 117 | reporter: json-file 118 | json_file: 119 | location: <%= File.join('/tmp', Time.now.utc.strftime('inspec-%Y%m%d%H%M%S.json')) %> 120 | profiles: 121 | demo: 122 | url: https://github.com/adamleff/inspec-profile-chef-node-attributes/archive/master.tar.gz 123 | chef_node_attribute_enabled: true 124 | - name: chef-node-disabled 125 | run_list: 126 | - recipe[audit::default] 127 | attributes: 128 | audit: 129 | reporter: json-file 130 | json_file: 131 | location: <%= File.join('/tmp', Time.now.utc.strftime('inspec-%Y%m%d%H%M%S.json')) %> 132 | profiles: 133 | demo: 134 | url: https://github.com/adamleff/inspec-profile-chef-node-attributes/archive/master.tar.gz 135 | - name: missing-profile-no-fail 136 | run_list: 137 | - recipe[test_helper::setup] 138 | - recipe[audit::default] 139 | attributes: 140 | audit: 141 | reporter: json-file 142 | profiles: 143 | ssh-hardening: 144 | url: https://github.com/dev-sec/this-is-not-available.zip 145 | includes: 146 | - ubuntu-18.04 147 | - name: missing-profile-fail 148 | run_list: 149 | - recipe[test_helper::setup] 150 | - recipe[audit::default] 151 | attributes: 152 | audit: 153 | reporter: json-file 154 | fail_if_not_present: true 155 | profiles: 156 | ssh-hardening: https://github.com/dev-sec/this-is-not-available.zip 157 | includes: 158 | - ubuntu-18.04 159 | - name: chef15-compatible-inspec 160 | run_list: 161 | - recipe[audit::default] 162 | driver: 163 | chef_version: 15 164 | attributes: 165 | audit: 166 | reporter: json-file 167 | inspec_version: 3.9.3 168 | fail_if_not_present: true 169 | - name: gem-install-core-version4 170 | run_list: 171 | - recipe[test_helper::install_inspec] 172 | - recipe[audit::default] 173 | attributes: 174 | audit: 175 | reporter: json-file 176 | inspec_version: 4.3.2 177 | fail_if_not_present: true 178 | - name: gem-install-core-version3 179 | run_list: 180 | - recipe[test_helper::install_inspec] 181 | - recipe[audit::default] 182 | driver: 183 | chef_version: 14 184 | attributes: 185 | audit: 186 | reporter: json-file 187 | inspec_version: 3.0.9 188 | fail_if_not_present: true 189 | - name: gem-install-core-version2 190 | run_list: 191 | - recipe[test_helper::install_inspec] 192 | - recipe[audit::default] 193 | driver: 194 | chef_version: 14 195 | attributes: 196 | audit: 197 | reporter: json-file 198 | inspec_version: 2.1.67 199 | fail_if_not_present: true 200 | - name: hash 201 | run_list: 202 | - recipe[audit::default] 203 | attributes: 204 | audit: 205 | reporter: json-file 206 | profiles: 207 | ssh-hardening: 208 | url: https://github.com/dev-sec/tests-ssh-hardening/archive/master.zip 209 | ssh-baseline: 210 | supermarket: dev-sec/ssh-baseline 211 | - name: waivers 212 | run_list: 213 | - recipe[test_helper::setup] 214 | - recipe[test_helper::setup_waiver_fixture] 215 | - recipe[audit::default] 216 | attributes: 217 | audit: 218 | reporter: json-file 219 | json_file: 220 | location: <%= File.join('/tmp', Time.now.utc.strftime('inspec-%Y%m%d%H%M%S.json')) %> 221 | waiver_file: <%= File.join('/tmp', 'waivers-fixture', 'waivers', 'waivers.yaml') %> 222 | profiles: 223 | waiver-test-profile: 224 | path: <%= File.join('/tmp', 'waivers-fixture') %> 225 | -------------------------------------------------------------------------------- /kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: 3 | name: vagrant 4 | 5 | transport: 6 | name: ssh 7 | 8 | provisioner: 9 | name: chef_zero 10 | 11 | verifier: 12 | name: inspec 13 | sudo: true 14 | 15 | platforms: 16 | - name: centos-6 17 | - name: centos-7 18 | - name: ubuntu-16.04 19 | - name: ubuntu-18.04 20 | 21 | suites: 22 | - name: default 23 | run_list: 24 | - recipe[audit::default] 25 | attributes: 26 | audit: 27 | reporter: json-file 28 | profiles: 29 | - name: ssh-hardening 30 | url: https://github.com/dev-sec/tests-ssh-hardening/archive/master.zip 31 | - name: ssh-baseline 32 | supermarket: dev-sec/ssh-baseline 33 | - name: hash 34 | run_list: 35 | - recipe[audit::default] 36 | attributes: 37 | audit: 38 | reporter: json-file 39 | profiles: 40 | ssh-hardening: 41 | url: https://github.com/dev-sec/tests-ssh-hardening/archive/master.zip 42 | ssh-baseline: 43 | supermarket: dev-sec/ssh-baseline 44 | -------------------------------------------------------------------------------- /libraries/helper.rb: -------------------------------------------------------------------------------- 1 | module ReportHelpers 2 | # Returns the uuid for the current converge 3 | def run_id 4 | return unless run_context && 5 | run_context.events && 6 | run_context.events.subscribers.is_a?(Array) 7 | run_context.events.subscribers.each do |sub| 8 | if sub.class == Chef::ResourceReporter && defined?(sub.run_id) 9 | return sub.run_id 10 | end 11 | end 12 | nil 13 | end 14 | 15 | # Returns the node's uuid 16 | def entity_uuid 17 | # the Chef::DataCollector::Messages API here is Chef < 15.0 backcompat and can be removed when Chef 14.x is no longer supported 18 | node['chef_guid'] || defined?(Chef::DataCollector::Messages) && Chef::DataCollector::Messages.node_uuid 19 | end 20 | 21 | # Convert the strings in the profile definitions into symbols for handling 22 | def tests_for_runner(profiles) 23 | profiles.map { |profile| Hash[profile.map { |k, v| [k.to_sym, v] }] } 24 | end 25 | 26 | def construct_url(server, path) 27 | # ensure we do not modify a frozen String 28 | srv = server.dup 29 | # sanitize inputs 30 | srv << '/' unless server =~ %r{/\z} 31 | path.sub!(%r{^/}, '') 32 | srv = URI(srv) 33 | srv.path = srv.path + path if path 34 | srv 35 | end 36 | 37 | def with_http_rescue(*) 38 | response = yield 39 | if response.respond_to?(:code) 40 | # handle non 200 error codes, they are not raised as Net::HTTPClientException 41 | handle_http_error_code(response.code) if response.code.to_i >= 300 42 | end 43 | response 44 | rescue Net::HTTPClientException => e 45 | Chef::Log.error e 46 | handle_http_error_code(e.response.code) 47 | end 48 | 49 | def handle_http_error_code(code) 50 | case code 51 | when /401|403/ 52 | Chef::Log.error 'Auth issue: see audit cookbook TROUBLESHOOTING.md' 53 | when /404/ 54 | Chef::Log.error 'Object does not exist on remote server.' 55 | when /413/ 56 | Chef::Log.error 'You most likely hit the erchef request size in Chef Server that defaults to ~2MB. To increase this limit see audit cookbook TROUBLESHOOTING.md OR https://docs.chef.io/config_rb_server.html' 57 | when /429/ 58 | Chef::Log.error "This error typically means the data sent was larger than Automate's limit (4 MB). Run InSpec locally to identify any controls producing large diffs." 59 | end 60 | msg = "Received HTTP error #{code}" 61 | Chef::Log.error msg 62 | raise msg if @raise_if_unreachable 63 | end 64 | 65 | def base_chef_server_url 66 | cs = chef_server_uri 67 | cs.path = '' 68 | cs.to_s 69 | end 70 | 71 | def chef_server_uri 72 | URI(Chef::Config.chef_server_url) 73 | end 74 | 75 | # used for interval timing 76 | def create_timestamp_file 77 | timestamp = Time.now.utc 78 | timestamp_file = File.new(report_timing_file, 'w') 79 | timestamp_file.puts(timestamp) 80 | timestamp_file.close 81 | end 82 | 83 | def report_timing_file 84 | # Will create and return the complete folder path for the chef cache location and the passed in value 85 | ::File.join(Chef::FileCache.create_cache_path('compliance'), 'report_timing.json') 86 | end 87 | 88 | def profile_overdue_to_run?(interval) 89 | # Calculate when a report was last created so we delay the next report if necessary 90 | return true unless ::File.exist?(report_timing_file) 91 | seconds_since_last_run = Time.now - ::File.mtime(report_timing_file) 92 | seconds_since_last_run > interval 93 | end 94 | 95 | def check_interval_settings(interval, interval_enabled, interval_time) 96 | # handle intervals 97 | interval_seconds = 0 # always run this by default, unless interval is defined 98 | if !interval.nil? && interval_enabled 99 | interval_seconds = interval_time * 60 # seconds in interval 100 | Chef::Log.debug "Auditing this machine every #{interval_seconds} seconds " 101 | end 102 | # returns true if profile is overdue to run 103 | profile_overdue_to_run?(interval_seconds) 104 | end 105 | 106 | # takes value of reporters and returns array to ensure backwards-compatibility 107 | def handle_reporters(reporters) 108 | return reporters if reporters.is_a? Array 109 | [reporters] 110 | end 111 | 112 | def cookbook_vendor_path 113 | File.expand_path('../files/default/vendor', __dir__) 114 | end 115 | 116 | def cookbook_handler_path 117 | File.expand_path('../files/default/handler', __dir__) 118 | end 119 | 120 | # Copies ['audit']['attributes'] into run_state for the audit_handler to read them later 121 | # Deletes ['audit']['attributes'] if instructed by ['audit']['attributes_save'] 122 | def copy_audit_attributes 123 | node.run_state['audit_attributes'] = node['audit']['attributes'] 124 | node.rm('audit', 'attributes') unless node['audit']['attributes_save'] 125 | end 126 | 127 | def load_audit_handler 128 | libpath = ::File.join(cookbook_handler_path, 'audit_report') 129 | Chef::Log.info("loading handler from #{libpath}") 130 | $LOAD_PATH.unshift(libpath) unless $LOAD_PATH.include?(libpath) 131 | require libpath 132 | handler = Chef::Handler::AuditReport.new 133 | Chef::Config.send('report_handlers') << handler 134 | Chef::Config.send('exception_handlers') << handler 135 | end 136 | 137 | # taking node['audit'] as parameter so that it can be called from the chef-server fetcher as well 138 | # audit['collector'] is the legacy reporter, 139 | def get_reporters(audit) 140 | if audit.nil? 141 | Chef::Log.warn("node ['audit'] is not defined") 142 | return [] 143 | end 144 | if audit['collector'] 145 | Chef::Log.warn("node ['audit']['collector'] is deprecated and will be removed from the next major version of the cookbook. Please use node ['audit']['reporter']") 146 | return handle_reporters(audit['collector']) 147 | end 148 | handle_reporters(audit['reporter']) 149 | end 150 | 151 | # Extracts all the profile sha256 IDs from an inspec report 152 | def report_profile_sha256s(report) 153 | return [] unless report.is_a?(Hash) && report[:profiles].is_a?(Array) 154 | report[:profiles].map { |p| p[:sha256] } 155 | end 156 | 157 | # Strips profile metadata (title, copyright, controls title, code block, descriptions, etc) 158 | # from the `report` profiles that are not in the `missing_report_shas` array. 159 | # Control results are also stripped of the `start_time` and `run_time` if they don't reach the `run_time_limit` threshold (seconds) 160 | def strip_profiles_meta(report, missing_report_shas, run_time_limit) 161 | return report unless report.is_a?(Hash) && report[:profiles].is_a?(Array) 162 | report[:profiles].each do |p| 163 | next if missing_report_shas.include?(p[:sha256]) 164 | # Profile 'name' is a required property. By not sending it in the report, we make it clear to the ingestion backend that the profile metadata has been stripped from this profile in the report. 165 | # Profile 'title' and 'version' are still kept for troubleshooting purposes in the backend. 166 | p.delete(:name) 167 | p.delete(:groups) 168 | p.delete(:copyright_email) 169 | p.delete(:copyright) 170 | p.delete(:summary) 171 | p.delete(:supports) 172 | p.delete(:license) 173 | p.delete(:maintainer) 174 | next unless p[:controls].is_a?(Array) 175 | p[:controls].each do |c| 176 | c.delete(:code) 177 | c.delete(:desc) 178 | c.delete(:descriptions) 179 | c.delete(:impact) 180 | c.delete(:refs) 181 | c.delete(:tags) 182 | c.delete(:title) 183 | c.delete(:source_location) 184 | c.delete(:waiver_data) if c[:waiver_data] == {} 185 | next unless c[:results].is_a?(Array) 186 | c[:results].each do |r| 187 | if r[:run_time].is_a?(Float) && r[:run_time] < run_time_limit 188 | r.delete(:start_time) 189 | r.delete(:run_time) 190 | end 191 | end 192 | end 193 | end 194 | report[:run_time_limit] = run_time_limit 195 | report 196 | end 197 | 198 | # Truncates the number of results per control in the report when they exceed max_results. 199 | # The truncation prioritizes failed and skipped results over passed ones. 200 | # Controls where results have been truncated will get a new object 'removed_results_counts' 201 | # with the status counts of the truncated results 202 | def truncate_controls_results(report, max_results) 203 | return report unless max_results.is_a?(Integer) && max_results > 0 204 | return report unless report.is_a?(Hash) && report[:profiles].is_a?(Array) 205 | report[:profiles].each do |profile| 206 | next unless profile[:controls].is_a?(Array) 207 | profile[:controls].each do |control| 208 | next unless control[:results].is_a?(Array) 209 | # Only bother with truncation if the number of results exceed max_results 210 | next unless control[:results].length > max_results 211 | res = control[:results] 212 | truncated = { failed: 0, skipped: 0, passed: 0 } 213 | res.sort_by! do |r| 214 | # Replacing "skipped" with "kipped" for the sort logic so that 215 | # the results are sorted in this order: failed, skipped, passed 216 | r[:status] == 'skipped' ? 'kipped' : r[:status] 217 | end 218 | # Count the results that will be truncated 219 | (max_results..res.length - 1).each do |i| 220 | case res[i][:status] 221 | when 'failed' 222 | truncated[:failed] += 1 223 | when 'skipped' 224 | truncated[:skipped] += 1 225 | when 'passed' 226 | truncated[:passed] += 1 227 | end 228 | end 229 | # Truncate the results array now 230 | control[:results] = res[0..max_results - 1] 231 | control[:removed_results_counts] = truncated 232 | end 233 | end 234 | report 235 | end 236 | 237 | # Contacts the metasearch Automate API to check which of the inspec profile sha256 ids 238 | # passed in via `report_shas` are missing from the Automate profiles metadata database. 239 | def missing_automate_profiles(automate_url, headers, report_shas) 240 | Chef::Log.debug "Checking the Automate profiles metadata for: #{report_shas}" 241 | meta_url = URI(automate_url) 242 | meta_url.path = '/compliance/profiles/metasearch' 243 | http = Chef::HTTP.new(meta_url.to_s) 244 | response_str = http.post(nil, "{\"sha256\": #{report_shas}}", headers) 245 | missing_shas = JSON.parse(response_str)['missing_sha256'] 246 | unless missing_shas.empty? 247 | Chef::Log.info "Automate is missing metadata for the following profile ids: #{missing_shas}" 248 | end 249 | missing_shas 250 | rescue => e 251 | Chef::Log.error "missing_automate_profiles error: #{e.message}" 252 | # If we get an error it's safer to assume none of the profile shas exist in Automate 253 | report_shas 254 | end 255 | end 256 | 257 | ::Chef::DSL::Recipe.include ReportHelpers 258 | -------------------------------------------------------------------------------- /libraries/reporters/audit-enforcer.rb: -------------------------------------------------------------------------------- 1 | module Reporter 2 | # 3 | # Used to raise an error on conformance failure 4 | # 5 | class AuditEnforcer 6 | class ControlFailure < StandardError; end 7 | 8 | def send_report(report) 9 | # iterate over each profile and control 10 | report[:profiles].each do |profile| 11 | next if profile[:controls].nil? 12 | profile[:controls].each do |control| 13 | next if control[:results].nil? 14 | control[:results].each do |result| 15 | raise ControlFailure, "Audit #{control[:id]} has failed. Aborting chef-client run." if result[:status] == 'failed' 16 | end 17 | end 18 | end 19 | true 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /libraries/reporters/automate.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | require_relative '../helper' 3 | 4 | module Reporter 5 | # 6 | # Used to send inspec reports to Chef Automate via the data_collector service 7 | # 8 | class ChefAutomate 9 | include ReportHelpers 10 | 11 | def initialize(opts) 12 | @entity_uuid = opts[:entity_uuid] 13 | @run_id = opts[:run_id] 14 | @node_name = opts[:node_info][:node] 15 | @environment = opts[:node_info][:environment] 16 | @roles = opts[:node_info][:roles] 17 | @recipes = opts[:node_info][:recipes] 18 | @insecure = opts[:insecure] 19 | @chef_tags = opts[:node_info][:chef_tags] 20 | @policy_group = opts[:node_info][:policy_group] 21 | @policy_name = opts[:node_info][:policy_name] 22 | @source_fqdn = opts[:node_info][:source_fqdn] 23 | @organization_name = opts[:node_info][:organization_name] 24 | @ipaddress = opts[:node_info][:ipaddress] 25 | @fqdn = opts[:node_info][:fqdn] 26 | @run_time_limit = opts[:run_time_limit] 27 | @control_results_limit = opts[:control_results_limit] 28 | 29 | if defined?(Chef) && 30 | defined?(Chef::Config) && 31 | Chef::Config[:data_collector] && 32 | Chef::Config[:data_collector][:token] && 33 | Chef::Config[:data_collector][:server_url] 34 | 35 | dc = Chef::Config[:data_collector] 36 | @url = dc[:server_url] 37 | @token = dc[:token] 38 | end 39 | end 40 | 41 | # Method used in order to send the inspec report to the data_collector server 42 | def send_report(report) 43 | unless @entity_uuid && @run_id 44 | Chef::Log.error "entity_uuid(#{@entity_uuid}) or run_id(#{@run_id}) can't be nil, not sending report to Chef Automate" 45 | return false 46 | end 47 | 48 | if defined?(Chef) && defined?(Chef::Config) 49 | headers = { 'Content-Type' => 'application/json' } 50 | unless @token.nil? 51 | headers['x-data-collector-token'] = @token 52 | headers['x-data-collector-auth'] = 'version=1.0' 53 | end 54 | 55 | # Enable OpenSSL::SSL::VERIFY_NONE via `node['audit']['insecure']` 56 | # See https://github.com/chef/chef/blob/master/lib/chef/http/ssl_policies.rb#L54 57 | if @insecure 58 | Chef::Config[:verify_api_cert] = false 59 | Chef::Config[:ssl_verify_mode] = :verify_none 60 | end 61 | 62 | all_report_shas = report_profile_sha256s(report) 63 | missing_report_shas = missing_automate_profiles(@url, headers, all_report_shas) 64 | 65 | full_report = truncate_controls_results(enriched_report(report), @control_results_limit) 66 | 67 | # If the Automate backend has the profile metadata for at least one profile, proceed with metadata stripping 68 | full_report = strip_profiles_meta(full_report, missing_report_shas, 1) if missing_report_shas.length < all_report_shas.length 69 | json_report = full_report.to_json 70 | 71 | report_size = json_report.bytesize 72 | # Automate GRPC currently has a message limit of ~4MB 73 | # https://github.com/chef/automate/issues/1417#issuecomment-541908157 74 | if report_size > 4 * 1024 * 1024 75 | Chef::Log.warn "Compliance report size is #{(report_size / (1024 * 1024.0)).round(2)} MB." 76 | Chef::Log.warn 'Automate has an internal 4MB limit that is not currently configurable.' 77 | end 78 | 79 | unless json_report 80 | Chef::Log.warn 'Something went wrong, report can\'t be nil' 81 | return false 82 | end 83 | 84 | begin 85 | Chef::Log.info "Report to Chef Automate: #{@url}" 86 | Chef::Log.debug "Audit Report: #{json_report}" 87 | http = Chef::HTTP.new(@url) 88 | http.post(nil, json_report, headers) 89 | true 90 | rescue => e 91 | Chef::Log.error "send_report: POST to #{@url} returned: #{e.message}" 92 | false 93 | end 94 | else 95 | Chef::Log.warn 'data_collector.token and data_collector.server_url must be defined in client.rb!' 96 | Chef::Log.warn 'Further information: https://github.com/chef-cookbooks/audit#direct-reporting-to-chef-automate' 97 | false 98 | end 99 | end 100 | 101 | # *************************************************************************************** 102 | # TODO: We could likely simplify/remove alot of the extra logic we have here with a small 103 | # revamp of the Automate expected input. 104 | # *************************************************************************************** 105 | 106 | def enriched_report(final_report) 107 | return unless final_report.is_a?(Hash) 108 | 109 | # Remove nil profiles if any 110 | final_report[:profiles].select! { |p| p } 111 | 112 | # Label this content as an inspec_report 113 | final_report[:type] = 'inspec_report' 114 | 115 | # Ensure controls are never stored or shipped, since this was an accidential 116 | # addition in InSpec and will be remove in the next inspec major release 117 | final_report.delete(:controls) 118 | final_report[:node_name] = @node_name 119 | final_report[:end_time] = Time.now.utc.strftime('%FT%TZ') 120 | final_report[:node_uuid] = @entity_uuid 121 | final_report[:environment] = @environment 122 | final_report[:roles] = @roles 123 | final_report[:recipes] = @recipes 124 | final_report[:report_uuid] = @run_id 125 | final_report[:source_fqdn] = @source_fqdn 126 | final_report[:organization_name] = @organization_name 127 | final_report[:policy_group] = @policy_group 128 | final_report[:policy_name] = @policy_name 129 | final_report[:chef_tags] = @chef_tags 130 | final_report[:ipaddress] = @ipaddress 131 | final_report[:fqdn] = @fqdn 132 | 133 | final_report 134 | end 135 | end 136 | end 137 | -------------------------------------------------------------------------------- /libraries/reporters/cs_automate.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | require_relative '../helper' 3 | require_relative './automate' 4 | 5 | module Reporter 6 | # 7 | # Used to send inspec reports to Chef Automate server via Chef Server 8 | # 9 | class ChefServerAutomate < ChefAutomate 10 | def initialize(opts) 11 | @entity_uuid = opts[:entity_uuid] 12 | @run_id = opts[:run_id] 13 | @node_name = opts[:node_info][:node] 14 | @insecure = opts[:insecure] 15 | @environment = opts[:node_info][:environment] 16 | @roles = opts[:node_info][:roles] 17 | @recipes = opts[:node_info][:recipes] 18 | @url = opts[:url] 19 | @chef_tags = opts[:node_info][:chef_tags] 20 | @policy_group = opts[:node_info][:policy_group] 21 | @policy_name = opts[:node_info][:policy_name] 22 | @source_fqdn = opts[:node_info][:source_fqdn] 23 | @organization_name = opts[:node_info][:organization_name] 24 | @ipaddress = opts[:node_info][:ipaddress] 25 | @fqdn = opts[:node_info][:fqdn] 26 | @control_results_limit = opts[:control_results_limit] 27 | end 28 | 29 | def send_report(report) 30 | unless @entity_uuid && @run_id 31 | Chef::Log.error "entity_uuid(#{@entity_uuid}) or run_id(#{@run_id}) can't be nil, not sending report to Chef Automate" 32 | return false 33 | end 34 | 35 | automate_report = truncate_controls_results(enriched_report(report), @control_results_limit) 36 | 37 | report_size = automate_report.to_json.bytesize 38 | # this is set to slightly less than the oc_erchef limit 39 | if report_size > 900 * 1024 40 | Chef::Log.warn "Compliance report size is #{(report_size / (1024 * 1024.0)).round(2)} MB." 41 | Chef::Log.warn 'Infra Server < 13.0 defaults to a limit of ~1MB, 13.0+ defaults to a limit of ~2MB.' 42 | end 43 | 44 | if @insecure 45 | Chef::Config[:verify_api_cert] = false 46 | Chef::Config[:ssl_verify_mode] = :verify_none 47 | end 48 | 49 | Chef::Log.info "Report to Chef Automate via Chef Server: #{@url}" 50 | rest = Chef::ServerAPI.new(@url, Chef::Config) 51 | with_http_rescue do 52 | rest.post(@url, automate_report) 53 | return true 54 | end 55 | false 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /libraries/reporters/json_file.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | 3 | module Reporter 4 | # 5 | # Used to write report to file on disk 6 | # 7 | class JsonFile 8 | def initialize(opts) 9 | @opts = opts 10 | end 11 | 12 | def send_report(report) 13 | write_to_file(report, @opts[:file]) 14 | end 15 | 16 | def write_to_file(report, path) 17 | json_file = File.new(path, 'w') 18 | json_file.puts(JSON.generate(report)) 19 | json_file.close 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name 'audit' 2 | maintainer 'Chef Software, Inc.' 3 | maintainer_email 'cookbooks@chef.io' 4 | license 'Apache-2.0' 5 | description 'Allows for fetching and executing compliance profiles, and reporting their results' 6 | version '9.5.0' 7 | 8 | source_url 'https://github.com/chef-cookbooks/audit' 9 | issues_url 'https://github.com/chef-cookbooks/audit/issues' 10 | 11 | chef_version '>= 12.20' 12 | 13 | supports 'aix' 14 | supports 'amazon' 15 | supports 'centos' 16 | supports 'debian' 17 | supports 'fedora' 18 | supports 'oracle' 19 | supports 'redhat' 20 | supports 'suse' 21 | supports 'opensuse' 22 | supports 'opensuseleap' 23 | supports 'ubuntu' 24 | supports 'windows' 25 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Recipe:: default 4 | # 5 | # Copyright:: 2016-2019 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | # The "audit cookbook" and Chef's own "Audit Mode" are not compatible 20 | # due to global state management done by RSpec which is used by both 21 | # implementations. To prevent unexpected results, the audit cookbook 22 | # will prevent Chef from continuing if Audit Mode is not disabled. 23 | unless Chef::Config[:audit_mode] == :disabled || Gem::Requirement.new('>= 15').satisfied_by?(Gem::Version.new(Chef::VERSION)) 24 | raise 'Audit Mode is enabled. The audit cookbook and Audit Mode' \ 25 | ' cannot be used at the same time. Please disable Audit Mode' \ 26 | ' in your client configuration.' 27 | end 28 | 29 | include_recipe 'audit::inspec' 30 | 31 | # Call helper methods located in libraries/helper.rb 32 | copy_audit_attributes 33 | load_audit_handler 34 | -------------------------------------------------------------------------------- /recipes/inspec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Recipe:: inspec 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | inspec_gem 'inspec' do 20 | version node['audit']['inspec_version'] 21 | source node['audit']['inspec_gem_source'] 22 | action :nothing 23 | end.run_action(:install) 24 | -------------------------------------------------------------------------------- /resources/inspec_gem.rb: -------------------------------------------------------------------------------- 1 | provides :inspec_gem 2 | unified_mode true 3 | resource_name :inspec_gem 4 | 5 | property :gem_name, String, name_property: true 6 | property :version, String 7 | property :source, String 8 | 9 | default_action :install 10 | 11 | action :install do 12 | # detect if installation is required 13 | compatible_version = compatible_with_client?(new_resource.version) 14 | installation_required = inspec_info.nil? || !new_resource.version.nil? 15 | 16 | # detect if the same version is already installed 17 | unless inspec_info.nil? 18 | installed_version = inspec_info.version.to_s 19 | Chef::Log.debug("Installed Chef-InSpec version: #{installed_version}") 20 | if new_resource.version == installed_version 21 | installation_required = false 22 | Chef::Log.info("inspec_gem: not installing Chef-InSpec. Requested version #{new_resource.version} already installed") 23 | return 24 | end 25 | end 26 | 27 | if installation_required && compatible_version 28 | Chef::Log.info("Installation of Chef-InSpec required: #{installation_required}") 29 | 30 | unless inspec_info.nil? 31 | converge_by 'uninstall all inspec and train gem versions' do 32 | uninstall_inspec_gem 33 | end 34 | end 35 | 36 | converge_by 'install given Chef-InSpec version' do 37 | install_inspec_gem(version: new_resource.version, source: new_resource.source) 38 | end 39 | elsif new_resource.version.nil? 40 | Chef::Log.info('inspec_gem: not installing Chef-InSpec. No Chef-Inspec version specified') 41 | elsif !compatible_version 42 | Chef::Log.warn("inspec_gem: not installing Chef-InSpec. Requested version #{new_resource.version} is not compatible with chef-client #{Chef::VERSION}") 43 | end 44 | end 45 | 46 | action_class do 47 | def install_inspec_gem(options) 48 | gem_source = options[:source] 49 | gem_version = options[:version] 50 | 51 | gem_version = nil if gem_version == 'latest' 52 | 53 | # use inspec-core for recent inspec versions 54 | gem_name = use_inspec_core?(gem_version) ? 'inspec-core' : 'inspec' 55 | 56 | chef_gem gem_name do 57 | version gem_version unless gem_version.nil? 58 | unless gem_source.nil? 59 | clear_sources true 60 | include_default_source false if respond_to?(:include_default_source) 61 | source gem_source 62 | end 63 | compile_time false 64 | action :install 65 | end 66 | 67 | chef_gem 'inspec-core-bin' do 68 | version gem_version unless gem_version.nil? 69 | unless gem_source.nil? 70 | clear_sources true 71 | include_default_source false if respond_to?(:include_default_source) 72 | source gem_source 73 | end 74 | compile_time false 75 | action :install 76 | only_if { need_inspec_core_bin?(gem_version) } 77 | end 78 | end 79 | 80 | def compatible_with_client?(gem_version) 81 | # No version specified so they will get the latest 82 | return true if gem_version.nil? 83 | 84 | if chef_gte_15? 85 | # Chef-15 can only run with the version of inspec-core and train-core that's being bundled with 86 | # It's pinned here: grep "inspec-" /opt/chef/bin/chef-client 87 | Chef::Log.warn('inspec_gem: Chef Infra Client >= 15 detected, can only use the embedded InSpec gem!!!') 88 | false 89 | else 90 | # min version required to run the audit handler 91 | Gem::Requirement.new(['>= 1.25.1']).satisfied_by?(Gem::Version.new(gem_version)) 92 | end 93 | end 94 | 95 | def chef_gte_15? 96 | Gem::Requirement.new('>= 15').satisfied_by?(Gem::Version.new(Chef::VERSION)) 97 | end 98 | 99 | def use_inspec_core?(gem_version) 100 | return true if gem_version.nil? # latest version 101 | Gem::Requirement.new('>= 2.1.67').satisfied_by?(Gem::Version.new(gem_version)) 102 | end 103 | 104 | # Inspec 4+ does not include inspec binaries and requires an additional gem 105 | def need_inspec_core_bin?(gem_version) 106 | return true if gem_version.nil? # latest version 107 | Gem::Requirement.new('>= 4').satisfied_by?(Gem::Version.new(gem_version)) 108 | end 109 | 110 | def uninstall_inspec_gem 111 | chef_gem 'remove all inspec versions' do 112 | package_name 'inspec' 113 | action :remove 114 | end 115 | 116 | chef_gem 'remove all inspec-core versions' do 117 | package_name 'inspec-core' 118 | action :remove 119 | end 120 | 121 | chef_gem 'remove all inspec-core-bin versions' do 122 | package_name 'inspec-core-bin' 123 | action :remove 124 | end 125 | 126 | chef_gem 'remove all train versions' do 127 | package_name 'train' 128 | action :remove 129 | end 130 | 131 | chef_gem 'remove all train-core versions' do 132 | package_name 'train-core' 133 | action :remove 134 | end 135 | end 136 | 137 | def inspec_info 138 | require 'rubygems' 139 | Gem::Specification.find { |s| %w(inspec inspec-core).include?(s.name) } 140 | rescue LoadError 141 | nil 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /spec/chef-client.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpQIBAAKCAQEA6daLaGZK27W5f1Pe4UXZ5/a0/FMAxPDznOapVUpMFZS2rOyt 3 | 3Lc+/038ZzsDORLW6FUAefjAp1H3G0IxcjODAlLOaUVNuso9XLDRU45FFGW0h9oz 4 | Le7NIq53PQ/btDoFA74xeQhEHtN9cRc4zb7r40sBe49HScJ5VkMsncy738+tP89+ 5 | sWJfSW41vUCI+Xb28AqdANiIynsgDJyoLDXoUbYi/AV1JxYJECeTdo2XzqwvjZfQ 6 | pGYtF4o19ivnMEj6/EQXhF9BnXpFB7LosCAOTuscW5B5BTq6kdqdnC2Uh5yJSRuz 7 | 3yhOfTR929dmsMUokEhBcB30gROCT70jNloUSQIDAQABAoIBAGxi1oFQkLggFlgP 8 | XwqZ3vPm5WLjckLW0IRUYf63jmaeZMHofnoEsf2Sf0C2GLtWoShVZgAjLeEgW+JV 9 | nyeo+ruT+DrRNcMzxJd3Gb+Z/SkEL1ac7AYJXyoJJhm2hQaXsgVXHgVUsIZ9TvKh 10 | aeHr8diLxqcn9UoaCzXRsxd9c0O8U2cpoO9Sz37PpCvL3v+wT838Ulrpir8kTT2v 11 | 4HtpcQw3apFF1MC2IAIkLOnpjh5XT84KmgyYiTD3omtXMlwJuEzMDJMLTRQ0r0qr 12 | 3/aljmgOYFf8v7MGU8hsaBLIpcwpEbF3iYIx/kPpySpj7CeQB7yId/ayypFO8cH4 13 | h/wBlSUCgYEA9b1RZlZJgH4vFaNnHREq3ZFGpYYXK83/IIHsArmOy4hD4W74Dl8x 14 | PVQjcPWaBNXXcIWTBoAF9hkSAXvCOPA6CK6w7UZesMvUPHYQYJH9BcSE4PZ0YzHN 15 | 5sB7enzOw32qG1dxsB2ovlsJ5e/kvcDhOHKETwrrdODNr3VOu326arMCgYEA85oD 16 | ZBXKyIPW/wlW8P0X8i4c5gQSnWzQKwtTRwH2O9P33/BOp6Rl/8WFT1hnNOOZRcqW 17 | dyOAvPGaMq7vj7Ll9ggyhCDceFDuFXOQfd2WOBDvx/hG8P3Swj+vYQU48FTeMkpG 18 | zqPYq7ecCvaIMQ2FCL6xoT8F6CI+om4RpzvzsxMCgYEAtNNvr491HME9on2QJdp5 19 | IXuCcdC/AjPeNayE3+htRCXsVVmT3Pd9QzTDs552jHJSyvDvpIvWVyZRkpff7ogP 20 | HE530NHEYfJLJYZ3PKiQeIsIgIW6VTfT3KXs9tAaUc4Ju37YIJFil1hkazfgqSTi 21 | VegmpgdSBbpagG8g1WSKJXMCgYEAt5ge8CKgd6kts39lgDEwB/2LGCx/nxgweBCM 22 | DhtDamniCmwBy8VSfoduZpOZDTpv/TKnXllqoHxym7pOoP3S5S/easidgSx1k8NK 23 | ZiJIIi9ZmFvdk6mpW29GDZgzBqbf5AUpAnpoRVsXhwexM08eMa4PEBkAqaiNjjvo 24 | oCLGE/MCgYEArv+F/lnt9Tdd6UbTGWxkKjhlb+TqJ6wYSFoKyiLN4uvDO4d6gL+f 25 | B8hk1m6pQt/58jeTVgVFYTaBzO4ygkaJv66CpuTEZTb/LajxQE1PlE9tjQjRI+Lb 26 | 8am0IjK7GBgWTicDONCeBAKdsQ7RC2LFlRm/kUMc98DGMBjD1qy4HcY= 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /spec/data/mock.rb: -------------------------------------------------------------------------------- 1 | class MockData 2 | def self.node_info 3 | { node: 'chef-client.solo', 4 | environment: 'My Prod Env', 5 | roles: %w(base_linux apache_linux), 6 | recipes: ['some_cookbook::some_recipe', 'some_cookbook'], 7 | policy_name: 'test_policy_name', 8 | policy_group: 'test_policy_group', 9 | chef_tags: ['mylinux', 'my.tag', 'some=tag'], 10 | organization_name: 'test_org', 11 | source_fqdn: 'api.chef.io', 12 | ipaddress: '192.168.56.33', 13 | fqdn: 'lb1.prod.example.com', 14 | } 15 | end 16 | 17 | def self.inspec_results 18 | { "version": '1.2.1', 19 | "profiles": 20 | [{ "name": 'tmp_compliance_profile', 21 | "title": '/tmp Compliance Profile', 22 | "summary": 'An Example Compliance Profile', 23 | "sha256": '7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd', 24 | "version": '0.1.1', 25 | "maintainer": 'Nathen Harvey ', 26 | "license": 'Apache 2.0 License', 27 | "copyright": 'Nathen Harvey ', 28 | "supports": [], 29 | "controls": 30 | [{ "title": 'A /tmp directory must exist', 31 | "desc": 'A /tmp directory must exist', 32 | "impact": 0.3, 33 | "refs": [], 34 | "tags": {}, 35 | "code": "control 'tmp-1.0' do\n impact 0.3\n title 'A /tmp directory must exist'\n desc 'A /tmp directory must exist'\n describe file '/tmp' do\n it { should be_directory }\n end\nend\n", 36 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 3 }, 37 | "id": 'tmp-1.0', 38 | "results": [ 39 | { "status": 'passed', "code_desc": 'File /tmp should be directory', "run_time": 0.002312, "start_time": '2016-10-19 11:09:43 -0400' }, 40 | ], 41 | }, 42 | { "title": '/tmp directory is owned by the root user', 43 | "desc": 'The /tmp directory must be owned by the root user', 44 | "impact": 0.3, 45 | "refs": [{ "url": 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf', "ref": 'Compliance Whitepaper' }], 46 | "tags": { "production": nil, "development": nil, "identifier": 'value', "remediation": 'https://github.com/chef-cookbooks/audit' }, 47 | "code": "control 'tmp-1.1' do\n impact 0.3\n title '/tmp directory is owned by the root user'\n desc 'The /tmp directory must be owned by the root user'\n tag 'production','development'\n tag identifier: 'value'\n tag remediation: 'https://github.com/chef-cookbooks/audit'\n ref 'Compliance Whitepaper', url: 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf'\n describe file '/tmp' do\n it { should be_owned_by 'root' }\n end\nend\n", 48 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 12 }, 49 | "id": 'tmp-1.1', 50 | "results": [ 51 | { "status": 'passed', "code_desc": 'File /tmp should be owned by "root"', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400' }, 52 | { "status": 'skipped', "code_desc": 'File /tmp should be owned by "root"', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400' }, 53 | { "status": 'failed', "code_desc": 'File /etc/hosts is expected to be directory', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400', "message": 'expected `File /etc/hosts.directory?` to return true, got false' }, 54 | ], 55 | }, 56 | ], 57 | "groups": [{ "title": '/tmp Compliance Profile', "controls": ['tmp-1.0', 'tmp-1.1'], "id": 'controls/tmp.rb' }], 58 | "attributes": [{ "name": 'syslog_pkg', "options": { "default": 'rsyslog', "description": 'syslog package...' } }] }], 59 | "other_checks": [], 60 | "statistics": { "duration": 0.032332 } } 61 | end 62 | 63 | def self.inspec_results2 64 | { "version": '1.2.1', 65 | "profiles": 66 | [{ "name": 'tmp_compliance_profile', 67 | "title": '/tmp Compliance Profile', 68 | "summary": 'An Example Compliance Profile', 69 | "sha256": '7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215ff', 70 | "version": '0.1.1', 71 | "maintainer": 'Nathen Harvey ', 72 | "license": 'Apache 2.0 License', 73 | "copyright": 'Nathen Harvey ', 74 | "supports": [], 75 | "controls": 76 | [{ "id": 'tmp-2.0', 77 | "title": 'A bunch of directories must exist', 78 | "desc": 'A bunch of directories must exist for testing', 79 | "impact": 0.3, 80 | "refs": [], 81 | "tags": {}, 82 | "code": "control 'tmp-2.0' do\n impact 0.3\n title 'A bunch of directories must exist'\n desc 'A bunch of directories must exist for testing'\n describe file '/tmp' do\n it { should be_directory }\n end\nend\n", 83 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 3 }, 84 | "results": [ 85 | { "status": 'passed', "code_desc": 'File /tmp should be directory', "run_time": 0.002312, "start_time": '2016-10-19 11:09:43 -0400' }, 86 | { "status": 'passed', "code_desc": 'File /etc should be directory', "run_time": 0.002314, "start_time": '2016-10-19 11:09:45 -0400' }, 87 | { "status": 'passed', "code_desc": 'File /opt should be directory', "run_time": 0.002315, "start_time": '2016-10-19 11:09:46 -0400' }, 88 | { "status": 'skipped', "code_desc": 'No-op', "run_time": 0.002316, "start_time": '2016-10-19 11:09:44 -0400', "skip_message": '4 testing' }, 89 | { "status": 'skipped', "code_desc": 'No-op', "run_time": 0.002317, "start_time": '2016-10-19 11:09:44 -0400', "skip_message": '4 testing' }, 90 | { "status": 'skipped', "code_desc": 'No-op', "run_time": 0.002318, "start_time": '2016-10-19 11:09:44 -0400', "skip_message": '4 testing' }, 91 | { "status": 'failed', "code_desc": 'File /etc/passwd should be directory', "run_time": 0.002313, "start_time": '2016-10-19 11:09:44 -0400' }, 92 | { "status": 'failed', "code_desc": 'File /etc/passwd should be directory', "run_time": 0.002313, "start_time": '2016-10-19 11:09:44 -0400' }, 93 | { "status": 'failed', "code_desc": 'File /etc/passwd should be directory', "run_time": 0.002313, "start_time": '2016-10-19 11:09:44 -0400' }, 94 | ], 95 | }, 96 | { "id": 'tmp-2.1', 97 | "title": '/tmp directory is owned by the root user', 98 | "desc": 'The /tmp directory must be owned by the root user', 99 | "impact": 0.3, 100 | "refs": [{ "url": 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf', "ref": 'Compliance Whitepaper' }], 101 | "tags": { "production": nil, "development": nil, "identifier": 'value', "remediation": 'https://github.com/chef-cookbooks/audit' }, 102 | "code": "control 'tmp-2.1' do\n impact 0.3\n title '/tmp directory is owned by the root user'\n desc 'The /tmp directory must be owned by the root user'\n tag 'production','development'\n tag identifier: 'value'\n tag remediation: 'https://github.com/chef-cookbooks/audit'\n ref 'Compliance Whitepaper', url: 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf'\n describe file '/tmp' do\n it { should be_owned_by 'root' }\n end\nend\n", 103 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 12 }, 104 | "results": [ 105 | { "status": 'passed', "code_desc": 'File /tmp should be owned by "root"', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400' }, 106 | { "status": 'passed', "code_desc": 'File /etc should be owned by "root"', "run_time": 1.238845, "start_time": '2016-10-19 11:09:43 -0400' }, 107 | ], 108 | }, 109 | ], 110 | "groups": [{ "title": '/tmp Compliance Profile', "controls": ['tmp-1.0', 'tmp-1.1'], "id": 'controls/tmp.rb' }], 111 | "attributes": [{ "name": 'syslog_pkg', "options": { "default": 'rsyslog', "description": 'syslog package...' } }] }], 112 | "other_checks": [], 113 | "statistics": { "duration": 0.032332 } } 114 | end 115 | end 116 | -------------------------------------------------------------------------------- /spec/data/mock_profile.rb: -------------------------------------------------------------------------------- 1 | # copyright: 2015, Chef Software, Inc. 2 | # license: All rights reserved 3 | 4 | title '/tmp profile' 5 | 6 | # you add controls here 7 | control 'tmp-1.0' do # A unique ID for this control 8 | impact 0.7 # The criticality, if this control fails. 9 | title 'Create /tmp directory' # A human-readable title 10 | desc 'An optional description...' # Describe why this is needed 11 | tag data: 'temp data' # A tag allows you to associate key information 12 | tag 'security' # to the test 13 | ref 'Document A-12', url: 'http://...' # Additional references 14 | 15 | describe file('/tmp') do # The actual test 16 | it { should be_directory } 17 | end 18 | end 19 | 20 | # you can also use plain tests 21 | describe file('/tmp') do 22 | it { should be_directory } 23 | end 24 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'chefspec' 2 | require 'chefspec/berkshelf' 3 | require 'webmock/rspec' 4 | 5 | # Berkshelf needs to connect to internet 6 | WebMock.disable_net_connect!( 7 | allow: [/supermarket.chef.io/, /127.0.0.1:8889/, %r{s3.amazonaws.com/community-files.opscode.com}] 8 | ) 9 | 10 | RSpec.configure do |config| 11 | config.file_cache_path = Chef::Config[:file_cache_path] 12 | end 13 | -------------------------------------------------------------------------------- /spec/unit/libraries/audit_enforcer_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: automate_spec 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require 'spec_helper' 20 | require_relative '../../../libraries/reporters/audit-enforcer' 21 | 22 | describe 'Reporter::ChefAutomate methods' do 23 | before :each do 24 | @min_report = { 25 | "profiles": [ 26 | { 27 | "controls": [ 28 | { "id": 'c1', "results": [{ "status": 'passed' }] }, 29 | { "id": 'c2', "results": [{ "status": 'passed' }] }, 30 | ], 31 | }, 32 | ], 33 | } 34 | @automate = Reporter::AuditEnforcer.new 35 | end 36 | 37 | it 'is not raising error for a successful InSpec report' do 38 | expect(@automate.send_report(@min_report)).to eq(true) 39 | end 40 | 41 | it 'is not raising error for an InSpec report with no controls' do 42 | expect(@automate.send_report("profiles": [{ "name": 'empty' }])).to eq(true) 43 | end 44 | 45 | it 'is not raising error for an InSpec report with controls but no results' do 46 | expect(@automate.send_report("profiles": [{ "controls": [{ "id": 'empty' }] }])).to eq(true) 47 | end 48 | 49 | it 'raises an error for a failed InSpec report' do 50 | @min_report[:profiles][0][:controls][1][:results][0][:status] = 'failed' 51 | expect { @automate.send_report(@min_report) }.to raise_error(Reporter::AuditEnforcer::ControlFailure, 'Audit c2 has failed. Aborting chef-client run.') 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /spec/unit/libraries/automate_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: automate_spec 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require 'spec_helper' 20 | require 'time' 21 | require_relative '../../../libraries/reporters/automate' 22 | require_relative '../../data/mock' 23 | 24 | describe 'Reporter::ChefAutomate methods' do 25 | before :each do 26 | entity_uuid = 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz' 27 | run_id = '3f0536f7-3361-4bca-ae53-b45118dceb5d' 28 | insecure = false 29 | @enriched_report_expected = { 30 | "version": '1.2.1', 31 | "profiles": [ 32 | { 33 | "name": 'tmp_compliance_profile', 34 | "title": '/tmp Compliance Profile', 35 | "summary": 'An Example Compliance Profile', 36 | "sha256": '7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd', 37 | "version": '0.1.1', 38 | "maintainer": 'Nathen Harvey ', 39 | "license": 'Apache 2.0 License', 40 | "copyright": 'Nathen Harvey ', 41 | "supports": [], 42 | "controls": [ 43 | { 44 | "title": 'A /tmp directory must exist', 45 | "desc": 'A /tmp directory must exist', 46 | "impact": 0.3, 47 | "refs": [], 48 | "tags": {}, 49 | "code": "control 'tmp-1.0' do\n impact 0.3\n title 'A /tmp directory must exist'\n desc 'A /tmp directory must exist'\n describe file '/tmp' do\n it { should be_directory }\n end\nend\n", 50 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 3 }, 51 | "id": 'tmp-1.0', 52 | "results": [ 53 | { "status": 'passed', "code_desc": 'File /tmp should be directory', "run_time": 0.002312, "start_time": '2016-10-19 11:09:43 -0400' }, 54 | ], 55 | }, 56 | { 57 | "title": '/tmp directory is owned by the root user', 58 | "desc": 'The /tmp directory must be owned by the root user', 59 | "impact": 0.3, 60 | "refs": [ 61 | { "url": 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf', "ref": 'Compliance Whitepaper' }, 62 | ], 63 | "tags": { "production": nil, "development": nil, "identifier": 'value', "remediation": 'https://github.com/chef-cookbooks/audit' }, 64 | "code": "control 'tmp-1.1' do\n impact 0.3\n title '/tmp directory is owned by the root user'\n desc 'The /tmp directory must be owned by the root user'\n tag 'production','development'\n tag identifier: 'value'\n tag remediation: 'https://github.com/chef-cookbooks/audit'\n ref 'Compliance Whitepaper', url: 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf'\n describe file '/tmp' do\n it { should be_owned_by 'root' }\n end\nend\n", 65 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 12 }, 66 | "id": 'tmp-1.1', 67 | "results": [ 68 | { "status": 'failed', "code_desc": 'File /etc/hosts is expected to be directory', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400', "message": 'expected `File /etc/hosts.directory?` to return true, got false' }, 69 | { "status": 'skipped', "code_desc": 'File /tmp should be owned by "root"', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400' }, 70 | ], 71 | "removed_results_counts": { "failed": 0, "skipped": 0, "passed": 1 }, 72 | }, 73 | ], 74 | "groups": [ 75 | { "title": '/tmp Compliance Profile', "controls": ['tmp-1.0', 'tmp-1.1'], "id": 'controls/tmp.rb' }, 76 | ], 77 | "attributes": [ 78 | { "name": 'syslog_pkg', "options": { "default": 'rsyslog', "description": 'syslog package...' } }, 79 | ], 80 | }, 81 | ], 82 | "other_checks": [], 83 | "statistics": { "duration": 0.032332 }, 84 | "type": 'inspec_report', 85 | "node_name": 'chef-client.solo', 86 | "end_time": '2016-07-19T18:19:19Z', 87 | "node_uuid": 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz', 88 | "environment": 'My Prod Env', 89 | "roles": %w(base_linux apache_linux), 90 | "recipes": ['some_cookbook::some_recipe', 'some_cookbook'], 91 | "report_uuid": '3f0536f7-3361-4bca-ae53-b45118dceb5d', 92 | "source_fqdn": 'api.chef.io', 93 | "organization_name": 'test_org', 94 | "policy_group": 'test_policy_group', 95 | "policy_name": 'test_policy_name', 96 | "chef_tags": ['mylinux', 'my.tag', 'some=tag'], 97 | "ipaddress": '192.168.56.33', 98 | "fqdn": 'lb1.prod.example.com', 99 | } 100 | 101 | @opts = { 102 | entity_uuid: entity_uuid, 103 | run_id: run_id, 104 | node_info: MockData.node_info, 105 | insecure: insecure, 106 | run_time_limit: 1.0, 107 | control_results_limit: 2, 108 | } 109 | # set data_collector 110 | Chef::Config[:data_collector] = { token: 'dctoken', server_url: 'https://automate.test/data_collector' } 111 | 112 | stub_request(:post, 'https://automate.test/compliance/profiles/metasearch') 113 | .with(body: '{"sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}', 114 | headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'identity', 'Content-Length' => /.+/, 'Content-Type' => 'application/json', 'Host' => 'automate.test', 'User-Agent' => /.+/, 'X-Chef-Version' => /.+/, 'X-Data-Collector-Auth' => 'version=1.0', 'X-Data-Collector-Token' => 'dctoken' }) 115 | .to_return(status: 200, body: '{"missing_sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}', headers: {}) 116 | 117 | stub_request(:post, 'https://automate.test/data_collector') 118 | .with(body: @enriched_report_expected.to_json, 119 | headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'identity', 'Content-Length' => /.+/, 'Content-Type' => 'application/json', 'Host' => 'automate.test', 'User-Agent' => /.+/, 'X-Chef-Version' => /.+/, 'X-Data-Collector-Auth' => 'version=1.0', 'X-Data-Collector-Token' => 'dctoken' }) 120 | .to_return(status: 200, body: '', headers: {}) 121 | 122 | @automate = Reporter::ChefAutomate.new(@opts) 123 | end 124 | 125 | it 'sends report successfully to ChefAutomate' do 126 | allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00')) 127 | expect(@automate.send_report(MockData.inspec_results)).to eq(true) 128 | end 129 | 130 | it 'enriches report correctly with the most test coverage' do 131 | allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00')) 132 | expect(@automate.truncate_controls_results(@automate.enriched_report(MockData.inspec_results), @opts[:control_results_limit])).to eq(@enriched_report_expected) 133 | end 134 | 135 | it 'is not sending report when entity_uuid is missing' do 136 | opts2 = { 137 | entity_uuid: nil, 138 | run_id: '3f0536f7-3361-4bca-ae53-b45118dceb5d', 139 | node_info: MockData.node_info, 140 | insecure: false, 141 | } 142 | viz2 = Reporter::ChefAutomate.new(opts2) 143 | expect(viz2.send_report(MockData.inspec_results)).to eq(false) 144 | end 145 | end 146 | -------------------------------------------------------------------------------- /spec/unit/libraries/cs_automate_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: cs_automate_spec 4 | 5 | require 'spec_helper' 6 | require 'time' 7 | require_relative '../../../libraries/reporters/cs_automate' 8 | require_relative '../../data/mock' 9 | 10 | describe 'Reporter::ChefServerAutomate methods' do 11 | before :each do 12 | entity_uuid = 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz' 13 | run_id = '3f0536f7-3361-4bca-ae53-b45118dceb5d' 14 | insecure = false 15 | @enriched_report_expected = { 16 | "version": '1.2.1', 17 | "profiles": [ 18 | { 19 | "name": 'tmp_compliance_profile', 20 | "title": '/tmp Compliance Profile', 21 | "summary": 'An Example Compliance Profile', 22 | "sha256": '7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd', 23 | "version": '0.1.1', 24 | "maintainer": 'Nathen Harvey ', 25 | "license": 'Apache 2.0 License', 26 | "copyright": 'Nathen Harvey ', 27 | "supports": [], 28 | "controls": [ 29 | { 30 | "title": 'A /tmp directory must exist', 31 | "desc": 'A /tmp directory must exist', 32 | "impact": 0.3, 33 | "refs": [], 34 | "tags": {}, 35 | "code": 36 | "control 'tmp-1.0' do\n impact 0.3\n title 'A /tmp directory must exist'\n desc 'A /tmp directory must exist'\n describe file '/tmp' do\n it { should be_directory }\n end\nend\n", 37 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 3 }, 38 | "id": 'tmp-1.0', 39 | "results": [{ "status": 'passed', "code_desc": 'File /tmp should be directory', "run_time": 0.002312, "start_time": '2016-10-19 11:09:43 -0400' }], 40 | }, 41 | { 42 | "title": '/tmp directory is owned by the root user', 43 | "desc": 'The /tmp directory must be owned by the root user', 44 | "impact": 0.3, 45 | "refs": [{ "url": 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf', "ref": 'Compliance Whitepaper' }], 46 | "tags": { "production": nil, "development": nil, "identifier": 'value', "remediation": 'https://github.com/chef-cookbooks/audit' }, 47 | "code": "control 'tmp-1.1' do\n impact 0.3\n title '/tmp directory is owned by the root user'\n desc 'The /tmp directory must be owned by the root user'\n tag 'production','development'\n tag identifier: 'value'\n tag remediation: 'https://github.com/chef-cookbooks/audit'\n ref 'Compliance Whitepaper', url: 'https://pages.chef.io/rs/255-VFB-268/images/compliance-at-velocity2015.pdf'\n describe file '/tmp' do\n it { should be_owned_by 'root' }\n end\nend\n", 48 | "source_location": { "ref": '/Users/vjeffrey/code/delivery/insights/data_generator/chef-client/cache/cookbooks/test-cookbook/recipes/../files/default/compliance_profiles/tmp_compliance_profile/controls/tmp.rb', "line": 12 }, 49 | "id": 'tmp-1.1', 50 | "results": [ 51 | { "status": 'failed', "code_desc": 'File /etc/hosts is expected to be directory', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400', "message": 'expected `File /etc/hosts.directory?` to return true, got false' }, 52 | { "status": 'skipped', "code_desc": 'File /tmp should be owned by "root"', "run_time": 1.228845, "start_time": '2016-10-19 11:09:43 -0400' }, 53 | ], 54 | "removed_results_counts": { "failed": 0, "skipped": 0, "passed": 1 }, 55 | }, 56 | ], 57 | "groups": [{ "title": '/tmp Compliance Profile', "controls": ['tmp-1.0', 'tmp-1.1'], "id": 'controls/tmp.rb' }], 58 | "attributes": [{ "name": 'syslog_pkg', "options": { "default": 'rsyslog', "description": 'syslog package...' } }], 59 | }, 60 | ], 61 | "other_checks": [], 62 | "statistics": { "duration": 0.032332 }, 63 | "type": 'inspec_report', 64 | "node_name": 'chef-client.solo', 65 | "end_time": '2016-07-19T18:19:19Z', 66 | "node_uuid": 'aaaaaaaa-709a-475d-bef5-zzzzzzzzzzzz', 67 | "environment": 'My Prod Env', 68 | "roles": %w(base_linux apache_linux), 69 | "recipes": ['some_cookbook::some_recipe', 'some_cookbook'], 70 | "report_uuid": '3f0536f7-3361-4bca-ae53-b45118dceb5d', 71 | "source_fqdn": 'api.chef.io', 72 | "organization_name": 'test_org', 73 | "policy_group": 'test_policy_group', 74 | "policy_name": 'test_policy_name', 75 | "chef_tags": ['mylinux', 'my.tag', 'some=tag'], 76 | "ipaddress": '192.168.56.33', 77 | "fqdn": 'lb1.prod.example.com', 78 | } 79 | 80 | @opts = { 81 | entity_uuid: entity_uuid, 82 | run_id: run_id, 83 | node_info: MockData.node_info, 84 | insecure: insecure, 85 | url: 'https://chef.server/data_collector', 86 | control_results_limit: 2, 87 | } 88 | 89 | Chef::Config[:client_key] = File.expand_path('../../chef-client.pem', File.dirname(__FILE__)) 90 | Chef::Config[:node_name] = 'spec-node' 91 | 92 | # set data_collector 93 | stub_request(:post, 'https://chef.server/data_collector') 94 | .with(body: @enriched_report_expected.to_json, 95 | headers: { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length' => /.+/, 'Content-Type' => 'application/json', 'Host' => /.+/, 'User-Agent' => /.+/, 'X-Chef-Version' => /.+/, 'X-Ops-Authorization-1' => /.+/, 'X-Ops-Authorization-2' => /.+/, 'X-Ops-Authorization-3' => /.+/, 'X-Ops-Authorization-4' => /.+/, 'X-Ops-Authorization-5' => /.+/, 'X-Ops-Authorization-6' => /.+/, 'X-Ops-Content-Hash' => /.+/, 'X-Ops-Server-Api-Version' => '1', 'X-Ops-Sign' => 'algorithm=sha1;version=1.1;', 'X-Ops-Timestamp' => /.+/, 'X-Ops-Userid' => 'spec-node', 'X-Remote-Request-Id' => /.+/ }) 96 | .to_return(status: 200, body: '', headers: {}) 97 | 98 | @automate = Reporter::ChefServerAutomate.new(@opts) 99 | end 100 | 101 | it 'sends report successfully to ChefServerAutomate' do 102 | allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00')) 103 | expect(@automate.send_report(MockData.inspec_results)).to eq(true) 104 | end 105 | 106 | it 'enriches report correctly with the most test coverage' do 107 | allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00')) 108 | expect(@automate.truncate_controls_results(@automate.enriched_report(MockData.inspec_results), @opts[:control_results_limit])).to eq(@enriched_report_expected) 109 | end 110 | end 111 | -------------------------------------------------------------------------------- /spec/unit/libraries/helpers_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: helpers 4 | 5 | require 'spec_helper' 6 | require_relative '../../../libraries/helper' 7 | require_relative '../../../files/default/handler/audit_report' 8 | require_relative '../../data/mock' 9 | 10 | describe ReportHelpers do 11 | let(:helpers) { Class.new { extend ReportHelpers } } 12 | 13 | it 'tests_for_runner converts all key strings to symbols' do 14 | tests = [{ 'name': 'ssh', 'url': 'https://github.com/dev-sec/tests-ssh-hardening' }] 15 | symbol_tests = @helpers.tests_for_runner(tests) 16 | expect(symbol_tests).to eq([{ name: 'ssh', url: 'https://github.com/dev-sec/tests-ssh-hardening' }]) 17 | end 18 | 19 | it 'report_timing_file returns where the report timing file is located' do 20 | expect(@helpers.report_timing_file).to eq("#{Chef::Config[:file_cache_path]}/compliance/report_timing.json") 21 | end 22 | 23 | it 'handle_reporters returns array of reporters when given array' do 24 | reporters = %w(chef-compliance json-file) 25 | expect(@helpers.handle_reporters(reporters)).to eq(%w(chef-compliance json-file)) 26 | end 27 | 28 | it 'handle_reporters returns array of reporters when given string' do 29 | reporters = 'chef-compliance' 30 | expect(@helpers.handle_reporters(reporters)).to eq(['chef-compliance']) 31 | end 32 | 33 | it 'create_timestamp_file creates a new file' do 34 | expected_file_path = @helpers.report_timing_file 35 | @helpers.create_timestamp_file 36 | expect(File).to exist(expected_file_path.to_s) 37 | File.delete(expected_file_path.to_s) 38 | end 39 | 40 | it 'report_profile_sha256s returns array of profile ids found in the report' do 41 | expect(@helpers.report_profile_sha256s(MockData.inspec_results)).to eq(['7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd']) 42 | end 43 | 44 | it 'strip_profiles_meta removes the metadata from the profiles' do 45 | expected_stripped_report = { 46 | other_checks: [], 47 | profiles: [ 48 | { 49 | attributes: [ 50 | { 51 | name: 'syslog_pkg', 52 | options: { 53 | default: 'rsyslog', 54 | description: 'syslog package...', 55 | }, 56 | }, 57 | ], 58 | controls: [ 59 | { 60 | id: 'tmp-1.0', 61 | results: [ 62 | { 63 | code_desc: 'File /tmp should be directory', 64 | status: 'passed', 65 | }, 66 | ], 67 | }, 68 | { 69 | id: 'tmp-1.1', 70 | results: [ 71 | { 72 | code_desc: 'File /tmp should be owned by "root"', 73 | run_time: 1.228845, 74 | start_time: '2016-10-19 11:09:43 -0400', 75 | status: 'passed', 76 | }, 77 | { 78 | code_desc: 'File /tmp should be owned by "root"', 79 | run_time: 1.228845, 80 | start_time: '2016-10-19 11:09:43 -0400', 81 | status: 'skipped', 82 | }, 83 | { 84 | code_desc: 'File /etc/hosts is expected to be directory', 85 | message: 'expected `File /etc/hosts.directory?` to return true, got false', 86 | run_time: 1.228845, 87 | start_time: '2016-10-19 11:09:43 -0400', 88 | status: 'failed', 89 | }, 90 | ], 91 | }, 92 | ], 93 | sha256: '7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd', 94 | title: '/tmp Compliance Profile', 95 | version: '0.1.1', 96 | }, 97 | ], 98 | run_time_limit: 1.1, 99 | statistics: { 100 | duration: 0.032332, 101 | }, 102 | version: '1.2.1', 103 | } 104 | expect(@helpers.strip_profiles_meta(MockData.inspec_results, [], 1.1)).to eq(expected_stripped_report) 105 | end 106 | 107 | it 'strip_profiles_meta is not removing the metadata from the missing profiles' do 108 | expected_stripped_report = MockData.inspec_results 109 | expected_stripped_report[:run_time_limit] = 1.1 110 | expect(@helpers.strip_profiles_meta(MockData.inspec_results, ['7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd'], 1.1)).to eq(expected_stripped_report) 111 | end 112 | 113 | it 'truncate_controls_results truncates controls results' do 114 | truncated_report = @helpers.truncate_controls_results(MockData.inspec_results2, 5) 115 | expect(truncated_report[:profiles][0][:controls][0][:results].length).to eq(5) 116 | statuses = truncated_report[:profiles][0][:controls][0][:results].map { |r| r[:status] } 117 | expect(statuses).to eq(%w(failed failed failed skipped skipped)) 118 | expect(truncated_report[:profiles][0][:controls][0][:removed_results_counts]).to eq(failed: 0, skipped: 1, passed: 3) 119 | 120 | expect(truncated_report[:profiles][0][:controls][1][:results].length).to eq(2) 121 | statuses = truncated_report[:profiles][0][:controls][1][:results].map { |r| r[:status] } 122 | expect(statuses).to eq(%w(passed passed)) 123 | expect(truncated_report[:profiles][0][:controls][1][:removed_results_counts]).to eq(nil) 124 | 125 | truncated_report = @helpers.truncate_controls_results(MockData.inspec_results2, 0) 126 | expect(truncated_report[:profiles][0][:controls][0][:results].length).to eq(9) 127 | 128 | truncated_report = @helpers.truncate_controls_results(MockData.inspec_results2, 1) 129 | expect(truncated_report[:profiles][0][:controls][0][:results].length).to eq(1) 130 | end 131 | end 132 | -------------------------------------------------------------------------------- /spec/unit/libraries/json_file_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: json-file 4 | 5 | require 'spec_helper' 6 | require_relative '../../../libraries/reporters/json_file' 7 | 8 | describe 'Reporter::JsonFile methods' do 9 | it 'writes the report to a file on disk' do 10 | report = { 'data' => 'some info' } 11 | timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S') 12 | expected_file_path = File.expand_path("inspec-#{timestamp}.json", File.dirname(__FILE__)) 13 | @jsonfile = Reporter::JsonFile.new(file: expected_file_path).send_report(report) 14 | expect(File).to exist(expected_file_path.to_s) 15 | # we sent a ruby hash and expect valid json in the file 16 | content = JSON.parse(File.read(expected_file_path)) 17 | expect(content).to eq(report) 18 | File.delete(expected_file_path.to_s) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/unit/recipes/default_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: compliance 3 | # Spec:: default 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require 'spec_helper' 20 | 21 | describe 'audit::default' do 22 | context 'When all attributes are default, on an unspecified platform' do 23 | let(:chef_run) do 24 | runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '18.04') 25 | runner.converge(described_recipe) 26 | end 27 | 28 | it 'installs the inspec gem' do 29 | expect(chef_run).to install_inspec_gem('inspec') 30 | end 31 | 32 | it 'converges successfully' do 33 | expect { chef_run }.to_not raise_error 34 | end 35 | end 36 | 37 | context 'When an inspec gem version is specified ' do 38 | let(:chef_run) do 39 | ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node| 40 | node.override['audit']['inspec_version'] = '0.0.0' 41 | end.converge(described_recipe) 42 | end 43 | 44 | it 'installs the inspec gem with the correct version' do 45 | expect(chef_run).to install_inspec_gem('inspec').with(version: '0.0.0') 46 | end 47 | 48 | it 'converges successfully' do 49 | expect { chef_run }.to_not raise_error 50 | end 51 | end 52 | 53 | context 'When an inspec gem alternate source is specified ' do 54 | let(:chef_run) do 55 | ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node| 56 | node.override['audit']['inspec_gem_source'] = 'http://0.0.0.0:8080' 57 | end.converge(described_recipe) 58 | end 59 | 60 | it 'installs the inspec gem from the alternate source' do 61 | expect(chef_run).to install_inspec_gem('inspec').with(source: 'http://0.0.0.0:8080') 62 | end 63 | 64 | it 'converges successfully' do 65 | expect { chef_run }.to_not raise_error 66 | end 67 | end 68 | 69 | context 'When server and refresh_token are specified' do 70 | let(:chef_run) do 71 | ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node| 72 | node.override['audit']['collector'] = 'chef-compliance' 73 | node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'admin/myprofile' } ] 74 | node.override['audit']['server'] = 'https://my.compliance.test/api' 75 | node.override['audit']['refresh_token'] = 'abcdefg' 76 | node.override['audit']['insecure'] = true 77 | end.converge(described_recipe) 78 | end 79 | 80 | it 'converges successfully' do 81 | expect { chef_run }.to_not raise_error 82 | end 83 | end 84 | 85 | context 'When two profiles are specified' do 86 | let(:chef_run) do 87 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 88 | runner.node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'admin/myprofile' }, { 'name': 'ssh', 'compliance': 'base/ssh' } ] 89 | runner.node.override['audit']['inspec_version'] = 'latest' 90 | runner.node.override['audit']['quiet'] = true 91 | runner.converge(described_recipe) 92 | end 93 | let(:myprofile) { chef_run.compliance_profile('myprofile') } 94 | 95 | it 'converges successfully' do 96 | expect { chef_run }.to_not raise_error 97 | end 98 | end 99 | 100 | # TODO: need to implement functionality for this 101 | # context 'When invalid profile is passed' do 102 | # let(:chef_run) do 103 | # runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6.9') 104 | # runner.node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'myprofile' } ] 105 | # runner.converge(described_recipe) 106 | # end 107 | 108 | # it 'does raise an error' do 109 | # expect { chef_run }.to raise_error("Invalid profile name 'myprofile'. Must contain /, e.g. 'john/ssh'") 110 | # end 111 | # end 112 | 113 | context 'When specifying profiles with alternate sources' do 114 | let(:chef_run) do 115 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 116 | runner.node.override['audit']['profiles'] = [ 117 | { 'name': 'linux', 'compliance': 'base/linux' }, 118 | { 'name': 'apache', 'compliance': 'base/apache' }, 119 | { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' }, 120 | { 'name': 'brewinc/tmp_compliance_profile', 121 | 'url': 'https://github.com/nathenharvey/tmp_compliance_profile', 122 | }, 123 | { 'name': 'brewinc/tmp_compliance_profile-master', 124 | 'path': '/tmp/tmp_compliance_profile-master', 125 | }, 126 | ] 127 | runner.converge(described_recipe) 128 | end 129 | it 'converges successfully' do 130 | expect { chef_run }.to_not raise_error 131 | end 132 | end 133 | 134 | context 'When specifying a single reporter' do 135 | let(:chef_run) do 136 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 137 | runner.node.override['audit']['collector'] = 'json-file' 138 | runner.node.override['audit']['profiles'] = [ 139 | { 'name': 'linux', 'compliance': 'base/linux' }, 140 | { 'name': 'apache', 'compliance': 'base/apache' }, 141 | { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' }, 142 | { 'name': 'brewinc/tmp_compliance_profile', 143 | 'url': 'https://github.com/nathenharvey/tmp_compliance_profile', 144 | }, 145 | { 'name': 'brewinc/tmp_compliance_profile-master', 146 | 'path': '/tmp/tmp_compliance_profile-master', 147 | }, 148 | ] 149 | runner.converge(described_recipe) 150 | end 151 | it 'converges successfully' do 152 | expect { chef_run }.to_not raise_error 153 | end 154 | end 155 | 156 | context 'When specifying multiple reporters' do 157 | let(:chef_run) do 158 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 159 | runner.node.override['audit']['collector'] = %w(chef-compliance json-file) 160 | runner.node.override['audit']['profiles'] = [ 161 | { 'name': 'linux', 'compliance': 'base/linux' }, 162 | { 'name': 'apache', 'compliance': 'base/apache' }, 163 | { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' }, 164 | { 'name': 'brewinc/tmp_compliance_profile', 165 | 'url': 'https://github.com/nathenharvey/tmp_compliance_profile', 166 | }, 167 | { 'name': 'brewinc/tmp_compliance_profile-master', 168 | 'path': '/tmp/tmp_compliance_profile-master', 169 | }, 170 | ] 171 | runner.converge(described_recipe) 172 | end 173 | it 'converges successfully' do 174 | expect { chef_run }.to_not raise_error 175 | end 176 | end 177 | 178 | context 'when audit attributes are not removed' do 179 | let(:chef_run) do 180 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 181 | runner.node.override['audit']['attributes']['my-inspec-attribute'] = 'ok' 182 | runner.converge(described_recipe) 183 | end 184 | it 'still contains the audit attributes after converge' do 185 | expect(chef_run.node.attributes['audit']['attributes']).to eq('my-inspec-attribute' => 'ok') 186 | end 187 | it 'should contain the inspec attributes in the run_state' do 188 | expect(chef_run.node.run_state['audit_attributes']).to eq('my-inspec-attribute' => 'ok') 189 | end 190 | it 'should not raise an exception' do 191 | expect { chef_run }.to_not raise_error 192 | end 193 | end 194 | 195 | context 'when audit attributes are removed' do 196 | let(:chef_run) do 197 | runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6') 198 | runner.node.override['audit']['attributes']['my-inspec-attribute'] = 'ok' 199 | runner.node.override['audit']['attributes_save'] = false 200 | runner.converge(described_recipe) 201 | end 202 | it 'should not contain the audit attributes after converge' do 203 | expect(chef_run.node.attributes['audit']['attributes']).to eq(nil) 204 | end 205 | it 'should contain the inspec attributes in the run_state' do 206 | expect(chef_run.node.run_state['audit_attributes']).to eq('my-inspec-attribute' => 'ok') 207 | end 208 | it 'should not raise an exception' do 209 | expect { chef_run }.to_not raise_error 210 | end 211 | end 212 | end 213 | -------------------------------------------------------------------------------- /spec/unit/report/audit_report_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: default 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require 'spec_helper' 20 | require 'json' 21 | require_relative '../../../libraries/helper' 22 | require_relative '../../../files/default/handler/audit_report' 23 | require_relative '../../data/mock' 24 | 25 | describe 'Chef::Handler::AuditReport methods' do 26 | let(:mynode) { Chef::Node.new } 27 | 28 | def set_inspec_backend_cache(status = false) 29 | mynode.default['audit']['inspec_backend_cache'] = status 30 | allow(@audit_report).to receive(:node).and_return(mynode) 31 | end 32 | 33 | before :each do 34 | @audit_report = Chef::Handler::AuditReport.new 35 | end 36 | 37 | describe ReportHelpers do 38 | let(:helpers) { Class.new { extend ReportHelpers } } 39 | before :each do 40 | @interval = 1440 41 | @interval_time = 1440 42 | interval_enabled = true 43 | write_to_file = false 44 | @helpers.create_timestamp_file 45 | end 46 | 47 | describe 'report when interval settings are set to default (disabled)' do 48 | interval_enabled = false 49 | 50 | it 'returns true for check_interval_settings' do 51 | status = @audit_report.check_interval_settings(@interval, interval_enabled, @interval_time) 52 | expect(status).to eq(true) 53 | end 54 | end 55 | 56 | describe 'report when interval settings are enabled' do 57 | interval_enabled = true 58 | 59 | it 'returns false for check_interval_settings' do 60 | status = @audit_report.check_interval_settings(@interval, interval_enabled, @interval_time) 61 | expect(status).to eq(false) 62 | end 63 | end 64 | end 65 | 66 | describe 'validate_inspec_version method' do 67 | before :each do 68 | require 'inspec' 69 | end 70 | 71 | it 'inspec min version fail' do 72 | stub_const('Inspec::VERSION', '1.20.0') 73 | expect { @audit_report.validate_inspec_version } 74 | .to raise_error(RuntimeError) 75 | .with_message('This audit cookbook version requires InSpec 1.25.1 or newer, aborting compliance scan...') 76 | end 77 | it 'inspec version warn for backend_cache' do 78 | stub_const('Inspec::VERSION', '1.46.0') 79 | set_inspec_backend_cache(true) 80 | expect(Chef::Log).to receive(:warn) 81 | .with('inspec_backend_cache requires InSpec version >= 1.47.0') 82 | .and_return('captured') 83 | expect(@audit_report.validate_inspec_version).to eq('captured') 84 | end 85 | it 'inspec version passes all requirements' do 86 | stub_const('Inspec::VERSION', '1.47.0') 87 | set_inspec_backend_cache(true) 88 | expect(Chef::Log).to_not receive(:warn) 89 | expect { @audit_report.validate_inspec_version }.to_not raise_error 90 | end 91 | end 92 | 93 | describe 'get_opts method' do 94 | it 'sets the format to json-min' do 95 | format = 'json-min' 96 | quiet = true 97 | set_inspec_backend_cache(true) 98 | opts = @audit_report.get_opts(format, quiet, {}) 99 | expect(opts['report']).to be true 100 | expect(opts['format']).to eq('json-min') 101 | expect(opts['output']).to eq('/dev/null') 102 | expect(opts['logger']).to eq(Chef::Log) 103 | expect(opts[:waiver_file]).to eq([]) 104 | expect(opts[:backend_cache]).to be true 105 | expect(opts[:attributes].empty?).to be true 106 | end 107 | it 'sets the format to json' do 108 | allow(File).to receive(:exist?).with('/tmp/exists.yaml').and_return(true) 109 | allow(File).to receive(:exist?).with('/tmp/missing.yaml').and_return(false) 110 | format = 'json' 111 | quiet = true 112 | set_inspec_backend_cache(true) 113 | mynode.default['audit']['waiver_file'] = ['/tmp/exists.yaml', '/tmp/missing.yaml'] 114 | opts = @audit_report.get_opts(format, quiet, {}) 115 | expect(opts['report']).to be true 116 | expect(opts['format']).to eq('json') 117 | expect(opts['output']).to eq('/dev/null') 118 | expect(opts['logger']).to eq(Chef::Log) 119 | expect(opts[:waiver_file]).to eq(['/tmp/exists.yaml']) 120 | expect(opts[:backend_cache]).to be true 121 | expect(opts[:attributes].empty?).to be true 122 | end 123 | it 'sets the backend_cache to false' do 124 | format = 'json' 125 | quiet = true 126 | set_inspec_backend_cache(false) 127 | opts = @audit_report.get_opts(format, quiet, {}) 128 | expect(opts['report']).to be true 129 | expect(opts['format']).to eq('json') 130 | expect(opts['output']).to eq('/dev/null') 131 | expect(opts['logger']).to eq(Chef::Log) 132 | expect(opts[:backend_cache]).to be false 133 | expect(opts[:attributes].empty?).to be true 134 | end 135 | it 'sets the attributes' do 136 | format = 'json-min' 137 | quiet = true 138 | attributes = { 139 | first: 'value1', 140 | second: 'value2', 141 | } 142 | set_inspec_backend_cache(true) 143 | opts = @audit_report.get_opts(format, quiet, attributes) 144 | expect(opts[:attributes][:first]).to eq('value1') 145 | expect(opts[:attributes][:second]).to eq('value2') 146 | end 147 | end 148 | 149 | describe 'call' do 150 | it 'given a profile, returns a json report' do 151 | opts = { 'report' => true, 'format' => 'json', 'output' => '/dev/null' } 152 | path = File.expand_path('../../data/mock_profile.rb', __dir__) 153 | profiles = [{ 'name': 'example', 'path': path }] 154 | # we circumvent the default load mechanisms, therefore we have to require inspec 155 | require 'inspec' 156 | report = @audit_report.call(opts, profiles) 157 | expected_report = /^.*profiles.*controls.*version.*statistics.*duration.*controls.*/ 158 | expect(report.to_json).to match(expected_report) 159 | end 160 | 161 | it 'given a profile, returns a json-min report' do 162 | require 'inspec' 163 | opts = { 'report' => true, 'format' => 'json-min', 'output' => '/dev/null' } 164 | path = File.expand_path('../../data/mock_profile.rb', __dir__) 165 | profiles = [{ 'name': 'example', 'path': path }] 166 | # we circumvent the default load mechanisms, therefore we have to require inspec 167 | require 'inspec' 168 | report = @audit_report.call(opts, profiles) 169 | expect(report[:controls].length).to eq(2) 170 | end 171 | 172 | it 'given an unfetchable profile, returns a min failed report' do 173 | require 'inspec' 174 | opts = { 'report' => true, 'format' => 'json-automate', 'output' => '/dev/null' } 175 | profiles = [{ 'name': 'example', 'path': '/tmp/missing-in-action-profile' }] 176 | # we circumvent the default load mechanisms, therefore we have to require inspec 177 | require 'inspec' 178 | report = @audit_report.call(opts, profiles) 179 | expect(report[:profiles].length).to eq(0) 180 | expect(report[:status]).to eq('failed') 181 | expect(report[:platform]).to eq({ 'name': 'unknown', 'release': 'unknown' }) 182 | expected_status_message = /^Cannot fetch all profiles.*does not exist$/ 183 | expect(report[:status_message]).to match(expected_status_message) 184 | end 185 | 186 | it 'given a bad InSpec config, returns a min failed report' do 187 | require 'inspec' 188 | opts = { 'backend' => 'ssh', 'report' => true, 'format' => 'json-automate', 'output' => '/dev/null' } 189 | path = File.expand_path('../../data/mock_profile.rb', __dir__) 190 | profiles = [{ 'name': 'example', 'path': path }] 191 | # we circumvent the default load mechanisms, therefore we have to require inspec 192 | require 'inspec' 193 | report = @audit_report.call(opts, profiles) 194 | expect(report[:profiles].length).to eq(0) 195 | expect(report[:status]).to eq('failed') 196 | expect(report[:platform]).to eq({ 'name': 'unknown', 'release': 'unknown' }) 197 | expected_status_message = /^Client error. can't connect.*/ 198 | expect(report[:status_message]).to match(expected_status_message) 199 | end 200 | end 201 | end 202 | -------------------------------------------------------------------------------- /spec/unit/report/fetcher_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: audit 3 | # Spec:: fetcher 4 | # 5 | # Copyright:: 2016 Chef Software, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require 'spec_helper' 20 | require_relative '../../../files/default/vendor/chef-server/fetcher' 21 | 22 | describe ChefServer::Fetcher do 23 | let(:mynode) { Chef::Node.new } 24 | let(:myprofile) { 'compliance://foobazz' } 25 | let(:profile_hash) do 26 | { 27 | name: 'linux-baseline', 28 | compliance: 'user/linux-baseline', 29 | version: '2.1.0', 30 | } 31 | end 32 | let(:profile_hash_target) do 33 | '/organizations/org/owners/user/compliance/linux-baseline/version/2.1.0/tar' 34 | end 35 | let(:non_profile_url) do 36 | 'http://127.0.0.1:8889/organizations/org/owners/user/compliance/linux-baseline/version/2.1.0/tar' 37 | end 38 | 39 | context 'when target is a string' do 40 | before :each do 41 | allow(Chef).to receive(:node).and_return(mynode) 42 | allow(ChefServer::Fetcher).to receive(:construct_url).and_return(URI(myprofile)) 43 | allow(ChefServer::Fetcher).to receive(:chef_server_visibility?).and_return(true) 44 | end 45 | 46 | it 'should resolve a target' do 47 | mynode.default['audit']['fetcher'] = nil 48 | res = ChefServer::Fetcher.resolve(myprofile) 49 | expect(res.target).to eq(myprofile) 50 | end 51 | 52 | it 'should add /compliance URL prefix if needed' do 53 | mynode.default['audit']['fetcher'] = 'chef-server' 54 | expect(ChefServer::Fetcher.url_prefix).to eq('/compliance') 55 | end 56 | 57 | it 'should omit /compliance if not' do 58 | mynode.default['audit']['fetcher'] = nil 59 | expect(ChefServer::Fetcher.url_prefix).to eq('') 60 | end 61 | end 62 | 63 | context 'when target is a hash' do 64 | before :each do 65 | Chef::Config[:chef_server_url] = 'http://127.0.0.1:8889/organizations/org' 66 | allow(Chef).to receive(:node).and_return(mynode) 67 | end 68 | 69 | it 'should resolve a target with a version' do 70 | mynode.default['audit']['fetcher'] = nil 71 | res = ChefServer::Fetcher.resolve(profile_hash) 72 | expect(res.target).to eq("http://127.0.0.1:8889#{profile_hash_target}") 73 | end 74 | end 75 | 76 | context 'when profile not found' do 77 | before :each do 78 | Chef::Config[:verify_api_cert] = false 79 | Chef::Config[:ssl_verify_mode] = :verify_none 80 | allow(Chef).to receive(:node).and_return(mynode) 81 | end 82 | 83 | it 'should raise error' do 84 | myproc = proc { 85 | config = { 86 | 'server_type' => 'automate', 87 | 'automate' => { 88 | 'ent' => 'my_ent', 89 | 'token_type' => 'dctoken', 90 | }, 91 | 'profile' => ['admin', 'linux-baseline', '2.0'], 92 | } 93 | mynode.default['audit']['reporter'] = 'chef-server' 94 | ChefServer::Fetcher.target_url('non_profile_url', config).read 95 | } 96 | expect { myproc.call }.to raise_error(Errno::ECONNREFUSED) 97 | end 98 | end 99 | end 100 | -------------------------------------------------------------------------------- /tasks/changelog.rake: -------------------------------------------------------------------------------- 1 | # Check the requirements for running an update of this repository. 2 | task :check_update_dependencies do 3 | require_command 'git' 4 | require_command 'github_changelog_generator', "\n"\ 5 | "For more information on how to install it see:\n"\ 6 | " https://github.com/skywinder/github-changelog-generator\n" 7 | require_env 'CHANGELOG_GITHUB_TOKEN', "\n"\ 8 | "Please configure this token to make sure you can run all commands\n"\ 9 | "against GitHub.\n\n"\ 10 | "See github_changelog_generator homepage for more information:\n"\ 11 | " https://github.com/skywinder/github-changelog-generator\n" 12 | end 13 | 14 | # Check if a command is available 15 | # 16 | # @param [Type] x the command you are interested in 17 | # @param [Type] msg the message to display if the command is missing 18 | def require_command(x, msg = nil) 19 | return if system("command -v #{x} || exit 1") 20 | msg ||= 'Please install it first!' 21 | puts "\033[31;1mCan't find command #{x.inspect}. #{msg}\033[0m" 22 | exit 1 23 | end 24 | 25 | # Check if a required environment variable has been set 26 | # 27 | # @param [String] x the variable you are interested in 28 | # @param [String] msg the message you want to display if the variable is missing 29 | def require_env(x, msg = nil) 30 | exists = `env | grep "^#{x}="` 31 | return unless exists.empty? 32 | puts "\033[31;1mCan't find environment variable #{x.inspect}. #{msg}\033[0m" 33 | exit 1 34 | end 35 | 36 | begin 37 | require 'chef/cookbook/metadata' 38 | require 'github_changelog_generator/task' 39 | 40 | metadata = Chef::Cookbook::Metadata.new 41 | metadata.from_file('metadata.rb') 42 | 43 | GitHubChangelogGenerator::RakeTask.new changelog: ['check_update_dependencies'] do |config| 44 | # Just have to add a v here because its the convention stove uses 45 | config.future_release = "v#{metadata.version}" 46 | config.user = 'chef-cookbooks' 47 | config.project = 'audit' 48 | config.bug_labels = ['bug', 'Bug', 'Type: Bug'] 49 | config.enhancement_labels = ['enhancement', 'Enhancement', 'Type: Enhancement'] 50 | end 51 | rescue LoadError 52 | puts 'Problem loading gems please install chef and github_changelog_generator' 53 | end 54 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | Berksfile.lock 3 | *~ 4 | *# 5 | .#* 6 | \#*# 7 | .*.sw[a-z] 8 | *.un~ 9 | 10 | # Bundler 11 | Gemfile.lock 12 | bin/* 13 | .bundle/* 14 | 15 | .kitchen/ 16 | .kitchen.local.yml 17 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/files/waivers-fixture/controls/waiver-check.rb: -------------------------------------------------------------------------------- 1 | 2 | # Some of these controls are waivered by the file waivers/waivers.yaml 3 | 4 | # control-01 is normal, should pass 5 | control 'control-01' do 6 | describe 'the expected value' do 7 | it { should cmp 'the expected value' } 8 | end 9 | end 10 | 11 | # control-02 is permanently waivered, should be skipped 12 | control 'control-02' do 13 | describe 'the expected value' do 14 | it { should cmp 'the expected value' } 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/files/waivers-fixture/inspec.yml: -------------------------------------------------------------------------------- 1 | name: waivers-fixture 2 | license: Apache-2.0 3 | summary: A simple profile to verify waiver functionality under audit cookbook 4 | version: 0.1.0 5 | supports: 6 | platform: os 7 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/files/waivers-fixture/waivers/waivers.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | control-02: 3 | justification: It must be waivered for this test 4 | run: false 5 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'test_helper' 2 | maintainer 'The InSpec Team' 3 | maintainer_email 'inspec@chef.io' 4 | license 'all rights reserved' 5 | description 'Installs/Configures test_helper' 6 | version '0.1.0' 7 | 8 | # If you upload to Supermarket you should set this so your cookbook 9 | # gets a `View Issues` link 10 | # issues_url 'https://github.com//test_helper/issues' if respond_to?(:issues_url) 11 | 12 | # If you upload to Supermarket you should set this so your cookbook 13 | # gets a `View Source` link 14 | # source_url 'https://github.com//test_helper' if respond_to?(:source_url) 15 | 16 | depends 'git' 17 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/recipes/create_file.rb: -------------------------------------------------------------------------------- 1 | # ensures that the file defined by attributes exists, so its associated profile will pass 2 | 3 | file node['audit']['attributes']['file'] do 4 | action :create 5 | end 6 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/recipes/force_inspec_core.rb: -------------------------------------------------------------------------------- 1 | chef_gem 'inspec-core' do 2 | compile_time true 3 | action :remove 4 | end 5 | 6 | chef_gem 'inspec-core-bin' do 7 | compile_time true 8 | action :remove 9 | end 10 | 11 | chef_gem 'inspec' do 12 | compile_time true 13 | action :remove 14 | end 15 | 16 | chef_gem 'inspec-core' do 17 | compile_time true 18 | version '4.3.2' 19 | action :install 20 | end 21 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/recipes/install_inspec.rb: -------------------------------------------------------------------------------- 1 | chef_gem 'inspec-core' do 2 | compile_time true 3 | action :remove 4 | end 5 | 6 | chef_gem 'inspec-core-bin' do 7 | compile_time true 8 | action :remove 9 | end 10 | 11 | chef_gem 'inspec' do 12 | compile_time true 13 | action :remove 14 | end 15 | 16 | chef_gem 'inspec' do 17 | compile_time true 18 | version '1.19.1' 19 | action :install 20 | end 21 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/recipes/setup.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: test_helper 3 | # Recipe:: setup 4 | # 5 | # Copyright:: (c) 2016 The Authors, All Rights Reserved. 6 | 7 | # needed to fetch github profiles 8 | include_recipe 'git::default' 9 | 10 | # needed for testing inspec version installed 11 | output = Chef::JSONCompat.to_json_pretty(node.to_hash).to_s 12 | file '/tmp/node.json' do 13 | content output 14 | sensitive true 15 | end 16 | -------------------------------------------------------------------------------- /test/cookbooks/test_helper/recipes/setup_waiver_fixture.rb: -------------------------------------------------------------------------------- 1 | 2 | %w(controls waivers).each do |subdir| 3 | directory "#{node['audit']['profiles']['waiver-test-profile']['path']}/#{subdir}" do 4 | recursive true 5 | end 6 | end 7 | 8 | %w(inspec.yml controls/waiver-check.rb waivers/waivers.yaml).each do |file| 9 | cookbook_file "#{node['audit']['profiles']['waiver-test-profile']['path']}/#{file}" do 10 | source "waivers-fixture/#{file}" 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/integration/chef-node-disabled/default.rb: -------------------------------------------------------------------------------- 1 | # get most recent json-file output 2 | json_file = command('ls -t /tmp/inspec-*.json').stdout.lines.first.chomp 3 | controls = json(json_file).profiles.first['controls'] 4 | results = [] 5 | controls.each do |c| 6 | c['results'].each do |r| 7 | results << r 8 | end 9 | end 10 | 11 | # the controls that read from chef_node should fail because the chef_node data should not be present 12 | cpu_key_control = results.find { |x| x['code_desc'] == 'Chef node data - cpu key should exist' } 13 | cpu_key_control = {} if cpu_key_control.nil? 14 | 15 | describe 'cpu_key control' do 16 | it 'status should be failed' do 17 | expect(cpu_key_control['status']).to eq('failed') 18 | end 19 | end 20 | 21 | chef_environment_control = results.find { |x| x['code_desc'] == 'Chef node data - chef_environment should exist' } 22 | chef_environment_control = {} if chef_environment_control.nil? 23 | 24 | describe 'chef_environment control' do 25 | it 'status should be failed' do 26 | expect(chef_environment_control['status']).to eq('failed') 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /test/integration/chef-node-enabled/default.rb: -------------------------------------------------------------------------------- 1 | # get most recent json-file output 2 | json_file = command('ls -t /tmp/inspec-*.json').stdout.lines.first.chomp 3 | controls = json(json_file).profiles.first['controls'] 4 | results = [] 5 | controls.each do |c| 6 | c['results'].each do |r| 7 | results << r 8 | end 9 | end 10 | 11 | # Test ability to read in Chef node attributes when the chef_node attribute is enabled 12 | cpu_key_control = results.find { |x| x['code_desc'] == 'Chef node data - cpu key should exist' } 13 | cpu_key_control = {} if cpu_key_control.nil? 14 | 15 | describe 'cpu_key control' do 16 | it 'status should be passed' do 17 | expect(cpu_key_control['status']).to eq('passed') 18 | end 19 | end 20 | 21 | chef_environment_control = results.find { |x| x['code_desc'] == 'Chef node data - chef_environment should exist' } 22 | chef_environment_control = {} if chef_environment_control.nil? 23 | 24 | describe 'chef_environment control' do 25 | it 'status should be passed' do 26 | expect(chef_environment_control['status']).to eq('passed') 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /test/integration/chef15-compatible-inspec/default.rb: -------------------------------------------------------------------------------- 1 | # verify that a specific inspec version is installed 2 | describe gem('inspec', :chef) do 3 | it { should_not be_installed } 4 | end 5 | 6 | describe gem('inspec-core', :chef) do 7 | it { should be_installed } 8 | its('version') { should_not cmp '3.0.9' } 9 | end 10 | -------------------------------------------------------------------------------- /test/integration/default/default.rb: -------------------------------------------------------------------------------- 1 | # copyright: 2015, Chef Software, Inc. 2 | # license: Apache-2.0 3 | 4 | unless os.linux? 5 | warn "\033[1;33mTODO: Not running #{__FILE__} because we are not on Linux.\033[0m" 6 | return 7 | end 8 | 9 | node = json('/tmp/node.json') 10 | 11 | # TODO: change once gem resource handles alternate path to `gem` command 12 | describe command('/opt/chef/embedded/bin/gem list --local -a -q inspec | grep \'^inspec\' | awk -F"[()]" \'{printf $2}\'') do 13 | its('stdout') { should_not be_nil } 14 | end 15 | 16 | command('find /opt/kitchen/cache/cookbooks/audit -type f -a -name inspec\*.json').stdout.split.each do |f| 17 | describe json(f.to_s) do 18 | its(%w(statistics duration)) { should be < 10 } 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /test/integration/gem-install-core-version2/default.rb: -------------------------------------------------------------------------------- 1 | # verify that a specific inspec version is installed 2 | describe gem('inspec-core', :chef) do 3 | it { should be_installed } 4 | its('version') { should cmp '2.1.67' } 5 | end 6 | 7 | describe gem('inspec', :chef) do 8 | it { should_not be_installed } 9 | end 10 | -------------------------------------------------------------------------------- /test/integration/gem-install-core-version3/default.rb: -------------------------------------------------------------------------------- 1 | # verify that a specific inspec version is installed 2 | describe gem('inspec-core', :chef) do 3 | it { should be_installed } 4 | its('version') { should cmp '3.0.9' } 5 | end 6 | 7 | describe gem('inspec-core-bin', :chef) do 8 | it { should_not be_installed } 9 | end 10 | 11 | describe gem('inspec', :chef) do 12 | it { should_not be_installed } 13 | end 14 | -------------------------------------------------------------------------------- /test/integration/gem-install-core-version4/default.rb: -------------------------------------------------------------------------------- 1 | # verify that a specific inspec version is installed 2 | describe gem('inspec-core', :chef) do 3 | it { should be_installed } 4 | its('version') { should cmp '4.3.2' } 5 | end 6 | 7 | describe gem('inspec-core-bin', :chef) do 8 | it { should be_installed } 9 | its('version') { should cmp '4.3.2' } 10 | end 11 | 12 | describe gem('inspec', :chef) do 13 | it { should_not be_installed } 14 | end 15 | -------------------------------------------------------------------------------- /test/integration/inspec-attributes/default.rb: -------------------------------------------------------------------------------- 1 | # get most recent json-file output 2 | json_file = command('ls -t /tmp/inspec-*.json').stdout.lines.first.chomp 3 | 4 | # ensure the control we expect is present and passed 5 | controls = json(json_file).profiles.first['controls'] 6 | results = [] 7 | controls.each do |c| 8 | c['results'].each do |r| 9 | results << r 10 | end 11 | end 12 | attribute_control = results.find { |x| x['code_desc'] == 'File /opt/kitchen/cache/attribute-file-exists.test is expected to exist' } 13 | attribute_control = {} if attribute_control.nil? 14 | 15 | describe 'attribute control' do 16 | it 'status should be passed' do 17 | expect(attribute_control['status']).to eq('passed') 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /test/integration/skip-inspec-gem-install/default.rb: -------------------------------------------------------------------------------- 1 | # verify that a specific inspec version is installed 2 | describe gem('inspec', :chef) do 3 | it { should be_installed } 4 | its('version') { should cmp '1.25.1' } 5 | end 6 | -------------------------------------------------------------------------------- /test/integration/waivers/default.rb: -------------------------------------------------------------------------------- 1 | # get most recent json-file output 2 | json_file = command('ls -t /tmp/inspec-*.json').stdout.lines.first.chomp 3 | controls = json(json_file).profiles.first['controls'] 4 | 5 | # The test fixture has two controls - the first should pass, 6 | # the second should be a skip with a waiver justification 7 | 8 | control 'the unwaivered control' do 9 | describe controls[0]['results'][0]['status'] do 10 | it { should cmp 'passed' } 11 | end 12 | end 13 | 14 | control 'the waivered control' do 15 | describe controls[1]['results'][0]['status'] do 16 | it { should cmp 'skipped' } 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /test/kitchen-automate/.kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: 3 | name: ec2 4 | region: us-east-1 5 | subnet_id: subnet-46b55431 6 | security_group_ids: ['sg-c94c25b4'] # audit-integration-tests security group 7 | aws_ssh_key_id: kitchen-key-compliance 8 | instance_type: t2.medium 9 | interface: public 10 | instance_initiated_shutdown_behavior: terminate 11 | tags: 12 | Name: 'kitchen-test-automate' 13 | # without these tags, the machine can be auto terminated: 14 | X-Dept: 'Eng' 15 | X-Contact: 'apop' 16 | X-Production: 'false' 17 | X-Environment: 'acceptance' 18 | X-Role: 'Standalone Chef Server + Automate' 19 | 20 | transport: 21 | name: ssh 22 | ssh_key: ~/.ssh/kitchen-key-compliance.pem 23 | 24 | provisioner: 25 | name: chef_zero 26 | require_chef_omnibus: 12.16.42 27 | 28 | verifier: 29 | name: inspec 30 | sudo: true 31 | 32 | platforms: 33 | - name: amzn 34 | driver: 35 | # start with a private ami created from an OpsWorks instance snapshot 36 | image_id: ami-b02b10a7 37 | transport: 38 | username: ec2-user 39 | 40 | suites: 41 | - name: default 42 | run_list: 43 | - recipe[kitchen-automate::default] 44 | attributes: 45 | -------------------------------------------------------------------------------- /test/kitchen-automate/Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | 5 | cookbook 'audit', path: '../..' 6 | -------------------------------------------------------------------------------- /test/kitchen-automate/README.md: -------------------------------------------------------------------------------- 1 | # kitchen-automate Cookbook 2 | 3 | Integration testing for the `audit` cookbook. 4 | 5 | ## Purpose of this cookbook 6 | 7 | Tests the audit cookbook for the `chef-automate` and `chef-server-automate` collectors with a node managed by a Chef Server that is integrated with Chef Automate. 8 | 9 | **Note:** This cookbook has been designed only for testing purposes, trying to minimize the external dependencies and test time. Don't use it for production. 10 | 11 | ## Requirements 12 | 13 | * `test-kitchen` installed with `kitchen-ec2` driver. 14 | * ENV variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` in order to create and destroy the test ec2 instance. 15 | * Access to the private SSH key (`~/.ssh/kitchen-key-compliance.pem`) used in `.kitchen.yml` 16 | 17 | ## How is it working? 18 | 19 | This cookbook uses the `kitchen-ec2` driver in order to spin up an OpsWorks (Chef Server + Automate) instance in ec2 using a previously snapshotted OpsWorks instance. 20 | 21 | A `kitchen test` will: 22 | 1. Create the ec2 instance 23 | 2. Converge the node with chef_zero using the `default` recipe 24 | 3. Test the success of the reporting using InSpec resources in `test/integration/default/`. This is verifying that compliance scan reports exist in ElasticSearch (used by Chef Automate) for both converges. 25 | 4. Terminate the instance 26 | 27 | Let's look a bit closer and the recipes used during the converge step: 28 | 29 | 1. `harakiri` - If the converge fails and the instance is not terminated, the `harakiri` recipe configures the instance to self terminate after 4 hours of operation. 30 | 2. `configure_services` - Idempotently configures the Chef Server and Chef Automate in order to enable the compliance profiles asset store and proxying. 31 | 3. `upload_profile` - Downloads a remote profile and uploads it to the Automate Asset Store via the API and the data collector token 32 | 4. `bootstrap_localhost` - In order to test the `audit` cookbook with the `chef-server-automate` collector, we need a node that is managed by a Chef Server. This recipe: 33 | a) Configures `knife.rb` to talk to the local Chef Server 34 | b) Uploads to the Chef Server all the cookbook synced by `test-kitchen` to `/tmp/kitchen/cookbooks` 35 | c) Sets up SSH so we can bootstrap localhost via SSH. We are using `/tmp/kitchen/client.pem` as a private key as kitchen creates it for us already. 36 | d) Bootstraps localhost as a node for the local Chef Server with an empty runlist 37 | 5. `converge_localhost` - Creates attribute files and converges the `audit::default` recipe once for each of the two collectors: `chef-server-automate` and `chef-automate`. The `chef-server-automate` converge uses the compliance profile stored in Automate (uploaded by the `upload_profile` recipe). 38 | 39 | 40 | ## Creating a starting ami for your account 41 | 42 | You can create an ec2 ami for this cookbook by launching an official OpsWorks instance. Once the instance is operational, you can optionally make this change: 43 | 44 | ```bash 45 | # enable profiles in Automate 46 | echo "compliance_profiles['enable'] = true" >> /etc/delivery/delivery.rb 47 | automate-ctl reconfigure 48 | ``` 49 | 50 | It's optional because this cookbook has a recipe (`configure_services`) to make this change, but takes an extra minute per converge later on. 51 | Stop the instance and create an image from it from the AWS Management Console with option `Image > Create Image`. Once the image id is available, use it for the `image_id` option in `.kitchen.yml`. 52 | -------------------------------------------------------------------------------- /test/kitchen-automate/libraries/helper.rb: -------------------------------------------------------------------------------- 1 | # module AutomateHelpers 2 | def dc_token 3 | @dc_token ||= JSON.parse(File.read('/etc/delivery/delivery-running.json'))['delivery']['data_collector']['token'] 4 | end 5 | # end 6 | # 7 | # ::Chef::Recipe.send(:include, AutomateHelpers) 8 | -------------------------------------------------------------------------------- /test/kitchen-automate/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'kitchen-automate' 2 | maintainer 'Chef' 3 | maintainer_email 'support@chef.io' 4 | license 'All rights reserved' 5 | description 'Installs/Configures kitchen-automate' 6 | version '0.1.0' 7 | 8 | depends 'audit' 9 | depends 'line' 10 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/bootstrap_localhost.rb: -------------------------------------------------------------------------------- 1 | directory '/root/.chef' do 2 | mode '770' 3 | action :create 4 | end 5 | 6 | # Configure knife against the local Chef Server 7 | template '/root/.chef/knife.rb' do 8 | source 'knife.rb.erb' 9 | mode '0660' 10 | variables( 11 | log_level: 'info' 12 | ) 13 | end 14 | 15 | # Upload the cookbooks from the location specified in /root/.chef/knife.rb 16 | # This is the directory where test-kitchen syncs the cookbooks into 17 | execute 'Uploading cookbooks to Chef Server' do 18 | command <<-EOH 19 | sudo knife cookbook upload -a 20 | sudo knife cookbook list 21 | EOH 22 | live_stream true 23 | action :run 24 | end 25 | 26 | # Ensure the node is in the expected state 27 | raise 'Cannot find /tmp/kitchen/client.pem' unless ::File.exist?('/tmp/kitchen/client.pem') 28 | raise 'Cannot find /home/ec2-user/.ssh/authorized_keys' unless ::File.exist?('/home/ec2-user/.ssh/authorized_keys') 29 | 30 | # Add a public key to be used for SSH bootstrapping 31 | execute 'Add SSH public key to be used by self bootstrapping' do 32 | command <<-EOH 33 | key=$(ssh-keygen -y -f /tmp/kitchen/client.pem) 34 | echo "$key kitchen_client.pem" >> /home/ec2-user/.ssh/authorized_keys 35 | EOH 36 | action :run 37 | not_if { File.open('/home/ec2-user/.ssh/authorized_keys').read().index('kitchen_client.pem') } 38 | end 39 | 40 | # Bootstraps the node against the local Chef Server. Will run on first converge only as '/etc/chef/client.pem' is not in place yet 41 | execute 'Bootstrapping the node with local Chef Server' do 42 | command <<-EOH 43 | sudo knife bootstrap localhost --sudo \ 44 | --ssh-user "ec2-user" \ 45 | --ssh-identity-file /tmp/kitchen/client.pem \ 46 | --node-name "testus" \ 47 | --json-attribute-file /tmp/kitchen/dna.json \ 48 | --node-ssl-verify-mode none 49 | EOH 50 | live_stream true 51 | action :run 52 | not_if { ::File.exist?('/etc/chef/client.pem') } 53 | end 54 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/configure_services.rb: -------------------------------------------------------------------------------- 1 | # Ensure the Automate Asset Store is enabled and trigger a reconfigure otherwise 2 | replace_or_add 'Enable profiles in Automate' do 3 | path '/etc/delivery/delivery.rb' 4 | pattern "^compliance_profiles['enable'].*" 5 | line "compliance_profiles['enable'] = true" 6 | notifies :run, 'execute[automate_reconfigure]', :immediately 7 | end 8 | 9 | execute 'automate_reconfigure' do 10 | command <<-EOH 11 | automate-ctl reconfigure 12 | EOH 13 | action :nothing 14 | end 15 | 16 | # Because we snapshot the instance, the initial S3 storage config goes away 17 | # Disable the S3 store for cookbooks: 18 | delete_lines 'Avoid cookbook storage in S3' do 19 | path '/etc/opscode/chef-server.rb' 20 | pattern '^opscode_erchef.+base_resource_url' 21 | end 22 | 23 | replace_or_add 'Enable forwarding of profiles to Automate' do 24 | path '/etc/opscode/chef-server.rb' 25 | pattern "profiles['root_url'].*" 26 | line "profiles['root_url'] = 'https://localhost'" 27 | end 28 | 29 | # Needs to run for a new instance since IPs and hostname changed 30 | execute 'chef_reconfigure' do 31 | command <<-EOH 32 | chef-server-ctl reconfigure 33 | touch /etc/opscode/chef-server-reconfigured.txt 34 | EOH 35 | action :run 36 | not_if { ::File.exist?('/etc/opscode/chef-server-reconfigured.txt') } 37 | end 38 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/converge_localhost.rb: -------------------------------------------------------------------------------- 1 | # Create the attributes file to test the 'chef-server-visibility' collector 2 | file '/root/attrs_chef-server-visibility.json' do 3 | content <<-EOH 4 | { 5 | "audit": { 6 | "collector": "chef-server-visibility", 7 | "insecure": true, 8 | "profiles": [ 9 | { 10 | "name": "ssh-hardening", 11 | "compliance": "admin/ssh-hardening" 12 | } 13 | ] 14 | } 15 | } 16 | EOH 17 | mode '600' 18 | end 19 | 20 | # Not needed for 'chef-server-visibility' collector converge 21 | delete_lines 'Delete data_collector.server_url from /etc/chef/client.rb' do 22 | path '/etc/chef/client.rb' 23 | pattern '^data_collector.server_url.*' 24 | end 25 | delete_lines 'Delete data_collector.token from /etc/chef/client.rb' do 26 | path '/etc/chef/client.rb' 27 | pattern '^data_collector.token.*' 28 | end 29 | 30 | # Executes chef-client if the node is bootstrapped('/etc/chef/client.pem' file to exists) 31 | # Removing the 'audit' attributes tree to avoid attributes state issues between multiple converges 32 | execute '### Run chef-client w/ collector chef-server-visibility' do 33 | command <<-EOH 34 | echo "*** Removing audit node attributes from the Chef Server..." 35 | sudo knife exec -E "nodes.transform(:all) {|n| n.normal_attrs.delete(:audit) rescue nil }" 36 | echo "*** Removing local insights-* ElasticSearch indices" 37 | curl -X DELETE "http://127.0.0.1:9200/insights-*" 38 | echo "*** Showing testus node with normal attributes" 39 | sudo knife node show testus -m 40 | sudo chef-client --override-runlist "recipe[audit::default]" \ 41 | --json-attributes /root/attrs_chef-server-visibility.json 42 | EOH 43 | live_stream true 44 | action :run 45 | only_if { ::File.exist?('/etc/chef/client.pem') } 46 | end 47 | 48 | # Create the attributes file to test the 'chef-visibility' collector 49 | file '/root/attrs_chef-visibility.json' do 50 | content <<-EOH 51 | { 52 | "audit": { 53 | "collector": "chef-visibility", 54 | "insecure": true, 55 | "profiles": [ 56 | { 57 | "name": "linux-patch-benchmark", 58 | "url": "https://github.com/dev-sec/linux-patch-benchmark/archive/master.zip" 59 | } 60 | ] 61 | } 62 | } 63 | EOH 64 | mode '600' 65 | end 66 | 67 | # Collector 'chef-visibility' needs these client.rb settings: 68 | replace_or_add 'Add data_collector.server_url to /etc/chef/client.rb' do 69 | path '/etc/chef/client.rb' 70 | pattern '^data_collector.server_url.*' 71 | line "data_collector.server_url 'https://127.0.0.1/data-collector/v0/'" 72 | end 73 | replace_or_add 'Add data_collector.token to /etc/chef/client.rb' do 74 | path '/etc/chef/client.rb' 75 | pattern '^data_collector.token.*' 76 | line "data_collector.token '#{dc_token}'" 77 | end 78 | 79 | # Executes chef-client if the node is bootstrapped('/etc/chef/client.pem' file to exists) 80 | # Removing the 'audit' attributes tree to avoid attributes state issues between multiple converges 81 | execute '### Run chef-client w/ collector chef-visibility' do 82 | command <<-EOH 83 | echo "*** Removing audit node attributes from the Chef Server..." 84 | sudo knife exec -E "nodes.transform(:all) {|n| n.normal_attrs.delete(:audit) rescue nil }" 85 | echo "*** Showing testus node with normal attributes" 86 | sudo knife node show testus -m 87 | sudo chef-client --override-runlist "recipe[audit::default]" \ 88 | --json-attributes /root/attrs_chef-visibility.json 89 | # give time to ElasticSearch to index the report 90 | sleep 10 91 | EOH 92 | live_stream true 93 | action :run 94 | only_if { ::File.exist?('/etc/chef/client.pem') } 95 | end 96 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/default.rb: -------------------------------------------------------------------------------- 1 | include_recipe 'kitchen-automate::harakiri' 2 | include_recipe 'kitchen-automate::configure_services' 3 | include_recipe 'kitchen-automate::upload_profile' 4 | include_recipe 'kitchen-automate::bootstrap_localhost' 5 | include_recipe 'kitchen-automate::converge_localhost' 6 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/harakiri.rb: -------------------------------------------------------------------------------- 1 | package 'at' do 2 | action :install 3 | end 4 | 5 | service 'atd' do 6 | action [ :enable, :start ] 7 | end 8 | 9 | # Run the "halt" command on the instance 4 hours from now 10 | # Ensures instances are not left hanging around if integration tests fail 11 | # To prevent an instance from halting use the `atq` and `atrm ID` commands as root 12 | execute 'echo "halt" | at now + 50 minutes' do 13 | action :run 14 | end 15 | -------------------------------------------------------------------------------- /test/kitchen-automate/recipes/upload_profile.rb: -------------------------------------------------------------------------------- 1 | # Download this profile locally and trigger the upload 2 | remote_file '/root/ssh-hardening.tar.gz' do 3 | source 'https://github.com/dev-sec/tests-ssh-hardening/archive/2.0.0.tar.gz' 4 | action :create 5 | notifies :run, 'ruby_block[upload_profile]', :immediately 6 | end 7 | 8 | # Upload '/root/ssh-hardening.tar.gz' to the local asset store using 9 | # the Automate Asset Store API and the shared token 10 | ruby_block 'upload_profile' do 11 | block do 12 | require 'json' 13 | require 'openssl' 14 | require 'net/http' 15 | 16 | http = Net::HTTP.new('localhost', 443) 17 | http.use_ssl = true 18 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 19 | req = Net::HTTP::Post.new('/compliance/profiles/admin') 20 | 21 | file_path = '/root/ssh-hardening.tar.gz' 22 | req.body_stream = File.open(file_path, 'rb') 23 | req.add_field('Content-Length', File.size(file_path)) 24 | req.add_field('Content-Type', 'application/x-gtar') 25 | 26 | req.add_field('x-data-collector-auth', 'version=1.0') 27 | req.add_field('chef-delivery-enterprise', 'default') 28 | req.add_field('x-data-collector-token', dc_token) 29 | 30 | boundary = 'INSPEC-PROFILE-UPLOAD' 31 | req.add_field('session', boundary) 32 | res = http.request(req) 33 | end 34 | action :nothing 35 | end 36 | -------------------------------------------------------------------------------- /test/kitchen-automate/templates/default/knife.rb.erb: -------------------------------------------------------------------------------- 1 | base_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..') 2 | 3 | log_level <%= @log_level %> 4 | log_location STDOUT 5 | node_name 'pivotal' 6 | client_key '/etc/opscode/pivotal.pem' 7 | syntax_check_cache_path File.join(base_dir, '.chef', 'syntax_check_cache') 8 | cookbook_path '/tmp/kitchen/cookbooks' 9 | chef_server_url 'https://localhost/organizations/default' 10 | trusted_certs_dir File.join(base_dir, '.chef', 'ca_certs') 11 | ssl_verify_mode :verify_none 12 | -------------------------------------------------------------------------------- /test/kitchen-automate/test/integration/default/chef-server-visibility_spec.rb: -------------------------------------------------------------------------------- 1 | describe file '/etc/chef/client.rb' do 2 | it { should exist } 3 | end 4 | 5 | curl_elasticsearch = 'curl -X POST http://127.0.0.1:9200/insights-*/_search -d \' 6 | { 7 | "query": { 8 | "filtered": { 9 | "filter": { 10 | "bool": { 11 | "must": [ 12 | { 13 | "term": { 14 | "event_type": "inspec" 15 | } 16 | }, 17 | { 18 | "term": { 19 | "event_action": "exec" 20 | } 21 | } 22 | ] 23 | } 24 | } 25 | } 26 | }, 27 | "sort": { 28 | "@timestamp": { 29 | "order": "asc" 30 | } 31 | } 32 | }\'' 33 | 34 | elastic_response = json(command: curl_elasticsearch) 35 | 36 | describe elastic_response do 37 | it { should_not be nil } 38 | end 39 | 40 | elastic_hits = elastic_response['hits', 'hits'] 41 | 42 | # one for the chef-server-visibility collector 43 | # one for the chef-visibility collector 44 | describe elastic_hits.length do 45 | it { should eq 2 } 46 | end 47 | 48 | # guard the elastic_hits array from index out of bounds exception 49 | if elastic_hits.length == 2 50 | describe elastic_hits[0]['_source']['compliance_summary'] do 51 | its(['status']) { should eq 'failed' } 52 | its(['total']) { should be > 60 } 53 | end 54 | 55 | describe elastic_hits[0]['_source']['profiles'][0]['name'] do 56 | it { should eq 'ssh-hardening' } 57 | end 58 | 59 | describe elastic_hits[1]['_source']['profiles'][0]['name'] do 60 | it { should eq 'linux-patch-benchmark' } 61 | end 62 | end 63 | --------------------------------------------------------------------------------