Class: Puppet::Provider::UfwRule::UfwRule

Inherits:
ResourceApi::SimpleProvider
  • Object
show all
Defined in:
lib/puppet/provider/ufw_rule/ufw_rule.rb

Overview

Implementation for the ufw_rule type using the Resource API.

Instance Method Summary collapse

Constructor Details

#initializeUfwRule

Returns a new instance of UfwRule.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 9

def initialize
  @default_rule_hash = {
    ensure: 'present',
    action: 'reject',
    direction: 'in',
    interface: nil,
    log: nil,
    from_addr: 'any',
    from_ports_app: nil,
    to_addr: 'any',
    to_ports_app: nil,
    proto: 'any',
  }
  @instances = []
  super()
end

Instance Method Details

#create(context, name, should) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 156

def create(context, name, should)
  context.notice("Creating '#{name}' with #{should.inspect}")
  rule = @default_rule_hash.merge(should)
  params = rule_to_ufw_params(rule)

  Puppet::Util::Execution.execute("/usr/sbin/ufw #{params}", failonfail: true)
end

#delete(context, name) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 176

def delete(context, name)
  context.notice("Deleting '#{name}'")

  is = @instances.find { |r| r[:name] == name }
  params = rule_to_ufw_params_nocomment(is)
  Puppet::Util::Execution.execute("/usr/sbin/ufw delete #{params}", failonfail: true)
end

#get(context) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 26

def get(context)
  context.debug('Returning list of rules')

  @instances = []
  rule_list_lines.each do |line|
    context.debug(line)
    hash = rule_to_hash(context, line)
    @instances << hash unless hash.nil?
    context.warning("Could not parse existing rule: #{line}") if hash.nil?
  end
  @instances
end

#parse_line_full_syntax(line) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 76

def parse_line_full_syntax(line)
  %r{\scomment\s'(?<name>[^']+)'} =~ line
  no_comment = line.sub(%r{\scomment\s'(?<name>[^']+)'}, '')

  %r{ufw (?<action>allow|deny|reject|limit)\s*(?<direction>in|out)*\s*(on\s(?<interface>[\w\d]+))*\s*(?<log>log|log-all)*} =~ no_comment
  %r{\sfrom\s(?<from_addr>[^\s]+)(\s(port|app)\s(?<from_ports_app>[^\s]+))*} =~ no_comment
  %r{\sto\s(?<to_addr>[^\s]+)(\s(port|app)\s(?<to_ports_app>[^\s]+))*} =~ no_comment
  %r{\sproto\s(?<proto>\w+)} =~ no_comment

  rule = {
    action: action,
    direction: direction,
    interface: interface,
    log: log,
    from_addr: from_addr,
    from_ports_app: from_ports_app,
    to_addr: to_addr,
    to_ports_app: to_ports_app,
    proto: proto,
  }.delete_if { |_k, v| v.nil? }

  return nil if rule.empty?

  rule[:name] = name.nil? ? Digest::SHA256.hexdigest(no_comment) : name

  @default_rule_hash.merge(rule)
end

#parse_line_simple_syntax(line) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 55

def parse_line_simple_syntax(line)
  %r{\scomment\s'(?<name>[^']+)'} =~ line
  no_comment = line.sub(%r{\scomment\s'(?<name>[^']+)'}, '')

  %r{^ufw (?<action>allow|deny|reject|limit)\s*(?<direction>in|out)*\s*(?<log>log|log-all)*\s*(?<to_ports_app>[\w,:]+)/*(?<proto>\w+)*$} =~ no_comment

  rule = {
    action: action,
    direction: direction,
    log: log,
    to_ports_app: to_ports_app,
    proto: proto,
  }.delete_if { |_k, v| v.nil? }

  return nil if rule.empty?

  rule[:name] = name.nil? ? Digest::SHA256.hexdigest(line) : name

  @default_rule_hash.merge(rule)
end

#rule_list_linesObject



39
40
41
42
43
44
45
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 39

def rule_list_lines
  result = Puppet::Util::Execution.execute(['/usr/sbin/ufw', 'show', 'added'], failonfail: true)
  result.each_line
        .map(&:strip)
        .reject { |line| line.start_with?('ufw route') }
        .reject { |line| line.start_with?('Added user rules') }
end

#rule_to_hash(_context, line) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 47

def rule_to_hash(_context, line)
  rule = parse_line_simple_syntax(line)

  return rule unless rule.nil?

  parse_line_full_syntax(line)
end

#rule_to_ufw_params(rule) ⇒ Object



148
149
150
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 148

def rule_to_ufw_params(rule)
  rule_to_ufw_params_array(rule).join(' ')
end

#rule_to_ufw_params_array(rule) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 104

def rule_to_ufw_params_array(rule)
  interface_definition = rule[:interface].nil? ? nil : "on #{rule[:interface]}"

  from_addr = rule[:from_addr].nil? ? 'any' : rule[:from_addr]
  from_checked = "#{from_addr}!#{rule[:from_ports_app]}"
  from_definition = case from_checked
                    when %r{.+!$}
                      "from #{from_addr}"
                    when %r{![\d,:]+$}
                      "from #{from_addr} port #{rule[:from_ports_app]}"
                    when %r{!\w+$}
                      "from #{from_addr} app #{rule[:from_ports_app]}"
                    end

  to_addr = rule[:to_addr].nil? ? 'any' : rule[:to_addr]
  to_checked = "#{to_addr}!#{rule[:to_ports_app]}"
  to_definition = case to_checked
                  when %r{.+!$}
                    "to #{to_addr}"
                  when %r{![\d,:]+$}
                    "to #{to_addr} port #{rule[:to_ports_app]}"
                  when %r{!\w+$}
                    "to #{to_addr} app #{rule[:to_ports_app]}"
                  end

  uses_app_name = "#{from_definition} #{to_definition}".include? ' app '

  proto_definition = rule[:proto].nil? ? nil : "proto #{rule[:proto]}"
  proto_definition = nil if uses_app_name # Can't use proto with applications

  comment_definition = rule[:name].nil? ? nil : "comment \'#{rule[:name]}\'"

  [
    rule[:action],
    rule[:direction],
    interface_definition,
    rule[:log],
    from_definition,
    to_definition,
    proto_definition,
    comment_definition,
  ].compact
end

#rule_to_ufw_params_nocomment(rule) ⇒ Object



152
153
154
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 152

def rule_to_ufw_params_nocomment(rule)
  rule_to_ufw_params_array(rule)[0...-1].join(' ')
end

#update(context, name, should) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppet/provider/ufw_rule/ufw_rule.rb', line 164

def update(context, name, should)
  context.notice("Updating '#{name}' with #{should.inspect}")
  is = @instances.find { |r| r[:name] == name }
  rule = @default_rule_hash.merge(is).merge(should)

  is_params = rule_to_ufw_params_nocomment(is)
  Puppet::Util::Execution.execute("/usr/sbin/ufw delete #{is_params}", failonfail: true)

  params = rule_to_ufw_params(rule)
  Puppet::Util::Execution.execute("/usr/sbin/ufw #{params}", failonfail: true)
end