PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/recipes/default.rb

https://gitlab.com/gitlab-cookbooks/gitlab-iptables
Ruby | 99 lines | 75 code | 10 blank | 14 comment | 10 complexity | 9a0085a354660412cf88efb494552248 MD5 | raw file
  1. #
  2. # Cookbook:: gitlab-iptables
  3. # Recipe:: default
  4. # License:: MIT
  5. #
  6. # Copyright:: (C) 2016 GitLab Inc.
  7. #
  8. def normalize(text)
  9. text_dup = text.dup
  10. text_dup.downcase!
  11. text_dup.tr(' ', '_')
  12. end
  13. # If we are on AWS, don't bother with iptables/ufw. This behavior can
  14. # be overruled if node['gitlab-iptables']['enable'] == true
  15. enable_iptables = node['gitlab-iptables']['enable'] ||
  16. !AwsHelper.aws?
  17. unless enable_iptables
  18. include_recipe 'ufw::disable' if platform_family?('debian')
  19. return
  20. end
  21. if platform_family?('rhel') && node['platform_version'] =~ /^7\./
  22. include_recipe 'gitlab-iptables::iptables-instead-of-firewalld'
  23. end
  24. # IPv4 rules
  25. iptables_ng_rule '1-ssh-allow' do
  26. rule '--protocol tcp --dport 22 -j ACCEPT'
  27. ip_version 4
  28. end
  29. # Allow rule for specific src/dest then reject rule for any
  30. rule_nr = 10
  31. firewall_rules = nil
  32. firewall_rules = node['firewall']['rules'] if node['firewall']
  33. firewall_rules ||= []
  34. firewall_rules.each do |rule|
  35. rule.each do |rule_name, rule_spec|
  36. source = "--source #{rule_spec['source']}" if rule_spec['source']
  37. destination = "--destination #{rule_spec['destination']}" if rule_spec['destination']
  38. protocols = rule_spec['protocol'] ? [rule_spec['protocol']] : %w(tcp udp)
  39. protocols.each do |protocol|
  40. iptables_ng_rule "#{rule_nr}-#{normalize(rule_name)}-#{protocol}-allow" do
  41. rule "--protocol #{protocol} #{source} #{destination} --dport #{rule_spec['port']} -j ACCEPT"
  42. ip_version 4
  43. end
  44. rule_nr += 1
  45. end
  46. end
  47. end
  48. iptables_ng_rule '998-log-before-reject' do
  49. rule '-j LOG --log-level debug --log-prefix "Reject unmatched packet " -m limit --limit 1/second --limit-burst 10'
  50. ip_version 4
  51. end
  52. # If not on Azure drop all traffic by default
  53. azure = false
  54. azure = true if node['kernel']['modules'].include?('hid_hyperv')
  55. unless azure
  56. iptables_ng_rule '1-lo-allow' do
  57. rule '-i lo -j ACCEPT'
  58. ip_version 4
  59. end
  60. iptables_ng_rule '1-icmp-allow' do
  61. rule '-p icmp -j ACCEPT'
  62. ip_version 4
  63. end
  64. iptables_ng_rule '1-state-rel-est-allow' do
  65. rule '-m state --state RELATED,ESTABLISHED -j ACCEPT'
  66. ip_version 4
  67. end
  68. iptables_ng_rule '999-all-drop' do
  69. rule '-j REJECT'
  70. ip_version 4
  71. end
  72. end
  73. # IPv6 rules
  74. iptables_ng_rule '999-all-drop-ip6' do
  75. rule '-j REJECT'
  76. ip_version 6
  77. end
  78. # Restart docker service which uses own iptables chains
  79. if File.exist?('/etc/init.d/docker') || File.exist?('/usr/lib/systemd/system/docker.service')
  80. service 'docker' do
  81. if platform?('ubuntu')
  82. if node['platform_version'].to_f >= 9.10 && node['platform_version'].to_f < 16.04
  83. provider Chef::Provider::Service::Upstart
  84. end
  85. end
  86. action :restart
  87. not_if 'iptables -L DOCKER'
  88. end
  89. end