« auto/share/dist/Zonemaster-Backend/patch/patch_postgresql_db_zonemaster_backend_ver_8.0.0.pl
↑ Summary
Zonemaster/Backend/DB/MySQL.pm »
| 1 | package Zonemaster::Backend::RPCAPI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.14.2; | ||
| 6 | |||
| 7 | # Public Modules | ||
| 8 | use DBI qw(:utils); | ||
| 9 | use Digest::MD5 qw(md5_hex); | ||
| 10 | use File::Slurp qw(append_file); | ||
| 11 | use HTML::Entities; | ||
| 12 | use JSON::PP; | ||
| 13 | use JSON::Validator::Joi; | ||
| 14 | use Log::Any qw($log); | ||
| 15 | use Mojo::JSON::Pointer; | ||
| 16 | use Scalar::Util qw(blessed); | ||
| 17 | use JSON::Validator::Schema::Draft7; | ||
| 18 | use Locale::TextDomain qw[Zonemaster-Backend]; | ||
| 19 | use Locale::Messages qw[LC_MESSAGES LC_ALL]; | ||
| 20 | use POSIX qw (setlocale); | ||
| 21 | use Encode; | ||
| 22 | |||
| 23 | # Zonemaster Modules | ||
| 24 | use Zonemaster::Engine; | ||
| 25 | use Zonemaster::Engine::Normalization qw( normalize_name trim_space ); | ||
| 26 | use Zonemaster::Engine::Profile; | ||
| 27 | use Zonemaster::Engine::Recursor; | ||
| 28 | use Zonemaster::Backend; | ||
| 29 | use Zonemaster::Backend::Config; | ||
| 30 | use Zonemaster::Backend::Translator; | ||
| 31 | use Zonemaster::Backend::Validator; | ||
| 32 | use Zonemaster::Backend::Errors; | ||
| 33 | |||
| 34 | my $zm_validator = Zonemaster::Backend::Validator->new; | ||
| 35 | our %json_schemas; | ||
| 36 | my $recursor = Zonemaster::Engine::Recursor->new; | ||
| 37 | |||
| 38 | sub joi { | ||
| 39 | return JSON::Validator::Joi->new; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ( $type, $params ) = @_; | ||
| 44 | |||
| 45 | my $self = {}; | ||
| 46 | bless( $self, $type ); | ||
| 47 | |||
| 48 | if ( ! $params || ! $params->{config} ) { | ||
| 49 | handle_exception("Missing 'config' parameter"); | ||
| 50 | } | ||
| 51 | |||
| 52 | $self->{config} = $params->{config}; | ||
| 53 | |||
| 54 | my $dbtype; | ||
| 55 | if ( $params->{dbtype} ) { | ||
| 56 | $dbtype = $self->{config}->check_db($params->{dbtype}); | ||
| 57 | } else { | ||
| 58 | $dbtype = $self->{config}->DB_engine; | ||
| 59 | } | ||
| 60 | |||
| 61 | $self->_init_db($dbtype); | ||
| 62 | |||
| 63 | $self->{_profiles} = Zonemaster::Backend::Config->load_profiles( # | ||
| 64 | $self->{config}->PUBLIC_PROFILES, | ||
| 65 | $self->{config}->PRIVATE_PROFILES, | ||
| 66 | ); | ||
| 67 | |||
| 68 | return ( $self ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _init_db { | ||
| 72 | my ( $self, $dbtype ) = @_; | ||
| 73 | |||
| 74 | eval { | ||
| 75 | my $dbclass = Zonemaster::Backend::DB->get_db_class( $dbtype ); | ||
| 76 | $self->{db} = $dbclass->from_config( $self->{config} ); | ||
| 77 | }; | ||
| 78 | |||
| 79 | if ($@) { | ||
| 80 | handle_exception("Failed to initialize the [$dbtype] database backend module: [$@]"); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub handle_exception { | ||
| 85 | my ( $exception ) = @_; | ||
| 86 | |||
| 87 | if ( !$exception->isa('Zonemaster::Backend::Error') ) { | ||
| 88 | my $reason = $exception; | ||
| 89 | $exception = Zonemaster::Backend::Error::Internal->new( reason => $reason ); | ||
| 90 | } | ||
| 91 | |||
| 92 | my $log_extra = $exception->as_hash; | ||
| 93 | delete $log_extra->{message}; | ||
| 94 | |||
| 95 | if ( $exception->isa('Zonemaster::Backend::Error::Internal') ) { | ||
| 96 | $log->error($exception->as_string, $log_extra); | ||
| 97 | } else { | ||
| 98 | $log->info($exception->as_string, $log_extra); | ||
| 99 | } | ||
| 100 | |||
| 101 | die $exception->as_hash; | ||
| 102 | } | ||
| 103 | |||
| 104 | $json_schemas{version_info} = joi->object->strict; | ||
| 105 | sub version_info { | ||
| 106 | my ( $self ) = @_; | ||
| 107 | |||
| 108 | my %ver; | ||
| 109 | eval { | ||
| 110 | $ver{zonemaster_ldns} = Zonemaster::LDNS->VERSION; | ||
| 111 | $ver{zonemaster_engine} = Zonemaster::Engine->VERSION; | ||
| 112 | $ver{zonemaster_backend} = Zonemaster::Backend->VERSION; | ||
| 113 | }; | ||
| 114 | if ($@) { | ||
| 115 | handle_exception( $@ ); | ||
| 116 | } | ||
| 117 | |||
| 118 | return \%ver; | ||
| 119 | } | ||
| 120 | |||
| 121 | # Experimental | ||
| 122 | $json_schemas{system_versions} = $json_schemas{version_info}; | ||
| 123 | sub system_versions { | ||
| 124 | return version_info( @_ ); | ||
| 125 | } | ||
| 126 | |||
| 127 | $json_schemas{profile_names} = joi->object->strict; | ||
| 128 | sub profile_names { | ||
| 129 | my ( $self ) = @_; | ||
| 130 | |||
| 131 | my %profiles; | ||
| 132 | eval { %profiles = $self->{config}->PUBLIC_PROFILES }; | ||
| 133 | if ( $@ ) { | ||
| 134 | handle_exception( $@ ); | ||
| 135 | } | ||
| 136 | |||
| 137 | return [ keys %profiles ]; | ||
| 138 | } | ||
| 139 | |||
| 140 | # Experimental | ||
| 141 | $json_schemas{conf_profiles} = $json_schemas{profile_names}; | ||
| 142 | sub conf_profiles { | ||
| 143 | my $result = { | ||
| 144 | profiles => profile_names( @_ ) | ||
| 145 | }; | ||
| 146 | return $result; | ||
| 147 | } | ||
| 148 | |||
| 149 | # Return the list of language tags supported by get_test_results(). The tags are | ||
| 150 | # derived from the locale tags set in the configuration file. | ||
| 151 | $json_schemas{get_language_tags} = joi->object->strict; | ||
| 152 | sub get_language_tags { | ||
| 153 | my ( $self ) = @_; | ||
| 154 | |||
| 155 | my @lang_tags; | ||
| 156 | eval { | ||
| 157 | my %locales = $self->{config}->LANGUAGE_locale; | ||
| 158 | |||
| 159 | @lang_tags = sort keys %locales; | ||
| 160 | }; | ||
| 161 | if ( $@ ) { | ||
| 162 | handle_exception( $@ ); | ||
| 163 | } | ||
| 164 | |||
| 165 | return \@lang_tags; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Experimental | ||
| 169 | $json_schemas{conf_languages} = $json_schemas{get_language_tags}; | ||
| 170 | sub conf_languages { | ||
| 171 | my $result = { | ||
| 172 | languages => get_language_tags( @_ ) | ||
| 173 | }; | ||
| 174 | return $result; | ||
| 175 | } | ||
| 176 | |||
| 177 | $json_schemas{get_host_by_name} = { | ||
| 178 | type => 'object', | ||
| 179 | additionalProperties => 0, | ||
| 180 | required => [ 'hostname' ], | ||
| 181 | properties => { | ||
| 182 | hostname => $zm_validator->domain_name | ||
| 183 | } | ||
| 184 | }; | ||
| 185 | sub get_host_by_name { | ||
| 186 | my ( $self, $params ) = @_; | ||
| 187 | my @adresses; | ||
| 188 | |||
| 189 | eval { | ||
| 190 | my $ns_name = $params->{hostname}; | ||
| 191 | |||
| 192 | @adresses = map { {$ns_name => $_->short} } $recursor->get_addresses_for($ns_name); | ||
| 193 | @adresses = { $ns_name => '0.0.0.0' } if not @adresses; | ||
| 194 | |||
| 195 | }; | ||
| 196 | if ($@) { | ||
| 197 | handle_exception( $@ ); | ||
| 198 | } | ||
| 199 | |||
| 200 | return \@adresses; | ||
| 201 | } | ||
| 202 | |||
| 203 | # Experimental | ||
| 204 | $json_schemas{lookup_address_records} = $json_schemas{get_host_by_name}; | ||
| 205 | sub lookup_address_records { | ||
| 206 | my $result = { | ||
| 207 | address_records => get_host_by_name( @_ ) | ||
| 208 | }; | ||
| 209 | return $result; | ||
| 210 | } | ||
| 211 | |||
| 212 | $json_schemas{get_data_from_parent_zone} = { | ||
| 213 | type => 'object', | ||
| 214 | additionalProperties => 0, | ||
| 215 | required => [ 'domain' ], | ||
| 216 | properties => { | ||
| 217 | domain => $zm_validator->domain_name, | ||
| 218 | language => $zm_validator->language_tag, | ||
| 219 | } | ||
| 220 | }; | ||
| 221 | sub get_data_from_parent_zone { | ||
| 222 | my ( $self, $params ) = @_; | ||
| 223 | |||
| 224 | my $result = eval { | ||
| 225 | my %result; | ||
| 226 | my $domain = $params->{domain}; | ||
| 227 | my ( $_errors, $normalized_domain ) = normalize_name( trim_space ( $domain ) ); | ||
| 228 | |||
| 229 | my @ns_list; | ||
| 230 | my @ns_names; | ||
| 231 | |||
| 232 | my $zone = Zonemaster::Engine->zone( $normalized_domain ); | ||
| 233 | push @ns_list, { ns => $_->name->string, ip => $_->address->short} for @{$zone->glue}; | ||
| 234 | |||
| 235 | my @ds_list; | ||
| 236 | |||
| 237 | $zone = Zonemaster::Engine->zone($normalized_domain); | ||
| 238 | my $ds_p = $zone->parent->query_one( $zone->name, 'DS', { dnssec => 1, cd => 1, recurse => 1 } ); | ||
| 239 | if ($ds_p) { | ||
| 240 | my @ds = $ds_p->get_records( 'DS', 'answer' ); | ||
| 241 | |||
| 242 | foreach my $ds ( @ds ) { | ||
| 243 | next unless $ds->type eq 'DS'; | ||
| 244 | push(@ds_list, { keytag => $ds->keytag, algorithm => $ds->algorithm, digtype => $ds->digtype, digest => $ds->hexdigest }); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | $result{ns_list} = \@ns_list; | ||
| 249 | $result{ds_list} = \@ds_list; | ||
| 250 | return \%result; | ||
| 251 | }; | ||
| 252 | if ($@) { | ||
| 253 | handle_exception( $@ ); | ||
| 254 | } | ||
| 255 | elsif ($result) { | ||
| 256 | return $result; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | # Experimental | ||
| 261 | $json_schemas{lookup_delegation_data} = $json_schemas{get_data_from_parent_zone}; | ||
| 262 | sub lookup_delegation_data { | ||
| 263 | return get_data_from_parent_zone( @_ ); | ||
| 264 | } | ||
| 265 | |||
| 266 | $json_schemas{start_domain_test} = { | ||
| 267 | type => 'object', | ||
| 268 | additionalProperties => 0, | ||
| 269 | required => [ 'domain' ], | ||
| 270 | properties => { | ||
| 271 | domain => $zm_validator->domain_name, | ||
| 272 | ipv4 => joi->boolean->compile, | ||
| 273 | ipv6 => joi->boolean->compile, | ||
| 274 | nameservers => { | ||
| 275 | type => 'array', | ||
| 276 | items => $zm_validator->nameserver | ||
| 277 | }, | ||
| 278 | ds_info => { | ||
| 279 | type => 'array', | ||
| 280 | items => $zm_validator->ds_info | ||
| 281 | }, | ||
| 282 | profile => $zm_validator->profile_name, | ||
| 283 | client_id => $zm_validator->client_id->compile, | ||
| 284 | client_version => $zm_validator->client_version->compile, | ||
| 285 | config => joi->string->compile, | ||
| 286 | priority => $zm_validator->priority->compile, | ||
| 287 | queue => $zm_validator->queue->compile, | ||
| 288 | language => $zm_validator->language_tag, | ||
| 289 | } | ||
| 290 | }; | ||
| 291 | sub start_domain_test { | ||
| 292 | my ( $self, $params ) = @_; | ||
| 293 | |||
| 294 | my $result = 0; | ||
| 295 | eval { | ||
| 296 | $params->{profile} //= "default"; | ||
| 297 | $params->{priority} //= 10; | ||
| 298 | $params->{queue} //= 0; | ||
| 299 | |||
| 300 | my $profile = $self->{_profiles}{ $params->{profile} }; | ||
| 301 | $params->{ipv4} //= $profile->get( "net.ipv4" ); | ||
| 302 | $params->{ipv6} //= $profile->get( "net.ipv6" ); | ||
| 303 | |||
| 304 | $result = $self->{db}->create_new_test( $params->{domain}, $params, $self->{config}->ZONEMASTER_age_reuse_previous_test ); | ||
| 305 | }; | ||
| 306 | if ($@) { | ||
| 307 | handle_exception( $@ ); | ||
| 308 | } | ||
| 309 | |||
| 310 | return $result; | ||
| 311 | } | ||
| 312 | |||
| 313 | # Experimental | ||
| 314 | $json_schemas{job_create} = $json_schemas{start_domain_test}; | ||
| 315 | sub job_create { | ||
| 316 | my $result = { | ||
| 317 | job_id => start_domain_test( @_ ) | ||
| 318 | }; | ||
| 319 | return $result; | ||
| 320 | } | ||
| 321 | |||
| 322 | $json_schemas{test_progress} = joi->object->strict->props( | ||
| 323 | test_id => $zm_validator->test_id->required | ||
| 324 | ); | ||
| 325 | sub test_progress { | ||
| 326 | my ( $self, $params ) = @_; | ||
| 327 | |||
| 328 | my $result = 0; | ||
| 329 | eval { | ||
| 330 | my $test_id = $params->{test_id}; | ||
| 331 | $result = $self->{db}->test_progress( $test_id ); | ||
| 332 | }; | ||
| 333 | if ($@) { | ||
| 334 | handle_exception( $@ ); | ||
| 335 | } | ||
| 336 | |||
| 337 | return $result; | ||
| 338 | } | ||
| 339 | |||
| 340 | # Experimental | ||
| 341 | $json_schemas{job_status} = joi->object->strict->props( | ||
| 342 | job_id => $zm_validator->test_id->required | ||
| 343 | ); | ||
| 344 | sub job_status { | ||
| 345 | my ( $self, $params ) = @_; | ||
| 346 | |||
| 347 | $params->{test_id} = delete $params->{job_id}; | ||
| 348 | |||
| 349 | my $result = { | ||
| 350 | progress => $self->test_progress( $params ) | ||
| 351 | }; | ||
| 352 | return $result; | ||
| 353 | } | ||
| 354 | |||
| 355 | $json_schemas{get_test_params} = joi->object->strict->props( | ||
| 356 | test_id => $zm_validator->test_id->required | ||
| 357 | ); | ||
| 358 | sub get_test_params { | ||
| 359 | my ( $self, $params ) = @_; | ||
| 360 | |||
| 361 | my $result; | ||
| 362 | eval { | ||
| 363 | my $test_id = $params->{test_id}; | ||
| 364 | |||
| 365 | $result = $self->{db}->get_test_params( $test_id ); | ||
| 366 | }; | ||
| 367 | if ($@) { | ||
| 368 | handle_exception( $@ ); | ||
| 369 | } | ||
| 370 | |||
| 371 | return $result; | ||
| 372 | } | ||
| 373 | |||
| 374 | # Experimental | ||
| 375 | $json_schemas{job_params} = joi->object->strict->props( | ||
| 376 | job_id => $zm_validator->test_id->required | ||
| 377 | ); | ||
| 378 | sub job_params { | ||
| 379 | my ( $self, $params ) = @_; | ||
| 380 | |||
| 381 | $params->{test_id} = delete $params->{job_id}; | ||
| 382 | |||
| 383 | return $self->get_test_params( $params ); | ||
| 384 | } | ||
| 385 | |||
| 386 | $json_schemas{get_test_results} = { | ||
| 387 | type => 'object', | ||
| 388 | additionalProperties => 0, | ||
| 389 | required => [ 'id', 'language' ], | ||
| 390 | properties => { | ||
| 391 | id => $zm_validator->test_id->required->compile, | ||
| 392 | language => $zm_validator->language_tag, | ||
| 393 | } | ||
| 394 | }; | ||
| 395 | sub get_test_results { | ||
| 396 | my ( $self, $params ) = @_; | ||
| 397 | |||
| 398 | my $result; | ||
| 399 | eval{ | ||
| 400 | |||
| 401 | my $locale = $self->_get_locale( $params ); | ||
| 402 | |||
| 403 | my $translator; | ||
| 404 | $translator = Zonemaster::Backend::Translator->instance(); | ||
| 405 | |||
| 406 | my $previous_locale = $translator->locale; | ||
| 407 | if ( !$translator->locale( $locale ) ) { | ||
| 408 | die "Failed to set locale: $locale"; | ||
| 409 | } | ||
| 410 | |||
| 411 | eval { $translator->data } if $translator; # Provoke lazy loading of translation data | ||
| 412 | |||
| 413 | my @zm_results; | ||
| 414 | my %testcases; | ||
| 415 | |||
| 416 | my $test_info = $self->{db}->test_results( $params->{id} ); | ||
| 417 | foreach my $test_res ( @{ $test_info->{results} } ) { | ||
| 418 | my $res; | ||
| 419 | if ( $test_res->{module} eq 'Nameserver' ) { | ||
| 420 | $res->{ns} = ( $test_res->{args}->{ns} ) ? ( $test_res->{args}->{ns} ) : ( 'All' ); | ||
| 421 | } | ||
| 422 | elsif ($test_res->{module} eq 'SYSTEM' | ||
| 423 | && $test_res->{tag} eq 'POLICY_DISABLED' | ||
| 424 | && $test_res->{args}->{name} eq 'Example' ) | ||
| 425 | { | ||
| 426 | next; | ||
| 427 | } | ||
| 428 | |||
| 429 | $res->{module} = $test_res->{module}; | ||
| 430 | $res->{message} = $translator->translate_tag( $test_res ) . "\n"; | ||
| 431 | $res->{message} =~ s/,/, /isg; | ||
| 432 | $res->{message} =~ s/;/; /isg; | ||
| 433 | $res->{level} = $test_res->{level}; | ||
| 434 | $res->{testcase} = $test_res->{testcase} // 'UNSPECIFIED'; | ||
| 435 | $testcases{$res->{testcase}} = $translator->test_case_description($res->{testcase}); | ||
| 436 | |||
| 437 | if ( $test_res->{module} eq 'SYSTEM' ) { | ||
| 438 | if ( $res->{message} =~ /policy\.json/ ) { | ||
| 439 | my ( $policy ) = ( $res->{message} =~ /\s(\/.*)$/ ); | ||
| 440 | if ( $policy ) { | ||
| 441 | my $policy_description = 'DEFAULT POLICY'; | ||
| 442 | $policy_description = 'SOME OTHER POLICY' if ( $policy =~ /some\/other\/policy\/path/ ); | ||
| 443 | $res->{message} =~ s/$policy/$policy_description/; | ||
| 444 | } | ||
| 445 | else { | ||
| 446 | $res->{message} = 'UNKNOWN POLICY FORMAT'; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | elsif ( $res->{message} =~ /config\.json/ ) { | ||
| 450 | my ( $config ) = ( $res->{message} =~ /\s(\/.*)$/ ); | ||
| 451 | if ( $config ) { | ||
| 452 | my $config_description = 'DEFAULT CONFIGURATION'; | ||
| 453 | $config_description = 'SOME OTHER CONFIGURATION' if ( $config =~ /some\/other\/configuration\/path/ ); | ||
| 454 | $res->{message} =~ s/$config/$config_description/; | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | $res->{message} = 'UNKNOWN CONFIG FORMAT'; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | push( @zm_results, $res ); | ||
| 463 | } | ||
| 464 | |||
| 465 | $result = $test_info; | ||
| 466 | $result->{testcase_descriptions} = \%testcases; | ||
| 467 | $result->{results} = \@zm_results; | ||
| 468 | |||
| 469 | $translator->locale( $previous_locale ); | ||
| 470 | |||
| 471 | $result = $test_info; | ||
| 472 | $result->{results} = \@zm_results; | ||
| 473 | }; | ||
| 474 | if ($@) { | ||
| 475 | handle_exception( $@ ); | ||
| 476 | } | ||
| 477 | |||
| 478 | return $result; | ||
| 479 | } | ||
| 480 | |||
| 481 | # Experimental | ||
| 482 | $json_schemas{job_results} = { | ||
| 483 | type => 'object', | ||
| 484 | additionalProperties => 0, | ||
| 485 | required => [ 'job_id', 'language' ], | ||
| 486 | properties => { | ||
| 487 | job_id => $zm_validator->test_id->required->compile, | ||
| 488 | language => $zm_validator->language_tag, | ||
| 489 | } | ||
| 490 | }; | ||
| 491 | sub job_results { | ||
| 492 | my ( $self, $params ) = @_; | ||
| 493 | |||
| 494 | $params->{id} = delete $params->{job_id}; | ||
| 495 | |||
| 496 | my $result = $self->get_test_results( $params ); | ||
| 497 | |||
| 498 | return { | ||
| 499 | created_at => $result->{created_at}, | ||
| 500 | job_id => $result->{hash_id}, | ||
| 501 | results => $result->{results}, | ||
| 502 | params => $result->{params}, | ||
| 503 | testcase_descriptions => $result->{testcase_descriptionsd}, | ||
| 504 | }; | ||
| 505 | } | ||
| 506 | |||
| 507 | $json_schemas{get_test_history} = { | ||
| 508 | type => 'object', | ||
| 509 | additionalProperties => 0, | ||
| 510 | required => [ 'frontend_params' ], | ||
| 511 | properties => { | ||
| 512 | offset => joi->integer->min(0)->compile, | ||
| 513 | limit => joi->integer->min(0)->compile, | ||
| 514 | filter => joi->string->regex('^(?:all|delegated|undelegated)$')->compile, | ||
| 515 | frontend_params => { | ||
| 516 | type => 'object', | ||
| 517 | additionalProperties => 0, | ||
| 518 | required => [ 'domain' ], | ||
| 519 | properties => { | ||
| 520 | domain => $zm_validator->domain_name | ||
| 521 | } | ||
| 522 | } | ||
| 523 | } | ||
| 524 | }; | ||
| 525 | sub get_test_history { | ||
| 526 | my ( $self, $params ) = @_; | ||
| 527 | |||
| 528 | my $results; | ||
| 529 | |||
| 530 | eval { | ||
| 531 | $params->{offset} //= 0; | ||
| 532 | $params->{limit} //= 200; | ||
| 533 | $params->{filter} //= "all"; | ||
| 534 | |||
| 535 | $results = $self->{db}->get_test_history( $params ); | ||
| 536 | my @results = map { { %$_, undelegated => $_->{undelegated} ? JSON::PP::true : JSON::PP::false } } @$results; | ||
| 537 | $results = \@results; | ||
| 538 | |||
| 539 | }; | ||
| 540 | if ($@) { | ||
| 541 | handle_exception( $@ ); | ||
| 542 | } | ||
| 543 | |||
| 544 | return $results; | ||
| 545 | } | ||
| 546 | |||
| 547 | # Experimental | ||
| 548 | $json_schemas{domain_history} = { | ||
| 549 | type => 'object', | ||
| 550 | additionalProperties => 0, | ||
| 551 | required => [ 'params' ], | ||
| 552 | properties => { | ||
| 553 | offset => joi->integer->min(0)->compile, | ||
| 554 | limit => joi->integer->min(0)->compile, | ||
| 555 | filter => joi->string->regex('^(?:all|delegated|undelegated)$')->compile, | ||
| 556 | params => { | ||
| 557 | type => 'object', | ||
| 558 | additionalProperties => 0, | ||
| 559 | required => [ 'domain' ], | ||
| 560 | properties => { | ||
| 561 | domain => $zm_validator->domain_name | ||
| 562 | } | ||
| 563 | } | ||
| 564 | } | ||
| 565 | }; | ||
| 566 | sub domain_history { | ||
| 567 | my ( $self, $params ) = @_; | ||
| 568 | |||
| 569 | $params->{frontend_params} = delete $params->{params}; | ||
| 570 | |||
| 571 | my $results = $self->get_test_history( $params ); | ||
| 572 | |||
| 573 | return { | ||
| 574 | history => [ | ||
| 575 | map { | ||
| 576 | { | ||
| 577 | job_id => $_->{id}, | ||
| 578 | created_at => $_->{created_at}, | ||
| 579 | overall_result => $_->{overall_result}, | ||
| 580 | undelegated => $_->{undelegated}, | ||
| 581 | } | ||
| 582 | } @$results | ||
| 583 | ], | ||
| 584 | }; | ||
| 585 | } | ||
| 586 | |||
| 587 | $json_schemas{add_api_user} = joi->object->strict->props( | ||
| 588 | username => $zm_validator->username->required, | ||
| 589 | api_key => $zm_validator->api_key->required, | ||
| 590 | ); | ||
| 591 | sub add_api_user { | ||
| 592 | my ( $self, $params, undef, $remote_ip ) = @_; | ||
| 593 | |||
| 594 | my $result = 0; | ||
| 595 | |||
| 596 | eval { | ||
| 597 | my $allow = 0; | ||
| 598 | if ( defined $remote_ip ) { | ||
| 599 | $allow = 1 if ( $remote_ip eq '::1' || $remote_ip eq '127.0.0.1' || $remote_ip eq '::ffff:127.0.0.1' ); | ||
| 600 | } | ||
| 601 | else { | ||
| 602 | $allow = 1; | ||
| 603 | } | ||
| 604 | |||
| 605 | if ( $allow ) { | ||
| 606 | $result = 1 if ( $self->{db}->add_api_user( $params->{username}, $params->{api_key} ) eq '1' ); | ||
| 607 | } | ||
| 608 | else { | ||
| 609 | die Zonemaster::Backend::Error::PermissionDenied->new( | ||
| 610 | message => 'Call to "add_api_user" method not permitted from a remote IP', | ||
| 611 | data => { remote_ip => $remote_ip } | ||
| 612 | ); | ||
| 613 | } | ||
| 614 | }; | ||
| 615 | if ($@) { | ||
| 616 | handle_exception( $@ ); | ||
| 617 | } | ||
| 618 | |||
| 619 | return $result; | ||
| 620 | } | ||
| 621 | |||
| 622 | # Experimental | ||
| 623 | $json_schemas{user_create} = $json_schemas{add_api_user}; | ||
| 624 | sub user_create { | ||
| 625 | my $result = { | ||
| 626 | success => add_api_user( @_ ) | ||
| 627 | }; | ||
| 628 | return $result; | ||
| 629 | } | ||
| 630 | |||
| 631 | $json_schemas{add_batch_job} = { | ||
| 632 | type => 'object', | ||
| 633 | additionalProperties => 0, | ||
| 634 | required => [ 'username', 'api_key', 'domains' ], | ||
| 635 | properties => { | ||
| 636 | username => $zm_validator->username->required->compile, | ||
| 637 | api_key => $zm_validator->api_key->required->compile, | ||
| 638 | domains => { | ||
| 639 | type => "array", | ||
| 640 | additionalItems => 0, | ||
| 641 | items => $zm_validator->domain_name, | ||
| 642 | minItems => 1 | ||
| 643 | }, | ||
| 644 | test_params => { | ||
| 645 | type => 'object', | ||
| 646 | additionalProperties => 0, | ||
| 647 | properties => { | ||
| 648 | ipv4 => joi->boolean->compile, | ||
| 649 | ipv6 => joi->boolean->compile, | ||
| 650 | nameservers => { | ||
| 651 | type => 'array', | ||
| 652 | items => $zm_validator->nameserver | ||
| 653 | }, | ||
| 654 | ds_info => { | ||
| 655 | type => 'array', | ||
| 656 | items => $zm_validator->ds_info | ||
| 657 | }, | ||
| 658 | profile => $zm_validator->profile_name, | ||
| 659 | client_id => $zm_validator->client_id->compile, | ||
| 660 | client_version => $zm_validator->client_version->compile, | ||
| 661 | config => joi->string->compile, | ||
| 662 | priority => $zm_validator->priority->compile, | ||
| 663 | queue => $zm_validator->queue->compile, | ||
| 664 | } | ||
| 665 | } | ||
| 666 | } | ||
| 667 | }; | ||
| 668 | sub add_batch_job { | ||
| 669 | my ( $self, $params ) = @_; | ||
| 670 | |||
| 671 | my $results; | ||
| 672 | eval { | ||
| 673 | $params->{test_params}{profile} //= "default"; | ||
| 674 | $params->{test_params}{priority} //= 5; | ||
| 675 | $params->{test_params}{queue} //= 0; | ||
| 676 | |||
| 677 | my $profile = $self->{_profiles}{ $params->{test_params}{profile} }; | ||
| 678 | $params->{test_params}{ipv4} //= $profile->get( "net.ipv4" ); | ||
| 679 | $params->{test_params}{ipv6} //= $profile->get( "net.ipv6" ); | ||
| 680 | |||
| 681 | $results = $self->{db}->add_batch_job( $params ); | ||
| 682 | }; | ||
| 683 | if ($@) { | ||
| 684 | handle_exception( $@ ); | ||
| 685 | } | ||
| 686 | |||
| 687 | return $results; | ||
| 688 | } | ||
| 689 | |||
| 690 | # Experimental | ||
| 691 | $json_schemas{batch_create} = { | ||
| 692 | type => 'object', | ||
| 693 | additionalProperties => 0, | ||
| 694 | required => [ 'username', 'api_key', 'domains' ], | ||
| 695 | properties => { | ||
| 696 | username => $zm_validator->username->required->compile, | ||
| 697 | api_key => $zm_validator->api_key->required->compile, | ||
| 698 | domains => { | ||
| 699 | type => "array", | ||
| 700 | additionalItems => 0, | ||
| 701 | items => $zm_validator->domain_name, | ||
| 702 | minItems => 1 | ||
| 703 | }, | ||
| 704 | job_params => { | ||
| 705 | type => 'object', | ||
| 706 | additionalProperties => 0, | ||
| 707 | properties => { | ||
| 708 | ipv4 => joi->boolean->compile, | ||
| 709 | ipv6 => joi->boolean->compile, | ||
| 710 | nameservers => { | ||
| 711 | type => 'array', | ||
| 712 | items => $zm_validator->nameserver | ||
| 713 | }, | ||
| 714 | ds_info => { | ||
| 715 | type => 'array', | ||
| 716 | items => $zm_validator->ds_info | ||
| 717 | }, | ||
| 718 | profile => $zm_validator->profile_name, | ||
| 719 | client_id => $zm_validator->client_id->compile, | ||
| 720 | client_version => $zm_validator->client_version->compile, | ||
| 721 | config => joi->string->compile, | ||
| 722 | priority => $zm_validator->priority->compile, | ||
| 723 | queue => $zm_validator->queue->compile, | ||
| 724 | } | ||
| 725 | } | ||
| 726 | } | ||
| 727 | }; | ||
| 728 | sub batch_create { | ||
| 729 | my ( $self, $params ) = @_; | ||
| 730 | |||
| 731 | $params->{test_params} = delete $params->{job_params}; | ||
| 732 | |||
| 733 | my $result = { | ||
| 734 | batch_id => $self->add_batch_job( $params ) | ||
| 735 | }; | ||
| 736 | |||
| 737 | return $result; | ||
| 738 | } | ||
| 739 | |||
| 740 | $json_schemas{batch_status} = { | ||
| 741 | type => 'object', | ||
| 742 | additionalProperties => 0, | ||
| 743 | required => [ 'batch_id' ], | ||
| 744 | properties => { | ||
| 745 | batch_id => $zm_validator->batch_id->required, | ||
| 746 | list_waiting_tests => joi->boolean->compile, | ||
| 747 | list_running_tests => joi->boolean->compile, | ||
| 748 | list_finished_tests => joi->boolean->compile, | ||
| 749 | } | ||
| 750 | }; | ||
| 751 | sub batch_status { | ||
| 752 | my ( $self, $params ) = @_; | ||
| 753 | |||
| 754 | my $result; | ||
| 755 | eval { | ||
| 756 | $result = $self->{db}->batch_status($params); | ||
| 757 | }; | ||
| 758 | if ($@) { | ||
| 759 | handle_exception( $@ ); | ||
| 760 | } | ||
| 761 | return $result; | ||
| 762 | } | ||
| 763 | |||
| 764 | sub _get_locale { | ||
| 765 | my ( $self, $params ) = @_; | ||
| 766 | my @error; | ||
| 767 | |||
| 768 | if ( ref $params ne 'HASH' ) { | ||
| 769 | return undef; | ||
| 770 | } | ||
| 771 | |||
| 772 | my $language = $params->{language}; | ||
| 773 | if ( !defined $language ) { | ||
| 774 | return undef; | ||
| 775 | } | ||
| 776 | |||
| 777 | my %locales = $self->{config}->LANGUAGE_locale; | ||
| 778 | |||
| 779 | my $locale = $locales{$language}; | ||
| 780 | if ( !defined $locale ) { | ||
| 781 | return undef; | ||
| 782 | } | ||
| 783 | |||
| 784 | return $locale . '.UTF-8'; | ||
| 785 | } | ||
| 786 | |||
| 787 | sub _set_error_message_locale { | ||
| 788 | my ( $self, $params ) = @_; | ||
| 789 | |||
| 790 | my @error_response = (); | ||
| 791 | my $locale = $self->_get_locale( $params ); | ||
| 792 | |||
| 793 | if (not defined $locale or $locale eq "") { | ||
| 794 | # Don't translate message if locale is not defined | ||
| 795 | $locale = "C"; | ||
| 796 | } | ||
| 797 | |||
| 798 | # Use POSIX implementation instead of Locale::Messages wrapper | ||
| 799 | setlocale( LC_ALL, $locale ); | ||
| 800 | return @error_response; | ||
| 801 | } | ||
| 802 | |||
| 803 | my $rpc_request = joi->object->props( | ||
| 804 | jsonrpc => joi->string->required, | ||
| 805 | method => $zm_validator->jsonrpc_method()->required, | ||
| 806 | id => joi->type([qw(null number string)])); | ||
| 807 | sub jsonrpc_validate { | ||
| 808 | my ( $self, $jsonrpc_request ) = @_; | ||
| 809 | |||
| 810 | my @error_rpc = $rpc_request->validate($jsonrpc_request); | ||
| 811 | if ((ref($jsonrpc_request) eq 'HASH' && !exists $jsonrpc_request->{id}) || @error_rpc) { | ||
| 812 | $self->_set_error_message_locale; | ||
| 813 | return { | ||
| 814 | jsonrpc => '2.0', | ||
| 815 | id => undef, | ||
| 816 | error => { | ||
| 817 | code => '-32600', | ||
| 818 | message => 'The JSON sent is not a valid request object.', | ||
| 819 | data => "@error_rpc" | ||
| 820 | } | ||
| 821 | } | ||
| 822 | } | ||
| 823 | |||
| 824 | my $method_schema = $json_schemas{$jsonrpc_request->{method}}; | ||
| 825 | if (blessed $method_schema) { | ||
| 826 | $method_schema = $method_schema->compile; | ||
| 827 | } | ||
| 828 | |||
| 829 | # The "params" key of the JSONRPC object is optional per the JSONRPC 2.0 | ||
| 830 | # specification, but if the method being called requires at least one | ||
| 831 | # parameter, omitting it is an error. | ||
| 832 | |||
| 833 | if ( exists $method_schema->{required} and not exists $jsonrpc_request->{params} ) { | ||
| 834 | return { | ||
| 835 | jsonrpc => '2.0', | ||
| 836 | id => $jsonrpc_request->{id}, | ||
| 837 | error => { | ||
| 838 | code => '-32602', | ||
| 839 | message => "Missing 'params' object", | ||
| 840 | } | ||
| 841 | }; | ||
| 842 | } | ||
| 843 | elsif ( exists $jsonrpc_request->{params} ) { | ||
| 844 | my @error_response = $self->validate_params($method_schema, $jsonrpc_request->{params}); | ||
| 845 | |||
| 846 | if ( scalar @error_response ) { | ||
| 847 | return { | ||
| 848 | jsonrpc => '2.0', | ||
| 849 | id => $jsonrpc_request->{id}, | ||
| 850 | error => { | ||
| 851 | code => '-32602', | ||
| 852 | message => decode_utf8(__ 'Invalid method parameter(s).'), | ||
| 853 | data => \@error_response | ||
| 854 | } | ||
| 855 | }; | ||
| 856 | } | ||
| 857 | } | ||
| 858 | |||
| 859 | return ''; | ||
| 860 | } | ||
| 861 | |||
| 862 | sub validate_params { | ||
| 863 | my ( $self, $method_schema, $params ) = @_; | ||
| 864 | my @error_response = (); | ||
| 865 | |||
| 866 | push @error_response, $self->_set_error_message_locale( $params ); | ||
| 867 | |||
| 868 | if (blessed $method_schema) { | ||
| 869 | $method_schema = $method_schema->compile; | ||
| 870 | } | ||
| 871 | my $jv = JSON::Validator::Schema::Draft7->new->coerce('booleans,numbers,strings')->data($method_schema); | ||
| 872 | $jv->formats(Zonemaster::Backend::Validator::formats( $self->{config} )); | ||
| 873 | my @json_validation_error = $jv->validate( $params ); | ||
| 874 | |||
| 875 | # Customize error message from json validation | ||
| 876 | foreach my $err ( @json_validation_error ) { | ||
| 877 | my $message = $err->message; | ||
| 878 | my @details = @{$err->details}; | ||
| 879 | |||
| 880 | # Handle 'required' errors globally so it does not get overwritten | ||
| 881 | if ($details[1] eq 'required') { | ||
| 882 | $message = N__ 'Missing property'; | ||
| 883 | } else { | ||
| 884 | my @path = split '/', $err->path, -1; | ||
| 885 | shift @path; # first item is an empty string | ||
| 886 | my $found = 1; | ||
| 887 | my $data = Mojo::JSON::Pointer->new($method_schema); | ||
| 888 | |||
| 889 | foreach my $p (@path) { | ||
| 890 | if ( $data->contains("/properties/$p") ) { | ||
| 891 | $data = $data->get("/properties/$p") | ||
| 892 | } elsif ( $p =~ /^\d+$/ and $data->contains("/items") ) { | ||
| 893 | $data = $data->get("/items") | ||
| 894 | } else { | ||
| 895 | $found = 0; | ||
| 896 | last; | ||
| 897 | } | ||
| 898 | $data = Mojo::JSON::Pointer->new($data); | ||
| 899 | } | ||
| 900 | |||
| 901 | if ($found and exists $data->data->{'x-error-message'}) { | ||
| 902 | $message = $data->data->{'x-error-message'}; | ||
| 903 | } | ||
| 904 | } | ||
| 905 | |||
| 906 | push @error_response, { path => $err->path, message => $message }; | ||
| 907 | |||
| 908 | } | ||
| 909 | |||
| 910 | # Translate messages | ||
| 911 | @error_response = map { { %$_, ( message => decode_utf8 __ $_->{message} ) } } @error_response; | ||
| 912 | |||
| 913 | return @error_response; | ||
| 914 | } | ||
| 915 | |||
| 916 | 1; |