-
Notifications
You must be signed in to change notification settings - Fork 22
/
Config.pm
573 lines (390 loc) · 18.9 KB
/
Config.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package Zonemaster::Backend::Config;
use strict;
use warnings;
use 5.14.2;
our $VERSION = '1.1.0';
use Config::IniFiles;
use Config;
use File::ShareDir qw[dist_file];
use File::Slurp qw( read_file );
use Log::Any qw( $log );
use Readonly;
our $path;
if ($ENV{ZONEMASTER_BACKEND_CONFIG_FILE}) {
$path = $ENV{ZONEMASTER_BACKEND_CONFIG_FILE};
}
elsif ( -e '/etc/zonemaster/backend_config.ini' ) {
$path = '/etc/zonemaster/backend_config.ini';
}
else {
$path = dist_file('Zonemaster-Backend', "backend_config.ini");
}
Readonly my @SIG_NAME => split ' ', $Config{sig_name};
=head1 CONSTRUCTORS
=head2 load_config
A wrapper around L<parse> that also determines where the config file is located
in the file system and reads it.
Throws an exception if the determined configuration file cannot be read.
See L<parse> for details on additional parsing-related error modes.
=cut
sub load_config {
my ( $class ) = @_;
$log->notice( "Loading config: $path" );
my $text = read_file $path;
my $obj = eval { $class->parse( $text ) };
if ( $@ ) {
die "File $path: $@";
}
return $obj;
}
=head2 parse
Construct a new Zonemaster::Backend::Config based on a given configuration.
my $config = Zonemaster::Backend::Config->parse(
q{
[DB]
engine = SQLite
[SQLITE]
database_file = /var/db/zonemaster.sqlite
}
);
The configuration is interpreted according to the
L<configuration format specification|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md>.
Returns a new Zonemaster::Backend::Config instance with its properties set to
values according to the given configuration with defaults according to the
configuration format.
Emits a log warning with a deprecation message for each deprecated property that
is present.
Throws an exception if the given configuration contains errors.
Unrecognized sections and properties are silently ignored.
=cut
sub parse {
my ( $class, $text ) = @_;
my $obj = bless( {}, $class );
my $ini = Config::IniFiles->new( -file => \$text )
or die "Failed to parse config: " . join( '; ', @Config::IniFiles::errors ) . "\n";
# Validate, normalize, and apply default values
{
my $engine = $ini->val( 'DB', 'engine' );
eval {
$engine = $obj->check_db($engine);
};
if ($@) {
die "Unknown config value DB.engine: $engine\n";
}
$obj->{_DB_engine} = $engine;
}
$obj->{_DB_polling_interval} = $ini->val( 'DB', 'polling_interval', '0.5' );
$obj->{_MYSQL_host} = $ini->val( 'MYSQL', 'host', undef );
$obj->{_MYSQL_user} = $ini->val( 'MYSQL', 'user', undef );
$obj->{_MYSQL_password} = $ini->val( 'MYSQL', 'password', undef );
$obj->{_MYSQL_database} = $ini->val( 'MYSQL', 'database', undef );
$obj->{_POSTGRESQL_host} = $ini->val( 'POSTGRESQL', 'host', undef );
$obj->{_POSTGRESQL_user} = $ini->val( 'POSTGRESQL', 'user', undef );
$obj->{_POSTGRESQL_password} = $ini->val( 'POSTGRESQL', 'password', undef );
$obj->{_POSTGRESQL_database} = $ini->val( 'POSTGRESQL', 'database', undef );
$obj->{_SQLITE_database_file} = $ini->val( 'SQLITE', 'database_file', undef );
$obj->{_ZONEMASTER_max_zonemaster_execution_time} = $ini->val( 'ZONEMASTER', 'max_zonemaster_execution_time', '600' );
$obj->{_ZONEMASTER_maximal_number_of_retries} = $ini->val( 'ZONEMASTER', 'maximal_number_of_retries', '0' );
$obj->{_ZONEMASTER_number_of_processes_for_frontend_testing} = $ini->val( 'ZONEMASTER', 'number_of_processes_for_frontend_testing', '20' );
$obj->{_ZONEMASTER_number_of_processes_for_batch_testing} = $ini->val( 'ZONEMASTER', 'number_of_processes_for_batch_testing', '20' );
$obj->{_ZONEMASTER_lock_on_queue} = $ini->val( 'ZONEMASTER', 'lock_on_queue', '0' );
$obj->{_ZONEMASTER_age_reuse_previous_test} = $ini->val( 'ZONEMASTER', 'age_reuse_previous_test', '600' );
$obj->{_LANGUAGE_locale} = {};
for my $locale_tag ( split /\s+/, $ini->val( 'LANGUAGE', 'locale' ) || 'en_US' ) {
$locale_tag =~ /^[a-z]{2}_[A-Z]{2}$/
or die "Illegal locale tag in LANGUAGE.locale: $locale_tag\n";
!exists $obj->{_LANGUAGE_locale}{$locale_tag}
or die "Repeated locale tag in LANGUAGE.locale: $locale_tag\n";
$obj->{_LANGUAGE_locale}{$locale_tag} = 1;
}
$obj->{_public_profiles} = {
default => '',
};
for my $name ( $ini->Parameters( 'PUBLIC PROFILES' ) ) {
$obj->{_public_profiles}{lc $name} = $ini->val( 'PUBLIC PROFILES', $name );
}
$obj->{_private_profiles} = {};
for my $name ( $ini->Parameters( 'PRIVATE PROFILES' ) ) {
$obj->{_private_profiles}{lc $name} = $ini->val( 'PRIVATE PROFILES', $name );
}
# Handle deprecated properties
if ( defined( my $value = $ini->val( 'DB', 'database_host' ) ) ) {
$log->warning( "Use of deprecated config property DB.database_host. Use MYSQL.host or POSTGRESQL.host instead." );
$obj->{_MYSQL_host} = $value
if $obj->DB_engine eq 'MySQL' && !defined $obj->MYSQL_host;
$obj->{_POSTGRESQL_host} = $value
if $obj->DB_engine eq 'PostgreSQL' && !defined $obj->POSTGRESQL_host;
}
if ( defined( my $value = $ini->val( 'DB', 'user' ) ) ) {
$log->warning( "Use of deprecated config property DB.user. Use MYSQL.user or POSTGRESQL.user instead." );
$obj->{_MYSQL_user} = $value
if $obj->DB_engine eq 'MySQL' && !defined $obj->MYSQL_user;
$obj->{_POSTGRESQL_user} = $value
if $obj->DB_engine eq 'PostgreSQL' && !defined $obj->POSTGRESQL_user;
}
if ( defined( my $value = $ini->val( 'DB', 'password' ) ) ) {
$log->warning( "Use of deprecated config property DB.password. Use MYSQL.password or POSTGRESQL.password instead." );
$obj->{_MYSQL_password} = $value
if $obj->DB_engine eq 'MySQL' && !defined $obj->MYSQL_password;
$obj->{_POSTGRESQL_password} = $value
if $obj->DB_engine eq 'PostgreSQL' && !defined $obj->POSTGRESQL_password;
}
if ( defined( my $value = $ini->val( 'DB', 'database_name' ) ) ) {
$log->warning( "Use of deprecated config property DB.database_name. Use MYSQL.database, POSTGRESQL.database or SQLITE.database_file instead." );
$obj->{_MYSQL_database} = $value
if $obj->DB_engine eq 'MySQL' && !defined $obj->MYSQL_database;
$obj->{_POSTGRESQL_database} = $value
if $obj->DB_engine eq 'PostgreSQL' && !defined $obj->POSTGRESQL_database;
$obj->{_SQLITE_database_file} = $value
if $obj->DB_engine eq 'SQLite' && !defined $obj->SQLITE_database_file;
}
if ( defined( my $value = $ini->val( 'ZONEMASTER', 'number_of_professes_for_frontend_testing' ) ) ) {
$log->warning( "Use of deprecated config property ZONEMASTER.number_of_professes_for_frontend_testing. Use ZONEMASTER.number_of_processes_for_frontend_testing instead." );
$obj->{_ZONEMASTER_number_of_processes_for_frontend_testing} = $value
if !defined $obj->NumberOfProcessesForFrontendTesting;
}
if ( defined( my $value = $ini->val( 'ZONEMASTER', 'number_of_professes_for_batch_testing' ) ) ) {
$log->warning( "Use of deprecated config property ZONEMASTER.number_of_professes_for_batch_testing. Use ZONEMASTER.number_of_processes_for_batch_testing instead." );
$obj->{_ZONEMASTER_number_of_processes_for_batch_testing} = $value
if !defined $obj->NumberOfProcessesForBatchTesting;
}
return $obj;
}
=head1 METHODS
=cut
sub check_db {
my ( $self, $db ) = @_;
if ( lc $db eq 'sqlite' ) {
return 'SQLite';
}
elsif ( lc $db eq 'postgresql' ) {
return 'PostgreSQL';
}
elsif ( lc $db eq 'mysql' ) {
return 'MySQL';
}
else {
die "Unknown database '$db', should be one of SQLite, MySQL or PostgreSQL\n";
}
}
=head2 MYSQL_database
Get the value of L<MYSQL.database|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#database>.
=head2 MySQL_host
Get the value of L<MYSQL.host|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#host>.
=head2 MYSQL_password
Get the value of L<MYSQL.password|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#password-1>.
=head2 MYSQL_user
Get the value of L<MYSQL.user|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#user-1>.
=head2 POSTGRESQL_database
Get the value of L<POSTGRESQL.database|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#database-1>.
=head2 POSTGRESQL_host
Get the value of L<POSTGRESQL.host|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#host-1>.
=head2 POSTGRESQL_password
Get the value of L<POSTGRESQL.password|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#password-2>.
=head2 POSTGRESQL_user
Get the value of L<POSTGRESQL.user|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#user-2>.
=head2 SQLITE_database_file
Get the value of L<SQLITE.database_file|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#database_file>.
=cut
sub DB_engine { return $_[0]->{_DB_engine}; }
sub DB_polling_interval { return $_[0]->{_DB_polling_interval}; }
sub MYSQL_host { return $_[0]->{_MYSQL_host}; }
sub MYSQL_user { return $_[0]->{_MYSQL_user}; }
sub MYSQL_password { return $_[0]->{_MYSQL_password}; }
sub MYSQL_database { return $_[0]->{_MYSQL_database}; }
sub POSTGRESQL_host { return $_[0]->{_POSTGRESQL_host}; }
sub POSTGRESQL_user { return $_[0]->{_POSTGRESQL_user}; }
sub POSTGRESQL_password { return $_[0]->{_POSTGRESQL_password}; }
sub POSTGRESQL_database { return $_[0]->{_POSTGRESQL_database}; }
sub SQLITE_database_file { return $_[0]->{_SQLITE_database_file}; }
sub ZONEMASTER_max_zonemaster_execution_time { return $_[0]->{_ZONEMASTER_max_zonemaster_execution_time}; }
sub ZONEMASTER_maximal_number_of_retries { return $_[0]->{_ZONEMASTER_maximal_number_of_retries}; }
sub ZONEMASTER_lock_on_queue { return $_[0]->{_ZONEMASTER_lock_on_queue}; }
sub ZONEMASTER_number_of_processes_for_frontend_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_frontend_testing}; }
sub ZONEMASTER_number_of_processes_for_batch_testing { return $_[0]->{_ZONEMASTER_number_of_processes_for_batch_testing}; }
sub ZONEMASTER_age_reuse_previous_test { return $_[0]->{_ZONEMASTER_age_reuse_previous_test}; }
=head2 Language_Locale_hash
Read LANGUAGE.locale from the configuration (.ini) file and returns
the valid language tags for RPCAPI. The incoming language tag
from RPCAPI is compared to those. The language tags are mapped to
locale setting value.
=head3 INPUT
None
=head3 RETURNS
A hash of valid language tags as keys with set locale value as value.
The hash is never empty.
=cut
sub Language_Locale_hash {
# There is one special value to capture ambiguous (and therefore
# not permitted) translation language tags.
my ($self) = @_;
my @localetags = keys %{ $self->{_LANGUAGE_locale} };
my %locale;
foreach my $la (@localetags) {
(my $a) = split (/_/,$la); # $a is the language code only
my $lo = "$la.UTF-8";
# Set special value if the same language code is used more than once
# with different country codes.
if ( $locale{$a} and $locale{$a} ne $lo ) {
$locale{$a} = 'NOT-UNIQUE';
}
else {
$locale{$a} = $lo;
}
$locale{$la} = $lo;
}
return %locale;
}
=head2 ListLanguageTags
Read indirectly LANGUAGE.locale from the configuration (.ini) file
and returns a list of valid language tags for RPCAPI. The list can
be retrieved via an RPCAPI method.
=head3 INPUT
None
=head3 RETURNS
An array of valid language tags. The array is never empty.
=cut
sub ListLanguageTags {
my ($self) = @_;
my %locale = &Language_Locale_hash($self);
my @langtags;
foreach my $key (keys %locale) {
push @langtags, $key unless $locale{$key} eq 'NOT-UNIQUE';
}
return @langtags;
}
=head2 DB_engine
Get the value of L<DB.engine|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#polling_interval>.
Returns one of C<"SQLite">, C<"PostgreSQL"> or C<"MySQL">.
=head2 DB_polling_interval
Get the value of L<DB.polling_interval|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#polling_interval>.
=head2 ZONEMASTER_max_zonemaster_execution_time
Get the value of L<ZONEMASTER.max_zonemaster_execution_time|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#max_zonemaster_execution_time>.
Returns an integer (number of seconds).
=head2 ZONEMASTER_number_of_processes_for_frontend_testing
Get the value of
L<ZONEMASTER.number_of_processes_for_frontend_testing|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#number_of_processes_for_frontend_testing>.
Returns a positive integer.
=head2 ZONEMASTER_number_of_processes_for_batch_testing
Get the value of
L<ZONEMASTER.number_of_processes_for_batch_testing|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#number_of_processes_for_batch_testing>.
Returns an integer.
=head2 ZONEMASTER_lock_on_queue
Get the value of
L<ZONEMASTER.lock_on_queue|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#lock_on_queue>.
Returns an integer.
=head2 ZONEMASTER_maximal_number_of_retries
Get the value of
L<ZONEMASTER.maximal_number_of_retries|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#maximal_number_of_retries>.
Returns an integer.
=head2 ZONEMASTER_age_reuse_previous_test
Get the value of
L<ZONEMASTER.age_reuse_previous_test|https://github.com/zonemaster/zonemaster-backend/blob/master/docs/Configuration.md#age_reuse_previous_test>.
Returns an integer.
=cut
sub ReadProfilesInfo {
my ($self) = @_;
my $profiles;
foreach my $public_profile ( keys %{ $self->{_public_profiles} } ) {
$profiles->{$public_profile}->{type} = 'public';
$profiles->{$public_profile}->{profile_file_name} = $self->{_public_profiles}{$public_profile};
}
foreach my $private_profile ( keys %{ $self->{_private_profiles} } ) {
$profiles->{$private_profile}->{type} = 'private';
$profiles->{$private_profile}->{profile_file_name} = $self->{_private_profiles}{$private_profile};
}
return $profiles;
}
sub ListPublicProfiles {
my ($self) = @_;
return keys %{ $self->{_public_profiles} };
}
=head2 check_db
Returns a normalized string based on the supported databases.
=head3 EXCEPTION
Dies if the value is not one of SQLite, PostgreSQL or MySQL.
=head2 new_DB
Create a new database adapter object according to configuration.
The adapter connects to the database before it is returned.
=head3 INPUT
The database adapter class is selected based on the return value of
L<DB_engine>.
The database adapter class constructor is called without arguments and is
expected to configure itself according to available global configuration.
=head3 RETURNS
A configured L<Zonemaster::Backend::DB> object.
=head3 EXCEPTIONS
=over 4
=item Dies if no database engine type is defined in the configuration.
=item Dies if no adapter for the configured database engine can be loaded.
=item Dies if the adapter is unable to connect to the database.
=back
=cut
sub new_DB {
my ($self) = @_;
# Get DB type from config
my $dbtype = $self->DB_engine;
if (!defined $dbtype) {
die "Unrecognized DB.engine in backend config";
}
# Load and construct DB adapter
my $dbclass = 'Zonemaster::Backend::DB::' . $dbtype;
require( join( "/", split( /::/, $dbclass ) ) . ".pm" );
$dbclass->import();
my $db = $dbclass->new({ config => $self });
# Connect or die
$db->dbh;
return $db;
}
=head2 new_PM
Create a new processing manager object according to configuration.
=head3 INPUT
The values of the following attributes affect the construction of the returned object:
=over
=item ZONEMASTER_max_zonemaster_execution_time
=item ZONEMASTER_number_of_processes_for_batch_testing
=item ZONEMASTER_number_of_processes_for_frontend_testing
=back
=head3 RETURNS
A configured L<Parallel::ForkManager> object.
=cut
sub new_PM {
my $self = shift;
my $maximum_processes = $self->ZONEMASTER_number_of_processes_for_frontend_testing + $self->ZONEMASTER_number_of_processes_for_batch_testing;
my $timeout = $self->ZONEMASTER_max_zonemaster_execution_time;
my %times;
my $pm = Parallel::ForkManager->new( $maximum_processes );
$pm->set_waitpid_blocking_sleep( 0 ) if $pm->can( 'set_waitpid_blocking_sleep' );
$pm->run_on_wait(
sub {
foreach my $pid ( $pm->running_procs ) {
my $diff = time() - $times{$pid}[0];
my $id = $times{$pid}[1];
if ( $diff > $timeout ) {
$log->warning( "Worker process (pid $pid, testid $id): Timeout, sending SIGKILL" );
kill 9, $pid;
}
}
},
1
);
$pm->run_on_start(
sub {
my ( $pid, $id ) = @_;
$times{$pid} = [ time(), $id ];
}
);
$pm->run_on_finish(
sub {
my ( $pid, $exitcode, $id, $signal ) = @_;
delete $times{$pid};
my $message =
( $signal )
? "Terminated by signal $signal (SIG$SIG_NAME[$signal])"
: "Terminated with exit code $exitcode";
$log->notice( "Worker process (pid $pid, testid $id): $message" );
}
);
return $pm;
}
1;