Zonemaster/Config.pm untested

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 235 CC 18 SCAR 23.7
CC
18
Cov
0%
load_policy_file
42.0
BUILD
12.0
should_run
12.0
CRAP 10.7
1 package Zonemaster::Config v1.0.1;
2  
3 use 5.14.2;
4 use Moose;
5 use JSON;
6 use File::ShareDir qw[dist_dir dist_file];
7 use File::Slurp;
8 use Hash::Merge;
9 use File::Spec;
10  
11 use Zonemaster;
12  
13 has 'cfiles' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
14 has 'pfiles' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
15 has 'testcases' => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
16  
17 my $merger = Hash::Merge->new;
18 $merger->specify_behavior(
19 {
20 'SCALAR' => {
21 'SCALAR' => sub { $_[1] },
22 'ARRAY' => sub { [ $_[0], @{ $_[1] } ] },
23 'HASH' => sub { $_[1] },
24 },
25 'ARRAY' => {
26 'SCALAR' => sub { $_[1] },
27 'ARRAY' => sub { [ @{ $_[1] } ] },
28 'HASH' => sub { $_[1] },
29 },
30 'HASH' => {
31 'SCALAR' => sub { $_[1] },
32 'ARRAY' => sub { [ values %{ $_[0] }, @{ $_[1] } ] },
33 'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
34 },
35 }
36 );
37  
38 our $config;
39 _load_base_config();
40  
41 our $policy = {};
42  
43 sub BUILD {
44 my ( $self ) = @_;
45  
46 foreach my $dir ( _config_directory_list() ) {
47 my $cfile = File::Spec->catfile( $dir, 'config.json' );
48 my $new = eval { decode_json read_file $cfile };
49 if ( $new ) {
50 $config = $merger->merge( $config, $new );
51 push @{ $self->cfiles }, $cfile;
52 }
53  
54 my $pfile = File::Spec->catfile( $dir, 'policy.json' );
55 $new = eval { decode_json read_file $pfile };
56 if ( $new ) {
57 my $tc = $new->{__testcases__};
58 delete $new->{__testcases__};
59 foreach my $case (keys %$tc) {
60 $self->testcases->{$case} = $tc->{$case};
61 }
62 $policy = $merger->merge( $policy, $new );
63 push @{ $self->pfiles }, $pfile;
64 }
65  
66 }
67 } ## end sub BUILD
68  
69 sub get {
70 my ( $class ) = @_;
71  
72 return $config;
73 }
74  
75 sub policy {
76 my ( $class ) = @_;
77  
78 if ( not $policy ) {
79 _load_base_policy();
80 }
81  
82 return $policy;
83 }
84  
85 sub _config_directory_list {
86 my @dirlist;
87  
88 push @dirlist, dist_dir( 'Zonemaster' );
89 push @dirlist, '/etc/zonemaster';
90 push @dirlist, '/usr/local/etc/zonemaster';
91  
92 my $dir = ( getpwuid( $> ) )[7];
93 if ( $dir ) {
94 push @dirlist, $dir . '/.zonemaster';
95 }
96  
97 return @dirlist;
98 }
99  
100 sub _load_base_config {
101 my $internal = decode_json( join( '', <DATA> ) );
102 # my $filename = dist_file( 'Zonemaster', 'config.json' );
103 # my $default = eval { decode_json read_file $filename };
104 #
105 # $internal = $merger->merge( $internal, $default ) if $default;
106  
107 $config = $internal;
108 }
109  
110 sub load_module_policy {
111 my ( $class, $mod ) = @_;
112  
113 my $m = 'Zonemaster::Test::' . $mod;
114 if ( $m->can( 'policy' ) and $m->policy ) {
115 $policy = $merger->merge( $policy, { $mod => $m->policy } );
116 }
117  
118 return;
119 }
120  
121 sub load_config_file {
122 my ( $class, $filename ) = @_;
123 my $new = decode_json read_file $filename;
124 $config = $merger->merge( $config, $new ) if $new;
125  
126 return !!$new;
127 }
128  
129 sub load_policy_file {
130 my ( $self, $filename ) = @_;
131  
132 if ( not -r $filename ) {
133 foreach my $dir ( _config_directory_list() ) {
134 my $name = File::Spec->catfile( $dir, $filename );
135 if ( -r $name ) {
136 $filename = $name;
137 last;
138 }
139 else {
140 if ( -r $name . '.json' ) {
141 $filename = $name . '.json';
142 last;
143 }
144 }
145 }
146 }
147  
148 my $new = decode_json read_file $filename;
149 if ( $new ) {
150 my $tc = $new->{__testcases__};
151 delete $new->{__testcases__};
152 foreach my $case (keys %$tc) {
153 $self->testcases->{$case} = $tc->{$case};
154 }
155 $policy = $merger->merge( $policy, $new );
156 push @{ $self->pfiles }, $filename if ( ref( $self ) and $self->isa( __PACKAGE__ ) );
157 }
158  
159 return !!$new;
160 } ## end sub load_policy_file
161  
162 sub no_network {
163 my ( $class, $value ) = @_;
164  
165 if ( defined( $value ) ) {
166 $class->get->{no_network} = $value;
167 }
168  
169 return $class->get->{no_network};
170 }
171  
172 sub ipv4_ok {
173 my ( $class, $value ) = @_;
174  
175 if ( defined( $value ) ) {
176 $class->get->{net}{ipv4} = $value;
177 }
178  
179 return $class->get->{net}{ipv4};
180 }
181  
182 sub ipv6_ok {
183 my ( $class, $value ) = @_;
184  
185 if ( defined( $value ) ) {
186 $class->get->{net}{ipv6} = $value;
187 }
188  
189 return $class->get->{net}{ipv6};
190 }
191  
192 sub resolver_defaults {
193 my ( $class ) = @_;
194  
195 return $class->get->{resolver}{defaults};
196 }
197  
198 sub resolver_source {
199 my ($class, $sourceaddr) = @_;
200  
201 if (defined($sourceaddr)) {
202 $class->get->{resolver}{source} = $sourceaddr;
203 }
204  
205 return $class->get->{resolver}{source};
206 }
207  
208 sub logfilter {
209 my ( $class ) = @_;
210  
211 return $class->get->{logfilter};
212 }
213  
214 sub asnroots {
215 my ( $class ) = @_;
216  
217 return $class->get->{asnroots};
218 }
219  
220 sub should_run {
221 my ( $self, $name ) = @_;
222  
223 if (not defined $self->testcases->{$name}) {
224 return 1; # Default to runnings test
225 }
226 elsif ($self->testcases->{$name}) {
227 return 1;
228 }
229 else {
230 return;
231 }
232 }
233  
234 no Moose;
235 __PACKAGE__->meta->make_immutable;
236  
237 1;
238  
239 =head1 NAME
240  
241 Zonemaster::Config - configuration access module for Zonemaster
242  
243 =head1 SYNOPSIS
244  
245 Zonemaster->config->no_network(1); # Forbid network traffic
246  
247 my $value = Zonemaster::Config->get->{key}{subkey}; # Not really recommended way to access config data
248  
249 =head1 LOADING CONFIGURATION
250  
251 Configuration data is loaded in several stages, each one overlaying the result
252 from the previous one (that is, the later in the list take priority over the
253 earlier). The first stage is hardcoded into the source code and loaded while it
254 is being compiled, to make sure that there will always be some basic
255 information available. Later, when the configuration object is first used, the
256 system will look for a file named F<config.json> in each of a list of
257 directories. If the file exists, is readable and contains proper JSON data, it
258 will be loaded and overlaid on the current internal config. The directories
259 are, in order from first checked to last:
260  
261 =over
262  
263 =item The L<Zonemaster> perl module installation directory
264  
265 This is where the installation process puts the default configuration. It is
266 not meant to be modified by the user, and it will be overwritten when the
267 module is upgraded (or reinstalled for any other reason). If you really need to
268 know where it is, you can either check the log message left when loading it or
269 run this command to find the path:
270  
271 perl -MFile::ShareDir=dist_dir -E 'say dist_dir( "Zonemaster" )'
272  
273 =item /etc/zonemaster
274  
275 Intended to hold system-global configuration changes.
276  
277 =item /usr/local/etc/zonemaster
278  
279 Basically the same as the previous one, but for those who like to keep their
280 locally installed software inside F</usr/local>.
281  
282 =item ~/.zonemaster
283  
284 That is, a F<.zonemaster> directory in the home directory of the current user.
285 Intended, obviously, for configuration changes local to one particular user.
286  
287 =back
288  
289 The possible contents of the JSON data is described further down in this manual
290 page.
291  
292 =head1 METHODS FOR CONFIGURATION ITEMS
293  
294 =over
295  
296 =item no_network([$value])
297  
298 Returns the value of the C<no_network> flag. If given a defined value, sets the value to that value.
299  
300 =item ipv4_ok([$value])
301  
302 Returns the value of the C<ipv4> flag. If given a defined value, sets the value to that value.
303  
304 =item ipv6_ok([$value])
305  
306 Returns the value of the C<ipv6> flag. If given a defined value, sets the value to that value.
307  
308 =item resolver_defaults()
309  
310 Returns a reference to the resolver_defaults hash.
311  
312 =item resolver_source([$addr])
313  
314 Returns the source address all resolver objects should use when sending
315 queries, if one is set. If given an argument, sets the source address to the
316 argument.
317  
318 =item logfilter()
319  
320 Returns a reference to the logfilter hash.
321  
322 =item asnroots()
323  
324 Returns a reference to the list of ASN lookup domains.
325  
326 =back
327  
328 =head1 METHODS
329  
330 =over
331  
332 =item get()
333  
334 Returns a reference to a hash with configuration values.
335  
336 =item policy()
337  
338 Returns a reference to the current policy data. The format of that data is described further down in this document.
339  
340 =item load_policy_file($filename)
341  
342 Load policy information from the given file and merge it into the pre-loaded
343 policy. Information from the loaded file overrides the pre-loaded information
344 when the same keys exist in both places.
345  
346 If the given name does not lead directly to a readable file, each of the usual
347 directories will be checked if the name is there. If the plain name isn't, the
348 suffix C<.json> will be appended and another try will be done. For example, a
349 file F<$HOME/.zonemaster/Example.json> may be loaded by calling this method
350 with the string C<"Example">.
351  
352 =item load_config_file($filename)
353  
354 Load configuration information from the given file and merge it into the pre-loaded config. Information from the loaded file overrides the pre-loaded information when the same keys exist in both places.
355  
356 =item load_module_policy($module)
357  
358 Loads policy data included in a test module. The argument must be the short
359 form (without the initial C<Zonemaster::Test::>) and correctly capitalized.
360  
361 =item BUILD
362  
363 Internal method only mentioned here to please L<Pod::Coverage>.
364  
365 =item should_run($name)
366  
367 Given a test case name, it returns true if that test case should be included in
368 a test run according to the currently active policy or false if not.
369  
370 =back
371  
372 =head1 CONFIGURATION DATA
373  
374 The configuration data is stored internally in a nested hash (possibly with arrays as values in places). As of this writing, the file format used is JSON.
375  
376 The interesting keys are as follows.
377  
378 =head2 resolver
379  
380 =head3 defaults
381  
382 These are the default flag and timing values used for the resolver objects used to actually send DNS queries.
383  
384 =over
385  
386 =item usevc
387  
388 If set, only use TCP. Default not set.
389  
390 =item retrans
391  
392 The number of seconds between retries. Default 3.
393  
394 =item dnssec
395  
396 If set, sets the DO flag in queries. Default not set.
397  
398 =item recurse
399  
400 If set, sets the RD flag in queries. Default not set (and almost certainly should remain that way).
401  
402 =item retry
403  
404 The number of times a query is sent before we give up. Can be set to zero, although that's not very useful (since no queries will be sent at all). Defaults to 2.
405  
406 =item igntc
407  
408 If set, queries that get truncated UDP responses will be automatically retried over TCP. Default not set.
409  
410 =back
411  
412 =head2 net
413  
414 =over
415  
416 =item ipv4
417  
418 If set, resolver objects are allowed to send queries over IPv4. Default set.
419  
420 =item ipv6
421  
422 If set, resolver objects are allowed to send queries over IPv6. Default set.
423  
424 =back
425  
426 =head2 no_network
427  
428 If set to a true value, network traffic is forbidden. Use when you want to be sure that any data is only taken from a preloaded cache.
429  
430 =head2 asnroots
431  
432 This key must be a list of domain names. The domains will be assumed to be
433 Cymru-style AS lookup zones. Normally only the first name in the list will be
434 used, the rest are backups in case the earlier ones don't work.
435  
436 =head2 logfilter
437  
438 By using this key, the log level of messages can be set in a much more fine-grained way than by the policy file. The intended use is to remove known erroneous results. If you, for example, know that a certain name server is recursive and for some reason should be, you can use this functionality to lower the severity of the complaint about it to a lower level than normal.
439  
440 The the data under the C<logfilter> key should be structured like this:
441  
442 Module
443 Tag
444 "when"
445 Hash with conditions
446 "set"
447 Level to set if all conditions match
448  
449 The hash with conditions should have keys matching the attributes of the log entry that's being filtered (check the translation files to see what they are). The values for the keys should be either a single value that the attribute should be, or an array of values any one of which the attribute should be.
450  
451 A complete entry might could look like this:
452  
453 "SYSTEM": {
454 "FILTER_THIS": {
455 "when": {
456 "count": 1,
457 "type": ["this", "or"]
458 },
459 "set": "INFO"
460 }
461 }
462  
463 This would set the level to C<INFO> for any C<SYSTEM:FILTER_THIS> messages that had a C<count> attribute set to 1 and a C<type> attribute set to either C<this> or C<or>.
464  
465 =head1 POLICY DATA
466  
467 Like the configuration data, policy data is stored in JSON format. Structurally, it's a bit less complex. All the keys on the top level, with one exception, are names of test implementation modules (without the C<Zonemaster::Test::> prefix). Each of those keys hold another hash, where the keys are the tags that the module in question can emit and the values are the the severity levels that should apply to the tags. Any tags that are not found in the policy data will default to level C<DEBUG>.
468  
469 The one exception is a top-level key C<__testcases__>. The value of that must be a hash where the keys are names of test cases from the test specifications, and the corresponding values are booleans specifying if the test case in question should be executed or not. Any missing test cases are treated as if they had the value C<true> set. The test cases C<basic00>, C<basic01> and C<basic02> will be executed even if their values are set to C<false>, since part of their function is to verify that the given name can be tested at all. The values here only apply when test modules are asked to run all their tests. A test case that is set to C<false> here will still run if asked for specifically.
470  
471 The easiest way to create a modified policy is to copy the default one and change the relevant values.
472  
473 =cut
474  
475 __DATA__
476 {
477 "asnroots" : [ "asnlookup.zonemaster.net", "asnlookup.iis.se"],
478 "net" : {
479 "ipv4" : 1,
480 "ipv6" : 1
481 },
482 "no_network" : 0,
483 "resolver" : {
484 "defaults" : {
485 "debug" : 0,
486 "dnssec" : 0,
487 "edns_size" : 0,
488 "igntc" : 0,
489 "recurse" : 0,
490 "retrans" : 3,
491 "retry" : 2,
492 "usevc" : 0
493 }
494 }
495 }