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 1 ▶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://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md>.
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  
186 my $ini = Config::IniFiles->new( -file => \$text )
187 or die "Failed to parse config: " . join( '; ', @Config::IniFiles::errors ) . "\n";
188  
189 my $get_and_clear = sub { # Read and clear a property from a Config::IniFiles object.
190 my ( $section, $param ) = @_;
191 my ( $value, @extra ) = $ini->val( $section, $param );
192 if ( @extra ) {
193 die "Property not unique: $section.$param\n";
194 }
195 $ini->delval( $section, $param );
196 return $value;
197 };
198  
199 # Validate section names
200 {
201 my %sections = map { $_ => 1 } ( 'DB', 'MYSQL', 'POSTGRESQL', 'SQLITE', 'LANGUAGE', 'PUBLIC PROFILES', 'PRIVATE PROFILES', 'ZONEMASTER', 'METRICS', 'RPCAPI' );
202 for my $section ( $ini->Sections ) {
203 if ( !exists $sections{$section} ) {
204 die "config: unrecognized section: $section\n";
205 }
206 }
207 }
208  
209 # Assign default values
210 $obj->_set_DB_polling_interval( '0.5' );
211 $obj->_set_MYSQL_port( '3306' );
212 $obj->_set_POSTGRESQL_port( '5432' );
213 $obj->_set_ZONEMASTER_max_zonemaster_execution_time( '600' );
214 $obj->_set_ZONEMASTER_number_of_processes_for_frontend_testing( '20' );
215 $obj->_set_ZONEMASTER_number_of_processes_for_batch_testing( '20' );
216 $obj->_set_ZONEMASTER_lock_on_queue( '0' );
217 $obj->_set_ZONEMASTER_age_reuse_previous_test( '600' );
218 $obj->_set_RPCAPI_enable_user_create( 'no' ); # experimental
219 $obj->_set_RPCAPI_enable_batch_create( 'yes' ); # experimental
220 $obj->_set_RPCAPI_enable_add_api_user( 'no' );
221 $obj->_set_RPCAPI_enable_add_batch_job( 'yes' );
222 $obj->_set_locales( 'en_US' );
223 $obj->_add_public_profile( 'default', undef );
224 $obj->_set_METRICS_statsd_port( '8125' );
225  
226 # Assign property values (part 1/2)
227 if ( defined( my $value = $get_and_clear->( 'DB', 'engine' ) ) ) {
228 $obj->_set_DB_engine( $value );
229 }
230  
231 # Check required properties (part 1/2)
232 if ( !defined $obj->DB_engine ) {
233 die "config: missing required property DB.engine\n";
234 }
235  
236 # Check deprecated properties and assign fallback values
237 my @warnings;
238 #currently no deprecation warnings
239  
240 # Assign property values (part 2/2)
241 if ( defined( my $value = $get_and_clear->( 'DB', 'polling_interval' ) ) ) {
242 $obj->_set_DB_polling_interval( $value );
243 }
244 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'host' ) ) ) {
245 $obj->_set_MYSQL_host( $value );
246 }
247 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'port' ) ) ) {
248 if ( $obj->MYSQL_host eq 'localhost' ) {
249 push @warnings, "MYSQL.port is disregarded if MYSQL.host is set to 'localhost'";
250 }
251 $obj->{_MYSQL_port} = $value;
252 }
253 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'user' ) ) ) {
254 $obj->_set_MYSQL_user( $value );
255 }
256 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'password' ) ) ) {
257 $obj->_set_MYSQL_password( $value );
258 }
259 if ( defined( my $value = $get_and_clear->( 'MYSQL', 'database' ) ) ) {
260 $obj->_set_MYSQL_database( $value );
261 }
262 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'host' ) ) ) {
263 $obj->_set_POSTGRESQL_host( $value );
264 }
265 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'port' ) ) ) {
266 $obj->{_POSTGRESQL_port} = $value;
267 }
268 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'user' ) ) ) {
269 $obj->_set_POSTGRESQL_user( $value );
270 }
271 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'password' ) ) ) {
272 $obj->_set_POSTGRESQL_password( $value );
273 }
274 if ( defined( my $value = $get_and_clear->( 'POSTGRESQL', 'database' ) ) ) {
275 $obj->_set_POSTGRESQL_database( $value );
276 }
277 if ( defined( my $value = $get_and_clear->( 'SQLITE', 'database_file' ) ) ) {
278 $obj->_set_SQLITE_database_file( $value );
279 }
280 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'max_zonemaster_execution_time' ) ) ) {
281 $obj->_set_ZONEMASTER_max_zonemaster_execution_time( $value );
282 }
283 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'number_of_processes_for_frontend_testing' ) ) ) {
284 $obj->_set_ZONEMASTER_number_of_processes_for_frontend_testing( $value );
285 }
286 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'number_of_processes_for_batch_testing' ) ) ) {
287 $obj->_set_ZONEMASTER_number_of_processes_for_batch_testing( $value );
288 }
289 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'lock_on_queue' ) ) ) {
290 $obj->_set_ZONEMASTER_lock_on_queue( $value );
291 }
292 if ( defined( my $value = $get_and_clear->( 'ZONEMASTER', 'age_reuse_previous_test' ) ) ) {
293 $obj->_set_ZONEMASTER_age_reuse_previous_test( $value );
294 }
295 if ( defined( my $value = $get_and_clear->( 'METRICS', 'statsd_host' ) ) ) {
296 $obj->_set_METRICS_statsd_host( $value );
297 }
298 if ( defined( my $value = $get_and_clear->( 'METRICS', 'statsd_port' ) ) ) {
299 $obj->_set_METRICS_statsd_port( $value );
300 }
301 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_user_create' ) ) ) {
302 if ( defined( $get_and_clear->( 'RPCAPI', 'enable_add_api_user' ) ) ) {
303 die "Error: cannot specify both RPCAPI.enable_add_api_user and RPCAPI.enable_user_create\n";
304 }
305 $obj->_set_RPCAPI_enable_add_api_user( $value );
306 $obj->_set_RPCAPI_enable_user_create( $value );
307 } else {
308 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_add_api_user' ) ) ) {
309 $obj->_set_RPCAPI_enable_add_api_user( $value );
310 $obj->_set_RPCAPI_enable_user_create( $value );
311 }
312 }
313 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_batch_create' ) ) ) {
314 if ( defined( $get_and_clear->( 'RPCAPI', 'enable_add_batch_job' ) ) ) {
315 die "Error: cannot specify both RPCAPI.enable_add_batch_job and RPCAPI.enable_batch_create\n";
316 }
317 $obj->_set_RPCAPI_enable_add_batch_job( $value );
318 $obj->_set_RPCAPI_enable_batch_create( $value );
319 } else {
320 if ( defined( my $value = $get_and_clear->( 'RPCAPI', 'enable_add_batch_job' ) ) ) {
321 $obj->_set_RPCAPI_enable_add_batch_job( $value );
322 $obj->_set_RPCAPI_enable_batch_create( $value );
323 }
324 }
325 if ( defined( my $value = $get_and_clear->( 'LANGUAGE', 'locale' ) ) ) {
326 $obj->_set_locales( $value );
327 }
328  
329 for my $name ( $ini->Parameters( 'PUBLIC PROFILES' ) ) {
330 my $path = $get_and_clear->( 'PUBLIC PROFILES', $name );
331 $obj->_add_public_profile( $name, $path );
332 }
333  
334 for my $name ( $ini->Parameters( 'PRIVATE PROFILES' ) ) {
335 my $path = $get_and_clear->( 'PRIVATE PROFILES', $name );
336 $obj->_add_private_profile( $name, $path );
337 }
338  
339 # Check required propertys (part 2/2)
340 if ( $obj->DB_engine eq 'MySQL' ) {
341 die "config: missing required property MYSQL.host (required when DB.engine = MySQL)\n"
342 if !defined $obj->MYSQL_host;
343  
344 die "config: missing required property MYSQL.user (required when DB.engine = MySQL)\n"
345 if !defined $obj->MYSQL_user;
346  
347 die "config: missing required property MYSQL.password (required when DB.engine = MySQL)\n"
348 if !defined $obj->MYSQL_password;
349  
350 die "config: missing required property MYSQL.database (required when DB.engine = MySQL)\n"
351 if !defined $obj->MYSQL_database;
352 }
353 elsif ( $obj->DB_engine eq 'PostgreSQL' ) {
354 die "config: missing required property POSTGRESQL.host (required when DB.engine = PostgreSQL)\n"
355 if !defined $obj->POSTGRESQL_host;
356  
357 die "config: missing required property POSTGRESQL.user (required when DB.engine = PostgreSQL)\n"
358 if !defined $obj->POSTGRESQL_user;
359  
360 die "config: missing required property POSTGRESQL.password (required when DB.engine = PostgreSQL)\n"
361 if !defined $obj->POSTGRESQL_password;
362  
363 die "config: missing required property POSTGRESQL.database (required when DB.engine = PostgreSQL)\n"
364 if !defined $obj->POSTGRESQL_database;
365 }
366 elsif ( $obj->DB_engine eq 'SQLite' ) {
367 die "config: missing required property SQLITE.database_file (required when DB.engine = SQLite)\n"
368 if !defined $obj->SQLITE_database_file;
369 }
370  
371 # Check unknown property names
372 {
373 my @unrecognized;
374 for my $section ( $ini->Sections ) {
375 for my $param ( $ini->Parameters( $section ) ) {
376 push @unrecognized, "$section.$param";
377 }
378 }
379 if ( @unrecognized ) {
380 die "config: unrecognized property(s): " . join( ", ", sort @unrecognized ) . "\n";
381 }
382 }
383  
384 # Emit deprecation warnings
385 for my $message ( @warnings ) {
386 $log->warning( $message );
387 }
388  
389 return $obj;
390 }
391  
392 =head1 METHODS
393  
394 =head2 check_db
395  
396 Returns a normalized string based on the supported databases.
397  
398 =head3 EXCEPTION
399  
400 Dies if the value is not one of SQLite, PostgreSQL or MySQL.
401  
402 =cut
403  
404 sub check_db {
405 my ( $self, $db ) = @_;
406  
407 $db = untaint_engine_type( $db ) #
408 // die "Unknown database '$db', should be one of SQLite, MySQL or PostgreSQL\n";
409  
410 return _normalize_engine_type( $db );
411 }
412  
413  
414 =head2 DB_engine
415  
416 Get the value of L<DB.engine|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#engine>.
417  
418 Returns one of C<"SQLite">, C<"PostgreSQL"> or C<"MySQL">.
419  
420 =cut
421  
422 sub DB_engine {
423 my ( $self ) = @_;
424 return $self->{_DB_engine};
425 }
426  
427 sub _set_DB_engine {
428 my ( $self, $value ) = @_;
429  
430 $value = untaint_engine_type( $value ) #
431 // die "Invalid value for DB.engine: $value\n";
432  
433 $self->{_DB_engine} = _normalize_engine_type( $value );
434 return;
435 }
436  
437 =head2 DB_polling_interval
438  
439 Get the value of L<DB.polling_interval|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#polling_interval>.
440  
441 Returns a number.
442  
443  
444 =head2 MYSQL_database
445  
446 Get the value of L<MYSQL.database|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#database>.
447  
448 Returns a string.
449  
450  
451 =head2 MYSQL_host
452  
453 Get the value of L<MYSQL.host|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#host>.
454  
455 Returns a string.
456  
457  
458 =head2 MYSQL_port
459  
460 Returns the L<MYSQL.port|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#port>
461 property from the loaded config.
462  
463 Returns a number.
464  
465  
466 =head2 MYSQL_password
467  
468 Get the value of L<MYSQL.password|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#password>.
469  
470 Returns a string.
471  
472  
473 =head2 MYSQL_user
474  
475 Get the value of L<MYSQL.user|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#user>.
476  
477 Returns a string.
478  
479  
480 =head2 POSTGRESQL_database
481  
482 Get the value of L<POSTGRESQL.database|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#database-1>.
483  
484 Returns a string.
485  
486  
487 =head2 POSTGRESQL_host
488  
489 Get the value of L<POSTGRESQL.host|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#host-1>.
490  
491 Returns a string.
492  
493  
494 =head2 POSTGRESQL_port
495  
496 Returns the L<POSTGRESQL.port|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#port-1>
497 property from the loaded config.
498  
499 Returns a number.
500  
501  
502 =head2 POSTGRESQL_password
503  
504 Get the value of L<POSTGRESQL.password|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#password-1>.
505  
506 Returns a string.
507  
508  
509 =head2 POSTGRESQL_user
510  
511 Get the value of L<POSTGRESQL.user|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#user-1>.
512  
513 Returns a string.
514  
515  
516 =head2 SQLITE_database_file
517  
518 Get the value of L<SQLITE.database_file|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#database_file>.
519  
520 Returns a string.
521  
522  
523 =head2 LANGUAGE_locale
524  
525 Get the value of L<LANGUAGE.locale|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#locale>.
526  
527 Returns a mapping from two-letter locale tag prefixes to full locale tags.
528 This is represented by a hash mapping prefix to full locale tag.
529  
530 E.g.:
531  
532 (
533 en => "en_US",
534 sv => "sv_SE",
535 )
536  
537  
538 =head2 PUBLIC_PROFILES
539  
540 Get the set of L<PUBLIC PROFILES|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#public-profiles-and-private-profiles-sections>.
541  
542 Returns a hash mapping profile names to profile paths.
543 The profile names are normalized to lowercase.
544 Profile paths are either strings or C<undef>.
545 C<undef> means that the Zonemaster Engine default profile should be used.
546  
547  
548 =head2 PRIVATE_PROFILES
549  
550 Get the set of L<PRIVATE PROFILES|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#public-profiles-and-private-profiles-sections>.
551  
552 Returns a hash mapping profile names to profile paths.
553 The profile names are normalized to lowercase.
554 Profile paths are always strings (contrast with L<PUBLIC_PROFILES>).
555  
556  
557 =head2 ZONEMASTER_max_zonemaster_execution_time
558  
559 Get the value of L<ZONEMASTER.max_zonemaster_execution_time|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#max_zonemaster_execution_time>.
560  
561 Returns a number.
562  
563  
564 =head2 ZONEMASTER_number_of_processes_for_frontend_testing
565  
566 Get the value of
567 L<ZONEMASTER.number_of_processes_for_frontend_testing|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#number_of_processes_for_frontend_testing>.
568  
569 Returns a number.
570  
571  
572 =head2 ZONEMASTER_number_of_processes_for_batch_testing
573  
574 Get the value of
575 L<ZONEMASTER.number_of_processes_for_batch_testing|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#number_of_processes_for_batch_testing>.
576  
577 Returns a number.
578  
579  
580 =head2 ZONEMASTER_lock_on_queue
581  
582 Get the value of
583 L<ZONEMASTER.lock_on_queue|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#lock_on_queue>.
584  
585 Returns a number.
586  
587  
588 =head2 ZONEMASTER_age_reuse_previous_test
589  
590 Get the value of
591 L<ZONEMASTER.age_reuse_previous_test|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#age_reuse_previous_test>.
592  
593 Returns a number.
594  
595  
596 =head2 METRICS_statsd_host
597  
598 Get the value of
599 L<METRICS.statsd_host|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#statsd_host>.
600  
601 Returns a string.
602  
603  
604 =head2 METRICS_statsd_port
605  
606 Get the value of
607 L<METRICS.statsd_host|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#statsd_port>.
608  
609 Returns a number.
610  
611  
612 =head2 RPCAPI_enable_user_create
613  
614 Experimental.
615 Get the value of
616 L<RPCAPI.enable_user_create|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#enable_user_create>.
617  
618 Return 0 or 1
619  
620  
621 =head2 RPCAPI_enable_batch_create
622  
623 Experimental.
624 Get the value of
625 L<RPCAPI.enable_batch_create|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#enable_batch_create>.
626  
627 Return 0 or 1
628  
629  
630 =head2 RPCAPI_enable_add_api_user
631  
632 Get the value of
633 L<RPCAPI.enable_add_api_user|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#enable_add_api_user>.
634  
635 Return 0 or 1
636  
637  
638 =head2 RPCAPI_enable_add_batch_job
639  
640 Get the value of
641 L<RPCAPI.enable_add_batch_job|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/backend.md#enable_add_batch_job>.
642  
643 Return 0 or 1
644  
645 =cut
646  
647 # Getters for the properties documented above
648 sub DB_polling_interval { return $_[0]->{_DB_polling_interval}; }
649 sub MYSQL_host { return $_[0]->{_MYSQL_host}; }
650 sub MYSQL_port { return $_[0]->{_MYSQL_port}; }
651 sub MYSQL_user { return $_[0]->{_MYSQL_user}; }
652 sub MYSQL_password { return $_[0]->{_MYSQL_password}; }
653 sub MYSQL_database { return $_[0]->{_MYSQL_database}; }
654 sub POSTGRESQL_host { return $_[0]->{_POSTGRESQL_host}; }
655 sub POSTGRESQL_port { return $_[0]->{_POSTGRESQL_port}; }
656 sub POSTGRESQL_user { return $_[0]->{_POSTGRESQL_user}; }
657 sub POSTGRESQL_password { return $_[0]->{_POSTGRESQL_password}; }
658 sub POSTGRESQL_database { return $_[0]->{_POSTGRESQL_database}; }
659 sub SQLITE_database_file { return $_[0]->{_SQLITE_database_file}; }
660 sub LANGUAGE_locale { return %{ $_[0]->{_LANGUAGE_locale} }; }
661 sub PUBLIC_PROFILES { return %{ $_[0]->{_public_profiles} }; }
662 sub PRIVATE_PROFILES { return %{ $_[0]->{_private_profiles} }; }
663 sub ZONEMASTER_max_zonemaster_execution_time { return $_[0]->{_ZONEMASTER_max_zonemaster_execution_time}; }
664 sub ZONEMASTER_lock_on_queue { return $_[0]->{_ZONEMASTER_lock_on_queue}; }
665 sub ZONEMASTER_number_of_processes_for_frontend_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_frontend_testing}; }
666 sub ZONEMASTER_number_of_processes_for_batch_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_batch_testing}; }
667 sub ZONEMASTER_age_reuse_previous_test { return $_[0]->{_ZONEMASTER_age_reuse_previous_test}; }
668 sub METRICS_statsd_host { return $_[0]->{_METRICS_statsd_host}; }
669 sub METRICS_statsd_port { return $_[0]->{_METRICS_statsd_port}; }
670 sub RPCAPI_enable_user_create { return $_[0]->{_RPCAPI_enable_user_create}; } # experimental
671 sub RPCAPI_enable_batch_create { return $_[0]->{_RPCAPI_enable_batch_create}; } # experimental
672 sub RPCAPI_enable_add_api_user { return $_[0]->{_RPCAPI_enable_add_api_user}; }
673 sub RPCAPI_enable_add_batch_job { return $_[0]->{_RPCAPI_enable_add_batch_job}; }
674  
675 # Compile time generation of setters for the properties documented above
676 UNITCHECK {
677 _create_setter( '_set_DB_polling_interval', '_DB_polling_interval', \&untaint_strictly_positive_millis );
678 _create_setter( '_set_MYSQL_host', '_MYSQL_host', \&untaint_host );
679 _create_setter( '_set_MYSQL_port', '_MYSQL_port', \&untaint_strictly_positive_int );
680 _create_setter( '_set_MYSQL_user', '_MYSQL_user', \&untaint_mariadb_user );
681 _create_setter( '_set_MYSQL_password', '_MYSQL_password', \&untaint_password );
682 _create_setter( '_set_MYSQL_database', '_MYSQL_database', \&untaint_mariadb_database );
683 _create_setter( '_set_POSTGRESQL_host', '_POSTGRESQL_host', \&untaint_host );
684 _create_setter( '_set_POSTGRESQL_port', '_POSTGRESQL_port', \&untaint_strictly_positive_int );
685 _create_setter( '_set_POSTGRESQL_user', '_POSTGRESQL_user', \&untaint_postgresql_ident );
686 _create_setter( '_set_POSTGRESQL_password', '_POSTGRESQL_password', \&untaint_password );
687 _create_setter( '_set_POSTGRESQL_database', '_POSTGRESQL_database', \&untaint_postgresql_ident );
688 _create_setter( '_set_SQLITE_database_file', '_SQLITE_database_file', \&untaint_abs_path );
689 _create_setter( '_set_ZONEMASTER_max_zonemaster_execution_time', '_ZONEMASTER_max_zonemaster_execution_time', \&untaint_strictly_positive_int );
690 _create_setter( '_set_ZONEMASTER_lock_on_queue', '_ZONEMASTER_lock_on_queue', \&untaint_non_negative_int );
691 _create_setter( '_set_ZONEMASTER_number_of_processes_for_frontend_testing', '_ZONEMASTER_number_of_processes_for_frontend_testing', \&untaint_strictly_positive_int );
692 _create_setter( '_set_ZONEMASTER_number_of_processes_for_batch_testing', '_ZONEMASTER_number_of_processes_for_batch_testing', \&untaint_non_negative_int );
693 _create_setter( '_set_ZONEMASTER_age_reuse_previous_test', '_ZONEMASTER_age_reuse_previous_test', \&untaint_strictly_positive_int );
694 _create_setter( '_set_METRICS_statsd_host', '_METRICS_statsd_host', \&untaint_host );
695 _create_setter( '_set_METRICS_statsd_port', '_METRICS_statsd_port', \&untaint_strictly_positive_int );
696 _create_setter( '_set_RPCAPI_enable_user_create', '_RPCAPI_enable_user_create', \&untaint_bool ); # experimental
697 _create_setter( '_set_RPCAPI_enable_batch_create', '_RPCAPI_enable_batch_create', \&untaint_bool ); # experimental
698 _create_setter( '_set_RPCAPI_enable_add_api_user', '_RPCAPI_enable_add_api_user', \&untaint_bool );
699 _create_setter( '_set_RPCAPI_enable_add_batch_job', '_RPCAPI_enable_add_batch_job', \&untaint_bool );
700 }
701  
702 =head2 new_DB
703  
704 Create a new database adapter object according to configuration.
705  
706 The adapter connects to the database before it is returned.
707  
708 =head3 INPUT
709  
710 The database adapter class is selected based on the return value of
711 L<DB_engine>.
712 The database adapter class constructor is called without arguments and is
713 expected to configure itself according to available global configuration.
714  
715 =head3 RETURNS
716  
717 A configured L<Zonemaster::Backend::DB> object.
718  
719 =head3 EXCEPTIONS
720  
721 =over 4
722  
723 =item Dies if no adapter for the configured database engine can be loaded.
724  
725 =item Dies if the adapter is unable to connect to the database.
726  
727 =back
728  
729 =cut
730  
731 sub new_DB {
732 my ( $self ) = @_;
733  
734 my $dbtype = $self->DB_engine;
735 my $dbclass = Zonemaster::Backend::DB->get_db_class( $dbtype );
736 my $db = $dbclass->from_config( $self );
737  
738 return $db;
739 }
740  
741 =head2 new_PM
742  
743 Create a new processing manager object according to configuration.
744  
745 =head3 INPUT
746  
747 The values of the following attributes affect the construction of the returned object:
748  
749 =over
750  
751 =item ZONEMASTER_max_zonemaster_execution_time
752  
753 =item ZONEMASTER_number_of_processes_for_batch_testing
754  
755 =item ZONEMASTER_number_of_processes_for_frontend_testing
756  
757 =back
758  
759 =head3 RETURNS
760  
761 A configured L<Parallel::ForkManager> object.
762  
763 =cut
764  
765 sub new_PM {
766 my $self = shift;
767  
768 my $maximum_processes = $self->ZONEMASTER_number_of_processes_for_frontend_testing + $self->ZONEMASTER_number_of_processes_for_batch_testing;
769  
770 my $timeout = $self->ZONEMASTER_max_zonemaster_execution_time;
771  
772 my %times;
773  
774 my $pm = Parallel::ForkManager->new( $maximum_processes );
775 $pm->set_waitpid_blocking_sleep( 0 ) if $pm->can( 'set_waitpid_blocking_sleep' );
776  
777 $pm->run_on_wait(
778 sub {
779 foreach my $pid ( $pm->running_procs ) {
780 my $diff = time() - $times{$pid}[0];
781 my $id = $times{$pid}[1];
782  
783 if ( $diff > $timeout ) {
784 $log->warning( "Worker process (pid $pid, testid $id): Timeout, sending SIGKILL" );
785 kill 9, $pid;
786 }
787 }
788 },
789 1
790 );
791  
792 $pm->run_on_start(
793 sub {
794 my ( $pid, $id ) = @_;
795  
796 $times{$pid} = [ time(), $id ];
797 }
798 );
799  
800 $pm->run_on_finish(
801 sub {
802 my ( $pid, $exitcode, $id, $signal ) = @_;
803  
804 delete $times{$pid};
805  
806 my $message =
807 ( $signal )
808 ? "Terminated by signal $signal (SIG$SIG_NAME[$signal])"
809 : "Terminated with exit code $exitcode";
810  
811 $log->notice( "Worker process (pid $pid, testid $id): $message" );
812 }
813 );
814  
815 return $pm;
816 }
817  
818 sub _set_locales {
819 my ( $self, $value ) = @_;
820  
821 my @locale_tags = split / +/, $value;
822  
823 if ( !@locale_tags ) {
824 die "config: Use of empty LANGUAGE.locale property is not permitted. Remove the LANGUAGE.locale entry or specify LANGUAGE.locale = en_US instead.";
825 }
826  
827 my %locales;
828  
829 for my $locale_tag ( @locale_tags ) {
830 $locale_tag = untaint_locale_tag( $locale_tag ) #
831 // die "Illegal locale tag in LANGUAGE.locale: $locale_tag\n";
832  
833 my $lang_code = $locale_tag =~ s/_..$//r;
834  
835 if ( exists $locales{$lang_code} ) {
836 die "Repeated language code in LANGUAGE.locale: $lang_code\n";
837 }
838  
839 $locales{$lang_code} = $locale_tag;
840 }
841  
842 $self->{_LANGUAGE_locale} = \%locales;
843  
844 return;
845 }
846  
847 sub _add_public_profile {
848 my ( $self, $name, $path ) = @_;
849  
850 $name = untaint_profile_name( $name ) #
851 // die "Invalid profile name in PUBLIC PROFILES section: $name\n";
852  
853 $name = lc $name;
854  
855 if ( defined $self->{_public_profiles}{$name} || exists $self->{_private_profiles}{$name} ) {
856 die "Profile name not unique: $name\n";
857 }
858  
859 if ( defined $path ) {
860 $path = untaint_abs_path( $path ) #
861 // die "Path must be absolute for profile: $name\n";
862 }
863  
864 $self->{_public_profiles}{$name} = $path;
865 return;
866 }
867  
868 sub _add_private_profile {
869 my ( $self, $name, $path ) = @_;
870  
871 $name = untaint_profile_name( $name ) #
872 // die "Invalid profile name in PRIVATE PROFILES section: $name\n";
873  
874 $name = lc $name;
875  
876 if ( $name eq 'default' ) {
877 die "Profile name must not be present in PRIVATE PROFILES section: $name\n";
878 }
879  
880 if ( exists $self->{_public_profiles}{$name} || exists $self->{_private_profiles}{$name} ) {
881 die "Profile name not unique: $name\n";
882 }
883  
884 $path = untaint_abs_path( $path ) #
885 // die "Path must be absolute for profile: $name\n";
886  
887 $self->{_private_profiles}{$name} = $path;
888 return;
889 }
890  
891 # Create a setter method with a given name using the given field and validator
892 sub _create_setter {
893 my ( $setter, $field, $validate ) = @_;
894  
895 $setter =~ /^_set_([A-Z_]*)_([a-z_]*)$/
896 or confess "Invalid setter name";
897 my $section = $1;
898 my $property = $2;
899  
900 my $setter_impl = sub {
901 my ( $self, $value ) = @_;
902  
903 $self->{$field} = $validate->( $value ) #
904 // die "Invalid value for $section.$property: $value\n";
905  
906 return;
907 };
908  
909 no strict 'refs';
910 *$setter = $setter_impl;
911  
912 return;
913 }
914  
915 sub _normalize_engine_type {
916 my ( $value ) = @_;
917  
918 # Normalized to camel case to match the database engine Perl module name, e.g. "SQLite.pm".
919 state $db_module_names = {
920 mysql => 'MySQL',
921 postgresql => 'PostgreSQL',
922 sqlite => 'SQLite',
923 };
924  
925 return $db_module_names->{ lc $value };
926 }
927  
928 1;
« auto/share/dist/Zonemaster-Backend/patch/patch_postgresql_db_zonemaster_backend_ver_5.0.0.pl ↑ Summary Zonemaster/Backend/Validator.pm »