| 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"
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"}
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $ENV{"OPENBAO_VERIFY_SSL"} and $ENV{"OPENBAO_VERIFY_SSL"} =~ /^(0|false|no|off)$/i
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $self->token_file and length $self->token_file
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
| |||||||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | die "OpenBao: token_file and role_id/secret_id exclude each other; give one\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition71%5/7
length($self->role_id // "") or length($self->secret_id // "")
$self->role_id // ""
$self->secret_id // ""
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 112 | 2 | return; | |||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | die "OpenBao: role_id and secret_id are required (or token_file)\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/7
length($self->role_id // "") and length($self->secret_id // "")
$self->role_id // ""
$self->secret_id // ""
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | open(my $fh, '<', $path) or return "OpenBao token file $path cannot be read: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 138 | 7 | my $token = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||
| 139 | 7 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | $token = '' unless defined $token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 144 | 5 | ▶ | return "OpenBao token file $path holds more than one line" if $token =~ /[\r\n]/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 156 | 4 | my $error = $self->_read_token_file; | |||||||||||||||||||||||||||||||||||||
| 157 | 4 | ▶ | warn "$error; keeping the token read before\n" if defined $error; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | die "OpenBao AppRole login failed: " . ($resp->{content} // 'unknown error'); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$resp->{"content"} // "unknown error"
MC/DC0%0/1
$resp->{"content"} // "unknown error"
$resp->{"content"}
Truth table0%0/2
A
A= $resp->{"content"}
| |||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | my $data = decode_json($resp->{content})->{auth} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&decode_json($resp->{"content"})->{"auth"} // {}
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"}
| |||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $token = $data->{client_token} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"client_token"} // ""
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
| |||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $ttl = $data->{ttl} // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"ttl"} // 0
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
| |||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | die "OpenBao AppRole login returned no token" unless length $token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 196 | 6 | $self->_reread_token_file; | |||||||||||||||||||||||||||||||||||||
| 197 | 6 | ▶ | return $self->_token ? 0 : 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return 1 unless $self->_token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 209 | 0 | my $error = $self->_read_token_file; | |||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | die "$error\n" if defined($error) && !$self->_token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and not $self->_token
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
| |||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | warn "$error; keeping the token read before\n" if defined $error; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | unless ($renew_token) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my $data = decode_json($resp->{content})->{auth} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&decode_json($resp->{"content"})->{"auth"} // {}
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"}
| |||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | my $new_client_token = $data->{client_token} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"client_token"} // ""
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
| |||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | my $ttl = $data->{ttl} // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"ttl"} // 0
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
| |||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | die "renew-self returned no client_token" unless length $new_client_token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$payload // ""
MC/DC0%0/1
$payload // ""
$payload
Truth table50%1/2
A
A= $payload
| |||||||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | my $data = $decoded->{data} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$decoded->{"data"} // {}
MC/DC0%0/1
$decoded->{"data"} // {}
$decoded->{"data"}
Truth table50%1/2
A
A= $decoded->{"data"}
| |||||||||||||||||||||||||||||||||||||||
| 268 | 6 | ▶ | return $data->{data}{api_key} // $data->{api_key} // undef; | ||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($data->{"data"}{"api_key"} // $data->{"api_key"}) // undef
$data->{"data"}{"api_key"} // $data->{"api_key"}
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"}
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $ref and length $ref
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
| |||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | # Refresh token if needed | ||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | $self->refresh if $self->needs_refresh; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | my $token = $self->_token or die "No OpenBao token"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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"} // "???"
MC/DC0%0/1
$resp->{"status"} // "???"
$resp->{"status"}
Truth table0%0/2
A
A= $resp->{"status"}
| |||||||||||||||||||||||||||||||||||||||
| 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 {
}
MC/DC0%0/2
$cb ||= sub {
}
$cb
sub {
}
Truth table33%1/3
A ||= B
A= $cbB= sub {
}
| |||||||||||||||||||||||||||||||||||||||
| 303 | 6 | ▶ | return $cb->(undef, 'no key reference') unless defined($ref) && length($ref); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ref and length $ref
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
| |||||||||||||||||||||||||||||||||||||||
| 304 | 6 | ▶ | return $self->SUPER::resolve_key_async($ref, $cb) unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $token and length $token
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
| |||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
| |||||||||||||||||||||||||||||||||||||||
| 315 | 6 | ▶ | return $cb->(undef, "OpenBao read failed for '$ref': HTTP " . ($status || 'no response')) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$status >= 200 and $status < 300
$status || 'no response'
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
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $key and length $key
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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 {
}
MC/DC0%0/2
$cb ||= sub {
}
$cb
sub {
}
Truth table0%0/3
A ||= B
A= $cbB= sub {
}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my $error = $self->_read_token_file; | |||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | return $cb->(undef, $error) if defined($error) && !$self->_token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and not $self->_token
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
| |||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | warn "$error; keeping the token read before\n" if defined $error; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | return $settle->(undef, 'No renew token available') unless $renew_token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table0%0/2
A
A= $tx->res->code
| |||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | return $settle->(undef, 'OpenBao token renewal failed: HTTP ' . ($status || 'no response')) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$status >= 200 and $status < 300
$status || 'no response'
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
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||||||||||||
| 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"}
}
} // {}
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"}
}
}
| |||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | my $token = $data->{client_token} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"client_token"} // ""
MC/DC0%0/1
$data->{"client_token"} // ""
$data->{"client_token"}
Truth table0%0/2
A
A= $data->{"client_token"}
| |||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | return $settle->(undef, 'renew-self returned no client_token') unless length $token; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | my $ttl = $data->{ttl} // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"ttl"} // 0
MC/DC0%0/1
$data->{"ttl"} // 0
$data->{"ttl"}
Truth table0%0/2
A
A= $data->{"ttl"}
| |||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $self->_token($token); | |||||||||||||||||||||||||||||||||||||
| 375 | 0 | $self->_renew_token($token); | |||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | $self->_token_expire(time + ($ttl > 0 ? $ttl : 3600)); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||
| 388 | 2 | my $every = $self->renew_secs; | |||||||||||||||||||||||||||||||||||||
| 389 | 2 | ▶ | $every = 60 if !$every || $every < 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $every or $every < 1
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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 401 | 1 | my $error = $broker->_read_token_file; | |||||||||||||||||||||||||||||||||||||
| 402 | 1 | ▶ | warn "$error; keeping the token read before\n" if defined $error; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | die "OpenBao token renewal failed and the token has expired: $error" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $path and length $path
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
| |||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | $self->refresh if $self->needs_refresh; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | my $token = $self->_token or return []; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 425 | 0 | $path =~ s{^/+}{}; | |||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | $path .= '/' unless $path =~ m{/$}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | my $data = decode_json($resp->{content})->{data} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&decode_json($resp->{"content"})->{"data"} // {}
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"}
| |||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | return [ map { $_->{key} } @{$data->{keys} // []} ]; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"keys"} // []
MC/DC0%0/1
$data->{"keys"} // []
$data->{"keys"}
Truth table0%0/2
A
A= $data->{"keys"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||