ACL/Store.pm

filter: Statementstmt 27.8% 32 / 115 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 38.4% 10 / 26 Podpod 100% 13 / 13 totaltotal 24.8% 55 / 221 CC 30 SCAR 41.0
CC
30
Cov
19%
add_grant
210.0
dbh
30.0
__ANON__
20.0
CRAP 60.4
« ACL/Proxy.pm ↑ Summary
1 package PowerDNS::API::ACL::Store 0.002;
2  
3 #ABSTRACT: The tokens and grants of PowerDNS::API::ACL, kept in SQLite
4  
5 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Crypt::PRNG qw{random_bytes_b64u};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::SHA qw{hmac_sha256_hex};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util 1.33 qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Time::HiRes qw{time};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use PowerDNS::API::ACL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 my @ACCESS = qw{read write manage};
20  
21 2 my @SCHEMA = (
22 q{CREATE TABLE IF NOT EXISTS settings (
23 name TEXT PRIMARY KEY,
24 value TEXT NOT NULL
25 )},
26 q{CREATE TABLE IF NOT EXISTS tokens (
27 id INTEGER PRIMARY KEY,
28 label TEXT NOT NULL UNIQUE,
29 digest TEXT NOT NULL UNIQUE,
30 created REAL NOT NULL
31 )},
32 q{CREATE TABLE IF NOT EXISTS grants (
33 id INTEGER PRIMARY KEY,
34 token_id INTEGER NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,
35 zone TEXT NOT NULL,
36 name TEXT NOT NULL,
37 types TEXT NOT NULL,
38 access TEXT NOT NULL CHECK (access IN ('read', 'write', 'manage')),
39 UNIQUE (token_id, zone, name, types, access)
40 )},
41 );
42  
43  
44 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
45 0 my ( $class, %opts ) = @_;
46 0 ▶ die "PowerDNS::API::ACL::Store needs a path\n" unless defined $opts{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"path"} 0 0 0
47  
48 0 my $self = bless { path => $opts{path} }, $class;
49 0 my $dbh = $self->dbh;
50  
51 # OR IGNORE, so that the first connection makes the salt and every later
52 # one keeps it.
53 0 $dbh->do( q{INSERT OR IGNORE INTO settings (name, value) VALUES ('salt', ?)}, undef, random_bytes_b64u(32) );
54 0 ( $self->{salt} ) = $dbh->selectrow_array(q{SELECT value FROM settings WHERE name = 'salt'});
55  
56 0 return $self;
57 }
58  
59  
60 0 ▶sub dbh {
Subroutine dbh: not called CC=5 SCAR=34.0
Pod documented
61 0 my ($self) = @_;
62 0 ▶ return $self->{dbh} if $self->{dbh} && $self->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} and $self->{"pid"} == $$ 0 0 0
Condition0%0/3
$self->{"dbh"} and $self->{"pid"} == $$
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dbh"} and $self->{"pid"} == $$
$self->{"dbh"} $self->{"pid"} == $$
Truth table0%0/3
A and B
A= $self->{"dbh"}B= $self->{"pid"} == $$
ABresult
0X0
100
111
63  
64 # AutoInactiveDestroy, so that a forked process that drops the connection
65 # of its parent does not close it under the parent.
66 0 my $dbh = DBI->connect( "dbi:SQLite:dbname=$self->{path}", q{}, q{}, { RaiseError => 1, PrintError => 0, AutoCommit => 1, AutoInactiveDestroy => 1, sqlite_unicode => 1 } );
67  
68 # The timeout first, because each statement after it can wait on a lock.
69 0 $dbh->do('PRAGMA busy_timeout = 5000');
70  
71 # WAL lets readers go on while one connection writes. SQLite answers with
72 # the mode it is in, which is not WAL where it cannot be.
73 0 my ($mode) = $dbh->selectrow_array('PRAGMA journal_mode = WAL');
74 0 ▶ die "SQLite keeps $self->{path} in $mode mode, not WAL, so its writers would block its readers\n" unless lc($mode) eq 'wal';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $mode eq "wal" 0 0 0
75  
76 # NORMAL is safe in WAL mode: a crash can lose the last commits, but it
77 # cannot corrupt the database.
78 0 $dbh->do('PRAGMA synchronous = NORMAL');
79  
80 # SQLite enforces a foreign key only when each connection asks it to, and
81 # the cascade is what removes the grants of a removed token.
82 0 $dbh->do('PRAGMA foreign_keys = ON');
83 0 $dbh->do($_) foreach @SCHEMA;
84  
85 0 @{$self}{qw{dbh pid}} = ( $dbh, $$ );
86 0 return $dbh;
87 }
88  
89  
90 0 ▶sub digest {
Subroutine digest: not called CC=1 SCAR=6.9
Pod documented
91 0 my ( $self, $secret ) = @_;
92 0 return hmac_sha256_hex( $secret, $self->{salt} );
93 }
94  
95  
96 0 ▶sub add_token {
Subroutine add_token: not called CC=3 SCAR=24.8
Pod documented
97 0 my ( $self, $label, @grants ) = @_;
98 0 ▶ die "A token needs a label\n" unless $label;
Branch: 1 branch
BranchTrueFalseTotal
unless $label 0 0 0
99 0 ▶ die "A token with no grant could do nothing, so $label needs at least one\n" unless @grants;
Branch: 1 branch
BranchTrueFalseTotal
unless @grants 0 0 0
100  
101 0 my $secret = random_bytes_b64u(32);
102 $self->transaction(
103 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
104 0 ▶ die "There is already a token labelled $label\n" if $self->token_id($label);
Branch: 1 branch
BranchTrueFalseTotal
if $self->token_id($label) 0 0 0
105 0 $self->dbh->do( 'INSERT INTO tokens (label, digest, created) VALUES (?, ?, ?)', undef, $label, $self->digest($secret), time );
106 0 $self->add_grant( $label, %$_ ) foreach @grants;
107 }
108 0 );
109  
110 0 return $secret;
111 }
112  
113  
114 0 ▶sub remove_token {
Subroutine remove_token: not called CC=2 SCAR=17.9
Pod documented
115 0 my ( $self, $label ) = @_;
116 0 ▶ return $self->dbh->do( 'DELETE FROM tokens WHERE label = ?', undef, $label ) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->do('DELETE FROM tokens WHERE label = ?', undef, $label) > 0 ? : 0 0 0
117 }
118  
119  
120 0 ▶sub tokens {
Subroutine tokens: not called CC=1 SCAR=6.9
Pod documented
121 0 my ($self) = @_;
122 return @{
123 0 $self->dbh->selectall_arrayref(
124 'SELECT t.label, t.created, COUNT(g.id) AS grants FROM tokens t LEFT JOIN grants g ON g.token_id = t.id GROUP BY t.id ORDER BY t.label',
125 { Slice => {} },
126 )
127 };
128 }
129  
130  
131 0 ▶sub token_id {
Subroutine token_id: not called CC=1 SCAR=6.9
Pod documented
132 0 my ( $self, $label ) = @_;
133 0 my ($id) = $self->dbh->selectrow_array( 'SELECT id FROM tokens WHERE label = ?', undef, $label );
134 0 return $id;
135 }
136  
137  
138 0 ▶sub add_grant {
Subroutine add_grant: not called CC=14 SCAR=53.5
Pod documented
139 0 my ( $self, $label, %grant ) = @_;
140  
141 0 ▶ my $token = $self->token_id($label) // die "There is no token labelled $label\n";
Condition0%0/2
$self->token_id($label) // die("There is no token labelled $label\n")
l!l
00
MC/DC0%0/1
$self->token_id($label) // die("There is no token labelled $label\n")
$self->token_id($label)
Truth table0%0/2
A
A= $self->token_id($label)
Aresult
11
00
142 0 ▶ my $access = $grant{access} // q{};
Condition0%0/2
$grant{"access"} // ""
l!l
00
MC/DC0%0/1
$grant{"access"} // ""
$grant{"access"}
Truth table0%0/2
A
A= $grant{"access"}
Aresult
11
00
143 0 ▶ die "Access is read, write or manage, not '$access'\n" unless any { $_ eq $access } @ACCESS;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $access; } , @ACCESS) 0 0 0
144  
145 0 my $zone = PowerDNS::API::ACL->canonical_name( $grant{zone} );
146 0 ▶ die "A grant needs a zone\n" if $zone eq q{.};
Branch: 1 branch
BranchTrueFalseTotal
if $zone eq "." 0 0 0
147  
148 0 ▶ my $name = PowerDNS::API::ACL->canonical_name( $grant{name} // ( $access eq 'manage' ? $zone : undef ) );
Branch: 1 branch
BranchTrueFalseTotal
$access eq 'manage' ? : 0 0 0
Condition0%0/3
$grant{"name"} // ($access eq "manage" ? $zone : undef)
l!l&&r!l&&!r
000
MC/DC0%0/2
$grant{"name"} // ($access eq "manage" ? $zone : undef)
$grant{"name"} ($access eq "manage" ? $zone : undef)
Truth table0%0/3
A // B
A= $grant{"name"}B= ($access eq "manage" ? $zone : undef)
ABresult
1X1
011
000
149 0 ▶ die "A manage grant is for the zone $zone itself, not for $name\n" if $access eq 'manage' && $name ne $zone;
Branch: 1 branch
BranchTrueFalseTotal
if $access eq "manage" and $name ne $zone 0 0 0
Condition0%0/3
$access eq "manage" and $name ne $zone
!ll&&!rl&&r
000
MC/DC0%0/2
$access eq "manage" and $name ne $zone
$access eq "manage" $name ne $zone
Truth table0%0/3
A and B
A= $access eq "manage"B= $name ne $zone
ABresult
0X0
100
111
150  
151 0 ▶ my $bare = index( $name, q{*.} ) == 0 ? substr( $name, 2 ) : $name;
Branch: 1 branch
BranchTrueFalseTotal
index($name, '*.') == 0 ? : 0 0 0
152 0 ▶ die "$name is not in the zone $zone\n" unless $bare eq $zone || substr( $bare, -length ".$zone" ) eq ".$zone";
Branch: 1 branch
BranchTrueFalseTotal
unless $bare eq $zone or substr($bare, -length(".$zone")) eq ".$zone" 0 0 0
Condition0%0/3
$bare eq $zone or substr($bare, -length(".$zone")) eq ".$zone"
l!l&&r!l&&!r
000
MC/DC0%0/2
$bare eq $zone or substr($bare, -length(".$zone")) eq ".$zone"
$bare eq $zone substr($bare, -length(".$zone")) eq ".$zone"
Truth table0%0/3
A or B
A= $bare eq $zoneB= substr($bare, -length(".$zone")) eq ".$zone"
ABresult
1X1
011
000
153  
154 0 ▶ my $types = $access eq 'manage' ? q{*} : join( q{,}, map { uc } @{ $grant{types} // [q{*}] } );
Branch: 1 branch
BranchTrueFalseTotal
$access eq 'manage' ? : 0 0 0
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
155 0 $self->dbh->do( 'INSERT OR IGNORE INTO grants (token_id, zone, name, types, access) VALUES (?, ?, ?, ?, ?)', undef, $token, $zone, $name, $types, $access );
156  
157 0 return 1;
158 }
159  
160  
161 0 ▶sub remove_grant {
Subroutine remove_grant: not called CC=1 SCAR=6.9
Pod documented
162 0 my ( $self, $id ) = @_;
163  
164 # One transaction, so that two removals at once cannot each see a second
165 # grant and between them remove both.
166 0 my $removed = 0;
167 $self->transaction(
168 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
169 0 my ( $label, $count ) = $self->dbh->selectrow_array( 'SELECT t.label, (SELECT COUNT(*) FROM grants o WHERE o.token_id = g.token_id) FROM grants g JOIN tokens t ON t.id = g.token_id WHERE g.id = ?', undef, $id );
170 0 ▶ return unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 0 0
171 0 ▶ die "Grant $id is the last grant of the token $label, which could then do nothing. Remove the token instead.\n" if $count < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 2 0 0 0
172 0 ▶ $removed = $self->dbh->do( 'DELETE FROM grants WHERE id = ?', undef, $id ) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->do('DELETE FROM grants WHERE id = ?', undef, $id) > 0 ? : 0 0 0
173 }
174 0 );
175  
176 0 return $removed;
177 }
178  
179  
180 0 ▶sub grants {
Subroutine grants: not called CC=1 SCAR=6.9
Pod documented
181 0 my ( $self, $label ) = @_;
182 0 return $self->select_grants( 'SELECT g.id, g.zone, g.name, g.types, g.access FROM grants g JOIN tokens t ON t.id = g.token_id WHERE t.label = ? ORDER BY g.zone, g.name, g.id', $label );
183 }
184  
185  
186 0 ▶sub grants_for_token {
Subroutine grants_for_token: not called CC=2 SCAR=17.9
Pod documented
187 0 my ( $self, $secret ) = @_;
188 0 ▶ return () unless $secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $secret 0 0 0
189 0 return $self->select_grants( 'SELECT g.id, g.zone, g.name, g.types, g.access FROM grants g JOIN tokens t ON t.id = g.token_id WHERE t.digest = ? ORDER BY g.id', $self->digest($secret) );
190 }
191  
192  
193 0 ▶sub select_grants {
Subroutine select_grants: not called CC=2 SCAR=17.9
Pod documented
194 0 my ( $self, $sql, @bind ) = @_;
195 0 my $rows = $self->dbh->selectall_arrayref( $sql, { Slice => {} }, @bind );
196 0 $_->{types} = [ split m/,/, $_->{types} ] foreach @$rows;
197 0 return @$rows;
198 }
199  
200  
201 0 ▶sub transaction {
Subroutine transaction: not called CC=2 SCAR=17.9
Pod documented
202 0 my ( $self, $code ) = @_;
203  
204 0 my $dbh = $self->dbh;
205 0 $dbh->begin_work;
206 0 my $ok = eval { $code->(); 1 };
207 0 my $error = $@;
208 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
209 0 $dbh->commit;
210 0 return 1;
211 }
212 0 $dbh->rollback;
213 0 die $error;
214 }
215  
216 2 1;
217  
218 __END__
« ACL/Proxy.pm ↑ Summary