blib/lib/Langertha/Skeid/KeyBroker.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 84.6% 22 / 26 Conditioncond 45.4% 10 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 88.2% 15 / 17 Podpod 100% 7 / 7 totaltotal 77.7% 122 / 157 CC 23 SCAR 14.3
CC
23
Cov
82%
cached_key
6.3
key_async
6.2
resolve_key_async
6.0
CRAP 4.2
« lib/Langertha/Skeid/SwaggerUI.pm ↑ Summary lib/Langertha/Skeid/Secret.pm »
1 package Langertha::Skeid::KeyBroker;
2 6 our $VERSION = '0.004';
3 # ABSTRACT: Pluggable API key resolution for Skeid nodes
4 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9  
10 has cache_ttl => (
11 is => 'rw',
12 95 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 6 );
14  
15  
16 has negative_cache_ttl => (
17 is => 'rw',
18 95 ▶ default => sub { 5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 6 );
20  
21 # ref => { key => $key_or_undef, error => $msg_or_undef, expires => $epoch }
22 has _cache => (
23 is => 'ro',
24 96 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 6 );
26  
27 # ref => [ @callbacks ] for a resolution currently in flight
28 has _pending => (
29 is => 'ro',
30 96 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 6 );
32  
33  
34 1 ▶sub resolve_key {
Subroutine resolve_key: called CC=1 SCAR=0.0
Pod documented
35 1 my ($self, $ref) = @_;
36 1 croak ref($self) . " must implement resolve_key()";
37 }
38  
39  
40 94 ▶sub key_async {
Subroutine key_async: called CC=6 SCAR=18.3
Pod documented
41 94 my ($self, $ref, $cb) = @_;
42 94 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub { }
l!l&&r!l&&!r
9400
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
43 94 ▶ return $cb->(undef, 'no key reference') unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 94 94
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
0094
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
44  
45 94 ▶ if (my $hit = $self->cached_key($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hit = $self->cached_key($ref)) 10 84 94
46 10 $cb->($hit->{key}, $hit->{error});
47 10 return;
48 }
49  
50 # One round-trip per reference, however many requests are waiting on it. Without this, a cold
51 # cache at concurrency 64 is 64 identical vault calls -- and the moment a key expires, every
52 # in-flight request misses at once.
53 84 my $pending = $self->_pending;
54 84 ▶ if ($pending->{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending->{$ref}) 4 80 84
55 4 push @{$pending->{$ref}}, $cb;
56 4 return;
57 }
58 80 $pending->{$ref} = [$cb];
59  
60 79 ▶ $self->resolve_key_async($ref, sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
61 79 my ($key, $error) = @_;
62 79 ▶ $key = undef unless defined($key) && length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 34 45 79
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
34045
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
63 79 $self->_cache_result($ref, $key, $error);
64 79 ▶ my $waiting = delete($pending->{$ref}) || [];
Condition50%1/2
delete $pending->{$ref} || []
l!l
790
MC/DC0%0/1
delete $pending->{$ref} || []
delete $pending->{$ref}
Truth table50%1/2
A
A= delete $pending->{$ref}
Aresult
11
00
65 79 $_->($key, $error) for @$waiting;
66 80 });
67 80 return;
68 }
69  
70  
71 71 ▶sub resolve_key_async {
Subroutine resolve_key_async: called CC=6 SCAR=18.0
Pod documented
72 71 my ($self, $ref, $cb) = @_;
73 71 my $key = eval { $self->resolve_key($ref) };
74 71 ▶ my $error = $@ ? do { my $e = "$@"; $e =~ s/\s+\z//; $e } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 31 40 71
75 71 ▶ $key = undef unless defined($key) && length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 31 40 71
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
31040
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
76 71 ▶ $error //= 'no key resolved' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 31 40 71
Condition50%1/2
$error //= "no key resolved"
l!l
310
MC/DC0%0/1
$error //= "no key resolved"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
77 71 $cb->($key, $error);
78 71 return;
79 }
80  
81  
82 96 ▶sub cached_key {
Subroutine cached_key: called CC=5 SCAR=18.4
Pod documented
83 96 my ($self, $ref) = @_;
84 96 ▶ return unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 96 96
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
0096
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
85 96 ▶ my $hit = $self->_cache->{$ref} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $hit = $self->_cache->{$ref} or return 11 85 96
86 11 ▶ if ($hit->{expires} <= time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit->{"expires"} <= time) 0 11 11
87 0 delete $self->_cache->{$ref};
88 0 return;
89 }
90 11 return $hit;
91 }
92  
93  
94 1 ▶sub forget_key {
Subroutine forget_key: called CC=3 SCAR=14.2
Pod documented
95 1 my ($self, $ref) = @_;
96 1 ▶ if (defined($ref) && length($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref and length $ref) 1 0 1
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
001
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table33%1/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
97 1 delete $self->_cache->{$ref};
98 1 return 1;
99 }
100 0 %{$self->_cache} = ();
101 0 return 1;
102 }
103  
104 79 ▶sub _cache_result {
Subroutine _cache_result: called CC=3 SCAR=11.0
105 79 my ($self, $ref, $key, $error) = @_;
106 79 ▶ my $ttl = defined($key) ? $self->cache_ttl : $self->negative_cache_ttl;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 45 34 79
107 79 ▶ return unless $ttl > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $ttl > 0 1 78 79
108 78 $self->_cache->{$ref} = { key => $key, error => $error, expires => time + $ttl };
109 78 return;
110 }
111  
112  
113 1 ▶sub needs_refresh { 0 }
Subroutine needs_refresh: called CC=1 SCAR=0.0
Pod documented
114  
115  
116 0 ▶sub refresh { }
Subroutine refresh: not called CC=1 SCAR=0.0
Pod documented
117  
118  
119 6 1;
120  
121 __END__
« lib/Langertha/Skeid/SwaggerUI.pm ↑ Summary lib/Langertha/Skeid/Secret.pm »