Class: Puppet::Provider::IosAclEntry::CiscoIos

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/ios_acl_entry/cisco_ios.rb

Overview

Configure Access List Entries on the device

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 247

def self.commands_from_instance(instance)
  commands = []

  # some validation
  if instance[:acl_type] == 'extended'
    raise "ios_acl_entry requires 'source_address_wildcard_mask' to be set alongside 'source_address' " if (!instance[:source_address].nil? && instance[:source_address_wildcard_mask].nil?) ||
                                                                                                           (instance[:source_address].nil? && !instance[:source_address_wildcard_mask].nil?)
    raise "ios_acl_entry requires 'destination_address_wildcard_mask' to be set alongside 'destination_address' " if (!instance[:destination_address].nil? &&
                                                                                                                      instance[:destination_address_wildcard_mask].nil?) ||
                                                                                                                     (instance[:destination_address].nil? &&
                                                                                                                      !instance[:destination_address_wildcard_mask].nil?)
    raise 'Either Source Address, address object-group, any or source host are required.' if instance[:source_address].nil? &&
                                                                                             instance[:source_address_group].nil? &&
                                                                                             instance[:source_address_any].nil? &&
                                                                                             instance[:source_address_host].nil? && !instance[:permission].casecmp('evaluate').zero?
    raise 'Either log or log_input can be set, but not both' if !instance[:log].nil? && !instance[:log_input].nil?
    raise 'reflect_timeout requires reflect entry to be set' if !instance[:relect_timeout].nil? && instance[:relfect].nil?
    raise 'protocol must be icmp to set icmp_message_type' if !instance[:protocol].casecmp('icmp') && instance[:icmp_message_type]
    raise 'protocol must be igmp to set igmp_message_type' if !instance[:protocol].casecmp('igmp') && instance[:igmp_message_type]
  end

  unless instance[:icmp_message_type].to_s =~ %r{(^\d+$)}
    if instance[:icmp_message_code]
      warn 'icmp_message_code can only be set when icmp_message_type is an Integer'
      instance[:icmp_message_code] = nil
    end
  end

  if instance[:permission] && instance[:permission].casecmp('evaluate').zero?
    command_line = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, 'evaluation_name', 'set_value', false)
    command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command_line, 'evaluation_name', instance[:evaluation_name], false)
    command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command, 'entry', instance[:entry], false)
  else
    # some tidyup
    unless instance[:dynamic].nil?
      instance[:dynamic] = "Dynamic #{instance[:dynamic]}"
    end
    unless instance[:source_address_host].nil?
      instance[:source_address_host] = "host #{instance[:source_address_host]}"
    end
    unless instance[:source_address_group].nil?
      instance[:source_address_group] = "addrgroup #{instance[:source_address_group]}"
    end
    unless instance[:source_address_any].nil?
      if instance[:source_address_any]
        instance[:source_address_any] = 'any'
      end
    end
    unless instance[:source_eq].nil?
      instance[:source_eq] = "eq #{instance[:source_eq].join(' ')}"
    end
    unless instance[:source_gt].nil?
      instance[:source_gt] = "gt #{instance[:source_gt]}"
    end
    unless instance[:source_lt].nil?
      instance[:source_lt] = "lt #{instance[:source_lt]}"
    end
    unless instance[:source_neq].nil?
      instance[:source_neq] = "neq #{instance[:source_neq]}"
    end
    unless instance[:source_portgroup].nil?
      instance[:source_portgroup] = "portgroup #{instance[:source_portgroup]}"
    end
    unless instance[:source_range].nil?
      instance[:source_range] = "range #{instance[:source_range][0]} #{instance[:source_range][1]}"
    end
    unless instance[:destination_address_host].nil?
      instance[:destination_address_host] = "host #{instance[:destination_address_host]}"
    end
    unless instance[:destination_address_group].nil?
      instance[:destination_address_group] = "addrgroup #{instance[:destination_address_group]}"
    end
    unless instance[:destination_address_any].nil?
      if instance[:destination_address_any] == true
        instance[:destination_address_any] = 'any'
      end
    end
    unless instance[:destination_eq].nil?
      instance[:destination_eq] = "eq #{instance[:destination_eq].join(' ')}"
    end
    unless instance[:destination_gt].nil?
      instance[:destination_gt] = "gt #{instance[:destination_gt]}"
    end
    unless instance[:destination_lt].nil?
      instance[:destination_lt] = "lt #{instance[:destination_lt]}"
    end
    unless instance[:destination_neq].nil?
      instance[:destination_neq] = "neq #{instance[:destination_neq]}"
    end
    unless instance[:destination_portgroup].nil?
      instance[:destination_portgroup] = "portgroup #{instance[:destination_portgroup]}"
    end
    unless instance[:destination_range].nil?
      instance[:destination_range] = "range #{instance[:destination_range][0]} #{instance[:destination_range][1]}"
    end
    unless instance[:ack].nil?
      instance[:ack] = 'ack'
    end
    unless instance[:dscp].nil?
      instance[:dscp] = "dscp #{instance[:dscp]}"
    end
    unless instance[:fin].nil?
      instance[:fin] = 'fin'
    end
    unless instance[:fragments].nil?
      instance[:fragments] = 'fragments'
    end
    unless instance[:log].nil?
      instance[:log] = 'log'
    end
    unless instance[:log_input].nil?
      instance[:log_input] = 'log_input'
    end
    unless instance[:match_all].nil?
      instance[:match_all] = "match-all #{instance[:match_all].join(' ')}"
    end
    unless instance[:match_any].nil?
      instance[:match_any] = "match-any #{instance[:match_any].join(' ')}"
    end
    unless instance[:option].nil?
      instance[:option] = "option #{instance[:option]}"
    end
    unless instance[:precedence].nil?
      instance[:precedence] = "precedence #{instance[:precedence]}"
    end
    unless instance[:psh].nil?
      instance[:psh] = 'psh'
    end
    unless instance[:reflect].nil?
      instance[:reflect] = "reflect #{instance[:reflect]}"
    end
    unless instance[:reflect_timeout].nil?
      instance[:reflect_timeout] = "timeout #{instance[:reflect_timeout]}"
    end
    unless instance[:rst].nil?
      instance[:rst] = 'rst'
    end
    unless instance[:syn].nil?
      instance[:syn] = 'syn'
    end
    unless instance[:time_range].nil?
      instance[:time_range] = "time-range #{instance[:time_range]}"
    end
    unless instance[:tos].nil?
      instance[:tos] = "tos #{instance[:tos]}"
    end
    unless instance[:urg].nil?
      instance[:urg] = 'urg'
    end
    command = PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash)
  end

  if instance[:ensure].to_s == 'absent'
    command = 'no ' + command
  end
  commands << command
  commands
