cisco_acl_intp 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +2 -2
  4. data/.travis.yml +4 -2
  5. data/Gemfile +7 -6
  6. data/README.md +23 -18
  7. data/Rakefile +11 -13
  8. data/cisco_acl_intp.gemspec +7 -7
  9. data/lib/cisco_acl_intp/acc.rb +111 -0
  10. data/lib/cisco_acl_intp/{ace.rb → ace_base.rb} +9 -8
  11. data/lib/cisco_acl_intp/{extended_ace.rb → ace_extended.rb} +8 -10
  12. data/lib/cisco_acl_intp/{standard_ace.rb → ace_standard.rb} +5 -5
  13. data/lib/cisco_acl_intp/acespec_base.rb +15 -0
  14. data/lib/cisco_acl_intp/{ace_ip.rb → acespec_ip.rb} +14 -22
  15. data/lib/cisco_acl_intp/{ace_other_qualifiers.rb → acespec_other_qualifiers.rb} +7 -7
  16. data/lib/cisco_acl_intp/{ace_port.rb → acespec_port.rb} +10 -13
  17. data/lib/cisco_acl_intp/{ace_port_opr.rb → acespec_port_opr.rb} +75 -73
  18. data/lib/cisco_acl_intp/{ace_port_opr_base.rb → acespec_port_opr_base.rb} +49 -34
  19. data/lib/cisco_acl_intp/{ace_proto.rb → acespec_proto.rb} +4 -4
  20. data/lib/cisco_acl_intp/{ace_proto_base.rb → acespec_proto_base.rb} +8 -8
  21. data/lib/cisco_acl_intp/{ace_srcdst.rb → acespec_srcdst.rb} +9 -8
  22. data/lib/cisco_acl_intp/{ace_tcp_flags.rb → acespec_tcp_flags.rb} +4 -4
  23. data/lib/cisco_acl_intp/acl.rb +1 -1
  24. data/lib/cisco_acl_intp/acl_base.rb +108 -80
  25. data/lib/cisco_acl_intp/{mono_function_acl.rb → acl_category_base.rb} +5 -5
  26. data/lib/cisco_acl_intp/acl_utils.rb +1 -1
  27. data/lib/cisco_acl_intp/parser.rb +388 -406
  28. data/lib/cisco_acl_intp/parser.ry +8 -3
  29. data/lib/cisco_acl_intp/parser_api.rb +4 -4
  30. data/lib/cisco_acl_intp/scanner.rb +8 -10
  31. data/lib/cisco_acl_intp/scanner_special_token_handler.rb +3 -3
  32. data/lib/cisco_acl_intp/version.rb +1 -1
  33. data/spec/cisco_acl_intp/{extended_ace_spec.rb → ace_extended_spec.rb} +157 -128
  34. data/spec/cisco_acl_intp/ace_spec.rb +21 -19
  35. data/spec/cisco_acl_intp/{standard_ace_spec.rb → ace_standard_spec.rb} +7 -11
  36. data/spec/cisco_acl_intp/{ace_ip_spec.rb → acespec_ip_spec.rb} +34 -34
  37. data/spec/cisco_acl_intp/{ace_other_qualifier_spec.rb → acespec_other_qualifier_spec.rb} +18 -18
  38. data/spec/cisco_acl_intp/acespec_port_operator_spec.rb +331 -0
  39. data/spec/cisco_acl_intp/{ace_port_spec.rb → acespec_port_spec.rb} +33 -33
  40. data/spec/cisco_acl_intp/{ace_proto_spec.rb → acespec_proto_spec.rb} +61 -61
  41. data/spec/cisco_acl_intp/{ace_srcdst_spec.rb → acespec_srcdst_spec.rb} +113 -54
  42. data/spec/cisco_acl_intp/{ace_tcp_flags_spec.rb → acespec_tcp_flags_spec.rb} +10 -10
  43. data/spec/cisco_acl_intp/acl_base_spec.rb +14 -12
  44. data/spec/cisco_acl_intp/{extended_acl_spec.rb → acl_extended_spec.rb} +28 -28
  45. data/spec/cisco_acl_intp/{standard_acl_spec.rb → acl_standard_spec.rb} +24 -23
  46. data/spec/cisco_acl_intp/cisco_acl_intp_spec.rb +1 -1
  47. data/spec/cisco_acl_intp/parser_spec.rb +12 -12
  48. data/spec/cisco_acl_intp/scanner_spec.rb +31 -36
  49. data/spec/parser_fullfill_patterns.rb +6 -7
  50. data/spec/spec_helper.rb +6 -6
  51. data/tools/check_acl.rb +1 -1
  52. metadata +60 -59
  53. data/lib/cisco_acl_intp/single_acl_base.rb +0 -137
  54. data/spec/cisco_acl_intp/ace_port_operator_spec.rb +0 -340
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'cisco_acl_intp/acc'
4
+
5
+ module CiscoAclIntp
6
+ # AceSpec: Component element of a ACE.
7
+ class AceSpecBase < AccessControlContainer
8
+ end
9
+ end
10
+
11
+ ### Local variables:
12
+ ### mode: Ruby
13
+ ### coding: utf-8-unix
14
+ ### indent-tabs-mode: nil
15
+ ### End:
@@ -1,11 +1,11 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'forwardable'
3
3
  require 'netaddr'
