blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm

filter: Statementstmt 58.6% 142 / 242 Branchbran 30.3% 37 / 122 Conditioncond 16.8% 18 / 107 MC/DCmcdc 1.7% 1 / 58 Subroutinesub 70.7% 29 / 41 Podpod 75.0% 6 / 8 totaltotal 40.3% 233 / 578 CC 107 SCAR 38.4
CC
107
Cov
42%
refresh
182.0
__ANON__
110.0
_refresh_async
90.0
CRAP 46.5
« lib/Langertha/Skeid/UsageStore.pm ↑ Summary lib/Langertha/Skeid/Protocol/Anthropic.pm »
1 package Langertha::Skeid::KeyBroker::OpenBao;
2 1 our $VERSION = '0.005';
3 # ABSTRACT: OpenBao-backed KeyBroker with AppRole auth or a token file, and token renewal
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 extends 'Langertha::Skeid::KeyBroker';
15  
16  
17  
18 has addr => (
19 is => 'ro',
20 required => 1,
21 0 ▶ default => sub { $ENV{OPENBAO_ADDR} // 'http://127.0.0.1:8200' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$ENV{"OPENBAO_ADDR"} // "http://127.0.0.1:8200"
l!l
00
MC/DC0%0/1
$ENV{"OPENBAO_ADDR"} // "http://127.0.0.1:8200"
$ENV{"OPENBAO_ADDR"}
Truth table0%0/2
A
A= $ENV{"OPENBAO_ADDR"}
Aresult
11
00
22 1 );
23  
24 1 has role_id => (
25 is => 'ro',
26 );
27  
28 1 has secret_id => (
29 is => 'ro',
30 );
31  
32  
33 1 has token_file => (
34 is => 'ro',
35 );
36  
37 # What the token file was when it was last read: mtime, size, inode.
38 6 ▶has _token_file_seen => (is => 'rw', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 has renew_secs => (
41 is => 'rw',
42 6 ▶ default => sub { 300 }, # 5 minutes
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 );
44  
45  
46 has verify_ssl => (
47 is => 'ro',
48 6 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
49 6 ▶ return (defined($ENV{OPENBAO_VERIFY_SSL}) && $ENV{OPENBAO_VERIFY_SSL} =~ /^(0|false|no|off)$/i)
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'OPENBAO_VERIFY_SSL'} && $ENV{'OPENBAO_VERIFY_SSL'} =~ /^(0|false|no|off)$/i ? : 0 6 6
Condition33%1/3
defined $ENV{"OPENBAO_VERIFY_SSL"} and $ENV{"OPENBAO_VERIFY_SSL"} =~ /^(0|false|no|off)$/i
!ll&&!rl&&r
600
MC/DC0%0/2
defined $ENV{"OPENBAO_VERIFY_SSL"} and $ENV{"OPENBAO_VERIFY_SSL"} =~ /^(0|false|no|off)$/i
defined $ENV{"OPENBAO_VERIFY_SSL"} $ENV{"OPENBAO_VERIFY_SSL"} =~ /^(0|false|no|off)$/i
Truth table33%1/3
A and B
A= defined $ENV{"OPENBAO_VERIFY_SSL"}B= $ENV{"OPENBAO_VERIFY_SSL"} =~ /^(0|false|no|off)$/i
ABresult
0X0
100
111
50 ? 0 : 1;
51 },
52 1 );
53  
54 has _http => (
55 is => 'lazy',
56 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 my ($self) = @_;
58 0 HTTP::Tiny->new(
59 timeout => 10,
60 verify_ssl => $self->verify_ssl,
61 );
62 },
63 1 );
64  
65 # The non-blocking client, used on the request path. HTTP::Tiny stays for boot and for callers
66 # with no IO loop (CLI, tests, setup scripts).
67 has _ua => (
68 is => 'lazy',
69 1 ▶ builder => sub {
Subroutine _build__ua: called CC=2 SCAR=7.0
70 1 my ($self) = @_;
71 1 my $ua = Mojo::UserAgent->new;
72 1 $ua->connect_timeout(5);
73 1 $ua->request_timeout(10);
74 1 ▶ $ua->insecure(1) unless $self->verify_ssl;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verify_ssl 0 1 1
75 1 return $ua;
76 },
77 1 );
78  
79 # Callbacks waiting on a token renewal that is already in flight.
80 has _refresh_waiters => (
81 is => 'ro',
82 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 );
84  
85 1 has _token => (
86 is => 'rw',
87 clearer => '_clear_token',
88 );
89  
90 1 has _renew_token => (
91 is => 'rw',
92 clearer => '_clear_renew_token',
93 );
94  
95 has _token_expire => (
96 is => 'rw',
97 6 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 1 );
99  
100 1 has _renewal_loop => (
101 is => 'rw',
102 clearer => '_clear_renewal_loop',
103 );
104  
105 6 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=33.5
Pod undocumented
106 6 my ($self) = @_;
107 6 ▶ if (defined($self->token_file) && length($self->token_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->token_file and length $self->token_file) 6 0 6
Condition33%1/3
defined $self->token_file and length $self->token_file
!ll&&!rl&&r
006
MC/DC0%0/2
defined $self->token_file and length $self->token_file
defined $self->token_file length $self->token_file
Truth table33%1/3
A and B
A= defined $self->token_fileB= length $self->token_file
ABresult
0X0
100
111
108 6 ▶ die "OpenBao: token_file and role_id/secret_id exclude each other; give one\n"
Branch: 1 branch
BranchTrueFalseTotal
if length($self->role_id // "") or length($self->secret_id // "") 1 5 6
Condition71%5/7
length($self->role_id // "") or length($self->secret_id // "")
l!l&&r!l&&!r
105
$self->role_id // ""
l!l
15
$self->secret_id // ""
l!l
05
MC/DC50%1/2
length($self->role_id // "") or length($self->secret_id // "")
$self->role_id $self->secret_id
Truth table66%2/3
A or B
A= $self->role_idB= $self->secret_id
ABresult
1X1
011
000
109 if length($self->role_id // '') || length($self->secret_id // '');
110 5 my $error = $self->_read_token_file;
111 5 ▶ die "$error\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 3 2 5
112 2 return;
113 }
114 0 ▶ die "OpenBao: role_id and secret_id are required (or token_file)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->role_id // "") and length($self->secret_id // "") 0 0 0
Condition0%0/7
length($self->role_id // "") and length($self->secret_id // "")
!ll&&!rl&&r
000
$self->role_id // ""
l!l
00
$self->secret_id // ""
l!l
00
MC/DC0%0/2
length($self->role_id // "") and length($self->secret_id // "")
$self->role_id $self->secret_id
Truth table0%0/3
A and B
A= $self->role_idB= $self->secret_id
ABresult
0X0
100
111
115 unless length($self->role_id // '') && length($self->secret_id // '');
116 # Initial token fetch
117 0 $self->_fetch_token;
118 }
119  
120 7 ▶sub _uses_token_file { defined($_[0]->token_file) && length($_[0]->token_file) }
Subroutine _uses_token_file: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->token_file and length $_[0]->token_file 7 0 7
121  
122 17 ▶sub _token_file_stamp {
Subroutine _token_file_stamp: called CC=2 SCAR=6.9
123 17 my ($self) = @_;
124 # Sub-second mtime: a token rewritten in place within the same second is still a change.
125 17 my @st = Time::HiRes::stat($self->token_file);
126 17 ▶ return @st ? join(':', @st[9, 7, 1]) : 'missing';
Branch: 1 branch
BranchTrueFalseTotal
@st ? : 13 4 17
127 }
128  
129 # Reads the token file into the token. Undef when it did, else why not (the path, never the
130 # token). A file that cannot be read leaves the token as it was.
131 10 ▶sub _read_token_file {
Subroutine _read_token_file: called CC=6 SCAR=18.0
132 10 my ($self) = @_;
133 10 my $path = $self->token_file;
134 10 my $stamp = $self->_token_file_stamp;
135 10 $self->_token_file_seen($stamp);
136 10 ▶ return "OpenBao token file $path is missing" if $stamp eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $stamp eq "missing" 3 7 10
137 7 ▶ open(my $fh, '<', $path) or return "OpenBao token file $path cannot be read: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return "OpenBao token file $path cannot be read: $!" 7 0 7
138 7 my $token = do { local $/; <$fh> };
139 7 close $fh;
140 7 ▶ $token = '' unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 7 7
141 7 $token =~ s/\A\s+//;
142 7 $token =~ s/\s+\z//;
143 7 ▶ return "OpenBao token file $path is empty" unless length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 2 5 7
144 5 ▶ return "OpenBao token file $path holds more than one line" if $token =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /[\r\n]/ 0 5 5
145 5 $self->_token($token);
146 5 $self->_renew_token($token);
147 # The starter renews; this process only notices when the file changes.
148 5 $self->_token_expire(9**9**9);
149 5 return undef;
150 }
151  
152 # A changed token file is read again; a failed read warns and keeps the token in use.
153 6 ▶sub _reread_token_file {
Subroutine _reread_token_file: called CC=3 SCAR=11.0
154 6 my ($self) = @_;
155 6 ▶ return unless $self->_token_file_stamp ne $self->_token_file_seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_token_file_stamp ne $self->_token_file_seen 2 4 6
156 4 my $error = $self->_read_token_file;
157 4 ▶ warn "$error; keeping the token read before\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 2 4
158 4 return;
159 }
160  
161 0 ▶sub _fetch_token {
Subroutine _fetch_token: not called CC=8 SCAR=42.8
162 0 my ($self) = @_;
163 0 my $resp = $self->_http->post(
164 0 "${\$self->addr}/v1/auth/approle/login",
165 {
166 headers => { 'Content-Type' => 'application/json' },
167 content => encode_json({
168 role_id => $self->role_id,
169 secret_id => $self->secret_id,
170 }),
171 },
172 );
173  
174 0 ▶ unless ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
175 0 ▶ die "OpenBao AppRole login failed: " . ($resp->{content} // 'unknown error');
Condition0%0/2
$resp->{"content"} // "unknown error"
l!l
00
MC/DC0%0/1
$resp->{"content"} // "unknown error"
$resp->{"content"}
Truth table0%0/2
A
A= $resp->{"content"}
Aresult
11
00
176 }
177  
178 0 ▶ my $data = decode_json($resp->{content})->{auth} // {};
Condition0%0/2
&decode_json($resp->{"content"})->{"auth"} // {}
l!l
00
MC/DC0%0/1
&decode_json($resp->{"content"})->{"auth"} // {}
&decode_json($resp->{"content"})->{"auth"}
Truth table0%0/2
A
A= &decode_json($resp->{"content"})->{"auth"}
Aresult
11
00
179 0 ▶ my $token = $data->{client_token} // '';
Condition0%0/2
$data->{"client_token"} // ""
l!l
00
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
Aresult
11
00
180 0 ▶ my $ttl = $data->{ttl} // 0;
Condition0%0/2
$data->{"ttl"} // 0
l!l
00
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
Aresult
11
00
181  
182 0 ▶ die "OpenBao AppRole login returned no token" unless length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 0 0 0
183  
184 # The token from AppRole login is ONLY for renew-self
185 # Store it as _renew_token - we'll use it to get the actual API token
186 0 $self->_renew_token($token);
187 0 ▶ $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
Branch: 1 branch
BranchTrueFalseTotal
$ttl > 0 ? : 0 0 0
188  
189 0 return 1;
190 }
191  
192  
193 6 ▶sub needs_refresh {
Subroutine needs_refresh: called CC=5 SCAR=23.4
Pod documented
194 6 my ($self) = @_;
195 6 ▶ if ($self->_uses_token_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_uses_token_file) 6 0 6
196 6 $self->_reread_token_file;
197 6 ▶ return $self->_token ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_token ? : 6 0 6
198 }
199 # Refresh if: time is near expiry, OR we have no client_token yet
200 0 ▶ return 1 if time >= ($self->_token_expire - 60);
Branch: 1 branch
BranchTrueFalseTotal
if time >= $self->_token_expire - 60 0 0 0
201 0 ▶ return 1 unless $self->_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_token 0 0 0
202 0 return 0;
203 }
204  
205  
206 0 ▶sub refresh {
Subroutine refresh: not called CC=13 SCAR=52.0
Pod documented
207 0 my ($self) = @_;
208 0 ▶ if ($self->_uses_token_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_uses_token_file) 0 0 0
209 0 my $error = $self->_read_token_file;
210 0 ▶ die "$error\n" if defined($error) && !$self->_token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error and not $self->_token 0 0 0
Condition0%0/3
defined $error and not $self->_token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error and not $self->_token
defined $error not $self->_token
Truth table0%0/3
A and not(B)
A= defined $errorB= not $self->_token
ABresult
0X0
100
111
211 0 ▶ warn "$error; keeping the token read before\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
212 0 return 1;
213 }
214 # Token renewal via renew-self endpoint
215 # _token holds the RENEW token (from initial AppRole login or previous renew-self)
216 # renew-self returns a NEW client_token for API calls AND a new renew token
217 0 ▶ my $renew_token = $self->_token // $self->_renew_token;
Condition0%0/3
$self->_token // $self->_renew_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_token // $self->_renew_token
$self->_token $self->_renew_token
Truth table0%0/3
A // B
A= $self->_tokenB= $self->_renew_token
ABresult
1X1
011
000
218 0 ▶ unless ($renew_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($renew_token) 0 0 0
219 0 die "No renew token available - call _fetch_token first";
220 }
221  
222 0 my $resp = $self->_http->post(
223 0 "${\$self->addr}/v1/auth/token/renew-self",
224 {
225 headers => {
226 'Content-Type' => 'application/json',
227 'X-Vault-Token' => $renew_token,
228 },
229 content => encode_json({}),
230 },
231 );
232  
233 0 ▶ if ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"success"}) 0 0 0
234 0 ▶ my $data = decode_json($resp->{content})->{auth} // {};
Condition0%0/2
&decode_json($resp->{"content"})->{"auth"} // {}
l!l
00
MC/DC0%0/1
&decode_json($resp->{"content"})->{"auth"} // {}
&decode_json($resp->{"content"})->{"auth"}
Truth table0%0/2
A
A= &decode_json($resp->{"content"})->{"auth"}
Aresult
11
00
235 0 ▶ my $new_client_token = $data->{client_token} // '';
Condition0%0/2
$data->{"client_token"} // ""
l!l
00
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
Aresult
11
00
236 0 ▶ my $ttl = $data->{ttl} // 0;
Condition0%0/2
$data->{"ttl"} // 0
l!l
00
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
Aresult
11
00
237  
238 0 ▶ die "renew-self returned no client_token" unless length $new_client_token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $new_client_token 0 0 0
239  
240 # Store the new client_token for API calls
241 0 $self->_token($new_client_token);
242  
243 # Store a new renew token (same as client_token in Vault's default behavior)
244 # The renew_token is good for the lease_duration, then we need to renew again
245 0 $self->_renew_token($new_client_token);
246 0 ▶ $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
Branch: 1 branch
BranchTrueFalseTotal
$ttl > 0 ? : 0 0 0
247  
248 0 return 1;
249 }
250  
251 # Renewal failed - need new AppRole credentials
252 0 die "OpenBao token renewal failed. Container must be restarted to get new AppRole credential.";
253 }
254  
255 # KV-v2 reads go through secret/data/. Callers name the logical path
256 # ("secret/skeid/remote/groq"); the API path is "secret/data/skeid/remote/groq".
257 6 ▶sub _kv_path {
Subroutine _kv_path: called CC=1 SCAR=0.0
258 6 my ($self, $ref) = @_;
259 6 $ref =~ s{^/+}{};
260 6 $ref =~ s{^secret/?}{secret/data/};
261 6 return $ref;
262 }
263  
264 6 ▶sub _key_from_payload {
Subroutine _key_from_payload: called CC=6 SCAR=20.7
265 6 my ($self, $payload) = @_;
266 6 ▶ my $decoded = eval { decode_json($payload // '') } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $decoded = eval { do { &decode_json(scalar($payload // "")) } } or return undef 6 0 6
Condition50%1/2
$payload // ""
l!l
60
MC/DC0%0/1
$payload // ""
$payload
Truth table50%1/2
A
A= $payload
Aresult
11
00
267 6 ▶ my $data = $decoded->{data} // {};
Condition50%1/2
$decoded->{"data"} // {}
l!l
60
MC/DC0%0/1
$decoded->{"data"} // {}
$decoded->{"data"}
Truth table50%1/2
A
A= $decoded->{"data"}
Aresult
11
00
268 6 ▶ return $data->{data}{api_key} // $data->{api_key} // undef;
Condition40%2/5
($data->{"data"}{"api_key"} // $data->{"api_key"}) // undef
l!l
60
$data->{"data"}{"api_key"} // $data->{"api_key"}
l!l&&r!l&&!r
600
MC/DC0%0/2
($data->{"data"}{"api_key"} // $data->{"api_key"}) // undef
$data->{"data"}{"api_key"} $data->{"api_key"}
Truth table33%1/3
A // B
A= $data->{"data"}{"api_key"}B= $data->{"api_key"}
ABresult
1X1
011
000
269 }
270  
271  
272 0 ▶sub resolve_key {
Subroutine resolve_key: not called CC=7 SCAR=40.3
Pod documented
273 0 my ($self, $ref) = @_;
274 0 ▶ return undef unless defined $ref && length $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 0 0
Condition0%0/3
defined $ref and length $ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ref and length $ref
defined $ref length $ref
Truth table0%0/3
A and B
A= defined $refB= length $ref
ABresult
0X0
100
111
275  
276 # Refresh token if needed
277 0 ▶ $self->refresh if $self->needs_refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->needs_refresh 0 0 0
278  
279 0 ▶ my $token = $self->_token or die "No OpenBao token";
Branch: 1 branch
BranchTrueFalseTotal
my $token = $self->_token or die "No OpenBao token" 0 0 0
280  
281 0 my $path = $self->_kv_path($ref);
282 0 my $resp = $self->_http->get(
283 0 "${\$self->addr}/v1/$path",
284 {
285 headers => { 'X-Vault-Token' => $token },
286 },
287 );
288  
289 0 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
290 # Only the status: a response body from a vault read is the last thing that belongs in a
291 # log line, and the reference alone is enough to find the misconfiguration.
292 0 ▶ warn "OpenBao read failed for '$ref': HTTP " . ($resp->{status} // '???');
Condition0%0/2
$resp->{"status"} // "???"
l!l
00
MC/DC0%0/1
$resp->{"status"} // "???"
$resp->{"status"}
Truth table0%0/2
A
A= $resp->{"status"}
Aresult
11
00
293 0 return undef;
294 }
295  
296 0 return $self->_key_from_payload($resp->{content});
297 }
298  
299  
300 6 ▶sub resolve_key_async {
Subroutine resolve_key_async: called CC=6 SCAR=20.9
Pod documented
301 6 my ($self, $ref, $cb) = @_;
302 6 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$cb ||= sub { }
l!l&&r!l&&!r
600
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table33%1/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
303 6 ▶ return $cb->(undef, 'no key reference') unless defined($ref) && length($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref 0 6 6
Condition33%1/3
defined $ref and length $ref
!ll&&!rl&&r
006
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
304 6 ▶ return $self->SUPER::resolve_key_async($ref, $cb) unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 6 6
305  
306 6 ▶ my $read = sub {
Subroutine __ANON__: called CC=3 SCAR=12.9
307 6 my ($token, $error) = @_;
308 6 ▶ return $cb->(undef, $error) unless defined($token) && length($token);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 0 6 6
Condition33%1/3
defined $token and length $token
!ll&&!rl&&r
006
MC/DC0%0/2
defined $token and length $token
defined $token length $token
Truth table33%1/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
309  
310 $self->_ua->get(
311 6 ▶ $self->addr . '/v1/' . $self->_kv_path($ref) => { 'X-Vault-Token' => $token } => sub {
Subroutine __ANON__: called CC=7 SCAR=23.8
312 6 my (undef, $tx) = @_;
313 6 my $res = $tx->res;
314 6 ▶ my $status = $res->code // 0;
Condition50%1/2
$res->code // 0
l!l
60
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
315 6 ▶ return $cb->(undef, "OpenBao read failed for '$ref': HTTP " . ($status || 'no response'))
Branch: 1 branch
BranchTrueFalseTotal
unless $status >= 200 and $status < 300 0 6 6
Condition20%1/5
$status >= 200 and $status < 300
!ll&&!rl&&r
006
$status || 'no response'
l!l
00
MC/DC0%0/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
MC/DC0%0/1
$status || 'no response'
$status
Truth table33%1/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
Truth table0%0/2
A
A= $status
Aresult
11
00
316 unless $status >= 200 && $status < 300;
317  
318 6 my $key = $self->_key_from_payload($res->body);
319 6 ▶ return $cb->(undef, "OpenBao returned no api_key for '$ref'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 0 6 6
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
006
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
320 unless defined($key) && length($key);
321 6 $cb->($key, undef);
322 }
323 6 );
324 6 };
325  
326 6 ▶ return $self->_refresh_async($read) if $self->needs_refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->needs_refresh 0 6 6
327 6 $read->($self->_token);
328 6 return;
329 }
330  
331 # Renews the token without blocking, and only once at a time: a cold start at concurrency 64
332 # would otherwise fire 64 renew-self calls, and vault would answer them with 64 different
333 # tokens, 63 of which are immediately stale.
334 0 ▶sub _refresh_async {
Subroutine _refresh_async: not called CC=9 SCAR=45.0
335 0 my ($self, $cb) = @_;
336 0 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$cb ||= sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table0%0/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
337  
338 # A token file is a local read (a tmpfs, a few bytes), not a round trip.
339 0 ▶ if ($self->_uses_token_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_uses_token_file) 0 0 0
340 0 my $error = $self->_read_token_file;
341 0 ▶ return $cb->(undef, $error) if defined($error) && !$self->_token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error and not $self->_token 0 0 0
Condition0%0/3
defined $error and not $self->_token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error and not $self->_token
defined $error not $self->_token
Truth table0%0/3
A and not(B)
A= defined $errorB= not $self->_token
ABresult
0X0
100
111
342 0 ▶ warn "$error; keeping the token read before\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
343 0 return $cb->($self->_token, undef);
344 }
345  
346 0 my $waiters = $self->_refresh_waiters;
347 0 push @$waiters, $cb;
348 0 ▶ return if @$waiters > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$waiters > 1 0 0 0
349  
350 0 ▶ my $settle = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
351 0 my ($token, $error) = @_;
352 0 my @waiting = splice @$waiters;
353 0 $_->($token, $error) for @waiting;
354 0 };
355  
356 0 ▶ my $renew_token = $self->_token // $self->_renew_token;
Condition0%0/3
$self->_token // $self->_renew_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_token // $self->_renew_token
$self->_token $self->_renew_token
Truth table0%0/3
A // B
A= $self->_tokenB= $self->_renew_token
ABresult
1X1
011
000
357 0 ▶ return $settle->(undef, 'No renew token available') unless $renew_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $renew_token 0 0 0
358  
359 $self->_ua->post(
360 $self->addr . '/v1/auth/token/renew-self' => {
361 'Content-Type' => 'application/json',
362 'X-Vault-Token' => $renew_token,
363 0 ▶ } => encode_json({}) => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
364 0 my (undef, $tx) = @_;
365 0 ▶ my $status = $tx->res->code // 0;
Condition0%0/2
$tx->res->code // 0
l!l
00
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table0%0/2
A
A= $tx->res->code
Aresult
11
00
366 0 ▶ return $settle->(undef, 'OpenBao token renewal failed: HTTP ' . ($status || 'no response'))
Branch: 1 branch
BranchTrueFalseTotal
unless $status >= 200 and $status < 300 0 0 0
Condition0%0/5
$status >= 200 and $status < 300
!ll&&!rl&&r
000
$status || 'no response'
l!l
00
MC/DC0%0/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
MC/DC0%0/1
$status || 'no response'
$status
Truth table0%0/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
Truth table0%0/2
A
A= $status
Aresult
11
00
367 unless $status >= 200 && $status < 300;
368  
369 0 ▶ my $data = eval { decode_json($tx->res->body)->{auth} } // {};
Condition0%0/2
eval { do { &decode_json(scalar $tx->res->body)->{"auth"} } } // {}
l!l
00
MC/DC0%0/1
eval { do { &decode_json(scalar $tx->res->body)->{"auth"} } } // {}
eval { do { &decode_json(scalar $tx->res->body)->{"auth"} } }
Truth table0%0/2
A
A= eval { do { &decode_json(scalar $tx->res->body)->{"auth"} } }
Aresult
11
00
370 0 ▶ my $token = $data->{client_token} // '';
Condition0%0/2
$data->{"client_token"} // ""
l!l
00
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
Aresult
11
00
371 0 ▶ return $settle->(undef, 'renew-self returned no client_token') unless length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 0 0 0
372  
373 0 ▶ my $ttl = $data->{ttl} // 0;
Condition0%0/2
$data->{"ttl"} // 0
l!l
00
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
Aresult
11
00
374 0 $self->_token($token);
375 0 $self->_renew_token($token);
376 0 ▶ $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
Branch: 1 branch
BranchTrueFalseTotal
$ttl > 0 ? : 0 0 0
377 0 $settle->($token, undef);
378 }
379 0 );
380 0 return;
381 }
382  
383  
384 2 ▶sub start_renewal {
Subroutine start_renewal: called CC=4 SCAR=16.9
Pod documented
385 2 my ($self) = @_;
386 2 ▶ return $self->_renewal_loop if $self->_renewal_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_renewal_loop 0 2 2
387  
388 2 my $every = $self->renew_secs;
389 2 ▶ $every = 60 if !$every || $every < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $every or $every < 1 0 2 2
Condition33%1/3
not $every or $every < 1
l!l&&r!l&&!r
002
MC/DC0%0/2
not $every or $every < 1
not $every $every < 1
Truth table33%1/3
not(A) or B
A= not $everyB= $every < 1
ABresult
1X1
011
000
390  
391 # Weak, or the timer's closure keeps the broker alive forever and DEMOLISH never runs -- which
392 # is how a token outlives the object that was supposed to be the only thing holding it.
393 2 my $weak = $self;
394 2 weaken($weak);
395  
396 1 ▶ my $id = Mojo::IOLoop->recurring($every => sub {
Subroutine __ANON__: called CC=4 SCAR=20.1
397 1 ▶ my $broker = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $broker = $weak or return 1 0 1
398 # With a token file the starter renews; the timer reads the file again whatever its stamp
399 # says, so a change no stamp shows is picked up within one interval.
400 1 ▶ if ($broker->_uses_token_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($broker->_uses_token_file) 1 0 1
401 1 my $error = $broker->_read_token_file;
402 1 ▶ warn "$error; keeping the token read before\n" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 1 1
403 1 return;
404 }
405 0 ▶ $broker->_refresh_async(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
406 0 my ($token, $error) = @_;
407 0 ▶ return unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 0 0
408 0 ▶ die "OpenBao token renewal failed and the token has expired: $error"
Branch: 1 branch
BranchTrueFalseTotal
if time >= $broker->_token_expire 0 0 0
409 if time >= $broker->_token_expire;
410 0 warn "OpenBao token renewal failed (token still valid): $error";
411 0 });
412 2 });
413 2 $self->_renewal_loop($id);
414 2 return $id;
415 }
416  
417  
418 0 ▶sub list_secrets {
Subroutine list_secrets: not called CC=9 SCAR=45.0
Pod documented
419 0 my ($self, $path) = @_;
420 0 ▶ return [] unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
421  
422 0 ▶ $self->refresh if $self->needs_refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->needs_refresh 0 0 0
423  
424 0 ▶ my $token = $self->_token or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $token = $self->_token or return [] 0 0 0
425 0 $path =~ s{^/+}{};
426 0 ▶ $path .= '/' unless $path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[/$] 0 0 0
427  
428 0 my $resp = $self->_http->list(
429 0 "${\$self->addr}/v1/$path",
430 {
431 headers => { 'X-Vault-Token' => $token },
432 },
433 );
434  
435 0 ▶ return [] unless $resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 0 0 0
436  
437 0 ▶ my $data = decode_json($resp->{content})->{data} // {};
Condition0%0/2
&decode_json($resp->{"content"})->{"data"} // {}
l!l
00
MC/DC0%0/1
&decode_json($resp->{"content"})->{"data"} // {}
&decode_json($resp->{"content"})->{"data"}
Truth table0%0/2
A
A= &decode_json($resp->{"content"})->{"data"}
Aresult
11
00
438 0 ▶ return [ map { $_->{key} } @{$data->{keys} // []} ];
Condition0%0/2
$data->{"keys"} // []
l!l
00
MC/DC0%0/1
$data->{"keys"} // []
$data->{"keys"}
Truth table0%0/2
A
A= $data->{"keys"}
Aresult
11
00
439 }
440  
441 5 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
442 5 my ($self) = @_;
443 5 ▶ if (my $id = $self->_renewal_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_renewal_loop) 1 4 5
444 1 eval { Mojo::IOLoop->remove($id) };
445 1 $self->_clear_renewal_loop;
446 }
447 5 $self->forget_key;
448 5 $self->_clear_token;
449 5 $self->_clear_renew_token;
450 }
451  
452 1 1;
453  
454 __END__
« lib/Langertha/Skeid/UsageStore.pm ↑ Summary lib/Langertha/Skeid/Protocol/Anthropic.pm »