ACL.pm

filter: Statementstmt 19.0% 16 / 84 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 31.2% 5 / 16 Podpod 100% 5 / 5 totaltotal 12.3% 26 / 211 CC 42 SCAR 59.1
CC
42
Cov
9%
authorize
756.0
covers
56.0
filter_zone
30.0
CRAP 369.1
↑ Summary ACL/Proxy.pm »
1 package PowerDNS::API::ACL 0.002;
2  
3 #ABSTRACT: Per-token access control in front of the PowerDNS HTTP API
4  
5 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use List::Util 1.33 qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # A refusal: the status to answer with, and why.
14 0 ▶my $refuse = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 0 my ( $status, $reason ) = @_;
16 0 return { allow => 0, status => $status, reason => $reason };
17 3 };
18  
19  
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
21 0 my ( $class, %opts ) = @_;
22 0 ▶ die "PowerDNS::API::ACL needs a store\n" unless $opts{store};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"store"} 0 0 0
23 0 return bless { store => $opts{store} }, $class;
24 }
25  
26  
27 0 ▶sub canonical_name {
Subroutine canonical_name: not called CC=2 SCAR=17.9
Pod documented
28 0 my ( undef, $name ) = @_;
29 0 ▶ $name = lc( $name // q{} );
Condition0%0/2
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
30 0 $name =~ s/[.]*\z/./;
31 0 return $name;
32 }
33  
34  
35 0 ▶sub covers {
Subroutine covers: not called CC=7 SCAR=40.3
Pod documented
36 0 my ( $class, $grant, $name, $type ) = @_;
37 0 $name = $class->canonical_name($name);
38  
39 0 ▶ my @types = @{ $grant->{types} // ['*'] };
Condition0%0/2
$grant->{"types"} // ["*"]
l!l
00
MC/DC0%0/1
$grant->{"types"} // ["*"]
$grant->{"types"}
Truth table0%0/2
A
A= $grant->{"types"}
Aresult
11
00
40 0 ▶ return 0 unless any { $_ eq q{*} || uc($_) eq uc( $type // q{} ) } @types;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
unless &any(sub { $_ eq "*" or uc $_ eq uc($type // ""); } , @types) 0 0 0
$_ eq "*" or uc $_ eq uc($type // "") 0 0 0
Condition0%0/2
$type // ""
l!l
00
MC/DC0%0/1
$type // ""
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
41  
42 0 my $pattern = $class->canonical_name( $grant->{name} );
43 0 ▶ if ( index( $pattern, q{*.} ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($pattern, "*.") == 0) 0 0 0
44 0 my $below = substr( $pattern, 1 );
45 0 ▶ return length($name) > length($below) && substr( $name, -length $below ) eq $below ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $name > length $below && substr($name, -length($below)) eq $below ? : 0 0 0
Condition0%0/3
length $name > length $below and substr($name, -length($below)) eq $below
!ll&&!rl&&r
000
MC/DC0%0/2
length $name > length $below and substr($name, -length($below)) eq $below
length $name > length $below substr($name, -length($below)) eq $below
Truth table0%0/3
A and B
A= length $name > length $belowB= substr($name, -length($below)) eq $below
ABresult
0X0
100
111
46 }
47  
48 0 ▶ return $name eq $pattern ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$name eq $pattern ? : 0 0 0
49 }
50  
51  
52 0 ▶sub authorize {
Subroutine authorize: not called CC=27 SCAR=66.3
Pod documented
53 0 my ( $self, %req ) = @_;
54  
55 0 ▶ my @grants = $self->{store}->grants_for_token( $req{token} // q{} );
Condition0%0/2
$req{"token"} // ""
l!l
00
MC/DC0%0/1
$req{"token"} // ""
$req{"token"}
Truth table0%0/2
A
A= $req{"token"}
Aresult
11
00
56 0 ▶ return $refuse->( 401, 'Unknown API key' ) unless @grants;
Branch: 1 branch
BranchTrueFalseTotal
unless @grants 0 0 0
57  
58 0 ▶ my $method = uc( $req{method} // q{} );
Condition0%0/2
$req{"method"} // ""
l!l
00
MC/DC0%0/1
$req{"method"} // ""
$req{"method"}
Truth table0%0/2
A
A= $req{"method"}
Aresult
11
00
59 0 ▶ my $path = $req{path} // q{};
Condition0%0/2
$req{"path"} // ""
l!l
00
MC/DC0%0/1
$req{"path"} // ""
$req{"path"}
Truth table0%0/2
A
A= $req{"path"}
Aresult
11
00
60  
61 # Creating a zone names it in the body, not in the path.
62 0 ▶ if ( $path =~ m{\A/api/v1/servers/[^/]+/zones/?\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\A/api/v1/servers/[^/]+/zones/?\z]aasx) 0 0 0
63 0 ▶ my $new = ref $req{body} eq 'HASH' ? __PACKAGE__->canonical_name( $req{body}{name} ) : q{.};
Branch: 1 branch
BranchTrueFalseTotal
ref $req{'body'} eq 'HASH' ? : 0 0 0
64 0 ▶ return { allow => 1 } if $method eq 'POST' && any { $_->{access} eq 'manage' && __PACKAGE__->canonical_name( $_->{zone} ) eq $new } @grants;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
if $method eq "POST" and &any(sub { $_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new; } , @grants) 0 0 0
$_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new 0 0 0
Condition0%0/3
$method eq "POST" and &any(sub { $_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new; } , @grants)
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "POST" and &any(sub { $_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new; } , @grants)
$method eq "POST" &any(sub { $_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new; } , @grants)
Truth table0%0/3
A and B
A= $method eq "POST"B= &any(sub { $_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new; } , @grants)
ABresult
0X0
100
111
65 0 ▶ return $refuse->( 403, $method eq 'POST' ? "This key cannot create the zone $new" : 'This key cannot list zones' );
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'POST' ? : 0 0 0
66 }
67  
68 0 my ( $zone, $notify ) = $path =~ m{\A/api/v1/servers/[^/]+/zones/([^/]+?)(/notify)?/?\z};
69 0 ▶ return $refuse->( 403, 'Only the records of a zone are open to this key' ) unless defined $zone;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $zone 0 0 0
70  
71 # PowerDNS escapes a byte of a zone id as =XX, so a slash in a classless
72 # reverse zone is =2F.
73 0 $zone =~ s/=([[:xdigit:]]{2})/chr hex $1/ge;
74 0 $zone = __PACKAGE__->canonical_name($zone);
75 0 my @in_zone = grep { __PACKAGE__->canonical_name( $_->{zone} ) eq $zone } @grants;
76 0 ▶ return $refuse->( 403, "This key has no grant in $zone" ) unless @in_zone;
Branch: 1 branch
BranchTrueFalseTotal
unless @in_zone 0 0 0
77  
78 0 my @write = grep { $_->{access} eq 'write' } @in_zone;
79  
80 0 ▶ my $manages = any { $_->{access} eq 'manage' } @in_zone;
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 ▶ if ( $method eq 'DELETE' && !$notify ) { ## no critic (ValuesAndExpressions::PreventSQLInjection) -- an HTTP method, not SQL
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "DELETE" and not $notify) 0 0 0
Condition0%0/3
$method eq "DELETE" and not $notify
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "DELETE" and not $notify
$method eq "DELETE" not $notify
Truth table0%0/3
A and not(B)
A= $method eq "DELETE"B= not $notify
ABresult
0X0
100
111
82 0 ▶ return $manages ? { allow => 1 } : $refuse->( 403, "Removing the zone $zone is not open to this key" );
Branch: 1 branch
BranchTrueFalseTotal
$manages ? : 0 0 0
83 }
84 0 ▶ if ($notify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notify) 0 0 0
85 0 ▶ return $method eq 'PUT' && @write ? { allow => 1 } : $refuse->( 403, "This key cannot notify for $zone" );
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'PUT' && @write ? : 0 0 0
Condition0%0/3
$method eq "PUT" and @write
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "PUT" and @write
$method eq "PUT" @write
Truth table0%0/3
A and B
A= $method eq "PUT"B= @write
ABresult
0X0
100
111
86 }
87 0 my @readable = grep { $_->{access} ne 'manage' } @in_zone;
88 0 ▶ return { allow => 1, filter => \@readable } if $method eq 'GET' && @readable;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" and @readable 0 0 0
Condition0%0/3
$method eq "GET" and @readable
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "GET" and @readable
$method eq "GET" @readable
Truth table0%0/3
A and B
A= $method eq "GET"B= @readable
ABresult
0X0
100
111
89 0 ▶ return $refuse->( 403, "This key cannot $method $zone" ) unless $method eq 'PATCH';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "PATCH" 0 0 0
90  
91 0 ▶ my $rrsets = ref $req{body} eq 'HASH' ? $req{body}{rrsets} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $req{'body'} eq 'HASH' ? : 0 0 0
92 0 ▶ return $refuse->( 403, 'A PATCH without rrsets changes nothing this key may change' ) unless ref $rrsets eq 'ARRAY' && @$rrsets;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rrsets eq "ARRAY" and @$rrsets 0 0 0
Condition0%0/3
ref $rrsets eq "ARRAY" and @$rrsets
!ll&&!rl&&r
000
MC/DC0%0/2
ref $rrsets eq "ARRAY" and @$rrsets
ref $rrsets eq "ARRAY" @$rrsets
Truth table0%0/3
A and B
A= ref $rrsets eq "ARRAY"B= @$rrsets
ABresult
0X0
100
111
93  
94 0 foreach my $rrset (@$rrsets) {
95 0 my ( $name, $type ) = @{$rrset}{qw{name type}};
96 0 ▶ next if any { __PACKAGE__->covers( $_, $name, $type ) } @write;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { __PACKAGE__->covers($_, $name, $type); } , @write) 0 0 0
97 0 ▶ return $refuse->( 403, sprintf( 'This key cannot write the %s records of %s', $type // '(no type)', __PACKAGE__->canonical_name($name) ) );
Condition0%0/2
$type // "(no type)"
l!l
00
MC/DC0%0/1
$type // "(no type)"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
98 }
99  
100 0 return { allow => 1 };
101 }
102  
103  
104 0 ▶sub filter_zone {
Subroutine filter_zone: not called CC=5 SCAR=34.0
Pod documented
105 0 my ( $class, $zone, $grants ) = @_;
106 0 ▶ return $zone unless ref $zone eq 'HASH' && ref $zone->{rrsets} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $zone eq "HASH" and ref $zone->{"rrsets"} eq "ARRAY" 0 0 0
Condition0%0/3
ref $zone eq "HASH" and ref $zone->{"rrsets"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $zone eq "HASH" and ref $zone->{"rrsets"} eq "ARRAY"
ref $zone eq "HASH" ref $zone->{"rrsets"} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $zone eq "HASH"B= ref $zone->{"rrsets"} eq "ARRAY"
ABresult
0X0
100
111
107  
108 0 my @visible;
109 0 foreach my $rrset ( @{ $zone->{rrsets} } ) {
110 0 ▶ push( @visible, $rrset ) if any { $class->covers( $_, $rrset->{name}, $rrset->{type} ) } @$grants;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $class->covers($_, $rrset->{"name"}, $rrset->{"type"}); } , @$grants) 0 0 0
111 }
112 0 $zone->{rrsets} = \@visible;
113  
114 0 return $zone;
115 }
116  
117 3 1;
118  
119 __END__
↑ Summary ACL/Proxy.pm »