4
- require 'cisco_acl_intp/acl_base'
4
+ require 'cisco_acl_intp/acespec_base'
5
5
 
6
6
  module CiscoAclIntp
7
7
  # IP Address and Wildcard mask container
8
- class AceIpSpec < AclContainerBase
8
+ class AceIpSpec < AceSpecBase
9
9
  extend Forwardable
10
10
 
11
11
  # @param [NetAddr::CIDR] value IP address
@@ -41,7 +41,7 @@ module CiscoAclIntp
41
41
  @options = opts
42
42
  define_addrinfo
43
43
  else
44
- fail AclArgumentError, 'Not specified IP address'
44
+ raise AclArgumentError, 'Not specified IP address'
45
45
  end
46
46
  end
47
47
 
@@ -58,13 +58,11 @@ module CiscoAclIntp
58
58
  if to_wmasked_ip_s == '0.0.0.0'
59
59
  # ip = '0.0.0.0' or wildcard = '255.255.255.255'
60
60
  tag_ip('any')
61
+ elsif @wildcard == '0.0.0.0'
62
+ # /32 mask
63
+ format '%s %s', tag_mask('host'), tag_ip(@ipaddr.ip)
61
64
  else
62
- if @wildcard == '0.0.0.0'
63
- # /32 mask
64
- format '%s %s', tag_mask('host'), tag_ip(@ipaddr.ip)
65
- else
66
- format '%s %s', tag_ip(to_wmasked_ip_s), tag_mask(@wildcard)
67
- end
65
+ format '%s %s', tag_ip(to_wmasked_ip_s), tag_mask(@wildcard)
68
66
  end
69
67
  end
70
68
 
@@ -93,7 +91,7 @@ module CiscoAclIntp
93
91
  OCTET_BIT_LENGTH = {
94
92
  '255' => 0, '127' => 1, '63' => 2, '31' => 3,
95
93
  '15' => 4, '7' => 5, '3' => 6, '1' => 7, '0' => 8
96
- }
94
+ }.freeze
97
95
 
98
96
  # Covnet IPv4 bit-flapped wildcard to netmask length
99
97
  # @return [Fixnum] netmask length
@@ -102,11 +100,8 @@ module CiscoAclIntp
102
100
  # e.g. '0.0.0.1.255' #=> 31
103
101
  def wildcard_bitlength
104
102
  @wildcard.split(/\./).reduce(0) do |len, octet|
105
- if len && OCTET_BIT_LENGTH.key?(octet)
106
- len + OCTET_BIT_LENGTH[octet]
107
- else
108
- nil
109
- end
103
+ break unless len && OCTET_BIT_LENGTH.key?(octet)
104
+ len + OCTET_BIT_LENGTH[octet]
110
105
  end
