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
« blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm ↑ Summary blib/lib/Langertha/Skeid/Secret.pm »
1 package Langertha::Skeid::KeyBroker;
2 5 our $VERSION = '0.003';
3 # ABSTRACT: Pluggable API key resolution for Skeid nodes
4 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9  
10 has cache_ttl => (
11 is => 'rw',
12 76 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 5 );
14  
15  
16 has negative_cache_ttl => (
17 is => 'rw',
18 76 ▶ default => sub { 5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 5 );
20  
21 # ref => { key => $key_or_undef, error => $msg_or_undef, expires => $epoch }
22 has _cache => (
23 is => 'ro',
24 77 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 5 );
26  
27 # ref => [ @callbacks ] for a resolution currently in flight
28 has _pending => (
29 is => 'ro',
30 77 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 5 );
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 93 ▶sub key_async {
Subroutine key_async: called CC=6 SCAR=18.3
Pod documented
41 93 my ($self, $ref, $cb) = @_;
42 93 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub {
l!l&&r!l&&!r
9300
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
43 93 ▶ return $cb->(undef, 'no key reference') unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 93 93
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
0093
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 93 ▶ if (my $hit = $self->cached_key($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hit = $self->cached_key($ref)) 10 83 93
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 83 my $pending = $self->_pending;
54 83 ▶ if ($pending->{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending->{$ref}) 4 79 83
55 4 push @{$pending->{$ref}}, $cb;
56 4 return;
57 }
58 79 $pending->{$ref} = [$cb];
59  
60 78 ▶ $self->resolve_key_async($ref, sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
61 78 my ($key, $error) = @_;
62 78 ▶ $key = undef unless defined($key) && length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 34 44 78
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
34044
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 78 $self->_cache_result($ref, $key, $error);
64 78 ▶ my $waiting = delete($pending->{$ref}) || [];
Condition50%1/2
delete $pending->{$ref} || []
l!l
780
MC/DC0%0/1
delete $pending->{$ref} || []
delete $pending->{$ref}
Truth table50%1/2
A
A= delete $pending->{$ref}
Aresult
11
00
65 78 $_->($key, $error) for @$waiting;
66 79 });
67 79 return;
68 }
69  
70  
71 70 ▶sub resolve_key_async {
Subroutine resolve_key_async: called CC=6 SCAR=18.0
Pod documented
72 70 my ($self, $ref, $cb) = @_;
73 70 my $key = eval { $self->resolve_key($ref) };
74 70 ▶ my $error = $@ ? do { my $e = "$@"; $e =~ s/\s+\z//; $e } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 31 39 70
75 70 ▶ $key = undef unless defined($key) && length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 31 39 70
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
31039
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 70 ▶ $error //= 'no key resolved' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 31 39 70
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 70 $cb->($key, $error);
78 70 return;
79 }
80  
81  
82 95 ▶sub cached_key {
Subroutine cached_key: called CC=5 SCAR=18.4
Pod documented
83 95 my ($self, $ref) = @_;
84 95 ▶ return unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 95 95
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
0095
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 95 ▶ my $hit = $self->_cache->{$ref} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $hit = $self->_cache->{$ref} or return 11 84 95
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 78 ▶sub _cache_result {
Subroutine _cache_result: called CC=3 SCAR=11.0
105 78 my ($self, $ref, $key, $error) = @_;
106 78 ▶ my $ttl = defined($key) ? $self->cache_ttl : $self->negative_cache_ttl;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 44 34 78
107 78 ▶ return unless $ttl > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $ttl > 0 1 77 78
108 77 $self->_cache->{$ref} = { key => $key, error => $error, expires => time + $ttl };
109 77 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 5 1;
120  
121 __END__
122  
123 =pod
124  
125 =encoding UTF-8
126  
127 =head1 NAME
128  
129 Langertha::Skeid::KeyBroker - Pluggable API key resolution for Skeid nodes
130  
131 =head1 VERSION
132  
133 version 0.003
134  
135 =head1 DESCRIPTION
136  
137 The contract that turns a key reference (C<secret/skeid/remote/groq>) into a secret, in memory,
138 at the moment it is needed. L<Langertha::Skeid::KeyBroker::OpenBao> is the implementation;
139 this class is what code depends on.
140  
141 Implement L</resolve_key> in a subclass. Everything else — caching, request coalescing, the
142 non-blocking entry point — is provided here, so a three-line broker gets them for free.
143  
144 =head2 Two entry points, and which one is yours
145  
146 L</key_async> is the B<only> one the request path may call. It answers from cache without
147 touching the loop, and a miss is one round-trip however many requests are waiting on it.
148  
149 L</resolve_key> is what a subclass implements and what off-loop code (CLI, tests, setup
150 scripts) may call directly. Calling it from a Mojolicious handler blocks every other in-flight
151 request for the duration of the vault round-trip — see ADR 0005.
152  
153 =head2 cache_ttl
154  
155 How long a resolved key stays in memory, in seconds (default 300, 0 disables caching).
156 Memory only: ADR 0003 forbids a disk cache, not this one. The cost of a stale key is one
157 failed upstream call after a rotation; the cost of no cache is a vault round-trip on the
158 request path, per request.
159  
160 =head2 negative_cache_ttl
161  
162 How long a failed resolution is remembered, in seconds (default 5, 0 disables). Short on
163 purpose: it exists so a vault outage costs one round-trip per reference per few seconds
164 instead of one per request, not to make a fixed misconfiguration stick.
165  
166 =head2 resolve_key
167  
168 my $key = $broker->resolve_key('secret/skeid/remote/groq');
169  
170 What a subclass implements: resolve one reference, return the secret, die or return undef when
171 it cannot. Blocking. Never call it from a request handler — call L</key_async>.
172  
173 =head2 key_async
174  
175 $broker->key_async($ref, sub {
176 my ($key, $error) = @_;
177 ...
178 });
179  
180 The request path's entry point. The callback always runs exactly once, with the key, or with
181 undef and a message. A cached answer calls back immediately and synchronously.
182  
183 =head2 resolve_key_async
184  
185 $broker->resolve_key_async($ref, sub { my ($key, $error) = @_; ... });
186  
187 The non-blocking half of L</resolve_key>, for subclasses that can do better than blocking. The
188 default implementation calls C<resolve_key> and hands the result to the callback, which keeps
189 a simple broker correct — but keeps it blocking. L<Langertha::Skeid::KeyBroker::OpenBao>
190 overrides it.
191  
192 Callers want L</key_async>: this one is uncached and uncoalesced.
193  
194 =head2 cached_key
195  
196 my $hit = $broker->cached_key($ref); # { key => …, error => … } or nothing
197  
198 The live cache entry for a reference, or nothing when there is none or it has expired.
199 Distinguishes "cached failure" from "not cached" — both would look like undef otherwise.
200  
201 =head2 forget_key
202  
203 $broker->forget_key($ref); # one reference
204 $broker->forget_key; # all of them
205  
206 Drops cached resolutions, so the next request resolves again. What a key rotation calls.
207  
208 =head2 needs_refresh
209  
210 True when the broker's own credential has to be renewed before the next resolution. Always 0
211 here; L<Langertha::Skeid::KeyBroker::OpenBao> overrides it.
212  
213 =head2 refresh
214  
215 Renews the broker's own credential. A no-op here; L<Langertha::Skeid::KeyBroker::OpenBao>
216 overrides it.
217  
218 =head1 SEE ALSO
219  
220 L<Langertha::Skeid::KeyBroker::OpenBao>, L<Langertha::Skeid/key_broker>, and ADR 0003 (secrets
221 live in memory only) in the distribution repository.
222  
223 =head1 SUPPORT
224  
225 =head2 Issues
226  
227 Please report bugs and feature requests on GitHub at
228 L<https://github.com/Getty/langertha-skeid/issues>.
229  
230 =head2 IRC
231  
232 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
233  
234 =head1 CONTRIBUTING
235  
236 Contributions are welcome! Please fork the repository and submit a pull request.
237  
238 =head1 AUTHOR
239  
240 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2026 by Torsten Raudssus.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
« blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm ↑ Summary blib/lib/Langertha/Skeid/Secret.pm »