end

.commands_hashObject



6
7
8
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 6

def self.commands_hash
  @commands_hash ||= PuppetX::CiscoIOS::Utility.load_yaml(File.expand_path(__dir__) + '/command.yaml')
end

.instances_from_cli(output) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 204

def self.instances_from_cli(output)
  new_instance_fields = []

  output.scan(%r{#{PuppetX::CiscoIOS::Utility.get_instances(commands_hash)}}).each do |raw_instance_fields|
    acl_name = name_of_access_list(raw_instance_fields)
    acl_type = type_of_access_list(raw_instance_fields)
    acl_entries = raw_instance_fields.scan(%r{\n (.*)})

    acl_entries.flatten.each do |raw_acl_entry|
      new_instance = {}
      split_output = raw_acl_entry.split

      next_token = split_output.shift
      next unless next_token =~ %r{^\d+$}
      new_instance[:entry] = next_token.to_i
      next_token = split_output.shift

      if next_token.casecmp('dynamic').zero?
        new_instance[:dynamic] = split_output.shift
        next_token = split_output.shift
      end

      new_instance[:permission] = next_token
      if new_instance[:permission].casecmp('evaluate').zero?
        new_instance[:evaluation_name] = split_output.shift
      end
      new_instance[:name] = "#{acl_name} #{new_instance[:entry]}"
      new_instance[:access_list] = acl_name
      new_instance[:ensure] = 'present'

      new_instance = if acl_type != 'Extended' && !new_instance[:permission].casecmp('evaluate').zero?
                       parse_standard(split_output, new_instance)
                     else
                       parse_extended(split_output, new_instance)
                     end

      new_instance.delete_if { |_k, v| v.nil? }
      new_instance_fields << new_instance
    end
  end
  new_instance_fields
end

.name_of_access_list(output) ⇒ Object



200
201
202
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 200

def self.name_of_access_list(output)
  output.scan(%r{.*IP access list\s+(\S+)}).flatten.first
end

.parse_extended(split_output, instance) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
103
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 32

def self.parse_extended(split_output, instance)
  tcp_port_types = ['bgp', 'chargen', 'cifs', 'citrix-ica', 'cmd', 'ctiqbe', 'daytime', 'discard', 'domain', 'echo', 'exec', 'finger', 'ftp', 'ftp-data', 'gopher', 'h323', 'hostname', 'http',
                    'https', 'ident', 'imap4', 'irc', 'kerberos', 'klogin', 'kshell', 'ldap', 'ldaps', 'login', 'lotusnotes', 'lpd', 'netbios-ssn', 'nfs', 'nntp', 'pcanywhere-data', 'pim-auto-rp',
                    'pop2', 'pop3', 'pptp', 'rsh', 'rtsp', 'sip', 'smtp', 'sqlnet', 'ssh', 'sunrpc', 'tacacs', 'talk', 'telnet', 'uucp', 'whois', 'www']
  udp_port_types = ['biff', 'bootpc', 'bootps', 'discard', 'dnsix', 'domain', 'echo', 'isakmp', 'mobile-ip', 'nameserver', 'netbios-dgm', 'netbios-ns', 'netbios-ss', 'ntp', 'pim-auto-rp', 'rip',
                    'snmp', 'snmptrap', 'sunrpc', 'syslog', 'tacacs', 'talk', 'tftp', 'time', 'who', 'xdmcp']
  icmp_types = ['administratively-prohibited', 'alternate-address', 'conversion-error', 'dod-host-prohibited', 'dod-net-prohibited', 'echo', 'echo-reply', 'general-parameter-problem',
                'host-isolated', 'host-precedence-unreachable', 'host-redirect', 'host-tos-redirect', 'host-tos-unreachable', 'host-unknown', 'host-unreachable', 'information-reply',
                'information-request', 'mask-reply', 'mask-request', 'mobile-redirect', 'net-redirect', 'net-tos-redirect', 'net-tos-unreachable', 'net-unreachable', 'network-unknown',
                'no-room-for-option', 'option-missing', 'packet-too-big', 'parameter-problem', 'port-unreachable', 'precedence-unreachable', 'protocol-unreachable', 'reassembly-timeout',
                'redirect', 'router-advertisement', 'router-solicitation', 'source-quench', 'source-route-failed', 'time-exceeded', 'timestamp-reply', 'timestamp-request', 'traceroute',
                'ttl-exceeded', 'unreachable']
  igmp_types = ['dvmrp', 'host-query', 'host-report', 'mtrace', 'mtrace-response', 'pim', 'trace', 'v2-leave', 'v2-report', 'v3-report']

  instance[:protocol] = split_output.shift

  unless split_output.nil?
    next_token = split_output.shift
    if next_token == 'any'
      instance[:source_address_any] = true
    elsif next_token == 'host'
      instance[:source_address_host] = split_output.shift
    elsif next_token == 'addrgroup'
      instance[:source_address_group] = split_output.shift
    elsif split_output[0] =~ %r{(^\d+[.]\d+[.]\d+[.]\d+$)}
      instance[:source_address] = next_token
      if split_output[0] =~ %r{(^\d+[.]\d+[.]\d+[.]\d+$)}
        instance[:source_address_wildcard_mask] = split_output.shift
      end
    end

    next_token = split_output.shift
    if next_token == 'eq'
      instance[:source_eq] = []
      while split_output[0] && (split_output[0] =~ %r{(^\d+$)} || tcp_port_types.include?(split_output[0]) || udp_port_types.include?(split_output[0]))
        instance[:source_eq] << split_output.shift
      end
      next_token = split_output.shift
    elsif next_token == 'gt'
      instance[:source_gt] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'lt'
      instance[:source_lt] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'neq'
      instance[:source_neq] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'portgroup'
      instance[:source_portgroup] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'range'
      instance[:source_range] = []
      instance[:source_range] << split_output.shift
      instance[:source_range] << split_output.shift
      next_token = split_output.shift
    end
    if next_token == 'any'
      instance[:destination_address_any] = true
    elsif next_token == 'host'
      instance[:destination_address_host] = split_output.shift
    elsif next_token == 'addrgroup'
      instance[:destination_address_group] = split_output.shift
    elsif split_output[0] =~ %r{(^\d+[.]\d+[.]\d+[.]\d+$)}
      instance[:destination_address] = next_token
      if split_output[0] =~ %r{(^\d+[.]\d+[.]\d+[.]\d+$)}
        instance[:destination_address_wildcard_mask] = split_output.shift
      end
    end

    next_token = split_output.shift
    if next_token == 'eq'
      instance[:destination_eq] = []
      while split_output[0] && (split_output[0] =~ %r{(^\d+$)} || tcp_port_types.include?(split_output[0]) || udp_port_types.include?(split_output[0]))
        instance[:destination_eq] << split_output.shift
      end
      next_token = split_output.shift
    elsif next_token == 'gt'
      instance[:destination_gt] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'lt'
      instance[:destination_lt] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'neq'
      instance[:destination_neq] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'portgroup'
      instance[:destination_portgroup] = split_output.shift
      next_token = split_output.shift
    elsif next_token == 'range'
      instance[:destination_range] = []
      instance[:destination_range] << split_output.shift
      instance[:destination_range] << split_output.shift
      next_token = split_output.shift
    end
  end

  until next_token.nil?
    if next_token == 'ack'
      instance[:ack] = true
    elsif next_token == 'dscp'
      instance[:dscp] = split_output.shift
    elsif next_token == 'fin'
      instance[:fin] = true
    elsif next_token == 'fragments'
      instance[:fragments] = true
    elsif next_token == 'log'
      instance[:log] = true
    elsif next_token == 'log-input'
      instance[:log_input] = true
    elsif next_token == 'lt'
      instance[:lt] = split_output.shift
    elsif next_token == 'match-all'
      instance[:match_all] = []
      while split_output[0][0] == '-' || split_output[0][0] == '+'
        instance[:match_all] << split_output.shift
      end
    elsif next_token == 'match-any'
      instance[:match_any] = []
      while split_output[0][0] == '-' || split_output[0][0] == '+'
        instance[:match_any] << split_output.shift
      end
    elsif next_token == 'option'
      instance[:option] = split_output.shift
    elsif next_token == 'precedence'
      instance[:precedence] = split_output.shift
    elsif next_token == 'psh'
      instance[:psh] = true
    elsif next_token == 'reflect'
      instance[:reflect] = split_output.shift
      if split_output[0] == 'timeout'
        split_output.shift
        instance[:reflect_timeout] = split_output.shift
      end
    elsif next_token == 'rst'
      instance[:rst] = true
    elsif next_token == 'syn'
      instance[:syn] = true
    elsif next_token == 'time-range'
      instance[:time_range] = split_output.shift
    elsif next_token == 'tos'
      instance[:tos] = split_output.shift
    elsif next_token == 'urg'
      instance[:urg] = true
    elsif instance[:protocol] == 'icmp' && (next_token =~ %r{(^\d+$)} || icmp_types.include?(next_token))
      instance[:icmp_message_type] = next_token
      if split_output[0] =~ %r{(^\d+$)}
        instance[:icmp_message_code] = split_output.shift
      end
    elsif instance[:protocol] == 'igmp' && (next_token =~ %r{(^\d+$)} || igmp_types.include?(next_token))
      instance[:igmp_message_type] = next_token
    # If we reach the end with a matches summary
    # eg. (188 matches)
    # (time left 295)
    # Then break
    elsif next_token =~ %r{^\(\S*$}
      break
    end

    next_token = split_output.shift
  end

  instance
end

.parse_standard(split_output, instance) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 10

def self.parse_standard(split_output, instance)
  unless split_output.nil?
    next_token = split_output.shift
    if next_token == 'any'
      instance[:source_address_any] = true
    elsif next_token == 'host'
      instance[:source_address_host] = split_output.shift
    else
      instance[:source_address] = next_token
      if split_output[0] =~ %r{(^\d+[.]\d+[.]\d+[.]\d+$)}
        instance[:source_address_wildcard_mask] = split_output.shift
      end
    end
  end

  if split_output.shift == 'log'
    instance[:log] = true
  end

  instance
end

.type_of_access_list(output) ⇒ Object



196
197
198
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 196

def self.type_of_access_list(output)
  output.scan(%r{(\S*) IP access list.*}).flatten.first
end

Instance Method Details

#commands_hashObject



406
407
408
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 406

def commands_hash
  self.class.commands_hash
end

#delete(context, _name, acl_type, is) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 451

def delete(context, _name, acl_type, is)
  delete_instance = {}
  delete_instance[:ensure] = 'absent'
  delete_instance[:acl_type] = acl_type
  delete_instance[:access_list] = is[:access_list]
  delete_instance[:entry] = is[:entry]
  array_of_commands_to_run = self.class.commands_from_instance(delete_instance)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_acl_mode(delete_instance[:access_list], acl_type, command)
  end
end

#get(context) ⇒ Object



410
411
412
413
414
415
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 410

def get(context)
  context.warning('The ios_acl_entry type is deprecated, due to unreconcilable implementation issues. Use the ios_acl type instead.')
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  return [] if output.nil?
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, self.class.instances_from_cli(output))
end

#set(context, changes) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 417

def set(context, changes)
  changes.each do |name, change|
    # What type of ACL are we using
    access_list_output = context.transport.run_command_enable_mode("show ip access-lists #{change[:should][:access_list]}")
    if self.class.name_of_access_list(access_list_output).nil?
      raise "ios_acl_entry #{change[:should][:name]} requires parent ios_access_list #{change[:should][:access_list]} to be already present"
    end
    acl_type = self.class.type_of_access_list(access_list_output)

    is = change.key?(:is) ? change[:is] : (get(context) || []).find { |key| key[:name] == name }
    should = change[:should]
    if should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name, acl_type, is)
      end
    else
      context.updating(name) do
        update(context, name, acl_type, is, should)
      end
    end
  end
end

#update(context, name, acl_type, is, should) ⇒ Object



440
441
442
443
444
445
446
447
448
449
# File 'lib/puppet/provider/ios_acl_entry/cisco_ios.rb', line 440

def update(context, name, acl_type, is, should)
  should[:acl_type] = acl_type
  if is[:ensure] == 'present'
    delete(context, name, acl_type, is)
  end
  array_of_commands_to_run = self.class.commands_from_instance(should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_acl_mode(should[:access_list], acl_type, command)
  end
end