DBIO
view release on metacpan or search on metacpan
lib/DBIO/AccessBroker.pm view on Meta::CPAN
? $self->connect_info_for_storage($storage, $mode)
: $self->connect_info_for($mode);
}
# Pair this single credential identity with one host, returning a HostBound
# view. The view shares this broker's credentials and rotation lifecycle but
# reports the given host in its connect info, so one credential can serve many
# servers without this broker ever knowing the host list. Accepts a plain host
# string or a hashref ({ host => ..., port => ... }).
sub for_host {
my ($self, @args) = @_;
my %host_args =
@args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] }
: @args == 1 ? (host => $args[0])
: @args;
return DBIO::AccessBroker::HostBound->new(broker => $self, %host_args);
}
# Check refresh and return storage-native connect info.
sub current_connect_info_for_storage {
my ($self, $storage, $mode) = @_;
$mode //= 'write';
if ($self->needs_refresh) {
$self->refresh;
}
return $self->connect_info_for_storage($storage, $mode);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
DBIO::AccessBroker - Credential lifecycle for DBIO connections
=head1 VERSION
version 0.900007
=head1 SYNOPSIS
# Static â one fixed set of connection parameters
use DBIO::AccessBroker::Static;
my $broker = DBIO::AccessBroker::Static->new(
dbname => 'myapp',
username => 'app', password => 'secret',
);
# Storage gets storage-native connect info
my $info = $broker->current_connect_info_for_storage($schema->storage);
# Vault â rotating credentials from OpenBao/Vault
use DBIO::AccessBroker::Vault;
my $broker = DBIO::AccessBroker::Vault->new(
vault => WWW::OpenBao->new(endpoint => 'http://vault:8200', token => $token),
dsn => 'dbi:Pg:dbname=myapp;host=db',
cred_path => 'database/creds/myapp',
ttl => 3600, # credentials valid for 1 hour
refresh_margin => 900, # refresh 15 min before expiry
);
# DBIO can now connect directly with a broker
my $schema = MyApp::Schema->connect($broker);
# the async mode is the connect call's, beside the broker
my $async_schema = MyApp::Schema->connect($broker, { async => 'future_io' });
# so are the other connect options -- the broker answers which database,
# and as whom (see DBIO::Storage::DBI/connect_info)
my $schema = MyApp::Schema->connect($broker, {
quote_names => 0,
on_connect_do => [ 'SET search_path TO app' ],
});
See F<t/access_broker/> for a runnable example.
=head1 DESCRIPTION
AccessBroker is a B<CredentialSource>: it supplies the connect info for
exactly one backend identity (one set of credentials). It is
B<storage-agnostic> â it returns connection parameters, not handles â
so it works with both C<Storage::DBI> (sync) and C<Storage::Async>
(async/Future-based). It handles:
=over 4
=item * B<Credential lifecycle> â fetching, rotating, and caching database credentials
=back
A broker does B<not> route, and it does B<not> own a host list. Read/write
routing and the master/replicant topology belong to L<DBIO::Replicated>. One
credential can serve many servers via a L</for_host> view, which pairs this
single identity with one host at connect time.
=head1 METHODS
=head2 set_storage
$broker->set_storage($storage);
Attaches this broker to a storage instance, so that a subsequent
L</current_connect_info_for> knows which storage to hand to
C<connect_info_for_storage>. (L</current_connect_info_for_storage> is given
its storage as an argument and does not read this one.) Called by C<Storage>
when the broker is attached; not normally called directly.
A broker keeps a reference to B<every> storage it is attached to:
a synchronous owner and its embedded async backend, or the master and the
replicants of L<DBIO::Replicated>, are several storages of one broker.
Setting a storage adds it; storages already attached stay. Setting the same
storage again changes nothing.
The storages are held B<weakly>: the storage holds its broker, not the other
way round. Held strongly, the two would keep each other alive, and a storage
dropped with a broker attached would never close its connection.
( run in 1.152 second using v1.01-cache-2.11-cpan-eb134014566 )