├── .editorconfig ├── .envrc ├── .gitattributes ├── .github ├── CODEOWNERS └── workflows │ ├── ci.yml │ └── stale.yml ├── .gitignore ├── .markdownlint-cli2.yaml ├── .overcommit.yml ├── .vscode └── extensions.json ├── .yamllint ├── Berksfile ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Dangerfile ├── LICENSE ├── README.md ├── TESTING.md ├── attributes └── default.rb ├── chefignore ├── documentation └── .gitkeep ├── examples ├── data_bags │ └── firewall │ │ ├── apache2.json │ │ └── apache2__mod_ssl.json └── roles │ ├── fw_example.rb │ ├── fw_https.rb │ ├── securitylevel_green.rb │ ├── securitylevel_red.rb │ └── securitylevel_yellow.rb ├── kitchen.dokken.yml ├── kitchen.exec.yml ├── kitchen.global.yml ├── kitchen.yml ├── metadata.rb ├── recipes ├── databag.rb ├── default.rb ├── disable.rb ├── recipes.rb └── securitylevel.rb ├── renovate.json └── spec ├── spec_helper.rb └── unit └── recipes └── default_spec.rb /.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 | -------------------------------------------------------------------------------- /.envrc: -------------------------------------------------------------------------------- 1 | use chefworkstation 2 | export KITCHEN_GLOBAL_YAML=kitchen.global.yml 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @sous-chefs/maintainers 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: ci 3 | 4 | "on": 5 | pull_request: 6 | push: 7 | branches: [main] 8 | 9 | jobs: 10 | lint-unit: 11 | uses: sous-chefs/.github/.github/workflows/lint-unit.yml@3.1.1 12 | permissions: 13 | actions: write 14 | checks: write 15 | pull-requests: write 16 | statuses: write 17 | issues: write 18 | 19 | integration: 20 | needs: lint-unit 21 | runs-on: ubuntu-latest 22 | strategy: 23 | matrix: 24 | os: 25 | # - almalinux-8 26 | # - almalinux-9 27 | # - amazonlinux-2023 28 | # - centos-7 29 | # - centos-stream-8 30 | # - centos-stream-9 31 | - debian-10 32 | - debian-11 33 | - debian-12 34 | # - fedora-latest 35 | # - opensuse-leap-15 36 | # - rockylinux-8 37 | # - rockylinux-9 38 | - ubuntu-1804 39 | - ubuntu-2004 40 | - ubuntu-2204 41 | suite: 42 | - default 43 | fail-fast: false 44 | 45 | steps: 46 | - name: Check out code 47 | uses: actions/checkout@v4 48 | - name: Install Chef 49 | uses: actionshub/chef-install@main 50 | - name: Dokken 51 | uses: actionshub/test-kitchen@main 52 | env: 53 | CHEF_LICENSE: accept-no-persist 54 | KITCHEN_LOCAL_YAML: kitchen.dokken.yml 55 | with: 56 | suite: ${{ matrix.suite }} 57 | os: ${{ matrix.os }} 58 | - name: Print debug output on failure 59 | if: failure() 60 | run: | 61 | set -x 62 | sudo journalctl -l --since today 63 | KITCHEN_LOCAL_YAML=kitchen.dokken.yml /usr/bin/kitchen exec ${{ matrix.suite }}-${{ matrix.os }} -c "journalctl -l" 64 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Mark stale issues and pull requests 3 | 4 | "on": 5 | schedule: [cron: "0 0 * * *"] 6 | 7 | jobs: 8 | stale: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/stale@v9 12 | with: 13 | repo-token: ${{ secrets.GITHUB_TOKEN }} 14 | close-issue-message: > 15 | Closing due to inactivity. 16 | If this is still an issue please reopen or open another issue. 17 | Alternatively drop by the #sous-chefs channel on the [Chef Community Slack](http://community-slack.chef.io/) and we'll be happy to help! 18 | Thanks, Sous-Chefs. 19 | days-before-close: 7 20 | days-before-stale: 365 21 | stale-issue-message: > 22 | Marking stale due to inactivity. 23 | Remove stale label or comment or this will be closed in 7 days. 24 | Alternatively drop by the #sous-chefs channel on the [Chef Community Slack](http://community-slack.chef.io/) and we'll be happy to help! 25 | Thanks, Sous-Chefs. 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.rbc 2 | .config 3 | InstalledFiles 4 | pkg 5 | test/tmp 6 | test/version_tmp 7 | tmp 8 | _Store 9 | *~ 10 | *# 11 | .#* 12 | \#*# 13 | *.un~ 14 | *.tmp 15 | *.bk 16 | *.bkup 17 | 18 | # editor files 19 | .idea 20 | .*.sw[a-z] 21 | 22 | # ruby/bundler/rspec files 23 | .ruby-version 24 | .ruby-gemset 25 | .rvmrc 26 | Gemfile.lock 27 | .bundle 28 | *.gem 29 | coverage 30 | spec/reports 31 | 32 | # YARD / rdoc artifacts 33 | .yardoc 34 | _yardoc 35 | doc/ 36 | rdoc 37 | 38 | # chef infra stuff 39 | Berksfile.lock 40 | .kitchen 41 | kitchen.local.yml 42 | vendor/ 43 | .coverage/ 44 | .zero-knife.rb 45 | Policyfile.lock.json 46 | 47 | # vagrant stuff 48 | .vagrant/ 49 | .vagrant.d/ 50 | -------------------------------------------------------------------------------- /.markdownlint-cli2.yaml: -------------------------------------------------------------------------------- 1 | config: 2 | ul-indent: false # MD007 3 | line-length: false # MD013 4 | no-duplicate-heading: false # MD024 5 | reference-links-images: false # MD052 6 | ignores: 7 | - .github/copilot-instructions.md 8 | -------------------------------------------------------------------------------- /.overcommit.yml: -------------------------------------------------------------------------------- 1 | --- 2 | PreCommit: 3 | TrailingWhitespace: 4 | enabled: true 5 | YamlLint: 6 | enabled: true 7 | required_executable: "yamllint" 8 | ChefSpec: 9 | enabled: true 10 | required_executable: "chef" 11 | command: ["chef", "exec", "rspec"] 12 | Cookstyle: 13 | enabled: true 14 | required_executable: "cookstyle" 15 | command: ["cookstyle"] 16 | MarkdownLint: 17 | enabled: false 18 | required_executable: "npx" 19 | command: ["npx", "markdownlint-cli2", "'**/*.md'"] 20 | include: ["**/*.md"] 21 | 22 | CommitMsg: 23 | HardTabs: 24 | enabled: true 25 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "chef-software.chef", 4 | "rebornix.ruby", 5 | "editorconfig.editorconfig", 6 | "DavidAnson.vscode-markdownlint" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | rules: 4 | line-length: 5 | max: 256 6 | level: warning 7 | document-start: disable 8 | braces: 9 | forbid: false 10 | min-spaces-inside: 0 11 | max-spaces-inside: 1 12 | min-spaces-inside-empty: -1 13 | max-spaces-inside-empty: -1 14 | comments: 15 | min-spaces-from-content: 1 16 | -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | 5 | group :integration do 6 | cookbook 'apt' 7 | end 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # ufw Cookbook CHANGELOG 2 | 3 | This file is used to list changes made in each version of the ufw cookbook. 4 | 5 | ## Unreleased 6 | 7 | ## 4.0.8 - *2024-11-18* 8 | 9 | Standardise files with files in sous-chefs/repo-management 10 | 11 | Standardise files with files in sous-chefs/repo-management 12 | 13 | ## 4.0.7 - *2024-07-15* 14 | 15 | Standardise files with files in sous-chefs/repo-management 16 | 17 | Standardise files with files in sous-chefs/repo-management 18 | 19 | Standardise files with files in sous-chefs/repo-management 20 | 21 | ## 4.0.6 - *2024-05-03* 22 | 23 | ## 4.0.5 - *2024-05-03* 24 | 25 | ## 4.0.4 - *2023-09-28* 26 | 27 | ## 4.0.3 - *2023-09-11* 28 | 29 | ## 4.0.2 - *2023-07-10* 30 | 31 | ## 4.0.1 - *2023-05-17* 32 | 33 | ## 4.0.0 - *2023-04-25* 34 | 35 | - Finalaize Sous-Chefs adoption 36 | - Update workflow to 2.0.2 37 | - Require Chef 15.3 38 | - Change node.normal for node.default 39 | - Chef/Correctness/NodeNormal: Do not use node.normal. Replace with default/override/force_default/force_override attribute levels. () 40 | 41 | ## 3.2.14 - *2023-04-07* 42 | 43 | Standardise files with files in sous-chefs/repo-management 44 | 45 | ## 3.2.13 - *2023-04-01* 46 | 47 | Standardise files with files in sous-chefs/repo-management 48 | 49 | ## 3.2.12 - *2023-04-01* 50 | 51 | Standardise files with files in sous-chefs/repo-management 52 | 53 | ## 3.2.11 - *2023-03-20* 54 | 55 | Standardise files with files in sous-chefs/repo-management 56 | 57 | ## 3.2.10 - *2023-03-15* 58 | 59 | Standardise files with files in sous-chefs/repo-management 60 | 61 | ## 3.2.9 - *2023-02-27* 62 | 63 | ## 3.2.8 - *2023-02-27* 64 | 65 | ## 3.2.7 - *2023-02-23* 66 | 67 | Standardise files with files in sous-chefs/repo-management 68 | 69 | ## 3.2.6 - *2023-02-15* 70 | 71 | ## 3.2.5 - *2023-02-15* 72 | 73 | Standardise files with files in sous-chefs/repo-management 74 | 75 | ## 3.2.4 - *2022-12-15* 76 | 77 | Standardise files with files in sous-chefs/repo-management 78 | 79 | Standardise files with files in sous-chefs/repo-management 80 | 81 | Standardise files with files in sous-chefs/repo-management 82 | 83 | ## 3.2.3 - *2021-08-30* 84 | 85 | - Standardise files with files in sous-chefs/repo-management 86 | 87 | ## 3.2.2 - *2021-06-01* 88 | 89 | - resolved cookstyle error: recipes/default.rb:36:15 convention: `Style/HashEachMethods` 90 | - resolved cookstyle error: recipes/default.rb:44:7 convention: `Style/HashEachMethods` 91 | 92 | ## 3.2.1 (2018-10-04) 93 | 94 | - Update README.md formatting 95 | 96 | ## 3.2.0 (2018-07-24) 97 | 98 | - allow rules attribute to be specified as Hash 99 | 100 | ## 3.1.1 (2018-01-03) 101 | 102 | - Fix failure in recipes recipe from issue #21 103 | - Update apache2 license string 104 | - Call 'concat' on an array instead of on the node object 105 | 106 | ## 3.1.0 (2017-03-02) 107 | 108 | - Add use of the default['firewall']['allow_ssh'] attribute in the default recipe. Default for this cookbook is set to true, as the default recipe assumed that ssh would be enabled. 109 | 110 | ## 3.0.0 (2017-03-01) 111 | 112 | - Require Chef 12.4 (Depends on firewall which requires Chef 12.4+ at this point) 113 | - Update default to remove installation of ufw which is duplication from firewall cookbook, and remove state changes 114 | - Due to the change in default recipe, bumping major version in case this is breaking change for some. 115 | - Added debian platform as firewall cookbook supports ufw on debian 116 | 117 | ## 2.0.0 (2016-11-25) 118 | 119 | - Add chef_version metadata + remove chef 11 compat 120 | - Replace node.set with node.normal 121 | - Require Chef 12.1 122 | - Fix the recipe to properly converge 123 | 124 | ## v1.0.0 (12-14-2015) 125 | 126 | - Update to use / require the Firewall v2.0.0+ cookbook, which requires Chef 12 127 | - Updated all Opscode references to Chef Software Inc. 128 | - Updated testing, contributing, and maintainers docs 129 | - Added source_url and issues_url metadata for supermarket 130 | - Resolved all rubocop warnings and add the standard Chef rubocop file 131 | - Added travis and supermarket version badges to the readme 132 | - Added requirements section to the readme 133 | - Added a chefignore file 134 | - Added a Rakefile for simplified testing 135 | - Added a basic converge chefspec 136 | 137 | ## v0.7.4 138 | 139 | No change. Version bump for toolchain 140 | 141 | ## v0.7.2 142 | 143 | Updating metadata to depend on firewall >= 0.9 144 | 145 | ## v0.7.0 146 | 147 | - [COOK-3592] - allow source ports to be defined as a range in ufw 148 | 149 | ## v0.6.4 150 | 151 | ### Bug 152 | 153 | - [COOK-3316] - Fix README.md example 154 | 155 | ## v0.6.2 156 | 157 | ### Bug 158 | 159 | - [COOK-2487]: when setting a node attribute you must specify the precedence 160 | - [COOK-2982]: ufw cookbook has foodcritic failures 161 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Community Guidelines 2 | 3 | This project follows the Chef Community Guidelines 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Please refer to 4 | [https://github.com/chef-cookbooks/community_cookbook_documentation/blob/main/CONTRIBUTING.MD](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/main/CONTRIBUTING.MD) 5 | -------------------------------------------------------------------------------- /Dangerfile: -------------------------------------------------------------------------------- 1 | # Reference: http://danger.systems/reference.html 2 | 3 | # A pull request summary is required. Add a description of the pull request purpose. 4 | # Changelog must be updated for each pull request that changes code. 5 | # Warnings will be issued for: 6 | # Pull request with more than 400 lines of code changed 7 | # Pull reqest that change more than 5 lines without test changes 8 | # Failures will be issued for: 9 | # Pull request without summary 10 | # Pull requests with code changes without changelog entry 11 | 12 | def code_changes? 13 | code = %w(libraries attributes recipes resources files templates) 14 | code.each do |location| 15 | return true unless git.modified_files.grep(/#{location}/).empty? 16 | end 17 | false 18 | end 19 | 20 | def test_changes? 21 | tests = %w(spec test kitchen.yml kitchen.dokken.yml) 22 | tests.each do |location| 23 | return true unless git.modified_files.grep(/#{location}/).empty? 24 | end 25 | false 26 | end 27 | 28 | failure 'Please provide a summary of your Pull Request.' if github.pr_body.length < 10 29 | 30 | warn 'This is a big Pull Request.' if git.lines_of_code > 400 31 | 32 | warn 'This is a Table Flip.' if git.lines_of_code > 2000 33 | 34 | # Require a CHANGELOG entry for non-test changes. 35 | if !git.modified_files.include?('CHANGELOG.md') && code_changes? 36 | failure 'Please include a CHANGELOG entry.' 37 | end 38 | 39 | # Require Major Minor Patch version labels 40 | unless github.pr_labels.grep /minor|major|patch/i 41 | warn 'Please add a release label to this pull request' 42 | end 43 | 44 | # A sanity check for tests. 45 | if git.lines_of_code > 5 && code_changes? && !test_changes? 46 | warn 'This Pull Request is probably missing tests.' 47 | end 48 | -------------------------------------------------------------------------------- /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 | # Description 2 | 3 | Configures Uncomplicated Firewall (ufw) on Ubuntu and Debian. Including the `ufw` recipe in a run list means the firewall will be enabled and will deny everything except SSH and ICMP ping by default. 4 | 5 | Rules may be added to the node by adding them to the `['firewall']['rules']` attributes in roles or on the node directly. The `firewall` cookbook has an LWRP that may be used to apply rules directly from other recipes as well. There is no need to explicitly remove rules, they are reevaluated on changes and reset. Rules are applied in the order of the run list, unless ordering is explicitly added. 6 | 7 | ## Requirements 8 | 9 | ### Platforms 10 | 11 | - Ubuntu 12 | - Debian 13 | 14 | ### Chef 15 | 16 | - Chef 15.3 17 | 18 | ### Cookbooks 19 | 20 | - firewall 2.0+ 21 | 22 | ## Recipes 23 | 24 | ### default 25 | 26 | The `default` recipe looks for the list of firewall rules to apply from the `['firewall']['rules']` attribute added to roles and on the node itself. The list of rules is then applied to the node in the order specified. 27 | 28 | ### disable 29 | 30 | The `disable` recipe is used if there is a need to disable the existing firewall, perhaps for testing. It disables the ufw firewall even if other ufw recipes attempt to enable it. 31 | 32 | If you remove this recipe, the firewall does not get automatically re-enabled. You will need clear the value of the `['firewall']['state']` to force a recalculation of the firewall rules. This can be done with `knife node edit`. 33 | 34 | ### databag 35 | 36 | The `databag` recipe looks in the `firewall` data bag for to apply firewall rules based on inspecting the runlist for roles and recipe names for keys that map to the data bag items and are applied in the the order specified. 37 | 38 | The `databag` recipe calls the `default` recipe after the `['firewall']['rules']` attribute is set to apply the rules, so you may mix roles with databag items if you want (roles apply first, then data bag contents). 39 | 40 | ### recipes 41 | 42 | The `recipes` recipe applies firewall rules based on inspecting the runlist for recipes that have node[\]['firewall']['rules'] attributes. These are appended to node['firewall']['rules'] and applied to the node. Cookbooks may define attributes for recipes like so: 43 | 44 | #### attributes/default.rb for test cookbook 45 | 46 | ```ruby 47 | default['test']['firewall']['rules'] = [ 48 | {"test"=> { 49 | "port"=> "27901", 50 | "protocol"=> "udp" 51 | } 52 | } 53 | ] 54 | default['test::awesome']['firewall']['rules'] = [ 55 | {"awesome"=> { 56 | "port"=> "99427", 57 | "protocol"=> "udp" 58 | } 59 | }, 60 | {"awesome2"=> { 61 | "port"=> "99428" 62 | } 63 | } 64 | ] 65 | ``` 66 | 67 | Note that the 'test::awesome' rules are only applied if that specific recipe is in the runlist. Recipe-applied firewall rules are applied after any rules defined in role attributes. 68 | 69 | ### securitylevel 70 | 71 | The `securitylevel` recipe is used if there are any node['firewall']['securitylevel'] settings that need to be enforced. It is a reference implementation with nothing configured. 72 | 73 | ## Attributes 74 | 75 | Roles and the node may have the `['firewall']['rules']` attribute set. This attribute is a list of hashes, the key will be rule name, the value will be the hash of parameters. Application order is based on run list. 76 | 77 | ### Example Role 78 | 79 | ```ruby 80 | name "fw_example" 81 | description "Firewall rules for Examples" 82 | override_attributes( 83 | "firewall" => { 84 | "rules" => [ 85 | {"tftp" => {}}, 86 | {"http" => { 87 | "port" => "80" 88 | } 89 | }, 90 | {"block tomcat from 192.168.1.0/24" => { 91 | "port" => "8080", 92 | "source" => "192.168.1.0/24", 93 | "action" => "deny" 94 | } 95 | }, 96 | {"Allow access to udp 1.2.3.4 port 5469 from 1.2.3.5 port 5469" => { 97 | "protocol" => "udp", 98 | "port" => "5469", 99 | "source" => "1.2.3.4", 100 | "destination" => "1.2.3.5", 101 | "dest_port" => "5469" 102 | } 103 | }, 104 | {"allow to tcp ports 8000-8010 from 192.168.1.0/24" => { 105 | "port_range" => "8000..8010", 106 | "source" => "192.168.1.0/24", 107 | "protocol" => "tcp" //protocol is mandatory when using port ranges 108 | } 109 | } 110 | ] 111 | } 112 | ) 113 | ``` 114 | 115 | - default['firewall']['allow_ssh'] Opens port 22 for SSH when set to true. Default set to true. 116 | 117 | ## Data Bags 118 | 119 | The `firewall` data bag may be used with the `databag` recipe. It will contain items that map to role names (eg. the 'apache' role will map to the 'apache' item in the 'firewall' data bag). Either roles or recipes may be keys (role[webserver] is 'webserver', recipe[apache2] is 'apache2'). If you have recipe-specific firewall rules, you will need to replace the '::' with '**' (double underscores) (eg. recipe[apache2::mod_ssl] is 'apache2**mod_ssl' in the data bag item). 120 | 121 | The items in the data bag will contain a 'rules' array of hashes to apply to the `['firewall']['rules']` attribute. 122 | 123 | ```shell 124 | % knife data bag create firewall 125 | % knife data bag from file firewall examples/data_bags/firewall/apache2.json 126 | % knife data bag from file firewall examples/data_bags/firewall/apache2__mod_ssl.json 127 | ``` 128 | 129 | ### Example 'firewall' data bag item 130 | 131 | ```javascript 132 | { 133 | "id": "apache2", 134 | "rules": [ 135 | {"http": { 136 | "port": "80" 137 | }}, 138 | {"block http from 192.168.1.0/24": { 139 | "port": "80", 140 | "source": "192.168.1.0/24", 141 | "action": "deny" 142 | }} 143 | ] 144 | } 145 | ``` 146 | 147 | ## Resources/Providers 148 | 149 | The `firewall` cookbook provides the `firewall` and `firewall_rule` LWRPs, for which there is a ufw provider. 150 | 151 | ## License & Authors 152 | 153 | **Author:** Cookbook Engineering Team ([cookbooks@chef.io](mailto:cookbooks@chef.io)) 154 | 155 | **Copyright:** 2011-2014, Chef Software, Inc. 156 | 157 | ```text 158 | Licensed under the Apache License, Version 2.0 (the "License"); 159 | you may not use this file except in compliance with the License. 160 | You may obtain a copy of the License at 161 | 162 | http://www.apache.org/licenses/LICENSE-2.0 163 | 164 | Unless required by applicable law or agreed to in writing, software 165 | distributed under the License is distributed on an "AS IS" BASIS, 166 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 167 | See the License for the specific language governing permissions and 168 | limitations under the License. 169 | ``` 170 | -------------------------------------------------------------------------------- /TESTING.md: -------------------------------------------------------------------------------- 1 | # Testing 2 | 3 | Please refer to [the community cookbook documentation on testing](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/main/TESTING.MD). 4 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | default['firewall']['rules'] = [] 2 | default['firewall']['securitylevel'] = '' 3 | default['firewall']['allow_ssh'] = true 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /documentation/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sous-chefs/ufw/20a1e9e34ebfa706c89093c0719e11cf9c7c98c2/documentation/.gitkeep -------------------------------------------------------------------------------- /examples/data_bags/firewall/apache2.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "apache2", 3 | "rules": [ 4 | {"http": { 5 | "port": "80" 6 | }}, 7 | {"block http from 192.168.1.0/24": { 8 | "port": "80", 9 | "source": "192.168.1.0/24", 10 | "action": "deny" 11 | }} 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /examples/data_bags/firewall/apache2__mod_ssl.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "apache2__mod_ssl", 3 | "rules": [ 4 | {"http": { 5 | "port": "443" 6 | }} 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /examples/roles/fw_example.rb: -------------------------------------------------------------------------------- 1 | name 'fw_example' 2 | description 'Firewall rules for Examples' 3 | override_attributes( 4 | 'firewall' => { 5 | 'rules' => [ 6 | { 'tftp' => {} }, 7 | { 'http' => { 8 | 'port' => '80', 9 | }, 10 | }, 11 | { 'block tomcat from 192.168.1.0/24' => { 12 | 'port' => '8080', 13 | 'source' => '192.168.1.0/24', 14 | 'action' => 'deny', 15 | }, 16 | }, 17 | { 'Allow access to udp 1.2.3.4 port 5469 from 1.2.3.5 port 5469' => { 18 | 'protocol' => 'udp', 19 | 'port' => '5469', 20 | 'source' => '1.2.3.4', 21 | 'destination' => '1.2.3.5', 22 | 'dest_port' => '5469', 23 | }, 24 | }, 25 | ], 26 | } 27 | ) 28 | -------------------------------------------------------------------------------- /examples/roles/fw_https.rb: -------------------------------------------------------------------------------- 1 | name 'fw_https' 2 | description 'Firewall rules for https' 3 | override_attributes( 4 | 'firewall' => { 5 | 'rules' => [ 6 | { 'https' => { 7 | 'port' => '443', 8 | }, 9 | }, 10 | ], 11 | } 12 | ) 13 | -------------------------------------------------------------------------------- /examples/roles/securitylevel_green.rb: -------------------------------------------------------------------------------- 1 | name 'securitylevel_green' 2 | description "Security level 'green'" 3 | override_attributes( 4 | 'firewall' => { 5 | 'securitylevel' => 'green', 6 | } 7 | ) 8 | -------------------------------------------------------------------------------- /examples/roles/securitylevel_red.rb: -------------------------------------------------------------------------------- 1 | name 'securitylevel_red' 2 | description "Security level 'red'" 3 | override_attributes( 4 | 'firewall' => { 5 | 'securitylevel' => 'red', 6 | } 7 | ) 8 | -------------------------------------------------------------------------------- /examples/roles/securitylevel_yellow.rb: -------------------------------------------------------------------------------- 1 | name 'securitylevel_yellow' 2 | description "Security level 'yellow'" 3 | override_attributes( 4 | 'firewall' => { 5 | 'securitylevel' => 'yellow', 6 | } 7 | ) 8 | -------------------------------------------------------------------------------- /kitchen.dokken.yml: -------------------------------------------------------------------------------- 1 | driver: 2 | name: dokken 3 | privileged: true 4 | chef_version: <%= ENV['CHEF_VERSION'] || 'current' %> 5 | 6 | transport: { name: dokken } 7 | provisioner: { name: dokken } 8 | 9 | platforms: 10 | - name: almalinux-8 11 | driver: 12 | image: dokken/almalinux-8 13 | pid_one_command: /usr/lib/systemd/systemd 14 | 15 | - name: almalinux-9 16 | driver: 17 | image: dokken/almalinux-9 18 | pid_one_command: /usr/lib/systemd/systemd 19 | 20 | - name: almalinux-10 21 | driver: 22 | image: dokken/almalinux-10 23 | pid_one_command: /usr/lib/systemd/systemd 24 | 25 | - name: amazonlinux-2023 26 | driver: 27 | image: dokken/amazonlinux-2023 28 | pid_one_command: /usr/lib/systemd/systemd 29 | 30 | - name: centos-stream-9 31 | driver: 32 | image: dokken/centos-stream-9 33 | pid_one_command: /usr/lib/systemd/systemd 34 | 35 | - name: centos-stream-10 36 | driver: 37 | image: dokken/centos-stream-10 38 | pid_one_command: /usr/lib/systemd/systemd 39 | 40 | - name: debian-11 41 | driver: 42 | image: dokken/debian-11 43 | pid_one_command: /bin/systemd 44 | 45 | - name: debian-12 46 | driver: 47 | image: dokken/debian-12 48 | pid_one_command: /bin/systemd 49 | 50 | - name: fedora-latest 51 | driver: 52 | image: dokken/fedora-latest 53 | pid_one_command: /usr/lib/systemd/systemd 54 | 55 | - name: opensuse-leap-15 56 | driver: 57 | image: dokken/opensuse-leap-15 58 | pid_one_command: /usr/lib/systemd/systemd 59 | 60 | - name: oraclelinux-8 61 | driver: 62 | image: dokken/oraclelinux-8 63 | pid_one_command: /usr/lib/systemd/systemd 64 | 65 | - name: oraclelinux-9 66 | driver: 67 | image: dokken/oraclelinux-9 68 | pid_one_command: /usr/lib/systemd/systemd 69 | 70 | - name: rockylinux-8 71 | driver: 72 | image: dokken/rockylinux-8 73 | pid_one_command: /usr/lib/systemd/systemd 74 | 75 | - name: rockylinux-9 76 | driver: 77 | image: dokken/rockylinux-9 78 | pid_one_command: /usr/lib/systemd/systemd 79 | 80 | - name: ubuntu-20.04 81 | driver: 82 | image: dokken/ubuntu-20.04 83 | pid_one_command: /bin/systemd 84 | 85 | - name: ubuntu-22.04 86 | driver: 87 | image: dokken/ubuntu-22.04 88 | pid_one_command: /bin/systemd 89 | 90 | - name: ubuntu-24.04 91 | driver: 92 | image: dokken/ubuntu-24.04 93 | pid_one_command: /bin/systemd 94 | -------------------------------------------------------------------------------- /kitchen.exec.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: { name: exec } 3 | transport: { name: exec } 4 | 5 | platforms: 6 | - name: macos-latest 7 | - name: windows-latest 8 | -------------------------------------------------------------------------------- /kitchen.global.yml: -------------------------------------------------------------------------------- 1 | --- 2 | provisioner: 3 | name: chef_infra 4 | product_name: chef 5 | product_version: <%= ENV['CHEF_VERSION'] || 'latest' %> 6 | channel: stable 7 | install_strategy: once 8 | chef_license: accept 9 | enforce_idempotency: <%= ENV['ENFORCE_IDEMPOTENCY'] || true %> 10 | multiple_converge: <%= ENV['MULTIPLE_CONVERGE'] || 2 %> 11 | deprecations_as_errors: true 12 | log_level: <%= ENV['CHEF_LOG_LEVEL'] || 'auto' %> 13 | 14 | verifier: 15 | name: inspec 16 | 17 | platforms: 18 | - name: almalinux-8 19 | - name: almalinux-9 20 | - name: amazonlinux-2023 21 | - name: centos-stream-9 22 | - name: debian-11 23 | - name: debian-12 24 | - name: fedora-latest 25 | - name: opensuse-leap-15 26 | - name: oraclelinux-8 27 | - name: oraclelinux-9 28 | - name: rockylinux-8 29 | - name: rockylinux-9 30 | - name: ubuntu-20.04 31 | - name: ubuntu-22.04 32 | - name: ubuntu-24.04 33 | -------------------------------------------------------------------------------- /kitchen.yml: -------------------------------------------------------------------------------- 1 | driver: 2 | name: vagrant 3 | 4 | provisioner: 5 | name: chef_infra 6 | product_name: chef 7 | # enforce_idempotency: true #fails on the firewall_rule resource 8 | # multiple_converge: 2 9 | deprecations_as_errors: true 10 | 11 | verifier: 12 | name: inspec 13 | 14 | platforms: 15 | - debian-10 16 | - debian-11 17 | - debian-12 18 | - ubuntu-1804 19 | - ubuntu-2004 20 | - ubuntu-2204 21 | 22 | suites: 23 | - name: default 24 | run_list: 25 | - recipe[ufw::default] 26 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name 'ufw' 2 | maintainer 'Sous Chefs' 3 | maintainer_email 'help@sous-chefs.org' 4 | license 'Apache-2.0' 5 | description 'Installs and configures Uncomplicated Firewall (ufw)' 6 | version '4.0.8' 7 | source_url 'https://github.com/sous-chefs/ufw' 8 | issues_url 'https://github.com/sous-chefs/ufw/issues' 9 | chef_version '>= 15.3' 10 | 11 | supports 'ubuntu' 12 | supports 'debian' 13 | 14 | depends 'firewall', '>= 2.0' 15 | -------------------------------------------------------------------------------- /recipes/databag.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Matt Ray 3 | # Cookbook:: ufw 4 | # Recipe:: databag 5 | # 6 | # Copyright:: 2011-2019, Chef Software, Inc. 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | # flatten the run_list to just the names of the roles and recipes in order 22 | def run_list_names(run_list) 23 | names = [] 24 | run_list.each do |entry| 25 | Chef::Log.debug "ufw::databag:run_list_names+name: #{entry.name}" 26 | if entry.name.index('::') # cookbook::recipe 27 | names.push(entry.name.sub('::', '__')) 28 | else 29 | names.push(entry.name) 30 | end 31 | if entry.role? 32 | rol = search(:role, "name:#{entry.name}")[0] 33 | names.concat(run_list_names(rol.run_list)) 34 | end 35 | end 36 | Chef::Log.debug "ufw::databag:run_list_names+names: #{names}" 37 | names 38 | end 39 | 40 | rlist = run_list_names(node.run_list) 41 | rlist.uniq! 42 | Chef::Log.debug "ufw::databag:rlist: #{rlist}" 43 | 44 | fw_db = data_bag('firewall') 45 | Chef::Log.debug "ufw::databag:firewall:#{fw_db}" 46 | 47 | rlist.each do |entry| 48 | Chef::Log.debug "ufw::databag: \"#{entry}\"" 49 | next unless fw_db.member?(entry) 50 | 51 | # add the list of firewall rules to the current list 52 | item = data_bag_item('firewall', entry) 53 | rules = item['rules'] 54 | node.default['firewall']['rules'] = node.default['firewall']['rules'].to_a.concat(rules) unless rules.nil? 55 | end 56 | 57 | # now go apply the rules 58 | include_recipe 'ufw::default' 59 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Matt Ray 3 | # Cookbook:: ufw 4 | # Recipe:: default 5 | # 6 | # Copyright:: 2011-2019, Chef Software, Inc. 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | firewall 'default' do 22 | action :install 23 | end 24 | 25 | # leave this on by default 26 | firewall_rule 'ssh' do 27 | port 22 28 | action :create 29 | only_if { node['firewall']['allow_ssh'] } 30 | end 31 | 32 | # handle rules specified as array or as hash 33 | rules = {} 34 | if node['firewall']['rules'].is_a?(Array) 35 | node['firewall']['rules'].each do |rule_mash| 36 | rule_mash.each_key do |rule| 37 | rules[rule] = rule_mash[rule] 38 | end 39 | end 40 | elsif node['firewall']['rules'].is_a?(Hash) 41 | rules = node['firewall']['rules'] 42 | end 43 | 44 | rules.each_key do |rule| 45 | Chef::Log.debug "ufw:rule:name \"#{rule}\"" 46 | Chef::Log.debug "ufw:rule:parameters \"#{params}\"" 47 | Chef::Log.debug "ufw:rule:name #{params['name']}" if params['name'] 48 | Chef::Log.debug "ufw:rule:protocol #{params['protocol']}" if params['protocol'] 49 | Chef::Log.debug "ufw:rule:direction #{params['direction']}" if params['direction'] 50 | Chef::Log.debug "ufw:rule:interface #{params['interface']}" if params['interface'] 51 | Chef::Log.debug "ufw:rule:logging #{params['logging']}" if params['logging'] 52 | Chef::Log.debug "ufw:rule:port #{params['port']}" if params['port'] 53 | Chef::Log.debug "ufw:rule:port_range #{params['port_range']}" if params['port_range'] 54 | Chef::Log.debug "ufw:rule:source #{params['source']}" if params['source'] 55 | Chef::Log.debug "ufw:rule:destination #{params['destination']}" if params['destination'] 56 | Chef::Log.debug "ufw:rule:dest_port #{params['dest_port']}" if params['dest_port'] 57 | Chef::Log.debug "ufw:rule:position #{params['position']}" if params['position'] 58 | 59 | params = rules[rule] 60 | act = params['action'] 61 | act ||= 'create' 62 | 63 | raise 'ufw: port_range was specified to firewall_rule without protocol' if params['port_range'] && !params['protocol'] 64 | 65 | Chef::Log.debug "ufw:rule:action :#{act}" 66 | 67 | firewall_rule rule do 68 | firewall_name params['name'] if params['name'] 69 | protocol params['protocol'].to_sym if params['protocol'] 70 | direction params['direction'].to_sym if params['direction'] 71 | interface params['interface'] if params['interface'] 72 | logging params['logging'].to_sym if params['logging'] 73 | port params['port'].to_i if params['port'] 74 | if params['port_range'] 75 | ends = params['port_range'].split('..').map { |d| Integer(d) } 76 | port ends[0]..ends[1] 77 | end 78 | source params['source'] if params['source'] 79 | destination params['destination'] if params['destination'] 80 | dest_port params['dest_port'].to_i if params['dest_port'] 81 | position params['position'].to_i if params['position'] 82 | action act 83 | end 84 | end 85 | -------------------------------------------------------------------------------- /recipes/disable.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Matt Ray 3 | # Cookbook:: ufw 4 | # Recipe:: disable 5 | # 6 | # Copyright:: 2011-2019, Chef Software, Inc. 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | firewall 'ufw' do 22 | action :disable 23 | end 24 | -------------------------------------------------------------------------------- /recipes/recipes.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Matt Ray 3 | # Cookbook:: ufw 4 | # Recipe:: recipes 5 | # 6 | # Copyright:: 2011-2019, Chef Software, Inc. 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | # expand and parse the node's runlist for recipes and find attributes of the form node[]['firewall']['rules'] 22 | # append them to the node['firewall']['rules'] array attribute 23 | node.expand!.recipes.each do |recipe| 24 | Chef::Log.debug "ufw::recipes: #{recipe}" 25 | cookbook = recipe.split('::')[0] 26 | # get the cookbook attributes if there are any 27 | if recipe != cookbook && node[cookbook] && node[cookbook]['firewall'] && node[cookbook]['firewall']['rules'] 28 | rules = node[cookbook]['firewall']['rules'] 29 | Chef::Log.debug "ufw::recipes:#{cookbook}:rules #{rules}" 30 | node.default['firewall']['rules'] = node['firewall']['rules'].to_a.concat(rules) unless rules.nil? 31 | end 32 | # get the recipe attributes if there are any 33 | next unless node[recipe] && node[recipe]['firewall'] && node[recipe]['firewall']['rules'] 34 | 35 | rules = node[recipe]['firewall']['rules'] 36 | Chef::Log.debug "ufw::recipes:#{recipe}:rules #{rules}" 37 | node.default['firewall']['rules'] = node.default['firewall']['rules'].to_a.concat(rules) unless rules.nil? 38 | end 39 | 40 | # now go apply the rules 41 | include_recipe 'ufw::default' 42 | -------------------------------------------------------------------------------- /recipes/securitylevel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Matt Ray 3 | # Cookbook:: ufw 4 | # Recipe:: securitylevel 5 | # 6 | # Copyright:: 2011-2019, Chef Software, Inc. 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | securitylevel = node['firewall']['securitylevel'] 22 | 23 | Chef::Log.info "ufw::securitylevel:#{securitylevel}" 24 | 25 | # verify that only 1 "color" security group is applied" 26 | count = node.expand!.roles.count { |role| role =~ /SecurityLevel-(Red|Green|Yellow)/ } 27 | if count > 1 28 | raise Chef::Exceptions::AmbiguousRunlistSpecification, "conflicting SecurityLevel-'color' roles, only 1 may be applied." 29 | end 30 | 31 | # case securitylevel 32 | # when 'red' 33 | # # put special stuff for red here 34 | # when 'yellow' 35 | # # put special stuff for red here 36 | # when 'green' 37 | # # put special stuff for red here 38 | # end 39 | 40 | # now go apply the rules 41 | include_recipe 'ufw::default' 42 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": ["config:base"], 4 | "packageRules": [ 5 | { 6 | "groupName": "Actions", 7 | "matchUpdateTypes": ["minor", "patch", "pin"], 8 | "automerge": true, 9 | "addLabels": ["Release: Patch", "Skip: Announcements"] 10 | }, 11 | { 12 | "groupName": "Actions", 13 | "matchUpdateTypes": ["major"], 14 | "automerge": false, 15 | "addLabels": ["Release: Patch", "Skip: Announcements"] 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'chefspec' 2 | require 'chefspec/berkshelf' 3 | 4 | RSpec.configure do |config| 5 | config.color = true # Use color in STDOUT 6 | config.formatter = :documentation # Use the specified formatter 7 | config.log_level = :error # Avoid deprecation notice SPAM 8 | config.platform = 'ubuntu' 9 | config.version = '16.04' 10 | end 11 | -------------------------------------------------------------------------------- /spec/unit/recipes/default_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'ufw::default' do 4 | # context 'rules attribute is Array' do 5 | # cached(:chef_run) do 6 | # ChefSpec::ServerRunner.new do |node, _server| 7 | # node.default['firewall']['rules'] = [ 8 | # { 'http' => { 'port' => '80' } }, 9 | # { 'https' => { 'port' => '443' } }, 10 | # ] 11 | # end.converge(described_recipe) 12 | # end 13 | 14 | # it 'calls firewall rule for each ' do 15 | # expect(chef_run).to create_firewall_rule('http').with( 16 | # port: 80, 17 | # protocol: :tcp 18 | # ) 19 | # expect(chef_run).to create_firewall_rule('https').with( 20 | # port: 443, 21 | # protocol: :tcp 22 | # ) 23 | # end 24 | # end 25 | 26 | # context 'rules attribute is Hash' do 27 | # cached(:chef_run) do 28 | # ChefSpec::ServerRunner.new do |node, _server| 29 | # node.default['firewall']['rules'] = { 'http' => { 'port' => '80' } } 30 | # node.override['firewall']['rules'] = { 'https' => { 'port' => '443' } } 31 | # end.converge(described_recipe) 32 | # end 33 | 34 | # it 'calls firewall rule for each ' do 35 | # expect(chef_run).to create_firewall_rule('http').with( 36 | # port: 80, 37 | # protocol: :tcp 38 | # ) 39 | # expect(chef_run).to create_firewall_rule('https').with( 40 | # port: 443, 41 | # protocol: :tcp 42 | # ) 43 | # end 44 | # end 45 | end 46 | --------------------------------------------------------------------------------