| 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
| |||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
| |||||||||||||||||||||||||||||||||
| 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"} // ["*"]
MC/DC0%0/1
$grant->{"types"} // ["*"]
$grant->{"types"}
Truth table0%0/2
A
A= $grant->{"types"}
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$type // ""
MC/DC0%0/1
$type // ""
$type
Truth table0%0/2
A
A= $type
| |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 0 | my $pattern = $class->canonical_name( $grant->{name} ); | |||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ( index( $pattern, q{*.} ) == 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
length $name > length $below and substr($name, -length($below)) eq $below
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
| |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $name eq $pattern ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$req{"token"} // ""
$req{"token"}
Truth table0%0/2
A
A= $req{"token"}
| |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return $refuse->( 401, 'Unknown API key' ) unless @grants; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $method = uc( $req{method} // q{} ); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$req{"method"} // ""
MC/DC0%0/1
$req{"method"} // ""
$req{"method"}
Truth table0%0/2
A
A= $req{"method"}
| |||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $path = $req{path} // q{}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$req{"path"} // ""
MC/DC0%0/1
$req{"path"} // ""
$req{"path"}
Truth table0%0/2
A
A= $req{"path"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $new = ref $req{body} eq 'HASH' ? __PACKAGE__->canonical_name( $req{body}{name} ) : q{.}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$method eq "POST" and &any(sub {
$_->{"access"} eq "manage" and __PACKAGE__->canonical_name($_->{"zone"}) eq $new;
}
, @grants)
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)
| |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return $refuse->( 403, $method eq 'POST' ? "This key cannot create the zone $new" : 'This key cannot list zones' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$method eq "DELETE" and not $notify
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
| |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $manages ? { allow => 1 } : $refuse->( 403, "Removing the zone $zone is not open to this key" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($notify) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return $method eq 'PUT' && @write ? { allow => 1 } : $refuse->( 403, "This key cannot notify for $zone" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$method eq "PUT" and @write
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
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$method eq "GET" and @readable
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
| |||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $refuse->( 403, "This key cannot $method $zone" ) unless $method eq 'PATCH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $rrsets = ref $req{body} eq 'HASH' ? $req{body}{rrsets} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return $refuse->( 403, 'A PATCH without rrsets changes nothing this key may change' ) unless ref $rrsets eq 'ARRAY' && @$rrsets; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $rrsets eq "ARRAY" and @$rrsets
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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)"
MC/DC0%0/1
$type // "(no type)"
$type
Truth table0%0/2
A
A= $type
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
ref $zone eq "HASH" and ref $zone->{"rrsets"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | 0 | $zone->{rrsets} = \@visible; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 0 | return $zone; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 3 | 1; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||