blib/lib/Net/Cisco/ISE.pm untested

filter: Statementstmt 0.0% 0 / 527 Branchbran 0.0% 0 / 232 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 809 CC 117 SCAR 56.7
CC
117
Cov
0%
query
702.0
parse_xml
600.0
create
210.0
CRAP 289.3
1 package Net::Cisco::ISE;
2 use strict;
3 use Moose;
4  
5 # REST IO stuff here
6 use IO::Socket::SSL qw( SSL_VERIFY_NONE );
7 use LWP::UserAgent;
8 use XML::Simple;
9  
10 # Generics
11 use MIME::Base64;
12 use URI::Escape;
13 use Data::Dumper;
14  
15 # Net::Cisco::ISE::*
16 use Net::Cisco::ISE::InternalUser;
17 use Net::Cisco::ISE::IdentityGroup;
18 #use Net::Cisco::ISE::NetworkDevice;
19 #use Net::Cisco::ISE::NetworkDeviceGroup;
20 #use Net::Cisco::ISE::Endpoint;
21 #use Net::Cisco::ISE::EndpointCertificate;
22 #use Net::Cisco::ISE::EndpointIdentityGroup;
23 #use Net::Cisco::ISE::Portal;
24 #use Net::Cisco::ISE::Profile;
25  
26 BEGIN {
27 use Exporter ();
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $ERROR %actions);
29 $VERSION = '0.01';
30 @ISA = qw(Exporter);
31 @EXPORT = qw();
32 @EXPORT_OK = qw();
33 %EXPORT_TAGS = ();
34
35 $ERROR = ""; # TODO: Document error properly!
36 %actions = ( "version" => "/Rest/Common/AcsVersion",
37 "serviceLocation" => "/Rest/Common/ServiceLocation",
38 "errorMessage" => "/Rest/Common/ErrorMessage",
39 );
40 }
41  
42 # Moose!
43  
44 has 'ssl_options' => (
45 is => 'rw',
46 isa => 'HashRef',
47 default => sub { { 'SSL_verify_mode' => SSL_VERIFY_NONE, 'verify_hostname' => '0' } }
48 );
49  
50 has 'ssl' => (
51 is => 'rw',
52 isa => 'Str',
53 default => '1',
54 );
55  
56 has 'hostname' => (
57 is => 'rw',
58 isa => 'Str',
59 required => '1',
60 );
61  
62 has 'port' => (
63 is => 'rw',
64 isa => 'Str',
65 default => '9060'
66 );
67
68 has 'mock' => (
69 is => 'rw',
70 isa => 'Str',
71 default => '0',
72 );
73
74 sub internalusers # No Moose here :(
75 { my $self = shift;
76 $ERROR = "";
77 if (@_)
78 { my %args = @_;
79 $self->{"InternalUsers"} = $args{"internalusers"};
80 if ($self->mock())
81 { return $self->{"InternalUsers"}; }
82 if ($args{"id"})
83 { $self->{"InternalUsers"} = $self->query("InternalUser","id",$args{"id"}); }
84 } else
85 { $self->{"InternalUsers"} = $self->query("InternalUser");
86 }
87 return $self->{"InternalUsers"};
88 }
89  
90 sub identitygroups # No Moose here :(
91 { my $self = shift;
92 $ERROR = "";
93 if (@_)
94 { my %args = @_;
95 $self->{"IdentityGroups"} = $args{"identitygroups"};
96 if ($self->mock())
97 { return $self->{"IdentityGroups"}; }
98  
99 if ($args{"id"})
100 { $self->{"IdentityGroups"} = $self->query("IdentityGroup","id",$args{"id"}); }
101 } else
102 { $self->{"IdentityGroups"} = $self->query("IdentityGroup");
103 }
104 return $self->{"IdentityGroups"};
105 }
106  
107 sub endpointidentitygroups # No Moose here :(
108 { my $self = shift;
109 $ERROR = "";
110 if (@_)
111 { my %args = @_;
112 $self->{"EndpointIdentityGroups"} = $args{"endpointidentitygroups"};
113 if ($self->mock())
114 { return $self->{"EndpointIdentityGroups"}; }
115  
116 if ($args{"id"})
117 { $self->{"EndpointIdentityGroups"} = $self->query("EndpointIdentityGroup","id",$args{"id"}); }
118 } else
119 { $self->{"EndpointIdentityGroups"} = $self->query("EndpointIdentityGroup");
120 }
121 return $self->{"EndpointIdentityGroups"};
122 }
123  
124 sub networkdevices # No Moose here :(
125 { my $self = shift;
126 $ERROR = "";
127 if (@_)
128 { my %args = @_;
129 $self->{"NetworkDevices"} = $args{"networkdevices"};
130 if ($self->mock())
131 { return $self->{"NetworkDevices"}; }
132  
133 if ($args{"id"})
134 { $self->{"NetworkDevices"} = $self->query("NetworkDevice","id",$args{"id"}); }
135 } else
136 { $self->{"NetworkDevices"} = $self->query("NetworkDevice");
137 }
138 return $self->{"NetworkDevices"};
139 }
140  
141 sub networkdevicegroups # No Moose here :(
142 { my $self = shift;
143 $ERROR = "";
144 if (@_)
145 { my %args = @_;
146 $self->{"NetworkDeviceGroups"} = $args{"networkdevicegroups"};
147 if ($self->mock())
148 { return $self->{"NetworkDeviceGroups"}; }
149  
150 if ($args{"id"})
151 { $self->{"NetworkDeviceGroups"} = $self->query("NetworkDeviceGroup","id",$args{"id"}); }
152 } else
153 { $self->{"NetworkDeviceGroups"} = $self->query("NetworkDeviceGroup");
154 }
155 return $self->{"NetworkDeviceGroups"};
156 }
157  
158 sub endpoints # No Moose here :(
159 { my $self = shift;
160 $ERROR = "";
161 if (@_)
162 { my %args = @_;
163 $self->{"Endpoints"} = $args{"endpoints"};
164 if ($self->mock())
165 { return $self->{"Endpoints"}; }
166  
167 if ($args{"id"})
168 { $self->{"Endpoints"} = $self->query("Endpoint","id",$args{"id"}); }
169 } else
170 { $self->{"Endpoints"} = $self->query("Endpoint");
171 }
172 return $self->{"Endpoints"};
173 }
174
175 sub endpointcertificates # No Moose here :(
176 { my $self = shift;
177 $ERROR = "";
178 if (@_)
179 { my %args = @_;
180 $self->{"EndpointCertificates"} = $args{"endpointcertificates"};
181 if ($self->mock())
182 { return $self->{"EndpointCertificates"}; }
183  
184 if ($args{"id"})
185 { $self->{"EndpointCertificates"} = $self->query("EndpointCertificate","id",$args{"id"}); }
186 } else
187 { $self->{"EndpointCertificates"} = $self->query("EndpointCertificate");
188 }
189 return $self->{"EndpointCertificates"};
190 }
191  
192 sub portals # No Moose here :(
193 { my $self = shift;
194 $ERROR = "";
195 if (@_)
196 { my %args = @_;
197 $self->{"Portals"} = $args{"portals"};
198 if ($self->mock())
199 { return $self->{"Portals"}; }
200  
201 if ($args{"id"})
202 { $self->{"Portals"} = $self->query("Portal","id",$args{"id"}); }
203 } else
204 { $self->{"Portals"} = $self->query("Portal");
205 }
206 return $self->{"Portals"};
207 }
208  
209 sub profiles # No Moose here :(
210 { my $self = shift;
211 $ERROR = "";
212 if (@_)
213 { my %args = @_;
214 $self->{"Profiles"} = $args{"profiles"};
215 if ($self->mock())
216 { return $self->{"Profiles"}; }
217  
218 if ($args{"id"})
219 { $self->{"Profiles"} = $self->query("Profile","id",$args{"id"}); }
220 } else
221 { $self->{"Profiles"} = $self->query("Profile");
222 }
223 return $self->{"Profiles"};
224 }
225  
226 has 'username' => (
227 is => 'rw',
228 isa => 'Str',
229 required => '1',
230 );
231  
232 has 'password' => (
233 is => 'rw',
234 isa => 'Str',
235 required => '1',
236 );
237  
238 sub version # No Moose here :(
239 { my $self = shift;
240 $ERROR = "";
241 unless ($self->{"Version"}) # Version is not going to magically change in one session
242 { $self->{"Version"} = $self->query("Version"); }
243 return $self->{"Version"};
244 }
245
246 sub servicelocation # No Moose here :(
247 { my $self = shift;
248 $ERROR = "";
249 unless ($self->{"ServiceLocation"}) # serviceLocation is not going to magically change in one session
250 { $self->{"ServiceLocation"} = $self->query("ServiceLocation"); }
251 return $self->{"ServiceLocation"};
252 }
253  
254 sub errormessage # No Moose here :(
255 { my $self = shift;
256 $ERROR = "";
257 $self->{"ErrorMessage"} = $self->query("ErrorMessage");
258 return $self->{"ErrorMessage"};
259 }
260
261 # Non-Moose
262  
263 sub query
264 { my ($self, $type, $key, $value) = @_;
265 my $hostname = $self->hostname;
266 my $credentials = encode_base64($self->username.":".$self->password);
267 if ($self->ssl)
268 { $hostname = "https://$hostname"; } else
269 { $hostname = "http://$hostname"; }
270 $hostname .= ":".$self->port if $self->port;
271 my $action = "";
272 my $mode = "";
273 my $accepttype ="";
274 $key ||= "";
275 if ($type eq "InternalUser")
276 { $action = $Net::Cisco::ISE::InternalUser::actions{"query"};
277 $mode = "InternalUsers";
278 $accepttype = "identity.internaluser.1.0";
279 if ($key eq "id")
280 { $action = $Net::Cisco::ISE::InternalUser::actions{"getById"}.$value;
281 $mode = "InternalUser";
282 }
283 }
284 if ($type eq "IdentityGroup")
285 { $action = $Net::Cisco::ISE::IdentityGroup::actions{"query"};
286 $mode = "IdentityGroups";
287 $accepttype = "identity.identitygroup.1.0";
288 if ($key eq "id")
289 { $action = $Net::Cisco::ISE::IdentityGroup::actions{"getById"}.$value;
290 $mode = "IdentityGroup";
291 }
292 }
293  
294 if ($type eq "EndpointIdentityGroup")
295 { #$action = $Net::Cisco::ISE::EndpointIdentityGroup::actions{"query"};
296 $mode = "EndpointIdentityGroups";
297 $accepttype = "identity.endpointgroup.1.0";
298 if ($key eq "id")
299 { #$action = $Net::Cisco::ISE::EndpointIdentityGroup::actions{"getById"}.$value;
300 $mode = "EndpointIdentityGroup";
301 }
302 }
303 if ($type eq "NetworkDevice")
304 { #$action = $Net::Cisco::ISE::NetworkDevice::actions{"query"};
305 $mode = "NetworkDevices";
306 $accepttype = "network.networkdevice.1.1";
307 if ($key eq "id")
308 { #$action = $Net::Cisco::ISE::NetworkDevice::actions{"getById"}.$value;
309 $mode = "NetworkDevice";
310 }
311 }
312 if ($type eq "NetworkDeviceGroup")
313 { #$action = $Net::Cisco::ISE::NetworkDeviceGroup::actions{"query"};
314 $mode = "NetworkDeviceGroups";
315 $accepttype = "network.networkdevicegroup.1.1";
316 if ($key eq "id")
317 { #$action = $Net::Cisco::ISE::NetworkDeviceGroup::actions{"getById"}.$value;
318 $mode = "NetworkDeviceGroup";
319 }
320 }
321 if ($type eq "Endpoint")
322 { #$action = $Net::Cisco::ISE::Endpoint::actions{"query"};
323 $mode = "Endpoints";
324 $accepttype = "identity.endpoint.1.0";
325 if ($key eq "id")
326 { #$action = $Net::Cisco::ISE::Endpoint::actions{"getById"}.$value;
327 $mode = "Endpoint";
328 }
329 }
330 if ($type eq "EndpointCertificate")
331 { #$action = $Net::Cisco::ISE::EndpointCertificate::actions{"query"};
332 $mode = "EndpointCertificates";
333 $accepttype = "ca.endpointcert.1.0";
334 if ($key eq "id")
335 { #$action = $Net::Cisco::ISE::EndpointCertificate::actions{"getById"}.$value;
336 $mode = "EndpointCertificate";
337 }
338 }
339  
340 if ($type eq "Portal")
341 { #$action = $Net::Cisco::ISE::Portal::actions{"query"};
342 $mode = "Portals";
343 $accepttype = "identity.portal.1.0";
344 if ($key eq "id")
345 { #$action = $Net::Cisco::ISE::Portal::actions{"getById"}.$value;
346 $mode = "Portal";
347 }
348 }
349 if ($type eq "Profile")
350 { #$action = $Net::Cisco::ISE::Profile::actions{"query"};
351 $mode = "Profiles";
352 $accepttype = "identity.profilerprofile.1.0";
353 if ($key eq "id")
354 { #$action = $Net::Cisco::ISE::Profile::actions{"getById"}.$value;
355 $mode = "Profile";
356 }
357 }
358  
359 if ($type eq "Version")
360 { $action = $Net::Cisco::ISE::actions{"version"};
361 $mode = "Version";
362 }
363 if ($type eq "ServiceLocation")
364 { $action = $Net::Cisco::ISE::actions{"serviceLocation"};
365 $mode = "ServiceLocation";
366 }
367 if ($type eq "ErrorMessage")
368 { $action = $Net::Cisco::ISE::actions{"errorMessage"};
369 $mode = "ErrorMessage";
370 }
371  
372 $hostname = $hostname . $action;
373 my $useragent = LWP::UserAgent->new (ssl_opts => $self->ssl_options);
374 my $request = HTTP::Request->new(GET => $hostname );
375 $request->header('Authorization' => "Basic $credentials", Accept => "application/vnd.com.cisco.ise.$accepttype+xml");
376 my $result = $useragent->request($request);
377 if ($result->code eq "400") { $ERROR = "Bad Request - HTTP Status: 400"; }
378 if ($result->code eq "410") { $ERROR = "Unknown $type queried by name or ID - HTTP Status: 410"; }
379 print $result->content;
380 $self->parse_xml($mode, $result->content);
381 }
382  
383 sub create
384 { my $self = shift;
385 my $record = shift;
386 return unless $record;
387 my $hostname = $self->hostname;
388 my $credentials = encode_base64($self->username.":".$self->password);
389 if ($self->ssl)
390 { $hostname = "https://$hostname"; } else
391 { $hostname = "http://$hostname"; }
392 $hostname .= ":".$self->port if $self->port;
393 my $action = "";
394 my $data = "";
395 my $accepttype = "";
396 if (ref($record) eq "Net::Cisco::ISE::InternalUser")
397 { $action = $Net::Cisco::ISE::InternalUser::actions{"create"};
398 $accepttype = "identity.internaluser.1.0";
399 }
400  
401 if (ref($record) eq "Net::Cisco::ISE::IdentityGroup")
402 { #$action = $Net::Cisco::ISE::IdentityGroup::actions{"create"};
403 #$accepttype = "identity.identitygroup.1.0";
404 # ISE does not support creating Identity Groups through the API. No idea why this is!
405 }
406  
407 if (ref($record) eq "Net::Cisco::ISE::NetworkDevice")
408 { #$action = $Net::Cisco::ISE::NetworkDevice::actions{"create"};
409 $accepttype = "network.networkdevice.1.1";
410 }
411
412 if (ref($record) eq "Net::Cisco::ISE::NetworkDeviceGroup")
413 { #$action = $Net::Cisco::ISE::NetworkDeviceGroup::actions{"create"};
414 $accepttype = "network.networkdevicegroup.1.1";
415 }
416  
417 if (ref($record) eq "Net::Cisco::ISE::Endpoint")
418 { #$action = $Net::Cisco::ISE::Endpoint::actions{"create"};
419 $accepttype = "identity.endpoint.1.0";
420 }
421 if (ref($record) eq "Net::Cisco::ISE::EndpointCertificate")
422 { #$action = $Net::Cisco::ISE::EndpointCertificate::actions{"create"};
423 $accepttype = "ca.endpointcert.1.0";
424 }
425 if (ref($record) eq "Net::Cisco::ISE::EndpointIdentityGroup")
426 { #$action = $Net::Cisco::ISE::EndpointIdentityGroup::actions{"create"};
427 $accepttype = "identity.endpointgroup.1.0";
428 }
429 if (ref($record) eq "Net::Cisco::ISE::Portal")
430 { #$action = $Net::Cisco::ISE::Portal::actions{"create"};
431 $accepttype = "identity.portal.1.0";
432 }
433 if (ref($record) eq "Net::Cisco::ISE::Profile")
434 { #$action = $Net::Cisco::ISE::Profile::actions{"create"};
435 $accepttype = "identity.profilerprofile.1.0";
436 }
437  
438 $data .= $record->toXML;
439 $data = $record->header($data,$record);
440 warn $data;
441 $hostname = $hostname . $action;
442 my $useragent = LWP::UserAgent->new (ssl_opts => $self->ssl_options);
443 my $request = HTTP::Request->new(POST => $hostname );
444 $request->content_type("application/xml");
445 $request->header("Authorization" => "Basic $credentials", "Content-Type" => "application/vnd.com.cisco.ise.$accepttype+xml");
446 $request->content($data);
447 my $result = $useragent->request($request);
448 my $id = "";
449 if ($result->code ne "201")
450 { my $result_ref = $self->parse_xml("messages", $result->content);
451 $ERROR = $result_ref->{"messages"}{"message"}{"type"}.":".$result_ref->{"messages"}{"message"}{"code"}." - ".$result_ref->{"messages"}{"message"}{"title"}." "." - HTTP Status: ".$result->code;
452 } else
453 { my $location = $result->header("location");
454 ($id) = $location =~ /^.*\/([^\/]*)$/;
455 }
456 return $id;
457 }
458  
459 sub update
460 { my $self = shift;
461 my $record = shift;
462 return unless $record;
463 my $hostname = $self->hostname;
464 my $credentials = encode_base64($self->username.":".$self->password);
465 if ($self->ssl)
466 { $hostname = "https://$hostname"; } else
467 { $hostname = "http://$hostname"; }
468 $hostname .= ":".$self->port if $self->port;
469 my $action = "";
470 my $data = "";
471 my $accepttype = "";
472 if (ref($record) eq "Net::Cisco::ISE::InternalUser")
473 { $action = $Net::Cisco::ISE::InternalUser::actions{"update"};
474 $accepttype = "identity.internaluser.1.0";
475 }
476  
477 if (ref($record) eq "Net::Cisco::ISE::IdentityGroup")
478 { $action = $Net::Cisco::ISE::IdentityGroup::actions{"update"};
479 $accepttype = "identity.identitygroup.1.0";
480 }
481  
482 if (ref($record) eq "Net::Cisco::ISE::NetworkDevice")
483 { #$action = $Net::Cisco::ISE::NetworkDevice::actions{"update"};
484 $accepttype = "network.networkdevice.1.1";
485 }
486
487 if (ref($record) eq "Net::Cisco::ISE::NetworkDeviceGroup")
488 { #$action = $Net::Cisco::ISE::NetworkDeviceGroup::actions{"update"};
489 $accepttype = "network.networkdevicegroup.1.1";
490 }
491  
492 if (ref($record) eq "Net::Cisco::ISE::Endpoint")
493 { #$action = $Net::Cisco::ISE::Endpoint::actions{"update"};
494 $accepttype = "identity.endpoint.1.0";
495 }
496  
497 if (ref($record) eq "Net::Cisco::ISE::EndpointCertificate")
498 { #$action = $Net::Cisco::ISE::EndpointCertificate::actions{"update"};
499 $accepttype = "ca.endpointcert.1.0";
500 }
501  
502 if (ref($record) eq "Net::Cisco::ISE::EndpointIdentityGroup")
503 { #$action = $Net::Cisco::ISE::EndpointIdentityGroup::actions{"update"};
504 $accepttype = "identity.endpointgroup.1.0";
505 }
506  
507 if (ref($record) eq "Net::Cisco::ISE::Portal")
508 { #$action = $Net::Cisco::ISE::Portal::actions{"update"};
509 $accepttype = "identity.portal.1.0";
510 }
511  
512 if (ref($record) eq "Net::Cisco::ISE::Profile")
513 { #$action = $Net::Cisco::ISE::Profile::actions{"update"};
514 $accepttype = "identity.profilerprofile.1.0";
515 }
516  
517 $data .= $record->toXML;
518  
519 $data = $record->header($data, $record);
520 $hostname = $hostname . $action.$record->id;
521 my $useragent = LWP::UserAgent->new (ssl_opts => $self->ssl_options);
522 my $request = HTTP::Request->new(PUT => $hostname );
523 $request->content_type("application/xml");
524 $request->header("Authorization" => "Basic $credentials", "Content-Type" => "application/vnd.com.cisco.ise.$accepttype+xml");
525 $request->content($data);
526 my $result = $useragent->request($request);
527 my $id = "";
528 if ($result->code ne "200")
529 { my $result_ref = $self->parse_xml("messages", $result->content);
530 $ERROR = $result_ref->{"messages"}{"message"}{"type"}.":".$result_ref->{"messages"}{"message"}{"code"}." - ".$result_ref->{"messages"}{"message"}{"title"}." "." - HTTP Status: ".$result->code;
531 } else
532 { my $location = $result->header("location");
533 warn $location;
534 ($id) = $location =~ /^.*\/([^\/]*)$/;
535 }
536 return $id;
537 }
538  
539 sub delete
540 { my $self = shift;
541 my $record = shift;
542 my $hostname = $self->hostname;
543 my $credentials = encode_base64($self->username.":".$self->password);
544 if ($self->ssl)
545 { $hostname = "https://$hostname"; } else
546 { $hostname = "http://$hostname"; }
547 $hostname .= ":".$self->port if $self->port;
548 my $action = "";
549 my $type = "";
550 my $accepttype = "";
551
552 if (ref($record) eq "ARRAY") { $record = $record->[0]; }
553 if (ref($record) eq "Net::Cisco::ISE::InternalUser")
554 { $action = $Net::Cisco::ISE::InternalUser::actions{"getById"};
555 $type = "InternalUser";
556 $accepttype = "identity.internaluser.1.0";
557 }
558  
559 if (ref($record) eq "Net::Cisco::ISE::IdentityGroup")
560 { $action = $Net::Cisco::ISE::IdentityGroup::actions{"getById"};
561 $type = "IdentityGroup";
562 $accepttype = "identity.identitygroup.1.0";
563 }
564  
565 if (ref($record) eq "Net::Cisco::ISE::NetworkDevice")
566 { #$action = $Net::Cisco::ISE::NetworkDevice::actions{"getById"};
567 $type = "NetworkDevice";
568 $accepttype = "network.networkdevice.1.1";
569 }
570
571 if (ref($record) eq "Net::Cisco::ISE::NetworkDeviceGroup")
572 { #$action = $Net::Cisco::ISE::NetworkDeviceGroup::actions{"getById"};
573 $type = "NetworkDeviceGroup";
574 $accepttype = "network.networkdevicegroup.1.1";
575 }
576  
577 if (ref($record) eq "Net::Cisco::ISE::Endpoint")
578 { #$action = $Net::Cisco::ISE::Endpoint::actions{"getById"};
579 $type = "Endpoint";
580 $accepttype = "identity.endpoint.1.0";
581 }
582  
583 # Not sure Endpoint Certificates can be deleted
584 if (ref($record) eq "Net::Cisco::ISE::EndpointCertificate")
585 { #$action = $Net::Cisco::ISE::EndpointCertificate::actions{"getById"};
586 $type = "EndpointCertificate";
587 $accepttype = "ca.endpointcert.1.0";
588 }
589  
590 if (ref($record) eq "Net::Cisco::ISE::EndpointIdentityGroup")
591 { #$action = $Net::Cisco::ISE::EndpointIdentityGroup::actions{"getById"};
592 $type = "EndpointIdentityGroup";
593 $accepttype = "identity.endpointgroup.1.0";
594 }
595  
596 if (ref($record) eq "Net::Cisco::ISE::Portal")
597 { #$action = $Net::Cisco::ISE::Portal::actions{"getById"};
598 $type = "Portal";
599 $accepttype = "identity.portal.1.0";
600 }
601  
602 if (ref($record) eq "Net::Cisco::ISE::Profile")
603 { #$action = $Net::Cisco::ISE::Profile::actions{"getById"};
604 $type = "Profile";
605 $accepttype = "identity.profilerprofile.1.0";
606 }
607  
608
609 $hostname = $hostname . $action.$record->id;
610 my $useragent = LWP::UserAgent->new (ssl_opts => $self->ssl_options);
611 my $request = HTTP::Request->new(DELETE => $hostname );
612 $request->content_type("application/xml");
613 $request->header("Authorization" => "Basic $credentials", Accept => "application/vnd.com.cisco.ise.$accepttype+xml");
614 my $result = $useragent->request($request);
615 my $id = "";
616 if ($result->code ne "204")
617 { $ERROR = $result->{"code"};
618 }
619 }
620  
621 sub parse_xml
622 { my $self = shift;
623 my $type = shift;
624 my $xml_ref = shift;
625 my $xmlsimple = XML::Simple->new();
626 my $xmlout = $xmlsimple->XMLin($xml_ref);
627 if ($type eq "InternalUsers")
628 { #my $users_ref = $xmlout->{"InternalUser"};
629 my $users_ref = $xmlout->{"resources"}{"resource"};
630 my %users = ();
631 for my $key (keys % {$users_ref})
632 { my $user = Net::Cisco::ISE::InternalUser->new( name => $key, %{ $users_ref->{$key} } );
633 $users{$key} = $user;
634 }
635 $self->{"InternalUsers"} = \%users;
636 return $self->{"InternalUsers"};
637 }
638 if ($type eq "InternalUser") # userByName and userById DO NOT return hash but a single instance of Net::Cisco::ISE::InternalUser
639 { my %user_hash = %{ $xmlout };
640 my $user = Net::Cisco::ISE::InternalUser->new( %user_hash );
641 $self->{"InternalUsers"} = $user ;
642 return $self->{"InternalUsers"};
643 }
644  
645 if ($type eq "IdentityGroups")
646 { my $identitygroups_ref = $xmlout->{"resources"}{"resource"};
647 my %identitygroups = ();
648 for my $key (keys % {$identitygroups_ref})
649 { my $identitygroup = Net::Cisco::ISE::IdentityGroup->new( name => $key, %{ $identitygroups_ref->{$key} } );
650 $identitygroups{$key} = $identitygroup;
651 }
652 $self->{"IdentityGroups"} = \%identitygroups;
653 return $self->{"IdentityGroups"};
654 }
655 if ($type eq "IdentityGroup") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::IdentityGroup
656 { my %identitygroup_hash = %{ $xmlout };
657 my $identitygroup = Net::Cisco::ISE::IdentityGroup->new( %identitygroup_hash );
658 $self->{"IdentityGroups"} = $identitygroup;
659 return $self->{"IdentityGroups"};
660 }
661  
662 if ($type eq "NetworkDevices")
663 { my $device_ref = $xmlout->{"resources"}{"resource"};
664 my %devices = ();
665 for my $key (keys % {$device_ref})
666 { #my $device = Net::Cisco::ISE::NetworkDevice->new( name => $key, %{ $device_ref->{$key} } );
667 #$devices{$key} = $device;
668 }
669 $self->{"NetworkDevices"} = \%devices;
670 return $self->{"NetworkDevices"};
671 }
672 if ($type eq "NetworkDevice") # deviceByName and deviceById DO NOT return hash but a single instance of Net::Cisco::ISE::NetworkDevice
673 { my %device_hash = %{ $xmlout };
674 #my $device = Net::Cisco::ISE::NetworkDevice->new( %device_hash );
675 #$self->{"NetworkDevices"} = $device;
676 return $self->{"NetworkDevices"};
677 }
678  
679 if ($type eq "NetworkDeviceGroups")
680 { my $devicegroup_ref = $xmlout->{"resources"}{"resource"};
681 my %devicegroups = ();
682 for my $key (keys % {$devicegroup_ref})
683 { #my $devicegroup = Net::Cisco::ISE::NetworkDeviceGroup->new( name => $key, %{ $devicegroup_ref->{$key} } );
684 #$devicegroups{$key} = $devicegroup;
685 }
686 $self->{"NetworkDeviceGroups"} = \%devicegroups;
687 return $self->{"NetworkDeviceGroups"};
688 }
689 if ($type eq "NetworkDeviceGroup") # deviceGroupByName and deviceGroupById DO NOT return hash but a single instance of Net::Cisco::ISE::NetworkDeviceGroup
690 { my %devicegroup_hash = %{ $xmlout };
691 #my $devicegroup = Net::Cisco::ISE::NetworkDeviceGroup->new( %devicegroup_hash );
692 #$self->{"NetworkDeviceGroups"} = $devicegroup;
693 return $self->{"NetworkDeviceGroups"};
694 }
695
696 if ($type eq "Endpoints")
697 { my $host_ref = $xmlout->{"resources"}{"resource"};
698 my %hosts = ();
699 for my $key (keys % {$host_ref})
700 { #my $host = Net::Cisco::ISE::Endpoint->new( macAddress => $key, %{ $host_ref->{$key} } );
701 #$hosts{$key} = $host;
702 }
703 $self->{"Endpoints"} = \%hosts;
704 return $self->{"Endpoints"};
705 }
706 if ($type eq "Endpoint") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::Endpoint
707 { my %host_hash = %{ $xmlout };
708 #my $host = Net::Cisco::ISE::Endpoint->new( %host_hash );
709 #$self->{"Endpoints"} = $host;
710 return $self->{"Endpoints"};
711 }
712
713 if ($type eq "EndpointCertificates")
714 { my $host_ref = $xmlout->{"resources"}{"resource"};
715 my %hosts = ();
716 for my $key (keys % {$host_ref})
717 { #my $host = Net::Cisco::ISE::EndpointCertificate->new( name => $key, %{ $host_ref->{$key} } );
718 #$hosts{$key} = $host;
719 }
720 $self->{"EndpointCertificates"} = \%hosts;
721 return $self->{"EndpointCertificates"};
722 }
723 if ($type eq "EndpointCertificate") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::Endpoint
724 { my %host_hash = %{ $xmlout };
725 #my $host = Net::Cisco::ISE::EndpointCertificate->new( %host_hash );
726 #$self->{"EndpointCertificates"} = $host;
727 return $self->{"EndpointCertificates"};
728 }
729  
730 if ($type eq "EndpointIdentityGroups")
731 { my $host_ref = $xmlout->{"resources"}{"resource"};
732 my %hosts = ();
733 for my $key (keys % {$host_ref})
734 { #my $host = Net::Cisco::ISE::EndpointIdentityGroup->new( name => $key, %{ $host_ref->{$key} } );
735 #$hosts{$key} = $host;
736 }
737 $self->{"EndpointIdentityGroups"} = \%hosts;
738 return $self->{"EndpointIdentityGroups"};
739 }
740 if ($type eq "EndpointIdentityGroup") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::Endpoint
741 { my %host_hash = %{ $xmlout };
742 #my $host = Net::Cisco::ISE::EndpointIdentityGroup->new( %host_hash );
743 #$self->{"EndpointIdentityGroups"} = $host;
744 return $self->{"EndpointIdentityGroups"};
745 }
746  
747 if ($type eq "Portals")
748 { my $host_ref = $xmlout->{"resources"}{"resource"};
749 my %hosts = ();
750 for my $key (keys % {$host_ref})
751 { #my $host = Net::Cisco::ISE::Portal->new( name => $key, %{ $host_ref->{$key} } );
752 #$hosts{$key} = $host;
753 }
754 $self->{"Portals"} = \%hosts;
755 return $self->{"Portals"};
756 }
757 if ($type eq "Portal") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::Endpoint
758 { my %host_hash = %{ $xmlout };
759 #my $host = Net::Cisco::ISE::Portal->new( %host_hash );
760 #$self->{"Portals"} = $host;
761 return $self->{"Portals"};
762 }
763  
764 if ($type eq "Profiles")
765 { my $host_ref = $xmlout->{"resources"}{"resource"};
766 my %hosts = ();
767 for my $key (keys % {$host_ref})
768 { #my $host = Net::Cisco::ISE::Profile->new( name => $key, %{ $host_ref->{$key} } );
769 #$hosts{$key} = $host;
770 }
771 $self->{"Profiles"} = \%hosts;
772 return $self->{"Profiles"};
773 }
774 if ($type eq "Profile") # ByName and ById DO NOT return hash but a single instance of Net::Cisco::ISE::Endpoint
775 { my %host_hash = %{ $xmlout };
776 #my $host = Net::Cisco::ISE::Profile->new( %host_hash );
777 #$self->{"Profiles"} = $host;
778 return $self->{"Profiles"};
779 }
780  
781 if ($type eq "result")
782 { my %result_hash = %{ $xmlout };
783 return \%result_hash;
784 }
785 if ($type eq "messages")
786 { my %result_hash = %{ $xmlout };
787 return \%result_hash;
788 }
789 if ($type eq "Version")
790 { my %version_hash = %{ $xmlout };
791 return \%version_hash;
792 }
793 if ($type eq "ServiceLocation")
794 { my %servicelocation_hash = %{ $xmlout };
795 return \%servicelocation_hash;
796 }
797 if ($type eq "ErrorMessage")
798 { my %errormessage_hash = %{ $xmlout };
799 return \%errormessage_hash;
800 }
801  
802 }
803  
804 =head1 NAME
805  
806 Net::Cisco::ISE - Access Cisco ISE functionality through REST API
807  
808 =head1 SYNOPSIS
809  
810 use Net::Cisco::ISE;
811 my $ise = Net::Cisco::ISE->new(hostname => '10.0.0.1', username => 'admin', password => 'testPassword');
812 # Options:
813 # hostname - IP or hostname of Cisco ISE 5.x server
814 # username - Username of Administrator user
815 # password - Password of user
816 # port - TCP port 9060 by default
817 # ssl - SSL enabled (1 - default) or disabled (0)
818
819 my %users = $ise->internalusers;
820 # Retrieve all users from ISE
821 # Returns hash with username / Net::Cisco::ISE::InternalUser pairs
822
823 print $ise->internalusers->{"admin"}->toXML;
824 # Dump in XML format (used by ISE for API calls)
825
826 my $user = $ise->internalusers("name","admin");
827 # Faster call to request specific user information by name
828  
829 my $user = $ise->internalusers("id","b74a0ef2-b29c-40e3-a0d1-4c0dfb51ace9");
830 # Faster call to request specific user information by ID (assigned by ISE, present in Net::Cisco::ISE::InternalUser)
831  
832 my %identitygroups = $ise->identitygroups;
833 # Retrieve all identitygroups from ISE
834 # Returns hash with name / Net::Cisco::ISE::IdentityGroup pairs
835
836 print $ise->identitygroups->{"All Groups"}->toXML;
837 # Dump in XML format (used by ISE for API calls)
838
839 my $identitygroup = $ise->identitygroups("name","All Groups");
840 # Faster call to request specific identity group information by name
841  
842 my $identitygroup = $ise->identitygroups("id","4fffc260-9b96-11e6-93fb-005056ad1454");
843 # Faster call to request specific identity group information by ID (assigned by ISE, present in Net::Cisco::ISE::IdentityGroup)
844
845 $user->id(0); # Required for new user!
846 my $id = $ise->create($user);
847 # Create new user based on Net::Cisco::ISE::InternalUser instance
848 # Return value is ID generated by ISE
849 print "Record ID is $id" if $id;
850 print $Net::Cisco::ISE::ERROR unless $id;
851 # $Net::Cisco::ISE::ERROR contains details about failure
852  
853 my $id = $ise->create(@users); # Still requires nullified ID!
854 # Create new users based on Net::Cisco::ISE::InternalUser instances in arguments
855 # Return value is not guaranteed in this case!
856 # print "Record ID is $id" if $id;
857 # print $Net::Cisco::ISE::ERROR unless $id;
858 # $Net::Cisco::ISE::ERROR contains details about failure
859
860 $identitygroup->id(0); # Required for new record!
861 my $id = $ise->create($identitygroup);
862 # Create new identity group based on Net::Cisco::ISE::IdentityGroup instance
863 # Return value is ID generated by ISE
864 print "Record ID is $id" if $id;
865 print $Net::Cisco::ISE::ERROR unless $id;
866 # $Net::Cisco::ISE::ERROR contains details about failure
867  
868 # Cisco ISE does not support modifying an identity group through the API
869
870 my $id = $ise->update($user);
871 # Update existing user based on Net::Cisco::ISE::InternalUser instance
872 # Return value is ID generated by ISE
873 print "Record ID is $id" if $id;
874 print $Net::Cisco::ISE::ERROR unless $id;
875 # $Net::Cisco::ISE::ERROR contains details about failure
876  
877 my $id = $ise->update(@users);
878 # Update existing users based on Net::Cisco::ISE::InternalUser instances in arguments
879 # Return value is not guaranteed in this case!
880 # print "Record ID is $id" if $id;
881 # print $Net::Cisco::ISE::ERROR unless $id;
882 # $Net::Cisco::ISE::ERROR contains details about failure
883
884 $ise->delete($user);
885 # Delete existing user based on Net::Cisco::ISE::InternalUser instance
886
887 =head1 DESCRIPTION
888  
889 Net::Cisco::ISE is an implementation of the Cisco Secure Access Control System (ISE) REST API. Cisco ISE is a application / appliance that can be used for network access policy control. In short, it allows configuration of access policies for specific users onto specific devices and applications (either using RADIUS or TACISE+ authentication). Net::Cisco::ISE currently supports InternalUser and IdentityGroup.
890  
891 =head1 USAGE
892  
893 All calls are handled through an instance of the L<Net::Cisco::ISE> class.
894  
895 use Net::Cisco::ISE;
896 my $ise = Net::Cisco::ISE->new(hostname => '10.0.0.1', username => 'admin', password => 'testPassword');
897  
898 =over 3
899  
900 =item new
901  
902 Class constructor. Returns object of Net::Cisco::ISE on succes. Required fields are:
903  
904 =over 5
905  
906 =item hostname
907  
908 =item username
909  
910 =item password
911  
912 =back
913  
914 Optional fields are
915  
916 =over 5
917  
918 =item ssl
919  
920 =item ssl_options
921  
922 =back
923  
924 =item hostname
925  
926 IP or hostname of Cisco ISE 2.x server. This is a required value in the constructor but can be redefined afterwards.
927  
928 =item username
929  
930 Username of Administrator user. This is a required value in the constructor but can be redefined afterwards.
931  
932 =item password
933  
934 Password of user. This is a required value in the constructor but can be redefined afterwards.
935  
936 =item ssl
937  
938 SSL enabled (1 - default) or disabled (0).
939  
940 =item ssl_options
941  
942 Value is passed directly to LWP::UserAGent as ssl_opt. Default value (hash-ref) is
943  
944 { 'SSL_verify_mode' => SSL_VERIFY_NONE, 'verify_hostname' => '0' }
945  
946 =back
947  
948 From the class instance, call the different methods for retrieving values.
949  
950 =over 3
951  
952 =item users
953  
954 Returns hash or single instance, depending on context.
955  
956 my %users = $ise->internalusers(); # Slow
957 my $user = $ise->internalusers()->{"admin"};
958 print $user->name;
959
960 The returned hash contains instances of L<Net::Cisco::ISE::InternalUser>, using name (typically the username) as the hash key. Using a call to C<users> with no arguments will retrieve all users and can take quite a few seconds (depending on the size of your database). When you know the username or ID, use the L<users> call with arguments as listed below.
961
962 my $user = $ise->internalusers("name","admin"); # Faster
963 # or
964 my $user = $ise->internalusers("id","b74a0ef2-b29c-40e3-a0d1-4c0dfb51ace9"); # Faster
965 print $user->name;
966  
967 The ID is typically generated by Cisco ISE when the entry is created. It can be retrieved by calling the C<id> method on the object.
968  
969 print $user->id;
970  
971 =item identitygroups
972  
973 Returns hash or single instance, depending on context.
974  
975 my %identitygroups = $ise->identitygroups(); # Slow
976 my $identitygroup = $ise->identitygroups()->{"All Groups"};
977 print $identitgroup->name;
978
979 The returned hash contains instances of L<Net::Cisco::ISE::IdentityGroup>, using name (typically the username) as the hash key. Using a call to C<identitygroup> with no arguments will retrieve all identitygroups and can take quite a few seconds (depending on the size of your database). When you know the group name or ID, use the L<identitygroups> call with arguments as listed below.
980
981 my $identitygroup = $ise->identitygroups("name","All Groups"); # Faster
982 # or
983 my $identitygroup = $ise->identitygroups("id","4fffc260-9b96-11e6-93fb-005056ad1454"); # Faster
984 print $identitygroup->name;
985  
986 The ID is typically generated by Cisco ISE when the entry is created. It can be retrieved by calling the C<id> method on the object.
987  
988 print $identitygroup->id;
989
990 =item create
991  
992 This method created a new entry in Cisco ISE, depending on the argument passed. Record type is detected automatically. For all record types, the ID value must be set to 0.
993  
994 my $user = $ise->internalusers("name","admin");
995 $user->id(0); # Required for new user!
996 $user->name("altadmin"); # Required field
997 $user->password("TopSecret"); # Password policies will be enforced!
998 $user->description("Alternate Admin");
999 my $id = $ise->create($user);
1000 # Create new user based on Net::Cisco::ISE::InternalUser instance
1001 # Return value is ID generated by ISE
1002 print "Record ID is $id" if $id;
1003 print $Net::Cisco::ISE::ERROR unless $id;
1004 # $Net::Cisco::ISE::ERROR contains details about failure
1005  
1006 Multiple instances can be passed as an argument. Objects will be created in bulk (one transaction). The returned ID is not guaranteed to be the IDs of the created objects.
1007  
1008 my $user = $ise->internalusers("name","admin");
1009 $user->id(0); # Required for new user!
1010 $user->name("altadmin"); # Required field
1011 $user->password("TopSecret"); # Password policies will be enforced!
1012 $user->description("Alternate Admin");
1013  
1014 my $user2 = $ise->internalusers("name","admin");
1015 $user2->id(0); # Required for new user!
1016 $user2->name("altadmin"); # Required field
1017 $user2->password("TopSecret"); # Password policies will be enforced!
1018 $user2->description("Alternate Admin");
1019  
1020 my $id = $ise->create($user,$user2);
1021 # Create new users based on Net::Cisco::ISE::InternalUser instances in argument.
1022 # Return value is ID generated by ISE but not guaranteed.
1023 # print "Record ID is $id" if $id;
1024 # print $Net::Cisco::ISE::ERROR unless $id;
1025 # $Net::Cisco::ISE::ERROR contains details about failure
1026
1027 =item update
1028  
1029 This method updates an existing entry in Cisco ISE, depending on the argument passed. Record type is detected automatically.
1030  
1031 my $user = $ise->internalusers("name","admin");
1032 $user->password("TopSecret"); # Change password. Password policies will be enforced!
1033 my $id = $ise->update($user);
1034 # Update user based on Net::Cisco::ISE::InternalUser instance
1035 # Return value is ID generated by ISE
1036 print "Record ID is $id" if $id;
1037 print $Net::Cisco::ISE::ERROR unless $id;
1038 # $Net::Cisco::ISE::ERROR contains details about failure
1039  
1040 Multiple instances can be passed as an argument. Objects will be updated in bulk (one transaction). The returned ID is not guaranteed to be the IDs of the created objects.
1041  
1042 my $user = $ise->internalusers("name","admin");
1043 $user->id(0); # Required for new user!
1044 $user->password("TopSecret"); # Password policies will be enforced!
1045  
1046 my $user2 = $ise->internalusers("name","admin2");
1047 $user2->password("TopSecret"); # Password policies will be enforced!
1048  
1049 my $id = $ise->update($user,$user2);
1050 # Update users based on Net::Cisco::ISE::InternalUser instances in arguments
1051 # Return value is ID generated by ISE but not guaranteed.
1052 # print "Record ID is $id" if $id;
1053 # print $Net::Cisco::ISE::ERROR unless $id;
1054 # $Net::Cisco::ISE::ERROR contains details about failure
1055
1056 =item delete
1057  
1058 This method deletes an existing entry in Cisco ISE, depending on the argument passed. Record type is detected automatically.
1059  
1060 my $user = $ise->internalusers("name","admin");
1061 $ise->delete($user);
1062  
1063 =item $ERROR
1064  
1065 This variable will contain detailed error information, based on the REST API answer. This value is reset during every call to C<internalusers> and C<identitygroups>.
1066
1067 =back
1068  
1069 =head1 REQUIREMENTS
1070  
1071 For this library to work, you need an instance with Cisco ISE (obviously) or a simulator like L<Net::Cisco::ISE::Mock>.
1072  
1073 Instructions on enabling Cisco ISE for API access will be added later.
1074  
1075 You will also need
1076  
1077 =over 3
1078  
1079 =item L<Moose>
1080  
1081 =item L<IO::Socket::SSL>
1082  
1083 =item L<LWP::UserAgent>
1084  
1085 =item L<XML::Simple>
1086  
1087 =item L<MIME::Base64>
1088  
1089 =item L<URI::Escape>
1090  
1091 =back
1092
1093 =head1 BUGS
1094  
1095 None so far
1096  
1097 =head1 SUPPORT
1098  
1099 None so far :)
1100  
1101 =head1 AUTHOR
1102  
1103 Hendrik Van Belleghem
1104 CPAN ID: BEATNIK
1105 hendrik.vanbelleghem@gmail.com
1106  
1107 =head1 COPYRIGHT
1108  
1109 This program is free software licensed under the...
1110  
1111 The General Public License (GPL)
1112 Version 2, June 1991
1113  
1114 The full text of the license can be found in the
1115 LICENSE file included with this module.
1116  
1117 =head1 COMPATIBILITY
1118  
1119 Certain API calls are not support from Cisco ISE 5.0 onwards. The current supported versions of Cisco ISE (by Cisco) are 5.6, 5.7 and 5.8 (Active).
1120  
1121 =head1 SEE ALSO
1122  
1123 =over 3
1124  
1125 See L<Net::Cisco::ISE::InternalUser> for more information on User management.
1126  
1127 See L<Net::Cisco::ISE::IdentityGroup> for more information on User Group management.
1128  
1129 See the L<Cisco ISE product page|http://www.cisco.com/c/en/us/products/security/secure-access-control-system/index.html> for more information.
1130  
1131 L<Net::Cisco::ISE> relies on L<Moose>.
1132  
1133 =back
1134  
1135 =cut
1136  
1137 #################### main pod documentation end ###################
1138  
1139 __PACKAGE__->meta->make_immutable();
1140  
1141 1;
1142 # The preceding line will help the module return a true value
1143