111
106
  end
112
107
 
@@ -145,12 +140,9 @@ module CiscoAclIntp
145
140
  # Set instance variables. Secondary prioritize option is netmask,
146
141
  # and third(last) one is default-mask
147
142
  def define_addrinfo_by_netmask_or_default
148
- if @options.key?(:netmask)
149
- define_addrinfo_with_netmask
150
- else
151
- @options[:netmask] = 32 # default ('host' mask)
152
- define_addrinfo_with_netmask
153
- end
143
+ # default ('host' mask)
144
+ @options[:netmask] = 32 unless @options.key?(:netmask)
145
+ define_addrinfo_with_netmask
154
146
  end
155
147
 
156
148
  # Set instance variables with ip/wildcard
@@ -166,7 +158,7 @@ module CiscoAclIntp
166
158
  def define_addrinfo_with_netmask
167
159
  @netmask = @options[:netmask]
168
160
  @ipaddr = NetAddr::CIDR.create(
169
- format '%s/%s', @options[:ipaddr], @netmask
161
+ format('%s/%s', @options[:ipaddr], @netmask)
170
162
  )
171
163
  @wildcard = @ipaddr.wildcard_mask(true)
172
164
  end
@@ -1,11 +1,11 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'forwardable'
4
- require 'cisco_acl_intp/acl_base'
4
+ require 'cisco_acl_intp/acespec_base'
5
5
 
6
6
  module CiscoAclIntp
7
7
  # List of other-qualifiers for extended ace
8
- class AceOtherQualifierList < AclContainerBase
8
+ class AceOtherQualifierList < AceSpecBase
9
9
  extend Forwardable
10
10
 
11
11
  # @param [Array] value List of {AceOtherQualifierList} object
@@ -23,7 +23,7 @@ module CiscoAclIntp
23
23
  # Generate string for Cisco IOS access list
24
24
  # @return [String]
25
25
  def to_s
26
- tag_other_qualifier(@list.map { |each| each.to_s }.join(' '))
26
+ tag_other_qualifier(@list.map(&:to_s).join(' '))
27
27
  end
28
28
 
29
29
  # @param [AceOtherQualifierList] other RHS Object
@@ -36,7 +36,7 @@ module CiscoAclIntp
36
36
  end
37
37
 
38
38
  # Access list entry qualifier base
39
- class AceOtherQualifierBase < AclContainerBase
39
+ class AceOtherQualifierBase < AceSpecBase
40
40
  end
41
41
 
42
42
  # Log spec container
@@ -51,7 +51,7 @@ module CiscoAclIntp
51
51
 
52
52
  # alias as boolean method
53
53
  # @return [Boolean]
54
- alias_method(:input?, :input)
54
+ alias input? input
55
55
 
56
56
  # Constructor
57
57
  # @param [String] cookie Log cookie
@@ -90,10 +90,10 @@ module CiscoAclIntp
90
90
  # Constructor
91
91
  # @param [String] name Recursive name
92
92
  def initialize(name)
93
- if name && (!name.empty?)
93
+ if name && !name.empty?
94
94
  @recursive_name = name
95
95
  else
96
- fail AclArgumentError, 'Not specified recursive name'
96
+ raise AclArgumentError, 'Not specified recursive name'
97
97
  end
98
98
  end
99
99
 
@@ -1,11 +1,11 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'forwardable'
3
- require 'cisco_acl_intp/ace_proto'
4
- require 'cisco_acl_intp/ace_port_opr'
3
+ require 'cisco_acl_intp/acespec_proto'
4
+ require 'cisco_acl_intp/acespec_port_opr'
5
5
 
6
6
  module CiscoAclIntp
7
7
  # IP(TCP/UDP) port number and operator container
8
- class AcePortSpec < AclContainerBase
8
+ class AcePortSpec < AceSpecBase
9
9
  extend Forwardable
10
10
 
