PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/create_iptables_rules.rb

https://github.com/sewer2/cb-iptables-ng
Ruby | 78 lines | 36 code | 12 blank | 30 comment | 5 complexity | 60c799d089490881eda4721fab417c13 MD5 | raw file
  1. #
  2. # Cookbook Name:: iptables-ng
  3. # Recipe:: manage
  4. #
  5. # Copyright 2013, Chris Aumann
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # This was implemented as a internal-only provider.
  21. # Apparently, calling a LWRP from a LWRP doesnt' really work with
  22. # subscribes / notifies. Therefore, using this workaround.
  23. module Iptables
  24. module Manage
  25. def create_iptables_rules(ip_version)
  26. rules = {}
  27. # Retrieve all iptables rules for this ip_version,
  28. # as well as default policies
  29. Dir["/etc/iptables.d/*/*/*.rule_v#{ip_version}",
  30. '/etc/iptables.d/*/*/default'].each do |path|
  31. # /etc/iptables.d/#{table}/#{chain}/#{rule}.rule_v#{ip_version}
  32. table, chain, filename = path.split('/')[3..5]
  33. rule = ::File.basename(filename)
  34. # ipv6 doesn't support nat
  35. next if table == 'nat' and ip_version == 6
  36. # Create hashes unless they already exist, and add the rule
  37. rules[table] ||= {}
  38. rules[table][chain] ||= {}
  39. rules[table][chain][rule] = ::File.read(path)
  40. end
  41. iptables_restore = ''
  42. rules.each do |table, chains|
  43. iptables_restore << "*#{table}\n"
  44. # Get default policies and rules for this chain
  45. default_policies = chains.inject({}) {|new_chain, rule| new_chain[rule[0]] = rule[1].select{|k, v| k == 'default'}; new_chain }
  46. all_chain_rules = chains.inject({}) {|new_chain, rule| new_chain[rule[0]] = rule[1].reject{|k, v| k == 'default'}; new_chain }
  47. # Apply default policies first
  48. default_policies.each do |chain, policy|
  49. iptables_restore << ":#{chain} #{policy['default'].chomp}\n"
  50. end
  51. # Apply rules for this chain, but sort before adding
  52. all_chain_rules.each do |chain, chain_rules|
  53. chain_rules.sort.each { |r| iptables_restore << "#{r.last.chomp}\n" }
  54. end
  55. iptables_restore << "COMMIT\n"
  56. end
  57. Chef::Resource::File.new(node['iptables-ng']["script_ipv#{ip_version}"], run_context).tap do |file|
  58. file.owner('root')
  59. file.group('root')
  60. file.mode(00600)
  61. file.content(iptables_restore)
  62. file.run_action(:create)
  63. end
  64. end
  65. end
  66. end