Zonalizer/Collector.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Zonalizer.pm ↑ Summary Zonalizer/Server.pm »
1 package Lim::Plugin::Zonalizer::Collector;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 with 'MooseX::Getopt';
9  
10 0 ▶use Zonemaster;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Zonemaster::Util ();
12 use Zonemaster::Logger::Entry;
13  
14 use Net::LDNS;
15 use JSON::XS;
16  
17 my $can_use_threads = eval 'use threads; use Thread::Queue; 1';
18  
19 =encoding utf8
20  
21 =head1 NAME
22  
23 Lim::Plugin::Zonalizer::Collector - Collector that runs Zonemaster in multiple
24 threads.
25  
26 =head1 VERSION
27  
28 See L<Lim::Plugin::Zonalizer> for version.
29  
30 =cut
31  
32 our $VERSION = $Lim::Plugin::Zonalizer::VERSION;
33  
34 our %numeric = Zonemaster::Logger::Entry->levels;
35  
36 =head1 SYNOPSIS
37  
38 use Lim::Plugin::Zonalizer::Collector;
39  
40 Lim::Plugin::Zonalizer::Collector->new_with_options->run;
41  
42 =head1 METHODS
43  
44 =over 4
45  
46 =item config
47  
48 Name of configuration file to load.
49  
50 =cut
51  
52 has 'config' => (
53 is => 'ro',
54 isa => 'Str',
55 required => 0,
56 documentation => 'Name of configuration file to load.',
57 );
58  
59 =item policy
60  
61 Name of policy file to load.
62  
63 =cut
64  
65 has 'policy' => (
66 is => 'ro',
67 isa => 'Str',
68 required => 0,
69 documentation => 'Name of policy file to load.',
70 );
71  
72 =item sourceaddr
73  
74 Local IP address that the test engine should try to send its requests from.
75  
76 =cut
77  
78 has 'sourceaddr' => (
79 is => 'ro',
80 isa => 'Maybe[Str]',
81 required => 0,
82 default => undef,
83 documentation => 'Local IP address that the test engine should try to send its requests from.',
84 );
85  
86 =item threads
87  
88 Number of threads to start.
89  
90 =cut
91  
92 has 'threads' => (
93 is => 'ro',
94 isa => 'Num',
95 required => 0,
96 default => 5,
97 documentation => 'Number of threads to start.',
98 );
99  
100 =item debug
101  
102 Send debug information to STDERR.
103  
104 =cut
105  
106 has 'debug' => (
107 is => 'ro',
108 isa => 'Bool',
109 required => 0,
110 documentation => 'Send debug information to STDERR.'
111 );
112  
113 =item run
114  
115 Starts the collector, sets up the thread queues and creates all the threads
116 needed.
117  
118 =cut
119  
120 sub run {
121 my ( $self ) = @_;
122  
123 $self->debug and say STDERR 'start';
124  
125 if ( $self->sourceaddr ) {
126 Zonemaster->config->resolver_source( $self->sourceaddr );
127 }
128  
129 if ( $self->policy ) {
130 Zonemaster->config->load_policy_file( $self->policy );
131 }
132  
133 if ( $self->config ) {
134 Zonemaster->config->load_config_file( $self->config );
135 }
136  
137 my ( $in_q, $out_q );
138  
139 if ( $can_use_threads ) {
140 $in_q = Thread::Queue->new;
141 $out_q = Thread::Queue->new;
142  
143 my $threads = $self->threads;
144  
145 unless ( $threads > 0 ) {
146 confess;
147 }
148  
149 while ( $threads-- ) {
150 $self->debug and say STDERR 'start thread';
151  
152 threads->create(
153 sub {
154 my $json = JSON::XS->new->allow_blessed->convert_blessed->canonical;
155  
156 $self->debug and say STDERR 'thread started';
157 while ( my $in = $in_q->dequeue ) {
158 $self->debug and say STDERR 'dequeued in';
159 $self->process( $json->decode( $in ), $out_q );
160 }
161 }
162 );
163 }
164  
165 threads->create(
166 sub {
167 while ( my $out = $out_q->dequeue ) {
168 $self->debug and say STDERR 'dequeued out';
169 say $out;
170 }
171 }
172 )->detach;
173 }
174  
175 {
176 my $json = JSON::XS->new->allow_blessed->convert_blessed->canonical;
177  
178 $self->debug and say STDERR 'waiting';
179 while ( <STDIN> ) {
180 $self->debug and say STDERR 'read: ', $_;
181 my @in = $json->incr_parse( $_ );
182 foreach my $in ( @in ) {
183 unless ( $self->validate( $in ) ) {
184 $self->debug and say STDERR 'invalid in';
185 next;
186 }
187  
188 $self->debug and say STDERR 'queued in';
189  
190 if ( $can_use_threads ) {
191 $in_q->enqueue( $json->encode( $in ) );
192 }
193 else {
194 $self->process( $in );
195 }
196 }
197 }
198 }
199  
200 $self->debug and say STDERR 'end';
201  
202 if ( $can_use_threads ) {
203 $in_q->end;
204  
205 foreach ( threads->list ) {
206 $_->join;
207 }
208 }
209  
210 return;
211 }
212  
213 =item process
214  
215 Process an analyze request.
216  
217 =cut
218  
219 sub process {
220 my ( $self, $in, $out_q ) = @_;
221  
222 unless ( ref( $in ) eq 'HASH' ) {
223 confess;
224 }
225 if ( $can_use_threads ) {
226 unless ( blessed $out_q and $out_q->isa( 'Thread::Queue' ) ) {
227 confess;
228 }
229 }
230  
231 my $json = JSON::XS->new->allow_blessed->convert_blessed->canonical;
232  
233 Zonemaster->reset;
234  
235 Zonemaster->config->ipv4_ok( $in->{ipv4} ? 1 : 0 );
236 Zonemaster->config->ipv6_ok( $in->{ipv6} ? 1 : 0 );
237  
238 Zonemaster->logger->callback(
239 sub {
240 my ( $entry ) = @_;
241  
242 if ( $numeric{ uc $entry->level } >= $numeric{DEBUG} ) {
243 $self->debug and say STDERR 'queued out';
244 my $out = $json->encode(
245 {
246 _id => $in->{id},
247 timestamp => $entry->timestamp,
248 module => $entry->module,
249 tag => $entry->tag,
250 level => $entry->level,
251 $entry->args ? ( args => $entry->args ) : ()
252 }
253 );
254  
255 if ( $can_use_threads ) {
256 $out_q->enqueue( $out );
257 }
258 else {
259 say $out;
260 }
261 }
262 }
263 );
264  
265 Zonemaster::Util::info(
266 MODULE_VERSION => {
267 module => 'Zonemaster::Test::Basic',
268 version => Zonemaster::Test::Basic->version
269 }
270 );
271 foreach my $mod ( Zonemaster::Test->modules ) {
272 $mod = 'Zonemaster::Test::' . $mod;
273 Zonemaster::Util::info(
274 MODULE_VERSION => {
275 module => $mod,
276 version => $mod->version
277 }
278 );
279 }
280  
281 my $domain = $self->to_idn( $in->{fqdn} );
282  
283 if ( exists $in->{ns} ) {
284 my %ns;
285 foreach ( @{ $in->{ns} } ) {
286 my $idn = $self->to_idn( $_->{fqdn} );
287 my @ips;
288  
289 if ( $_->{ip} ) {
290 push @ips, $_->{ip};
291 }
292 else {
293 push @ips, Net::LDNS->new->name2addr( $idn );
294 }
295  
296 push @{ $ns{$idn} }, @ips;
297 }
298 if ( scalar %ns ) {
299 Zonemaster->add_fake_delegation( $domain => \%ns );
300 }
301 }
302  
303 if ( exists $in->{ds} ) {
304 Zonemaster->add_fake_ds( $domain => $in->{ds} );
305 }
306  
307 if ( exists $in->{test} ) {
308 foreach ( @{ $in->{test} } ) {
309 if ( $_->{method} ) {
310 Zonemaster->test_method( $_->{module}, $_->{method}, Zonemaster->zone( $domain ) );
311 }
312 else {
313 Zonemaster->test_module( $_->{module}, $domain );
314 }
315 }
316 }
317 else {
318 Zonemaster->test_zone( $domain );
319 }
320  
321 Zonemaster::Util::info( MODULE_END => { module => 'Lim::Plugin::Zonalizer::Collector' } );
322  
323 return;
324 }
325  
326 =back
327  
328 =head1 PRIVATE METHODS
329  
330 =over 4
331  
332 =item to_idn
333  
334 Converts input into an IDN string is its not ASCII and Net::LDNS has support for
335 IDN.
336  
337 =cut
338  
339 sub to_idn {
340 my ( $self, $str ) = @_;
341  
342 if ( $str =~ m/^[[:ascii:]]+$/ ) {
343 return $str;
344 }
345  
346 if ( Net::LDNS::has_idn() ) {
347 return Net::LDNS::to_idn( decode( $self->encoding, $str ) );
348 }
349 else {
350 return $str;
351 }
352 }
353  
354 =item validate
355  
356 Validates an input object.
357  
358 =cut
359  
360 sub validate {
361 my ( $self, $in ) = @_;
362  
363 unless ( ref( $in ) eq 'HASH' ) {
364 return;
365 }
366 foreach ( qw(id fqdn ipv4 ipv6) ) {
367 unless ( defined $in->{$_} ) {
368 return;
369 }
370 }
371 if ( exists $in->{ns} ) {
372 unless ( ref( $in->{ns} ) eq 'ARRAY' ) {
373 return;
374 }
375 foreach my $ns ( @{ $in->{ns} } ) {
376 unless ( ref( $ns ) eq 'HASH' ) {
377 return;
378 }
379 foreach ( qw(fqdn) ) {
380 unless ( defined $ns->{$_} ) {
381 return;
382 }
383 }
384 foreach ( qw(ip) ) {
385 if ( exists $ns->{$_} and !defined $ns->{$_} ) {
386 return;
387 }
388 }
389 }
390 }
391 if ( exists $in->{ds} ) {
392 unless ( ref( $in->{ds} ) eq 'ARRAY' ) {
393 return;
394 }
395 foreach my $ds ( @{ $in->{ds} } ) {
396 unless ( ref( $ds ) eq 'HASH' ) {
397 return;
398 }
399 foreach ( qw(keytag algorithm type digest) ) {
400 unless ( defined $ds->{$_} ) {
401 return;
402 }
403 }
404 }
405 }
406 if ( exists $in->{test} ) {
407 unless ( ref( $in->{test} ) eq 'ARRAY' ) {
408 return;
409 }
410 foreach my $test ( @{ $in->{test} } ) {
411 unless ( ref( $test ) eq 'HASH' ) {
412 return;
413 }
414 foreach ( qw(module) ) {
415 unless ( defined $test->{$_} ) {
416 return;
417 }
418 }
419 foreach ( qw(method) ) {
420 if ( exists $test->{$_} and !defined $test->{$_} ) {
421 return;
422 }
423 }
424 }
425 }
426 return 1;
427 }
428  
429 =back
430  
431 =head1 AUTHOR
432  
433 Jerry Lundström, C<< <lundstrom.jerry@gmail.com> >>
434  
435 =head1 BUGS
436  
437 Please report any bugs or feature requests to L<https://github.com/jelu/lim-plugin-zonalizer/issues>.
438  
439 =head1 SUPPORT
440  
441 You can find documentation for this module with the perldoc command.
442  
443 perldoc Lim::Plugin::Zonalizer::Collector
444  
445 You can also look for information at:
446  
447 =over 4
448  
449 =item * Lim issue tracker (report bugs here)
450  
451 L<https://github.com/jelu/lim-plugin-zonalizer/issues>
452  
453 =back
454  
455 =head1 ACKNOWLEDGEMENTS
456  
457 =head1 LICENSE AND COPYRIGHT
458  
459 Copyright 2015-2016 Jerry Lundström
460 Copyright 2015-2016 IIS (The Internet Foundation in Sweden)
461  
462 This program is free software; you can redistribute it and/or modify it
463 under the terms of either: the GNU General Public License as published
464 by the Free Software Foundation; or the Artistic License.
465  
466 See http://dev.perl.org/licenses/ for more information.
467  
468  
469 =cut
470  
471 1; # End of Lim::Plugin::Zonalizer::Collector
« Zonalizer.pm ↑ Summary Zonalizer/Server.pm »