11
11
  # @return [AcePortOperatorBase] value Port-set operator
@@ -32,7 +32,7 @@ module CiscoAclIntp
32
32
  @options = opts
33
33
  define_operator_and_ports
34
34
  else
35
- fail AclArgumentError, 'Not specified port operator'
35
+ raise AclArgumentError, 'Not specified port operator'
36
36
  end
37
37
  end
38
38
 
@@ -61,21 +61,18 @@ module CiscoAclIntp
61
61
  lt: AcePortOpLt,
62
62
  gt: AcePortOpGt,
63
63
  range: AcePortOpRange
64
- }
64
+ }.freeze
65
65
 
66
66
  # Set instance variables
67
67
  # @raise [AclArgumentError]
68
68
  # @return [AcePortOperatorBase] Port set operator object.
69
69
  def define_operator_and_ports
70
70
  opr = @options.key?(:operator) ? @options[:operator].intern : :any
71
- if OPERATOR_CLASS.key?(opr)
72
- @operator = OPERATOR_CLASS[opr].new(
73
- (@options[:port] || @options[:begin_port]),
74
- @options[:end_port]
75
- )
76
- else
77
- fail AclArgumentError, 'Unknown operator'
78
- end
71
+ raise AclArgumentError, 'Unknown operator' unless OPERATOR_CLASS.key?(opr)
72
+ @operator = OPERATOR_CLASS[opr].new(
73
+ (@options[:port] || @options[:begin_port]),
74
+ @options[:end_port]
75
+ )
79
76
  end
80
77
  end
81
78
  end # module
@@ -1,21 +1,21 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'cisco_acl_intp/ace_port_opr_base'
2
+ require 'cisco_acl_intp/acespec_port_opr_base'
3
3
 
4
4
  module CiscoAclIntp
5
5
  # ANY operator class
6
6
  class AcePortOpAny < AceUnaryOpBase
7
7
  # Constructor
8
- def initialize(*args)
8
+ def initialize(*_args)
9
9
  @begin_port = nil
10
10
  @end_port = nil
11
11
  @operator = :any
12
12
  end
13
13
 
14
- # Specified port-set is contained or not?
15
- # @param [AcePortOperator] other Another operator
14
+ # ANY contains other_port? (always true)
15
+ # @param [AcePortOperatorBase] _other Another operator
16
16
  # @return [Boolean]
17
- def contains?(other)
18
- other.kind_of?(AcePortOperatorBase) # match any conditions
17
+ def contains?(_other)
18
+ true
19
19
  end
20
20
 
21
21
  # Generate string for Cisco IOS access list
@@ -26,7 +26,7 @@ module CiscoAclIntp
26
26
  end
27
27
  end
28
28
 
29
- # SSTRICT-ANY operator class
29
+ # STRICT-ANY operator class
30
30
  class AcePortOpStrictAny < AcePortOpAny
31
31
  # Constructor
32
32
  def initialize(*args)
@@ -34,8 +34,8 @@ module CiscoAclIntp
34
34
  @operator = :strict_any
35
35
  end
36
36
 
37
- # Specified port-set is contained or not?
38
- # @param [AcePortOperator] other Another operator
37
+ # STRICT_ANY contains other_port?
38
+ # @param [AcePortOperatorBase] other Another operator
39
39
  # @return [Boolean]
40
40
  def contains?(other)
41
41
  case other
@@ -55,16 +55,11 @@ module CiscoAclIntp
55
55
  @operator = :eq
56
56
  end
57
57
 
58
- # Specified port-set is contained or not?
59
- # @param [AcePortOperator] other Another operator
58
+ # EQ contains EQ?
59
+ # @param [AcePortOpEq] other Another operator
60
60
  # @return [Boolean]
61
- def contains?(other)
62
- case other
63
- when AcePortOpEq
64
- other.port == @begin_port
65
- else
66
- contains_default(other)
67
- end
61
+ def contains_eq?(other)
62
+ other.port == @begin_port
68
63
  end
69
64
  end
70
65
 
