blib/lib/DBIO/AccessBroker/Vault.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 14 / 14 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 100% 108 / 108 CC 17 SCAR 18.6
CC
17
Cov
100%
_fetch_credentials
11.0
new
7.0
BEGIN
1.0
CRAP 6.4
« blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/VersionResult.pm ↑ Summary blib/lib/DBIO/HashAccessor.pm »
1 # ABSTRACT: Credential rotation with TTL
2 package DBIO::AccessBroker::Vault;
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.900007';
8  
9 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use base 'DBIO::AccessBroker';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 __PACKAGE__->mk_group_accessors('simple' => qw(
15 vault dsn dbi_attrs cred_path ttl refresh_margin
16 _current_username _current_password _expires_at
17 ));
18  
19  
20  
21 64 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
22 64 my ($class, %args) = @_;
23 64 my $self = $class->SUPER::new(%args);
24  
25 64 ▶ $self->vault($args{vault}) // croak "Vault broker requires 'vault'";
Condition100%2/2
$self->vault($args{"vault"}) // croak("Vault broker requires 'vault'")
l!l
631
MC/DC100%1/1
$self->vault($args{"vault"}) // croak("Vault broker requires 'vault'")
$self->vault($args{"vault"})
Truth table100%2/2
A
A= $self->vault($args{"vault"})
Aresult
11
00
26 63 ▶ $self->dsn($args{dsn}) // croak "Vault broker requires 'dsn'";
Condition100%2/2
$self->dsn($args{"dsn"}) // croak("Vault broker requires 'dsn'")
l!l
621
MC/DC100%1/1
$self->dsn($args{"dsn"}) // croak("Vault broker requires 'dsn'")
$self->dsn($args{"dsn"})
Truth table100%2/2
A
A= $self->dsn($args{"dsn"})
Aresult
11
00
27 62 ▶ $self->cred_path($args{cred_path}) // croak "Vault broker requires 'cred_path'";
Condition100%2/2
$self->cred_path($args{"cred_path"}) // croak("Vault broker requires 'cred_path'")
l!l
611
MC/DC100%1/1
$self->cred_path($args{"cred_path"}) // croak("Vault broker requires 'cred_path'")
$self->cred_path($args{"cred_path"})
Truth table100%2/2
A
A= $self->cred_path($args{"cred_path"})
Aresult
11
00
28 61 ▶ $self->ttl($args{ttl} // 3600);
Condition100%2/2
$args{"ttl"} // 3600
l!l
556
MC/DC100%1/1
$args{"ttl"} // 3600
$args{"ttl"}
Truth table100%2/2
A
A= $args{"ttl"}
Aresult
11
00
29 61 ▶ $self->refresh_margin($args{refresh_margin} // 900); # 15 min before expiry
Condition100%2/2
$args{"refresh_margin"} // 900
l!l
556
MC/DC100%1/1
$args{"refresh_margin"} // 900
$args{"refresh_margin"}
Truth table100%2/2
A
A= $args{"refresh_margin"}
Aresult
11
00
30 61 ▶ $self->dbi_attrs($args{dbi_attrs} // {});
Condition100%2/2
$args{"dbi_attrs"} // {}
l!l
1348
MC/DC100%1/1
$args{"dbi_attrs"} // {}
$args{"dbi_attrs"}
Truth table100%2/2
A
A= $args{"dbi_attrs"}
Aresult
11
00
31 61 $self->_expires_at(0);
32  
33 # Fetch initial credentials
34 61 $self->_fetch_credentials;
35  
36 45 return $self;
37 }
38  
39 # Reads the secret and takes its username and password. A secret that lacks a
40 # defined username or a defined password (an empty string is a value) -- nothing,
41 # an empty hash, a hash with other keys only, a half secret -- or that is
42 # no hash croaks, naming cred_path and the shape, never a value: a plain string
43 # from a vault may be the secret itself (karr k270). It croaks before anything
44 # is replaced, so a failed refresh leaves the credentials of before whole.
45 98 ▶sub _fetch_credentials {
Subroutine _fetch_credentials: called CC=11 SCAR=24.0
46 98 my ($self) = @_;
47 98 my $creds = $self->vault->read_secret($self->cred_path);
48 84 ▶ croak "Vault returned no credentials for " . $self->cred_path unless $creds;
Branch: 1 branch
BranchTrueFalseTotal
unless $creds 3 81 84
49  
50 81 ▶ unless ((reftype($creds) // '') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((builtin::reftype($creds) // "") eq "HASH") 8 73 81
Condition100%2/2
builtin::reftype($creds) // ""
l!l
792
MC/DC100%1/1
builtin::reftype($creds) // ""
builtin::reftype($creds)
Truth table100%2/2
A
A= builtin::reftype($creds)
Aresult
11
00
51 8 ▶ my $shape = !ref $creds ? 'a plain scalar'
Branch: 3 branches
BranchTrueFalseTotal
!ref($creds) ? : 2 6 8
builtin::blessed($creds) ? : 2 4 6
ref $creds eq 'ARRAY' ? : 2 2 4
52 : blessed($creds) ? 'an object of class ' . ref $creds
53 : ref $creds eq 'ARRAY' ? 'an ARRAY reference'
54 : 'a ' . ref($creds) . ' reference';
55 8 croak 'Vault returned credentials for ' . $self->cred_path
56 . " that are not a hash ($shape)";
57 }
58  
59 croak "Vault returned no credentials for " . $self->cred_path
60 . ' (a hash with neither a username nor a password)'
61 73 ▶ unless defined $creds->{username} or defined $creds->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $creds->{"username"} or defined $creds->{"password"} 8 65 73
Condition100%3/3
defined $creds->{"username"} or defined $creds->{"password"}
l!l&&r!l&&!r
6148
MC/DC100%2/2
defined $creds->{"username"} or defined $creds->{"password"}
defined $creds->{"username"} defined $creds->{"password"}
Truth table100%3/3
A or B
A= defined $creds->{"username"}B= defined $creds->{"password"}
ABresult
1X1
011
000
62  
63 65 for my $key (qw(username password)) {
64 croak "Vault returned incomplete credentials for " . $self->cred_path
65 . " (the hash has no defined $key)"
66 126 ▶ unless defined $creds->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $creds->{$key} 8 118 126
67 }
68  
69 57 $self->_current_username($creds->{username});
70 57 $self->_current_password($creds->{password});
71 57 $self->_expires_at(time() + $self->ttl);
72 }
73  
74  
75 115 ▶sub connect_info_for {
Subroutine connect_info_for: called CC=1 SCAR=0.0
Pod documented
76 115 my ($self, $mode) = @_;
77 115 return [$self->dsn, $self->_current_username, $self->_current_password, $self->dbi_attrs];
78 }
79  
80  
81 131 ▶sub needs_refresh {
Subroutine needs_refresh: called CC=1 SCAR=0.0
Pod documented
82 131 my ($self) = @_;
83 131 return time() > ($self->_expires_at - $self->refresh_margin);
84 }
85  
86  
87 37 ▶sub refresh {
Subroutine refresh: called CC=1 SCAR=0.0
Pod documented
88 37 my ($self) = @_;
89 37 $self->_fetch_credentials;
90 }
91  
92  
93 35 ▶sub has_rotating_credentials { 1 }
Subroutine has_rotating_credentials: called CC=1 SCAR=0.0
Pod documented
94  
95 7 1;
96  
97 __END__
« blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/VersionResult.pm ↑ Summary blib/lib/DBIO/HashAccessor.pm »