File Coverage

blib/lib/Net/SecurityCenter/API/ScanResult.pm
Criterion Covered Total %
statement 67 119 56.3
branch 7 28 25.0
condition n/a
subroutine 13 20 65.0
pod 12 12 100.0
total 99 179 55.3


line stmt bran cond sub pod time code
1             package Net::SecurityCenter::API::ScanResult;
2              
3 2     2   764 use warnings;
  2         4  
  2         74  
4 2     2   9 use strict;
  2         5  
  2         61  
5              
6 2     2   11 use Carp;
  2         3  
  2         94  
7 2     2   1144 use IO::Uncompress::Unzip qw(unzip $UnzipError);
  2         65484  
  2         179  
8              
9 2     2   15 use parent 'Net::SecurityCenter::Base';
  2         4  
  2         12  
10              
11 2     2   107 use Net::SecurityCenter::Utils qw(:all);
  2         4  
  2         3520  
12              
13             our $VERSION = '0.310';
14              
15             my $common_template = {
16              
17             id => {
18             required => 1,
19             allow => qr/^\d+$/,
20             messages => {
21             required => 'Scan Result ID is required',
22             allow => 'Invalid Scan Result ID',
23             },
24             },
25              
26             filter => {
27             allow => [ 'usable', 'manageable', 'running', 'completed' ]
28             },
29              
30             fields => {
31             allow => \&sc_filter_array_to_string,
32             }
33              
34             };
35              
36             #-------------------------------------------------------------------------------
37             # METHODS
38             #-------------------------------------------------------------------------------
39              
40             sub download {
41              
42 0     0 1 0 my ( $self, %args ) = @_;
43              
44             my $tmpl = {
45             filename => {},
46 0         0 id => $common_template->{'id'},
47             };
48              
49 0         0 my $params = sc_check_params( $tmpl, \%args );
50              
51 0         0 my $scan_result_id = delete( $params->{'id'} );
52 0         0 my $filename = delete( $params->{'filename'} );
53              
54 0         0 my $sc_scan_data = $self->client->post( "/scanResult/$scan_result_id/download", { 'downloadType' => 'v2' } );
55 0         0 my $nessus_scan_data = q{};
56              
57 0 0       0 return if ( !$sc_scan_data );
58              
59 0 0       0 if ($sc_scan_data) {
60 0 0       0 unzip \$sc_scan_data => \$nessus_scan_data or croak "Failed to uncompress Nessus scan: $UnzipError\n";
61             }
62              
63 0 0       0 return $nessus_scan_data if ( !$filename );
64              
65 0 0       0 open my $fh, '>', $filename
66             or croak("Could not open file '$filename': $!");
67              
68 0         0 print $fh $nessus_scan_data;
69              
70 0 0       0 close $fh
71             or carp("Failed to close file '$filename': $!");
72              
73 0         0 return 1;
74              
75             }
76              
77             #-------------------------------------------------------------------------------
78              
79             sub list {
80              
81 1     1 1 4 my ( $self, %args ) = @_;
82              
83             my $tmpl = {
84             fields => $common_template->{'fields'},
85 1         19 filter => $common_template->{'filter'},
86             raw => {},
87             start_date => {
88             filter => \&sc_filter_datetime_to_epoch,
89             remap => 'startTime',
90             },
91             end_date => {
92             filter => \&sc_filter_datetime_to_epoch,
93             remap => 'endTime',
94             },
95             start_time => {
96             allow => qr/^\d+$/,
97             remap => 'startTime'
98             },
99             end_time => {
100             allow => qr/^\d+$/,
101             remap => 'endTime'
102             }
103             };
104              
105 1         6 my $params = sc_check_params( $tmpl, \%args );
106 1         3 my $raw = delete( $params->{'raw'} );
107 1         8 my $scans = $self->client->get( '/scanResult', $params );
108              
109 1 50       5 return if ( !$scans );
110              
111 1 50       4 if ($raw) {
112 0 0       0 return wantarray ? @{$scans} : $scans;
  0         0  
113             }
114              
115 1 50       5 return wantarray ? @{ sc_merge($scans) } : sc_merge($scans);
  0         0  
116              
117             }
118              
119             #-------------------------------------------------------------------------------
120              
121             sub list_running {
122              
123 0     0 1 0 my ( $self, %args ) = @_;
124              
125 0         0 my $tmpl = { fields => $common_template->{'fields'}, raw => {}, };
126              
127 0         0 my $params = sc_check_params( $tmpl, \%args );
128              
129 0         0 $params->{'filter'} = 'running';
130              
131 0         0 return $self->list( %{$params} );
  0         0  
132              
133             }
134              
135             #-------------------------------------------------------------------------------
136              
137             sub list_completed {
138              
139 0     0 1 0 my ( $self, %args ) = @_;
140              
141 0         0 my $tmpl = { fields => $common_template->{'fields'}, raw => {}, };
142              
143 0         0 my $params = sc_check_params( $tmpl, \%args );
144              
145 0         0 $params->{'filter'} = 'completed';
146              
147 0         0 return $self->list( %{$params} );
  0         0  
148              
149             }
150              
151             #-------------------------------------------------------------------------------
152              
153             sub get {
154              
155 3     3 1 9 my ( $self, %args ) = @_;
156              
157             my $tmpl = {
158             id => $common_template->{'id'},
159 3         10 fields => $common_template->{'fields'},
160             raw => {},
161             };
162              
163 3         11 my $params = sc_check_params( $tmpl, \%args );
164 3         6 my $scan_result_id = delete( $params->{'id'} );
165 3         6 my $raw = delete( $params->{'raw'} );
166              
167 3         11 my $scan_result = $self->client->get( "/scanResult/$scan_result_id", $params );
168              
169 3 50       10 return if ( !$scan_result );
170 3 50       6 return $scan_result if ($raw);
171              
172 3         12 return sc_normalize_hash($scan_result);
173              
174             }
175              
176             #-------------------------------------------------------------------------------
177              
178             sub progress {
179              
180 1     1 1 4 my ( $self, %args ) = @_;
181              
182 1         4 my $tmpl = { id => $common_template->{'id'}, };
183              
184 1         4 my $params = sc_check_params( $tmpl, \%args );
185 1         3 my $scan_result_id = delete( $params->{'id'} );
186              
187 1         4 my $scan_data = $self->get(
188             id => $scan_result_id,
189             fields => [ 'id', 'totalChecks', 'completedChecks' ]
190             );
191              
192 1 50       4 return if ( !$scan_data );
193              
194 1         23 return sprintf( '%d', ( $scan_data->{'completedChecks'} * 100 ) / $scan_data->{'totalChecks'} );
195              
196             }
197              
198             #-------------------------------------------------------------------------------
199              
200             sub status {
201              
202 1     1 1 4 my ( $self, %args ) = @_;
203              
204 1         3 my $tmpl = { id => $common_template->{'id'}, };
205              
206 1         4 my $params = sc_check_params( $tmpl, \%args );
207 1         6 my $scan_result_id = delete( $params->{'id'} );
208              
209 1         6 my $scan_data = $self->get(
210             id => $scan_result_id,
211             fields => [ 'id', 'status' ]
212             );
213              
214 1 50       5 return if ( !$scan_data );
215              
216 1         19 return lc( $scan_data->{'status'} );
217              
218             }
219              
220             #-------------------------------------------------------------------------------
221              
222             sub pause {
223              
224 1     1 1 3 my ( $self, %args ) = @_;
225              
226 1         4 my $tmpl = { id => $common_template->{'id'}, };
227              
228 1         5 my $params = sc_check_params( $tmpl, \%args );
229 1         3 my $scan_result_id = delete( $params->{'id'} );
230              
231 1         5 $self->client->post("/scanResult/$scan_result_id/pause");
232 1         7 return 1;
233              
234             }
235              
236             #-------------------------------------------------------------------------------
237              
238             sub resume {
239              
240 1     1 1 4 my ( $self, %args ) = @_;
241              
242 1         4 my $tmpl = { id => $common_template->{'id'}, };
243              
244 1         4 my $params = sc_check_params( $tmpl, \%args );
245 1         3 my $scan_result_id = delete( $params->{'id'} );
246              
247 1         5 $self->client->post("/scanResult/$scan_result_id/resume");
248 1         5 return 1;
249              
250             }
251              
252             #-------------------------------------------------------------------------------
253              
254             sub reimport {
255              
256 0     0 1 0 my ( $self, %args ) = @_;
257              
258             my $tmpl = {
259 0         0 id => $common_template->{'id'},
260             scan_vhost => {
261             remap => 'scanningVirtualHosts',
262             filter => \&sc_filter_int_to_bool,
263             allow => qr/\d/,
264             },
265             dhcp_tracking => {
266             remap => 'dhcpTracking',
267             filter => \&sc_filter_int_to_bool,
268             allow => qr/\d/,
269             },
270             classify_mitigated_age => { remap => 'classifyMitigatedAge' }
271             };
272              
273 0         0 my $params = sc_check_params( $tmpl, \%args );
274 0         0 my $scan_result_id = delete( $params->{'id'} );
275              
276 0         0 $self->client->post("/scanResult/$scan_result_id/import");
277 0         0 return 1;
278              
279             }
280              
281             #-------------------------------------------------------------------------------
282              
283             sub import {
284              
285 0     0   0 my ( $self, %args ) = @_;
286              
287             my $single_id_filter = sub {
288 0     0   0 return { 'id' => $_[0] };
289 0         0 };
290              
291 0         0 my $tmpl = {
292             filename => {},
293             dhcp_tracking => {
294             remap => 'dhcpTracking',
295             filter => \&sc_filter_int_to_bool,
296             allow => qr/\d/,
297             },
298             classify_mitigated_age => { remap => 'classifyMitigatedAge' },
299             scan_vhost => {
300             remap => 'scanningVirtualHosts',
301             filter => \&sc_filter_int_to_bool,
302             allow => qr/\d/,
303             },
304             repository => {
305             allow => qr/\d+/,
306             errors => { allow => 'Invalid Repository ID' },
307             filter => \&$single_id_filter
308             },
309             };
310              
311 0         0 my $params = sc_check_params( $tmpl, \%args );
312 0         0 my $filename = delete( $params->{'filename'} );
313 0         0 my $sc_filename = $self->client->upload($filename)->{'filename'};
314              
315 0         0 $params->{'filename'} = $sc_filename;
316              
317 0         0 $self->client->post( "/scanResult/import", $params );
318 0         0 return 1;
319              
320             }
321              
322             #-------------------------------------------------------------------------------
323              
324             sub email {
325              
326 0     0 1 0 my ( $self, %args ) = @_;
327              
328 0         0 my $tmpl = { id => $common_template->{'id'}, email => {} };
329              
330 0         0 my $params = sc_check_params( $tmpl, \%args );
331 0         0 my $scan_result_id = delete( $params->{'id'} );
332              
333 0         0 $self->client->post( "/scanResult/$scan_result_id/import", $params );
334 0         0 return 1;
335              
336             }
337              
338             #-------------------------------------------------------------------------------
339              
340             sub stop {
341              
342 1     1 1 5 my ( $self, %args ) = @_;
343              
344             my $tmpl = {
345 1         6 id => $common_template->{'id'},
346             type => {
347             allow => ['import']
348             }
349             };
350              
351 1         6 my $params = sc_check_params( $tmpl, \%args );
352 1         3 my $scan_result_id = delete( $params->{'id'} );
353              
354 1         6 $self->client->post( "/scanResult/$scan_result_id/stop", $params );
355 1         6 return 1;
356              
357             }
358              
359             #-------------------------------------------------------------------------------
360              
361             1;
362              
363             __END__