| 1 | package PowerDNS::API::ACL::Proxy 0.002; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: A PSGI application that passes allowed requests on to the PowerDNS API | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.014; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use re '/aasx'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Cpanel::JSON::XS (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use HTTP::Tiny::UNIX (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Plack::Request (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use PowerDNS::API::ACL; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use PowerDNS::API::ACL::Store; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | 1 | my $JSON = Cpanel::JSON::XS->new->utf8->canonical; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||
| 22 | 0 | my ( $class, %opts ) = @_; | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | 0 | foreach my $required (qw{upstream api_key}) { | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | die "PowerDNS::API::ACL::Proxy needs $required\n" unless $opts{$required}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $store = $opts{store} // PowerDNS::API::ACL::Store->new( path => $opts{db} // die "PowerDNS::API::ACL::Proxy needs db or store\n" ); | ||||||||||||||||||||||||||||
|
Condition0%0/5
$opts{"store"} // "PowerDNS::API::ACL::Store"->new("path", $opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n"))
$opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n")
MC/DC0%0/2
$opts{"store"} // "PowerDNS::API::ACL::Store"->new("path", $opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n"))
$opts{"store"}
"PowerDNS::API::ACL::Store"->new("path", $opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n"))
MC/DC0%0/1
$opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n")
$opts{"db"}
Truth table0%0/3
A // B
A= $opts{"store"}B= "PowerDNS::API::ACL::Store"->new("path", $opts{"db"} // die("PowerDNS::API::ACL::Proxy needs db or store\n"))
Truth table0%0/2
A
A= $opts{"db"}
| |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | # HTTP::Tiny::UNIX reads http: followed by a socket path, then the path of | ||||||||||||||||||||||||||||||
| 30 | # the request after a second slash. | ||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $base = index( $opts{upstream}, q{/} ) == 0 ? "http:$opts{upstream}/" : $opts{upstream} =~ s{/+\z}{}r; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | # No keep_alive: HTTP::Tiny checks a kept connection with peerhost, which a | ||||||||||||||||||||||||||||||
| 34 | # unix socket does not have, and a connection to one costs next to nothing. | ||||||||||||||||||||||||||||||
| 35 | return bless { | ||||||||||||||||||||||||||||||
| 36 | acl => PowerDNS::API::ACL->new( store => $store ), | ||||||||||||||||||||||||||||||
| 37 | base => $base, | ||||||||||||||||||||||||||||||
| 38 | api_key => $opts{api_key}, | ||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | http => HTTP::Tiny::UNIX->new( keep_alive => 0, timeout => $opts{timeout} // 10 ), | ||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"timeout"} // 10
MC/DC0%0/1
$opts{"timeout"} // 10
$opts{"timeout"}
Truth table0%0/2
A
A= $opts{"timeout"}
| |||||||||||||||||||||||||||||||
| 40 | }, $class; | ||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub to_app { | ||||||||||||||||||||||||||||
|
Subroutine
to_app: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 45 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return sub { return $self->handle( Plack::Request->new(shift) ) }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||
| 51 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 0 | my $content = $req->content; | |||||||||||||||||||||||||||||
| 54 | 0 | my $body; | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($content) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | 0 | $body = eval { $JSON->decode($content) }; | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return error( 400, 'The body is not JSON' ) unless defined $body; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | my $verdict = $self->{acl}->authorize( | ||||||||||||||||||||||||||||||
| 61 | 0 | token => scalar $req->header('X-API-Key'), | |||||||||||||||||||||||||||||
| 62 | method => $req->method, | ||||||||||||||||||||||||||||||
| 63 | path => $req->path_info, | ||||||||||||||||||||||||||||||
| 64 | body => $body, | ||||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | return error( $verdict->{status}, $verdict->{reason} ) unless $verdict->{allow}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 0 | my $query = $req->env->{QUERY_STRING}; | |||||||||||||||||||||||||||||
| 69 | my $res = $self->{http}->request( | ||||||||||||||||||||||||||||||
| 70 | $req->method, | ||||||||||||||||||||||||||||||
| 71 | $self->{base} . $req->path_info . ( $query ? "?$query" : q{} ), | ||||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | headers => { 'X-API-Key' => $self->{api_key}, 'Content-Type' => 'application/json', Accept => 'application/json' }, | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 74 | ( $content ? ( content => $content ) : () ), | ||||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | # 599 is HTTP::Tiny saying that no answer came, which is not PowerDNS | ||||||||||||||||||||||||||||||
| 79 | # refusing the request. | ||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return error( 502, "PowerDNS did not answer: $res->{content}" ) if $res->{status} == 599; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $out = $res->{content} // q{}; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$res->{"content"} // ""
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table0%0/2
A
A= $res->{"content"}
| |||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $verdict->{filter} && $res->{success} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$verdict->{"filter"} and $res->{"success"}
MC/DC0%0/2
$verdict->{"filter"} and $res->{"success"}
$verdict->{"filter"}
$res->{"success"}
Truth table0%0/3
A and B
A= $verdict->{"filter"}B= $res->{"success"}
| |||||||||||||||||||||||||||||||
| 84 | 0 | my $zone = eval { $JSON->decode($out) }; | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return error( 502, 'PowerDNS answered with a zone that is not JSON' ) unless ref $zone eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | 0 | $out = $JSON->encode( PowerDNS::API::ACL->filter_zone( $zone, $verdict->{filter} ) ); | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return [ $res->{status}, [ 'Content-Type' => $res->{headers}{'content-type'} // 'application/json' ], [$out] ]; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$res->{"headers"}{"content-type"} // "application/json"
MC/DC0%0/1
$res->{"headers"}{"content-type"} // "application/json"
$res->{"headers"}{"content-type"}
Truth table0%0/2
A
A= $res->{"headers"}{"content-type"}
| |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 94 | 0 | my ( $status, $message ) = @_; | |||||||||||||||||||||||||||||
| 95 | 0 | return [ $status, [ 'Content-Type' => 'application/json' ], [ $JSON->encode( { error => $message } ) ] ]; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 1 | 1; | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||