@@ -78,38 +73,38 @@ module CiscoAclIntp
78
73
 
79
74
  private
80
75
 
81
- # Operate EQUAL containing check
82
- # @param [AcePortOperator] other Another operator
76
+ # NEQ contains EQ?
77
+ # @param [AcePortOpEq] other Another operator
83
78
  # @return [Boolean]
84
- def compare_eq(other)
79
+ def contains_eq?(other)
85
80
  other.port != @begin_port
86
81
  end
87
82
 
88
- # Operate NOT_EQUAL containing check
89
- # @param [AcePortOperator] other Another operator
83
+ # NEQ contains NEQ?
84
+ # @param [AcePortOpNeq] other Another operator
90
85
  # @return [Boolean]
91
- def compare_neq(other)
86
+ def contains_neq?(other)
92
87
  other.port == @begin_port
93
88
  end
94
89
 
95
- # Operate LOWER_THAN containing check
96
- # @param [AcePortOperator] other Another operator
90
+ # NEQ contains LT?
91
+ # @param [AcePortOpLt] other Another operator
97
92
  # @return [Boolean]
98
- def compare_lt(other)
93
+ def contains_lt?(other)
99
94
  other.port <= @begin_port
100
95
  end
101
96
 
102
- # Operate GREATER_THAN containing check
103
- # @param [AcePortOperator] other Another operator
97
+ # NEQ contains GT?
98
+ # @param [AcePortOpGt] other Another operator
104
99
  # @return [Boolean]
105
- def compare_gt(other)
100
+ def contains_gt?(other)
106
101
  @begin_port <= other.port
107
102
  end
108
103
 
109
- # Operate RANGE containing check
110
- # @param [AcePortOperator] other Another operator
104
+ # NEQ contains RANGE?
105
+ # @param [AcePortOpRange] other Another operator
111
106
  # @return [Boolean]
112
- def compare_range(other)
107
+ def contains_range?(other)
113
108
  other.end_port < @begin_port || @begin_port < other.begin_port
114
109
  end
115
110
  end
@@ -124,31 +119,31 @@ module CiscoAclIntp
124
119
 
125
120
  private
126
121
 
127
- # Operate EQUAL containing check
128
- # @param [AcePortOperator] other Another operator
122
+ # LT contains EQ?
123
+ # @param [AcePortOpEq] other Another operator
129
124
  # @return [Boolean]
130
- def compare_eq(other)
125
+ def contains_eq?(other)
131
126
  other.port < @begin_port
132
127
  end
133
128
 
134
- # Operate NOT_EQUAL containing check
135
- # @param [AcePortOperator] other Another operator
129
+ # LT contains NEQ?
130
+ # @param [AcePortOpNeq] other Another operator
136
131
  # @return [Boolean]
137
- def compare_neq(other)
132
+ def contains_neq?(other)
138
133
  other.port.max? && @begin_port.max?
139
134
  end
140
135
 
141
- # Operate LOWER_THAN containing check
142
- # @param [AcePortOperator] other Another operator
136
+ # LT contains LT?
137
+ # @param [AcePortOpLt] other Another operator
143
138
  # @return [Boolean]
144
- def compare_lt(other)
139
+ def contains_lt?(other)
145
140
  other.port <= @begin_port
146
141
  end
147
142
 
148
- # Operate RANGE containing check
149
- # @param [AcePortOperator] other Another operator
143
+ # LT contains RANGE?
144
+ # @param [AcePortOpRange] other Another operator
150
145
  # @return [Boolean]
151
- def compare_range(other)
146
+ def contains_range?(other)
152
147
  other.end_port < @begin_port
153
148
  end
154
149
  end
@@ -163,31 +158,31 @@ module CiscoAclIntp
163
158
 
164
159
  private
165
160
 
166
- # Operate EQUAL containing check
167
- # @param [AcePortOperator] other Another operator
161
+ # GT contains EQ?
162
+ # @param [AcePortOpEq] other Another operator
168
163
  # @return [Boolean]
