itamae-plugin-resource-iptables 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +2 -0
- data/examples/firewall.rb +71 -0
- data/examples/rtmpsuck.rb +32 -0
- data/itamae-plugin-resource-iptables.gemspec +24 -0
- data/lib/itamae/plugin/resource/iptables.rb +17 -0
- data/lib/itamae/plugin/resource/iptables/basic_rule.rb +38 -0
- data/lib/itamae/plugin/resource/iptables/log_rule.rb +26 -0
- data/lib/itamae/plugin/resource/iptables/multiport_rule.rb +79 -0
- data/lib/itamae/plugin/resource/iptables/owner_rule.rb +34 -0
- data/lib/itamae/plugin/resource/iptables/state_rule.rb +27 -0
- data/lib/itamae/plugin/resource/iptables/version.rb +9 -0
- data/lib/itamae/plugin/resource/iptables_chain.rb +26 -0
- data/lib/itamae/plugin/resource/iptables_flush.rb +24 -0
- data/lib/itamae/plugin/resource/iptables_policy.rb +33 -0
- data/lib/itamae/plugin/resource/iptables_rule.rb +63 -0
- data/lib/itamae/plugin/resource/iptables_save.rb +21 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f317f0705017d3ee086290e31d0d3783da45a1c2
|
4
|
+
data.tar.gz: 5a47520726d648ef2c2b068c80be2aeff9b8ea07
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e3a0bb59121a2d19b1c5211c7687cf3f80ecc8ab50b1af8531e92d95cfde391c998139956b6dbf0fa010e7b564cf8cc5a65cf998fe70fb206fcf1c2a5bc37aa
|
7
|
+
data.tar.gz: 3deda76ba4a82ad8669c8ed20ac19ba71728f52eaad5b56de10d34903d43989de7d980a9e0d25f7748c0b1833d211e812e85bcb0f055b9b4bb0e83e4e0442139
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kohei Suzuki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Itamae::Plugin::Resource::Iptables
|
2
|
+
|
3
|
+
[itamae](https://github.com/ryotarai/itamae) resource plugin to manage iptables.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'itamae-plugin-resource-iptables'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install itamae-plugin-resource-iptables
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
- [examples/firewall.rb](examples/firewall.rb)
|
24
|
+
- [examples/rtmpsuck.rb](examples/rtmpsuck.rb)
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it ( https://github.com/eagletmt/itamae-plugin-resource-iptables/fork )
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'itamae/plugin/resource/iptables'
|
2
|
+
|
3
|
+
iptables_flush 'flush' do
|
4
|
+
end
|
5
|
+
|
6
|
+
iptables_policy 'INPUT' do
|
7
|
+
action :drop
|
8
|
+
end
|
9
|
+
|
10
|
+
iptables_policy 'OUTPUT' do
|
11
|
+
action :accept
|
12
|
+
end
|
13
|
+
|
14
|
+
iptables_policy 'FORWARD' do
|
15
|
+
action :drop
|
16
|
+
end
|
17
|
+
|
18
|
+
iptables_rule 'accept loopback' do
|
19
|
+
action :accept
|
20
|
+
chain 'INPUT'
|
21
|
+
in_interface 'lo'
|
22
|
+
end
|
23
|
+
|
24
|
+
iptables_rule 'accept ping' do
|
25
|
+
action :accept
|
26
|
+
chain 'INPUT'
|
27
|
+
protocol 'icmp'
|
28
|
+
end
|
29
|
+
|
30
|
+
iptables_rule 'accept related,established' do
|
31
|
+
action :accept
|
32
|
+
chain 'INPUT'
|
33
|
+
state %w[RELATED ESTABLISHED]
|
34
|
+
end
|
35
|
+
|
36
|
+
iptables_rule 'accept from local' do
|
37
|
+
action :accept
|
38
|
+
chain 'INPUT'
|
39
|
+
source '192.168.10.0/24'
|
40
|
+
end
|
41
|
+
|
42
|
+
iptables_chain 'SSH_LOGGING' do
|
43
|
+
end
|
44
|
+
|
45
|
+
iptables_rule 'chain ssh' do
|
46
|
+
action :create
|
47
|
+
chain 'INPUT'
|
48
|
+
protocol 'tcp'
|
49
|
+
dport 22
|
50
|
+
state %w[NEW]
|
51
|
+
jump 'SSH_LOGGING'
|
52
|
+
end
|
53
|
+
|
54
|
+
iptables_rule 'log ssh' do
|
55
|
+
action :log
|
56
|
+
chain 'SSH_LOGGING'
|
57
|
+
log_prefix '[ssh] '
|
58
|
+
log_level 'info'
|
59
|
+
end
|
60
|
+
|
61
|
+
iptables_rule 'accept ssh' do
|
62
|
+
action :accept
|
63
|
+
chain 'SSH_LOGGING'
|
64
|
+
end
|
65
|
+
|
66
|
+
iptables_save '/etc/iptables/iptables.rules' do
|
67
|
+
end
|
68
|
+
|
69
|
+
service 'iptables.service' do
|
70
|
+
action [:enable, :start]
|
71
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'itamae/plugin/resource/iptables'
|
2
|
+
|
3
|
+
iptables_flush 'flush' do
|
4
|
+
end
|
5
|
+
|
6
|
+
iptables_policy 'INPUT' do
|
7
|
+
action :accept
|
8
|
+
end
|
9
|
+
|
10
|
+
iptables_policy 'OUTPUT' do
|
11
|
+
action :accept
|
12
|
+
end
|
13
|
+
|
14
|
+
iptables_policy 'FORWARD' do
|
15
|
+
action :accept
|
16
|
+
end
|
17
|
+
|
18
|
+
iptables_rule 'redirect to rtmpsuck' do
|
19
|
+
action :redirect
|
20
|
+
table 'nat'
|
21
|
+
chain 'OUTPUT'
|
22
|
+
protocol 'tcp'
|
23
|
+
dport 1935
|
24
|
+
not_uid_owner 'rtmpsuck'
|
25
|
+
end
|
26
|
+
|
27
|
+
iptables_save '/etc/iptables/iptables.rules' do
|
28
|
+
end
|
29
|
+
|
30
|
+
service 'iptables.service' do
|
31
|
+
action [:enable, :start]
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'itamae/plugin/resource/iptables/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "itamae-plugin-resource-iptables"
|
8
|
+
spec.version = Itamae::Plugin::Resource::Iptables::VERSION
|
9
|
+
spec.authors = ["Kohei Suzuki"]
|
10
|
+
spec.email = ["eagletmt@gmail.com"]
|
11
|
+
spec.summary = %q{itamae resource plugin to manage iptables.}
|
12
|
+
spec.description = %q{itamae resource plugin to manage iptables.}
|
13
|
+
spec.homepage = "https://github.com/eagletmt/itamae-plugin-resource-iptables"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "itamae"
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'itamae/plugin/resource/iptables/version'
|
2
|
+
|
3
|
+
require 'itamae/plugin/resource/iptables_chain'
|
4
|
+
require 'itamae/plugin/resource/iptables_flush'
|
5
|
+
require 'itamae/plugin/resource/iptables_policy'
|
6
|
+
require 'itamae/plugin/resource/iptables_rule'
|
7
|
+
require 'itamae/plugin/resource/iptables_save'
|
8
|
+
|
9
|
+
module Itamae
|
10
|
+
module Plugin
|
11
|
+
module Resource
|
12
|
+
module Iptables
|
13
|
+
# Your code goes here...
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Itamae
|
2
|
+
module Plugin
|
3
|
+
module Resource
|
4
|
+
module Iptables
|
5
|
+
module BasicRule
|
6
|
+
RULES = {
|
7
|
+
protocol: { type: String },
|
8
|
+
source: { type: String },
|
9
|
+
destination: { type: String },
|
10
|
+
in_interface: { type: String },
|
11
|
+
out_interface: { type: String },
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.prepended(base)
|
15
|
+
base.class_eval do
|
16
|
+
RULES.each do |key, opts|
|
17
|
+
define_attribute key, opts
|
18
|
+
define_attribute :"not_#{key}", opts
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_rule(attrs)
|
24
|
+
rule = []
|
25
|
+
RULES.each_key.map(&:to_s).each do |key|
|
26
|
+
if attrs.has_key?(key)
|
27
|
+
rule << "--#{key.gsub('_', '-')}" << attrs[key]
|
28
|
+
elsif attrs.has_key?("not_#{key}")
|
29
|
+
rule << "--#{key.gsub('_', '-')}" << '!' << attrs[key]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
super + rule
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Itamae
|
2
|
+
module Plugin
|
3
|
+
module Resource
|
4
|
+
module Iptables
|
5
|
+
module LogRule
|
6
|
+
def self.prepended(base)
|
7
|
+
base.class_eval do
|
8
|
+
define_attribute :log_level, type: String
|
9
|
+
define_attribute :log_prefix, type: String
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_rule(attrs)
|
14
|
+
rule = []
|
15
|
+
%w[log_level log_prefix].each do |key|
|
16
|
+
if attrs.has_key?(key)
|
17
|
+
rule << "--#{key.gsub('_', '-')}" << attrs[key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
super + rule
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Itamae
|
2
|
+
module Plugin
|
3
|
+
module Resource
|
4
|
+
module Iptables
|
5
|
+
module MultiportRule
|
6
|
+
def self.prepended(base)
|
7
|
+
base.class_eval do
|
8
|
+
[:sport, :dport].each do |key|
|
9
|
+
define_attribute key, type: [Fixnum, Range, Array]
|
10
|
+
define_attribute :"not_#{key}", type: [Fixnum, Range, Array]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_rule(attrs)
|
16
|
+
rule = super
|
17
|
+
spec = PortSpecifier.new
|
18
|
+
%w[dport sport].each do |key|
|
19
|
+
if port = attrs[key]
|
20
|
+
spec.and(key, port)
|
21
|
+
elsif not_port = attrs["not_#{key}"]
|
22
|
+
spec.not(key, not_port)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
super + spec.to_rule
|
26
|
+
end
|
27
|
+
|
28
|
+
class PortSpecifier
|
29
|
+
def initialize
|
30
|
+
@multiport = false
|
31
|
+
@rule = []
|
32
|
+
end
|
33
|
+
|
34
|
+
def and(key, port)
|
35
|
+
add_port_spec(key, port)
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def not(key, port)
|
40
|
+
@rule << '!'
|
41
|
+
add_port_spec(key, port)
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_rule
|
46
|
+
if @multiport
|
47
|
+
%w[--match multiport] + @rule
|
48
|
+
else
|
49
|
+
@rule
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def add_port_spec(key, port)
|
56
|
+
case port
|
57
|
+
when Array
|
58
|
+
@multiport = true
|
59
|
+
@rule << "--#{key}s" << port.join(',')
|
60
|
+
when Range
|
61
|
+
@multiport = true
|
62
|
+
start = port.begin
|
63
|
+
finish = port.end
|
64
|
+
if port.exclude_end?
|
65
|
+
finish -= 1
|
66
|
+
end
|
67
|
+
@rule << "--#{key}s" << "#{start}:#{finish}"
|
68
|
+
when Fixnum
|
69
|
+
@rule << "--#{key}" << port.to_s
|
70
|
+
else
|
71
|
+
raise "Wrong port specifier: #{port.inspect}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Itamae
|
2
|
+
module Plugin
|
3
|
+
module Resource
|
4
|
+
module Iptables
|
5
|
+
module OwnerRule
|
6
|
+
def self.prepended(base)
|
7
|
+
base.class_eval do
|
8
|
+
[:uid_owner, :gid_owner].each do |key|
|
9
|
+
define_attribute key, type: String
|
10
|
+
define_attribute :"not_#{key}", type: String
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_rule(attrs)
|
16
|
+
rule = %w[uid_owner gid_owner].flat_map do |key|
|
17
|
+
if id = attrs[key]
|
18
|
+
["--#{key.gsub('_', '-')}", id]
|
19
|
+
elsif not_id = attrs["not_#{key}"]
|
20
|
+
['!', "--#{key.gsub('_', '-')}", not_id]
|
21
|
+
else
|
22
|
+
[]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
unless rule.empty?
|
26
|
+
rule.unshift('--match', 'owner')
|
27
|
+
end
|
28
|
+
super + rule
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Itamae
|
2
|
+
module Plugin
|
3
|
+
module Resource
|
4
|
+
module Iptables
|
5
|
+
module StateRule
|
6
|
+
def self.prepended(base)
|
7
|
+
base.class_eval do
|
8
|
+
define_attribute :state, type: Array
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_rule(attrs)
|
13
|
+
rule =
|
14
|
+
if state = attrs['state']
|
15
|
+
['--match', 'state', '--state', state.join(',')]
|
16
|
+
elsif not_state = attrs['not_state']
|
17
|
+
['--match', 'state', '!', '--state', not_state.join(',')]
|
18
|
+
else
|
19
|
+
[]
|
20
|
+
end
|
21
|
+
super + rule
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'itamae/resource/base'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
module Plugin
|
5
|
+
module Resource
|
6
|
+
class IptablesChain < Itamae::Resource::Base
|
7
|
+
define_attribute :action, default: :create
|
8
|
+
define_attribute :chain, type: String, default_name: true
|
9
|
+
define_attribute :table, type: String, default: 'filter'
|
10
|
+
|
11
|
+
def set_current_attributes
|
12
|
+
current.exist = run_command(['iptables', '--table', attributes.table, '--list-rules', attributes.chain], error: false).exit_status == 0
|
13
|
+
unless current.exist
|
14
|
+
Logger.info "Create chain #{attributes.chain}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def action_create(options)
|
19
|
+
unless current.exist
|
20
|
+
run_command(['iptables', '--table', attributes.table, '--new-chain', attributes.chain])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'itamae/resource/base'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
module Plugin
|
5
|
+
module Resource
|
6
|
+
class IptablesFlush < Itamae::Resource::Base
|
7
|
+
define_attribute :action, default: :run
|
8
|
+
|
9
|
+
def action_run(options)
|
10
|
+
get_tables.each do |table|
|
11
|
+
run_command(['iptables', '--table', table, '--flush'])
|
12
|
+
run_command(['iptables', '--table', table, '--delete-chain'])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def get_tables
|
19
|
+
run_command(['cat', '/proc/net/ip_tables_names']).stdout.each_line.map(&:chomp)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'itamae/resource/base'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
module Plugin
|
5
|
+
module Resource
|
6
|
+
class IptablesPolicy < Itamae::Resource::Base
|
7
|
+
define_attribute :action, required: true
|
8
|
+
define_attribute :chain, default_name: true
|
9
|
+
define_attribute :table, type: String, default: 'filter'
|
10
|
+
|
11
|
+
def set_current_attributes
|
12
|
+
super
|
13
|
+
current.action = get_policy(attributes.table, attributes.chain)
|
14
|
+
end
|
15
|
+
|
16
|
+
def action_drop(options)
|
17
|
+
run_command(['iptables', '--table', attributes.table, '--policy', attributes.chain, 'DROP'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def action_accept(options)
|
21
|
+
run_command(['iptables', '--table', attributes.table, '--policy', attributes.chain, 'ACCEPT'])
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def get_policy(table, chain)
|
27
|
+
line = run_command(['iptables', '--table', table, '--list-rules', chain]).stdout.each_line.first.chomp
|
28
|
+
line[/\A-P #{Regexp.escape(chain)} (.+)\z/, 1].downcase.to_sym
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'itamae/resource/base'
|
2
|
+
|
3
|
+
require 'itamae/plugin/resource/iptables/basic_rule'
|
4
|
+
require 'itamae/plugin/resource/iptables/multiport_rule'
|
5
|
+
require 'itamae/plugin/resource/iptables/log_rule'
|
6
|
+
require 'itamae/plugin/resource/iptables/owner_rule'
|
7
|
+
require 'itamae/plugin/resource/iptables/state_rule'
|
8
|
+
|
9
|
+
module Itamae
|
10
|
+
module Plugin
|
11
|
+
module Resource
|
12
|
+
class IptablesRule < Itamae::Resource::Base
|
13
|
+
define_attribute :chain, type: String, required: true
|
14
|
+
define_attribute :table, type: String, default: 'filter'
|
15
|
+
define_attribute :jump, type: String
|
16
|
+
define_attribute :comment, type: String, default_name: true
|
17
|
+
|
18
|
+
prepend Iptables::BasicRule
|
19
|
+
prepend Iptables::LogRule
|
20
|
+
prepend Iptables::MultiportRule
|
21
|
+
prepend Iptables::OwnerRule
|
22
|
+
prepend Iptables::StateRule
|
23
|
+
|
24
|
+
def pre_action
|
25
|
+
super
|
26
|
+
if attributes.action != :create
|
27
|
+
attributes.jump = attributes.action.to_s.upcase
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_current_attributes
|
32
|
+
super
|
33
|
+
rule = build_rule(attributes)
|
34
|
+
current.exist = run_command(['iptables', '--table', attributes.table, '--check', attributes.chain] + rule, error: false).exit_status == 0
|
35
|
+
unless current.exist
|
36
|
+
Logger.info "Create rule for #{attributes.chain}: #{rule.join(' ')}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def action_create(options)
|
41
|
+
unless current.exist
|
42
|
+
rule = build_rule(attributes)
|
43
|
+
run_command(['iptables', '--table', attributes.table, '--append', attributes.chain] + rule)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_method :action_accept, :action_create
|
48
|
+
alias_method :action_drop, :action_create
|
49
|
+
alias_method :action_log, :action_create
|
50
|
+
alias_method :action_redirect, :action_create
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def build_rule(attrs)
|
55
|
+
rule = []
|
56
|
+
rule << "--jump" << attrs.jump
|
57
|
+
rule << '--match' << 'comment' << '--comment' << attrs.comment
|
58
|
+
rule
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'itamae/resource/file'
|
2
|
+
|
3
|
+
module Itamae
|
4
|
+
module Plugin
|
5
|
+
module Resource
|
6
|
+
class IptablesSave < Itamae::Resource::File
|
7
|
+
def pre_action
|
8
|
+
attributes.content = run_command(['iptables-save']).stdout
|
9
|
+
attributes.owner ||= 'root'
|
10
|
+
attributes.group ||= 'root'
|
11
|
+
attributes.mode ||= '644'
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def show_differences
|
16
|
+
# suppress differences
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itamae-plugin-resource-iptables
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kohei Suzuki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: itamae
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: itamae resource plugin to manage iptables.
|
56
|
+
email:
|
57
|
+
- eagletmt@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- examples/firewall.rb
|
68
|
+
- examples/rtmpsuck.rb
|
69
|
+
- itamae-plugin-resource-iptables.gemspec
|
70
|
+
- lib/itamae/plugin/resource/iptables.rb
|
71
|
+
- lib/itamae/plugin/resource/iptables/basic_rule.rb
|
72
|
+
- lib/itamae/plugin/resource/iptables/log_rule.rb
|
73
|
+
- lib/itamae/plugin/resource/iptables/multiport_rule.rb
|
74
|
+
- lib/itamae/plugin/resource/iptables/owner_rule.rb
|
75
|
+
- lib/itamae/plugin/resource/iptables/state_rule.rb
|
76
|
+
- lib/itamae/plugin/resource/iptables/version.rb
|
77
|
+
- lib/itamae/plugin/resource/iptables_chain.rb
|
78
|
+
- lib/itamae/plugin/resource/iptables_flush.rb
|
79
|
+
- lib/itamae/plugin/resource/iptables_policy.rb
|
80
|
+
- lib/itamae/plugin/resource/iptables_rule.rb
|
81
|
+
- lib/itamae/plugin/resource/iptables_save.rb
|
82
|
+
homepage: https://github.com/eagletmt/itamae-plugin-resource-iptables
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.4.5
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: itamae resource plugin to manage iptables.
|
106
|
+
test_files: []
|
107
|
+
has_rdoc:
|