v1/Role/FixAccessruleLiterals.pm

filter: Statementstmt 47.5% 19 / 40 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 62.5% 5 / 8 Podpod n/a totaltotal 35.8% 24 / 67 CC 9 SCAR 39.4
CC
9
Cov
35%
_fix_accessrule_literals
90.0
__ANON__
2.0
__ANON__
2.0
CRAP 51.2
« v1/Role/REST/Client.pm ↑ Summary v1/Role/ObjectMethods.pm »
1 package Net::Cisco::FMC::v1::Role::FixAccessruleLiterals;
2 1 $Net::Cisco::FMC::v1::Role::FixAccessruleLiterals::VERSION = '0.007000';
3 # ABSTRACT: Role for Cisco Firepower Management Center (FMC) API version 1 method generation
4  
5 1 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use NetAddr::IP::Lite ':nofqdn';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 requires qw( get_accessrule list_accessrules );
13  
14  
15 ▶sub _fix_accessrule_literals ($accessrule) {
Subroutine _fix_accessrule_literals: not called CC=9 SCAR=45.0
16 0 for my $attr (qw( sourceNetworks destinationNetworks )) {
17 0 my @literals;
18 0 for my $obj ($accessrule->{$attr}->{literals}->@*) {
19 0 ▶ if ($obj->{type} eq 'FQDN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"type"} eq "FQDN") 0 0 0
20 # NOTE: this never throws an exception but returns undef
21 0 my $ip = NetAddr::IP::Lite->new($obj->{value});
22 0 ▶ if (defined $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ip) 0 0 0
23 $obj->{type} =
24 0 ▶ ( ( $ip->version == 4 && $ip->masklen == 32 )
Branch: 1 branch
BranchTrueFalseTotal
$ip->version == 4 && $ip->masklen == 32 || $ip->version == 6 && $ip->masklen == 128 ? : 0 0 0
Condition0%0/9
$ip->version == 4 and $ip->masklen == 32 or $ip->version == 6 and $ip->masklen == 128
l!l&&r!l&&!r
000
$ip->version == 4 and $ip->masklen == 32
!ll&&!rl&&r
000
$ip->version == 6 and $ip->masklen == 128
!ll&&!rl&&r
000
MC/DC0%0/4
$ip->version == 4 and $ip->masklen == 32 or $ip->version == 6 and $ip->masklen == 128
$ip->version == 4 $ip->masklen == 32 $ip->version == 6 $ip->masklen == 128
Truth table0%0/7
A and B or C and D
A= $ip->version == 4B= $ip->masklen == 32C= $ip->version == 6D= $ip->masklen == 128
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
25 || ( $ip->version == 6 && $ip->masklen == 128 ) )
26 ? 'Host'
27 : 'Network';
28 }
29 }
30 0 push @literals, $obj;
31 }
32 0 $accessrule->{$attr}->{literals} = \@literals;
33 }
34  
35 0 return $accessrule;
36 }
37  
38 around 'get_accessrule' => sub {
39 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 my $self = shift;
41 0 my $data = $orig->($self, @_);
42  
43 0 return _fix_accessrule_literals($data);
44 1 };
45  
46 around 'list_accessrules' => sub {
47 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 my $self = shift;
49 0 my $data = $orig->($self, @_);
50  
51 0 my @rules = map { _fix_accessrule_literals($_) } $data->{items}->@*;
52 0 $data->{items} = \@rules;
53  
54 0 return $data;
55 1 };
56  
57 1 1;
58  
59 __END__
60  
61 =pod
62  
63 =encoding UTF-8
64  
65 =head1 NAME
66  
67 Net::Cisco::FMC::v1::Role::FixAccessruleLiterals - Role for Cisco Firepower Management Center (FMC) API version 1 method generation
68  
69 =head1 VERSION
70  
71 version 0.007000
72  
73 =head1 SYNOPSIS
74  
75 use strict;
76 use warnings;
77 use Net::Cisco::FMC::v1;
78 use Moo::Role ();
79  
80 my $fmc = Net::Cisco::FMC::v1->new(
81 server => 'https://fmcrestapisandbox.cisco.com',
82 user => 'admin',
83 passwd => '$password',
84 clientattrs => { timeout => 30 },
85 );
86  
87 Moo::Role->apply_roles_to_object($fmc,
88 'Net::Cisco::FMC::v1::Role::FixAccessruleLiterals');
89  
90 =head1 DESCRIPTION
91  
92 Cisco FMC 6.3.0 introduced support for FQDN objects which broke literal IPv4
93 host and network objects via the accessrules REST API.
94 Even worse not only are the types of the replies incorrect but updating an
95 existing rule or creating a new one based on a reply silently swallows literal
96 host and network objects which have their type set to FQDN.
97  
98 This role works around this bug by modifying the reply of
99 L<Net::Cisco::FMC::v1/get_accessrule> and
100 L<Net::Cisco::FMC::v1/list_accessrules> and replacing 'FQDN' with 'Network'.
101  
102 This is how an accessrule API response looks like in 6.2.3.7:
103  
104 {
105 "links": {
106 "self": "https://fmc6237.example.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/policy/accesspolicies/005056A6-88ED-0ed3-0000-330712486749/accessrules?offset=0&limit=1&expanded=true"
107 },
108 "items": [
109 {
110 "metadata": {
111 "ruleIndex": 1,
112 "section": "Mandatory",
113 "category": "--Undefined--",
114 "accessPolicy": {
115 "type": "AccessPolicy",
116 "name": "test",
117 "id": "005056A6-88ED-0ed3-0000-330712486749"
118 },
119 "timestamp": 1551185188796,
120 "domain": {
121 "name": "Global",
122 "id": "e276abec-e0f2-11e3-8169-6d9ed49b625f",
123 "type": "Domain"
124 }
125 },
126 "links": {
127 "self": "https://fmc6237.example.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/policy/accesspolicies/005056A6-88ED-0ed3-0000-330712486749/accessrules/005056A6-88ED-0ed3-0000-000268435459"
128 },
129 "enabled": true,
130 "name": "test",
131 "type": "AccessRule",
132 "action": "ALLOW",
133 "id": "005056A6-88ED-0ed3-0000-000268435459",
134 "sourceNetworks": {
135 "literals": [
136 {
137 "type": "Network",
138 "value": "10.0.0.0/24"
139 }
140 ]
141 },
142 "destinationNetworks": {
143 "literals": [
144 {
145 "type": "Host",
146 "value": "10.1.0.1"
147 }
148 ]
149 },
150 "logBegin": false,
151 "logEnd": false,
152 "variableSet": {
153 "name": "Default-Set",
154 "id": "76fa83ea-c972-11e2-8be8-8e45bb1343c0",
155 "type": "VariableSet"
156 },
157 "logFiles": false,
158 "vlanTags": {},
159 "sendEventsToFMC": false
160 }
161 ],
162 "paging": {
163 "offset": 0,
164 "limit": 1,
165 "count": 1,
166 "pages": 1
167 }
168 }
169  
170 And on FMC 6.3.0.1:
171  
172 {
173 "links": {
174 "self": "https://fmc6301.example.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/policy/accesspolicies/00505688-74E1-0ed3-0000-193273532969/accessrules?offset=0&limit=1&expanded=true"
175 },
176 "items": [
177 {
178 "metadata": {
179 "ruleIndex": 1,
180 "section": "Mandatory",
181 "category": "--Undefined--",
182 "accessPolicy": {
183 "type": "AccessPolicy",
184 "name": "test",
185 "id": "00505688-74E1-0ed3-0000-193273532969"
186 },
187 "timestamp": 1551185492316,
188 "domain": {
189 "name": "Global",
190 "id": "e276abec-e0f2-11e3-8169-6d9ed49b625f",
191 "type": "Domain"
192 }
193 },
194 "links": {
195 "self": "https://fmc.6301.example.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/policy/accesspolicies/00505688-74E1-0ed3-0000-193273532969/accessrules/00505688-74E1-0ed3-0000-000268447785"
196 },
197 "id": "00505688-74E1-0ed3-0000-000268447785",
198 "sourceNetworks": {
199 "literals": [
200 {
201 "type": "Network",
202 "value": "10.0.0.0/24"
203 }
204 ]
205 },
206 "destinationNetworks": {
207 "literals": [
208 {
209 "type": "FQDN",
210 "value": "1.1.0.1"
211 }
212 ]
213 },
214 "logFiles": false,
215 "logBegin": false,
216 "logEnd": false,
217 "variableSet": {
218 "name": "Default-Set",
219 "id": "76fa83ea-c972-11e2-8be8-8e45bb1343c0",
220 "type": "VariableSet"
221 },
222 "enableSyslog": false,
223 "vlanTags": {},
224 "sendEventsToFMC": false,
225 "type": "AccessRule",
226 "action": "ALLOW",
227 "name": "test",
228 "enabled": true
229 }
230 ],
231 "paging": {
232 "offset": 0,
233 "limit": 1,
234 "count": 1,
235 "pages": 1
236 }
237 }
238  
239 =head1 AUTHOR
240  
241 Alexander Hartmaier <abraxxa@cpan.org>
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 This software is copyright (c) 2018 - 2023 by Alexander Hartmaier.
246  
247 This is free software; you can redistribute it and/or modify it under
248 the same terms as the Perl 5 programming language system itself.
249  
250 =cut
« v1/Role/REST/Client.pm ↑ Summary v1/Role/ObjectMethods.pm »