blib/lib/DBIO/AccessBroker.pm

filter: Statementstmt 100% 75 / 75 Branchbran 88.4% 23 / 26 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 95.4% 21 / 22 Podpod 100% 12 / 12 totaltotal 92.5% 150 / 162 CC 22 SCAR 11.5
CC
22
Cov
94%
current_connect_info_for
6.0
set_storage
5.3
_attached_storages
4.0
CRAP 3.2
« blib/lib/DBIO/Storage/DateTimeFormat.pm ↑ Summary blib/lib/DBIO/Timestamp.pm »
1 # ABSTRACT: Credential lifecycle for DBIO connections
2 package DBIO::AccessBroker;
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 weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # A CredentialSource: supplies connect info for exactly one backend identity.
16 # It provides credentials — it does NOT route. Read/write routing and the
17 # host topology belong to DBIO::Replicated, never here.
18 #
19 # Storage-agnostic: works with both Storage::DBI and Storage::Async. The
20 # primary interface is connect_info_for_storage($storage), which returns
21 # storage-native connection parameters. Legacy connect_info_for() remains
22 # available for DBI-shaped broker subclasses.
23 #
24 # Subclasses must implement:
25 # connect_info_for_storage($storage) — returns storage-native connect info
26 # connect_info_for() — legacy DBI-shaped connect info
27 # needs_refresh() — returns true if credentials need rotation
28 # refresh() — perform credential rotation
29 #
30 # The $mode argument ('read'/'write') is vestigial: under a single-identity
31 # CredentialSource there is nothing to route on. It is accepted for
32 # backward compatibility and ignored by all built-in brokers.
33  
34 45 ▶use Class::Accessor::Grouped;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 45 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # HostBound is a subclass of this class, so it can only be compiled once this
38 # package is itself a Class::Accessor::Grouped subclass — hence loaded here,
39 # after the line above, not at the top of the file. for_host() builds one.
40 45 ▶use DBIO::AccessBroker::HostBound ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 324 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
43 324 my ($class, %args) = @_;
44 324 my $self = bless {}, $class;
45 324 return $self;
46 }
47  
48  
49 # Set by Storage when broker is attached
50 824 ▶sub set_storage {
Subroutine set_storage: called CC=5 SCAR=16.7
Pod documented
51 824 my ($self, $storage) = @_;
52 824 ▶ if (ref $storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $storage) 824 0 824
53 824 ▶ my $refs = $self->{_attached_storages} ||= {};
Condition100%2/2
$self->{"_attached_storages"} ||= {}
l!l
54770
MC/DC100%1/1
$self->{"_attached_storages"} ||= {}
$self->{"_attached_storages"}
Truth table100%2/2
A
A= $self->{"_attached_storages"}
Aresult
11
00
54 824 my $key = refaddr($storage);
55 824 ▶ unless (defined $refs->{$key} and refaddr($refs->{$key}) == $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $refs->{$key} and builtin::refaddr($refs->{$key}) == $key) 824 0 824
Condition33%1/3
defined $refs->{$key} and builtin::refaddr($refs->{$key}) == $key
!ll&&!rl&&r
82400
MC/DC0%0/2
defined $refs->{$key} and builtin::refaddr($refs->{$key}) == $key
defined $refs->{$key} builtin::refaddr($refs->{$key}) == $key
Truth table33%1/3
A and B
A= defined $refs->{$key}B= builtin::refaddr($refs->{$key}) == $key
ABresult
0X0
100
111
56 824 $refs->{$key} = $storage;
57 824 weaken $refs->{$key};
58 }
59 }
60 824 return $storage;
61 }
62  
63  
64 81 ▶sub unset_storage {
Subroutine unset_storage: called CC=3 SCAR=12.0
Pod documented
65 81 my ($self, $storage) = @_;
66 81 ▶ if (ref $storage and my $refs = $self->{_attached_storages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $storage and my $refs = $self->{"_attached_storages"}) 81 0 81
Condition33%1/3
ref $storage and my $refs = $self->{"_attached_storages"}
!ll&&!rl&&r
0081
MC/DC0%0/2
ref $storage and my $refs = $self->{"_attached_storages"}
ref $storage my $refs = $self->{"_attached_storages"}
Truth table33%1/3
A and B
A= ref $storageB= my $refs = $self->{"_attached_storages"}
ABresult
0X0
100
111
67 81 delete $refs->{ refaddr($storage) };
68 }
69 81 return $storage;
70 }
71  
72 # The live storages this broker is attached to. Storages that have been freed
73 # are dropped on the way. Private: asking the broker which storages it has is
74 # not an interface; current_connect_info_for is the reader.
75 91 ▶sub _attached_storages {
Subroutine _attached_storages: called CC=4 SCAR=13.9
76 91 my $self = shift;
77 91 ▶ my $refs = $self->{_attached_storages} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $refs = $self->{"_attached_storages"} or return 50 41 91
78 50 for my $key (keys %$refs) {
79 50 ▶ delete $refs->{$key} unless defined $refs->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $refs->{$key} 4 46 50
80 }
81 50 return values %$refs;
82 }
83  
84  
85 # Legacy DBI-shaped interface retained for built-in brokers and compatibility.
86 8 ▶sub connect_info_for {
Subroutine connect_info_for: called CC=1 SCAR=0.0
Pod documented
87 8 croak ref($_[0]) . " must implement connect_info_for()";
88 }
89  
90  
91 # Primary storage-aware interface. Built-in brokers can often derive
92 # storage-native info from the legacy DBI-shaped form, so we provide that
93 # bridge here.
94 1368 ▶sub connect_info_for_storage {
Subroutine connect_info_for_storage: called CC=2 SCAR=6.9
Pod documented
95 1368 my ($self, $storage, $mode) = @_;
96 1368 ▶ $mode //= 'write';
Condition100%2/2
$mode //= "write"
l!l
13644
MC/DC100%1/1
$mode //= "write"
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
97 # Subclasses with storage-native formats override this.
98 # Default: delegate to connect_info_for (DBI-shaped).
99 1368 return $self->connect_info_for($mode);
100 }
101  
102  
103 # Do credentials need rotation?
104 1177 ▶sub needs_refresh { 0 }
Subroutine needs_refresh: called CC=1 SCAR=0.0
Pod documented
105  
106  
107 # Perform credential rotation
108 0 ▶sub refresh { }
Subroutine refresh: not called CC=1 SCAR=0.0
Pod documented
109  
110  
111 # Does this broker rotate credentials over time?
112 27 ▶sub has_rotating_credentials { 0 }
Subroutine has_rotating_credentials: called CC=1 SCAR=0.0
Pod documented
113  
114  
115 # Can transactions safely run through this broker without an explicit override?
116 # A broker only supplies credentials, so the sole safety hazard is credential
117 # rotation mid-transaction. Routing is not a broker concern (see Replicated).
118 49 ▶sub is_transaction_safe {
Subroutine is_transaction_safe: called CC=2 SCAR=6.9
Pod documented
119 49 my $self = shift;
120 49 ▶ return $self->has_rotating_credentials ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_rotating_credentials ? : 37 12 49
121 }
122  
123  
124 # Check refresh and return connect info — legacy convenience for DBI-shaped
125 # callers or brokers already attached to a storage.
126 25 ▶sub current_connect_info_for {
Subroutine current_connect_info_for: called CC=6 SCAR=17.9
Pod documented
127 25 my ($self, $mode, $storage) = @_;
128 25 ▶ $mode //= 'write';
Condition100%2/2
$mode //= "write"
l!l
421
MC/DC100%1/1
$mode //= "write"
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
129  
130 25 ▶ unless ($storage) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storage) 23 2 25
131 23 my @attached = $self->_attached_storages;
132 23 ▶ DBIO::Exception->throw(sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if @attached > 1 4 19 23
133 '%s is attached to %d storages and cannot tell which one '
134 . 'current_connect_info_for is asked for: pass the storage as the '
135 . 'second argument, or use current_connect_info_for_storage($storage)',
136 ref($self), scalar @attached,
137 )) if @attached > 1;
138 19 $storage = $attached[0];
139 }
140  
141 21 ▶ if ($self->needs_refresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->needs_refresh) 3 18 21
142 3 $self->refresh;
143 }
144 20 ▶ return $storage
Branch: 1 branch
BranchTrueFalseTotal
$storage ? : 11 9 20
145 ? $self->connect_info_for_storage($storage, $mode)
146 : $self->connect_info_for($mode);
147 }
148  
149  
150 # Pair this single credential identity with one host, returning a HostBound
151 # view. The view shares this broker's credentials and rotation lifecycle but
152 # reports the given host in its connect info, so one credential can serve many
153 # servers without this broker ever knowing the host list. Accepts a plain host
154 # string or a hashref ({ host => ..., port => ... }).
155 182 ▶sub for_host {
Subroutine for_host: called CC=4 SCAR=13.9
Pod documented
156 182 my ($self, @args) = @_;
157 my %host_args =
158 182 ▶ @args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] }
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 62 120 182
@args == 1 ? : 117 3 120
Condition100%3/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
311762
MC/DC100%2/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
159 : @args == 1 ? (host => $args[0])
160 : @args;
161 182 return DBIO::AccessBroker::HostBound->new(broker => $self, %host_args);
162 }
163  
164  
165 # Check refresh and return storage-native connect info.
166 1434 ▶sub current_connect_info_for_storage {
Subroutine current_connect_info_for_storage: called CC=3 SCAR=11.0
Pod documented
167 1434 my ($self, $storage, $mode) = @_;
168 1434 ▶ $mode //= 'write';
Condition100%2/2
$mode //= "write"
l!l
14304
MC/DC100%1/1
$mode //= "write"
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
169 1434 ▶ if ($self->needs_refresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->needs_refresh) 35 1399 1434
170 35 $self->refresh;
171 }
172 1410 return $self->connect_info_for_storage($storage, $mode);
173 }
174  
175 45 1;
176  
177 __END__
« blib/lib/DBIO/Storage/DateTimeFormat.pm ↑ Summary blib/lib/DBIO/Timestamp.pm »