blib/lib/DBIO/AccessBroker/HostBound.pm

filter: Statementstmt 100% 95 / 95 Branchbran 95.0% 38 / 40 Conditioncond 85.2% 29 / 34 MC/DCmcdc 73.6% 14 / 19 Subroutinesub 100% 22 / 22 Podpod 100% 12 / 12 totaltotal 94.5% 210 / 222 CC 34 SCAR 21.8
CC
34
Cov
96%
_bind_host_into_dsn
16.1
_bind_host
13.0
new
6.0
CRAP 8.8
« blib/lib/DBIO/Test/AsyncParity.pm ↑ Summary blib/lib/DBIO/DeploymentHandler/DeployMethod/Native.pm »
1 # ABSTRACT: One credential identity pinned to one host
2 package DBIO::AccessBroker::HostBound;
3  
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 our $VERSION = '0.900007';
8  
9 45 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use base 'DBIO::AccessBroker';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use DBIO::Storage::DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 45 __PACKAGE__->mk_group_accessors('simple' => qw(
17 _broker _host _port
18 ));
19  
20  
21 190 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
22 190 my ($class, %args) = @_;
23  
24 190 my $broker = $args{broker};
25 190 ▶ croak "HostBound requires 'broker'" unless defined $broker;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $broker 2 188 190
26 188 ▶ croak "HostBound 'broker' must be a DBIO::AccessBroker instance"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($broker) and $broker->isa("DBIO::AccessBroker") 4 184 188
Condition100%3/3
builtin::blessed($broker) and $broker->isa("DBIO::AccessBroker")
!ll&&!rl&&r
22184
MC/DC100%2/2
builtin::blessed($broker) and $broker->isa("DBIO::AccessBroker")
builtin::blessed($broker) $broker->isa("DBIO::AccessBroker")
Truth table100%3/3
A and B
A= builtin::blessed($broker)B= $broker->isa("DBIO::AccessBroker")
ABresult
0X0
100
111
27 unless blessed($broker) && $broker->isa('DBIO::AccessBroker');
28 184 ▶ croak "HostBound requires 'host'" unless defined $args{host};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"host"} 6 178 184
29  
30 178 my $self = $class->SUPER::new(%args);
31 178 $self->_broker($broker);
32 178 $self->_host($args{host});
33 178 ▶ $self->_port($args{port}) if defined $args{port};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"port"} 62 116 178
34 178 return $self;
35 }
36  
37  
38 # The wrapped broker is the real CredentialSource. The view holds no
39 # credentials of its own; callers that need the underlying identity
40 # (e.g. to compare two views sharing one lease) read it here.
41 18 ▶sub underlying_broker { $_[0]->_broker }
Subroutine underlying_broker: called CC=1 SCAR=0.0
Pod documented
42  
43 3 ▶sub host { $_[0]->_host }
Subroutine host: called CC=1 SCAR=0.0
Pod documented
44 3 ▶sub port { $_[0]->_port }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
45  
46  
47 # Lifecycle delegates entirely to the wrapped CredentialSource: one lease,
48 # one rotation schedule, shared across every host this credential serves.
49 260 ▶sub needs_refresh { $_[0]->_broker->needs_refresh }
Subroutine needs_refresh: called CC=1 SCAR=0.0
Pod documented
50 8 ▶sub refresh { $_[0]->_broker->refresh }
Subroutine refresh: called CC=1 SCAR=0.0
Pod documented
51 20 ▶sub has_rotating_credentials { $_[0]->_broker->has_rotating_credentials }
Subroutine has_rotating_credentials: called CC=1 SCAR=0.0
Pod documented
52 25 ▶sub is_transaction_safe { $_[0]->_broker->is_transaction_safe }
Subroutine is_transaction_safe: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 # Attaching the view to a storage must also reach the underlying broker, so
56 # storage-aware credential lookups and storage-tied rotation keep working.
57 128 ▶sub set_storage {
Subroutine set_storage: called CC=1 SCAR=0.0
Pod documented
58 128 my ($self, $storage) = @_;
59 128 $self->SUPER::set_storage($storage);
60 128 $self->_broker->set_storage($storage);
61 128 return $self;
62 }
63  
64 4 ▶sub unset_storage {
Subroutine unset_storage: called CC=1 SCAR=0.0
Pod documented
65 4 my ($self, $storage) = @_;
66 4 $self->SUPER::unset_storage($storage);
67 4 $self->_broker->unset_storage($storage);
68 4 return $storage;
69 }
70  
71  
72 37 ▶sub connect_info_for {
Subroutine connect_info_for: called CC=2 SCAR=7.1
Pod documented
73 37 my ($self, $mode) = @_;
74 # The storage whose class may bind the host: the view's one storage, when
75 # it has exactly one. With none or several there is no class to ask.
76 37 my @attached = $self->_attached_storages;
77 37 ▶ return $self->_bind_host(
Branch: 1 branch
BranchTrueFalseTotal
@attached == 1 ? : 0 36 36
78 $self->_broker->connect_info_for($mode), @attached == 1 ? $attached[0] : undef
79 );
80 }
81  
82 265 ▶sub connect_info_for_storage {
Subroutine connect_info_for_storage: called CC=1 SCAR=0.0
Pod documented
83 265 my ($self, $storage, $mode) = @_;
84 265 return $self->_bind_host(
85 $self->_broker->connect_info_for_storage($storage, $mode), $storage
86 );
87 }
88  
89 # Inject this view's host into whatever connect-info shape the wrapped broker
90 # produces, without the broker ever knowing the host list. Two shapes occur
91 # in the wild: the hashref form ({host,port,dbname,...}, e.g. Static) and the
92 # DBI-arrayref form ([$dsn,$user,$pass,\%attrs], e.g. Vault). A DSN string is
93 # bound through the driver seam (_bind_host_into_dsn); what cannot be bound
94 # is refused, naming the form and never the DSN (karr k276).
95 300 ▶sub _bind_host {
Subroutine _bind_host: called CC=13 SCAR=25.7
96 300 my ($self, $info, $storage) = @_;
97 300 my $host = $self->_host;
98 300 my $port = $self->_port;
99  
100 # An empty hash is no answer: binding a host into it would make it one,
101 # and the storage would open a connection with host and port alone instead
102 # of refusing the broker (karr k253). Hand it on as it came.
103 300 ▶ if (ref $info eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "HASH") 142 158 300
104 142 ▶ return $info unless %$info;
Branch: 1 branch
BranchTrueFalseTotal
unless %$info 7 135 142
105  
106 $self->_cannot_bind('dbh_maker: code opens the connection')
107 135 ▶ if defined $info->{dbh_maker};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"dbh_maker"} 1 134 135
108  
109 134 my %bound = %$info;
110 134 ▶ if (defined $bound{dsn} && !ref $bound{dsn} && length $bound{dsn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bound{"dsn"} and not ref $bound{"dsn"} and length $bound{"dsn"}) 56 78 134
Condition83%5/6
defined $bound{"dsn"} and not ref $bound{"dsn"} and length $bound{"dsn"}
!ll&&!rl&&r
70856
defined $bound{"dsn"} and not ref $bound{"dsn"}
!ll&&!rl&&r
70064
MC/DC66%2/3
defined $bound{"dsn"} and not ref $bound{"dsn"} and length $bound{"dsn"}
defined $bound{"dsn"} not ref $bound{"dsn"} length $bound{"dsn"}
Truth table75%3/4
A and not(B) and C
A= defined $bound{"dsn"}B= not ref $bound{"dsn"}C= length $bound{"dsn"}
ABCresult
0XX0
10X0
1100
1111
111 56 $bound{dsn} = $self->_bind_host_into_dsn($bound{dsn}, $storage);
112 56 return \%bound;
113 }
114  
115 78 $bound{host} = $host;
116 78 ▶ $bound{port} = $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 8 70 78
117 78 return \%bound;
118 }
119  
120 158 ▶ if (ref $info eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY") 138 20 158
121 138 my @bound = @$info;
122 138 ▶ if (ref $bound[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bound[0]) { } 1 137 138
elsif (defined $bound[0] and length $bound[0]) { } 116 34 150
123 1 ▶ $self->_cannot_bind('a code reference in place of a DSN')
Branch: 1 branch
BranchTrueFalseTotal
if ref $bound[0] eq "CODE" 1 0 1
124 if ref $bound[0] eq 'CODE';
125 }
126 ▶ elsif (defined $bound[0] && length $bound[0]) {
Condition100%3/3
defined $bound[0] and length $bound[0]
!ll&&!rl&&r
138116
MC/DC100%2/2
defined $bound[0] and length $bound[0]
defined $bound[0] length $bound[0]
Truth table100%3/3
A and B
A= defined $bound[0]B= length $bound[0]
ABresult
0X0
100
111
127 116 $bound[0] = $self->_bind_host_into_dsn($bound[0], $storage);
128 }
129 123 return \@bound;
130 }
131  
132 20 return $info;
133 }
134  
135 # The DSN with this view's host and port bound, by the first driver seam that
136 # is not core's default; else by the default; or a refusal naming the form.
137 172 ▶sub _bind_host_into_dsn {
Subroutine _bind_host_into_dsn: called CC=16 SCAR=27.8
138 172 my ($self, $dsn, $storage) = @_;
139 172 my ($host, $port) = ($self->_host, $self->_port);
140  
141 # Before any seam is asked: a driver never sees a value that would split
142 # the DSN.
143 172 ▶ $self->_cannot_bind("host/port containing ';'")
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /;/ or defined $port and $port =~ /;/ 2 170 172
Condition100%6/6
$host =~ /;/ or defined $port and $port =~ /;/
l!l&&r!l&&!r
11170
defined $port and $port =~ /;/
!ll&&!rl&&r
116541
MC/DC100%3/3
$host =~ /;/ or defined $port and $port =~ /;/
$host =~ /;/ defined $port $port =~ /;/
Truth table100%4/4
A or B and C
A= $host =~ /;/B= defined $portC= $port =~ /;/
ABCresult
1XX1
0111
00X0
0100
144 if $host =~ /;/ || (defined $port && $port =~ /;/);
145  
146 170 my ($dbd) = $dsn =~ /^dbi:([^:(]*)/i;
147 170 ▶ $dbd //= '';
Condition100%2/2
$dbd //= ""
l!l
1691
MC/DC100%1/1
$dbd //= ""
$dbd
Truth table100%2/2
A
A= $dbd
Aresult
11
00
148  
149 170 my $default = \&DBIO::Storage::DBI::_bind_host_into_dsn;
150 170 my @candidates;
151 170 ▶ push @candidates, ref $storage || $storage
Branch: 1 branch
BranchTrueFalseTotal
if defined $storage and builtin::blessed($storage) and $storage->can("_bind_host_into_dsn") 142 28 170
Condition66%6/9
defined $storage and builtin::blessed($storage) and $storage->can("_bind_host_into_dsn")
!ll&&!rl&&r
244142
defined $storage and builtin::blessed($storage)
!ll&&!rl&&r
240146
ref $storage || $storage
l!l&&r!l&&!r
14200
MC/DC66%2/3
defined $storage and builtin::blessed($storage) and $storage->can("_bind_host_into_dsn")
defined $storage builtin::blessed($storage) $storage->can("_bind_host_into_dsn")
MC/DC0%0/2
ref $storage || $storage
ref $storage $storage
Truth table75%3/4
A and B and C
A= defined $storageB= builtin::blessed($storage)C= $storage->can("_bind_host_into_dsn")
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A || B
A= ref $storageB= $storage
ABresult
1X1
011
000
152 if defined $storage && blessed($storage) && $storage->can('_bind_host_into_dsn');
153 170 ▶ if (length $dbd) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dbd) 169 1 170
154 169 my $class = DBIO::Storage::DBI->driver_storage_class($dbd);
155 169 ▶ push @candidates, $class
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and $class->can("_bind_host_into_dsn") 103 66 169
Condition100%3/3
defined $class and $class->can("_bind_host_into_dsn")
!ll&&!rl&&r
4224103
MC/DC100%2/2
defined $class and $class->can("_bind_host_into_dsn")
defined $class $class->can("_bind_host_into_dsn")
Truth table100%3/3
A and B
A= defined $classB= $class->can("_bind_host_into_dsn")
ABresult
0X0
100
111
156 if defined $class && $class->can('_bind_host_into_dsn');
157 }
158  
159 170 my $seam_class = 'DBIO::Storage::DBI';
160 170 for my $candidate (@candidates) {
161 242 ▶ next if $candidate->can('_bind_host_into_dsn') == $default;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->can("_bind_host_into_dsn") == $default 236 6 242
162 6 $seam_class = $candidate;
163 6 last;
164 }
165  
166 170 my ($bound, $form) = $seam_class->_bind_host_into_dsn($dbd, $dsn, $host, $port);
167 170 ▶ return $bound if defined $bound;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bound 158 12 170
168  
169 12 ▶ $self->_cannot_bind($form // 'a DSN form the driver does not name');
Condition50%1/2
$form // "a DSN form the driver does not name"
l!l
120
MC/DC0%0/1
$form // "a DSN form the driver does not name"
$form
Truth table50%1/2
A
A= $form
Aresult
11
00
170 }
171  
172 # Refuse; the form is a key or a dialect, never a value of the answer.
173 16 ▶sub _cannot_bind {
Subroutine _cannot_bind: called CC=1 SCAR=0.0
174 16 my ($self, $form) = @_;
175 16 croak 'HostBound view over ' . ref($self->_broker)
176 . " cannot bind its host into the answer: $form. The storage's driver "
177 . 'seam _bind_host_into_dsn (ADR 0013, addendum 2026-10-09) translates '
178 . 'the DSN forms its DBD takes; give the answer as connection parameters '
179 . 'or a DSN naming its server with host=/port=.';
180 }
181  
182 45 1;
183  
184 __END__
« blib/lib/DBIO/Test/AsyncParity.pm ↑ Summary blib/lib/DBIO/DeploymentHandler/DeployMethod/Native.pm »