v1.pm

filter: Statementstmt 16.3% 39 / 238 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 34.3% 11 / 32 Podpod 100% 16 / 16 totaltotal 16.9% 66 / 390 CC 68 SCAR 57.8
CC
68
Cov
12%
create_cleaned_accesspolicy
992.0
_list
56.0
wait_for_task
56.0
CRAP 323.6
↑ Summary v1/Role/REST/Client.pm »
1 package Net::Cisco::FMC::v1;
2 2 $Net::Cisco::FMC::v1::VERSION = '0.007000';
3 # ABSTRACT: Cisco Firepower Management Center (FMC) API version 1 client library
4  
5 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Types::Standard qw( ArrayRef Dict Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Clone qw( clone );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Net::Cisco::FMC::v1::Role::ObjectMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # use Data::Dumper::Concise;
16  
17 2 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21 2 has 'domains' => (
22 is => 'rwp',
23 isa => ArrayRef[Dict[name => Str, uuid => Str]],
24 clearer => 1,
25 );
26  
27  
28 2 has 'domain_uuid' => (
29 is => 'rw',
30 clearer => 1,
31 );
32  
33 2 has '_refresh_token' => (
34 is => 'rw',
35 clearer => 1,
36 );
37  
38 2 with 'Net::Cisco::FMC::v1::Role::REST::Client';
39  
40 0 ▶sub _create ($self, $url, $object_data, $query_params = {}, $expected_code = 201) {
Subroutine _create: not called CC=4 SCAR=30.0
41 0 my $params = $self->user_agent->www_form_urlencode( $query_params );
42 0 my $res = $self->post("$url?$params", $object_data);
43 0 my $code = $res->code;
44 0 my $data = $res->data;
45 croak($data->{error}->{messages}[0]->{description})
46 0 ▶ unless $code == $expected_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code == $expected_code 0 0 0
47 0 return $data;
48 }
49  
50 0 ▶sub _list ($self, $url, $query_params = {}) {
Subroutine _list: not called CC=7 SCAR=40.3
51 # the API only allows 1000 objects at a time
52 # work around that by making multiple API calls
53 0 my $offset = 0;
54 0 my $limit = 1000;
55 0 my $more_data_available = 1;
56 0 my @items;
57 0 ▶ while ($more_data_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more_data_available) 0 0 0
58 0 my $res = $self->get($url, {
59 offset => $offset,
60 limit => $limit,
61 %$query_params,
62 });
63 0 my $code = $res->code;
64 0 my $data = $res->data;
65  
66 croak($data->{error}->{messages}[0]->{description})
67 0 ▶ unless $code == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 200 0 0 0
68  
69 push @items, $data->{items}->@*
70 0 ▶ if exists $data->{items} && ref $data->{items} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"items"} and ref $data->{"items"} eq "ARRAY" 0 0 0
Condition0%0/3
exists $data->{"items"} and ref $data->{"items"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"items"} and ref $data->{"items"} eq "ARRAY"
exists $data->{"items"} ref $data->{"items"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $data->{"items"}B= ref $data->{"items"} eq "ARRAY"
ABresult
0X0
100
111
71  
72 # check if more data is available
73 0 ▶ if ($offset + $limit < $data->{paging}->{count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset + $limit < $data->{'paging'}{'count'}) { } 0 0 0
74 0 $more_data_available = 1;
75 0 $offset += $limit;
76 }
77 else {
78 0 $more_data_available = 0;
79 }
80 }
81  
82 # return response similar to FMC API
83 0 return { items => \@items };
84 }
85  
86 0 ▶sub _get ($self, $url, $query_params = {}) {
Subroutine _get: not called CC=3 SCAR=24.8
87 0 my $res = $self->get($url, $query_params);
88 0 my $code = $res->code;
89 0 my $data = $res->data;
90  
91 croak($data->{error}->{messages}[0]->{description})
92 0 ▶ unless $code == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 200 0 0 0
93  
94 0 return $data;
95 }
96  
97 ▶sub _update ($self, $url, $object, $object_data) {
Subroutine _update: not called CC=3 SCAR=24.8
98 0 my $updated_data = clone($object);
99 0 delete $updated_data->{links};
100 0 delete $updated_data->{metadata};
101 0 delete $updated_data->{error};
102 0 $updated_data = { %$updated_data, %$object_data };
103  
104 0 my $res = $self->put($url, $updated_data);
105 0 my $code = $res->code;
106 0 my $data = $res->data;
107 my $errmsg = ref $data eq 'HASH'
108 ? $data->{error}->{messages}[0]->{description}
109 0 ▶ : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
110 0 ▶ croak($errmsg)
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 200 0 0 0
111 unless $code == 200;
112  
113 0 return $data;
114 }
115  
116 ▶sub _delete ($self, $url) {
Subroutine _delete: not called CC=2 SCAR=17.9
117 0 my $res = $self->delete($url);
118 croak($res->data->{error}->{messages}[0]->{description})
119 0 ▶ unless $res->code == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
120 0 return 1;
121 }
122  
123 2 Net::Cisco::FMC::v1::Role::ObjectMethods->apply([
124 {
125 path => 'object',
126 object => 'portobjectgroups',
127 singular => 'portobjectgroup',
128 },
129 {
130 path => 'object',
131 object => 'protocolportobjects',
132 singular => 'protocolportobject',
133 },
134 {
135 path => 'object',
136 object => 'icmpv4objects',
137 singular => 'icmpv4object',
138 },
139 {
140 path => 'object',
141 object => 'icmpv6objects',
142 singular => 'icmpv6object',
143 },
144 {
145 path => 'object',
146 object => 'interfacegroups',
147 singular => 'interfacegroup',
148 },
149 {
150 path => 'object',
151 object => 'networkgroups',
152 singular => 'networkgroup',
153 },
154 {
155 path => 'object',
156 object => 'networks',
157 singular => 'network',
158 },
159 {
160 path => 'object',
161 object => 'hosts',
162 singular => 'host',
163 },
164 {
165 path => 'object',
166 object => 'ranges',
167 singular => 'range',
168 },
169 {
170 path => 'object',
171 object => 'securityzones',
172 singular => 'securityzone',
173 },
174 {
175 path => 'object',
176 object => 'slamonitors',
177 singular => 'slamonitor',
178 },
179 {
180 path => 'object',
181 object => 'urlgroups',
182 singular => 'urlgroup',
183 },
184 {
185 path => 'object',
186 object => 'urls',
187 singular => 'url',
188 },
189 {
190 path => 'object',
191 object => 'vlangrouptags',
192 singular => 'vlangrouptag',
193 },
194 {
195 path => 'object',
196 object => 'vlantags',
197 singular => 'vlantag',
198 },
199 {
200 path => 'policy',
201 object => 'accesspolicies',
202 singular => 'accesspolicy',
203 },
204 {
205 path => 'object',
206 object => 'networkaddresses',
207 singular => 'networkaddress',
208 },
209 {
210 path => 'object',
211 object => 'ports',
212 singular => 'port',
213 },
214 {
215 path => 'devices',
216 object => 'devicerecords',
217 singular => 'devicerecord',
218 },
219 {
220 path => 'assignment',
221 object => 'policyassignments',
222 singular => 'policyassignment',
223 },
224 ]);
225  
226  
227 ▶sub login($self) {
Subroutine login: not called CC=2 SCAR=17.9
Pod documented
228 0 my $res = $self->post('/api/fmc_platform/v1/auth/generatetoken', undef,
229 { authentication => 'basic' });
230 0 ▶ if ($res->code == 204) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 204) { } 0 0 0
231 # the allowed domains are returned in the domains header JSON
232 # encoded
233 0 my $domains = decode_json($res->response->header('domains'));
234 #say Dumper($domains);
235 0 $self->_set_domains($domains);
236 # set the current domain to the first available
237 0 $self->domain_uuid($domains->[0]->{uuid});
238  
239 # store refresh token
240 0 $self->_refresh_token($res->response->header('x-auth-refresh-token'));
241 0 $self->set_persistent_header('X-auth-access-token',
242 $res->response->header('x-auth-access-token'));
243 }
244 else {
245 0 croak($res->data->{error}->{messages}[0]->{description});
246 }
247 }
248  
249  
250 ▶sub relogin($self) {
Subroutine relogin: not called CC=3 SCAR=24.8
Pod documented
251 0 my $domain_uuid = $self->domain_uuid;
252 0 $self->login;
253 0 ▶ $self->domain_uuid($domain_uuid)
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain_uuid and $domain_uuid ne "" 0 0 0
Condition0%0/3
defined $domain_uuid and $domain_uuid ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $domain_uuid and $domain_uuid ne ""
defined $domain_uuid $domain_uuid ne ""
Truth table0%0/3
A and B
A= defined $domain_uuidB= $domain_uuid ne ""
ABresult
0X0
100
111
254 if defined $domain_uuid && $domain_uuid ne '';
255 }
256  
257  
258 ▶sub logout($self) {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
259 0 my $res = $self->post('/api/fmc_platform/v1/auth/revokeaccess');
260 0 ▶ if ($res->code == 204) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 204) { } 0 0 0
261 0 $self->clear_domains;
262 0 $self->clear_domain_uuid;
263 0 $self->_clear_refresh_token;
264 0 $self->clear_persistent_headers;
265 }
266 else {
267 0 croak($res->data->{error}->{messages}[0]->{description});
268 }
269 }
270  
271  
272 0 ▶sub create_accessrule ($self, $accesspolicy_id, $object_data, $query_params = {}) {
Subroutine create_accessrule: not called CC=2 SCAR=17.9
Pod documented
273 0 return $self->_create(join('/',
274 '/api/fmc_config/v1/domain',
275 $self->domain_uuid,
276 'policy',
277 'accesspolicies',
278 $accesspolicy_id,
279 'accessrules'
280 ), $object_data, $query_params);
281 }
282  
283  
284 0 ▶sub list_accessrules ($self, $accesspolicy_id, $query_params = {}) {
Subroutine list_accessrules: not called CC=2 SCAR=17.9
Pod documented
285 0 return $self->_list(join('/',
286 '/api/fmc_config/v1/domain',
287 $self->domain_uuid,
288 'policy',
289 'accesspolicies',
290 $accesspolicy_id,
291 'accessrules'
292 ), $query_params);
293 }
294  
295  
296 0 ▶sub get_accessrule ($self, $accesspolicy_id, $id, $query_params = {}) {
Subroutine get_accessrule: not called CC=2 SCAR=17.9
Pod documented
297 0 return $self->_get(join('/',
298 '/api/fmc_config/v1/domain',
299 $self->domain_uuid,
300 'policy',
301 'accesspolicies',
302 $accesspolicy_id,
303 'accessrules',
304 $id
305 ), $query_params);
306 }
307  
308  
309 ▶sub update_accessrule ($self, $accesspolicy_id, $object, $object_data) {
Subroutine update_accessrule: not called CC=2 SCAR=17.9
Pod documented
310 0 my $id = $object->{id};
311 0 my $fmc_rule = clone($object);
312 0 for my $user ($fmc_rule->{users}->{objects}->@*) {
313 0 delete $user->{realm};
314 }
315 0 return $self->_update(join('/',
316 '/api/fmc_config/v1/domain',
317 $self->domain_uuid,
318 'policy',
319 'accesspolicies',
320 $accesspolicy_id,
321 'accessrules',
322 $id
323 ), $fmc_rule, $object_data);
324 }
325  
326  
327  
328 ▶sub delete_accessrule ($self, $accesspolicy_id, $id) {
Subroutine delete_accessrule: not called CC=1 SCAR=6.9
Pod documented
329 0 return $self->_delete(join('/',
330 '/api/fmc_config/v1/domain',
331 $self->domain_uuid,
332 'policy',
333 'accesspolicies',
334 $accesspolicy_id,
335 'accessrules',
336 $id
337 ));
338 }
339  
340  
341 0 ▶sub list_deployabledevices ($self, $query_params = {}) {
Subroutine list_deployabledevices: not called CC=2 SCAR=17.9
Pod documented
342 0 return $self->_list(join('/',
343 '/api/fmc_config/v1/domain',
344 $self->domain_uuid,
345 'deployment',
346 'deployabledevices'
347 ), $query_params);
348 }
349  
350  
351 ▶sub create_deploymentrequest ($self, $object_data) {
Subroutine create_deploymentrequest: not called CC=1 SCAR=6.9
Pod documented
352 0 my $data = $self->_create(join('/',
353 '/api/fmc_config/v1/domain',
354 $self->domain_uuid,
355 'deployment',
356 'deploymentrequests'
357 ), $object_data, {}, 202);
358 0 return $data;
359 }
360  
361  
362 ▶sub get_task ($self, $id) {
Subroutine get_task: not called CC=1 SCAR=6.9
Pod documented
363 0 return $self->_get(join('/',
364 '/api/fmc_config/v1/domain',
365 $self->domain_uuid,
366 'job',
367 'taskstatuses',
368 $id
369 ));
370 }
371  
372  
373 ▶sub wait_for_task ($self, $id, $callback) {
Subroutine wait_for_task: not called CC=7 SCAR=40.3
Pod documented
374 0 ▶ croak "id missing"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
375 unless defined $id;
376 0 ▶ croak "callback must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback and ref $callback ne "CODE" 0 0 0
Condition0%0/3
defined $callback and ref $callback ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback ref $callback ne "CODE"
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
ABresult
0X0
100
111
377 if defined $callback && ref $callback ne 'CODE';
378  
379 0 my %in_progress_status_for_type = (
380 DEVICE_DEPLOYMENT => 'Deploying',
381 );
382  
383 0 my $task = $self->get_task($id);
384 die "support for task type '$task->{taskType}' not implemented\n"
385 0 ▶ unless exists $in_progress_status_for_type{$task->{taskType}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $in_progress_status_for_type{$task->{"taskType"}} 0 0 0
386 do {
387 0 ▶ &$callback($task)
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback 0 0 0
388 if defined $callback;
389 0 sleep 1;
390 0 $task = $self->get_task($id);
391 } until (
392 0 ▶ $task->{status} ne $in_progress_status_for_type{$task->{taskType}});
Branch: 1 branch
BranchTrueFalseTotal
$task->{"status"} ne $in_progress_status_for_type{$task->{"taskType"}} or do { do { &$callback($task) if defined $callback; sleep 1; $task = $self->get_task($id) } } 0 0 0
393 0 return $task;
394 }
395  
396  
397 ▶sub cleanup_protocolport ($self, $portobj) {
Subroutine cleanup_protocolport: not called CC=3 SCAR=24.8
Pod documented
398 #say Dumper($rule);
399 #say "protocolport: " . Dumper($portobj);
400 0 my $protocolportobject = $self->get_protocolportobject($portobj->{id});
401 #say Dumper($protocolportobject);
402 0 my $new_name = lc($protocolportobject->{protocol});
403 0 ▶ if ( exists $protocolportobject->{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocolportobject->{'port'}) { } 0 0 0
404 0 $new_name .= '_' . $protocolportobject->{port};
405 }
406 # avoid 'predefined name' errors
407 else {
408 0 $new_name .= '_any';
409 }
410  
411 0 say "\t", $protocolportobject->{name}, ' ⮕ ', $new_name;
412 try {
413 0 my $portobject = $self->update_protocolportobject($protocolportobject, { name => $new_name });
414 0 say "\tname updated";
415 0 return { %$portobject{qw( id type )} };
416 }
417 0 catch {
418 # replace with existing object
419 0 ▶ if ( $@ =~ /The object name \S+ already exists/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /The object name \S+ already exists/u) { } 0 0 0
420 # find existing object
421 0 my $existing_portobject = $self->find_protocolportobject({ name => $new_name });
422 0 say "\texisting object used";
423 0 return { %$existing_portobject{qw( id type )} };
424 }
425 else {
426 0 croak "name update failed: $@";
427 }
428 }
429 }
430  
431  
432 ▶sub cleanup_icmpv4object ($self, $icmpv4obj) {
Subroutine cleanup_icmpv4object: not called CC=4 SCAR=30.0
Pod documented
433 #say "icmpv4object: " . Dumper($icmpv4obj);
434 0 my $icmpv4object = $self->get_icmpv4object($icmpv4obj->{id});
435 #say Dumper($icmpv4object);
436 0 my $new_name = 'icmp_' . lc($icmpv4object->{icmpType});
437 $new_name .= '_' . $icmpv4object->{code}
438 0 ▶ if exists $icmpv4object->{code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $icmpv4object->{"code"} 0 0 0
439  
440 0 say "\t", $icmpv4object->{name}, ' ⮕ ', $new_name;
441 try {
442 0 my $obj = $self->update_icmpv4object($icmpv4object, { name => $new_name });
443 0 say "\tname updated";
444 0 return { %$obj{qw( id type )} };
445 }
446 0 catch {
447 #say "name update failed: $@";
448 # replace with existing object
449 0 ▶ if ( $@ =~ /The object name \S+ already exists/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /The object name \S+ already exists/u) { } 0 0 0
elsif ($@ =~ /conflicts with predefined name on device/u) { } 0 0 0
450 # find existing object
451 0 my $existing_object = $self->find_icmpv4object({ name => $new_name });
452 0 say "\texisting object used";
453 0 return { %$existing_object{qw( id type )} };
454 }
455 elsif ( $@ =~ /conflicts with predefined name on device/ ) {
456 0 say "\t$@";
457 }
458 else {
459 0 croak "name update failed: $@";
460 }
461 }
462 }
463  
464  
465 ▶sub cleanup_hosts($self) {
Subroutine cleanup_hosts: not called CC=4 SCAR=30.0
Pod documented
466 0 for my $object ($self->list_hosts({ expanded => 'true' })->{items}->@*) {
467 try {
468 #say $object->{name};
469 #say Dumper($object);
470 0 ▶ if ($object->{name} =~ /^(.*)_Mask32$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"name"} =~ /^(.*)_Mask32$/u) 0 0 0
471 0 my $new_name = $1;
472 0 say 'renaming host ', $object->{name}, ' ⮕ ', $new_name;
473 0 $self->update_host($object, { name => $new_name });
474 }
475 # clear description
476 0 ▶ if ($object->{description} eq 'Created during ASA Migration') {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"description"} eq "Created during ASA Migration") 0 0 0
477 0 $self->update_host($object, { description => '' });
478 }
479 }
480 0 catch {
481 0 warn $@;
482 }
483 }
484 }
485  
486  
487 sub create_cleaned_accesspolicy (
488 $self,
489 $source_accesspolicy_name,
490 0 ▶ $optional = {}) {
Subroutine create_cleaned_accesspolicy: not called CC=31 SCAR=69.0
Pod documented
491 my $destination_accesspolicy_name = exists $optional->{target_access_policy_name}
492 ? $optional->{target_access_policy_name}
493 0 ▶ : $source_accesspolicy_name . '-cleaned';
Branch: 1 branch
BranchTrueFalseTotal
exists $optional->{'target_access_policy_name'} ? : 0 0 0
494  
495 my @accesspolicies = $self->list_accesspolicies({ expanded => 'true'
496 0 })->{items}->@*;
497 0 for my $accesspolicy (@accesspolicies) {
498 next
499 0 ▶ unless $accesspolicy->{name} eq $source_accesspolicy_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $accesspolicy->{"name"} eq $source_accesspolicy_name 0 0 0
500 0 say "cleaning " . $accesspolicy->{id}, ': ', $accesspolicy->{name};
501 #say Dumper($accesspolicy);
502 #say "creating new accesspolicy: " . Dumper($accesspolicy);
503  
504 # check if the cleaned accesspolicy already exists, in that case
505 # resume
506 0 my $new_accesspolicy;
507 0 for my $accesspolicy (@accesspolicies) {
508 0 ▶ if ($accesspolicy->{name} eq $destination_accesspolicy_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accesspolicy->{"name"} eq $destination_accesspolicy_name) 0 0 0
509 0 $new_accesspolicy = $accesspolicy;
510 0 last;
511 }
512 }
513  
514 0 my $resume_rulenumber;
515 0 ▶ if (defined $new_accesspolicy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_accesspolicy) { } 0 0 0
516 # find first rule to resume cleanup
517 my @rules = $self->list_accessrules($new_accesspolicy->{id})
518 0 ->{items}->@*;
519 0 $resume_rulenumber = scalar @rules + 1;
520 0 say "resuming cleanup of $destination_accesspolicy_name ",
521 "at rule #$resume_rulenumber\n";
522 }
523 else {
524 0 $new_accesspolicy = $self->create_accesspolicy({
525 name => $destination_accesspolicy_name,
526 defaultAction => {
527 action => 'BLOCK',
528 logBegin => 1,
529 sendEventsToFMC => 1,
530 },
531 });
532 }
533 #say Dumper($new_accesspolicy);
534  
535 0 my $rulenumber = 1;
536 0 RULE: for my $rule ($self->list_accessrules($accesspolicy->{id},
537 { expanded => 'true' })->{items}->@*) {
538 0 ▶ if ( defined $resume_rulenumber
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resume_rulenumber and $rulenumber < $resume_rulenumber) 0 0 0
Condition0%0/3
defined $resume_rulenumber and $rulenumber < $resume_rulenumber
!ll&&!rl&&r
000
MC/DC0%0/2
defined $resume_rulenumber and $rulenumber < $resume_rulenumber
defined $resume_rulenumber $rulenumber < $resume_rulenumber
Truth table0%0/3
A and B
A= defined $resume_rulenumberB= $rulenumber < $resume_rulenumber
ABresult
0X0
100
111
539 && $rulenumber < $resume_rulenumber ) {
540 0 $rulenumber++;
541 0 next RULE;
542 }
543  
544 #next RULE
545 # unless $rule->{name} eq 'outside_access_in#15-1';
546 0 say $rule->{name};
547 # copy all attributes of the existing rule to the new one
548 0 my $updated_data = clone($rule);
549 # remove attributes that are not needed/allowed in the create
550 # call
551 0 delete $updated_data->{id};
552 0 delete $updated_data->{links};
553 0 delete $updated_data->{metadata};
554 0 delete $updated_data->{commentHistoryList};
555 #my $rule_for_diff = clone($updated_data);
556  
557 0 ▶ if (exists $optional->{rule_name_coderef}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $optional->{"rule_name_coderef"}) 0 0 0
558 0 $updated_data->{name} = $optional->{rule_name_coderef}->($rulenumber, $rule);
559 }
560  
561 0 $rulenumber++;
562  
563 0 for my $networktype (qw( sourceNetworks destinationNetworks )) {
564 0 my $src_networks = $rule->{$networktype};
565 0 for my $key (keys $src_networks->%*) {
566 0 ▶ if ($key eq 'objects') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'objects') { } 0 0 0
567 0 $updated_data->{$networktype}->{objects} = [];
568 #say "old: " . Dumper($src_networks->{objects});
569 0 for my $network ($src_networks->{objects}->@*) {
570 #say Dumper($network);
571 0 my $name = $network->{name};
572 0 my $type = $network->{type};
573 0 ▶ if ( $name =~ /^DM_INLINE_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^DM_INLINE_/u) { } 0 0 0
574 # eliminate autogenerated NetworkGroups
575 0 ▶ if ( $type eq 'NetworkGroup' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'NetworkGroup') { } 0 0 0
576 0 my $networkgroup = $self->get_networkgroup($network->{id});
577 my $object_count =
578 (exists $networkgroup->{objects}
579 ? scalar $networkgroup->{objects}->@*
580 : 0)
581 + (exists $networkgroup->{literals}
582 ? scalar $networkgroup->{literals}->@*
583 0 ▶ : 0);
Branch: 2 branches
BranchTrueFalseTotal
exists $networkgroup->{'objects'} ? : 0 0 0
exists $networkgroup->{'literals'} ? : 0 0 0
584 0 ▶ if ( $object_count > 50 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_count > 50) { } 0 0 0
585 0 warn "\tnumber of objects (",
586 $object_count, ") would exceed ",
587 "current FMC limit of 50, ",
588 "keeping current contents\n";
589 }
590 else {
591 0 say "\tmoving contents of group $name directly into rule";
592 0 for my $objecttype (qw( objects literals )) {
593 0 for my $networkobject ($networkgroup->{$objecttype}->@*) {
594 #say Dumper($networkobject);
595 0 push $updated_data->{$networktype}->{$objecttype}->@*, $networkobject;
596 }
597 }
598 }
599 }
600 else {
601 0 warn "object type $type not supported, keeping original object!";
602 0 push $updated_data->{$networktype}->{objects}->@*, $network;
603 }
604 }
605 # keep non-autogenerated objects
606 else {
607 0 push $updated_data->{$networktype}->{objects}->@*, $network;
608 }
609 }
610 }
611 # copy all other contents
612 else {
613 0 $updated_data->{$networktype}->{$key} = $src_networks->{$key};
614 }
615 }
616 }
617  
618 0 my $ports = $rule->{destinationPorts};
619 0 ▶ if (exists $ports->{objects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ports->{"objects"}) 0 0 0
620 $updated_data->{destinationPorts} = {
621 0 objects => []
622 };
623 #say "old: " . Dumper($ports->{objects});
624 0 for my $portobj ($ports->{objects}->@*) {
625 #say Dumper($portobj);
626 0 my $name = $portobj->{name};
627 0 my $type = $portobj->{type};
628  
629 0 ▶ if ( $name =~ /^DM_INLINE_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^DM_INLINE_/u) { } 0 0 0
630 # eliminate autogenerated PortObjectGroups
631 0 ▶ if ( $type eq 'ProtocolPortObject' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ProtocolPortObject') { } 0 0 0
elsif ($type eq 'ICMPV4Object') { } 0 0 0
elsif ($type eq 'PortObjectGroup') { } 0 0 0
632 push
633 $updated_data->{destinationPorts}->{objects}->@*,
634 $self->cleanup_protocolport({
635 0 %$portobj{qw( id type )} });
636 }
637 elsif ( $type eq 'ICMPV4Object' ) {
638 push
639 $updated_data->{destinationPorts}->{objects}->@*,
640 $self->cleanup_icmpv4object({
641 0 %$portobj{qw( id type )} });
642 }
643 elsif ( $type eq 'PortObjectGroup' ) {
644 0 say "\tmoving contents of group $name directly into rule";
645 #say Dumper($rule);
646 0 my $portobjectgroup = $self->get_portobjectgroup($portobj->{id});
647 0 for my $portobject ($portobjectgroup->{objects}->@*) {
648 #say Dumper($portobject);
649 my $object = $portobject->{type} eq
650 'ProtocolPortObject'
651 ? $self->cleanup_protocolport(
652 {%$portobject{qw( id type )}}
653 )
654 0 ▶ : {%$portobject{qw( id type )}};
Branch: 1 branch
BranchTrueFalseTotal
$portobject->{'type'} eq 'ProtocolPortObject' ? : 0 0 0
655 push $updated_data->{destinationPorts}->{objects}->@*,
656 0 $object;
657 }
658 }
659 else {
660 0 warn "unhandled object type $type, keeping unmodified\n";
661 0 push $updated_data->{destinationPorts}->{objects}->@*, $portobj;
662 }
663  
664 #my $protocolportobject = $self->get_protocolportobject($portobj->{id});
665 #say Dumper($protocolportobject);
666 }
667 # keep non-autogenerated objects
668 else {
669 0 push $updated_data->{destinationPorts}->{objects}->@*, $portobj;
670 }
671 }
672 }
673 #say "new: " . Dumper($updated_data);
674 # always replace existing destinationPorts because one of
675 # them might have been replaced with an existing one
676 # FIXME: check if literals get lost
677 #$self->update_accessrule($accesspolicy->{id}, $rule, $updated_data)
678 # if $updated_data->%*;
679 #say Dumper($updated_data);
680 #use Test::Differences;
681 #eq_or_diff($updated_data, $rule_for_diff, 'rule');
682 0 $self->create_accessrule($new_accesspolicy->{id}, $updated_data);
683 #last RULE;
684 }
685 }
686 }
687  
688 2 1;
689  
690 __END__
691  
692 =pod
693  
694 =encoding UTF-8
695  
696 =head1 NAME
697  
698 Net::Cisco::FMC::v1 - Cisco Firepower Management Center (FMC) API version 1 client library
699  
700 =head1 VERSION
701  
702 version 0.007000
703  
704 =head1 SYNOPSIS
705  
706 use strict;
707 use warnings;
708 use Net::Cisco::FMC::v1;
709 use Data::Dumper::Concise;
710  
711 my $fmc = Net::Cisco::FMC::v1->new(
712 server => 'https://fmcrestapisandbox.cisco.com',
713 user => 'admin',
714 passwd => '$password',
715 clientattrs => { timeout => 30 },
716 );
717  
718 # login to populate domains
719 $fmc->login;
720  
721 # list all domain uuids and names
722 print Dumper($fmc->domains);
723 # switch domain
724 $fmc->domain_uuid("e276abec-e0f2-11e3-8169-6d9ed49b625f");
725  
726 =head1 DESCRIPTION
727  
728 This module is a client library for the Cisco Firepower Management
729 Center (FMC) REST API version 1.
730 Currently it is developed and tested against FMC version 6.2.3.6.
731  
732 =head1 ATTRIBUTES
733  
734 =head2 domains
735  
736 Returns a list of hashrefs containing name and uuid of all domains which gets
737 populated by L</login>.
738  
739 =head2 domain_uuid
740  
741 The UUID of the domain which is used by all methods.
742  
743 =head1 METHODS
744  
745 =head2 login
746  
747 Logs into the FMC by fetching an authentication token via http basic
748 authentication.
749  
750 =head2 relogin
751  
752 Refreshes the session by loging in again (not using the refresh token) and
753 restores the currently set domain_uuid.
754  
755 =head2 logout
756  
757 Logs out of the FMC.
758  
759 =head2 create_accessrule
760  
761 Takes an access policy id, a hashref of the rule which should be created and
762 optional query parameters.
763  
764 =head2 list_accessrules
765  
766 Takes an access policy id and query parameters and returns a hashref with a
767 single key 'items' that has a list of access rules similar to the FMC API.
768  
769 =head2 get_accessrule
770  
771 Takes an access policy id, rule id and query parameters and returns the access
772 rule.
773  
774 =head2 update_accessrule
775  
776 Takes an access policy id, rule object and a hashref of the rule and returns
777 a hashref of the updated access rule.
778  
779 =head2 delete_accessrule
780  
781 Takes an access policy id and a rule object id.
782  
783 Returns true on success.
784  
785 =head2 list_deployabledevices
786  
787 Takes optional query parameters and returns a hashref with a
788 single key 'items' that has a list of deployable devices similar to the FMC
789 API.
790  
791 =head2 create_deploymentrequest
792  
793 Takes a hashref of deployment parameters.
794  
795 Returns the created task in the ->{metadata}->{task} hashref.
796  
797 =head2 get_task
798  
799 Takes a task id and returns its status.
800  
801 =head2 wait_for_task
802  
803 Takes a task id and an optional callback and checks its status every second
804 until it isn't in-progress any more.
805 The in-progress status is different for each task type, currently only
806 'DEVICE_DEPLOYMENT' is supported.
807 The callback coderef which is called for every check with the task as argument.
808  
809 Returns the task.
810  
811 =head2 cleanup_protocolport
812  
813 Takes a ProtocolPortObject and renames it to protocol_port, e.g. tcp_443.
814 If it has no port 'any' is used instead of the port number no avoid
815 'predefined name' errors.
816 Returns the ProtocolPortObject with the updated attributes.
817  
818 =head2 cleanup_icmpv4object
819  
820 Takes a ICMPv4Object and renames it to protocol_type[_code], e.g. icmp_8_0.
821 If it has no code only protocol and type is used.
822  
823 =head2 cleanup_hosts
824  
825 =over
826  
827 =item removes '_Mask32' from the name
828  
829 =item removes the description if it is 'Created during ASA Migration'
830  
831 =back
832  
833 =head2 create_cleaned_accesspolicy
834  
835 Takes an access policy name and a hashref of optional arguments.
836  
837 =head3 Optional arguments
838  
839 =over
840  
841 =item target_access_policy_name
842  
843 Defaults to access policy name with the postfix '-cleaned'.
844  
845 =item rule_name_coderef
846  
847 Gets passed the rule number and rule object and must return the new rule name.
848  
849 =back
850  
851 Creates a new access policy with the target name containing all rules of the
852 input access policy but cleaned by the following rules:
853  
854 =over
855  
856 =item the commentHistoryList is omitted
857  
858 =item replace autogenerated DM_INLINE_ NetworkGroups by their content
859  
860 Only if they don't contain more than 50 items because of the current limit in
861 FMC.
862  
863 =item replace autogenerated DM_INLINE_ PortObjectGroups by their content
864  
865 =item optional: the rule name is generated
866  
867 By passing a coderef named 'rule_name_coderef' in the optional arguments
868 hashref.
869  
870 =back
871  
872 The new access policy is created with a defaultAction of:
873  
874 action => 'BLOCK'
875 logBegin => true
876 sendEventsToFMC => true
877  
878 This is mainly for access policies migrated by the Cisco Firepower Migration
879 Tool from a Cisco ASA.
880  
881 Supports resuming.
882  
883 =head1 KNOWN BUGS
884  
885 Older FMC versions have bugs like:
886  
887 =over
888  
889 =item truncated JSON responses
890  
891 No workaround on client side possible, only a FMC update helps.
892  
893 =item no response to the 11th call (version 6.2.2.1)
894  
895 No workaround on client side because newer FMC versions (at least 6.2.3.6)
896 throttle the login call too.
897  
898 =item accessrule is created but error 'You do not have the required
899 authorization to do this operation' is thrown (version 6.2.2)
900  
901 No workaround on client side possible, only a FMC update helps.
902  
903 =back
904  
905 =head1 AUTHOR
906  
907 Alexander Hartmaier <abraxxa@cpan.org>
908  
909 =head1 COPYRIGHT AND LICENSE
910  
911 This software is copyright (c) 2018 - 2023 by Alexander Hartmaier.
912  
913 This is free software; you can redistribute it and/or modify it under
914 the same terms as the Perl 5 programming language system itself.
915  
916 =cut
↑ Summary v1/Role/REST/Client.pm »