Zonalizer/Server.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 96.6% 58 / 60 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Zonalizer/Collector.pm ↑ Summary Zonalizer/CLI.pm »
1 package Lim::Plugin::Zonalizer::Server;
2  
3 8 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Lim::Plugin::Zonalizer qw(:err :status);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Lim::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Data::UUID ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use URI::Escape::XS qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use AnyEvent::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 0 ▶use Zonemaster ();
Subroutine BEGIN: called CC=1 SCAR=2.6
22 use Zonemaster::Translator ();
23 use Zonemaster::Logger::Entry ();
24 use POSIX qw(setlocale LC_MESSAGES);
25  
26 use base qw(Lim::Component::Server);
27  
28 =encoding utf8
29  
30 =head1 NAME
31  
32 Lim::Plugin::Zonalizer::Server - Server class for the zonalizer Lim plugin
33  
34 =head1 VERSION
35  
36 See L<Lim::Plugin::Zonalizer> for version.
37  
38 =cut
39  
40 our $VERSION = $Lim::Plugin::Zonalizer::VERSION;
41  
42 our %STAT = (
43 api => {
44 requests => 0,
45 errors => 0
46 },
47 analysis => {
48 ongoing => 0,
49 completed => 0,
50 failed => 0
51 }
52 );
53  
54 our %TEST;
55 our %TEST_DB;
56 our %TEST_SPACE;
57  
58 our $TRANSLATOR;
59  
60 =head1 SYNOPSIS
61  
62 use Lim::Plugin::Zonalizer;
63  
64 # Create a Server object
65 $server = Lim::Plugin::Zonalizer->Server;
66  
67 =head1 METHODS
68  
69 These methods are called from the Lim framework and should not be used else
70 where.
71  
72 Please see L<Lim::Plugin::Zonalizer> for full documentation of calls.
73  
74 =over 4
75  
76 =item Init
77  
78 =cut
79  
80 sub Init {
81 my ( $self ) = @_;
82 my $real_self = $self;
83 weaken( $self );
84  
85 #
86 # Default configuration
87 #
88  
89 $self->{default_limit} = 10;
90 $self->{max_limit} = 10;
91 $self->{base_url} = 1;
92 $self->{db_driver} = 'Memory';
93 $self->{db_conf} = {};
94 $self->{lang} = $ENV{LC_MESSAGES} || $ENV{LC_ALL} || $ENV{LANG} || $ENV{LANGUAGE} || 'en_US';
95 $self->{test_ipv4} = 1;
96 $self->{test_ipv6} = 1;
97 $self->{allow_ipv4} = 1;
98 $self->{allow_ipv6} = 1;
99 $self->{max_ongoing} = 5;
100 $self->{collector} = {
101 exec => '/usr/bin/zonalizer-collector',
102 threads => 5
103 };
104 $self->{collector_policy} = {};
105 $self->{allow_undelegated} = 1;
106 $self->{force_undelegated} = 0;
107 $self->{max_undelegated_ns} = 10;
108 $self->{max_undelegated_ds} = 10;
109 $self->{allow_meta_data} = 0;
110 $self->{max_meta_data_entries} = 42;
111 $self->{max_meta_data_entry_size} = 512;
112  
113 #
114 # Load configuration
115 #
116  
117 if ( ref( Lim->Config->{zonalizer} ) eq 'HASH' ) {
118 foreach (
119 qw(default_limit max_limit base_url db_driver custom_base_url
120 lang test_ipv4 test_ipv6 allow_ipv4 allow_ipv6 max_ongoing
121 allow_undelegated force_undelegated max_undelegated_ns
122 max_undelegated_ds allow_meta_data max_meta_data_entries
123 max_meta_data_entry_size)
124 )
125 {
126 if ( defined Lim->Config->{zonalizer}->{$_} ) {
127 $self->{$_} = Lim->Config->{zonalizer}->{$_};
128 }
129 }
130  
131 if ( defined Lim->Config->{zonalizer}->{db_conf} ) {
132 unless ( ref( Lim->Config->{zonalizer}->{db_conf} ) eq 'HASH' ) {
133 confess 'Configuration for db_conf is wrong, must be HASH';
134 }
135  
136 $self->{db_conf} = Lim->Config->{zonalizer}->{db_conf};
137 }
138  
139 if ( defined Lim->Config->{zonalizer}->{collector} ) {
140 unless ( ref( Lim->Config->{zonalizer}->{collector} ) eq 'HASH' ) {
141 confess 'Configuration for collector is wrong, must be HASH';
142 }
143  
144 foreach ( qw(exec config policy sourceaddr threads) ) {
145 if ( defined Lim->Config->{zonalizer}->{collector}->{$_} ) {
146 $self->{collector}->{$_} = Lim->Config->{zonalizer}->{collector}->{$_};
147 }
148 }
149  
150 if ( defined Lim->Config->{zonalizer}->{collector}->{policies} ) {
151 unless ( ref( Lim->Config->{zonalizer}->{collector}->{policies} ) eq 'ARRAY' ) {
152 confess 'Configuration for collector->policies is wrong, must be ARRAY';
153 }
154  
155 my $count = 1;
156 foreach my $policy ( @{ Lim->Config->{zonalizer}->{collector}->{policies} } ) {
157 unless ( ref( $policy ) eq 'HASH' ) {
158 confess 'Configuration for collector->policies[' . $count . '] is wrong, must be HASH';
159 }
160  
161 foreach ( qw(name display policy) ) {
162 unless ( defined $policy->{$_} ) {
163 confess 'Configuration for collector->policies[' . $count . ']->' . $_ . ' is wrong, must be defined';
164 }
165 }
166  
167 foreach ( qw(description exec config sourceaddr threads) ) {
168 if ( exists $policy->{$_} and !defined $policy->{$_} ) {
169 confess 'Configuration for collector->policies[' . $count . ']->' . $_ . ' is wrong, must be defined';
170 }
171 if ( !exists $policy->{$_} and exists $self->{collector}->{$_} ) {
172 $policy->{$_} = $self->{collector}->{$_};
173 }
174 }
175  
176 unless ( $policy->{name} =~ /^[\w-]+/o ) {
177 confess 'Configuration for collector->policies[' . $count . '] is wrong, illegal characters in policy name';
178 }
179 if ( exists $self->{collector_policy}->{ $policy->{name} } ) {
180 confess 'Configuration for collector->policies[' . $count . '] is wrong, policy ' . $policy->{name} . ' already exists';
181 }
182 if ( $policy->{exec} and !-x $policy->{exec} ) {
183 confess 'Configuration for collector->policies[' . $count . '] is wrong, ' . $policy->{exec} . ' is not an executable';
184 }
185  
186 $self->{collector_policy}->{ $policy->{name} } = $policy;
187 $count++;
188 }
189 }
190 }
191 }
192  
193 if ( exists $self->{custom_base_url} ) {
194 $self->{custom_base_url} =~ s/[\/\s]+$//o;
195 }
196  
197 unless ( $self->{allow_ipv4} ) {
198 $self->{test_ipv4} = 0;
199 }
200 unless ( $self->{allow_ipv6} ) {
201 $self->{test_ipv6} = 0;
202 }
203 unless ( $self->{allow_ipv4} || $self->{allow_ipv6} ) {
204 confess 'Configuration error: Must have atleast one of allow_ipv4 or allow_ipv6 set';
205 }
206 unless ( $self->{test_ipv4} || $self->{test_ipv6} ) {
207 confess 'Configuration error: Must have atleast one of test_ipv4 or test_ipv6 set';
208 }
209 unless ( $self->{max_ongoing} > 0 ) {
210 confess 'Configuration error: max_ongoing must be 1 or greater';
211 }
212 unless ( -x $self->{collector}->{exec} ) {
213 confess 'Configuration error: collector->exec ' . $self->{collector}->{exec} . ' is not an executable';
214 }
215 unless ( $self->{collector}->{threads} > 0 ) {
216 confess 'Configuration error: collector->threads must be 1 or greater';
217 }
218 if ( !$self->{allow_undelegated} and $self->{force_undelegated} ) {
219 confess 'Configuration error: allow_undelegated can not be false when force_undelegated is true';
220 }
221 if ( $self->{allow_undelegated} ) {
222 unless ( $self->{max_undelegated_ns} > 0 ) {
223 confess 'Configuration error: max_undelegated_ns must be 1 or greater';
224 }
225 unless ( $self->{max_undelegated_ds} > 0 ) {
226 confess 'Configuration error: max_undelegated_ds must be 1 or greater';
227 }
228 }
229 if ( $self->{allow_meta_data} ) {
230 unless ( $self->{max_meta_data_entries} > 0 ) {
231 confess 'Configuration error: max_meta_data_entries must be 1 or greater';
232 }
233 unless ( $self->{max_meta_data_entry_size} > 0 ) {
234 confess 'Configuration error: max_meta_data_entry_size must be 1 or greater';
235 }
236 }
237  
238 #
239 # Load database
240 #
241  
242 my $db_driver = 'Lim::Plugin::Zonalizer::DB::' . $self->{db_driver};
243 eval 'use ' . $db_driver . ';';
244 if ( $@ ) {
245 confess $@;
246 }
247 $self->{db} = $db_driver->new( %{ $self->{db_conf} } );
248  
249 #
250 # Build translator
251 #
252  
253 $self->{lang} =~ s/\..*$//o;
254  
255 unless ( $self->{lang} =~ /^[a-z]{2}_[A-Z]{2}$/o ) {
256 confess 'Invalid language set for translations';
257 }
258  
259 $TRANSLATOR = Zonemaster::Translator->new;
260 $TRANSLATOR->data;
261  
262 unless ( setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' ) ) {
263 confess 'Unsupported locale, setlocale( ' . $self->{lang} . '.UTF-8 ) failed: ' . $!;
264 }
265  
266 #
267 # Temporary memory scrubber
268 #
269  
270 $self->{cleaner} = AnyEvent->timer(
271 after => 60,
272 interval => 60,
273 cb => sub {
274 unless ( $self ) {
275 return;
276 }
277  
278 foreach ( values %TEST ) {
279 if ( $_->{updated} < ( time - 600 ) ) {
280 delete $TEST{ $_->{id} };
281 delete $TEST_DB{ $_->{id} };
282 delete $TEST_SPACE{ $_->{id} };
283 }
284 }
285 }
286 );
287  
288 #
289 # Start collector
290 #
291  
292 my $code;
293 $code = sub {
294 unless ( defined $self ) {
295 return;
296 }
297  
298 Lim::DEBUG and $self->{logger}->debug( '(re)starting default collector' );
299  
300 $self->{collector}->{analyze} = $self->StartCollector(
301 exec => $self->{collector}->{exec},
302 $self->{collector}->{config} ? ( config => $self->{collector}->{config} ) : (),
303 $self->{collector}->{policy} ? ( policy => $self->{collector}->{policy} ) : (),
304 $self->{collector}->{sourceaddr} ? ( sourceaddr => $self->{collector}->{sourceaddr} ) : (),
305 $self->{collector}->{threads} ? ( threads => $self->{collector}->{threads} ) : (),
306 on_eof => $code
307 );
308 };
309 $code->();
310  
311 foreach ( values %{ $self->{collector_policy} } ) {
312 my $policy = $_;
313 my $code;
314 $code = sub {
315 unless ( defined $self ) {
316 return;
317 }
318  
319 Lim::DEBUG and $self->{logger}->debug( '(re)starting collector for policy ', $policy->{name} );
320  
321 $self->{collector_policy}->{ $policy->{name} }->{analyze} = $self->StartCollector(
322 exec => $policy->{exec},
323 $policy->{config} ? ( config => $policy->{config} ) : (),
324 $policy->{policy} ? ( policy => $policy->{policy} ) : (),
325 $policy->{sourceaddr} ? ( sourceaddr => $policy->{sourceaddr} ) : (),
326 $policy->{threads} ? ( threads => $policy->{threads} ) : (),
327 on_eof => $code
328 );
329 };
330 $code->();
331 }
332 }
333  
334 =item Read1
335  
336 =cut
337  
338 sub Read1 {
339 my ( $self, $cb ) = @_;
340 $STAT{api}->{requests}++;
341  
342 #
343 # This call is only used to map to other calls and should not be called
344 # directly so return an error if anyone does.
345 #
346  
347 $STAT{api}->{errors}++;
348 $self->Error(
349 $cb,
350 Lim::Error->new(
351 module => $self,
352 code => HTTP::Status::HTTP_BAD_REQUEST
353 )
354 );
355 return;
356 }
357  
358 =item Create1
359  
360 =cut
361  
362 sub Create1 {
363 my ( $self, $cb ) = @_;
364 $STAT{api}->{requests}++;
365  
366 #
367 # This call is only used to map to other calls and should not be called
368 # directly so return an error if anyone does.
369 #
370  
371 $STAT{api}->{errors}++;
372 $self->Error(
373 $cb,
374 Lim::Error->new(
375 module => $self,
376 code => HTTP::Status::HTTP_BAD_REQUEST
377 )
378 );
379 return;
380 }
381  
382 =item Update1
383  
384 =cut
385  
386 sub Update1 {
387 my ( $self, $cb ) = @_;
388 $STAT{api}->{requests}++;
389  
390 #
391 # This call is only used to map to other calls and should not be called
392 # directly so return an error if anyone does.
393 #
394  
395 $STAT{api}->{errors}++;
396 $self->Error(
397 $cb,
398 Lim::Error->new(
399 module => $self,
400 code => HTTP::Status::HTTP_BAD_REQUEST
401 )
402 );
403 return;
404 }
405  
406 =item Delete1
407  
408 =cut
409  
410 sub Delete1 {
411 my ( $self, $cb ) = @_;
412 $STAT{api}->{requests}++;
413  
414 #
415 # This call is only used to map to other calls and should not be called
416 # directly so return an error if anyone does.
417 #
418  
419 $STAT{api}->{errors}++;
420 $self->Error(
421 $cb,
422 Lim::Error->new(
423 module => $self,
424 code => HTTP::Status::HTTP_BAD_REQUEST
425 )
426 );
427 return;
428 }
429  
430 =item ReadVersion
431  
432 =cut
433  
434 sub ReadVersion {
435 my ( $self, $cb, $q ) = @_;
436 $STAT{api}->{requests}++;
437  
438 unless ( $q->{version} == 1 ) {
439 $STAT{api}->{errors}++;
440 $self->Error(
441 $cb,
442 Lim::Error->new(
443 module => $self,
444 code => HTTP::Status::HTTP_BAD_REQUEST,
445 message => ERR_INVALID_API_VERSION
446 )
447 );
448 return;
449 }
450  
451 my @tests = (
452 {
453 name => 'Basic',
454 version => Zonemaster::Test::Basic->VERSION
455 }
456 );
457  
458 foreach ( Zonemaster::Test->modules ) {
459 push(
460 @tests,
461 {
462 name => $_,
463 version => ( 'Zonemaster::Test::' . $_ )->VERSION
464 }
465 );
466 }
467  
468 $self->Successful(
469 $cb,
470 {
471 version => $VERSION,
472 zonemaster => {
473 version => Zonemaster->VERSION,
474 tests => \@tests
475 }
476 }
477 );
478 return;
479 }
480  
481 =item ReadStatus
482  
483 =cut
484  
485 sub ReadStatus {
486 my ( $self, $cb, $q ) = @_;
487 $STAT{api}->{requests}++;
488  
489 unless ( $q->{version} == 1 ) {
490 $STAT{api}->{errors}++;
491 $self->Error(
492 $cb,
493 Lim::Error->new(
494 module => $self,
495 code => HTTP::Status::HTTP_BAD_REQUEST,
496 message => ERR_INVALID_API_VERSION
497 )
498 );
499 return;
500 }
501  
502 $self->Successful( $cb, \%STAT );
503 return;
504 }
505  
506 =item ReadAnalysis
507  
508 =cut
509  
510 sub ReadAnalysis {
511 my ( $self, $cb, $q ) = @_;
512 my $real_self = $self;
513 weaken( $self );
514 $STAT{api}->{requests}++;
515  
516 unless ( $q->{version} == 1 ) {
517 $STAT{api}->{errors}++;
518 $self->Error(
519 $cb,
520 Lim::Error->new(
521 module => $self,
522 code => HTTP::Status::HTTP_BAD_REQUEST,
523 message => ERR_INVALID_API_VERSION
524 )
525 );
526 return;
527 }
528  
529 if ( exists $q->{search} and ( !defined $q->{search} || $q->{search} !~ /^(?:(?:[a-zA-Z0-9-]+\.)*(?:[a-zA-Z0-9-]+\.?|\.)|\.(?:[a-zA-Z0-9-]+\.)*(?:[a-zA-Z0-9-]+\.?))$/o ) ) {
530 $STAT{api}->{errors}++;
531 $self->Error(
532 $cb,
533 Lim::Error->new(
534 module => $self,
535 code => HTTP::Status::HTTP_BAD_REQUEST,
536 message => ERR_INVALID_FQDN
537 )
538 );
539 return;
540 }
541  
542 #
543 # Verify limit and set base url if configured/requested.
544 #
545  
546 my $limit = $q->{limit} > 0 ? $q->{limit} : $self->{default_limit};
547 if ( $limit > $self->{max_limit} ) {
548 $limit = $self->{max_limit};
549 }
550 my $base_url = '';
551 if ( exists $self->{custom_base_url} ) {
552 $base_url = $self->{custom_base_url};
553 }
554 elsif ( ( defined $q->{base_url} ? $q->{base_url} : $self->{base_url} ) and $cb->request ) {
555 $base_url = $cb->request->header( 'X-Lim-Base-URL' );
556 }
557  
558 #
559 # Get translator
560 #
561  
562 my ( $translator, $lang, $error ) = $self->GetTranslator( $q->{lang} );
563  
564 unless ( $translator and $lang ) {
565 $self->Error(
566 $cb,
567 $error ? $error : Lim::Error->new(
568 module => $self,
569 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
570 )
571 );
572 return;
573 }
574  
575 #
576 # Returned in memory ongoing analysis if requested.
577 #
578  
579 if ( defined $q->{ongoing} && $q->{ongoing} == 1 ) {
580 my @analysis;
581  
582 foreach ( sort { $b->{update} <=> $a->{update} } values %TEST ) {
583 if ( $q->{space} ) {
584 unless ( exists $TEST_SPACE{ $_->{id} }
585 and $TEST_SPACE{ $_->{id} } eq $q->{space} )
586 {
587 next;
588 }
589 }
590 else {
591 if ( exists $TEST_SPACE{ $_->{id} } ) {
592 next;
593 }
594 }
595  
596 my %test = %{$_};
597  
598 unless ( $q->{results} ) {
599 delete $test{results};
600 }
601 else {
602 my @results;
603  
604 setlocale( LC_MESSAGES, $lang . '.UTF-8' );
605 foreach my $result ( @{ $test{results} } ) {
606 my $entry = Zonemaster::Logger::Entry->new( $result );
607  
608 my $message = $translator->translate_tag( $entry );
609 utf8::decode( $message );
610  
611 push(
612 @results,
613 {
614 %$result,
615 message => $message
616 }
617 );
618 }
619 setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' );
620  
621 $test{results} = \@results;
622 }
623 $test{url} = $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis/' . uri_escape( $test{id} );
624 if ( $q->{space} ) {
625 $test{url} .= '?space' . uri_escape( $q->{space} );
626 }
627  
628 push( @analysis, \%test );
629  
630 if ( scalar @analysis == $limit ) {
631 last;
632 }
633 }
634  
635 $self->Successful( $cb, { analysis => \@analysis } );
636 return;
637 }
638  
639 #
640 # Query database for analysis.
641 #
642  
643 $self->{db}->ReadAnalysis(
644 %$q,
645 limit => $limit,
646 cb => sub {
647 my $paging = shift;
648 my @analysis;
649  
650 # uncoverable branch true
651 unless ( defined $self ) {
652  
653 # uncoverable statement
654 return;
655 }
656  
657 if ( $@ ) {
658 $STAT{api}->{errors}++;
659  
660 if ( $@ eq ERR_INVALID_LIMIT or $@ eq ERR_INVALID_SORT_FIELD ) {
661 $self->Error(
662 $cb,
663 Lim::Error->new(
664 module => $self,
665 code => HTTP::Status::HTTP_BAD_REQUEST,
666 message => $@
667 )
668 );
669 return;
670 }
671  
672 # uncoverable branch false
673 Lim::ERR and $self->{logger}->error( $@ );
674 $self->Error(
675 $cb,
676 Lim::Error->new(
677 module => $self,
678 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
679 )
680 );
681 return;
682 }
683  
684 #
685 # Construct the result
686 #
687  
688 if ( defined $q->{results} and $q->{results} == 1 ) {
689 setlocale( LC_MESSAGES, $lang . '.UTF-8' );
690 foreach ( @_ ) {
691 unless ( ref( $_->{results} ) eq 'ARRAY' ) {
692 next;
693 }
694  
695 foreach my $result ( @{ $_->{results} } ) {
696 my $entry = Zonemaster::Logger::Entry->new( $result );
697 $result->{message} = $translator->translate_tag( $entry );
698 utf8::decode( $result->{message} );
699 }
700 }
701 setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' );
702 }
703 foreach ( @_ ) {
704 my ( @ns, @ds, @meta_data );
705  
706 if ( $_->{ns} ) {
707 foreach my $ns ( @{ $_->{ns} } ) {
708 push(
709 @ns,
710 {
711 fqdn => $ns->{fqdn},
712 exists $ns->{ip} ? ( ip => $ns->{ip} ) : ()
713 }
714 );
715 }
716 }
717  
718 if ( $_->{ds} ) {
719 foreach my $ds ( @{ $_->{ds} } ) {
720 push(
721 @ds,
722 {
723 keytag => $ds->{keytag},
724 algorithm => $ds->{algorithm},
725 type => $ds->{type},
726 digest => $ds->{digest}
727 }
728 );
729 }
730 }
731  
732 if ( $_->{meta_data} ) {
733 foreach my $meta_data ( @{ $_->{meta_data} } ) {
734 push(
735 @meta_data,
736 {
737 key => $meta_data->{key},
738 value => $meta_data->{value}
739 }
740 );
741 }
742 }
743  
744 push(
745 @analysis,
746 {
747 id => $_->{id},
748 fqdn => $_->{fqdn},
749 exists $_->{policy}
750 ? (
751 policy => {
752 name => $_->{policy}->{name},
753 display => $_->{policy}->{display},
754 $_->{policy}->{description} ? ( description => $_->{policy}->{description} ) : ()
755 }
756 )
757 : (),
758 url => $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis/' . uri_escape( $_->{id} ) . ( $q->{space} ? '?space=' . uri_escape( $q->{space} ) : '' ),
759 status => $_->{status},
760 progress => $_->{progress},
761 created => $_->{created},
762 updated => $_->{updated},
763 exists $_->{error} ? ( error => $_->{error} ) : (),
764 defined $q->{results} && $q->{results} == 1 && exists $_->{results} ? ( results => $_->{results} ) : (),
765 summary => {
766 notice => $_->{summary}->{notice},
767 warning => $_->{summary}->{warning},
768 error => $_->{summary}->{error},
769 critical => $_->{summary}->{critical}
770 },
771 ipv4 => $_->{ipv4},
772 ipv6 => $_->{ipv6},
773 scalar @ns ? ( ns => \@ns ) : (),
774 scalar @ds ? ( ds => \@ds ) : (),
775 scalar @meta_data ? ( meta_data => \@meta_data ) : ()
776 }
777 );
778 }
779  
780 $self->Successful(
781 $cb,
782 {
783 analysis => \@analysis,
784 defined $paging
785 ? (
786 paging => {
787 cursors => {
788 after => $paging->{after},
789 before => $paging->{before}
790 },
791 $paging->{previous} ? ( previous => $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis?limit=' . uri_escape( $limit ) . '&before=' . uri_escape( $paging->{before} ) . ( defined $q->{sort} ? '&sort=' . uri_escape( $q->{sort} ) : '' ) . ( defined $q->{direction} ? '&direction=' . uri_escape( $q->{direction} ) : '' ) . ( defined $paging->{extra} ? '&' . $paging->{extra} : '' ) ) : (),
792 $paging->{next} ? ( next => $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis?limit=' . uri_escape( $limit ) . '&after=' . uri_escape( $paging->{after} ) . ( defined $q->{sort} ? '&sort=' . uri_escape( $q->{sort} ) : '' ) . ( defined $q->{direction} ? '&direction=' . uri_escape( $q->{direction} ) : '' ) . ( defined $paging->{extra} ? '&' . $paging->{extra} : '' ) ) : ()
793 }
794 )
795 : ()
796 }
797 );
798 }
799 );
800 return;
801 }
802  
803 =item DeleteAnalysis
804  
805 =cut
806  
807 sub DeleteAnalysis {
808 my ( $self, $cb, $q ) = @_;
809 my $real_self = $self;
810 weaken( $self );
811 $STAT{api}->{requests}++;
812  
813 unless ( $q->{version} == 1 ) {
814 $STAT{api}->{errors}++;
815 $self->Error(
816 $cb,
817 Lim::Error->new(
818 module => $self,
819 code => HTTP::Status::HTTP_BAD_REQUEST,
820 message => ERR_INVALID_API_VERSION
821 )
822 );
823 return;
824 }
825  
826 $self->{db}->DeleteAnalysis(
827 $q->{space} ? ( space => $q->{space} ) : (),
828 cb => sub {
829 my ( $deleted_analysis ) = @_;
830  
831 # uncoverable branch true
832 unless ( defined $self ) {
833  
834 # uncoverable statement
835 return;
836 }
837  
838 if ( $@ ) {
839 $STAT{api}->{errors}++;
840  
841 # uncoverable branch false
842 Lim::ERR and $self->{logger}->error( $@ );
843 $self->Error(
844 $cb,
845 Lim::Error->new(
846 module => $self,
847 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
848 )
849 );
850 return;
851 }
852  
853 if ( $q->{space} ) {
854 foreach ( keys %TEST_SPACE ) {
855 if ( $TEST_SPACE{$_} eq $q->{space} ) {
856 delete $TEST{$_};
857 delete $TEST_DB{$_};
858 delete $TEST_SPACE{$_};
859 }
860 }
861 }
862 else {
863 %TEST = ();
864 %TEST_DB = ();
865 %TEST_SPACE = ();
866 }
867  
868 $self->Successful( $cb );
869 }
870 );
871 return;
872 }
873  
874 =item CreateAnalyze
875  
876 =cut
877  
878 sub CreateAnalyze {
879 my ( $self, $cb, $q ) = @_;
880 my $real_self = $self;
881 weaken( $self );
882 $STAT{api}->{requests}++;
883  
884 unless ( $q->{version} == 1 ) {
885 $STAT{api}->{errors}++;
886 $self->Error(
887 $cb,
888 Lim::Error->new(
889 module => $self,
890 code => HTTP::Status::HTTP_BAD_REQUEST,
891 message => ERR_INVALID_API_VERSION
892 )
893 );
894 return;
895 }
896  
897 if ( $STAT{analysis}->{ongoing} >= $self->{max_ongoing} ) {
898 $STAT{api}->{errors}++;
899 $self->Error(
900 $cb,
901 Lim::Error->new(
902 module => $self,
903 code => HTTP::Status::HTTP_SERVICE_UNAVAILABLE,
904 message => ERR_QUEUE_FULL
905 )
906 );
907 return;
908 }
909 unless ( $q->{fqdn} =~ /^(?:[a-zA-Z0-9-]+\.)*(?:[a-zA-Z0-9-]+\.?|\.)$/o ) {
910 $STAT{api}->{errors}++;
911 $self->Error(
912 $cb,
913 Lim::Error->new(
914 module => $self,
915 code => HTTP::Status::HTTP_BAD_REQUEST,
916 message => ERR_INVALID_FQDN
917 )
918 );
919 return;
920 }
921  
922 my $policy;
923 if ( $q->{policy} ) {
924 unless ( exists $self->{collector_policy}->{ $q->{policy} } ) {
925 $STAT{api}->{errors}++;
926 $self->Error(
927 $cb,
928 Lim::Error->new(
929 module => $self,
930 code => HTTP::Status::HTTP_BAD_REQUEST,
931 message => ERR_POLICY_NOT_FOUND
932 )
933 );
934 return;
935 }
936  
937 $policy = {
938 name => $self->{collector_policy}->{ $q->{policy} }->{name},
939 display => $self->{collector_policy}->{ $q->{policy} }->{display},
940 $self->{collector_policy}->{ $q->{policy} }->{description} ? ( description => $self->{collector_policy}->{ $q->{policy} }->{description} ) : ()
941 };
942 }
943  
944 my ( $ipv4, $ipv6 ) = ( 0, 0 );
945  
946 if ( exists $q->{ipv4} ) {
947 if ( $q->{ipv4} ) {
948 unless ( $self->{allow_ipv4} ) {
949 $STAT{api}->{errors}++;
950 $self->Error(
951 $cb,
952 Lim::Error->new(
953 module => $self,
954 code => HTTP::Status::HTTP_BAD_REQUEST,
955 message => ERR_IPV4_NOT_ALLOWED
956 )
957 );
958 return;
959 }
960  
961 $ipv4 = 1;
962 }
963 }
964 else {
965 $ipv4 = $self->{test_ipv4};
966 }
967  
968 if ( exists $q->{ipv6} ) {
969 if ( $q->{ipv6} ) {
970 unless ( $self->{allow_ipv6} ) {
971 $STAT{api}->{errors}++;
972 $self->Error(
973 $cb,
974 Lim::Error->new(
975 module => $self,
976 code => HTTP::Status::HTTP_BAD_REQUEST,
977 message => ERR_IPV6_NOT_ALLOWED
978 )
979 );
980 return;
981 }
982  
983 $ipv6 = 1;
984 }
985 }
986 else {
987 $ipv6 = $self->{test_ipv6};
988 }
989  
990 unless ( $ipv4 || $ipv6 ) {
991 $STAT{api}->{errors}++;
992 $self->Error(
993 $cb,
994 Lim::Error->new(
995 module => $self,
996 code => HTTP::Status::HTTP_BAD_REQUEST,
997 message => ERR_NO_IP_PROTOCOL
998 )
999 );
1000 return;
1001 }
1002  
1003 my @ns;
1004 if ( $q->{ns} ) {
1005 unless ( ref( $q->{ns} ) eq 'ARRAY' ) {
1006 $q->{ns} = [ $q->{ns} ];
1007 }
1008  
1009 foreach ( @{ $q->{ns} } ) {
1010 if ( !$_->{fqdn} or ( exists $_->{ip} && !$_->{ip} ) ) {
1011 $STAT{api}->{errors}++;
1012 $self->Error(
1013 $cb,
1014 Lim::Error->new(
1015 module => $self,
1016 code => HTTP::Status::HTTP_BAD_REQUEST,
1017 message => ERR_INVALID_NS
1018 )
1019 );
1020 return;
1021 }
1022  
1023 push( @ns, join( '', '--ns=', $_->{fqdn}, $_->{ip} ? ( '/', $_->{ip} ) : () ) );
1024 }
1025 }
1026  
1027 my @ds;
1028 if ( $q->{ds} ) {
1029 unless ( ref( $q->{ds} ) eq 'ARRAY' ) {
1030 $q->{ds} = [ $q->{ds} ];
1031 }
1032  
1033 foreach ( @{ $q->{ds} } ) {
1034 unless ( $_->{keytag} and $_->{algorithm} and $_->{type} and $_->{digest} ) {
1035 $STAT{api}->{errors}++;
1036 $self->Error(
1037 $cb,
1038 Lim::Error->new(
1039 module => $self,
1040 code => HTTP::Status::HTTP_BAD_REQUEST,
1041 message => ERR_INVALID_DS
1042 )
1043 );
1044 return;
1045 }
1046  
1047 push( @ds, join( '--ds=', join( ',', $_->{keytag}, $_->{algorithm}, $_->{type}, $_->{digest} ) ) );
1048 }
1049 }
1050  
1051 if ( !$self->{allow_undelegated} and ( scalar @ns or scalar @ds ) ) {
1052 $STAT{api}->{errors}++;
1053 $self->Error(
1054 $cb,
1055 Lim::Error->new(
1056 module => $self,
1057 code => HTTP::Status::HTTP_BAD_REQUEST,
1058 message => ERR_UNDELEGATED_NOT_ALLOWED
1059 )
1060 );
1061 return;
1062 }
1063  
1064 if ( $self->{force_undelegated} and !scalar @ns ) {
1065 $STAT{api}->{errors}++;
1066 $self->Error(
1067 $cb,
1068 Lim::Error->new(
1069 module => $self,
1070 code => HTTP::Status::HTTP_BAD_REQUEST,
1071 message => ERR_UNDELEGATED_FORCED
1072 )
1073 );
1074 return;
1075 }
1076  
1077 if ( scalar @ns > $self->{max_undelegated_ns} ) {
1078 $STAT{api}->{errors}++;
1079 $self->Error(
1080 $cb,
1081 Lim::Error->new(
1082 module => $self,
1083 code => HTTP::Status::HTTP_BAD_REQUEST,
1084 message => ERR_INVALID_NS
1085 )
1086 );
1087 return;
1088 }
1089  
1090 if ( scalar @ds > $self->{max_undelegated_ds} ) {
1091 $STAT{api}->{errors}++;
1092 $self->Error(
1093 $cb,
1094 Lim::Error->new(
1095 module => $self,
1096 code => HTTP::Status::HTTP_BAD_REQUEST,
1097 message => ERR_INVALID_DS
1098 )
1099 );
1100 return;
1101 }
1102  
1103 if ( $q->{meta_data} ) {
1104 unless ( $self->{allow_meta_data} ) {
1105 $STAT{api}->{errors}++;
1106 $self->Error(
1107 $cb,
1108 Lim::Error->new(
1109 module => $self,
1110 code => HTTP::Status::HTTP_BAD_REQUEST,
1111 message => ERR_META_DATA_NOT_ALLOWED
1112 )
1113 );
1114 return;
1115 }
1116  
1117 unless ( ref( $q->{meta_data} ) eq 'ARRAY' ) {
1118 $q->{meta_data} = [ $q->{meta_data} ];
1119 }
1120  
1121 my $count = 0;
1122 foreach ( @{ $q->{meta_data} } ) {
1123 unless ($count < $self->{max_meta_data_entries}
1124 and $_->{key}
1125 and $_->{value}
1126 and ( length( $_->{key} ) + length( $_->{value} ) ) <= $self->{max_meta_data_entry_size} )
1127 {
1128 $STAT{api}->{errors}++;
1129 $self->Error(
1130 $cb,
1131 Lim::Error->new(
1132 module => $self,
1133 code => HTTP::Status::HTTP_BAD_REQUEST,
1134 message => ERR_INVALID_META_DATA
1135 )
1136 );
1137 return;
1138 }
1139  
1140 $count++;
1141 }
1142 }
1143  
1144 my $collector_analyze = $self->{collector}->{analyze};
1145 if ( $q->{policy} ) {
1146 $collector_analyze = $self->{collector_policy}->{ $q->{policy} }->{analyze};
1147 }
1148 unless ( ref( $collector_analyze ) eq 'CODE' ) {
1149 Lim::ERR and $self->{logger}->error( 'Unable to call collector' );
1150 $STAT{api}->{errors}++;
1151 $self->Error(
1152 $cb,
1153 Lim::Error->new(
1154 module => $self,
1155 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
1156 )
1157 );
1158 return;
1159 }
1160  
1161 my $fqdn = $q->{fqdn};
1162 $fqdn =~ s/\.$//o;
1163 $fqdn .= '.';
1164  
1165 my $id = MIME::Base64::encode_base64url( Data::UUID->new->create );
1166  
1167 if ( exists $TEST{$id} ) {
1168 $STAT{api}->{errors}++;
1169 $self->Error(
1170 $cb,
1171 Lim::Error->new(
1172 module => $self,
1173 code => HTTP::Status::HTTP_CONFLICT,
1174 message => ERR_INTERNAL_DATABASE
1175 )
1176 );
1177 return;
1178 }
1179  
1180 my $test = $TEST{$id} = {
1181 id => $id,
1182 fqdn => $fqdn,
1183 $policy ? ( policy => $policy ) : (),
1184 status => STATUS_RESERVED,
1185 progress => 0,
1186 created => time,
1187 updated => time,
1188 summary => {
1189 notice => 0,
1190 warning => 0,
1191 error => 0,
1192 critical => 0
1193 },
1194 ipv4 => $ipv4,
1195 ipv6 => $ipv6,
1196 $q->{ns} ? ( ns => $q->{ns} ) : (),
1197 $q->{ds} ? ( ds => $q->{ds} ) : (),
1198 $q->{meta_data} ? ( meta_data => $q->{meta_data} ) : ()
1199 };
1200 if ( $q->{space} ) {
1201 $TEST_SPACE{$id} = $q->{space};
1202 }
1203  
1204 $STAT{analysis}->{ongoing}++;
1205  
1206 $self->{logger}->debug( 'Reserving ', $fqdn, ' ', $id );
1207  
1208 $self->{db}->CreateAnalyze(
1209 $q->{space} ? ( space => $q->{space} ) : (),
1210 analyze => $test,
1211 cb => sub {
1212 my ( $object ) = @_;
1213  
1214 # uncoverable branch true
1215 unless ( defined $self ) {
1216  
1217 # uncoverable statement
1218 return;
1219 }
1220  
1221 if ( $@ ) {
1222 $self->{logger}->error( 'Unable to store ', $id, ' in database: ', $@ );
1223  
1224 $STAT{analysis}->{ongoing}--;
1225 $STAT{analysis}->{failed}++;
1226  
1227 delete $TEST{$id};
1228 delete $TEST_SPACE{$id};
1229  
1230 $STAT{api}->{errors}++;
1231 $self->Error(
1232 $cb,
1233 Lim::Error->new(
1234 module => $self,
1235 code => HTTP::Status::HTTP_CONFLICT,
1236 message => ERR_INTERNAL_DATABASE
1237 )
1238 );
1239 return;
1240 }
1241  
1242 $TEST_DB{$id} = $object;
1243  
1244 $test->{status} = STATUS_QUEUED;
1245  
1246 $self->{logger}->debug( 'Analyzing ', $fqdn, ' ', $id, ( $q->{policy} ? ' ' . $q->{policy} : '' ) );
1247  
1248 my $modules = 0;
1249 my $modules_done = 0;
1250 my $started = 0;
1251 my $result_id = 0;
1252 my $failed = sub {
1253 $STAT{analysis}->{ongoing}--;
1254 $STAT{analysis}->{failed}++;
1255 $test->{status} = STATUS_FAILED;
1256 };
1257 my $store = sub {
1258 $self->StoreAnalyze( $id );
1259 };
1260  
1261 $collector_analyze->(
1262 sub {
1263 my ( $msg ) = @_;
1264  
1265 unless ( $msg ) {
1266 $test->{progress} = 100;
1267 delete $test->{results};
1268 if ( defined $failed ) {
1269 $failed->();
1270 undef $failed;
1271 }
1272 if ( defined $store ) {
1273 $store->();
1274 undef $store;
1275 }
1276 return 1;
1277 }
1278  
1279 $test->{updated} = time;
1280 $test->{status} = STATUS_ANALYZING;
1281  
1282 if ( $msg->{level} eq 'DEBUG' || ( $msg->{level} eq 'INFO' && $msg->{tag} eq 'POLICY_DISABLED' ) ) {
1283 if ( !$started && $msg->{tag} eq 'MODULE_VERSION' ) {
1284 $modules++;
1285 }
1286 elsif ( $_->{tag} eq 'MODULE_END' and $_->{args}->{module} eq 'Lim::Plugin::Zonalizer::Collector' ) {
1287 $test->{progress} = 100;
1288 if ( defined $failed ) {
1289 $STAT{analysis}->{ongoing}--;
1290 $STAT{analysis}->{completed}++;
1291 $test->{status} = exists $test->{results} ? STATUS_DONE : STATUS_UNKNOWN;
1292 undef $failed;
1293 }
1294 if ( defined $store ) {
1295 $store->();
1296 undef $store;
1297 }
1298 return 1;
1299 }
1300 elsif ( $msg->{tag} eq 'MODULE_END' || $msg->{tag} eq 'POLICY_DISABLED' ) {
1301 $started = 1;
1302 $modules_done++;
1303  
1304 $test->{progress} = ( $modules_done * 100 ) / $modules;
1305  
1306 $self->{logger}->debug( 'done ', $modules_done, ' progress ', $test->{progress} );
1307 }
1308 else {
1309 $started = 1;
1310 }
1311 }
1312  
1313 if ( $msg->{level} eq 'DEBUG' ) {
1314 next;
1315 }
1316  
1317 if ( $msg->{level} eq 'NOTICE' ) {
1318 $test->{summary}->{notice}++;
1319 }
1320 elsif ( $msg->{level} eq 'WARNING' ) {
1321 $test->{summary}->{warning}++;
1322 }
1323 elsif ( $msg->{level} eq 'ERROR' ) {
1324 $test->{summary}->{error}++;
1325 }
1326 elsif ( $msg->{level} eq 'CRITICAL' ) {
1327 $test->{summary}->{critical}++;
1328 }
1329  
1330 $msg->{_id} = $result_id++;
1331 push( @{ $test->{results} }, $msg );
1332  
1333 return;
1334 },
1335 id => $id,
1336 fqdn => $fqdn,
1337 ipv4 => $ipv4,
1338 ipv6 => $ipv6,
1339 $q->{ns} ? ( ns => $q->{ns} ) : (),
1340 $q->{ds} ? ( ds => $q->{ds} ) : ()
1341 );
1342  
1343 $self->Successful( $cb, { id => $id } );
1344 return;
1345 }
1346 );
1347 return;
1348 }
1349  
1350 =item ReadAnalyze
1351  
1352 =cut
1353  
1354 sub ReadAnalyze {
1355 my ( $self, $cb, $q ) = @_;
1356 my $real_self = $self;
1357 weaken( $self );
1358 $STAT{api}->{requests}++;
1359  
1360 unless ( $q->{version} == 1 ) {
1361 $STAT{api}->{errors}++;
1362 $self->Error(
1363 $cb,
1364 Lim::Error->new(
1365 module => $self,
1366 code => HTTP::Status::HTTP_BAD_REQUEST,
1367 message => ERR_INVALID_API_VERSION
1368 )
1369 );
1370 return;
1371 }
1372  
1373 #
1374 # Set base url if configured/requested.
1375 #
1376  
1377 my $base_url = '';
1378 if ( exists $self->{custom_base_url} ) {
1379 $base_url = $self->{custom_base_url};
1380 }
1381 elsif ( ( defined $q->{base_url} ? $q->{base_url} : $self->{base_url} ) and $cb->request ) {
1382 $base_url = $cb->request->header( 'X-Lim-Base-URL' );
1383 }
1384  
1385 #
1386 # Get translator
1387 #
1388  
1389 my ( $translator, $lang, $error ) = $self->GetTranslator( $q->{lang} );
1390  
1391 unless ( $translator and $lang ) {
1392 $STAT{api}->{errors}++;
1393 $self->Error(
1394 $cb,
1395 $error ? $error : Lim::Error->new(
1396 module => $self,
1397 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
1398 )
1399 );
1400 return;
1401 }
1402  
1403 #
1404 # Check for analyze in memory.
1405 #
1406  
1407 if ( exists $TEST{ $q->{id} } ) {
1408 if ( $q->{space} ) {
1409 unless ( exists $TEST_SPACE{ $q->{id} }
1410 and $TEST_SPACE{ $q->{id} } eq $q->{space} )
1411 {
1412 $STAT{api}->{errors}++;
1413 $self->Error(
1414 $cb,
1415 Lim::Error->new(
1416 module => $self,
1417 code => HTTP::Status::HTTP_NOT_FOUND,
1418 message => ERR_ID_NOT_FOUND
1419 )
1420 );
1421 return;
1422 }
1423 }
1424 else {
1425 if ( exists $TEST_SPACE{ $q->{id} } ) {
1426 $STAT{api}->{errors}++;
1427 $self->Error(
1428 $cb,
1429 Lim::Error->new(
1430 module => $self,
1431 code => HTTP::Status::HTTP_NOT_FOUND,
1432 message => ERR_ID_NOT_FOUND
1433 )
1434 );
1435 return;
1436 }
1437 }
1438  
1439 my %test = %{ $TEST{ $q->{id} } };
1440 $test{url} = $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis/' . uri_escape( $test{id} );
1441 if ( $q->{space} ) {
1442 $test{url} .= '?space' . uri_escape( $q->{space} );
1443 }
1444  
1445 if ( exists $q->{last_results} and $q->{last_results} ) {
1446 my $results = delete $test{results};
1447 $test{results} = scalar @$results < $q->{last_results} ? [@$results] : [ @{$results}[ -$q->{last_results} .. -1 ] ];
1448 }
1449 elsif ( exists $q->{results} and !$q->{results} ) {
1450 delete $test{results};
1451 }
1452  
1453 if ( exists $test{results} ) {
1454 my @results;
1455  
1456 setlocale( LC_MESSAGES, $lang . '.UTF-8' );
1457 foreach my $result ( @{ $test{results} } ) {
1458 my $entry = Zonemaster::Logger::Entry->new( $result );
1459  
1460 my $message = $translator->translate_tag( $entry );
1461 utf8::decode( $message );
1462  
1463 push(
1464 @results,
1465 {
1466 %$result,
1467 message => $message
1468 }
1469 );
1470 }
1471 setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' );
1472  
1473 $test{results} = \@results;
1474 }
1475  
1476 $self->Successful( $cb, \%test );
1477 return;
1478 }
1479  
1480 #
1481 # Query the database for the analyze.
1482 #
1483  
1484 $self->{db}->ReadAnalyze(
1485 $q->{space} ? ( space => $q->{space} ) : (),
1486 id => $q->{id},
1487 cb => sub {
1488 my ( $analyze ) = @_;
1489  
1490 # uncoverable branch true
1491 unless ( defined $self ) {
1492  
1493 # uncoverable statement
1494 return;
1495 }
1496  
1497 if ( $@ ) {
1498 $STAT{api}->{errors}++;
1499  
1500 if ( $@ eq ERR_ID_NOT_FOUND ) {
1501 $self->Error(
1502 $cb,
1503 Lim::Error->new(
1504 module => $self,
1505 code => HTTP::Status::HTTP_NOT_FOUND,
1506 message => $@
1507 )
1508 );
1509 return;
1510 }
1511  
1512 # uncoverable branch false
1513 Lim::ERR and $self->{logger}->error( $@ );
1514 $self->Error(
1515 $cb,
1516 Lim::Error->new(
1517 module => $self,
1518 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
1519 )
1520 );
1521 return;
1522 }
1523  
1524 #
1525 # Construct the result
1526 #
1527  
1528 if ( exists $q->{last_results} and $q->{last_results} ) {
1529 my %test = %{ $TEST{ $q->{id} } };
1530 my $results = delete $analyze->{results};
1531 $analyze->{results} = scalar @$results < $q->{last_results} ? [@$results] : [ @{$results}[ -$q->{last_results} .. -1 ] ];
1532 }
1533 elsif ( exists $q->{results} and !$q->{results} ) {
1534 delete $analyze->{results};
1535 }
1536  
1537 if ( exists $analyze->{results} ) {
1538 setlocale( LC_MESSAGES, $lang . '.UTF-8' );
1539 foreach my $result ( @{ $analyze->{results} } ) {
1540 my $entry = Zonemaster::Logger::Entry->new( $result );
1541 $result->{message} = $translator->translate_tag( $entry );
1542 utf8::decode( $result->{message} );
1543 }
1544 setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' );
1545 }
1546  
1547 my ( @ns, @ds, @meta_data );
1548  
1549 if ( $analyze->{ns} ) {
1550 foreach my $ns ( @{ $analyze->{ns} } ) {
1551 push(
1552 @ns,
1553 {
1554 fqdn => $ns->{fqdn},
1555 exists $ns->{ip} ? ( ip => $ns->{ip} ) : ()
1556 }
1557 );
1558 }
1559 }
1560  
1561 if ( $analyze->{ds} ) {
1562 foreach my $ds ( @{ $analyze->{ds} } ) {
1563 push(
1564 @ds,
1565 {
1566 keytag => $ds->{keytag},
1567 algorithm => $ds->{algorithm},
1568 type => $ds->{type},
1569 digest => $ds->{digest}
1570 }
1571 );
1572 }
1573 }
1574  
1575 if ( $analyze->{meta_data} ) {
1576 foreach my $meta_data ( @{ $analyze->{meta_data} } ) {
1577 push(
1578 @meta_data,
1579 {
1580 key => $meta_data->{key},
1581 value => $meta_data->{value}
1582 }
1583 );
1584 }
1585 }
1586  
1587 $self->Successful(
1588 $cb,
1589 {
1590 id => $analyze->{id},
1591 fqdn => $analyze->{fqdn},
1592 exists $analyze->{policy}
1593 ? (
1594 policy => {
1595 name => $analyze->{policy}->{name},
1596 display => $analyze->{policy}->{display},
1597 $analyze->{policy}->{description} ? ( description => $analyze->{policy}->{description} ) : ()
1598 }
1599 )
1600 : (),
1601 url => $base_url . '/zonalizer/' . uri_escape( $q->{version} ) . '/analysis/' . uri_escape( $analyze->{id} ) . ( $q->{space} ? '?space=' . uri_escape( $q->{space} ) : '' ),
1602 status => $analyze->{status},
1603 progress => $analyze->{progress},
1604 created => $analyze->{created},
1605 updated => $analyze->{updated},
1606 exists $analyze->{error} ? ( error => $analyze->{error} ) : (),
1607 exists $analyze->{results} ? ( results => $analyze->{results} ) : (),
1608 summary => {
1609 notice => $analyze->{summary}->{notice},
1610 warning => $analyze->{summary}->{warning},
1611 error => $analyze->{summary}->{error},
1612 critical => $analyze->{summary}->{critical}
1613 },
1614 ipv4 => $analyze->{ipv4},
1615 ipv6 => $analyze->{ipv6},
1616 scalar @ns ? ( ns => \@ns ) : (),
1617 scalar @ds ? ( ds => \@ds ) : (),
1618 scalar @meta_data ? ( meta_data => \@meta_data ) : ()
1619 }
1620 );
1621 }
1622 );
1623 return;
1624 }
1625  
1626 =item ReadAnalyzeStatus
1627  
1628 =cut
1629  
1630 sub ReadAnalyzeStatus {
1631 my ( $self, $cb, $q ) = @_;
1632 my $real_self = $self;
1633 weaken( $self );
1634 $STAT{api}->{requests}++;
1635  
1636 unless ( $q->{version} == 1 ) {
1637 $STAT{api}->{errors}++;
1638 $self->Error(
1639 $cb,
1640 Lim::Error->new(
1641 module => $self,
1642 code => HTTP::Status::HTTP_BAD_REQUEST,
1643 message => ERR_INVALID_API_VERSION
1644 )
1645 );
1646 return;
1647 }
1648  
1649 #
1650 # Check for analyze in memory.
1651 #
1652  
1653 if ( exists $TEST{ $q->{id} } ) {
1654 if ( $q->{space} ) {
1655 unless ( exists $TEST_SPACE{ $q->{id} }
1656 and $TEST_SPACE{ $q->{id} } eq $q->{space} )
1657 {
1658 $STAT{api}->{errors}++;
1659 $self->Error(
1660 $cb,
1661 Lim::Error->new(
1662 module => $self,
1663 code => HTTP::Status::HTTP_NOT_FOUND,
1664 message => ERR_ID_NOT_FOUND
1665 )
1666 );
1667 return;
1668 }
1669 }
1670 else {
1671 if ( exists $TEST_SPACE{ $q->{id} } ) {
1672 $STAT{api}->{errors}++;
1673 $self->Error(
1674 $cb,
1675 Lim::Error->new(
1676 module => $self,
1677 code => HTTP::Status::HTTP_NOT_FOUND,
1678 message => ERR_ID_NOT_FOUND
1679 )
1680 );
1681 return;
1682 }
1683 }
1684  
1685 $self->Successful(
1686 $cb,
1687 {
1688 status => $TEST{ $q->{id} }->{status},
1689 progress => $TEST{ $q->{id} }->{progress},
1690 updated => $TEST{ $q->{id} }->{updated}
1691 }
1692 );
1693 return;
1694 }
1695  
1696 #
1697 # Query the database for the analyze.
1698 #
1699  
1700 $self->{db}->ReadAnalyze(
1701 $q->{space} ? ( space => $q->{space} ) : (),
1702 id => $q->{id},
1703 cb => sub {
1704 my ( $analyze ) = @_;
1705  
1706 # uncoverable branch true
1707 unless ( defined $self ) {
1708  
1709 # uncoverable statement
1710 return;
1711 }
1712  
1713 if ( $@ ) {
1714 $STAT{api}->{errors}++;
1715  
1716 if ( $@ eq ERR_ID_NOT_FOUND ) {
1717 $self->Error(
1718 $cb,
1719 Lim::Error->new(
1720 module => $self,
1721 code => HTTP::Status::HTTP_NOT_FOUND,
1722 message => $@
1723 )
1724 );
1725 return;
1726 }
1727  
1728 # uncoverable branch false
1729 Lim::ERR and $self->{logger}->error( $@ );
1730 $self->Error(
1731 $cb,
1732 Lim::Error->new(
1733 module => $self,
1734 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
1735 )
1736 );
1737 return;
1738 }
1739  
1740 #
1741 # Construct the result
1742 #
1743  
1744 $self->Successful(
1745 $cb,
1746 {
1747 status => $analyze->{status},
1748 progress => $analyze->{progress},
1749 updated => $analyze->{updated}
1750 }
1751 );
1752 }
1753 );
1754 return;
1755 }
1756  
1757 =item DeleteAnalyze
1758  
1759 =cut
1760  
1761 sub DeleteAnalyze {
1762 my ( $self, $cb, $q ) = @_;
1763 my $real_self = $self;
1764 weaken( $self );
1765 $STAT{api}->{requests}++;
1766  
1767 unless ( $q->{version} == 1 ) {
1768 $STAT{api}->{errors}++;
1769 $self->Error(
1770 $cb,
1771 Lim::Error->new(
1772 module => $self,
1773 code => HTTP::Status::HTTP_BAD_REQUEST,
1774 message => ERR_INVALID_API_VERSION
1775 )
1776 );
1777 return;
1778 }
1779  
1780 if ( exists $TEST{ $q->{id} } ) {
1781 if ( $q->{space} ) {
1782 unless ( exists $TEST_SPACE{ $q->{id} }
1783 and $TEST_SPACE{ $q->{id} } eq $q->{space} )
1784 {
1785 $STAT{api}->{errors}++;
1786 $self->Error(
1787 $cb,
1788 Lim::Error->new(
1789 module => $self,
1790 code => HTTP::Status::HTTP_NOT_FOUND,
1791 message => ERR_ID_NOT_FOUND
1792 )
1793 );
1794 return;
1795 }
1796 }
1797 else {
1798 if ( exists $TEST_SPACE{ $q->{id} } ) {
1799 $STAT{api}->{errors}++;
1800 $self->Error(
1801 $cb,
1802 Lim::Error->new(
1803 module => $self,
1804 code => HTTP::Status::HTTP_NOT_FOUND,
1805 message => ERR_ID_NOT_FOUND
1806 )
1807 );
1808 return;
1809 }
1810 }
1811  
1812 delete $TEST{ $q->{id} };
1813 delete $TEST_DB{ $q->{id} };
1814 delete $TEST_SPACE{ $q->{id} };
1815  
1816 $self->Successful( $cb );
1817 return;
1818 }
1819  
1820 $self->{db}->DeleteAnalyze(
1821 $q->{space} ? ( space => $q->{space} ) : (),
1822 id => $q->{id},
1823 cb => sub {
1824  
1825 # uncoverable branch true
1826 unless ( defined $self ) {
1827  
1828 # uncoverable statement
1829 return;
1830 }
1831  
1832 if ( $@ ) {
1833 $STAT{api}->{errors}++;
1834  
1835 if ( $@ eq ERR_ID_NOT_FOUND ) {
1836 $self->Error(
1837 $cb,
1838 Lim::Error->new(
1839 module => $self,
1840 code => HTTP::Status::HTTP_NOT_FOUND,
1841 message => $@
1842 )
1843 );
1844 return;
1845 }
1846  
1847 # uncoverable branch false
1848 Lim::ERR and $self->{logger}->error( $@ );
1849 $self->Error(
1850 $cb,
1851 Lim::Error->new(
1852 module => $self,
1853 code => HTTP::Status::HTTP_INTERNAL_SERVER_ERROR
1854 )
1855 );
1856 return;
1857 }
1858  
1859 $self->Successful( $cb );
1860 }
1861 );
1862 return;
1863 }
1864  
1865 =item ReadPolicies
1866  
1867 =cut
1868  
1869 sub ReadPolicies {
1870 my ( $self, $cb, $q ) = @_;
1871 my $real_self = $self;
1872 weaken( $self );
1873 $STAT{api}->{requests}++;
1874  
1875 unless ( $q->{version} == 1 ) {
1876 $STAT{api}->{errors}++;
1877 $self->Error(
1878 $cb,
1879 Lim::Error->new(
1880 module => $self,
1881 code => HTTP::Status::HTTP_BAD_REQUEST,
1882 message => ERR_INVALID_API_VERSION
1883 )
1884 );
1885 return;
1886 }
1887  
1888 my @policies;
1889  
1890 foreach ( values %{ $self->{collector_policy} } ) {
1891 push(
1892 @policies,
1893 {
1894 name => $_->{name},
1895 display => $_->{display},
1896 $_->{description} ? ( description => $_->{description} ) : ()
1897 }
1898 );
1899 }
1900  
1901 $self->Successful( $cb, { policies => \@policies } );
1902 return;
1903 }
1904  
1905 =item ReadPolicy
1906  
1907 =cut
1908  
1909 sub ReadPolicy {
1910 my ( $self, $cb, $q ) = @_;
1911 my $real_self = $self;
1912 weaken( $self );
1913 $STAT{api}->{requests}++;
1914  
1915 unless ( $q->{version} == 1 ) {
1916 $STAT{api}->{errors}++;
1917 $self->Error(
1918 $cb,
1919 Lim::Error->new(
1920 module => $self,
1921 code => HTTP::Status::HTTP_BAD_REQUEST,
1922 message => ERR_INVALID_API_VERSION
1923 )
1924 );
1925 return;
1926 }
1927  
1928 if ( $q->{name} and !exists $self->{collector_policy}->{ $q->{name} } ) {
1929 $STAT{api}->{errors}++;
1930 $self->Error(
1931 $cb,
1932 Lim::Error->new(
1933 module => $self,
1934 code => HTTP::Status::HTTP_BAD_REQUEST,
1935 message => ERR_POLICY_NOT_FOUND
1936 )
1937 );
1938 return;
1939 }
1940  
1941 $self->Successful(
1942 $cb,
1943 {
1944 name => $self->{collector_policy}->{ $q->{name} }->{name},
1945 display => $self->{collector_policy}->{ $q->{name} }->{display},
1946 $self->{collector_policy}->{ $q->{name} }->{description} ? ( description => $self->{collector_policy}->{ $q->{name} }->{description} ) : ()
1947 }
1948 );
1949 return;
1950 }
1951  
1952 =back
1953  
1954 =head1 PRIVATE METHODS
1955  
1956 =over 4
1957  
1958 =item StoreAnalyze
1959  
1960 =cut
1961  
1962 sub StoreAnalyze {
1963 my ( $self, $id ) = @_;
1964 my $real_self = $self;
1965 weaken( $self );
1966  
1967 unless ( defined $id ) {
1968 $self->{logger}->error( 'called without $id' );
1969 }
1970 unless ( exists $TEST{$id} ) {
1971 $self->{logger}->error( 'called but $TEST{ $id } does not exist' );
1972 return;
1973 }
1974 unless ( exists $TEST_DB{$id} ) {
1975 $self->{logger}->error( 'called but $TEST_DB{ $id } does not exist' );
1976 return;
1977 }
1978  
1979 $self->{logger}->debug( 'Storing ', $id );
1980  
1981 $self->{db}->UpdateAnalyze(
1982 $TEST_SPACE{$id} ? ( space => $TEST_SPACE{$id} ) : (),
1983 analyze => {
1984 %{ $TEST_DB{$id} },
1985 %{ $TEST{$id} }
1986 },
1987 cb => sub {
1988  
1989 # uncoverable branch true
1990 unless ( defined $self ) {
1991  
1992 # uncoverable statement
1993 return;
1994 }
1995  
1996 if ( $@ ) {
1997 $self->{logger}->error( 'Unable to store ', $id, ' in database, analyze will be lost: ', $@ );
1998 }
1999  
2000 delete $TEST{$id};
2001 delete $TEST_DB{$id};
2002 delete $TEST_SPACE{$id};
2003 }
2004 );
2005 return;
2006 }
2007  
2008 =item GetTranslator
2009  
2010 =cut
2011  
2012 sub GetTranslator {
2013 my ( $self, $lang ) = @_;
2014  
2015 unless ( $lang ) {
2016 $lang = $self->{lang};
2017 }
2018  
2019 unless ( $lang =~ /^[a-z]{2}_[A-Z]{2}$/o and setlocale( LC_MESSAGES, $lang . '.UTF-8' ) ) {
2020  
2021 # uncoverable branch false
2022 Lim::ERR and $self->{logger}->error( 'Invalid language ', $lang );
2023  
2024 return (
2025 undef,
2026 undef,
2027 Lim::Error->new(
2028 module => $self,
2029 code => HTTP::Status::HTTP_UNSUPPORTED_MEDIA_TYPE,
2030 message => ERR_INVALID_LANG
2031 )
2032 );
2033 }
2034  
2035 setlocale( LC_MESSAGES, $self->{lang} . '.UTF-8' );
2036  
2037 return ( $TRANSLATOR, $lang );
2038 }
2039  
2040 =item StartCollector
2041  
2042 =cut
2043  
2044 sub StartCollector {
2045 my ( $self, %args ) = @_;
2046 my $real_self = $self;
2047 weaken( $self );
2048  
2049 unless ( defined $args{exec} ) {
2050 confess 'exec not defined';
2051 }
2052 unless ( ref( $args{on_eof} ) eq 'CODE' ) {
2053 confess 'on_eof is not CODE';
2054 }
2055  
2056 my $json = JSON::XS->new->utf8;
2057 my ( $read, $write ) = AnyEvent::Util::portable_pipe;
2058 my $hdl = AnyEvent::Handle->new( fh => $write );
2059 my %id;
2060  
2061 Lim::DEBUG and $self->{logger}->debug(
2062 'open ',
2063 join(
2064 ' ',
2065 $args{exec},
2066 $args{config} ? ( '--config', $args{config} ) : (),
2067 $args{policy} ? ( '--policy', $args{policy} ) : (),
2068 $args{sourceaddr} ? ( '--sourceaddr', $args{sourceaddr} ) : (),
2069 $args{threads} ? ( '--threads', $args{threads} ) : ()
2070 )
2071 );
2072  
2073 my $cv;
2074 $cv = AnyEvent::Util::run_cmd(
2075 [
2076 $args{exec},
2077 $args{config} ? ( '--config', $args{config} ) : (),
2078 $args{policy} ? ( '--policy', $args{policy} ) : (),
2079 $args{sourceaddr} ? ( '--sourceaddr', $args{sourceaddr} ) : (),
2080 $args{threads} ? ( '--threads', $args{threads} ) : (),
2081 ],
2082 '>' => sub {
2083 unless ( defined $self ) {
2084 return;
2085 }
2086  
2087 my @entries;
2088 eval {
2089 @entries = $json->incr_parse( @_ );
2090 foreach ( @entries ) {
2091 unless ( ref( $_ ) eq 'HASH' and $_->{_id} ) {
2092 die;
2093 }
2094  
2095 unless ( $id{ $_->{_id} } ) {
2096 Lim::WARN and $self->{logger}->warn( 'collector received data for unknown id' );
2097 next;
2098 }
2099  
2100 if ( $id{ $_->{_id} }->( $_ ) ) {
2101 delete $id{ $_->{_id} };
2102 }
2103 }
2104 };
2105 if ( $@ ) {
2106 $hdl->destroy;
2107 $cv->croak( $@ );
2108 }
2109 },
2110 '2>' => sub {
2111 unless ( defined $self and scalar @_ and defined $_[0] ) {
2112 return;
2113 }
2114  
2115 Lim::DEBUG and $self->{logger}->debug( 'collector: ', @_ );
2116 },
2117 '<' => $read,
2118 );
2119  
2120 $cv->cb(
2121 sub {
2122 unless ( defined $self ) {
2123 return;
2124 }
2125  
2126 eval { shift->recv; };
2127 if ( $@ ) {
2128 Lim::ERR and $self->{logger}->error( 'collector: ', $@ );
2129 }
2130  
2131 foreach ( values %id ) {
2132 $_->();
2133 }
2134  
2135 close( $read );
2136 $hdl->destroy;
2137 $args{on_eof}->();
2138 }
2139 );
2140  
2141 return sub {
2142 my ( $cb, %args ) = @_;
2143  
2144 unless ( defined $self ) {
2145 return;
2146 }
2147  
2148 unless ( ref( $cb ) eq 'CODE' and $args{id} and !exists $id{ $args{id} } ) {
2149 confess 'collector->analyze called incorrectly';
2150 }
2151  
2152 eval { $hdl->push_write( $json->encode( \%args ) . "\n" ); };
2153 if ( $@ or $hdl->destroyed ) {
2154 $cb->();
2155 return;
2156 }
2157  
2158 $id{ $args{id} } = $cb;
2159 };
2160 }
2161  
2162 =back
2163  
2164 =head1 AUTHOR
2165  
2166 Jerry Lundström, C<< <lundstrom.jerry@gmail.com> >>
2167  
2168 =head1 BUGS
2169  
2170 Please report any bugs or feature requests to L<https://github.com/jelu/lim-plugin-zonalizer/issues>.
2171  
2172 =head1 SUPPORT
2173  
2174 You can find documentation for this module with the perldoc command.
2175  
2176 perldoc Lim::Plugin::Zonalizer::Server
2177  
2178 You can also look for information at:
2179  
2180 =over 4
2181  
2182 =item * Lim issue tracker (report bugs here)
2183  
2184 L<https://github.com/jelu/lim-plugin-zonalizer/issues>
2185  
2186 =back
2187  
2188 =head1 ACKNOWLEDGEMENTS
2189  
2190 =head1 LICENSE AND COPYRIGHT
2191  
2192 Copyright 2015-2016 Jerry Lundström
2193 Copyright 2015-2016 IIS (The Internet Foundation in Sweden)
2194  
2195 This program is free software; you can redistribute it and/or modify it
2196 under the terms of either: the GNU General Public License as published
2197 by the Free Software Foundation; or the Artistic License.
2198  
2199 See http://dev.perl.org/licenses/ for more information.
2200  
2201  
2202 =cut
2203  
2204 1; # End of Lim::Plugin::Zonalizer::Server
« Zonalizer/Collector.pm ↑ Summary Zonalizer/CLI.pm »