Zonemaster/Backend/Config.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« auto/share/dist/Zonemaster-Backend/patch/patch_postgresql_db_zonemaster_backend_ver_5.0.0.pl ↑ Summary Zonemaster/Backend/Validator.pm »
1 package Zonemaster::Backend::Config;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.14.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '1.1.0';
7  
8 1 ▶use Carp qw( confess croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir qw[dist_file];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Slurp qw( read_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use Zonemaster::Backend::Validator qw( :untaint );
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Zonemaster::Backend::DB;
17  
18 Readonly my @SIG_NAME => split ' ', $Config{sig_name};
19  
20 =head1 CLASS METHODS
21  
22 =head2 get_default_path
23  
24 Determine the path for the default backend_config.ini file.
25 A list of values and locations are checked and the first match is returned.
26 If all places are checked and no file is found, an exception is thrown.
27  
28 This procedure is idempotent - i.e. if you call this procedure multiple times
29 the same value is returned no matter if environment variables or the file system
30 have changed.
31  
32 The following checks are made in order:
33  
34 =over 4
35  
36 =item $ZONEMASTER_BACKEND_CONFIG_FILE
37  
38 If this environment variable is set ot a truthy value, that path is returned.
39  
40 =item /etc/zonemaster/backend_config.ini
41  
42 If a file exists at this path, it is returned.
43  
44 =item /usr/local/etc/zonemaster/backend_config.ini
45  
46 If a file exists at such a path, it is returned.
47  
48 =item DIST_DIR/backend_config.ini
49  
50 If a file exists at this path, it is returned.
51 DIST_DIR is wherever File::ShareDir installs the Zonemaster-Backend dist.
52  
53 =back
54  
55 =cut
56  
57 sub get_default_path {
58 state $path =
59 $ENV{ZONEMASTER_BACKEND_CONFIG_FILE} ? $ENV{ZONEMASTER_BACKEND_CONFIG_FILE}
60 : -e '/etc/zonemaster/backend_config.ini' ? '/etc/zonemaster/backend_config.ini'
61 : -e '/usr/local/etc/zonemaster/backend_config.ini' ? '/usr/local/etc/zonemaster/backend_config.ini'
62 : eval { dist_file( 'Zonemaster-Backend', 'backend_config.ini' ) };
63 return $path // croak "File not found: backend_config.ini\n";
64 }
65  
66 =head2 load_profiles
67  
68 Loads and returns a set of named profiles.
69  
70 my %all_profiles = (
71 $config->PUBLIC_PROFILES,
72 $config->PRIVATE_PROFILES,
73 );
74 my %profiles = %{ Zonemaster::Backend::Config->load_profiles( %all_profiles ) };
75  
76 Takes a hash mapping profile names to profile paths.
77 An `undef` path value means the default profile.
78  
79 Returns a hashref mapping profile names to profile objects.
80  
81 The returned profiles have omitted values filled in with defaults from the
82 default profile.
83  
84 Dies if any of the given paths cannot be read or their contents cannot be parsed
85 as JSON.
86  
87 =cut
88  
89 sub load_profiles {
90 my ( $class, %profile_paths ) = @_;
91  
92 my %profiles;
93 foreach my $name ( keys %profile_paths ) {
94 my $path = $profile_paths{$name};
95  
96 my $full_profile = Zonemaster::Engine::Profile->default;
97 if ( defined $path ) {
98 my $json = eval { read_file( $path, err_mode => 'croak' ) } #
99 // die "Error loading profile '$name': $@";
100 my $named_profile = eval { Zonemaster::Engine::Profile->from_json( $json ) } #
101 // die "Error loading profile '$name' at '$path': $@";
102 $full_profile->merge( $named_profile );
103 }
104 $profiles{$name} = $full_profile;
105 }
106  
107 return \%profiles;
108 }
109  
110 =head1 CONSTRUCTORS
111  
112 =head2 load_config
113  
114 A wrapper around L<parse> that also determines where the config file is located
115 in the file system and reads it.
116  
117 Throws an exception if the determined configuration file cannot be read.
118 See L<parse> for details on additional parsing-related error modes.
119  
120 =cut
121  
122 sub load_config {
123 my ( $class ) = @_;
124  
125 my $path = get_default_path();
126 $log->notice( "Loading config: $path" );
127 my $text = read_file $path;
128  
129 my $obj = eval { $class->parse( $text ) };
130 if ( $@ ) {
131 die "File $path: $@";
132 }
133  
134 return $obj;
135 }
136  
137 =head2 parse
138  
139 Construct a new Zonemaster::Backend::Config based on a given configuration.
140  
141 my $config = Zonemaster::Backend::Config->parse(
142 q{
143 [DB]
144 engine = SQLite
145  
146 [SQLITE]
147 database_file = /var/db/zonemaster.sqlite
148 }
149 );
150  
151 The configuration is interpreted according to the
152 L<configuration format specification|https://doc.zonemaster.net/latest/configuration/backend.html>.
153  
154 Returns a new Zonemaster::Backend::Config instance with its properties set to
155 normalized and untainted values according to the given configuration with
156 defaults according to the configuration format.
157  
158 Emits a log warning with a deprecation message for each deprecated property that
159 is present.
160  
161 Throws an exception if the given configuration contains errors.
162  
163 In a valid config file:
164  
165 =over 4
166  
167 =item
168  
169 all required properties are present, and
170  
171 =item
172  
173 all sections and properties are recognized.
174  
175 =back
176  
177 =cut
178  
179 sub parse {
180 my ( $class, $text ) = @_;
181  
182 my $obj = bless( {}, $class );
183 $obj->{_public_profiles} = {};
184 $obj->{_private_profiles} = {};
185 $obj->{_tld_url_override} = {};
186  
187 my $ini = Config::IniFiles->new( -file => \$text )
188 or die "Failed to parse config: " . join( '; ', @Config::IniFiles::errors ) . "\n";
189  
190 my $get_and_clear = sub { # Read and clear a property from a Config::IniFiles object.
191 my ( $section, $param ) = @_;
192 my ( $value, @extra ) = $ini->val( $section, $param );
193 if ( @extra ) {
194 die "Property not unique: $section.$param\n";
195 }
196 $ini->delval( $section, $param );
197 return $value;
198 };
199  
200 # Validate section names
201 {
202 my %sections = map { $_ => 1 } ( 'DB', 'MYSQL', 'POSTGRESQL', 'SQLITE', 'LANGUAGE', 'PUBLIC PROFILES', 'PRIVATE PROFILES', 'ZONEMASTER', 'METRICS', 'RPCAPI', 'TLD URL SETTINGS', 'TLD URL OVERRIDE' );
203 for my $section ( $ini->Sections ) {
204 if ( !exists $sections{$section} ) {
205 die "config: unrecognized section: $section\n";
206 }
207 }
208 }
209  
210 # Assign default values
211 $obj->_set_DB_polling_interval( '0.5' );
212 $obj->_set_MYSQL_port( '3306' );
213 $obj->_set_POSTGRESQL_port( '5432' );
214 $obj->_set_TLD_URL_SETTINGS_enable_tld_url( 'true' );
215 $obj->_set_TLD_URL_SETTINGS_lookup_timeout( '3' );
216 $obj->_set_TLD_URL_SETTINGS_include_source( 'true' );
217 $obj->_set_ZONEMASTER_max_zonemaster_execution_time( '600' );
218 $obj->_set_ZONEMASTER_number_of_processes_for_frontend_testing( '20' );
219 $obj->_set_ZONEMASTER_number_of_processes_for_batch_testing( '20' );
220 $obj->_set_ZONEMASTER_lock_on_queue( '0' );
221 $obj->_set_ZONEMASTER_age_reuse_previous_test( '600' );
222 $obj->_set_RPCAPI_enable_user_create( 'no' ); # experimental
223 $obj->_set_RPCAPI_enable_batch_create( 'yes' ); # experimental
224 $obj->_set_RPCAPI_enable_add_api_user( 'no' );
225 $obj->_set_RPCAPI_enable_add_batch_job( 'yes' );
226 $obj->_set_locales( 'en_US' );
227 $obj->_add_public_profile( 'default', undef );
228 $obj->_set_METRICS_statsd_port( '8125' );
229  
230 # Assign property values (part 1/2)
231 if ( defined( my $value = $get_and_clear->( 'DB', 'engine' ) ) ) {
232 $obj->_set_DB_engine( $value );
233 }
234  
235 # Check required properties (part 1/2)
236 if ( !defined $obj->DB_engine ) {
237 die "config: missing required property DB.engine\n";
238 }
239  
240 # Check deprecated properties and assign fallback values
241 my @warnings;
242  
243 #currently no deprecation warnings
244  
245 # Assign property values (part 2/2)
246 if ( defined( my $value = $get_and_clear->( 'DB', 'polling_interval' ) ) ) {
247 $obj->_set_DB_polling_interval( $value );
248 }
249 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'host' ) ) ) {
250 $obj->_set_MYSQL_host( $value );
251 }
252 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'port' ) ) ) {
253 if ( $obj->MYSQL_host eq 'localhost' ) {
254 push @warnings, "MYSQL.port is disregarded if MYSQL.host is set to 'localhost'";
255 }
256 $obj->{_MYSQL_port} = $value;
257 }
258 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'user' ) ) ) {
259 $obj->_set_MYSQL_user( $value );
260 }
261 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'password' ) ) ) {
262 $obj->_set_MYSQL_password( $value );
263 }
264 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'database' ) ) ) {
265 $obj->_set_MYSQL_database( $value );
266 }
267 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'host' ) ) ) {
268 $obj->_set_POSTGRESQL_host( $value );
269 }
270 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'port' ) ) ) {
271 $obj->{_POSTGRESQL_port} = $value;
272 }
273 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'user' ) ) ) {
274 $obj->_set_POSTGRESQL_user( $value );
275 }
276 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'password' ) ) ) {
277 $obj->_set_POSTGRESQL_password( $value );
278 }
279 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'database' ) ) ) {
280 $obj->_set_POSTGRESQL_database( $value );
281 }
282 if ( defined( my $value = $get_and_clear->( 'SQLITE', 'database_file' ) ) ) {
283 $obj->_set_SQLITE_database_file( $value );
284 }
285 if ( defined( my $value = $get_and_clear->( 'TLD URL SETTINGS', 'enable_tld_url' ) ) ) {
286 $obj->_set_TLD_URL_SETTINGS_enable_tld_url( $value );
287 }
288 if ( defined( my $value = $get_and_clear->( 'TLD URL SETTINGS', 'lookup_timeout' ) ) ) {
289 $obj->_set_TLD_URL_SETTINGS_lookup_timeout( $value );
290 }
291 if ( defined( my $value = $get_and_clear->( 'TLD URL SETTINGS', 'include_source' ) ) ) {
292 $obj->_set_TLD_URL_SETTINGS_include_source( $value );
293 }
294 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'max_zonemaster_execution_time' ) ) ) {
295 $obj->_set_ZONEMASTER_max_zonemaster_execution_time( $value );
296 }
297 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'number_of_processes_for_frontend_testing' ) ) ) {
298 $obj->_set_ZONEMASTER_number_of_processes_for_frontend_testing( $value );
299 }
300 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'number_of_processes_for_batch_testing' ) ) ) {
301 $obj->_set_ZONEMASTER_number_of_processes_for_batch_testing( $value );
302 }
303 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'lock_on_queue' ) ) ) {
304 $obj->_set_ZONEMASTER_lock_on_queue( $value );
305 }
306 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'age_reuse_previous_test' ) ) ) {
307 $obj->_set_ZONEMASTER_age_reuse_previous_test( $value );
308 }
309 if ( defined( my $value = $get_and_clear->( 'METRICS', 'statsd_host' ) ) ) {
310 $obj->_set_METRICS_statsd_host( $value );
311 }
312 if ( defined( my $value = $get_and_clear->( 'METRICS', 'statsd_port' ) ) ) {
313 $obj->_set_METRICS_statsd_port( $value );
314 }
315 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_user_create' ) ) ) {
316 if ( defined( $get_and_clear->( 'RPCAPI', 'enable_add_api_user' ) ) ) {
317 die "Error: cannot specify both RPCAPI.enable_add_api_user and RPCAPI.enable_user_create\n";
318 }
319 $obj->_set_RPCAPI_enable_add_api_user( $value );
320 $obj->_set_RPCAPI_enable_user_create( $value );
321 }
322 else {
323 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_add_api_user' ) ) ) {
324 $obj->_set_RPCAPI_enable_add_api_user( $value );
325 $obj->_set_RPCAPI_enable_user_create( $value );
326 }
327 }
328 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_batch_create' ) ) ) {
329 if ( defined( $get_and_clear->( 'RPCAPI', 'enable_add_batch_job' ) ) ) {
330 die "Error: cannot specify both RPCAPI.enable_add_batch_job and RPCAPI.enable_batch_create\n";
331 }
332 $obj->_set_RPCAPI_enable_add_batch_job( $value );
333 $obj->_set_RPCAPI_enable_batch_create( $value );
334 }
335 else {
336 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_add_batch_job' ) ) ) {
337 $obj->_set_RPCAPI_enable_add_batch_job( $value );
338 $obj->_set_RPCAPI_enable_batch_create( $value );
339 }
340 }
341 if ( defined( my $value = $get_and_clear->( 'LANGUAGE', 'locale' ) ) ) {
342 $obj->_set_locales( $value );
343 }
344  
345 for my $name ( $ini->Parameters( 'PUBLIC PROFILES' ) ) {
346 my $path = $get_and_clear->( 'PUBLIC PROFILES', $name );
347 $obj->_add_public_profile( $name, $path );
348 }
349  
350 for my $name ( $ini->Parameters( 'PRIVATE PROFILES' ) ) {
351 my $path = $get_and_clear->( 'PRIVATE PROFILES', $name );
352 $obj->_add_private_profile( $name, $path );
353 }
354  
355 for my $name ( $ini->Parameters( 'TLD URL OVERRIDE' ) ) {
356 my $path = $get_and_clear->( 'TLD URL OVERRIDE', $name );
357 $obj->_add_tld_url_override( $name, $path );
358 }
359  
360 # Check required propertys (part 2/2)
361 if ( $obj->DB_engine eq 'MySQL' ) {
362 die "config: missing required property MYSQL.host (required when DB.engine = MySQL)\n"
363 if !defined $obj->MYSQL_host;
364  
365 die "config: missing required property MYSQL.user (required when DB.engine = MySQL)\n"
366 if !defined $obj->MYSQL_user;
367  
368 die "config: missing required property MYSQL.password (required when DB.engine = MySQL)\n"
369 if !defined $obj->MYSQL_password;
370  
371 die "config: missing required property MYSQL.database (required when DB.engine = MySQL)\n"
372 if !defined $obj->MYSQL_database;
373 }
374 elsif ( $obj->DB_engine eq 'PostgreSQL' ) {
375 die "config: missing required property POSTGRESQL.host (required when DB.engine = PostgreSQL)\n"
376 if !defined $obj->POSTGRESQL_host;
377  
378 die "config: missing required property POSTGRESQL.user (required when DB.engine = PostgreSQL)\n"
379 if !defined $obj->POSTGRESQL_user;
380  
381 die "config: missing required property POSTGRESQL.password (required when DB.engine = PostgreSQL)\n"
382 if !defined $obj->POSTGRESQL_password;
383  
384 die "config: missing required property POSTGRESQL.database (required when DB.engine = PostgreSQL)\n"
385 if !defined $obj->POSTGRESQL_database;
386 }
387 elsif ( $obj->DB_engine eq 'SQLite' ) {
388 die "config: missing required property SQLITE.database_file (required when DB.engine = SQLite)\n"
389 if !defined $obj->SQLITE_database_file;
390 }
391  
392 # Check unknown property names
393 {
394 my @unrecognized;
395 for my $section ( $ini->Sections ) {
396 for my $param ( $ini->Parameters( $section ) ) {
397 push @unrecognized, "$section.$param";
398 }
399 }
400 if ( @unrecognized ) {
401 die "config: unrecognized property(s): " . join( ", ", sort @unrecognized ) . "\n";
402 }
403 }
404  
405 # Emit deprecation warnings
406 for my $message ( @warnings ) {
407 $log->warning( $message );
408 }
409  
410 return $obj;
411 }
412  
413 =head1 METHODS
414  
415 =head2 check_db
416  
417 Returns a normalized string based on the supported databases.
418  
419 =head3 EXCEPTION
420  
421 Dies if the value is not one of SQLite, PostgreSQL or MySQL.
422  
423 =cut
424  
425 sub check_db {
426 my ( $self, $db ) = @_;
427  
428 $db = untaint_engine_type( $db ) #
429 // die "Unknown database '$db', should be one of SQLite, MySQL or PostgreSQL\n";
430  
431 return _normalize_engine_type( $db );
432 }
433  
434 =head2 DB_engine
435  
436 Get the value of L<DB.engine|https://doc.zonemaster.net/latest/configuration/backend.html#engine>.
437  
438 Returns one of C<"SQLite">, C<"PostgreSQL"> or C<"MySQL">.
439  
440 =cut
441  
442 sub DB_engine {
443 my ( $self ) = @_;
444 return $self->{_DB_engine};
445 }
446  
447 sub _set_DB_engine {
448 my ( $self, $value ) = @_;
449  
450 $value = untaint_engine_type( $value ) #
451 // die "Invalid value for DB.engine: $value\n";
452  
453 $self->{_DB_engine} = _normalize_engine_type( $value );
454 return;
455 }
456  
457 =head2 DB_polling_interval
458  
459 Get the value of L<DB.polling_interval|https://doc.zonemaster.net/latest/configuration/backend.html#polling_interval>.
460  
461 Returns a number.
462  
463  
464 =head2 MYSQL_database
465  
466 Get the value of L<MYSQL.database|https://doc.zonemaster.net/latest/configuration/backend.md#database>.
467  
468 Returns a string.
469  
470  
471 =head2 MYSQL_host
472  
473 Get the value of L<MYSQL.host|https://doc.zonemaster.net/latest/configuration/backend.md#host>.
474  
475 Returns a string.
476  
477  
478 =head2 MYSQL_port
479  
480 Returns the L<MYSQL.port|https://doc.zonemaster.net/latest/configuration/backend.md#port>
481 property from the loaded config.
482  
483 Returns a number.
484  
485  
486 =head2 MYSQL_password
487  
488 Get the value of L<MYSQL.password|https://doc.zonemaster.net/latest/configuration/backend.md#password>.
489  
490 Returns a string.
491  
492  
493 =head2 MYSQL_user
494  
495 Get the value of L<MYSQL.user|https://doc.zonemaster.net/latest/configuration/backend.md#user>.
496  
497 Returns a string.
498  
499  
500 =head2 POSTGRESQL_database
501  
502 Get the value of L<POSTGRESQL.database|https://doc.zonemaster.net/latest/configuration/backend.md#database-1>.
503  
504 Returns a string.
505  
506  
507 =head2 POSTGRESQL_host
508  
509 Get the value of L<POSTGRESQL.host|https://doc.zonemaster.net/latest/configuration/backend.md#host-1>.
510  
511 Returns a string.
512  
513  
514 =head2 POSTGRESQL_port
515  
516 Returns the L<POSTGRESQL.port|https://doc.zonemaster.net/latest/configuration/backend.md#port-1>
517 property from the loaded config.
518  
519 Returns a number.
520  
521  
522 =head2 POSTGRESQL_password
523  
524 Get the value of L<POSTGRESQL.password|https://doc.zonemaster.net/latest/configuration/backend.md#password-1>.
525  
526 Returns a string.
527  
528  
529 =head2 POSTGRESQL_user
530  
531 Get the value of L<POSTGRESQL.user|https://doc.zonemaster.net/latest/configuration/backend.md#user-1>.
532  
533 Returns a string.
534  
535  
536 =head2 SQLITE_database_file
537  
538 Get the value of L<SQLITE.database_file|https://doc.zonemaster.net/latest/configuration/backend.md#database_file>.
539  
540 Returns a string.
541  
542  
543 =head2 LANGUAGE_locale
544  
545 Get the value of L<LANGUAGE.locale|https://doc.zonemaster.net/latest/configuration/backend.md#locale>.
546  
547 Returns a mapping from two-letter locale tag prefixes to full locale tags.
548 This is represented by a hash mapping prefix to full locale tag.
549  
550 E.g.:
551  
552 (
553 en => "en_US",
554 sv => "sv_SE",
555 )
556  
557  
558 =head2 PUBLIC_PROFILES
559  
560 Get the set of L<PUBLIC PROFILES|https://doc.zonemaster.net/latest/configuration/backend.md#public-profiles-and-private-profiles-sections>.
561  
562 Returns a hash mapping profile names to profile paths.
563 The profile names are normalized to lowercase.
564 Profile paths are either strings or C<undef>.
565 C<undef> means that the Zonemaster Engine default profile should be used.
566  
567  
568 =head2 PRIVATE_PROFILES
569  
570 Get the set of L<PRIVATE PROFILES|https://doc.zonemaster.net/latest/configuration/backend.md#public-profiles-and-private-profiles-sections>.
571  
572 Returns a hash mapping profile names to profile paths.
573 The profile names are normalized to lowercase.
574 Profile paths are always strings (contrast with L<PUBLIC_PROFILES>).
575  
576 =head2 TLD_URL_SETTINGS_enable_tld_url
577  
578 Get the value of L<TLD_URL_SETTINGS.enable_tld_url|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#enable_tld_url>.
579  
580 Returns a boolean.
581  
582  
583 =head2 TLD_URL_SETTINGS_lookup_timeout
584  
585 Get the value of L<TLD_URL_SETTINGS.lookup_timeout|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#lookup_timeout>.
586  
587 Returns a positive integer.
588  
589  
590 =head2 TLD_URL_SETTINGS_include_source
591  
592 Get the value of L<TLD_URL_SETTINGS.include_source|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#include_source>.
593  
594 Returns a boolean.
595  
596  
597 =head2 TLD_URL_OVERRIDE
598  
599 Get the set of L<TLD URL OVERRIDE|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#tld-url-override-section>.
600  
601 Returns a hash mapping TLD label (ASCII or A-label) to URL string or blocking policy.
602 The TLD label is normalized to lowercase. See the meaning of
603 L<URL string or blocking policy|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/tld-url-specification.md#url-string-or-blocking-policy>.
604  
605  
606 =head2 ZONEMASTER_max_zonemaster_execution_time
607  
608 Get the value of L<ZONEMASTER.max_zonemaster_execution_time|https://doc.zonemaster.net/latest/configuration/backend.md#max_zonemaster_execution_time>.
609  
610 Returns a number.
611  
612  
613 =head2 ZONEMASTER_number_of_processes_for_frontend_testing
614  
615 Get the value of
616 L<ZONEMASTER.number_of_processes_for_frontend_testing|https://doc.zonemaster.net/latest/configuration/backend.md#number_of_processes_for_frontend_testing>.
617  
618 Returns a number.
619  
620  
621 =head2 ZONEMASTER_number_of_processes_for_batch_testing
622  
623 Get the value of
624 L<ZONEMASTER.number_of_processes_for_batch_testing|https://doc.zonemaster.net/latest/configuration/backend.md#number_of_processes_for_batch_testing>.
625  
626 Returns a number.
627  
628  
629 =head2 ZONEMASTER_lock_on_queue
630  
631 Get the value of
632 L<ZONEMASTER.lock_on_queue|https://doc.zonemaster.net/latest/configuration/backend.md#lock_on_queue>.
633  
634 Returns a number.
635  
636  
637 =head2 ZONEMASTER_age_reuse_previous_test
638  
639 Get the value of
640 L<ZONEMASTER.age_reuse_previous_test|https://doc.zonemaster.net/latest/configuration/backend.md#age_reuse_previous_test>.
641  
642 Returns a number.
643  
644  
645 =head2 METRICS_statsd_host
646  
647 Get the value of
648 L<METRICS.statsd_host|https://doc.zonemaster.net/latest/configuration/backend.md#statsd_host>.
649  
650 Returns a string.
651  
652  
653 =head2 METRICS_statsd_port
654  
655 Get the value of
656 L<METRICS.statsd_host|https://doc.zonemaster.net/latest/configuration/backend.md#statsd_port>.
657  
658 Returns a number.
659  
660  
661 =head2 RPCAPI_enable_user_create
662  
663 Experimental.
664 Get the value of
665 L<RPCAPI.enable_user_create|https://doc.zonemaster.net/latest/configuration/backend.md#enable_user_create>.
666  
667 Return 0 or 1
668  
669  
670 =head2 RPCAPI_enable_batch_create
671  
672 Experimental.
673 Get the value of
674 L<RPCAPI.enable_batch_create|https://doc.zonemaster.net/latest/configuration/backend.md#enable_batch_create>.
675  
676 Return 0 or 1
677  
678  
679 =head2 RPCAPI_enable_add_api_user
680  
681 Get the value of
682 L<RPCAPI.enable_add_api_user|https://doc.zonemaster.net/latest/configuration/backend.md#enable_add_api_user>.
683  
684 Return 0 or 1
685  
686  
687 =head2 RPCAPI_enable_add_batch_job
688  
689 Get the value of
690 L<RPCAPI.enable_add_batch_job|https://doc.zonemaster.net/latest/configuration/backend.md#enable_add_batch_job>.
691  
692 Return 0 or 1
693  
694 =cut
695  
696 # Getters for the properties documented above
697 sub DB_polling_interval { return $_[0]->{_DB_polling_interval}; }
698 sub MYSQL_host { return $_[0]->{_MYSQL_host}; }
699 sub MYSQL_port { return $_[0]->{_MYSQL_port}; }
700 sub MYSQL_user { return $_[0]->{_MYSQL_user}; }
701 sub MYSQL_password { return $_[0]->{_MYSQL_password}; }
702 sub MYSQL_database { return $_[0]->{_MYSQL_database}; }
703 sub POSTGRESQL_host { return $_[0]->{_POSTGRESQL_host}; }
704 sub POSTGRESQL_port { return $_[0]->{_POSTGRESQL_port}; }
705 sub POSTGRESQL_user { return $_[0]->{_POSTGRESQL_user}; }
706 sub POSTGRESQL_password { return $_[0]->{_POSTGRESQL_password}; }
707 sub POSTGRESQL_database { return $_[0]->{_POSTGRESQL_database}; }
708 sub SQLITE_database_file { return $_[0]->{_SQLITE_database_file}; }
709 sub LANGUAGE_locale { return %{ $_[0]->{_LANGUAGE_locale} }; }
710 sub PUBLIC_PROFILES { return %{ $_[0]->{_public_profiles} }; }
711 sub PRIVATE_PROFILES { return %{ $_[0]->{_private_profiles} }; }
712 sub TLD_URL_SETTINGS_enable_tld_url { return $_[0]->{_TLD_URL_SETTINGS_enable_tld_url}; }
713 sub TLD_URL_SETTINGS_lookup_timeout { return $_[0]->{_TLD_URL_SETTINGS_lookup_timeout}; }
714 sub TLD_URL_SETTINGS_include_source { return $_[0]->{_TLD_URL_SETTINGS_include_source}; }
715 sub TLD_URL_OVERRIDE { return %{ $_[0]->{_tld_url_override} }; }
716 sub ZONEMASTER_max_zonemaster_execution_time { return $_[0]->{_ZONEMASTER_max_zonemaster_execution_time}; }
717 sub ZONEMASTER_lock_on_queue { return $_[0]->{_ZONEMASTER_lock_on_queue}; }
718 sub ZONEMASTER_number_of_processes_for_frontend_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_frontend_testing}; }
719 sub ZONEMASTER_number_of_processes_for_batch_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_batch_testing}; }
720 sub ZONEMASTER_age_reuse_previous_test { return $_[0]->{_ZONEMASTER_age_reuse_previous_test}; }
721 sub METRICS_statsd_host { return $_[0]->{_METRICS_statsd_host}; }
722 sub METRICS_statsd_port { return $_[0]->{_METRICS_statsd_port}; }
723 sub RPCAPI_enable_user_create { return $_[0]->{_RPCAPI_enable_user_create}; } # experimental
724 sub RPCAPI_enable_batch_create { return $_[0]->{_RPCAPI_enable_batch_create}; } # experimental
725 sub RPCAPI_enable_add_api_user { return $_[0]->{_RPCAPI_enable_add_api_user}; }
726 sub RPCAPI_enable_add_batch_job { return $_[0]->{_RPCAPI_enable_add_batch_job}; }
727  
728 # Compile time generation of setters for the properties documented above
729 UNITCHECK {
730 _create_setter( '_set_DB_polling_interval', '_DB_polling_interval', \&untaint_strictly_positive_millis );
731 _create_setter( '_set_MYSQL_host', '_MYSQL_host', \&untaint_host );
732 _create_setter( '_set_MYSQL_port', '_MYSQL_port', \&untaint_strictly_positive_int );
733 _create_setter( '_set_MYSQL_user', '_MYSQL_user', \&untaint_mariadb_user );
734 _create_setter( '_set_MYSQL_password', '_MYSQL_password', \&untaint_password );
735 _create_setter( '_set_MYSQL_database', '_MYSQL_database', \&untaint_mariadb_database );
736 _create_setter( '_set_POSTGRESQL_host', '_POSTGRESQL_host', \&untaint_host );
737 _create_setter( '_set_POSTGRESQL_port', '_POSTGRESQL_port', \&untaint_strictly_positive_int );
738 _create_setter( '_set_POSTGRESQL_user', '_POSTGRESQL_user', \&untaint_postgresql_ident );
739 _create_setter( '_set_POSTGRESQL_password', '_POSTGRESQL_password', \&untaint_password );
740 _create_setter( '_set_POSTGRESQL_database', '_POSTGRESQL_database', \&untaint_postgresql_ident );
741 _create_setter( '_set_SQLITE_database_file', '_SQLITE_database_file', \&untaint_abs_path );
742 _create_setter( '_set_TLD_URL_SETTINGS_enable_tld_url', '_TLD_URL_SETTINGS_enable_tld_url', \&untaint_json_bool );
743 _create_setter( '_set_TLD_URL_SETTINGS_lookup_timeout', '_TLD_URL_SETTINGS_lookup_timeout', \&untaint_strictly_positive_int );
744 _create_setter( '_set_TLD_URL_SETTINGS_include_source', '_TLD_URL_SETTINGS_include_source', \&untaint_json_bool );
745 _create_setter( '_set_ZONEMASTER_max_zonemaster_execution_time', '_ZONEMASTER_max_zonemaster_execution_time', \&untaint_strictly_positive_int );
746 _create_setter( '_set_ZONEMASTER_lock_on_queue', '_ZONEMASTER_lock_on_queue', \&untaint_non_negative_int );
747 _create_setter( '_set_ZONEMASTER_number_of_processes_for_frontend_testing', '_ZONEMASTER_number_of_processes_for_frontend_testing', \&untaint_strictly_positive_int );
748 _create_setter( '_set_ZONEMASTER_number_of_processes_for_batch_testing', '_ZONEMASTER_number_of_processes_for_batch_testing', \&untaint_non_negative_int );
749 _create_setter( '_set_ZONEMASTER_age_reuse_previous_test', '_ZONEMASTER_age_reuse_previous_test', \&untaint_strictly_positive_int );
750 _create_setter( '_set_METRICS_statsd_host', '_METRICS_statsd_host', \&untaint_host );
751 _create_setter( '_set_METRICS_statsd_port', '_METRICS_statsd_port', \&untaint_strictly_positive_int );
752 _create_setter( '_set_RPCAPI_enable_user_create', '_RPCAPI_enable_user_create', \&untaint_bool ); # experimental
753 _create_setter( '_set_RPCAPI_enable_batch_create', '_RPCAPI_enable_batch_create', \&untaint_bool ); # experimental
754 _create_setter( '_set_RPCAPI_enable_add_api_user', '_RPCAPI_enable_add_api_user', \&untaint_bool );
755 _create_setter( '_set_RPCAPI_enable_add_batch_job', '_RPCAPI_enable_add_batch_job', \&untaint_bool );
756 }
757  
758 =head2 new_DB( %opts )
759  
760 Create a new database adapter object according to configuration.
761  
762 =head3 INPUTS
763  
764 Options:
765  
766 =over 4
767  
768 =item override_dbtype
769  
770 A L<DB.engine|https://doc.zonemaster.net/latest/configuration/backend.md#engine> value.
771 Determines the database adapter class to invoke.
772 If not provided, the value of DB.engine is used.
773  
774 =back
775  
776 =head3 RETURNS
777  
778 A configured and connected L<Zonemaster::Backend::DB> object.
779  
780 =head3 EXCEPTIONS
781  
782 =over 4
783  
784 =item Dies if an invalid option is given.
785  
786 =item Dies if the adapter is unable to connect to the database.
787  
788 =back
789  
790 =cut
791  
792 sub new_DB {
793 my ( $self, %opts ) = @_;
794  
795 my $dbtype_opt = delete $opts{override_dbtype};
796  
797 if ( %opts ) {
798 croak 'Unrecognized options: ' . join( ', ', sort keys %opts );
799 }
800  
801 my $dbtype = $self->check_db( $dbtype_opt // $self->DB_engine );
802 my $dbclass = Zonemaster::Backend::DB->get_db_class( $dbtype );
803 my $db = $dbclass->from_config( $self );
804  
805 return $db;
806 }
807  
808 =head2 new_PM
809  
810 Create a new processing manager object according to configuration.
811  
812 =head3 INPUT
813  
814 The values of the following attributes affect the construction of the returned object:
815  
816 =over
817  
818 =item ZONEMASTER_max_zonemaster_execution_time
819  
820 =item ZONEMASTER_number_of_processes_for_batch_testing
821  
822 =item ZONEMASTER_number_of_processes_for_frontend_testing
823  
824 =back
825  
826 =head3 RETURNS
827  
828 A configured L<Parallel::ForkManager> object.
829  
830 =cut
831  
832 sub new_PM {
833 my $self = shift;
834  
835 my $maximum_processes = $self->ZONEMASTER_number_of_processes_for_frontend_testing + $self->ZONEMASTER_number_of_processes_for_batch_testing;
836  
837 my $timeout = $self->ZONEMASTER_max_zonemaster_execution_time;
838  
839 my %times;
840  
841 my $pm = Parallel::ForkManager->new( $maximum_processes );
842 $pm->set_waitpid_blocking_sleep( 0 ) if $pm->can( 'set_waitpid_blocking_sleep' );
843  
844 $pm->run_on_wait(
845 sub {
846 foreach my $pid ( $pm->running_procs ) {
847 my $diff = time() - $times{$pid}[0];
848 my $id = $times{$pid}[1];
849  
850 if ( $diff > $timeout ) {
851 $log->warning( "Worker process (pid $pid, testid $id): Timeout, sending SIGKILL" );
852 kill 9, $pid;
853 }
854 }
855 },
856 1
857 );
858  
859 $pm->run_on_start(
860 sub {
861 my ( $pid, $id ) = @_;
862  
863 $times{$pid} = [ time(), $id ];
864 }
865 );
866  
867 $pm->run_on_finish(
868 sub {
869 my ( $pid, $exitcode, $id, $signal ) = @_;
870  
871 delete $times{$pid};
872  
873 my $message =
874 ( $signal )
875 ? "Terminated by signal $signal (SIG$SIG_NAME[$signal])"
876 : "Terminated with exit code $exitcode";
877  
878 $log->notice( "Worker process (pid $pid, testid $id): $message" );
879 }
880 );
881  
882 return $pm;
883 }
884  
885 sub _set_locales {
886 my ( $self, $value ) = @_;
887  
888 my @locale_tags = split / +/, $value;
889  
890 if ( !@locale_tags ) {
891 die "config: Use of empty LANGUAGE.locale property is not permitted. Remove the LANGUAGE.locale entry or specify LANGUAGE.locale = en_US instead.";
892 }
893  
894 my %locales;
895  
896 for my $locale_tag ( @locale_tags ) {
897 $locale_tag = untaint_locale_tag( $locale_tag ) #
898 // die "Illegal locale tag in LANGUAGE.locale: $locale_tag\n";
899  
900 my $lang_code = $locale_tag =~ s/_..$//r;
901  
902 if ( exists $locales{$lang_code} ) {
903 die "Repeated language code in LANGUAGE.locale: $lang_code\n";
904 }
905  
906 $locales{$lang_code} = $locale_tag;
907 }
908  
909 $self->{_LANGUAGE_locale} = \%locales;
910  
911 return;
912 }
913  
914 sub _add_public_profile {
915 my ( $self, $name, $path ) = @_;
916  
917 $name = untaint_profile_name( $name ) #
918 // die "Invalid profile name in PUBLIC PROFILES section: $name\n";
919  
920 $name = lc $name;
921  
922 if ( defined $self->{_public_profiles}{$name} || exists $self->{_private_profiles}{$name} ) {
923 die "Profile name not unique: $name\n";
924 }
925  
926 if ( defined $path ) {
927 $path = untaint_abs_path( $path ) #
928 // die "Path must be absolute for profile: $name\n";
929 }
930  
931 $self->{_public_profiles}{$name} = $path;
932 return;
933 }
934  
935 sub _add_private_profile {
936 my ( $self, $name, $path ) = @_;
937  
938 $name = untaint_profile_name( $name ) #
939 // die "Invalid profile name in PRIVATE PROFILES section: $name\n";
940  
941 $name = lc $name;
942  
943 if ( $name eq 'default' ) {
944 die "Profile name must not be present in PRIVATE PROFILES section: $name\n";
945 }
946  
947 if ( exists $self->{_public_profiles}{$name} || exists $self->{_private_profiles}{$name} ) {
948 die "Profile name not unique: $name\n";
949 }
950  
951 $path = untaint_abs_path( $path ) #
952 // die "Path must be absolute for profile: $name\n";
953  
954 $self->{_private_profiles}{$name} = $path;
955 return;
956 }
957  
958 sub _add_tld_url_override {
959 my ( $self, $tld, $value ) = @_;
960  
961 unless ( untaint_tld_label( $tld ) ) {
962 die "Invalid TLD label in TLD URL OVERRIDE section: $tld\n";
963 }
964  
965 if ( exists $self->{_tld_url_override}{$tld} ) {
966 die "TLD label not unique: $tld\n";
967 }
968  
969 unless ( untaint_tld_block( $value )
970 or untaint_tld_url_no_path( $value )
971 or untaint_tld_url_string( $value ) )
972 {
973 die "Invalid value for a TLD label key: $value\n";
974 }
975  
976 $self->{_tld_url_override}{$tld} = $value;
977 return;
978 }
979  
980 # Create a setter method with a given name using the given field and validator
981 sub _create_setter {
982 my ( $setter, $field, $validate ) = @_;
983  
984 $setter =~ /^_set_([A-Z_]*)_([a-z_]*)$/
985 or confess "Invalid setter name";
986 my $section = $1;
987 my $property = $2;
988  
989 my $setter_impl = sub {
990 my ( $self, $value ) = @_;
991  
992 $self->{$field} = $validate->( $value ) #
993 // die "Invalid value for $section.$property: $value\n";
994  
995 return;
996 };
997  
998 no strict 'refs';
999 *$setter = $setter_impl;
1000  
1001 return;
1002 }
1003  
1004 sub _normalize_engine_type {
1005 my ( $value ) = @_;
1006  
1007 # Normalized to camel case to match the database engine Perl module name, e.g. "SQLite.pm".
1008 state $db_module_names = {
1009 mysql => 'MySQL',
1010 postgresql => 'PostgreSQL',
1011 sqlite => 'SQLite',
1012 };
1013  
1014 return $db_module_names->{ lc $value };
1015 }
1016  
1017 1;
« auto/share/dist/Zonemaster-Backend/patch/patch_postgresql_db_zonemaster_backend_ver_5.0.0.pl ↑ Summary Zonemaster/Backend/Validator.pm »