169
- def compare_eq(other)
164
+ def contains_eq?(other)
170
165
  @begin_port < other.port
171
166
  end
172
167
 
173
- # Operate NOT_EQUAL containing check
174
- # @param [AcePortOperator] other Another operator
168
+ # GT contains NEQ?
169
+ # @param [AcePortOpNeq] other Another operator
175
170
  # @return [Boolean]
176
- def compare_neq(other)
171
+ def contains_neq?(other)
177
172
  @begin_port.min? && other.port.min?
178
173
  end
179
174
 
180
- # Operate GREATER_THAN containing check
181
- # @param [AcePortOperator] other Another operator
175
+ # GT contains GT?
176
+ # @param [AcePortOpGt] other Another operator
182
177
  # @return [Boolean]
183
- def compare_gt(other)
178
+ def contains_gt?(other)
184
179
  @begin_port <= other.port
185
180
  end
186
181
 
187
- # Operate RANGE containing check
188
- # @param [AcePortOperator] other Another operator
182
+ # GT contains RANGE?
183
+ # @param [AcePortOperatorBase] other Another operator
189
184
  # @return [Boolean]
190
- def compare_range(other)
185
+ def contains_range?(other)
191
186
  @begin_port < other.begin_port
192
187
  end
193
188
  end
@@ -198,46 +193,53 @@ module CiscoAclIntp
198
193
  def initialize(*args)
199
194
  super
200
195
  unless @begin_port < @end_port
201
- fail AclArgumentError, 'Invalid port sequence'
196
+ raise AclArgumentError, 'Invalid port sequence'
202
197
  end
203
198
  @operator = :range
204
199
  end
205
200
 
206
201
  private
207
202
 
208
- # Operate EQUAL containing check
209
- # @param [AcePortOperator] other Another operator
203
+ # RANGE contains ANY?
204
+ # @param [AcePortOpAny] _other Another operator
205
+ # @return [Boolean]
206
+ def contains_any?(_other)
207
+ @begin_port.min? && @end_port.max?
208
+ end
209
+
210
+ # RANGE contains EQ?
211
+ # @param [AcePortOpEq] other Another operator
210
212
  # @return [Boolean]
211
- def compare_eq(other)
213
+ def contains_eq?(other)
212
214
  @begin_port <= other.port && other.port <= @end_port
213
215
  end
214
216
 
215
- # Operate NOT_EQUAL containing check
216
- # @param [AcePortOperator] other Another operator
217
+ # RANGE contains NEQ?
218
+ # @param [AcePortOpNeq] other Another operator
217
219
  # @return [Boolean]
218
- def compare_neq(other)
220
+ def contains_neq?(other)
219
221
  @begin_port.min? && @end_port.max? &&
220
222
  (other.port.min? || other.port.max?)
221
223
  end
222
224
 
223
- # Operate LOWER_THAN containing check
224
- # @param [AcePortOperator] other Another operator
225
+ # RANGE contains LT?
226
+ # @param [AcePortOpLt] other Another operator
225
227
  # @return [Boolean]
226
- def compare_lt(other)
228
+ def contains_lt?(other)
227
229
  @begin_port.min? && other.port < @end_port
228
230
  end
229
231
 
230
- # Operate GREATER_THAN containing check
231
- # @param [AcePortOperator] other Another operator
232
+ # RANGE contains GT?
233
+ # @param [AcePortOpGt] other Another operator
232
234
  # @return [Boolean]
233
- def compare_gt(other)
235
+ def contains_gt?(other)
234
236
  @begin_port < other.port && @end_port.max?
235
237
  end
236
238
 
237
- # Operate RANGE containing check
238
- # @param [AcePortOperator] other Another operator
239
+ # RANGE contains RANGE?
240
+ # @param [AcePortOpRange] other Another operator
239
241
  # @return [Boolean]
240
- def compare_range(other)
242
+ def contains_range?(other)
241
243
  @begin_port <= other.begin_port &&
242
244
  other.end_port <= @end_port
243
245
  end