ObjectGroup.pm

filter: Statementstmt 100% 28 / 28 Branchbran 86.3% 19 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 95.0% 58 / 61 CC 13 SCAR 23.3
CC
13
Cov
94%
new
13.1
BEGIN
1.0
BEGIN
1.0
CRAP 10.2
« ObjectGroup/Service.pm ↑ Summary ObjectGroup/Protocol.pm »
1 package Net::Cisco::ObjectGroup;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '1.01';
7 7 $VERSION = eval $VERSION; # numify for warning-free dev releases
8  
9 7 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
13 22 my $dummy_class = shift;
14 22 my $arg_ref = shift;
15  
16 22 ▶ croak 'missing parameter "type"' if !defined $arg_ref->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_ref->{"type"} 1 21 22
17 croak "unrecognized object-group type: '$arg_ref->{type}'"
18 21 ▶ if $arg_ref->{type} !~ m/^(?:icmp|network|protocol|service|port)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref->{"type"} =~ /^(?:icmp|network|protocol|service|port)$/i 1 20 21
19  
20 20 ▶ croak 'missing parameter "name"' if !defined $arg_ref->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_ref->{"name"} 1 19 20
21 croak "bad object-group name: '$arg_ref->{name}'"
22 19 ▶ if $arg_ref->{name} !~ m/^[a-zA-Z0-9_.-]{1,64}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref->{"name"} =~ /^[a-zA-Z0-9_.-]{1,64}$/ 3 16 19
23  
24 croak 'bad description' if exists $arg_ref->{description}
25 16 ▶ and length $arg_ref->{description} > 200;
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg_ref->{"description"} and length $arg_ref->{"description"} > 200 1 15 16
Condition100%3/3
exists $arg_ref->{"description"} and length $arg_ref->{"description"} > 200
!ll&&!rl&&r
1141
MC/DC100%2/2
exists $arg_ref->{"description"} and length $arg_ref->{"description"} > 200
exists $arg_ref->{"description"} length $arg_ref->{"description"} > 200
Truth table100%3/3
A and B
A= exists $arg_ref->{"description"}B= length $arg_ref->{"description"} > 200
ABresult
0X0
100
111
26  
27 my $type = $arg_ref->{type} =~ m/icmp/i ? 'ICMP'
28 : $arg_ref->{type} =~ m/network/i ? 'Network'
29 : $arg_ref->{type} =~ m/protocol/i ? 'Protocol'
30 15 ▶ : $arg_ref->{type} =~ m/(?:service|port)/i ? 'Service'
Branch: 4 branches
BranchTrueFalseTotal
$arg_ref->{'type'} =~ /icmp/i ? : 4 11 15
$arg_ref->{'type'} =~ /network/i ? : 3 8 11
$arg_ref->{'type'} =~ /protocol/i ? : 3 5 8
$arg_ref->{'type'} =~ /(?:service|port)/i ? : 5 0 5
31 : croak 'please submit a bug report against this module';
32  
33 15 my $class = 'Net::Cisco::ObjectGroup::'. $type;
34 15 ▶ $class->require or
Branch: 1 branch
BranchTrueFalseTotal
$class->require or croak("couldn't load '${class}' (maybe you forgot to install it?)") 15 0 15
35 croak "couldn't load '$class' (maybe you forgot to install it?)";
36  
37 my $og = $class->new({
38 type => lc $type,
39 name => $arg_ref->{name},
40 desc => $arg_ref->{description},
41 15 objs => [],
42 });
43  
44 15 ▶ $og->_init( $arg_ref ) if $og->can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if $og->can("_init") 15 0 15
45  
46 13 return $og;
47 }
48  
49 7 1;
50  
51 =head1 NAME
52  
53 Net::Cisco::ObjectGroup - Generate Cisco ACL object groups
54  
55 =head1 VERSION
56  
57 This document refers to version 1.01 of Net::Cisco::ObjectGroup.
58  
59 =head1 SYNOPSIS
60  
61 use Net::Cisco::ObjectGroup;
62 my $og = Net::Cisco::ObjectGroup->new({
63 type => 'icmp'
64 name => 'friendly_icmp',
65 description => 'ICMP types we like', # optional
66 pretty_print => 1, # optional
67 });
68  
69 $g->push({icmp_type => 8}); # this is an echo request
70 $g->push({group_object => $another_objectgroup_object});
71  
72 print $g->dump, "\n";
73 # prints the object-group configuration commands to STDOUT, something like:
74
75 object-group icmp friendly_icmp
76 description ICMP types we like
77 icmp-object echo
78 group-object other_icmp_types
79  
80 =head1 DESCRIPTION
81  
82 Use this module to manage the presentation of Cisco PIX or FWSM Object Groups.
83 Group entries are pushed into the object in a simple parmaterized fashion, and
84 you can then dump the content in a format that is parsable by Cisco devices.
85  
86 =head1 IMPORTANT NOTE
87  
88 This module's error checking is only concerned with B<syntactic correctness>.
89 It makes no judgement of the I<semantic correctness> of your group entries.
90  
91 For instance, newer FWSM systems use netmasks specified in terms of host
92 address network masks (e.g. C<255.255.255.0>), whereas older systems use
93 wildcard bits (e.g. C<0.0.0.255>). C<Net::Cisco::ObjectGroup> will not check
94 that you use the correct type of mask, or even that your mask isn't something
95 completely inappropriate (e.g. C<cabbages>).
96  
97 =head1 METHODS
98  
99 =head2 C<< Net::Cisco::ObjectGroup->new >>
100  
101 Each object group that you manage must be created through this method, which
102 takes at least two parameters: the C<type> and the C<name> of the object
103 group. The parameters must be provided in a single hash reference argument,
104 like so:
105  
106 my $g = Net::Cisco::ObjectGroup->new({
107 type => 'network',
108 name => 'my_new_object_group',
109 description => 'used for something useful', # optional
110 });
111  
112 Optionally you may also provide a description of the group. For details of
113 the types of object group available, and additional parameters to this method
114 that they accept, see L</"GROUP TYPES">, below.
115  
116 C<Net::Cisco::ObjectGroup> is actually a factory class, and this method
117 returns an object of the type that you requested in the C<type> parameter. All
118 objects inherit from C<Net::Cisco::ObjectGroup::Base>, and on success this
119 method will return an instance of one of the following:
120  
121 =over 4
122  
123 =item *
124  
125 Net::Cisco::ObjectGroup::ICMP
126  
127 =item *
128  
129 Net::Cisco::ObjectGroup::Network
130  
131 =item *
132  
133 Net::Cisco::ObjectGroup::Protocol
134  
135 =item *
136  
137 Net::Cisco::ObjectGroup::Service
138  
139 =back
140  
141 =head2 C<push>
142  
143 Use this method to add an entry to the object group. Although according to
144 Cisco's documentation order of the content of an object group is not
145 significant, this module will preseve the order of pushed entries, with new
146 entries being added to the end of the list of items in the group.
147  
148 Parameters are all passed within a single hash reference argument. Which keys
149 of that hash you populate will depend on the type of the object group on which
150 you are operating. Logic within the module should check that you are
151 syntactically correct, but for brevity of this documentation you are referred
152 to the many Cisco manuals containing object group syntax usage guidelines.
153  
154 See L</"GROUP TYPES">, below, for parameter specifics.
155  
156 =head2 C<dump>
157  
158 This method generates and returns the object group as it would look in a Cisco
159 configuration file.
160  
161 The returned value is a scalar, with embedded newline characters and no
162 terminating newline, so you will need to append that as required. Note that
163 when submitting this to, for example, a L<Net::Appliance::Session> session via
164 C<cmd()>, a newline will be automatically appended by that method.
165  
166 Fully compatible Cisco commands are produced on the fly from the data stored
167 in the C<Net::Cisco::ObjectGroup> object, so you can C<dump> and C<push>
168 repeatedly to your heart's content.
169  
170 =head1 GROUP TYPES
171  
172 Following Cisco configuration guidelines, there are four types of object group
173 available to you. Each of them implements a C<push()> object method to
174 populate the group, with custom parameters as described below.
175  
176 =head2 ICMP
177  
178 The C<new> method to C<Net::Cisco::ObjectGroup> will also accept a
179 C<pretty_print> parameter, which if set to a true value, enables the
180 substitution of some numeric ICMP types for their text aliases within the
181 output from C<dump>.
182  
183 The C<push> method for ICMP object groups accepts the following parameters:
184  
185 =over 4
186  
187 =item C<icmp_type>
188  
189 Fill this value in your parameter hash with an ICMP type. As mentioned above,
190 it is your responsibility to enter something that the Cisco device will parse
191 (e.g. a recognised ICMP type name or IANA assigned number).
192  
193 =item C<group_object>
194  
195 Use this parameter to refer to another ICMP object group in this group entry.
196  
197 =back
198  
199 =head2 Network
200  
201 The C<push> method for Network object groups accepts the following parameters:
202  
203 =over 4
204  
205 =item C<net_addr>, C<netmask>
206  
207 At a minimum, if configuring a network address, you must pass the C<net_addr>
208 parameter. If C<netmask> is omitted, then the C<net_addr> is assumed to be a
209 host address (32 bit netmask). Otherwise, specify a netmask in C<netmask>.
210  
211 =item C<group_object>
212  
213 Use this parameter to refer to another Network object group in this group
214 entry.
215  
216 =back
217  
218 =head2 Protocol
219  
220 The C<new> method to C<Net::Cisco::ObjectGroup> will also accept a
221 C<pretty_print> parameter, which if set to a true value, enables the
222 substitution of some protocol numbers for their text aliases within the output
223 from C<dump>.
224  
225 The C<push> method for Protocol object groups accepts the following
226 parameters:
227  
228 =over 4
229  
230 =item C<protocol>
231  
232 Fill this value in your parameter hash with a protocol type. As mentioned
233 above, it is your responsibility to enter something that the Cisco device will
234 parse (e.g. a recognised protocol name or IANA assigned number).
235  
236 =item C<group_object>
237  
238 Use this parameter to refer to another Protocol object group in this group
239 entry.
240  
241 =back
242  
243 =head2 Service
244  
245 The C<new> method to C<Net::Cisco::ObjectGroup> will also accept a
246 C<pretty_print> parameter, which if set to a true value, enables the
247 substitution of some port numbers for their corresponding service names within
248 the output from C<dump>.
249  
250 The C<new> method for Service object groups I<requires> the following
251 additional parameter:
252  
253 =over 4
254  
255 =item C<protocol>
256  
257 Service object groups must be specified with any of three possible IP protocol
258 groups, C<tcp>, C<udp> or C<tcp-udp> in this parameter.
259  
260 =back
261  
262 The C<push> method for Service object groups accepts the following parameters:
263  
264 =over 4
265  
266 =item C<svc_op>, C<svc>, C<svc_hi>
267  
268 If specifying one or more services (rather than a group, as below), then at a
269 minimum the C<svc_op> and C<svc> parameters must be completed. C<svc_op> may
270 be either C<eq> or C<range>, and if the latter then C<scv_hi> must also
271 contain the corresponding service to make a range.
272  
273 As mentioned above, it is your responsibility to enter values for C<svc> and
274 C<svc_hi> that the Cisco device will parse (e.g. a recognised service name or
275 IANA assigned number).
276  
277 =item C<group_object>
278  
279 Use this parameter to refer to another Service object group in this group
280 entry.
281  
282 =back
283  
284 You may encounter the following diagnostic messages from Protocol groups:
285  
286 =over 4
287  
288 =item C<missing parameter "protocol" when creating service group>
289  
290 This is a required parameter to the C<new()> class method when specifying an
291 object group type of C<service> (or C<port>).
292  
293 =item C<unrecognized protocol type:>...
294  
295 You have used an unrecognized value for the C<protocol> parameter to C<new()>.
296  
297 =item C<missing service operator>
298  
299 The C<svc_op> parameter is missing in your call to C<push()>.
300  
301 =item C<unrecognized service operator:>...
302  
303 You have used an unrecognized value for the C<svc_op> parameter to C<push()>.
304  
305 =back
306  
307 =head1 DIAGNOSTICS
308  
309 =over 4
310  
311 =item C<must specify either group-object or>...
312  
313 At a minimum please supply an object group or other required parameter.
314  
315 =item C<cannot specify both group-object and>...
316  
317 Likewise you should not specify I<both> an object group and type-specific
318 paramters.
319  
320 =item C<bad group-object>
321  
322 Referenced object groups must be of the same type as the group they are
323 referenced from.
324  
325 =item C<missing parameter "type">
326  
327 You forgot to specify the C<type> parameter to C<<
328 Net::Cisco::ObjectGroup->new >>.
329  
330 =item C<unrecognized object-group type:>...
331  
332 The group type must be one of C<icmp>, C<network>, C<protocol>, C<service> or
333 C<port>.
334  
335 =item C<missing parameter "name">
336  
337 You forgot to specify the C<name> parameter to C<<
338 Net::Cisco::ObjectGroup->new >>.
339  
340 =item C<bad object-group name:>...
341  
342 Object group names must be between one and 64 characters comprising only
343 upper and lowercase letters, digits, underscore, period and hyphen.
344  
345 =item C<bad description>
346  
347 The length of the description must not exceed 200 characters.
348  
349 =back
350  
351 =head1 DEPENDENCIES
352  
353 Other than the contents of the standard Perl distribution, you will need the
354 following:
355  
356 =over 4
357  
358 =item *
359  
360 UNIVERSAL::require
361  
362 =item *
363  
364 Class::Data::Inheritable
365  
366 =item *
367  
368 Class::Accessor >= 0.25
369  
370 =back
371  
372 =head1 BUGS
373  
374 If you spot a bug or are experiencing difficulties that are not explained
375 within the documentation, please send an email to oliver@cpan.org or submit a
376 bug to the RT system (http://rt.cpan.org/). It would help greatly if you are
377 able to pinpoint problems or even supply a patch.
378  
379 =head1 SEE ALSO
380  
381 L<Net::Cisco::AccessList::Extended>, L<Net::Appliance::Session>
382  
383 =head1 AUTHOR
384  
385 Oliver Gorwits C<< <oliver.gorwits@oucs.ox.ac.uk> >>
386  
387 =head1 COPYRIGHT & LICENSE
388  
389 Copyright (c) The University of Oxford 2008.
390  
391 This library is free software; you can redistribute it and/or modify it under
392 the same terms as Perl itself.
393  
394 =cut
« ObjectGroup/Service.pm ↑ Summary ObjectGroup/Protocol.pm »