DBIx-Class-Schema-Config
view release on metacpan or search on metacpan
- Thanks mst and ribasushi for the constant reviews!
- Pushed to CPAN as a stable release
0.1.3:
- connect() now handles passing through valid-looking DBI connect structures.
- _make_config now checks for $user and $pass to be hashrefs, this adds support
for structures like ->connect( 'CONFIG', { hostname => 'db.foo.com' } );
- Added tests to 01_*.t to ensure the new signatures work correctly.
- Updated tests in 06_*.t to use ->connect ('CONFIG', { dbname => ":memory:" )
to be more clear, as opposed to riding ->{user}
- Updated documentation to reflect the changes to the code, namely the hashref
as the second argument, and the statements referring to load_credentials having
responsibility to return normal DBI connect structures.
- Config::Any is only loaded when it's needed.
0.1.2:
- Makefile.PL depends on DBD::SQLite not DBD::SQLite3
- _make_config has a less annoying return
- connection() no longer tries to block ->load_credentials,
it is load_credential's responsablity to to check for credentials
it should allow to fall through.
- Added accessor on_credential_load, it provides access to the config
structure that load_credentials creates, and expects it as the return.
It can be used to make changes to the credentials, such as decrypting
passwords from the config file.
- A new Schema base was created for testing on_credential_load
- New tests added for on_credential_load
0.1.1:
- Replace SUPER:: with next::method
- Don't call load_credentials unless we're actually going to load some
- Move Config::Any into load_credentials to be lazy
- Allow handling of a normal hashref, no ->{options} (Should make handling cleaner)
- Add Testing schema for integration tests
0.1.0:
- Inital Version
# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.017.
Changes
MANIFEST
META.yml
Makefile.PL
README
lib/DBIx/Class/Schema/Config.pm
t/00_load.t
t/01_pass_through.t
t/02_load_credentials.t
t/03_config_paths.t
t/04_integration_test.t
t/05_integration_plugin.t
t/06_on_credential_load.t
t/07_integration_config_files.t
t/08_integration_env.t
t/09_no_modify_config.t
t/etc/config.perl
t/etc/dbic.perl
t/lib/DBIx/Class/Schema/Config/ConfigFiles.pm
---
abstract: 'Manage connection credentials for DBIx::Class::Schema'
author:
- 'Kaitlyn Parkhurst <symkat@symkat.com>'
build_requires:
DBIx::Class: '0.08100'
Data::Dumper: '0'
Test::More: '0.42'
lib: '0'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 0
Makefile.PL view on Meta::CPAN
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.017.
use strict;
use warnings;
use 5.005;
use ExtUtils::MakeMaker;
my %WriteMakefileArgs = (
"ABSTRACT" => "Manage connection credentials for DBIx::Class::Schema",
"AUTHOR" => "Kaitlyn Parkhurst <symkat\@symkat.com>",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "DBIx-Class-Schema-Config",
"LICENSE" => "perl",
"MIN_PERL_VERSION" => "5.005",
"NAME" => "DBIx::Class::Schema::Config",
"PREREQ_PM" => {
"Config::Any" => "0.23",
This archive contains the distribution DBIx-Class-Schema-Config,
version 0.001014:
Manage connection credentials for DBIx::Class::Schema
This software is copyright (c) 2011-2021 by Kaitlyn Parkhurst.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
This README file was generated by Dist::Zilla::Plugin::Readme v6.017.
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
sub connection {
my ( $class, @info ) = @_;
if ( ref($info[0]) eq 'CODE' ) {
return $class->next::method( @info );
}
my $attrs = $class->_make_connect_attrs(@info);
# We will not load credentials for someone who uses dbh_maker,
# however we will pass their request through.
return $class->next::method( $attrs )
if defined $attrs->{dbh_maker};
# Take responsibility for passing through normal-looking
# credentials.
$attrs = $class->load_credentials($attrs)
unless $attrs->{dsn} =~ /^dbi:/i;
return $class->next::method( $attrs );
}
sub coerce_credentials_from_mojolike {
my ( $class, $attrs ) = @_;
(my $in = $attrs->{dsn}) =~ s/^postgresql/http/;
my $url = URI->new( $in );
my $db = ($url->path_segments)[1];
my $dsn = defined $db ? "dbi:Pg:dbname=$db" : 'dbi:Pg:';
$dsn .= ";host=" . $url->host if $url->host;
$dsn .= ";port=" . $url->port if $url->port and $url->port != 80;
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
if ( ! $class->_config ) {
$class->_config( $class->_load_config );
}
return dclone( $class->_config );
}
sub _load_config {
my ( $class ) = @_;
require Config::Any; # Only loaded if we need to load credentials.
# If we have ->config_files, we'll use those and load_files
# instead of the default load_stems.
my %cf_opts = ( use_ext => 1 );
return @{$class->config_files}
? Config::Any->load_files({ files => $class->config_files, %cf_opts })
: Config::Any->load_stems({ stems => $class->config_paths, %cf_opts });
}
sub load_credentials {
my ( $class, $connect_args ) = @_;
# Handle mojo-like postgres:// urls
return $class->coerce_credentials_from_mojolike($connect_args)
if $connect_args->{dsn} =~ /^postgresql:/i;
# While ->connect is responsible for returning normal-looking
# credential information, we do it here as well so that it can be
# independently unit tested.
return $connect_args if $connect_args->{dsn} =~ /^dbi:/i;
return $class->filter_loaded_credentials(
$class->_find_credentials( $connect_args, $class->config ),
$connect_args
);
}
# This will look through the data structure returned by Config::Any
# and return the first instance of the database credentials it can
# find.
sub _find_credentials {
my ( $class, $connect_args, $ConfigAny ) = @_;
for my $cfile ( @$ConfigAny ) {
for my $filename ( keys %$cfile ) {
for my $database ( keys %{$cfile->{$filename}} ) {
if ( $database eq $connect_args->{dsn} ) {
return $cfile->{$filename}->{$database};
}
}
}
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
sub get_env_vars {
return $ENV{DBIX_CONFIG_DIR} . "/dbic" if exists $ENV{DBIX_CONFIG_DIR};
return ();
}
# Intended to be sub-classed, the default behavior is to
# overwrite the loaded configuration with any specified
# configuration from the connect() call, with the exception
# of the DSN itself.
sub filter_loaded_credentials {
my ( $class, $new, $old ) = @_;
local $old->{password}, delete $old->{password} unless $old->{password};
local $old->{user}, delete $old->{user} unless $old->{user};
local $old->{dsn}, delete $old->{dsn};
return merge( $old, $new );
};
__PACKAGE__->mk_classaccessor('config_paths');
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
=encoding UTF-8
=head1 NAME
DBIx::Class::Schema::Config - Credential Management for DBIx::Class
=head1 DESCRIPTION
DBIx::Class::Schema::Config is a subclass of DBIx::Class::Schema that allows
the loading of credentials & configuration from a file. The actual code itself
would only need to know about the name used in the configuration file. This
aims to make it simpler for operations teams to manage database credentials.
A simple tutorial that compliments this documentation and explains converting
an existing DBIx::Class Schema to use this software to manage credentials can
be found at L<http://www.symkat.com/credential-management-in-dbix-class>
=head1 SYNOPSIS
/etc/dbic.yaml
MY_DATABASE:
dsn: "dbi:Pg:host=localhost;database=blog"
user: "TheDoctor"
password: "dnoPydoleM"
TraceLevel: 1
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
to change the paths that are searched. For example:
package My::Schema
use warnings;
use strict;
use base 'DBIx::Class::Schema::Config';
__PACKAGE__->config_paths([( '/var/www/secret/dbic', '/opt/database' )]);
The above code would have I</var/www/secret/dbic.*> and I</opt/database.*>
searched, in that order. As above, the first credentials found would be used.
This will replace the files originally searched for, not add to them.
=head1 USE SPECIFIC CONFIG FILES
If you would rather explicitly state the configuration files you
want loaded, you can use the class accessor C<config_files>
instead.
package My::Schema
use warnings;
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
The name of the connection to load from the configuration file is still given
as the first argument, while other arguments may be given exactly as you would
for any other call to C<connect>.
Historical Note: This class accepts numerous ways to connect to DBIC that would
otherwise not be valid. These connection methods are discouraged but tested for
and kept for compatibility with earlier versions. For valid ways of connecting to DBIC
please see L<https://metacpan.org/pod/DBIx::Class::Storage::DBI#connect_info>
=head2 filter_loaded_credentials
Override this function if you want to change the loaded credentials before
they are passed to DBIC. This is useful for use-cases that include decrypting
encrypted passwords or making programmatic changes to the configuration before
using it.
sub filter_loaded_credentials {
my ( $class, $loaded_credentials, $connect_args ) = @_;
...
return $loaded_credentials;
}
C<$loaded_credentials> is the structure after it has been loaded from the
configuration file. In this case, C<$loaded_credentials-E<gt>{user}> eq
B<WalterWhite> and C<$loaded_credentials-E<gt>{dsn}> eq
B<DBI:mysql:database=students;host=%s;port=3306>.
C<$connect_args> is the structure originally passed on C<-E<gt>connect()>
after it has been turned into a hash. For instance,
C<-E<gt>connect('DATABASE', 'USERNAME')> will result in
C<$connect_args-E<gt>{dsn}> eq B<DATABASE> and C<$connect_args-E<gt>{user}>
eq B<USERNAME>.
Additional parameters can be added by appending a hashref,
to the connection call, as an example, C<-E<gt>connect( 'CONFIG',
lib/DBIx/Class/Schema/Config.pm view on Meta::CPAN
user: "WalterWhite"
password: "relykS"
In your Schema class, you could include the following:
package My::Schema
use warnings;
use strict;
use base 'DBIx::Class::Schema::Config';
sub filter_loaded_credentials {
my ( $class, $loaded_credentials, $connect_args ) = @_;
if ( $loaded_credentials->{dsn} =~ /\%s/ ) {
$loaded_credentials->{dsn} = sprintf( $loaded_credentials->{dsn},
$connect_args->{hostname});
}
}
__PACKAGE__->load_classes;
1;
Then the connection could be done with
C<$Schema-E<gt>connect('DATABASE', { hostname => 'my.hostname.com' });>
See L</load_credentials> for more complex changes that require changing
how the configuration itself is loaded.
=head2 load_credentials
Override this function to change the way that L<DBIx::Class::Schema::Config>
loads credentials. The function takes the class name, as well as a hashref.
If you take the route of having C<-E<gt>connect('DATABASE')> used as a key for
whatever configuration you are loading, I<DATABASE> would be
C<$config-E<gt>{dsn}>
Some::Schema->connect(
"SomeTarget",
"Yuri",
"Yawny",
{
TraceLevel => 1
}
);
Would result in the following data structure as $config in
C<load_credentials($class, $config)>:
{
dsn => "SomeTarget",
user => "Yuri",
password => "Yawny",
TraceLevel => 1,
}
Currently, load_credentials will NOT be called if the first argument to
C<-E<gt>connect()> looks like a valid DSN. This is determined by match
the DSN with C</^dbi:/i>.
The function should return the same structure. For instance:
package My::Schema
use warnings;
use strict;
use base 'DBIx::Class::Schema::Config';
use LWP::Simple;
use JSON
# Load credentials from internal web server.
sub load_credentials {
my ( $class, $config ) = @_;
return decode_json(
get( "http://someserver.com/v1.0/database?key=somesecret&db=" .
$config->{dsn} ));
}
__PACKAGE__->load_classes;
=head1 AUTHOR
t/02_load_credentials.t view on Meta::CPAN
},
title => "Mojo-like postgresql:// connect line, ambiguous user@ is username.",
},
{
put => [ 'postgresql://hostname/minion' ],
get => {
dsn => 'dbi:Pg:dbname=minion;host=hostname',
user => '',
password => '',
},
title => "Mojo-like postgresql:// connect line, no credentials.",
},
];
for my $test ( @$tests ) {
is_deeply(
DBIx::Class::Schema::Config->load_credentials(
DBIx::Class::Schema::Config->_make_connect_attrs(
ref $test->{put} eq 'ARRAY' ? @{$test->{put}} : $test->{put})
), $test->{get}, $test->{title} );
}
done_testing;
t/lib/DBIx/Class/Schema/Config/Plugin.pm view on Meta::CPAN
package DBIx::Class::Schema::Config::Plugin;
use strict;
use warnings;
use base 'DBIx::Class::Schema::Config';
__PACKAGE__->config_paths( [ ( 't/etc/config' ) ] );
sub filter_loaded_credentials {
my ( $class, $new, $orig ) = @_;
if ( $new->{dsn} =~ /\%s/ ) {
$new->{dsn} = sprintf($new->{dsn}, $orig->{dbname});
}
return $new;
}
__PACKAGE__->load_classes;
1;
( run in 0.287 second using v1.01-cache-2.11-cpan-4d50c553e7e )