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

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 435 CC 33 SCAR 32.4
CC
33
Cov
0%
resolve_key_async
56.0
_refresh_async
42.0
refresh
30.0
CRAP 25.5
1 package Langertha::Skeid::KeyBroker::OpenBao;
2 our $VERSION = '0.003';
3 # ABSTRACT: OpenBao-backed KeyBroker with AppRole auth and token renewal
4 use Moo;
5 use HTTP::Tiny;
6 use Mojo::IOLoop;
7 use Mojo::UserAgent;
8 use JSON::MaybeXS qw(decode_json encode_json);
9 use POSIX qw(strftime);
10 use Scalar::Util qw(weaken);
11 use namespace::clean;
12  
13 extends 'Langertha::Skeid::KeyBroker';
14  
15  
16  
17 has addr => (
18 is => 'ro',
19 required => 1,
20 default => sub { $ENV{OPENBAO_ADDR} // 'http://127.0.0.1:8200' },
21 );
22  
23 has role_id => (
24 is => 'ro',
25 required => 1,
26 );
27  
28 has secret_id => (
29 is => 'ro',
30 required => 1,
31 );
32  
33 has renew_secs => (
34 is => 'rw',
35 default => sub { 300 }, # 5 minutes
36 );
37  
38  
39 has verify_ssl => (
40 is => 'ro',
41 default => sub {
42 return (defined($ENV{OPENBAO_VERIFY_SSL}) && $ENV{OPENBAO_VERIFY_SSL} =~ /^(0|false|no|off)$/i)
43 ? 0 : 1;
44 },
45 );
46  
47 has _http => (
48 is => 'lazy',
49 builder => sub {
50 my ($self) = @_;
51 HTTP::Tiny->new(
52 timeout => 10,
53 verify_ssl => $self->verify_ssl,
54 );
55 },
56 );
57  
58 # The non-blocking client, used on the request path. HTTP::Tiny stays for boot and for callers
59 # with no IO loop (CLI, tests, setup scripts).
60 has _ua => (
61 is => 'lazy',
62 builder => sub {
63 my ($self) = @_;
64 my $ua = Mojo::UserAgent->new;
65 $ua->connect_timeout(5);
66 $ua->request_timeout(10);
67 $ua->insecure(1) unless $self->verify_ssl;
68 return $ua;
69 },
70 );
71  
72 # Callbacks waiting on a token renewal that is already in flight.
73 has _refresh_waiters => (
74 is => 'ro',
75 default => sub { [] },
76 );
77  
78 has _token => (
79 is => 'rw',
80 clearer => '_clear_token',
81 );
82  
83 has _renew_token => (
84 is => 'rw',
85 clearer => '_clear_renew_token',
86 );
87  
88 has _token_expire => (
89 is => 'rw',
90 default => sub { 0 },
91 );
92  
93 has _renewal_loop => (
94 is => 'rw',
95 clearer => '_clear_renewal_loop',
96 );
97  
98 sub BUILD {
99 my ($self) = @_;
100 # Initial token fetch
101 $self->_fetch_token;
102 }
103  
104 sub _fetch_token {
105 my ($self) = @_;
106 my $resp = $self->_http->post(
107 "${\$self->addr}/v1/auth/approle/login",
108 {
109 headers => { 'Content-Type' => 'application/json' },
110 content => encode_json({
111 role_id => $self->role_id,
112 secret_id => $self->secret_id,
113 }),
114 },
115 );
116  
117 unless ($resp->{success}) {
118 die "OpenBao AppRole login failed: " . ($resp->{content} // 'unknown error');
119 }
120  
121 my $data = decode_json($resp->{content})->{auth} // {};
122 my $token = $data->{client_token} // '';
123 my $ttl = $data->{ttl} // 0;
124  
125 die "OpenBao AppRole login returned no token" unless length $token;
126  
127 # The token from AppRole login is ONLY for renew-self
128 # Store it as _renew_token - we'll use it to get the actual API token
129 $self->_renew_token($token);
130 $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
131  
132 return 1;
133 }
134  
135  
136 sub needs_refresh {
137 my ($self) = @_;
138 # Refresh if: time is near expiry, OR we have no client_token yet
139 return 1 if time >= ($self->_token_expire - 60);
140 return 1 unless $self->_token;
141 return 0;
142 }
143  
144  
145 sub refresh {
146 my ($self) = @_;
147 # Token renewal via renew-self endpoint
148 # _token holds the RENEW token (from initial AppRole login or previous renew-self)
149 # renew-self returns a NEW client_token for API calls AND a new renew token
150 my $renew_token = $self->_token // $self->_renew_token;
151 unless ($renew_token) {
152 die "No renew token available - call _fetch_token first";
153 }
154  
155 my $resp = $self->_http->post(
156 "${\$self->addr}/v1/auth/token/renew-self",
157 {
158 headers => {
159 'Content-Type' => 'application/json',
160 'X-Vault-Token' => $renew_token,
161 },
162 content => encode_json({}),
163 },
164 );
165  
166 if ($resp->{success}) {
167 my $data = decode_json($resp->{content})->{auth} // {};
168 my $new_client_token = $data->{client_token} // '';
169 my $ttl = $data->{ttl} // 0;
170  
171 die "renew-self returned no client_token" unless length $new_client_token;
172  
173 # Store the new client_token for API calls
174 $self->_token($new_client_token);
175  
176 # Store a new renew token (same as client_token in Vault's default behavior)
177 # The renew_token is good for the lease_duration, then we need to renew again
178 $self->_renew_token($new_client_token);
179 $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
180  
181 return 1;
182 }
183  
184 # Renewal failed - need new AppRole credentials
185 die "OpenBao token renewal failed. Container must be restarted to get new AppRole credential.";
186 }
187  
188 # KV-v2 reads go through secret/data/. Callers name the logical path
189 # ("secret/skeid/remote/groq"); the API path is "secret/data/skeid/remote/groq".
190 sub _kv_path {
191 my ($self, $ref) = @_;
192 $ref =~ s{^/+}{};
193 $ref =~ s{^secret/?}{secret/data/};
194 return $ref;
195 }
196  
197 sub _key_from_payload {
198 my ($self, $payload) = @_;
199 my $decoded = eval { decode_json($payload // '') } or return undef;
200 my $data = $decoded->{data} // {};
201 return $data->{data}{api_key} // $data->{api_key} // undef;
202 }
203  
204  
205 sub resolve_key {
206 my ($self, $ref) = @_;
207 return undef unless defined $ref && length $ref;
208  
209 # Refresh token if needed
210 $self->refresh if $self->needs_refresh;
211  
212 my $token = $self->_token or die "No OpenBao token";
213  
214 my $path = $self->_kv_path($ref);
215 my $resp = $self->_http->get(
216 "${\$self->addr}/v1/$path",
217 {
218 headers => { 'X-Vault-Token' => $token },
219 },
220 );
221  
222 if (!$resp->{success}) {
223 # Only the status: a response body from a vault read is the last thing that belongs in a
224 # log line, and the reference alone is enough to find the misconfiguration.
225 warn "OpenBao read failed for '$ref': HTTP " . ($resp->{status} // '???');
226 return undef;
227 }
228  
229 return $self->_key_from_payload($resp->{content});
230 }
231  
232  
233 sub resolve_key_async {
234 my ($self, $ref, $cb) = @_;
235 $cb ||= sub { };
236 return $cb->(undef, 'no key reference') unless defined($ref) && length($ref);
237 return $self->SUPER::resolve_key_async($ref, $cb) unless Mojo::IOLoop->is_running;
238  
239 my $read = sub {
240 my ($token, $error) = @_;
241 return $cb->(undef, $error) unless defined($token) && length($token);
242  
243 $self->_ua->get(
244 $self->addr . '/v1/' . $self->_kv_path($ref) => { 'X-Vault-Token' => $token } => sub {
245 my (undef, $tx) = @_;
246 my $res = $tx->res;
247 my $status = $res->code // 0;
248 return $cb->(undef, "OpenBao read failed for '$ref': HTTP " . ($status || 'no response'))
249 unless $status >= 200 && $status < 300;
250  
251 my $key = $self->_key_from_payload($res->body);
252 return $cb->(undef, "OpenBao returned no api_key for '$ref'")
253 unless defined($key) && length($key);
254 $cb->($key, undef);
255 }
256 );
257 };
258  
259 return $self->_refresh_async($read) if $self->needs_refresh;
260 $read->($self->_token);
261 return;
262 }
263  
264 # Renews the token without blocking, and only once at a time: a cold start at concurrency 64
265 # would otherwise fire 64 renew-self calls, and vault would answer them with 64 different
266 # tokens, 63 of which are immediately stale.
267 sub _refresh_async {
268 my ($self, $cb) = @_;
269 $cb ||= sub { };
270  
271 my $waiters = $self->_refresh_waiters;
272 push @$waiters, $cb;
273 return if @$waiters > 1;
274  
275 my $settle = sub {
276 my ($token, $error) = @_;
277 my @waiting = splice @$waiters;
278 $_->($token, $error) for @waiting;
279 };
280  
281 my $renew_token = $self->_token // $self->_renew_token;
282 return $settle->(undef, 'No renew token available') unless $renew_token;
283  
284 $self->_ua->post(
285 $self->addr . '/v1/auth/token/renew-self' => {
286 'Content-Type' => 'application/json',
287 'X-Vault-Token' => $renew_token,
288 } => encode_json({}) => sub {
289 my (undef, $tx) = @_;
290 my $status = $tx->res->code // 0;
291 return $settle->(undef, 'OpenBao token renewal failed: HTTP ' . ($status || 'no response'))
292 unless $status >= 200 && $status < 300;
293  
294 my $data = eval { decode_json($tx->res->body)->{auth} } // {};
295 my $token = $data->{client_token} // '';
296 return $settle->(undef, 'renew-self returned no client_token') unless length $token;
297  
298 my $ttl = $data->{ttl} // 0;
299 $self->_token($token);
300 $self->_renew_token($token);
301 $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600));
302 $settle->($token, undef);
303 }
304 );
305 return;
306 }
307  
308  
309 sub start_renewal {
310 my ($self) = @_;
311 return $self->_renewal_loop if $self->_renewal_loop;
312  
313 my $every = $self->renew_secs;
314 $every = 60 if !$every || $every < 1;
315  
316 # Weak, or the timer's closure keeps the broker alive forever and DEMOLISH never runs -- which
317 # is how a token outlives the object that was supposed to be the only thing holding it.
318 my $weak = $self;
319 weaken($weak);
320  
321 my $id = Mojo::IOLoop->recurring($every => sub {
322 my $broker = $weak or return;
323 $broker->_refresh_async(sub {
324 my ($token, $error) = @_;
325 return unless defined $error;
326 die "OpenBao token renewal failed and the token has expired: $error"
327 if time >= $broker->_token_expire;
328 warn "OpenBao token renewal failed (token still valid): $error";
329 });
330 });
331 $self->_renewal_loop($id);
332 return $id;
333 }
334  
335  
336 sub list_secrets {
337 my ($self, $path) = @_;
338 return [] unless defined $path && length $path;
339  
340 $self->refresh if $self->needs_refresh;
341  
342 my $token = $self->_token or return [];
343 $path =~ s{^/+}{};
344 $path .= '/' unless $path =~ m{/$};
345  
346 my $resp = $self->_http->list(
347 "${\$self->addr}/v1/$path",
348 {
349 headers => { 'X-Vault-Token' => $token },
350 },
351 );
352  
353 return [] unless $resp->{success};
354  
355 my $data = decode_json($resp->{content})->{data} // {};
356 return [ map { $_->{key} } @{$data->{keys} // []} ];
357 }
358  
359 sub DEMOLISH {
360 my ($self) = @_;
361 if (my $id = $self->_renewal_loop) {
362 eval { Mojo::IOLoop->remove($id) };
363 $self->_clear_renewal_loop;
364 }
365 $self->forget_key;
366 $self->_clear_token;
367 $self->_clear_renew_token;
368 }
369  
370 1;
371  
372 __END__
373  
374 =pod
375  
376 =encoding UTF-8
377  
378 =head1 NAME
379  
380 Langertha::Skeid::KeyBroker::OpenBao - OpenBao-backed KeyBroker with AppRole auth and token renewal
381  
382 =head1 VERSION
383  
384 version 0.003
385  
386 =head1 SYNOPSIS
387  
388 my $broker = Langertha::Skeid::KeyBroker::OpenBao->new(
389 addr => $ENV{OPENBAO_ADDR} // 'http://127.0.0.1:8200',
390 role_id => $ENV{OPENBAO_ROLE_ID},
391 secret_id => $ENV{OPENBAO_SECRET_ID},
392 renew_secs => 300, # renew token every 5 minutes
393 );
394  
395 my $api_key = $broker->resolve_key('secret/skeid/remote/openai'); # blocking; CLI and tests
396  
397 # the request path
398 $broker->key_async('secret/skeid/remote/openai', sub { my ($key, $error) = @_; ... });
399  
400 =head2 addr
401  
402 The OpenBao address (default C<OPENBAO_ADDR>, else C<http://127.0.0.1:8200>).
403  
404 =head2 role_id
405  
406 Required. The AppRole role id, injected at container start (C<OPENBAO_ROLE_ID>).
407  
408 =head2 secret_id
409  
410 Required. The AppRole secret id, injected at container start (C<OPENBAO_SECRET_ID>). Held in
411 memory only.
412  
413 =head2 renew_secs
414  
415 How often L</start_renewal> renews the token, in seconds (default 300; below 1 means 60).
416  
417 =head2 verify_ssl
418  
419 Whether to verify the OpenBao server's TLS certificate (default 1, override with
420 C<OPENBAO_VERIFY_SSL=0>). Turning it off on an C<https://> address means anything that can
421 intercept the connection can hand out this process's AppRole token and every secret it
422 resolves. It exists for a dev vault with a self-signed certificate and nothing else.
423  
424 =head2 needs_refresh
425  
426 True when there is no client token yet or it expires within the next 60 seconds.
427  
428 =head2 refresh
429  
430 Renews the token through C<renew-self>, blocking. Dies when there is no token to renew with or
431 the renewal fails -- the AppRole credential is then spent and the process has to restart. The
432 request path renews through L</resolve_key_async> instead.
433  
434 =head2 resolve_key
435  
436 my $key = $broker->resolve_key('secret/skeid/remote/groq');
437  
438 Reads the KV-v2 secret the reference names and returns its C<api_key> field, renewing the token
439 first when it is due. C<secret/skeid/remote/groq> is read from C<secret/data/skeid/remote/groq>.
440 Blocking -- never call it from a request handler. Returns undef for an empty reference, a failed
441 read (warned with the reference and the HTTP status only) or a secret without C<api_key>.
442  
443 =head2 resolve_key_async
444  
445 Non-blocking resolution against OpenBao, renewing the token first when it is due. Used by
446 L<Langertha::Skeid::KeyBroker/key_async>, which is what the request path calls; a cache hit
447 never gets here.
448  
449 With no IO loop running there is nothing to yield to, so this falls back to the blocking path
450 — that is the CLI and the test suite, not the proxy.
451  
452 =head2 start_renewal
453  
454 $broker->start_renewal;
455  
456 Renews the token on a timer instead of waiting for a request to notice it expired, so the
457 request path finds a valid token already there. Called by
458 L<Langertha::Skeid::Proxy/build_app>; safe to call twice.
459  
460 A failed renewal warns while the current token is still valid — a blip is not worth dropping
461 in-flight requests for. Once it has actually expired the process dies, because a Skeid that
462 cannot resolve keys is not a Skeid that should keep answering: the container restarts and logs
463 in again with its AppRole credentials (ADR 0003).
464  
465 =head2 list_secrets
466  
467 my $names = $broker->list_secrets('secret/metadata/skeid/remote');
468  
469 The keys under a path, via a blocking C<LIST>. The path is the API path as given -- for KV-v2
470 that is C<secret/metadata/...>; no C<secret/> rewriting happens here. Returns an empty arrayref
471 on any failure. For setup scripts, not the request path.
472  
473 =head1 DESCRIPTION
474  
475 This KeyBroker implementation fetches API keys from OpenBao KV-v2 secrets. Security model:
476  
477 =over 4
478  
479 =item * AppRole credentials (C<role_id> + C<secret_id>) are injected at container start.
480 Construction logs in with them, blocking, and dies when the login fails.
481  
482 =item * The login token is used only to call C<renew-self>; the first resolution does that and
483 gets the client token for API calls.
484  
485 =item * C<renew-self> returns a new client token, which is also the next renew token.
486  
487 =item * The token is renewed every C<renew_secs> seconds via C<renew-self> once
488 L</start_renewal> runs.
489  
490 =item * If renewal fails after the token expired, the process dies, so the container restarts
491 and logs in again.
492  
493 =item * No secret is ever written to disk. Tokens and resolved keys live only in memory, and are
494 dropped when the broker is destroyed.
495  
496 =back
497  
498 =head1 SEE ALSO
499  
500 L<Langertha::Skeid::KeyBroker>, L<Langertha::Skeid::Proxy/build_app> (which builds this broker
501 from C<OPENBAO_ROLE_ID>, C<OPENBAO_SECRET_ID> and C<OPENBAO_ADDR>)
502  
503 =cut
504  
505 =head1 SUPPORT
506  
507 =head2 Issues
508  
509 Please report bugs and feature requests on GitHub at
510 L<https://github.com/Getty/langertha-skeid/issues>.
511  
512 =head2 IRC
513  
514 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
515  
516 =head1 CONTRIBUTING
517  
518 Contributions are welcome! Please fork the repository and submit a pull request.
519  
520 =head1 AUTHOR
521  
522 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
523  
524 =head1 COPYRIGHT AND LICENSE
525  
526 This software is copyright (c) 2026 by Torsten Raudssus.
527  
528 This is free software; you can redistribute it and/or modify it under
529 the same terms as the Perl 5 programming language system itself.
530  
531 =cut